mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* marshal.c (struct {dump,load}_arg): manage with dfree, instead
of using local variable which may be moved by context switch. [ruby-dev:39425] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@25230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c46c1db70f
commit
bf40bc6837
3 changed files with 105 additions and 73 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Sun Oct 4 19:30:54 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* marshal.c (struct {dump,load}_arg): manage with dfree, instead
|
||||||
|
of using local variable which may be moved by context switch.
|
||||||
|
[ruby-dev:39425]
|
||||||
|
|
||||||
Sun Oct 4 05:34:34 2009 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
|
Sun Oct 4 05:34:34 2009 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
|
||||||
|
|
||||||
* ext/tk/lib/tk/variable.rb: add TkVariable#to_hash,to_proc,to_int,
|
* ext/tk/lib/tk/variable.rb: add TkVariable#to_hash,to_proc,to_int,
|
||||||
|
|
144
marshal.c
144
marshal.c
|
@ -85,12 +85,10 @@ static ID s_dump_data, s_load_data, s_alloc, s_call;
|
||||||
static ID s_getc, s_read, s_write, s_binmode;
|
static ID s_getc, s_read, s_write, s_binmode;
|
||||||
|
|
||||||
struct dump_arg {
|
struct dump_arg {
|
||||||
VALUE obj;
|
|
||||||
VALUE str, dest;
|
VALUE str, dest;
|
||||||
st_table *symbols;
|
st_table *symbols;
|
||||||
st_table *data;
|
st_table *data;
|
||||||
int taint;
|
int taint;
|
||||||
VALUE wrapper;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct dump_call_arg {
|
struct dump_call_arg {
|
||||||
|
@ -104,22 +102,32 @@ check_dump_arg(arg, sym)
|
||||||
struct dump_arg *arg;
|
struct dump_arg *arg;
|
||||||
ID sym;
|
ID sym;
|
||||||
{
|
{
|
||||||
if (!DATA_PTR(arg->wrapper)) {
|
if (!arg->symbols) {
|
||||||
rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
|
rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
|
||||||
rb_id2name(sym));
|
rb_id2name(sym));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void clear_dump_arg _((struct dump_arg *arg));
|
||||||
|
|
||||||
static void
|
static void
|
||||||
mark_dump_arg(ptr)
|
mark_dump_arg(ptr)
|
||||||
void *ptr;
|
void *ptr;
|
||||||
{
|
{
|
||||||
struct dump_arg *p = ptr;
|
struct dump_arg *p = ptr;
|
||||||
if (!ptr)
|
if (!p->symbols)
|
||||||
return;
|
return;
|
||||||
rb_mark_set(p->data);
|
rb_mark_set(p->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
free_dump_arg(ptr)
|
||||||
|
void *ptr;
|
||||||
|
{
|
||||||
|
clear_dump_arg(ptr);
|
||||||
|
xfree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
class2path(klass)
|
class2path(klass)
|
||||||
VALUE klass;
|
VALUE klass;
|
||||||
|
@ -700,32 +708,17 @@ w_object(obj, arg, limit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static void
|
||||||
dump(arg)
|
clear_dump_arg(arg)
|
||||||
struct dump_call_arg *arg;
|
|
||||||
{
|
|
||||||
w_object(arg->obj, arg->arg, arg->limit);
|
|
||||||
if (arg->arg->dest) {
|
|
||||||
rb_io_write(arg->arg->dest, arg->arg->str);
|
|
||||||
rb_str_resize(arg->arg->str, 0);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE
|
|
||||||
dump_ensure(arg)
|
|
||||||
struct dump_arg *arg;
|
struct dump_arg *arg;
|
||||||
{
|
{
|
||||||
if (!DATA_PTR(arg->wrapper)) return 0;
|
if (!arg->symbols) return;
|
||||||
st_free_table(arg->symbols);
|
st_free_table(arg->symbols);
|
||||||
|
arg->symbols = 0;
|
||||||
st_free_table(arg->data);
|
st_free_table(arg->data);
|
||||||
DATA_PTR(arg->wrapper) = 0;
|
|
||||||
arg->wrapper = 0;
|
|
||||||
if (arg->taint) {
|
if (arg->taint) {
|
||||||
OBJ_TAINT(arg->str);
|
OBJ_TAINT(arg->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -761,8 +754,8 @@ marshal_dump(argc, argv)
|
||||||
{
|
{
|
||||||
VALUE obj, port, a1, a2;
|
VALUE obj, port, a1, a2;
|
||||||
int limit = -1;
|
int limit = -1;
|
||||||
struct dump_arg arg;
|
struct dump_arg *arg;
|
||||||
struct dump_call_arg c_arg;
|
VALUE wrapper;
|
||||||
|
|
||||||
port = Qnil;
|
port = Qnil;
|
||||||
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
|
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
|
||||||
|
@ -776,37 +769,40 @@ marshal_dump(argc, argv)
|
||||||
else if (NIL_P(a1)) goto type_error;
|
else if (NIL_P(a1)) goto type_error;
|
||||||
else port = a1;
|
else port = a1;
|
||||||
}
|
}
|
||||||
arg.dest = 0;
|
wrapper = Data_Make_Struct(rb_cData, struct dump_arg, mark_dump_arg, free_dump_arg, arg);
|
||||||
arg.symbols = st_init_numtable();
|
arg->dest = 0;
|
||||||
arg.data = st_init_numtable();
|
arg->symbols = st_init_numtable();
|
||||||
arg.taint = Qfalse;
|
arg->data = st_init_numtable();
|
||||||
arg.str = rb_str_buf_new(0);
|
arg->taint = Qfalse;
|
||||||
RBASIC(arg.str)->klass = 0;
|
arg->str = rb_str_buf_new(0);
|
||||||
arg.wrapper = Data_Wrap_Struct(rb_cData, mark_dump_arg, 0, &arg);
|
RBASIC(arg->str)->klass = 0;
|
||||||
if (!NIL_P(port)) {
|
if (!NIL_P(port)) {
|
||||||
if (!rb_respond_to(port, s_write)) {
|
if (!rb_respond_to(port, s_write)) {
|
||||||
type_error:
|
type_error:
|
||||||
rb_raise(rb_eTypeError, "instance of IO needed");
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
||||||
}
|
}
|
||||||
arg.dest = port;
|
arg->dest = port;
|
||||||
if (rb_respond_to(port, s_binmode)) {
|
if (rb_respond_to(port, s_binmode)) {
|
||||||
rb_funcall2(port, s_binmode, 0, 0);
|
rb_funcall2(port, s_binmode, 0, 0);
|
||||||
check_dump_arg(&arg, s_binmode);
|
check_dump_arg(arg, s_binmode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
port = arg.str;
|
port = arg->str;
|
||||||
}
|
}
|
||||||
|
|
||||||
c_arg.obj = obj;
|
w_byte(MARSHAL_MAJOR, arg);
|
||||||
c_arg.arg = &arg;
|
w_byte(MARSHAL_MINOR, arg);
|
||||||
c_arg.limit = limit;
|
|
||||||
|
|
||||||
w_byte(MARSHAL_MAJOR, &arg);
|
w_object(obj, arg, limit);
|
||||||
w_byte(MARSHAL_MINOR, &arg);
|
if (arg->dest) {
|
||||||
|
rb_io_write(arg->dest, arg->str);
|
||||||
|
rb_str_resize(arg->str, 0);
|
||||||
|
}
|
||||||
|
|
||||||
rb_ensure(dump, (VALUE)&c_arg, dump_ensure, (VALUE)&arg);
|
RBASIC(arg->str)->klass = rb_cString;
|
||||||
RBASIC(arg.str)->klass = rb_cString;
|
clear_dump_arg(arg);
|
||||||
|
RB_GC_GUARD(wrapper);
|
||||||
|
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
@ -818,7 +814,6 @@ struct load_arg {
|
||||||
st_table *data;
|
st_table *data;
|
||||||
VALUE proc;
|
VALUE proc;
|
||||||
int taint;
|
int taint;
|
||||||
VALUE wrapper;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -826,22 +821,31 @@ check_load_arg(arg, sym)
|
||||||
struct load_arg *arg;
|
struct load_arg *arg;
|
||||||
ID sym;
|
ID sym;
|
||||||
{
|
{
|
||||||
if (!DATA_PTR(arg->wrapper)) {
|
if (!arg->symbols) {
|
||||||
rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
|
rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
|
||||||
rb_id2name(sym));
|
rb_id2name(sym));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void clear_load_arg _((struct load_arg *arg));
|
||||||
|
|
||||||
static void
|
static void
|
||||||
mark_load_arg(ptr)
|
mark_load_arg(ptr)
|
||||||
void *ptr;
|
void *ptr;
|
||||||
{
|
{
|
||||||
struct load_arg *p = ptr;
|
struct load_arg *p = ptr;
|
||||||
if (!ptr)
|
if (!p->symbols)
|
||||||
return;
|
return;
|
||||||
rb_mark_tbl(p->data);
|
rb_mark_tbl(p->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
free_load_arg(void *ptr)
|
||||||
|
{
|
||||||
|
clear_load_arg(ptr);
|
||||||
|
xfree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE r_object _((struct load_arg *arg));
|
static VALUE r_object _((struct load_arg *arg));
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -1416,23 +1420,14 @@ r_object(arg)
|
||||||
return r_object0(arg, arg->proc, 0, Qnil);
|
return r_object0(arg, arg->proc, 0, Qnil);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static void
|
||||||
load(arg)
|
clear_load_arg(arg)
|
||||||
struct load_arg *arg;
|
struct load_arg *arg;
|
||||||
{
|
{
|
||||||
return r_object(arg);
|
if (!arg->symbols) return;
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE
|
|
||||||
load_ensure(arg)
|
|
||||||
struct load_arg *arg;
|
|
||||||
{
|
|
||||||
if (!DATA_PTR(arg->wrapper)) return 0;
|
|
||||||
st_free_table(arg->symbols);
|
st_free_table(arg->symbols);
|
||||||
|
arg->symbols = 0;
|
||||||
st_free_table(arg->data);
|
st_free_table(arg->data);
|
||||||
DATA_PTR(arg->wrapper) = 0;
|
|
||||||
arg->wrapper = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1452,35 +1447,36 @@ marshal_load(argc, argv)
|
||||||
VALUE *argv;
|
VALUE *argv;
|
||||||
{
|
{
|
||||||
VALUE port, proc;
|
VALUE port, proc;
|
||||||
int major, minor;
|
int major, minor, taint = Qfalse;
|
||||||
VALUE v;
|
VALUE v, wrapper;
|
||||||
struct load_arg arg;
|
struct load_arg *arg;
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "11", &port, &proc);
|
rb_scan_args(argc, argv, "11", &port, &proc);
|
||||||
v = rb_check_string_type(port);
|
v = rb_check_string_type(port);
|
||||||
if (!NIL_P(v)) {
|
if (!NIL_P(v)) {
|
||||||
arg.taint = OBJ_TAINTED(port); /* original taintedness */
|
taint = OBJ_TAINTED(port); /* original taintedness */
|
||||||
port = v;
|
port = v;
|
||||||
}
|
}
|
||||||
else if (rb_respond_to(port, s_getc) && rb_respond_to(port, s_read)) {
|
else if (rb_respond_to(port, s_getc) && rb_respond_to(port, s_read)) {
|
||||||
if (rb_respond_to(port, s_binmode)) {
|
if (rb_respond_to(port, s_binmode)) {
|
||||||
rb_funcall2(port, s_binmode, 0, 0);
|
rb_funcall2(port, s_binmode, 0, 0);
|
||||||
}
|
}
|
||||||
arg.taint = Qtrue;
|
taint = Qtrue;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rb_raise(rb_eTypeError, "instance of IO needed");
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
||||||
}
|
}
|
||||||
arg.src = port;
|
wrapper = Data_Make_Struct(rb_cData, struct load_arg, mark_load_arg, free_load_arg, arg);
|
||||||
arg.offset = 0;
|
arg->src = port;
|
||||||
arg.symbols = st_init_numtable();
|
arg->offset = 0;
|
||||||
arg.data = st_init_numtable();
|
arg->symbols = st_init_numtable();
|
||||||
arg.proc = 0;
|
arg->data = st_init_numtable();
|
||||||
arg.wrapper = Data_Wrap_Struct(rb_cData, mark_load_arg, 0, &arg);
|
arg->proc = 0;
|
||||||
|
|
||||||
major = r_byte(&arg);
|
major = r_byte(arg);
|
||||||
minor = r_byte(&arg);
|
minor = r_byte(arg);
|
||||||
if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
|
if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
|
||||||
|
clear_load_arg(arg);
|
||||||
rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
|
rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
|
||||||
\tformat version %d.%d required; %d.%d given",
|
\tformat version %d.%d required; %d.%d given",
|
||||||
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
||||||
|
@ -1491,8 +1487,10 @@ marshal_load(argc, argv)
|
||||||
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!NIL_P(proc)) arg.proc = proc;
|
if (!NIL_P(proc)) arg->proc = proc;
|
||||||
v = rb_ensure(load, (VALUE)&arg, load_ensure, (VALUE)&arg);
|
v = r_object(arg);
|
||||||
|
clear_load_arg(arg);
|
||||||
|
RB_GC_GUARD(wrapper);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,4 +71,32 @@ class TestMarshal < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
assert_equal("marshal data too short", e.message)
|
assert_equal("marshal data too short", e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DumpTest
|
||||||
|
def marshal_dump
|
||||||
|
loop { Thread.pass }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class LoadTest
|
||||||
|
def marshal_dump
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
def marshal_load(obj)
|
||||||
|
loop { Thread.pass }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_context_switch
|
||||||
|
o = DumpTest.new
|
||||||
|
Thread.new { Marshal.dump(o) }
|
||||||
|
GC.start
|
||||||
|
assert(true, '[ruby-dev:39425]')
|
||||||
|
|
||||||
|
o = LoadTest.new
|
||||||
|
m = Marshal.dump(o)
|
||||||
|
Thread.new { Marshal.load(m) }
|
||||||
|
GC.start
|
||||||
|
assert(true, '[ruby-dev:39425]')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue