Skip to content

Commit

Permalink
Reject improper use of command-line options
Browse files Browse the repository at this point in the history
We already did diagnose these (via 'getopt_long'), but then proceeded with
execution.
  • Loading branch information
tschwinge committed Nov 16, 2022
1 parent 7a8e9fb commit d93bdb4
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 43 deletions.
38 changes: 23 additions & 15 deletions nvptx-as.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,26 @@ program_available (const char *progname)
return false;
}

ATTRIBUTE_NORETURN static void
usage (FILE *stream, int status)
{
fprintf (stream, "\
Usage: nvptx-none-as [option...] [asmfile]\n\
Options:\n\
-m TARGET Override target architecture used for ptxas\n\
verification (default: deduce from input's preamble)\n\
-o FILE Write output to FILE\n\
-v Be verbose\n\
--verify Do verify output is acceptable to ptxas\n\
--no-verify Do not verify output is acceptable to ptxas\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (status);
}

static struct option long_options[] = {
{"traditional-format", no_argument, 0, 0 },
{"save-temps", no_argument, 0, 0 },
Expand Down Expand Up @@ -1144,21 +1164,8 @@ main (int argc, char **argv)
/* Ignore include paths. */
break;
case 'h':
printf ("\
Usage: nvptx-none-as [option...] [asmfile]\n\
Options:\n\
-m TARGET Override target architecture used for ptxas\n\
verification (default: deduce from input's preamble)\n\
-o FILE Write output to FILE\n\
-v Be verbose\n\
--verify Do verify output is acceptable to ptxas\n\
--no-verify Do not verify output is acceptable to ptxas\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (0);
usage (stdout, 0);
break;
case 'V':
printf ("\
nvptx-none-as %s%s\n\
Expand All @@ -1170,6 +1177,7 @@ This program has absolutely no warranty.\n",
PKGVERSION, NVPTX_TOOLS_VERSION, "2015");
exit (0);
default:
usage (stderr, 1);
break;
}
}
Expand Down
34 changes: 21 additions & 13 deletions nvptx-ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,24 @@ process_refs_defs (file *f, const char *ptx)
}
}

ATTRIBUTE_NORETURN static void
usage (FILE *stream, int status)
{
fprintf (stream, "\
Usage: nvptx-none-ld [option...] [files]\n\
Options:\n\
-o FILE Write output to FILE\n\
-v Be verbose\n\
-l LIBRARY Link with LIBRARY\n\
-L DIR Search for libraries in DIR\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (status);
}

static const struct option long_options[] = {
{"help", no_argument, 0, 'h' },
{"version", no_argument, 0, 'V' },
Expand Down Expand Up @@ -375,19 +393,8 @@ main (int argc, char **argv)
libpaths.push_back (optarg);
break;
case 'h':
printf ("\
Usage: nvptx-none-ld [option...] [files]\n\
Options:\n\
-o FILE Write output to FILE\n\
-v Be verbose\n\
-l LIBRARY Link with LIBRARY\n\
-L DIR Search for libraries in DIR\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (0);
usage (stdout, 0);
break;
case 'V':
printf ("\
nvptx-none-ld %s%s\n\
Expand All @@ -399,6 +406,7 @@ This program has absolutely no warranty.\n",
PKGVERSION, NVPTX_TOOLS_VERSION, "2015");
exit (0);
default:
usage (stderr, 1);
break;
}
}
Expand Down
41 changes: 26 additions & 15 deletions nvptx-run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extern "C" CUresult cuGetErrorName (CUresult, const char **);
extern "C" CUresult cuGetErrorString (CUresult, const char **);
#endif

#define HAVE_DECL_BASENAME 1
#include "libiberty.h"

#include "version.h"

#define DO_PRAGMA(x) _Pragma (#x)
Expand Down Expand Up @@ -205,6 +208,26 @@ compile_file (FILE *f, CUmodule *phModule, CUfunction *phKernel)
fatal_unless_success (r, "could not find kernel __main");
}

ATTRIBUTE_NORETURN static void
usage (FILE *stream, int status)
{
fprintf (stream, "\
Usage: nvptx-none-run [option...] program [argument...]\n\
Options:\n\
-S, --stack-size N Set per-lane GPU stack size to N (default: auto)\n\
-H, --heap-size N Set GPU heap size to N (default: 256 MiB)\n\
-L, --lanes N Launch N lanes (for testing gcc -muniform-simt)\n\
-O, --optlevel N Pass PTX JIT option to set optimization level N\n\
-g, --lineinfo Pass PTX JIT option to generate line information\n\
-G, --debuginfo Pass PTX JIT option to generate debug information\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (status);
}

static const struct option long_options[] =
{
{ "stack-size", required_argument, 0, 'S' },
Expand Down Expand Up @@ -254,21 +277,8 @@ main (int argc, char **argv)
jitopt_debuginfo = 1;
break;
case 'h':
printf ("\
Usage: nvptx-none-run [option...] program [argument...]\n\
Options:\n\
-S, --stack-size N Set per-lane GPU stack size to N (default: auto)\n\
-H, --heap-size N Set GPU heap size to N (default: 256 MiB)\n\
-L, --lanes N Launch N lanes (for testing gcc -muniform-simt)\n\
-O, --optlevel N Pass PTX JIT option to set optimization level N\n\
-g, --lineinfo Pass PTX JIT option to generate line information\n\
-G, --debuginfo Pass PTX JIT option to generate debug information\n\
--help Print this help and exit\n\
--version Print version number and exit\n\
\n\
Report bugs to %s.\n",
REPORT_BUGS_TO);
exit (0);
usage (stdout, 0);
break;
case 'V':
printf ("\
nvptx-none-run %s%s\n\
Expand All @@ -280,6 +290,7 @@ This program has absolutely no warranty.\n",
PKGVERSION, NVPTX_TOOLS_VERSION, "2015");
exit (0);
default:
usage (stderr, 1);
break;
}
}
Expand Down
37 changes: 37 additions & 0 deletions test/as/options-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Improper use of command-line options.


invalid option -- '/'

RUN: %target_as_cmd -/ > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-as '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option requires an argument -- 'o'

RUN: %target_as_cmd -o > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-as '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


unrecognized option '--holp'

RUN: %target_as_cmd --holp > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-as '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option '--help' doesn't allow an argument

RUN: %target_as_cmd --help=abc > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-as '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout
37 changes: 37 additions & 0 deletions test/ld/options-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Improper use of command-line options.


invalid option -- '/'

RUN: %target_ld_cmd -/ > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-ld '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option requires an argument -- 'o'

RUN: %target_ld_cmd -o > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-ld '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


unrecognized option '--holp'

RUN: %target_ld_cmd --holp > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-ld '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option '--help' doesn't allow an argument

RUN: %target_ld_cmd --help=abc > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-ld '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout
46 changes: 46 additions & 0 deletions test/run/options-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Improper use of command-line options.


invalid option -- '/'

RUN: %target_run_cmd -/ > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-run '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option requires an argument -- 'o'

RUN: %target_run_cmd -o > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-run '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


unrecognized option '--holp'

RUN: %target_run_cmd --holp > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-run '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option '--help' doesn't allow an argument

RUN: %target_run_cmd --help=abc > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-run '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout


option '--stack-size' requires an argument

RUN: %target_run_cmd --stack-size > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
RUN: test -s %t.stderr
RUN: < %t.stderr grep -qe '^Usage: nvptx-none-run '
RUN: test -f %t.stdout
RUN: ! test -s %t.stdout

0 comments on commit d93bdb4

Please sign in to comment.