diff options
Diffstat (limited to 'ruby.c')
| -rw-r--r-- | ruby.c | 515 |
1 files changed, 184 insertions, 331 deletions
@@ -15,10 +15,8 @@ #include <windows.h> #include <sys/cygwin.h> #endif -#include "ruby/encoding.h" -#include "ruby/thread.h" -#include "ruby/version.h" #include "internal.h" +#include "ruby/thread.h" #include "eval_intern.h" #include "dln.h" #include <stdio.h> @@ -52,10 +50,6 @@ #include "ruby/util.h" -#include "mjit.h" - -void Init_ruby_description(void); - #ifndef HAVE_STDLIB_H char *getenv(); #endif @@ -69,8 +63,6 @@ char *getenv(); #define DEFAULT_RUBYGEMS_ENABLED "enabled" #endif -void rb_warning_category_update(unsigned int mask, unsigned int bits); - #define COMMA , #define FEATURE_BIT(bit) (1U << feature_##bit) #define EACH_FEATURES(X, SEP) \ @@ -81,8 +73,6 @@ void rb_warning_category_update(unsigned int mask, unsigned int bits); X(rubyopt) \ SEP \ X(frozen_string_literal) \ - SEP \ - X(jit) \ /* END OF FEATURES */ #define EACH_DEBUG_FEATURES(X, SEP) \ X(frozen_string_literal) \ @@ -131,24 +121,6 @@ enum dump_flag_bits { typedef struct ruby_cmdline_options ruby_cmdline_options_t; -typedef struct { - unsigned int mask; - unsigned int set; -} ruby_features_t; - -static inline void -rb_feature_set_to(ruby_features_t *feat, unsigned int bit_mask, unsigned int bit_set) -{ - feat->mask |= bit_mask; - feat->set = (feat->set & ~bit_mask) | bit_set; -} - -#define FEATURE_SET_TO(feat, bit_mask, bit_set) \ - rb_feature_set_to(&(feat), bit_mask, bit_set) -#define FEATURE_SET(feat, bits) FEATURE_SET_TO(feat, bits, bits) -#define FEATURE_SET_RESTORE(feat, save) FEATURE_SET_TO(feat, (save).mask, (save).set & (save).mask) -#define FEATURE_SET_P(feat, bits) ((feat).set & (bits)) - struct ruby_cmdline_options { const char *script; VALUE script_name; @@ -160,12 +132,9 @@ struct ruby_cmdline_options { } enc; } src, ext, intern; VALUE req_list; - ruby_features_t features; - ruby_features_t warn; + unsigned int features; unsigned int dump; -#if USE_MJIT - struct mjit_options mjit; -#endif + int safe_level; int sflag, xflag; unsigned int warning: 1; unsigned int verbose: 1; @@ -193,7 +162,6 @@ enum { & ~FEATURE_BIT(gems) #endif & ~FEATURE_BIT(frozen_string_literal) - & ~FEATURE_BIT(jit) ) }; @@ -205,10 +173,7 @@ cmdline_options_init(ruby_cmdline_options_t *opt) opt->src.enc.index = src_encoding_index; opt->ext.enc.index = -1; opt->intern.enc.index = -1; - opt->features.set = DEFAULT_FEATURES; -#ifdef MJIT_FORCE_ENABLE /* to use with: ./configure cppflags="-DMJIT_FORCE_ENABLE" */ - opt->features.set |= FEATURE_BIT(jit); -#endif + opt->features = DEFAULT_FEATURES; return opt; } @@ -227,7 +192,7 @@ static void show_usage_line(const char *str, unsigned int namelen, unsigned int secondlen, int help) { const unsigned int w = 16; - const int wrap = help && namelen + secondlen - 1 > w; + const int wrap = help && namelen + secondlen - 2 > w; printf(" %.*s%-*.*s%-*s%s\n", namelen-1, str, (wrap ? 0 : w - namelen + 1), (help ? secondlen-1 : 0), str + namelen, @@ -267,12 +232,11 @@ usage(const char *name, int help) M("-rlibrary", "", "require the library before executing your script"), M("-s", "", "enable some switch parsing for switches after script name"), M("-S", "", "look for the script using PATH environment variable"), - M("-v", "", "print the version number, then turn on verbose mode"), + M("-T[level=1]", "", "turn on tainting checks"), + M("-v", ", --verbose", "print version number, then turn on verbose mode"), M("-w", "", "turn warnings on for your script"), - M("-W[level=2|:category]", "", "set warning level; 0=silence, 1=medium, 2=verbose"), + M("-W[level=2]", "", "set warning level; 0=silence, 1=medium, 2=verbose"), M("-x[directory]", "", "strip off text before #!ruby line and perhaps cd to directory"), - M("--jit", "", "enable JIT with default options (experimental)"), - M("--jit-[option]","", "enable JIT with an option (experimental)"), M("-h", "", "show this message, --help for more info"), }; static const struct message help_msg[] = { @@ -283,8 +247,7 @@ usage(const char *name, int help) "enable or disable features. see below for available features"), M("--external-encoding=encoding", ", --internal-encoding=encoding", "specify the default external or internal character encoding"), - M("--verbose", "", "turn on verbose mode and disable script from stdin"), - M("--version", "", "print the version number, then exit"), + M("--version", "", "print the version"), M("--help", "", "show this message, -h for short message"), }; static const struct message dumps[] = { @@ -298,20 +261,6 @@ usage(const char *name, int help) M("did_you_mean", "", "did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED")"), M("rubyopt", "", "RUBYOPT environment variable (default: enabled)"), M("frozen-string-literal", "", "freeze all string literals (default: disabled)"), - M("jit", "", "JIT compiler (default: disabled)"), - }; - static const struct message warn_categories[] = { - M("deprecated", "", "deprecated features"), - M("experimental", "", "experimental features"), - }; - static const struct message mjit_options[] = { - M("--jit-warnings", "", "Enable printing JIT warnings"), - M("--jit-debug", "", "Enable JIT debugging (very slow), or add cflags if specified"), - M("--jit-wait", "", "Wait until JIT compilation finishes every time (for testing)"), - M("--jit-save-temps", "", "Save JIT temporary files in $TMP or /tmp (for testing)"), - M("--jit-verbose=num", "", "Print JIT logs of level num or less to stderr (default: 0)"), - M("--jit-max-cache=num", "", "Max number of methods to be JIT-ed in a cache (default: 100)"), - M("--jit-min-calls=num", "", "Number of calls to trigger JIT (for testing, default: 10000)"), }; int i; const int num = numberof(usage_msg) - (help ? 1 : 0); @@ -331,12 +280,6 @@ usage(const char *name, int help) puts("Features:"); for (i = 0; i < numberof(features); ++i) SHOW(features[i]); - puts("Warning categories:"); - for (i = 0; i < numberof(warn_categories); ++i) - SHOW(warn_categories[i]); - puts("JIT options (experimental):"); - for (i = 0; i < numberof(mjit_options); ++i) - SHOW(mjit_options[i]); } #define rubylib_path_new rb_str_new @@ -499,61 +442,22 @@ str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to) # define str_conv_enc(str, from, to) (str) #endif -void ruby_init_loadpath(void); +void ruby_init_loadpath_safe(int safe_level); -#if defined(LOAD_RELATIVE) -static VALUE -runtime_libruby_path(void) +void +ruby_init_loadpath(void) { -#if defined _WIN32 || defined __CYGWIN__ - DWORD len = RSTRING_EMBED_LEN_MAX, ret; - VALUE path; - VALUE wsopath = rb_str_new(0, len*sizeof(WCHAR)); - WCHAR *wlibpath; - char *libpath; + ruby_init_loadpath_safe(0); +} - while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath), - ret = GetModuleFileNameW(libruby, wlibpath, len), - (ret == len)) - { - rb_str_modify_expand(wsopath, len*sizeof(WCHAR)); - rb_str_set_len(wsopath, (len += len)*sizeof(WCHAR)); - } - if (!ret || ret > len) rb_fatal("failed to get module file name"); -#if defined __CYGWIN__ - { - const int win_to_posix = CCP_WIN_W_TO_POSIX | CCP_RELATIVE; - size_t newsize = cygwin_conv_path(win_to_posix, wlibpath, 0, 0); - if (!newsize) rb_fatal("failed to convert module path to cygwin"); - path = rb_str_new(0, newsize); - libpath = RSTRING_PTR(path); - if (cygwin_conv_path(win_to_posix, wlibpath, libpath, newsize)) { - rb_str_resize(path, 0); - } - } -#else - { - DWORD i; - for (len = ret, i = 0; i < len; ++i) { - if (wlibpath[i] == L'\\') { - wlibpath[i] = L'/'; - ret = i+1; /* chop after the last separator */ - } - } - } - len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL); - path = rb_utf8_str_new(0, len); - libpath = RSTRING_PTR(path); - WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL); -#endif - rb_str_resize(wsopath, 0); - return path; -#elif defined(HAVE_DLADDR) +#if defined(LOAD_RELATIVE) && defined(HAVE_DLADDR) && !defined(__CYGWIN__) +static VALUE +dladdr_path(const void* addr) +{ Dl_info dli; VALUE fname, path; - const void* addr = (void *)(VALUE)expand_include_path; - if (!dladdr((void *)addr, &dli)) { + if (!dladdr(addr, &dli)) { return rb_str_new(0, 0); } #ifdef __linux__ @@ -568,66 +472,102 @@ runtime_libruby_path(void) } rb_str_resize(fname, 0); return path; -#else -# error relative load path is not supported on this platform. -#endif } #endif #define INITIAL_LOAD_PATH_MARK rb_intern_const("@gem_prelude_index") -VALUE ruby_archlibdir_path, ruby_prefix_path; - void -ruby_init_loadpath(void) +ruby_init_loadpath_safe(int safe_level) { - VALUE load_path, archlibdir = 0; + VALUE load_path; ID id_initial_load_path_mark; const char *paths = ruby_initial_load_paths; #if defined LOAD_RELATIVE -#if !defined ENABLE_MULTIARCH -# define RUBY_ARCH_PATH "" -#elif defined RUBY_ARCH -# define RUBY_ARCH_PATH "/"RUBY_ARCH -#else -# define RUBY_ARCH_PATH "/"RUBY_PLATFORM -#endif +# if defined HAVE_DLADDR || defined __CYGWIN__ || defined _WIN32 +# define VARIABLE_LIBPATH 1 +# else +# define VARIABLE_LIBPATH 0 +# endif +# if VARIABLE_LIBPATH char *libpath; VALUE sopath; +# else + char libpath[MAXPATHLEN + 1]; +# endif size_t baselen; - const char *p; + char *p; - sopath = runtime_libruby_path(); +#if defined _WIN32 || defined __CYGWIN__ + { + DWORD len = RSTRING_EMBED_LEN_MAX, ret, i; + VALUE wsopath = rb_str_new(0, len*sizeof(WCHAR)); + WCHAR *wlibpath; + while (wlibpath = (WCHAR *)RSTRING_PTR(wsopath), + ret = GetModuleFileNameW(libruby, wlibpath, len), + (ret == len)) + { + rb_str_modify_expand(wsopath, len*sizeof(WCHAR)); + rb_str_set_len(wsopath, (len += len)*sizeof(WCHAR)); + } + if (!ret || ret > len) rb_fatal("failed to get module file name"); + for (len = ret, i = 0; i < len; ++i) { + if (wlibpath[i] == L'\\') { + wlibpath[i] = L'/'; + ret = i+1; /* chop after the last separator */ + } + } + len = WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, NULL, 0, NULL, NULL); + sopath = rb_utf8_str_new(0, len); + libpath = RSTRING_PTR(sopath); + WideCharToMultiByte(CP_UTF8, 0, wlibpath, ret, libpath, len, NULL, NULL); + rb_str_resize(wsopath, 0); + } +#elif defined(HAVE_DLADDR) + sopath = dladdr_path((void *)(VALUE)expand_include_path); libpath = RSTRING_PTR(sopath); +#endif +#if !VARIABLE_LIBPATH + libpath[sizeof(libpath) - 1] = '\0'; +#endif +#if defined DOSISH && !defined _WIN32 + translit_char(libpath, '\\', '/'); +#elif defined __CYGWIN__ + { + const int win_to_posix = CCP_WIN_A_TO_POSIX | CCP_RELATIVE; + size_t newsize = cygwin_conv_path(win_to_posix, libpath, 0, 0); + if (newsize > 0) { + VALUE rubylib = rb_str_new(0, newsize); + p = RSTRING_PTR(rubylib); + if (cygwin_conv_path(win_to_posix, libpath, p, newsize) == 0) { + rb_str_resize(sopath, 0); + sopath = rubylib; + libpath = p; + } + } + } +#endif p = strrchr(libpath, '/'); if (p) { - static const char libdir[] = "/" + static const char bindir[] = "/bin"; #ifdef LIBDIR_BASENAME - LIBDIR_BASENAME + static const char libdir[] = "/"LIBDIR_BASENAME; #else - "lib" + static const char libdir[] = "/lib"; #endif - RUBY_ARCH_PATH; - const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - - rb_strlen_lit(RUBY_ARCH_PATH) - 1; - static const char bindir[] = "/bin"; const ptrdiff_t bindir_len = (ptrdiff_t)sizeof(bindir) - 1; - - const char *p2 = NULL; + const ptrdiff_t libdir_len = (ptrdiff_t)sizeof(libdir) - 1; #ifdef ENABLE_MULTIARCH + char *p2 = NULL; + multiarch: #endif if (p - libpath >= bindir_len && !STRNCASECMP(p - bindir_len, bindir, bindir_len)) { p -= bindir_len; - archlibdir = rb_str_subseq(sopath, 0, p - libpath); - rb_str_cat_cstr(archlibdir, libdir); - OBJ_FREEZE_RAW(archlibdir); } else if (p - libpath >= libdir_len && !strncmp(p - libdir_len, libdir, libdir_len)) { - archlibdir = rb_str_subseq(sopath, 0, (p2 ? p2 : p) - libpath); - OBJ_FREEZE_RAW(archlibdir); p -= libdir_len; } #ifdef ENABLE_MULTIARCH @@ -641,28 +581,37 @@ ruby_init_loadpath(void) p = p2; } #endif +#if !VARIABLE_LIBPATH + *p = 0; +#endif + } +#if !VARIABLE_LIBPATH + else { + strlcpy(libpath, ".", sizeof(libpath)); + p = libpath + 1; } baselen = p - libpath; +#define PREFIX_PATH() rb_str_new(libpath, baselen) +#else + baselen = p - libpath; rb_str_resize(sopath, baselen); libpath = RSTRING_PTR(sopath); #define PREFIX_PATH() sopath +#endif + #define BASEPATH() rb_str_buf_cat(rb_str_buf_new(baselen+len), libpath, baselen) + #define RUBY_RELATIVE(path, len) rb_str_buf_cat(BASEPATH(), (path), (len)) #else const size_t exec_prefix_len = strlen(ruby_exec_prefix); #define RUBY_RELATIVE(path, len) rubylib_path_new((path), (len)) #define PREFIX_PATH() RUBY_RELATIVE(ruby_exec_prefix, exec_prefix_len) #endif - rb_gc_register_address(&ruby_prefix_path); - ruby_prefix_path = PREFIX_PATH(); - OBJ_FREEZE_RAW(ruby_prefix_path); - if (!archlibdir) archlibdir = ruby_prefix_path; - rb_gc_register_address(&ruby_archlibdir_path); - ruby_archlibdir_path = archlibdir; - load_path = GET_VM()->load_path; - ruby_push_include(getenv("RUBYLIB"), identical_path); + if (safe_level == 0) { + ruby_push_include(getenv("RUBYLIB"), identical_path); + } id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK; while (*paths) { @@ -673,7 +622,7 @@ ruby_init_loadpath(void) paths += len + 1; } - rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), ruby_prefix_path); + rb_const_set(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX"), rb_obj_freeze(PREFIX_PATH())); } @@ -684,9 +633,11 @@ add_modules(VALUE *req_list, const char *mod) VALUE feature; if (!list) { - *req_list = list = rb_ary_tmp_new(0); + *req_list = list = rb_ary_new(); + RBASIC_CLEAR_CLASS(list); } - feature = rb_str_cat_cstr(rb_str_tmp_new(0), mod); + feature = rb_str_new2(mod); + RBASIC_CLEAR_CLASS(feature); rb_ary_push(list, feature); } @@ -788,7 +739,6 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) char **argv, *p; const char *ap = 0; VALUE argstr, argary; - void *ptr; while (ISSPACE(*s)) s++; if (!*s) return; @@ -811,8 +761,7 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) argc = RSTRING_LEN(argary) / sizeof(ap); ap = 0; rb_str_cat(argary, (char *)&ap, sizeof(ap)); - argv = ptr = ALLOC_N(char *, argc); - MEMMOVE(argv, RSTRING_PTR(argary), char *, argc); + argv = (char **)RSTRING_PTR(argary); while ((i = proc_options(argc, argv, opt, envopt)) > 1 && envopt && (argc -= i) > 0) { argv += i; @@ -825,7 +774,6 @@ moreswitches(const char *s, ruby_cmdline_options_t *opt, int envopt) } } - ruby_xfree(ptr); /* get rid of GC */ rb_str_resize(argary, 0); rb_str_resize(argstr, 0); @@ -835,7 +783,7 @@ static int name_match_p(const char *name, const char *str, size_t len) { if (len == 0) return 0; - while (1) { + do { while (TOLOWER(*str) == *name) { if (!--len || !*++str) return 1; ++name; @@ -845,7 +793,8 @@ name_match_p(const char *name, const char *str, size_t len) if (*name != '-' && *name != '_') return 0; ++name; ++str; - } + } while (len > 0); + return !*name; } #define NAME_MATCH_P(name, str, len) \ @@ -869,21 +818,21 @@ static void feature_option(const char *str, int len, void *arg, const unsigned int enable) { static const char list[] = EACH_FEATURES(LITERAL_NAME_ELEMENT, ", "); - ruby_features_t *argp = arg; + unsigned int *argp = arg; unsigned int mask = ~0U; - unsigned int set = 0U; #if AMBIGUOUS_FEATURE_NAMES + unsigned int set = 0U; int matched = 0; -# define FEATURE_FOUND ++matched +#define SET_FEATURE(bit) \ + if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); ++matched;} #else -# define FEATURE_FOUND goto found -#endif #define SET_FEATURE(bit) \ - if (NAME_MATCH_P(#bit, str, len)) {set |= mask = FEATURE_BIT(bit); FEATURE_FOUND;} + if (NAME_MATCH_P(#bit, str, len)) {mask = FEATURE_BIT(bit); goto found;} +#endif EACH_FEATURES(SET_FEATURE, ;); if (NAME_MATCH_P("all", str, len)) { found: - FEATURE_SET_TO(*argp, mask, (mask & enable)); + *argp = (*argp & ~mask) | (mask & enable); return; } #if AMBIGUOUS_FEATURE_NAMES @@ -925,12 +874,7 @@ static void debug_option(const char *str, int len, void *arg) { static const char list[] = EACH_DEBUG_FEATURES(LITERAL_NAME_ELEMENT, ", "); - ruby_features_t *argp = arg; -#define SET_WHEN_DEBUG(bit) \ - if (NAME_MATCH_P(#bit, str, len)) { \ - FEATURE_SET(*argp, DEBUG_BIT(bit)); \ - return; \ - } +#define SET_WHEN_DEBUG(bit) SET_WHEN(#bit, DEBUG_BIT(bit), str, len) EACH_DEBUG_FEATURES(SET_WHEN_DEBUG, ;); #ifdef RUBY_DEVEL if (ruby_patchlevel < 0 && ruby_env_debug_option(str, len, 0)) return; @@ -972,42 +916,6 @@ set_option_encoding_once(const char *type, VALUE *name, const char *e, long elen #define set_source_encoding_once(opt, e, elen) \ set_option_encoding_once("source", &(opt)->src.enc.name, (e), (elen)) -#if USE_MJIT -static void -setup_mjit_options(const char *s, struct mjit_options *mjit_opt) -{ - if (*s == 0) return; - else if (strcmp(s, "-warnings") == 0) { - mjit_opt->warnings = 1; - } - else if (strncmp(s, "-debug=", 7) == 0) { - mjit_opt->debug_flags = strdup(s + 7); - } - else if (strcmp(s, "-debug") == 0) { - mjit_opt->debug = 1; - } - else if (strcmp(s, "-wait") == 0) { - mjit_opt->wait = 1; - } - else if (strcmp(s, "-save-temps") == 0) { - mjit_opt->save_temps = 1; - } - else if (strncmp(s, "-verbose=", 9) == 0) { - mjit_opt->verbose = atoi(s + 9); - } - else if (strncmp(s, "-max-cache=", 11) == 0) { - mjit_opt->max_cache_size = atoi(s + 11); - } - else if (strncmp(s, "-min-calls=", 11) == 0) { - mjit_opt->min_calls = atoi(s + 11); - } - else { - rb_raise(rb_eRuntimeError, - "invalid MJIT option `%s' (--help will show valid MJIT options)", s + 1); - } -} -#endif - static long proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) { @@ -1066,29 +974,10 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) warning = 1; ruby_verbose = Qtrue; } - FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); s++; goto reswitch; case 'W': - if (s[1] == ':') { - unsigned int bits = 0; - static const char no_prefix[] = "no-"; - int enable = strncmp(s += 2, no_prefix, sizeof(no_prefix)-1) != 0; - if (!enable) s += sizeof(no_prefix)-1; - size_t len = strlen(s); - if (NAME_MATCH_P("deprecated", s, len)) { - bits = 1U << RB_WARN_CATEGORY_DEPRECATED; - } - else if (NAME_MATCH_P("experimental", s, len)) { - bits = 1U << RB_WARN_CATEGORY_EXPERIMENTAL; - } - else { - rb_warn("unknown warning category: `%s'", s); - } - if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0); - break; - } { size_t numlen; int v = 2; /* -W as -W2 */ @@ -1113,17 +1002,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) } } warning = 1; - switch (v) { - case 0: - FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_ALL_BITS, 0); - break; - case 1: - FEATURE_SET_TO(opt->warn, 1U << RB_WARN_CATEGORY_DEPRECATED, 0); - break; - default: - FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); - break; - } } goto reswitch; @@ -1259,15 +1137,18 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) goto reswitch; case 'T': - { - size_t numlen; + { + size_t numlen; + int v = 1; - if (*++s) { - scan_oct(s, 2, &numlen); - s += numlen; - } - } - rb_warn("ruby -T will be removed in Ruby 3.0"); + if (*++s) { + v = scan_oct(s, 2, &numlen); + if (numlen == 0) + v = 1; + s += numlen; + } + if (v > opt->safe_level) opt->safe_level = v; + } goto reswitch; case 'I': @@ -1387,14 +1268,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) opt->verbose = 1; ruby_verbose = Qtrue; } - else if (strncmp("jit", s, 3) == 0) { -#if USE_MJIT - FEATURE_SET(opt->features, FEATURE_BIT(jit)); - setup_mjit_options(s + 3, &opt->mjit); -#else - rb_warn("MJIT support is disabled."); -#endif - } else if (strcmp("yydebug", s) == 0) { if (envopt) goto noenvopt_long; opt->dump |= DUMP_BIT(yydebug); @@ -1419,9 +1292,16 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) default: { - rb_raise(rb_eRuntimeError, + if (ISPRINT(*s)) { + rb_raise(rb_eRuntimeError, "invalid option -%c (-h will show valid options)", (int)(unsigned char)*s); + } + else { + rb_raise(rb_eRuntimeError, + "invalid option -\\x%02X (-h will show valid options)", + (int)(unsigned char)*s); + } } goto switch_end; @@ -1449,12 +1329,10 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) return argc0 - argc; } -void Init_builtin_features(void); - static void ruby_init_prelude(void) { - Init_builtin_features(); + Init_prelude(); rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); } @@ -1476,16 +1354,15 @@ opt_enc_index(VALUE enc_name) #define rb_progname (GET_VM()->progname) #define rb_orig_progname (GET_VM()->orig_progname) VALUE rb_argv0; -VALUE rb_e_script; static VALUE -false_value(ID _x, VALUE *_y) +false_value(void) { return Qfalse; } static VALUE -true_value(ID _x, VALUE *_y) +true_value(void) { return Qtrue; } @@ -1517,7 +1394,7 @@ uscore_get(void) */ static VALUE -rb_f_sub(int argc, VALUE *argv, VALUE _) +rb_f_sub(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("sub"), argc, argv); rb_lastline_set(str); @@ -1536,7 +1413,7 @@ rb_f_sub(int argc, VALUE *argv, VALUE _) */ static VALUE -rb_f_gsub(int argc, VALUE *argv, VALUE _) +rb_f_gsub(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv); rb_lastline_set(str); @@ -1548,13 +1425,13 @@ rb_f_gsub(int argc, VALUE *argv, VALUE _) * chop -> $_ * * Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code> - * is never returned. See String#chop!. + * is never returned. See <code>String#chop!</code>. * Available only when -p/-n command line option specified. * */ static VALUE -rb_f_chop(VALUE _) +rb_f_chop(void) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chop"), 0, 0); rb_lastline_set(str); @@ -1568,13 +1445,13 @@ rb_f_chop(VALUE _) * chomp(string) -> $_ * * Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See - * String#chomp. + * <code>String#chomp</code>. * Available only when -p/-n command line option specified. * */ static VALUE -rb_f_chomp(int argc, VALUE *argv, VALUE _) +rb_f_chomp(int argc, VALUE *argv) { VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("chomp"), argc, argv); rb_lastline_set(str); @@ -1595,6 +1472,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) const char *s; char fbuf[MAXPATHLEN]; int i = (int)proc_options(argc, argv, opt, 0); + rb_binding_t *toplevel_binding; + const struct rb_block *base_block; unsigned int dump = opt->dump & dump_exit_bits; if (opt->dump & (DUMP_BIT(usage)|DUMP_BIT(help))) { @@ -1609,12 +1488,11 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) argc -= i; argv += i; - if ((opt->features.set & FEATURE_BIT(rubyopt)) && (s = getenv("RUBYOPT"))) { + if ((opt->features & FEATURE_BIT(rubyopt)) && + opt->safe_level == 0 && (s = getenv("RUBYOPT"))) { VALUE src_enc_name = opt->src.enc.name; VALUE ext_enc_name = opt->ext.enc.name; VALUE int_enc_name = opt->intern.enc.name; - ruby_features_t feat = opt->features; - ruby_features_t warn = opt->warn; opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0; moreswitches(s, opt, 1); @@ -1624,22 +1502,12 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) opt->ext.enc.name = ext_enc_name; if (int_enc_name) opt->intern.enc.name = int_enc_name; - FEATURE_SET_RESTORE(opt->features, feat); - FEATURE_SET_RESTORE(opt->warn, warn); } if (opt->src.enc.name) rb_warning("-K is specified; it is for 1.8 compatibility and may cause odd behavior"); -#if USE_MJIT - if (opt->features.set & FEATURE_BIT(jit)) { - opt->mjit.on = TRUE; /* set mjit.on for ruby_show_version() API and check to call mjit_init() */ - } -#endif if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) { -#if USE_MJIT - mjit_opts.on = opt->mjit.on; /* used by ruby_show_version(). mjit_init() still can't be called here. */ -#endif ruby_show_version(); if (opt->dump & DUMP_BIT(version)) return Qtrue; } @@ -1689,16 +1557,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) translit_char(RSTRING_PTR(opt->script_name), '\\', '/'); #endif - ruby_gc_set_params(); - ruby_init_loadpath(); - -#if USE_MJIT - if (opt->mjit.on) - /* Using TMP_RUBY_PREFIX created by ruby_init_loadpath(). */ - mjit_init(&opt->mjit); -#endif - - Init_ruby_description(); + ruby_gc_set_params(opt->safe_level); + ruby_init_loadpath_safe(opt->safe_level); Init_enc(); lenc = rb_locale_encoding(); rb_enc_associate(rb_progname, lenc); @@ -1771,22 +1631,19 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_ary_replace(vm->load_path_snapshot, load_path); } } - - rb_warning_category_update(opt->warn.mask, opt->warn.set); - Init_ext(); /* load statically linked extensions before rubygems */ - if (opt->features.set & FEATURE_BIT(gems)) { + if (opt->features & FEATURE_BIT(gems)) { rb_define_module("Gem"); - if (opt->features.set & FEATURE_BIT(did_you_mean)) { - rb_define_module("DidYouMean"); - } + } + if (opt->features & FEATURE_BIT(did_you_mean)) { + rb_define_module("DidYouMean"); } ruby_init_prelude(); - if (opt->features.mask & COMPILATION_FEATURES) { + if ((opt->features ^ DEFAULT_FEATURES) & COMPILATION_FEATURES) { VALUE option = rb_hash_new(); #define SET_COMPILE_OPTION(h, o, name) \ rb_hash_aset((h), ID2SYM(rb_intern_const(#name)), \ - (FEATURE_SET_P(o->features, FEATURE_BIT(name)) ? Qtrue : Qfalse)); + ((o)->features & FEATURE_BIT(name) ? Qtrue : Qfalse)); SET_COMPILE_OPTION(option, opt, frozen_string_literal); SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal); rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option); @@ -1795,7 +1652,13 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) ruby_set_argv(argc, argv); process_sflag(&opt->sflag); - rb_parser_set_context(parser, 0, TRUE); + GetBindingPtr(rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING")), + toplevel_binding); + /* need to acquire env from toplevel_binding each time, since it + * may update after eval() */ + + base_block = toplevel_context(toplevel_binding); + rb_parser_set_context(parser, base_block, TRUE); if (opt->e_script) { VALUE progname = rb_progname; @@ -1852,7 +1715,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_enc_set_default_internal(Qnil); rb_stdio_set_default_encoding(); - if (!ast->body.root) { + if (!ast->root) { rb_ast_dispose(ast); return Qfalse; } @@ -1874,7 +1737,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) } if (dump & (DUMP_BIT(parsetree)|DUMP_BIT(parsetree_with_comment))) { - rb_io_write(rb_stdout, rb_parser_dump_tree(ast->body.root, dump & DUMP_BIT(parsetree_with_comment))); + rb_io_write(rb_stdout, rb_parser_dump_tree(ast->root, dump & DUMP_BIT(parsetree_with_comment))); rb_io_flush(rb_stdout); dump &= ~DUMP_BIT(parsetree)&~DUMP_BIT(parsetree_with_comment); if (!dump) { @@ -1896,12 +1759,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_enc_copy(path, opt->script_name); } } - - rb_binding_t *toplevel_binding; - GetBindingPtr(rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING")), - toplevel_binding); - const struct rb_block *base_block = toplevel_context(toplevel_binding); - iseq = rb_iseq_new_main(&ast->body, opt->script_name, path, vm_block_iseq(base_block)); + base_block = toplevel_context(toplevel_binding); + iseq = rb_iseq_new_main(ast->root, opt->script_name, path, vm_block_iseq(base_block)); rb_ast_dispose(ast); } @@ -1917,22 +1776,8 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) rb_define_readonly_boolean("$-l", opt->do_line); rb_define_readonly_boolean("$-a", opt->do_split); - if ((rb_e_script = opt->e_script) != 0) { - rb_gc_register_mark_object(opt->e_script); - } - - { - rb_execution_context_t *ec = GET_EC(); + rb_set_safe_level(opt->safe_level); - if (opt->e_script) { - /* -e */ - rb_exec_event_hook_script_compiled(ec, iseq, opt->e_script); - } - else { - /* file */ - rb_exec_event_hook_script_compiled(ec, iseq, Qnil); - } - } return (VALUE)iseq; } @@ -2041,7 +1886,7 @@ load_file_internal(VALUE argp_v) else if (!NIL_P(c)) { rb_io_ungetbyte(f, c); } - if (NIL_P(c)) { + else { argp->f = f = Qnil; } if (!(opt->dump & ~DUMP_BIT(version_v))) { @@ -2126,7 +1971,7 @@ open_load_file(VALUE fname_v, int *xflag) #endif if ((fd = rb_cloexec_open(fname, mode, 0)) < 0) { - e = errno; + int e = errno; if (!rb_gc_for_fd(e)) { rb_load_fail(fname_v, strerror(e)); } @@ -2261,7 +2106,7 @@ ruby_setproctitle(VALUE title) } static void -set_arg0(VALUE val, ID id, VALUE *_) +set_arg0(VALUE val, ID id) { if (origarg.argv == 0) rb_raise(rb_eRuntimeError, "$0 not initialized"); @@ -2275,6 +2120,7 @@ external_str_new_cstr(const char *p) #if UTF8_PATH VALUE str = rb_utf8_str_new_cstr(p); str = str_conv_enc(str, NULL, rb_default_external_encoding()); + OBJ_TAINT_RAW(str); return str; #else return rb_external_str_new_cstr(p); @@ -2316,6 +2162,9 @@ init_ids(ruby_cmdline_options_t *opt) if (uid != euid) opt->setids |= 1; if (egid != gid) opt->setids |= 2; + if (uid && opt->setids) { + if (opt->safe_level < 1) opt->safe_level = 1; + } } #undef forbid_setid @@ -2326,17 +2175,21 @@ forbid_setid(const char *s, const ruby_cmdline_options_t *opt) rb_raise(rb_eSecurityError, "no %s allowed while running setuid", s); if (opt->setids & 2) rb_raise(rb_eSecurityError, "no %s allowed while running setgid", s); + if (opt->safe_level > 0) + rb_raise(rb_eSecurityError, "no %s allowed in tainted mode", s); } static void -verbose_setter(VALUE val, ID id, VALUE *variable) +verbose_setter(VALUE val, ID id, void *data) { + VALUE *variable = data; *variable = RTEST(val) ? Qtrue : val; } static VALUE -opt_W_getter(ID id, VALUE *variable) +opt_W_getter(ID id, void *data) { + VALUE *variable = data; switch (*variable) { case Qnil: return INT2FIX(0); |
