Load_to(file, mod)

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

e$B;XDj$7$?%b%8%e!<%k2<$Ke(B load e$B$9$k$?$a$Ke(B subject
e$B$K<($7$?$h$&$Je(B
e$B4X?t7?$N%a%=%C%I$,e(B (e$BL>A0$NNI$70-$7$OJ]N1$H$7$Fe(B)
e$BM_$7$$$J$H;W$C$F$$$^$9!%e(B

load(file, true) e$B$Oe(B load_to(file, Module.new) e$B$HF1$8$G$9!%e(B

e$B$=$&$$$&$b$N$,$J$$$h$&$K8+$($k$N$O!$<B$Oe(B

(1) e$BE,@Z$JJ}K!$,B>$KB8:$9$ke(B
(2) Thread e$B$H$NMm$
$GMn$A$ke(B or
e$B@5>o$KF0$+$J$$4m81@-$,$"$k$?$ae(B
(3) e$B2?$i$+$N%;%-%e%j%F%#%[!<%k$H$J$j$&$k$?$ae(B
(4) e$B$=$b$=$b$=$s$J$b$N$,M_$7$$$H;W$&H/A[<+BN$,$*$+$7$$e(B
(5) e$B$=$NB>!$FCJL$JM}M3$,B8:_$9$ke(B

e$B$N$$$:$l$+$^$?$OJ#?t$,@.N)$7$F$$$k$N$Ke(B
e$BC1$K;d$,5$$,IU$$$F$$$J$$$@$1$J$N$G$7$g$&$+!)e(B

e$B$A$g$C$HD4$Y$?8B$j$O8+$D$1$k$3$H$,$G$-$J$+$C$?$N$G$9$,!$e(B

e$B$=$l$bC1$KD4$YJ}$,B-$j$J$$$@$1$J$N$G$7$g$&$+!)e(B(^_^;

e$BG0$N$?$a!$%Q%C%A$rE:$($F$*$-$^$9!%e(B
e$B0z?t$,>/$75$;}$A0-$$$G$9$,!$=>Mh$H$N8_49@-0];}$He(B
e$BF1$8%3!<%I$N7+$jJV$72sHr$H$N$?$ae(B ( + e$B<jH4$-$N$?$ae(B ) e$B$Ke(B
e$B$=$&$7$F$^$9!%e(B

                                   e$B1J0fe(B e$B=(Mxe(B 

e$B!J6e9)Bge(B e$BCNG=>pJs!Ke(B
[email protected]

Index: eval.c

RCS file: /var/cvs/src/ruby/eval.c,v
retrieving revision 1.890
diff -u -r1.890 eval.c
— eval.c 23 Feb 2006 04:24:39 -0000 1.890
+++ eval.c 11 May 2006 15:56:35 -0000
@@ -6618,7 +6618,7 @@
NORETURN(static void load_failed(VALUE));

void
-rb_load(VALUE fname, int wrap)
+rb_load_to(VALUE fname, int wrap, VALUE wrapper_mod)
{
VALUE tmp;
int state;
@@ -6647,8 +6647,14 @@
ruby_wrapper = 0;
}
else {

  • /* load in anonymous module as toplevel */
  • ruby_wrapper = rb_module_new();
  •    if (rb_obj_is_kind_of(wrapper_mod, rb_cModule)) {
    
  •        /* load in the module as toplevel */
    
  •        ruby_wrapper = wrapper_mod;
    
  •    }
    
  •    else {
    
  •        /* load in anonymous module as toplevel */
    
  •        ruby_wrapper = rb_module_new();
    
  •    }
    
    self = rb_obj_clone(ruby_top_self);
    rb_extend_object(self, ruby_wrapper);
    PUSH_CREF(ruby_wrapper);
    @@ -6712,6 +6718,12 @@
    }

void
+rb_load(VALUE fname, int wrap)
+{

  • rb_load_to(fname, wrap, Qnil);
    +}

+void
rb_load_protect(VALUE fname, int wrap, int *state)
{
int status;
@@ -6727,6 +6739,22 @@
if (state) *state = status;
}

+void
+rb_load_protect_to(VALUE fname, int wrap, VALUE wrapper_mod, int
*state)
+{

  • int status;
  • PUSH_THREAD_TAG();
  • if ((status = EXEC_TAG()) == 0) {
  • rb_load_to(fname, wrap, wrapper_mod);
  • }
  • else if (status == TAG_THREAD) {
  • rb_thread_start_1();
  • }
  • POP_THREAD_TAG();
  • if (state) *state = status;
    +}

/*

  • call-seq:
  • load(filename, wrap=false)   => true
    

@@ -6752,6 +6780,26 @@
return Qtrue;
}

+/*

    • call-seq:
    • load_to(filename, wrapper)   => true
      
    • Loads and executes the Ruby
    • program in the file filename under the specified module wrapper
    • (See also the wrap parameter of load function).
    • If the filename does not
    • resolve to an absolute path, the file is searched for in the
      library
    • directories listed in $:.
  • */

+static VALUE
+rb_f_load_to(VALUE obj, VALUE fname, VALUE wrapper_mod)
+{

  • rb_load_to(fname, 1, wrapper_mod);
  • return Qtrue;
    +}

VALUE ruby_dln_librefs;
static VALUE rb_features;
static st_table *loading_tbl;
@@ -7875,6 +7923,7 @@
rb_features = rb_ary_new();

 rb_define_global_function("load", rb_f_load, -1);
  • rb_define_global_function(“load_to”, rb_f_load_to, 2);
    rb_define_global_function(“require”, rb_f_require, 1);
    rb_define_method(rb_cModule, “autoload”, rb_mod_autoload, 2);
    rb_define_method(rb_cModule, “autoload?”, rb_mod_autoload_p, 1);

e$B@DLZ$G$9!#D6CY%l%9$D$1$^$9e(B

In mail “[ruby-dev:28633] load_to(file, mod)”
Hidetoshi NAGAI [email protected] wrote:

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

e$B;XDj$7$?%b%8%e!<%k2<$Ke(B load e$B$9$k$?$a$Ke(B subject e$B$K<($7$?$h$&$Je(B
e$B4X?t7?$N%a%=%C%I$,e(B (e$BL>A0$NNI$70-$7$OJ]N1$H$7$Fe(B) e$BM_$7$$$J$H;W$C$F$$$^$9!%e(B

load(file, true) e$B$Oe(B load_to(file, Module.new) e$B$HF1$8$G$9!%e(B

mod.module_eval(File.read(path)) e$B$G!“$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B
e$B%m!<%I%Q%9$+$iC5$7$?$$$H$J$k$H!”$b$&>/$7$d$i$J$$$H$@$a$G$9$1$I$M!#e(B

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Minero A. [email protected]
Subject: [ruby-dev:28669] Re: load_to(file, mod)
Date: Mon, 29 May 2006 15:11:01 +0900
Message-ID: [email protected]

e$B;XDj$7$?%b%8%e!<%k2<$Ke(B load e$B$9$k$?$a$Ke(B subject e$B$K<($7$?$h$&$Je(B
e$B4X?t7?$N%a%=%C%I$,e(B (e$BL>A0$NNI$70-$7$OJ]N1$H$7$Fe(B) e$BM_$7$$$J$H;W$C$F$$$^$9!%e(B

load(file, true) e$B$Oe(B load_to(file, Module.new) e$B$HF1$8$G$9!%e(B

mod.module_eval(File.read(path)) e$B$G!“$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B
e$B%m!<%I%Q%9$+$iC5$7$?$$$H$J$k$H!”$b$&>/$7$d$i$J$$$H$@$a$G$9$1$I$M!#e(B

e$B%m!<%I%Q%9$N7o$O$H$j$"$($:CV$$$F$$/$H$7$F!$e(B
e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true)
e$B$NBe$j$H$7$F;H$
$&$H$9$k$H!$e(B
File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B
e$B$^$?!$e(Bself e$B$,e(B main e$B$G$O$J$/e(B mod
e$B$K$J$C$F$7$^$$$^$9$h$M!)e(B
self e$B$,e(B mod e$B$K$J$C$F$7$^$&$He(B path e$B$N%9%/%j%W%H$+$ie(B
mod e$B$K?($l$F$7$^$&$N$Ge(B (e$B$G$9$h$M!)e(B) e$B5$$KF~$j$^$;$s!%e(B

e$B$d$j$?$$$N$O!$e(B$SAFE == 4 e$B$Ge(B load(path, true)
e$B$7$?%9%/%j%W%HCf$Ne(B
load(path2) e$B$rF1$8L5L>%b%8%e!<%kCf$GI>2A$7$?$$$H$$$&$b$N$G$9!%e(B
e$B$5$i$K$O!$e(Bload() e$B$N30$+$ie(B load
e$BFb$N4F;k$b$7$?$$$H;W$C$F$^$9!%e(B

load e$B%a%=%C%I$r$/$k$s$G$d$k$3$H$G$"$kDxEY$^$G$O9T$1$k$N$G$9$,!$e(B
e$BL5L>%b%8%e!<%k$,<hF@$G$-$J$/$F$@$a$@$C$?$j!$e(B
e$B$=$l$rHr$1$k$He(B self e$B$,e(B main
e$B$G$O$J$/$J$C$F$7$^$C$?$j$GF,$rG:$^$;$F$^$9!%e(B

mod.module_eval(File.read(path)) e$B$Ne(B File.read(path) e$B$re(B
e$B$&$^$/$/$k$s$G$d$C$F!$e(Bself e$B$,e(B mod e$B$K$J$C$F$7$^$&E@$Oe(B
e$BBE6($9$k$7$+J}K!$O$J$$$N$G$7$g$&$+!%e(B

e$BA0ED$G$9!#e(B

Hidetoshi NAGAI wrote:

e$BBE6($9$k$7$+J}K!$O$J$$$N$G$7$g$&$+!%e(B
generate_binding.rbe$B$H$$$&L>A0$Ge(B

----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----
e$B$3$3$+$ie(B -----
$binding = binding
----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----
e$B$3$3$^$Ge(B -----

e$B$H$$$&FbMF$N%U%!%$%k$rMQ0U$7$F$*$$$F!"e(B

----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----
e$B$3$3$+$ie(B -----
def generate_binding
Thread.critical = true
begin
load(“generate_binding.rb”, true)
return $binding
ensure
Thread.critical = false
end
end

b = generate_binding

1e$B2sL$N<B9Te(B

eval(“def hello; puts ‘hello world’; end”, b)

2e$B2sL$N<B9Te(B

eval(“hello”, b)
----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----
e$B$3$3$^$Ge(B -----

e$B$H$$$&<j$r;H$&$N$O$I$&$G$7$g$&$+!#e(B

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Shugo M. [email protected]
Subject: [ruby-dev:28671] Re: load_to(file, mod)
Date: Mon, 29 May 2006 17:27:04 +0900
Message-ID: [email protected]

generate_binding.rbe$B$H$$$&L>A0$Ge(B

----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----
$binding = binding
----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----

e$B$H$$$&FbMF$N%U%!%$%k$rMQ0U$7$F$*$$$F!"e(B
(snip)
e$B$H$$$&<j$r;H$&$N$O$I$&$G$7$g$&$+!#e(B

e$B$J$k$[$I!%e(Bbinding e$B$r;H$C$F;}$A=P$7$F$7$^$&<j$,$“$j$^$7$?$+!%e(B
e$B3N$+$K$$$1$=$&$J5$$,$7$^$9!%$A$g$C$H9M$($F$_$^$9!%e(B
e$B$”$j$,$H$&$4$6$$$^$7$?!%e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:28672] Re: load_to(file, mod)”
on Mon, 29 May 2006 18:18:21 +0900, Minero A.
[email protected] writes:

|> e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$*$&$H$9$k$H!$e(B
|> File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B
|
|$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B

|1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B

e$B$J$s$G%:%l$F$k$s$@$m$&!)e(B e$B;W$$=P$;$J$$!#BhFs0z?t$,;XDj$5$l$Fe(B
e$B$$$k;~$K$Oe(B$SAFE=4e$B$G$b0BA4$G$O$J$$$+$H;W$C$?5-21$O$"$k$s$G$9e(B
e$B$1$I!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

e$B$D$$!“$&$C$+$j2a5n$N%a!<%ke(B(2006-05-29)e$B$K%j%W%i%$$7$F$7$^$$$^e(B
e$B$7$?$,!”$3$N7o!"$$$^$@$KJ|CV$5$l$F$k$s$G$9$h$M$(!#<B:]$N$H$3e(B
e$B$m$I$&$J$s$G$7$g!)e(B

In message “Re: [ruby-dev:30247] Re: load_to(file, mod)”
on Sat, 3 Feb 2007 10:10:21 +0900, Yukihiro M.
[email protected] writes:
|
|e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B
|
|In message “Re: [ruby-dev:28672] Re: load_to(file, mod)”
| on Mon, 29 May 2006 18:18:21 +0900, Minero A. [email protected] writes:
|
||> e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$*$&$H$9$k$H!$e(B
||> File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B
||
||$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B
|
||1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B
|
|e$B$J$s$G%:%l$F$k$s$@$m$&!)e(B e$B;W$$=P$;$J$$!#BhFs0z?t$,;XDj$5$l$Fe(B
|e$B$$$k;~$K$Oe(B$SAFE=4e$B$G$b0BA4$G$O$J$$$+$H;W$C$?5-21$O$"$k$s$G$9e(B
|e$B$1$I!#e(B

e$B@DLZ$G$9!#e(B

In mail “[ruby-dev:28670] Re: load_to(file, mod)”
Hidetoshi NAGAI [email protected] wrote:

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

mod.module_eval(File.read(path)) e$B$G!"$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B

e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$*$&$H$9$k$H!$e(B
File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B

$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B
e$B$J$s$+$$$+$K$b%@%a$=$&$J$s$G$9$1$I!#e(B

~/c/osm % cat t
$SAFE = 4
load “/tmp/a.rb”, true

~/c/osm % cat /tmp/a.rb
C = “OK”

~/c/osm % forall-ruby t
ruby 1.8.0 (2003-08-04) [x86_64-linux]
ruby 1.8.1 (2003-12-25) [x86_64-linux]
ruby 1.8.2 (2004-12-25) [x86_64-linux]
ruby 1.8.4 (2005-12-24) [x86_64-linux]
ruby 1.8.4 (2006-05-27) [x86_64-linux]
ruby 1.9.0 (2006-05-27) [x86_64-linux]
t:2:in `load’: Insecure operation - load (SecurityError)
from t:2

1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B