summaryrefslogtreecommitdiff
path: root/marshal.c
diff options
context:
space:
mode:
Diffstat (limited to 'marshal.c')
-rw-r--r--marshal.c3069
1 files changed, 2112 insertions, 957 deletions
diff --git a/marshal.c b/marshal.c
index 185ace6a72..e6ee3b47b0 100644
--- a/marshal.c
+++ b/marshal.c
@@ -3,17 +3,13 @@
marshal.c -
$Author$
- $Date$
created at: Thu Apr 27 16:30:01 JST 1995
- Copyright (C) 1993-2003 Yukihiro Matsumoto
+ Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
-#include "ruby.h"
-#include "rubyio.h"
-#include "st.h"
-#include "util.h"
+#include "ruby/internal/config.h"
#include <math.h>
#ifdef HAVE_FLOAT_H
@@ -23,27 +19,49 @@
#include <ieeefp.h>
#endif
+#include "encindex.h"
+#include "id_table.h"
+#include "internal.h"
+#include "internal/array.h"
+#include "internal/bignum.h"
+#include "internal/class.h"
+#include "internal/encoding.h"
+#include "internal/error.h"
+#include "internal/hash.h"
+#include "internal/numeric.h"
+#include "internal/object.h"
+#include "internal/re.h"
+#include "internal/struct.h"
+#include "internal/symbol.h"
+#include "internal/util.h"
+#include "internal/vm.h"
+#include "ruby/io.h"
+#include "ruby/ruby.h"
+#include "ruby/st.h"
+#include "ruby/util.h"
+#include "builtin.h"
+#include "shape.h"
+#include "ruby/internal/attr/nonstring.h"
+
#define BITSPERSHORT (2*CHAR_BIT)
#define SHORTMASK ((1<<BITSPERSHORT)-1)
-#define SHORTDN(x) RSHIFT(x,BITSPERSHORT)
+#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
-#if SIZEOF_SHORT == SIZEOF_BDIGITS
+#if SIZEOF_SHORT == SIZEOF_BDIGIT
#define SHORTLEN(x) (x)
#else
-static int
-shortlen(len, ds)
- long len;
- BDIGIT *ds;
+static size_t
+shortlen(size_t len, BDIGIT *ds)
{
BDIGIT num;
int offset = 0;
num = ds[len-1];
while (num) {
- num = SHORTDN(num);
- offset++;
+ num = SHORTDN(num);
+ offset++;
}
- return (len - 1)*sizeof(BDIGIT)/2 + offset;
+ return (len - 1)*SIZEOF_BDIGIT/2 + offset;
}
#define SHORTLEN(x) shortlen((x),d)
#endif
@@ -81,15 +99,71 @@ shortlen(len, ds)
#define TYPE_LINK '@'
static ID s_dump, s_load, s_mdump, s_mload;
-static ID s_dump_data, s_load_data, s_alloc;
-static ID s_getc, s_read, s_write, s_binmode;
+static ID s_dump_data, s_load_data, s_alloc, s_call;
+static ID s_getbyte, s_read, s_write, s_binmode;
+static ID s_encoding_short, s_ruby2_keywords_flag;
+#define s_encoding_long rb_id_encoding()
+
+#define name_s_dump "_dump"
+#define name_s_load "_load"
+#define name_s_mdump "marshal_dump"
+#define name_s_mload "marshal_load"
+#define name_s_dump_data "_dump_data"
+#define name_s_load_data "_load_data"
+#define name_s_alloc "_alloc"
+#define name_s_call "call"
+#define name_s_getbyte "getbyte"
+#define name_s_read "read"
+#define name_s_write "write"
+#define name_s_binmode "binmode"
+#define name_s_encoding_short "E"
+#define name_s_encoding_long "encoding"
+#define name_s_ruby2_keywords_flag "K"
+
+typedef struct {
+ VALUE newclass;
+ VALUE oldclass;
+ VALUE (*dumper)(VALUE);
+ VALUE (*loader)(VALUE, VALUE);
+} marshal_compat_t;
+
+static st_table *compat_allocator_tbl;
+static VALUE compat_allocator_tbl_wrapper;
+static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit);
+static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze);
+
+static st_table *compat_allocator_table(void);
+
+void
+rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
+{
+ marshal_compat_t *compat;
+ rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
+
+ if (!allocator) {
+ rb_raise(rb_eTypeError, "no allocator");
+ }
+
+ compat_allocator_table();
+ compat = ALLOC(marshal_compat_t);
+ compat->newclass = newclass;
+ compat->oldclass = oldclass;
+ compat->dumper = dumper;
+ compat->loader = loader;
+
+ st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
+ RB_OBJ_WRITTEN(compat_allocator_tbl_wrapper, Qundef, newclass);
+ RB_OBJ_WRITTEN(compat_allocator_tbl_wrapper, Qundef, oldclass);
+}
struct dump_arg {
- VALUE obj;
VALUE str, dest;
st_table *symbols;
st_table *data;
- int taint;
+ st_table *compat_tbl;
+ st_table *encodings;
+ st_table *userdefs;
+ st_index_t num_entries;
};
struct dump_call_arg {
@@ -99,110 +173,188 @@ struct dump_call_arg {
};
static VALUE
-class2path(klass)
- VALUE klass;
+check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
{
- VALUE path = rb_class_path(klass);
- char *n = RSTRING(path)->ptr;
+ if (!arg->symbols) {
+ rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
+ name);
+ }
+ return ret;
+}
+
+static VALUE
+check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
+ struct dump_arg *arg, const char *name)
+{
+ VALUE ret = rb_funcallv(obj, sym, argc, argv);
+ VALUE klass = CLASS_OF(obj);
+ if (CLASS_OF(ret) == klass) {
+ rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
+ klass, name);
+ }
+ return check_dump_arg(ret, arg, name);
+}
+
+#define dump_funcall(arg, obj, sym, argc, argv) \
+ check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
+#define dump_check_funcall(arg, obj, sym, argc, argv) \
+ check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
+
+static void clear_dump_arg(struct dump_arg *arg);
+static void
+mark_dump_arg(void *ptr)
+{
+ struct dump_arg *p = ptr;
+ if (!p->symbols)
+ return;
+ rb_mark_set(p->symbols);
+ rb_mark_set(p->data);
+ rb_mark_hash(p->compat_tbl);
+ rb_mark_set(p->userdefs);
+ rb_gc_mark(p->str);
+}
+
+static void
+free_dump_arg(void *ptr)
+{
+ clear_dump_arg(ptr);
+}
+
+static size_t
+memsize_dump_arg(const void *ptr)
+{
+ const struct dump_arg *p = (struct dump_arg *)ptr;
+ size_t memsize = 0;
+ if (p->symbols) memsize += rb_st_memsize(p->symbols);
+ if (p->data) memsize += rb_st_memsize(p->data);
+ if (p->compat_tbl) memsize += rb_st_memsize(p->compat_tbl);
+ if (p->userdefs) memsize += rb_st_memsize(p->userdefs);
+ if (p->encodings) memsize += rb_st_memsize(p->encodings);
+ return memsize;
+}
+
+static const rb_data_type_t dump_arg_data = {
+ "dump_arg",
+ {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
+};
+
+static VALUE
+must_not_be_anonymous(const char *type, VALUE path)
+{
+ char *n = RSTRING_PTR(path);
+
+ if (!rb_enc_asciicompat(rb_enc_get(path))) {
+ /* cannot occur? */
+ rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
+ type, path);
+ }
if (n[0] == '#') {
- rb_raise(rb_eTypeError, "can't dump anonymous %s %s",
- (TYPE(klass) == T_CLASS ? "class" : "module"),
- n);
+ rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
+ type, path);
}
- if (rb_path2class(n) != rb_class_real(klass)) {
- rb_raise(rb_eTypeError, "%s can't be referred", n);
+ return path;
+}
+
+static VALUE
+class2path(VALUE klass)
+{
+ VALUE path = rb_class_path(klass);
+
+ must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path);
+ if (rb_path_to_class(path) != rb_class_real(klass)) {
+ rb_raise(rb_eTypeError, "% "PRIsVALUE" can't be referred to", path);
}
return path;
}
-static void w_long _((long, struct dump_arg*));
+int ruby_marshal_write_long(long x, char *buf);
+static void w_long(long, struct dump_arg*);
+static int w_encoding(VALUE encname, struct dump_call_arg *arg);
+static VALUE encoding_name(VALUE obj, struct dump_arg *arg);
static void
-w_nbyte(s, n, arg)
- char *s;
- int n;
- struct dump_arg *arg;
+w_nbyte(const char *s, long n, struct dump_arg *arg)
{
VALUE buf = arg->str;
rb_str_buf_cat(buf, s, n);
- if (arg->dest && RSTRING(buf)->len >= BUFSIZ) {
- if (arg->taint) OBJ_TAINT(buf);
- rb_io_write(arg->dest, buf);
- rb_str_resize(buf, 0);
+ if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
+ rb_io_write(arg->dest, buf);
+ rb_str_resize(buf, 0);
}
}
static void
-w_byte(c, arg)
- char c;
- struct dump_arg *arg;
+w_byte(char c, struct dump_arg *arg)
{
w_nbyte(&c, 1, arg);
}
static void
-w_bytes(s, n, arg)
- char *s;
- int n;
- struct dump_arg *arg;
+w_bytes(const char *s, long n, struct dump_arg *arg)
{
w_long(n, arg);
w_nbyte(s, n, arg);
}
+#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
+
static void
-w_short(x, arg)
- int x;
- struct dump_arg *arg;
+w_short(int x, struct dump_arg *arg)
{
- w_byte((x >> 0) & 0xff, arg);
- w_byte((x >> 8) & 0xff, arg);
+ w_byte((char)((x >> 0) & 0xff), arg);
+ w_byte((char)((x >> 8) & 0xff), arg);
}
static void
-w_long(x, arg)
- long x;
- struct dump_arg *arg;
+w_long(long x, struct dump_arg *arg)
{
char buf[sizeof(long)+1];
- int i, len = 0;
+ int i = ruby_marshal_write_long(x, buf);
+ if (i < 0) {
+ rb_raise(rb_eTypeError, "long too big to dump");
+ }
+ w_nbyte(buf, i, arg);
+}
+
+int
+ruby_marshal_write_long(long x, char *buf)
+{
+ int i;
#if SIZEOF_LONG > 4
if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
- /* big long does not fit in 4 bytes */
- rb_raise(rb_eTypeError, "long too big to dump");
+ /* big long does not fit in 4 bytes */
+ return -1;
}
#endif
if (x == 0) {
- w_byte(0, arg);
- return;
+ buf[0] = 0;
+ return 1;
}
if (0 < x && x < 123) {
- w_byte(x + 5, arg);
- return;
+ buf[0] = (char)(x + 5);
+ return 1;
}
if (-124 < x && x < 0) {
- w_byte((x - 5)&0xff, arg);
- return;
- }
- for (i=1;i<sizeof(long)+1;i++) {
- buf[i] = x & 0xff;
- x = RSHIFT(x,8);
- if (x == 0) {
- buf[0] = i;
- break;
- }
- if (x == -1) {
- buf[0] = -i;
- break;
- }
+ buf[0] = (char)((x - 5)&0xff);
+ return 1;
}
- len = i;
- for (i=0;i<=len;i++) {
- w_byte(buf[i], arg);
+ for (i=1;i<(int)sizeof(long)+1;i++) {
+ buf[i] = (char)(x & 0xff);
+ x = RSHIFT(x,8);
+ if (x == 0) {
+ buf[0] = i;
+ break;
+ }
+ if (x == -1) {
+ buf[0] = -i;
+ break;
+ }
}
+ return i+1;
}
#ifdef DBL_MANT_DIG
@@ -218,73 +370,39 @@ w_long(x, arg)
#define MANT_BITS 8
#endif
-static int
-save_mantissa(d, buf)
- double d;
- char *buf;
-{
- int e, i = 0;
- unsigned long m;
- double n;
-
- d = modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
- if (d > 0) {
- buf[i++] = 0;
- do {
- d = modf(ldexp(d, MANT_BITS), &n);
- m = (unsigned long)n;
-#if MANT_BITS > 24
- buf[i++] = m >> 24;
-#endif
-#if MANT_BITS > 16
- buf[i++] = m >> 16;
-#endif
-#if MANT_BITS > 8
- buf[i++] = m >> 8;
-#endif
- buf[i++] = m;
- } while (d > 0);
- while (!buf[i - 1]) --i;
- }
- return i;
-}
-
static double
-load_mantissa(d, buf, len)
- double d;
- const char *buf;
- int len;
+load_mantissa(double d, const char *buf, long len)
{
+ if (!len) return d;
if (--len > 0 && !*buf++) { /* binary mantissa mark */
- int e, s = d < 0, dig = 0;
- unsigned long m;
-
- modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
- do {
- m = 0;
- switch (len) {
- default: m = *buf++ & 0xff;
+ int e, s = d < 0, dig = 0;
+ unsigned long m;
+
+ modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
+ do {
+ m = 0;
+ switch (len) {
+ default: m = *buf++ & 0xff; /* fall through */
#if MANT_BITS > 24
- case 3: m = (m << 8) | (*buf++ & 0xff);
+ case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
#endif
#if MANT_BITS > 16
- case 2: m = (m << 8) | (*buf++ & 0xff);
+ case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
#endif
#if MANT_BITS > 8
- case 1: m = (m << 8) | (*buf++ & 0xff);
+ case 1: m = (m << 8) | (*buf++ & 0xff);
#endif
- }
- dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
- d += ldexp((double)m, dig);
- } while ((len -= MANT_BITS / 8) > 0);
- d = ldexp(d, e - DECIMAL_MANT);
- if (s) d = -d;
+ }
+ dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
+ d += ldexp((double)m, dig);
+ } while ((len -= MANT_BITS / 8) > 0);
+ d = ldexp(d, e - DECIMAL_MANT);
+ if (s) d = -d;
}
return d;
}
#else
#define load_mantissa(d, buf, len) (d)
-#define save_mantissa(d, buf) 0
#endif
#ifdef DBL_DIG
@@ -294,415 +412,743 @@ load_mantissa(d, buf, len)
#endif
static void
-w_float(d, arg)
- double d;
- struct dump_arg *arg;
+w_float(double d, struct dump_arg *arg)
{
- char buf[100];
+ char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
if (isinf(d)) {
- if (d < 0) strcpy(buf, "-inf");
- else strcpy(buf, "inf");
+ if (d < 0) w_cstr("-inf", arg);
+ else w_cstr("inf", arg);
}
else if (isnan(d)) {
- strcpy(buf, "nan");
+ w_cstr("nan", arg);
}
else if (d == 0.0) {
- if (1.0/d < 0) strcpy(buf, "-0");
- else strcpy(buf, "0");
+ if (signbit(d)) w_cstr("-0", arg);
+ else w_cstr("0", arg);
}
else {
- int len;
+ int decpt, sign, digs, len = 0;
+ char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
+ if (sign) buf[len++] = '-';
+ digs = (int)(e - p);
+ if (decpt < -3 || decpt > digs) {
+ buf[len++] = p[0];
+ if (--digs > 0) buf[len++] = '.';
+ memcpy(buf + len, p + 1, digs);
+ len += digs;
+ len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
+ }
+ else if (decpt > 0) {
+ memcpy(buf + len, p, decpt);
+ len += decpt;
+ if ((digs -= decpt) > 0) {
+ buf[len++] = '.';
+ memcpy(buf + len, p + decpt, digs);
+ len += digs;
+ }
+ }
+ else {
+ buf[len++] = '0';
+ buf[len++] = '.';
+ if (decpt) {
+ memset(buf + len, '0', -decpt);
+ len -= decpt;
+ }
+ memcpy(buf + len, p, digs);
+ len += digs;
+ }
+ free(p);
+ w_bytes(buf, len, arg);
+ }
+}
+
- /* xxx: should not use system's sprintf(3) */
- sprintf(buf, "%.*g", FLOAT_DIG, d);
- len = strlen(buf);
- w_bytes(buf, len + save_mantissa(d, buf + len), arg);
- return;
+static VALUE
+w_encivar(VALUE str, struct dump_arg *arg)
+{
+ VALUE encname = encoding_name(str, arg);
+ if (NIL_P(encname) ||
+ is_ascii_string(str)) {
+ return Qnil;
}
- w_bytes(buf, strlen(buf), arg);
+ w_byte(TYPE_IVAR, arg);
+ return encname;
}
static void
-w_symbol(id, arg)
- ID id;
- struct dump_arg *arg;
+w_encname(VALUE encname, struct dump_arg *arg)
+{
+ if (!NIL_P(encname)) {
+ struct dump_call_arg c_arg;
+ c_arg.limit = 1;
+ c_arg.arg = arg;
+ w_long(1L, arg);
+ w_encoding(encname, &c_arg);
+ }
+}
+
+static void
+w_symbol(VALUE sym, struct dump_arg *arg)
{
- char *sym = rb_id2name(id);
st_data_t num;
+ VALUE encname;
- if (st_lookup(arg->symbols, id, &num)) {
- w_byte(TYPE_SYMLINK, arg);
- w_long((long)num, arg);
+ if (st_lookup(arg->symbols, sym, &num)) {
+ w_byte(TYPE_SYMLINK, arg);
+ w_long((long)num, arg);
}
else {
- w_byte(TYPE_SYMBOL, arg);
- w_bytes(sym, strlen(sym), arg);
- st_add_direct(arg->symbols, id, arg->symbols->num_entries);
+ const VALUE orig_sym = sym;
+ sym = rb_sym2str(sym);
+ if (!sym) {
+ rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
+ }
+ encname = w_encivar(sym, arg);
+ w_byte(TYPE_SYMBOL, arg);
+ w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
+ st_add_direct(arg->symbols, orig_sym, arg->symbols->num_entries);
+ w_encname(encname, arg);
}
}
static void
-w_unique(s, arg)
- char *s;
- struct dump_arg *arg;
+w_unique(VALUE s, struct dump_arg *arg)
{
- if (s[0] == '#') {
- rb_raise(rb_eTypeError, "can't dump anonymous class %s", s);
- }
- w_symbol(rb_intern(s), arg);
+ must_not_be_anonymous("class", s);
+ w_symbol(rb_str_intern(s), arg);
}
-static void w_object _((VALUE,struct dump_arg*,int));
+static void w_object(VALUE,struct dump_arg*,int);
static int
-hash_each(key, value, arg)
- VALUE key, value;
- struct dump_call_arg *arg;
+hash_each(VALUE key, VALUE value, VALUE v)
{
+ struct dump_call_arg *arg = (void *)v;
w_object(key, arg->arg, arg->limit);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
+#define SINGLETON_DUMP_UNABLE_P(klass) \
+ (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
+ rb_ivar_count(klass) > 0)
+
static void
-w_extended(klass, arg, check)
- VALUE klass;
- struct dump_arg *arg;
- int check;
+w_extended(VALUE klass, struct dump_arg *arg, int check)
{
- char *path;
-
- if (check && FL_TEST(klass, FL_SINGLETON)) {
- if (RCLASS(klass)->m_tbl->num_entries ||
- (RCLASS(klass)->iv_tbl && RCLASS(klass)->iv_tbl->num_entries > 1)) {
- rb_raise(rb_eTypeError, "singleton can't be dumped");
- }
- klass = RCLASS(klass)->super;
+ if (check && RCLASS_SINGLETON_P(klass)) {
+ VALUE origin = RCLASS_ORIGIN(klass);
+ if (SINGLETON_DUMP_UNABLE_P(klass) ||
+ (origin != klass && SINGLETON_DUMP_UNABLE_P(origin))) {
+ rb_raise(rb_eTypeError, "singleton can't be dumped");
+ }
+ klass = RCLASS_SUPER(klass);
}
while (BUILTIN_TYPE(klass) == T_ICLASS) {
- path = rb_class2name(RBASIC(klass)->klass);
- w_byte(TYPE_EXTENDED, arg);
- w_unique(path, arg);
- klass = RCLASS(klass)->super;
+ if (!RICLASS_IS_ORIGIN_P(klass) ||
+ BUILTIN_TYPE(RBASIC(klass)->klass) != T_MODULE) {
+ VALUE path = rb_class_name(RBASIC(klass)->klass);
+ w_byte(TYPE_EXTENDED, arg);
+ w_unique(path, arg);
+ }
+ klass = RCLASS_SUPER(klass);
}
}
static void
-w_class(type, obj, arg, check)
- int type;
- VALUE obj;
- struct dump_arg *arg;
- int check;
+w_class(char type, VALUE obj, struct dump_arg *arg, int check)
{
- char *path;
+ VALUE path;
+ st_data_t real_obj;
+ VALUE klass;
- VALUE klass = CLASS_OF(obj);
+ if (arg->compat_tbl &&
+ st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
+ obj = (VALUE)real_obj;
+ }
+ klass = CLASS_OF(obj);
w_extended(klass, arg, check);
w_byte(type, arg);
- path = RSTRING(class2path(rb_class_real(klass)))->ptr;
+ path = class2path(rb_class_real(klass));
w_unique(path, arg);
}
static void
-w_uclass(obj, base_klass, arg)
- VALUE obj, base_klass;
- struct dump_arg *arg;
+w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
{
VALUE klass = CLASS_OF(obj);
- w_extended(klass, arg, Qtrue);
+ w_extended(klass, arg, TRUE);
klass = rb_class_real(klass);
- if (klass != base_klass) {
- w_byte(TYPE_UCLASS, arg);
- w_unique(RSTRING(class2path(klass))->ptr, arg);
+ if (klass != super) {
+ w_byte(TYPE_UCLASS, arg);
+ w_unique(class2path(klass), arg);
}
}
+static bool
+rb_hash_ruby2_keywords_p(VALUE obj)
+{
+ return (RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS) != 0;
+}
+
+static void
+rb_hash_ruby2_keywords(VALUE obj)
+{
+ RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
+}
+
+/*
+ * if instance variable name `id` is a special name to be skipped,
+ * returns the name of it. otherwise it cannot be dumped (unnamed),
+ * returns `name` as-is. returns NULL for ID that can be dumped.
+ */
+static inline const char *
+skipping_ivar_name(const ID id, const char *name)
+{
+#define IS_SKIPPED_IVAR(idname) \
+ ((id == idname) && (name = name_##idname, true))
+ if (IS_SKIPPED_IVAR(s_encoding_short)) return name;
+ if (IS_SKIPPED_IVAR(s_ruby2_keywords_flag)) return name;
+ if (IS_SKIPPED_IVAR(s_encoding_long)) return name;
+ if (!rb_id2str(id)) return name;
+ return NULL;
+}
+
+struct w_ivar_arg {
+ struct dump_call_arg *dump;
+ st_data_t num_ivar;
+};
+
static int
-w_obj_each(id, value, arg)
- ID id;
- VALUE value;
- struct dump_call_arg *arg;
+w_obj_each(ID id, VALUE value, st_data_t a)
{
- w_symbol(id, arg->arg);
+ struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
+ struct dump_call_arg *arg = ivarg->dump;
+ const char unnamed[] = "", *ivname = skipping_ivar_name(id, unnamed);
+
+ if (ivname) {
+ if (ivname != unnamed) {
+ rb_warn("instance variable '%s' on class %"PRIsVALUE" is not dumped",
+ ivname, CLASS_OF(arg->obj));
+ }
+ return ST_CONTINUE;
+ }
+ --ivarg->num_ivar;
+ w_symbol(ID2SYM(id), arg->arg);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
-static void
-w_ivar(tbl, arg)
- st_table *tbl;
- struct dump_call_arg *arg;
+static int
+obj_count_ivars(ID id, VALUE val, st_data_t a)
{
- if (tbl) {
- w_long(tbl->num_entries, arg->arg);
- st_foreach_safe(tbl, w_obj_each, (st_data_t)arg);
+ if (!skipping_ivar_name(id, "") && UNLIKELY(!++*(st_index_t *)a)) {
+ rb_raise(rb_eRuntimeError, "too many instance variables");
+ }
+ return ST_CONTINUE;
+}
+
+static VALUE
+encoding_name(VALUE obj, struct dump_arg *arg)
+{
+ if (rb_enc_capable(obj)) {
+ int encidx = rb_enc_get_index(obj);
+ rb_encoding *enc = 0;
+ st_data_t name;
+
+ if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
+ return Qnil;
+ }
+
+ /* special treatment for US-ASCII and UTF-8 */
+ if (encidx == rb_usascii_encindex()) {
+ return Qfalse;
+ }
+ else if (encidx == rb_utf8_encindex()) {
+ return Qtrue;
+ }
+
+ if (arg->encodings ?
+ !st_lookup(arg->encodings, (st_data_t)rb_enc_name(enc), &name) :
+ (arg->encodings = st_init_strcasetable(), 1)) {
+ name = (st_data_t)rb_str_new_cstr(rb_enc_name(enc));
+ st_insert(arg->encodings, (st_data_t)rb_enc_name(enc), name);
+ }
+ return (VALUE)name;
}
else {
- w_long(0, arg->arg);
+ return Qnil;
}
}
+static int
+w_encoding(VALUE encname, struct dump_call_arg *arg)
+{
+ int limit = arg->limit;
+ if (limit >= 0) ++limit;
+ switch (encname) {
+ case Qfalse:
+ case Qtrue:
+ w_symbol(ID2SYM(s_encoding_short), arg->arg);
+ w_object(encname, arg->arg, limit);
+ return 1;
+ case Qnil:
+ return 0;
+ }
+ w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
+ w_object(encname, arg->arg, limit);
+ return 1;
+}
+
+static st_index_t
+has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
+{
+ st_index_t num = !NIL_P(encname);
+
+ if (SPECIAL_CONST_P(obj)) goto generic;
+ switch (BUILTIN_TYPE(obj)) {
+ case T_OBJECT:
+ case T_CLASS:
+ case T_MODULE:
+ break; /* counted elsewhere */
+ case T_HASH:
+ if (rb_hash_ruby2_keywords_p(obj)) ++num;
+ /* fall through */
+ default:
+ generic:
+ rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
+ if (num) *ivobj = obj;
+ }
+
+ return num;
+}
+
static void
-w_object(obj, arg, limit)
- VALUE obj;
- struct dump_arg *arg;
- int limit;
+w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
{
- struct dump_call_arg c_arg;
- st_table *ivtbl = 0;
- st_data_t num;
+ struct w_ivar_arg ivarg = {arg, num};
+ if (!num) return;
+ rb_ivar_foreach_buffered(obj, w_obj_each, (st_data_t)&ivarg);
+}
- if (limit == 0) {
- rb_raise(rb_eArgError, "exceed depth limit");
+static void
+w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
+{
+ w_long(num, arg->arg);
+ num -= w_encoding(encname, arg);
+ if (RB_TYPE_P(ivobj, T_HASH) && rb_hash_ruby2_keywords_p(ivobj)) {
+ int limit = arg->limit;
+ if (limit >= 0) ++limit;
+ w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
+ w_object(Qtrue, arg->arg, limit);
+ num--;
+ }
+ if (!UNDEF_P(ivobj) && num) {
+ w_ivar_each(ivobj, num, arg);
+ }
+}
+
+static void
+w_objivar(VALUE obj, struct dump_call_arg *arg)
+{
+ st_data_t num = 0;
+
+ rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
+ w_long(num, arg->arg);
+ w_ivar_each(obj, num, arg);
+}
+
+#if SIZEOF_LONG > 4
+// Optimized dump for fixnum larger than 31-bits
+static void
+w_bigfixnum(VALUE obj, struct dump_arg *arg)
+{
+ RUBY_ASSERT(FIXNUM_P(obj));
+
+ w_byte(TYPE_BIGNUM, arg);
+
+#if SIZEOF_LONG == SIZEOF_VALUE
+ long num, slen_num;
+ num = FIX2LONG(obj);
+#else
+ long long num, slen_num;
+ num = NUM2LL(obj);
+#endif
+
+ char sign = num < 0 ? '-' : '+';
+ w_byte(sign, arg);
+
+ // Guaranteed not to overflow, as FIXNUM is 1-bit less than long
+ if (num < 0) num = -num;
+
+ // calculate the size in shorts
+ int slen = 0;
+ {
+ slen_num = num;
+ while (slen_num) {
+ slen++;
+ slen_num = SHORTDN(slen_num);
+ }
}
- limit--;
- c_arg.limit = limit;
- c_arg.arg = arg;
+ RUBY_ASSERT(slen > 0 && slen <= SIZEOF_LONG / 2);
- if (st_lookup(arg->data, obj, &num)) {
- w_byte(TYPE_LINK, arg);
- w_long((long)num, arg);
- return;
+ w_long((long)slen, arg);
+
+ for (int i = 0; i < slen; i++) {
+ w_short(num & SHORTMASK, arg);
+ num = SHORTDN(num);
}
- if (ivtbl = rb_generic_ivar_table(obj)) {
- w_byte(TYPE_IVAR, arg);
+ // We aren't adding this object to the link table, but we need to increment
+ // the index.
+ arg->num_entries++;
+
+ RUBY_ASSERT(num == 0);
+}
+#endif
+
+static void
+w_remember(VALUE obj, struct dump_arg *arg)
+{
+ st_add_direct(arg->data, obj, arg->num_entries++);
+}
+
+static void
+w_object(VALUE obj, struct dump_arg *arg, int limit)
+{
+ struct dump_call_arg c_arg;
+ VALUE ivobj = Qundef;
+ st_data_t num;
+ st_index_t hasiv = 0;
+ VALUE encname = Qnil;
+
+ if (limit == 0) {
+ rb_raise(rb_eArgError, "exceed depth limit");
}
- if (obj == Qnil) {
- w_byte(TYPE_NIL, arg);
+
+ if (NIL_P(obj)) {
+ w_byte(TYPE_NIL, arg);
}
else if (obj == Qtrue) {
- w_byte(TYPE_TRUE, arg);
+ w_byte(TYPE_TRUE, arg);
}
else if (obj == Qfalse) {
- w_byte(TYPE_FALSE, arg);
+ w_byte(TYPE_FALSE, arg);
}
else if (FIXNUM_P(obj)) {
#if SIZEOF_LONG <= 4
- w_byte(TYPE_FIXNUM, arg);
- w_long(FIX2INT(obj), arg);
+ w_byte(TYPE_FIXNUM, arg);
+ w_long(FIX2INT(obj), arg);
#else
- if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
- w_byte(TYPE_FIXNUM, arg);
- w_long(FIX2LONG(obj), arg);
- }
- else {
- w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
- }
+ if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
+ w_byte(TYPE_FIXNUM, arg);
+ w_long(FIX2LONG(obj), arg);
+ }
+ else {
+ w_bigfixnum(obj, arg);
+ }
#endif
}
else if (SYMBOL_P(obj)) {
- w_symbol(SYM2ID(obj), arg);
+ w_symbol(obj, arg);
}
else {
- if (OBJ_TAINTED(obj)) arg->taint = Qtrue;
-
- st_add_direct(arg->data, obj, arg->data->num_entries);
- if (rb_respond_to(obj, s_mdump)) {
- VALUE v;
-
- v = rb_funcall(obj, s_mdump, 0, 0);
- w_class(TYPE_USRMARSHAL, obj, arg, Qfalse);
- w_object(v, arg, limit);
- if (ivtbl) w_ivar(0, &c_arg);
- return;
- }
- if (rb_respond_to(obj, s_dump)) {
- VALUE v;
-
- v = rb_funcall(obj, s_dump, 1, INT2NUM(limit));
- if (TYPE(v) != T_STRING) {
- rb_raise(rb_eTypeError, "_dump() must return string");
- }
- if (!ivtbl && (ivtbl = rb_generic_ivar_table(v))) {
- w_byte(TYPE_IVAR, arg);
- }
- w_class(TYPE_USERDEF, obj, arg, Qfalse);
- w_bytes(RSTRING(v)->ptr, RSTRING(v)->len, arg);
- if (ivtbl) {
- w_ivar(ivtbl, &c_arg);
- }
- return;
- }
-
- switch (BUILTIN_TYPE(obj)) {
- case T_CLASS:
- if (FL_TEST(obj, FL_SINGLETON)) {
- rb_raise(rb_eTypeError, "singleton class can't be dumped");
- }
- w_byte(TYPE_CLASS, arg);
- {
- VALUE path = class2path(obj);
- w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
- }
- break;
-
- case T_MODULE:
- w_byte(TYPE_MODULE, arg);
- {
- VALUE path = class2path(obj);
- w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
- }
- break;
-
- case T_FLOAT:
- w_byte(TYPE_FLOAT, arg);
- w_float(RFLOAT(obj)->value, arg);
- break;
-
- case T_BIGNUM:
- w_byte(TYPE_BIGNUM, arg);
- {
- char sign = RBIGNUM(obj)->sign ? '+' : '-';
- long len = RBIGNUM(obj)->len;
- BDIGIT *d = RBIGNUM(obj)->digits;
-
- w_byte(sign, arg);
- w_long(SHORTLEN(len), arg); /* w_short? */
- while (len--) {
-#if SIZEOF_BDIGITS > SIZEOF_SHORT
- BDIGIT num = *d;
- int i;
-
- for (i=0; i<SIZEOF_BDIGITS; i+=SIZEOF_SHORT) {
- w_short(num & SHORTMASK, arg);
- num = SHORTDN(num);
- if (len == 0 && num == 0) break;
- }
+ if (st_lookup(arg->data, obj, &num)) {
+ w_byte(TYPE_LINK, arg);
+ w_long((long)num, arg);
+ return;
+ }
+
+ if (limit > 0) limit--;
+ c_arg.limit = limit;
+ c_arg.arg = arg;
+ c_arg.obj = obj;
+
+ if (FLONUM_P(obj)) {
+ w_remember(obj, arg);
+ w_byte(TYPE_FLOAT, arg);
+ w_float(RFLOAT_VALUE(obj), arg);
+ return;
+ }
+
+ VALUE v;
+
+ if (!RBASIC_CLASS(obj)) {
+ rb_raise(rb_eTypeError, "can't dump internal %s",
+ rb_builtin_type_name(BUILTIN_TYPE(obj)));
+ }
+
+ if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
+ w_remember(obj, arg);
+
+ v = dump_funcall(arg, obj, s_mdump, 0, 0);
+ w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
+ w_object(v, arg, limit);
+ return;
+ }
+ if (rb_obj_respond_to(obj, s_dump, TRUE)) {
+ VALUE ivobj2 = Qundef;
+ st_index_t hasiv2;
+ VALUE encname2;
+
+ if (arg->userdefs && st_is_member(arg->userdefs, (st_data_t)obj)) {
+ rb_raise(rb_eRuntimeError, "can't dump recursive object using _dump()");
+ }
+ v = INT2NUM(limit);
+ v = dump_funcall(arg, obj, s_dump, 1, &v);
+ if (!RB_TYPE_P(v, T_STRING)) {
+ rb_raise(rb_eTypeError, "_dump() must return string");
+ }
+ hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
+ hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
+ if (hasiv2) {
+ hasiv = hasiv2;
+ ivobj = ivobj2;
+ encname = encname2;
+ }
+ if (hasiv) w_byte(TYPE_IVAR, arg);
+ w_class(TYPE_USERDEF, obj, arg, FALSE);
+ w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
+ if (hasiv) {
+ st_data_t userdefs = (st_data_t)obj;
+ if (!arg->userdefs) {
+ arg->userdefs = rb_init_identtable();
+ }
+ st_add_direct(arg->userdefs, userdefs, 0);
+ w_ivar(hasiv, ivobj, encname, &c_arg);
+ st_delete(arg->userdefs, &userdefs, NULL);
+ }
+ w_remember(obj, arg);
+ return;
+ }
+
+ w_remember(obj, arg);
+
+ hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
+ {
+ st_data_t compat_data;
+ VALUE klass = CLASS_OF(obj);
+ rb_alloc_func_t allocator = RCLASS_SINGLETON_P(klass) ? 0 : rb_get_alloc_func(klass);
+ if (allocator && st_lookup(compat_allocator_tbl,
+ (st_data_t)allocator,
+ &compat_data)) {
+ marshal_compat_t *compat = (marshal_compat_t*)compat_data;
+ VALUE real_obj = obj;
+ obj = compat->dumper(real_obj);
+ if (!arg->compat_tbl) {
+ arg->compat_tbl = rb_init_identtable();
+ }
+ st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
+ if (obj != real_obj && UNDEF_P(ivobj)) hasiv = 0;
+ }
+ }
+ if (hasiv) w_byte(TYPE_IVAR, arg);
+
+ switch (BUILTIN_TYPE(obj)) {
+ case T_CLASS:
+ if (FL_TEST(obj, FL_SINGLETON)) {
+ rb_raise(rb_eTypeError, "singleton class can't be dumped");
+ }
+ {
+ VALUE path = class2path(obj);
+ VALUE encname = w_encivar(path, arg);
+ w_byte(TYPE_CLASS, arg);
+ w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
+ w_encname(encname, arg);
+ RB_GC_GUARD(path);
+ }
+ break;
+
+ case T_MODULE:
+ {
+ VALUE path = class2path(obj);
+ VALUE encname = w_encivar(path, arg);
+ w_byte(TYPE_MODULE, arg);
+ w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
+ w_encname(encname, arg);
+ RB_GC_GUARD(path);
+ }
+ break;
+
+ case T_FLOAT:
+ w_byte(TYPE_FLOAT, arg);
+ w_float(RFLOAT_VALUE(obj), arg);
+ break;
+
+ case T_BIGNUM:
+ w_byte(TYPE_BIGNUM, arg);
+ {
+ char sign = BIGNUM_SIGN(obj) ? '+' : '-';
+ size_t len = BIGNUM_LEN(obj);
+ size_t slen;
+ size_t j;
+ BDIGIT *d = BIGNUM_DIGITS(obj);
+
+ slen = SHORTLEN(len);
+ if (LONG_MAX < slen) {
+ rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
+ }
+
+ w_byte(sign, arg);
+ w_long((long)slen, arg);
+ for (j = 0; j < len; j++) {
+#if SIZEOF_BDIGIT > SIZEOF_SHORT
+ BDIGIT num = *d;
+ int i;
+
+ for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
+ w_short(num & SHORTMASK, arg);
+ num = SHORTDN(num);
+ if (j == len - 1 && num == 0) break;
+ }
#else
- w_short(*d, arg);
+ w_short(*d, arg);
#endif
- d++;
- }
- }
- break;
-
- case T_STRING:
- w_uclass(obj, rb_cString, arg);
- w_byte(TYPE_STRING, arg);
- w_bytes(RSTRING(obj)->ptr, RSTRING(obj)->len, arg);
- break;
-
- case T_REGEXP:
- w_uclass(obj, rb_cRegexp, arg);
- w_byte(TYPE_REGEXP, arg);
- w_bytes(RREGEXP(obj)->str, RREGEXP(obj)->len, arg);
- w_byte(rb_reg_options(obj), arg);
- break;
-
- case T_ARRAY:
- w_uclass(obj, rb_cArray, arg);
- w_byte(TYPE_ARRAY, arg);
- {
- long len = RARRAY(obj)->len;
- VALUE *ptr = RARRAY(obj)->ptr;
-
- w_long(len, arg);
- while (len--) {
- w_object(*ptr, arg, limit);
- ptr++;
- }
- }
- break;
-
- case T_HASH:
- w_uclass(obj, rb_cHash, arg);
- if (NIL_P(RHASH(obj)->ifnone)) {
- w_byte(TYPE_HASH, arg);
- }
- else if (FL_TEST(obj, FL_USER2)) {
- /* FL_USER2 means HASH_PROC_DEFAULT (see hash.c) */
- rb_raise(rb_eTypeError, "can't dump hash with default proc");
- }
- else {
- w_byte(TYPE_HASH_DEF, arg);
- }
- w_long(RHASH(obj)->tbl->num_entries, arg);
- rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
- if (!NIL_P(RHASH(obj)->ifnone)) {
- w_object(RHASH(obj)->ifnone, arg, limit);
- }
- break;
-
- case T_STRUCT:
- w_class(TYPE_STRUCT, obj, arg, Qtrue);
- {
- long len = RSTRUCT(obj)->len;
- VALUE mem;
- long i;
-
- w_long(len, arg);
- mem = rb_struct_members(obj);
- for (i=0; i<len; i++) {
- w_symbol(SYM2ID(RARRAY(mem)->ptr[i]), arg);
- w_object(RSTRUCT(obj)->ptr[i], arg, limit);
- }
- }
- break;
-
- case T_OBJECT:
- w_class(TYPE_OBJECT, obj, arg, Qtrue);
- w_ivar(ROBJECT(obj)->iv_tbl, &c_arg);
- break;
-
- case T_DATA:
- {
- VALUE v;
-
- if (!rb_respond_to(obj, s_dump_data)) {
- rb_raise(rb_eTypeError,
- "no marshal_dump is defined for class %s",
- rb_obj_classname(obj));
- }
- v = rb_funcall(obj, s_dump_data, 0);
- w_class(TYPE_DATA, obj, arg, Qtrue);
- w_object(v, arg, limit);
- }
- break;
-
- default:
- rb_raise(rb_eTypeError, "can't dump %s",
- rb_obj_classname(obj));
- break;
- }
- }
- if (ivtbl) {
- w_ivar(ivtbl, &c_arg);
- }
-}
+ d++;
+ }
+ }
+ break;
+
+ case T_STRING:
+ w_uclass(obj, rb_cString, arg);
+ w_byte(TYPE_STRING, arg);
+ w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
+ break;
+
+ case T_REGEXP:
+ w_uclass(obj, rb_cRegexp, arg);
+ w_byte(TYPE_REGEXP, arg);
+ {
+ int opts = rb_reg_options(obj);
+ w_bytes(RREGEXP_SRC_PTR(obj), RREGEXP_SRC_LEN(obj), arg);
+ w_byte((char)opts, arg);
+ }
+ break;
+
+ case T_ARRAY:
+ w_uclass(obj, rb_cArray, arg);
+ w_byte(TYPE_ARRAY, arg);
+ {
+ long i, len = RARRAY_LEN(obj);
+
+ w_long(len, arg);
+ for (i=0; i<RARRAY_LEN(obj); i++) {
+ w_object(RARRAY_AREF(obj, i), arg, limit);
+ if (len != RARRAY_LEN(obj)) {
+ rb_raise(rb_eRuntimeError, "array modified during dump");
+ }
+ }
+ }
+ break;
-static VALUE
-dump(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);
+ case T_HASH:
+ w_uclass(obj, rb_cHash, arg);
+ if (rb_hash_compare_by_id_p(obj)) {
+ w_byte(TYPE_UCLASS, arg);
+ w_symbol(rb_sym_intern_ascii_cstr("Hash"), arg);
+ }
+ if (NIL_P(RHASH_IFNONE(obj))) {
+ w_byte(TYPE_HASH, arg);
+ }
+ else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
+ rb_raise(rb_eTypeError, "can't dump hash with default proc");
+ }
+ else {
+ w_byte(TYPE_HASH_DEF, arg);
+ }
+ w_long(rb_hash_size_num(obj), arg);
+ rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
+ if (!NIL_P(RHASH_IFNONE(obj))) {
+ w_object(RHASH_IFNONE(obj), arg, limit);
+ }
+ break;
+
+ case T_STRUCT:
+ w_class(TYPE_STRUCT, obj, arg, TRUE);
+ {
+ long len = RSTRUCT_LEN_RAW(obj);
+ VALUE mem;
+ long i;
+
+ w_long(len, arg);
+ mem = rb_struct_members(obj);
+ for (i=0; i<len; i++) {
+ w_symbol(RARRAY_AREF(mem, i), arg);
+ w_object(RSTRUCT_GET_RAW(obj, i), arg, limit);
+ }
+ }
+ break;
+
+ case T_OBJECT:
+ w_class(TYPE_OBJECT, obj, arg, TRUE);
+ w_objivar(obj, &c_arg);
+ break;
+
+ case T_DATA:
+ {
+ VALUE v;
+
+ if (!rb_obj_respond_to(obj, s_dump_data, TRUE)) {
+ rb_raise(rb_eTypeError,
+ "no _dump_data is defined for class %"PRIsVALUE,
+ rb_obj_class(obj));
+ }
+ v = dump_funcall(arg, obj, s_dump_data, 0, 0);
+ w_class(TYPE_DATA, obj, arg, TRUE);
+ w_object(v, arg, limit);
+ }
+ break;
+
+ default:
+ rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
+ rb_obj_class(obj));
+ break;
+ }
+ RB_GC_GUARD(obj);
+ }
+ if (hasiv) {
+ w_ivar(hasiv, ivobj, encname, &c_arg);
}
- return 0;
}
-static VALUE
-dump_ensure(arg)
- struct dump_arg *arg;
+static void
+clear_dump_arg(struct dump_arg *arg)
{
+ if (!arg->symbols) return;
st_free_table(arg->symbols);
+ arg->symbols = 0;
st_free_table(arg->data);
- if (arg->taint) {
- OBJ_TAINT(arg->str);
+ arg->data = 0;
+ arg->num_entries = 0;
+ if (arg->compat_tbl) {
+ st_free_table(arg->compat_tbl);
+ arg->compat_tbl = 0;
+ }
+ if (arg->encodings) {
+ st_free_table(arg->encodings);
+ arg->encodings = 0;
}
- return 0;
+ if (arg->userdefs) {
+ st_free_table(arg->userdefs);
+ arg->userdefs = 0;
+ }
+}
+
+NORETURN(static inline void io_needed(void));
+static inline void
+io_needed(void)
+{
+ rb_raise(rb_eTypeError, "instance of IO needed");
}
/*
* call-seq:
- * dump( obj [, anIO] , limit=--1 ) => anIO
+ * dump( obj [, anIO] , limit=-1 ) -> anIO
*
- * Serializes obj and all descendent objects. If anIO is
+ * Serializes obj and all descendant objects. If anIO is
* specified, the serialized data will be written to it, otherwise the
* data will be returned as a String. If limit is specified, the
* traversal of subobjects will be limited to that depth. If limit is
@@ -712,7 +1158,7 @@ dump_ensure(arg)
* def initialize(str)
* @str = str
* end
- * def sayHello
+ * def say_hello
* @str
* end
* end
@@ -722,720 +1168,1299 @@ dump_ensure(arg)
* o = Klass.new("hello\n")
* data = Marshal.dump(o)
* obj = Marshal.load(data)
- * obj.sayHello #=> "hello\n"
+ * obj.say_hello #=> "hello\n"
+ *
+ * Marshal can't dump following objects:
+ * * anonymous Class/Module.
+ * * objects which are related to system (ex: Dir, File::Stat, IO, File, Socket
+ * and so on)
+ * * an instance of MatchData, Method, UnboundMethod, Proc, Thread,
+ * ThreadGroup, Continuation
+ * * objects which define singleton methods
*/
static VALUE
-marshal_dump(argc, argv)
- int argc;
- VALUE* argv;
+marshal_dump(int argc, VALUE *argv, VALUE _)
{
VALUE obj, port, a1, a2;
int limit = -1;
- struct dump_arg arg;
- struct dump_call_arg c_arg;
port = Qnil;
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
if (argc == 3) {
- if (!NIL_P(a2)) limit = NUM2INT(a2);
- if (NIL_P(a1)) goto type_error;
- port = a1;
+ if (!NIL_P(a2)) limit = NUM2INT(a2);
+ if (NIL_P(a1)) io_needed();
+ port = a1;
}
else if (argc == 2) {
- if (FIXNUM_P(a1)) limit = FIX2INT(a1);
- else if (NIL_P(a1)) goto type_error;
- else port = a1;
+ if (FIXNUM_P(a1)) limit = FIX2INT(a1);
+ else if (NIL_P(a1)) io_needed();
+ else port = a1;
}
- arg.dest = 0;
+ return rb_marshal_dump_limited(obj, port, limit);
+}
+
+VALUE
+rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
+{
+ struct dump_arg *arg;
+ VALUE wrapper; /* used to avoid memory leak in case of exception */
+
+ wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
+ arg->dest = 0;
+ arg->symbols = st_init_numtable();
+ arg->data = rb_init_identtable();
+ arg->num_entries = 0;
+ arg->compat_tbl = 0;
+ arg->encodings = 0;
+ arg->userdefs = 0;
+ arg->str = rb_str_buf_new(0);
if (!NIL_P(port)) {
- if (!rb_respond_to(port, s_write)) {
- type_error:
- rb_raise(rb_eTypeError, "instance of IO needed");
- }
- arg.str = rb_str_buf_new(0);
- arg.dest = port;
- if (rb_respond_to(port, s_binmode)) {
- rb_funcall2(port, s_binmode, 0, 0);
- }
+ if (!rb_respond_to(port, s_write)) {
+ io_needed();
+ }
+ arg->dest = port;
+ dump_check_funcall(arg, port, s_binmode, 0, 0);
}
else {
- port = rb_str_buf_new(0);
- arg.str = port;
+ port = arg->str;
}
- arg.symbols = st_init_numtable();
- arg.data = st_init_numtable();
- arg.taint = Qfalse;
- c_arg.obj = obj;
- c_arg.arg = &arg;
- c_arg.limit = limit;
-
- w_byte(MARSHAL_MAJOR, &arg);
- w_byte(MARSHAL_MINOR, &arg);
+ w_byte(MARSHAL_MAJOR, arg);
+ w_byte(MARSHAL_MINOR, arg);
- rb_ensure(dump, (VALUE)&c_arg, dump_ensure, (VALUE)&arg);
+ w_object(obj, arg, limit);
+ if (arg->dest) {
+ rb_io_write(arg->dest, arg->str);
+ rb_str_resize(arg->str, 0);
+ }
+ clear_dump_arg(arg);
+ RB_GC_GUARD(wrapper);
return port;
}
struct load_arg {
VALUE src;
+ char *buf;
+ long bufsize;
+ long buflen;
+ long readable;
long offset;
st_table *symbols;
- VALUE data;
+ st_table *data;
+ st_table *partial_objects;
VALUE proc;
- int taint;
+ st_table *compat_tbl;
+ bool freeze;
};
-static VALUE r_object _((struct load_arg *arg));
+static VALUE
+check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
+{
+ if (!arg->symbols) {
+ rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
+ name);
+ }
+ return ret;
+}
+#define load_funcall(arg, obj, sym, argc, argv) \
+ check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
+
+static void clear_load_arg(struct load_arg *arg);
+
+static void
+mark_load_arg(void *ptr)
+{
+ struct load_arg *p = ptr;
+ if (!p->symbols)
+ return;
+ rb_mark_tbl(p->symbols);
+ rb_mark_tbl(p->data);
+ rb_mark_tbl(p->partial_objects);
+ rb_mark_hash(p->compat_tbl);
+}
+
+static void
+free_load_arg(void *ptr)
+{
+ clear_load_arg(ptr);
+}
+
+static size_t
+memsize_load_arg(const void *ptr)
+{
+ const struct load_arg *p = (struct load_arg *)ptr;
+ size_t memsize = 0;
+ if (p->symbols) memsize += rb_st_memsize(p->symbols);
+ if (p->data) memsize += rb_st_memsize(p->data);
+ if (p->partial_objects) memsize += rb_st_memsize(p->partial_objects);
+ if (p->compat_tbl) memsize += rb_st_memsize(p->compat_tbl);
+ return memsize;
+}
+
+static const rb_data_type_t load_arg_data = {
+ "load_arg",
+ {mark_load_arg, free_load_arg, memsize_load_arg,},
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
+};
+
+#define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
+static VALUE r_object(struct load_arg *arg);
+static VALUE r_symbol(struct load_arg *arg);
+
+NORETURN(static void too_short(void));
+static void
+too_short(void)
+{
+ rb_raise(rb_eArgError, "marshal data too short");
+}
+
+static st_index_t
+r_prepare(struct load_arg *arg)
+{
+ st_index_t idx = arg->data->num_entries;
+
+ st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
+ return idx;
+}
+
+static unsigned char
+r_byte1_buffered(struct load_arg *arg)
+{
+ if (arg->buflen == 0) {
+ long readable = arg->readable < arg->bufsize ? arg->readable : arg->bufsize;
+ long read_len;
+ VALUE str, n = LONG2NUM(readable);
+
+ str = load_funcall(arg, arg->src, s_read, 1, &n);
+ if (NIL_P(str)) too_short();
+ StringValue(str);
+ read_len = RSTRING_LEN(str);
+ if (UNLIKELY(read_len < readable)) too_short();
+ if (UNLIKELY(read_len > arg->bufsize)) {
+ arg->buf = ruby_sized_realloc_n(arg->buf, read_len, 1, arg->bufsize);
+ arg->bufsize = read_len;
+ }
+ memcpy(arg->buf, RSTRING_PTR(str), read_len);
+ arg->offset = 0;
+ arg->buflen = read_len;
+ RB_GC_GUARD(str);
+ }
+ arg->buflen--;
+ return arg->buf[arg->offset++];
+}
static int
-r_byte(arg)
- struct load_arg *arg;
+r_byte(struct load_arg *arg)
{
int c;
- if (TYPE(arg->src) == T_STRING) {
- if (RSTRING(arg->src)->len > arg->offset) {
- c = (unsigned char)RSTRING(arg->src)->ptr[arg->offset++];
- }
- else {
- rb_raise(rb_eArgError, "marshal data too short");
- }
+ if (RB_TYPE_P(arg->src, T_STRING)) {
+ if (RSTRING_LEN(arg->src) > arg->offset) {
+ c = (unsigned char)RSTRING_PTR(arg->src)[arg->offset++];
+ }
+ else {
+ too_short();
+ }
}
else {
- VALUE src = arg->src;
- VALUE v = rb_funcall2(src, s_getc, 0, 0);
- if (NIL_P(v)) rb_eof_error();
- c = (unsigned char)FIX2INT(v);
+ if (arg->readable >0 || arg->buflen > 0) {
+ c = r_byte1_buffered(arg);
+ }
+ else {
+ VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0);
+ if (NIL_P(v)) rb_eof_error();
+ c = (unsigned char)NUM2CHR(v);
+ }
}
return c;
}
+NORETURN(static void long_toobig(int size));
+
static void
-long_toobig(size)
- int size;
+long_toobig(int size)
{
- rb_raise(rb_eTypeError, "long too big for this architecture (size %d, given %d)",
- sizeof(long), size);
+ rb_raise(rb_eTypeError, "long too big for this architecture (size "
+ STRINGIZE(SIZEOF_LONG)", given %d)", size);
}
-#undef SIGN_EXTEND_CHAR
-#if __STDC__
-# define SIGN_EXTEND_CHAR(c) ((signed char)(c))
-#else /* not __STDC__ */
-/* As in Harbison and Steele. */
-# define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
-#endif
-
static long
-r_long(arg)
- struct load_arg *arg;
+r_long(struct load_arg *arg)
{
register long x;
- int c = SIGN_EXTEND_CHAR(r_byte(arg));
+ int c = (signed char)r_byte(arg);
long i;
if (c == 0) return 0;
if (c > 0) {
- if (4 < c && c < 128) {
- return c - 5;
- }
- if (c > sizeof(long)) long_toobig(c);
- x = 0;
- for (i=0;i<c;i++) {
- x |= (long)r_byte(arg) << (8*i);
- }
+ if (4 < c && c < 128) {
+ return c - 5;
+ }
+ if (c > (int)sizeof(long)) long_toobig(c);
+ x = 0;
+ for (i=0;i<c;i++) {
+ x |= (long)r_byte(arg) << (8*i);
+ }
}
else {
- if (-129 < c && c < -4) {
- return c + 5;
- }
- c = -c;
- if (c > sizeof(long)) long_toobig(c);
- x = -1;
- for (i=0;i<c;i++) {
- x &= ~((long)0xff << (8*i));
- x |= (long)r_byte(arg) << (8*i);
- }
+ if (-129 < c && c < -4) {
+ return c + 5;
+ }
+ c = -c;
+ if (c > (int)sizeof(long)) long_toobig(c);
+ x = -1;
+ for (i=0;i<c;i++) {
+ x &= ~((long)0xff << (8*i));
+ x |= (long)r_byte(arg) << (8*i);
+ }
}
return x;
}
+long
+ruby_marshal_read_long(const char **buf, long len)
+{
+ long x;
+ struct RString src = {RBASIC_INIT};
+ struct load_arg arg;
+ memset(&arg, 0, sizeof(arg));
+ arg.src = rb_setup_fake_str(&src, *buf, len, 0);
+ x = r_long(&arg);
+ *buf += arg.offset;
+ return x;
+}
+
+static long
+r_keep_readable(struct load_arg *arg, long len, size_t size)
+{
+ if (UNLIKELY(len < 0)) {
+ rb_raise(rb_eArgError, "negative length");
+ }
+ if (UNLIKELY((unsigned long)len > SIZE_MAX / size || arg->readable >= LONG_MAX - len)) {
+ rb_raise(rb_eArgError, "marshaled data too big");
+ }
+ return len;
+}
+
+static VALUE
+r_bytes1(long len, struct load_arg *arg)
+{
+ VALUE str, n = LONG2NUM(len);
+
+ str = load_funcall(arg, arg->src, s_read, 1, &n);
+ if (NIL_P(str)) too_short();
+ StringValue(str);
+ if (RSTRING_LEN(str) != len) too_short();
+
+ return str;
+}
+
+static VALUE
+r_bytes1_buffered(long len, struct load_arg *arg)
+{
+ VALUE str;
+
+ if (len <= arg->buflen) {
+ str = rb_str_new(arg->buf+arg->offset, len);
+ arg->offset += len;
+ arg->buflen -= len;
+ }
+ else {
+ long buflen = arg->buflen;
+ long readable = arg->readable + 1;
+ long tmp_len, read_len, need_len = len - buflen;
+ VALUE tmp, n;
+
+ readable = readable < arg->bufsize ? readable : arg->bufsize;
+ read_len = need_len > readable ? need_len : readable;
+ n = LONG2NUM(read_len);
+ tmp = load_funcall(arg, arg->src, s_read, 1, &n);
+ if (NIL_P(tmp)) too_short();
+ StringValue(tmp);
+
+ tmp_len = RSTRING_LEN(tmp);
+
+ if (tmp_len < need_len) too_short();
+
+ str = rb_str_new(arg->buf+arg->offset, buflen);
+ rb_str_cat(str, RSTRING_PTR(tmp), need_len);
+
+ if (tmp_len > need_len) {
+ buflen = tmp_len - need_len;
+ memcpy(arg->buf, RSTRING_PTR(tmp)+need_len, buflen);
+ arg->buflen = buflen;
+ }
+ else {
+ arg->buflen = 0;
+ }
+ arg->offset = 0;
+ }
+
+ return str;
+}
+
#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
static VALUE
-r_bytes0(len, arg)
- long len;
- struct load_arg *arg;
+r_bytes0(long len, struct load_arg *arg)
{
VALUE str;
if (len == 0) return rb_str_new(0, 0);
- if (TYPE(arg->src) == T_STRING) {
- if (RSTRING(arg->src)->len > arg->offset) {
- str = rb_str_new(RSTRING(arg->src)->ptr+arg->offset, len);
- arg->offset += len;
- }
- else {
- too_short:
- rb_raise(rb_eArgError, "marshal data too short");
- }
+ if (RB_TYPE_P(arg->src, T_STRING)) {
+ if (RSTRING_LEN(arg->src) - arg->offset >= len) {
+ str = rb_str_new(RSTRING_PTR(arg->src)+arg->offset, len);
+ arg->offset += len;
+ }
+ else {
+ too_short();
+ }
}
else {
- VALUE src = arg->src;
- VALUE n = LONG2NUM(len);
- str = rb_funcall2(src, s_read, 1, &n);
- if (NIL_P(str)) goto too_short;
- StringValue(str);
- if (RSTRING(str)->len != len) goto too_short;
- if (OBJ_TAINTED(str)) arg->taint = Qtrue;
+ if (arg->readable > 0 || arg->buflen > 0) {
+ str = r_bytes1_buffered(len, arg);
+ }
+ else {
+ str = r_bytes1(len, arg);
+ }
}
return str;
}
-static ID
-r_symlink(arg)
- struct load_arg *arg;
+static inline int
+name_equal(const char *name, size_t nlen, const char *p, long l)
{
- ID id;
- long num = r_long(arg);
+ if ((size_t)l != nlen || *p != *name) return 0;
+ return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
+}
- if (st_lookup(arg->symbols, num, &id)) {
- return id;
+static int
+sym2encidx(VALUE sym, VALUE val)
+{
+ RBIMPL_ATTR_NONSTRING() static const char name_encoding[8] = "encoding";
+ const char *p;
+ long l;
+ if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
+ RSTRING_GETMEM(sym, p, l);
+ if (l <= 0) return -1;
+ if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
+ int idx = rb_enc_find_index(StringValueCStr(val));
+ return idx;
}
- rb_raise(rb_eArgError, "bad symbol");
+ if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
+ if (val == Qfalse) return rb_usascii_encindex();
+ else if (val == Qtrue) return rb_utf8_encindex();
+ /* bogus ignore */
+ }
+ return -1;
}
-static ID
-r_symreal(arg)
- struct load_arg *arg;
+static int
+symname_equal(VALUE sym, const char *name, size_t nlen)
{
- ID id;
+ const char *p;
+ long l;
+ if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return 0;
+ RSTRING_GETMEM(sym, p, l);
+ return name_equal(name, nlen, p, l);
+}
+
+#define BUILD_ASSERT_POSITIVE(n) \
+ /* make 0 negative to workaround the "zero size array" GCC extension, */ \
+ ((sizeof(char [2*(ssize_t)(n)-1])+1)/2) /* assuming no overflow */
+#define symname_equal_lit(sym, sym_name) \
+ symname_equal(sym, sym_name, BUILD_ASSERT_POSITIVE(rb_strlen_lit(sym_name)))
+
+static VALUE
+r_symlink(struct load_arg *arg)
+{
+ st_data_t sym;
+ long num = r_long(arg);
- id = rb_intern(RSTRING(r_bytes(arg))->ptr);
- st_insert(arg->symbols, arg->symbols->num_entries, id);
+ if (!st_lookup(arg->symbols, num, &sym)) {
+ rb_raise(rb_eArgError, "bad symbol");
+ }
+ return (VALUE)sym;
+}
+
+static VALUE
+r_symreal(struct load_arg *arg, int ivar)
+{
+ VALUE s = r_bytes(arg);
+ VALUE sym;
+ int idx = -1;
+ st_index_t n = arg->symbols->num_entries;
+
+ if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
+ st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
+ if (ivar) {
+ long num = r_long(arg);
+ while (num-- > 0) {
+ sym = r_symbol(arg);
+ idx = sym2encidx(sym, r_object(arg));
+ }
+ }
+ if (idx > 0) {
+ rb_enc_associate_index(s, idx);
+ if (is_broken_string(s)) {
+ rb_raise(rb_eArgError, "invalid byte sequence in %s: %+"PRIsVALUE,
+ rb_enc_name(rb_enc_from_index(idx)), s);
+ }
+ }
- return id;
+ return s;
}
-static ID
-r_symbol(arg)
- struct load_arg *arg;
+static VALUE
+r_symbol(struct load_arg *arg)
{
- if (r_byte(arg) == TYPE_SYMLINK) {
- return r_symlink(arg);
+ int type, ivar = 0;
+
+ again:
+ switch ((type = r_byte(arg))) {
+ default:
+ rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
+ case TYPE_IVAR:
+ ivar = 1;
+ goto again;
+ case TYPE_SYMBOL:
+ return r_symreal(arg, ivar);
+ case TYPE_SYMLINK:
+ if (ivar) {
+ rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
+ }
+ return r_symlink(arg);
}
- return r_symreal(arg);
}
-static char*
-r_unique(arg)
- struct load_arg *arg;
+static VALUE
+r_unique(struct load_arg *arg)
{
- return rb_id2name(r_symbol(arg));
+ return r_symbol(arg);
}
static VALUE
-r_string(arg)
- struct load_arg *arg;
+r_string(struct load_arg *arg)
{
return r_bytes(arg);
}
static VALUE
-r_entry(v, arg)
- VALUE v;
- struct load_arg *arg;
+r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
{
- rb_hash_aset(arg->data, INT2FIX(RHASH(arg->data)->tbl->num_entries), v);
- if (arg->taint) OBJ_TAINT(v);
+ st_data_t real_obj = (st_data_t)v;
+ if (arg->compat_tbl) {
+ /* real_obj is kept if not found */
+ st_lookup(arg->compat_tbl, v, &real_obj);
+ }
+ st_insert(arg->data, num, real_obj);
+ st_insert(arg->partial_objects, (st_data_t)real_obj, Qtrue);
+ return v;
+}
+
+static VALUE
+r_fixup_compat(VALUE v, struct load_arg *arg)
+{
+ st_data_t data;
+ st_data_t key = (st_data_t)v;
+ if (arg->compat_tbl && st_delete(arg->compat_tbl, &key, &data)) {
+ VALUE real_obj = (VALUE)data;
+ rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj));
+ if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
+ marshal_compat_t *compat = (marshal_compat_t*)data;
+ compat->loader(real_obj, v);
+ }
+ v = real_obj;
+ }
return v;
}
+static VALUE
+r_post_proc(VALUE v, struct load_arg *arg)
+{
+ if (arg->proc) {
+ v = load_funcall(arg, arg->proc, s_call, 1, &v);
+ }
+ return v;
+}
+
+static VALUE
+r_leave(VALUE v, struct load_arg *arg, bool partial)
+{
+ v = r_fixup_compat(v, arg);
+ if (!partial) {
+ st_data_t data;
+ st_data_t key = (st_data_t)v;
+ st_delete(arg->partial_objects, &key, &data);
+ if (arg->freeze) {
+ if (RB_TYPE_P(v, T_MODULE) || RB_TYPE_P(v, T_CLASS)) {
+ // noop
+ }
+ else if (RB_TYPE_P(v, T_STRING)) {
+ v = rb_str_to_interned_str(v);
+ }
+ else {
+ OBJ_FREEZE(v);
+ }
+ }
+ v = r_post_proc(v, arg);
+ }
+ return v;
+}
+
+static int
+copy_ivar_i(ID vid, VALUE value, st_data_t arg)
+{
+ VALUE obj = (VALUE)arg;
+
+ if (!rb_ivar_defined(obj, vid))
+ rb_ivar_set(obj, vid, value);
+ return ST_CONTINUE;
+}
+
+static VALUE
+r_copy_ivar(VALUE v, VALUE data)
+{
+ rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
+ return v;
+}
+
+#define override_ivar_error(type, str) \
+ rb_raise(rb_eTypeError, \
+ "can't override instance variable of "type" '%"PRIsVALUE"'", \
+ (str))
+
+static int
+r_ivar_encoding(VALUE obj, struct load_arg *arg, VALUE sym, VALUE val)
+{
+ int idx = sym2encidx(sym, val);
+ if (idx >= 0) {
+ if (rb_enc_capable(obj)) {
+ // Check if needed to avoid rb_check_frozen() check for Regexps
+ if (rb_enc_get_index(obj) != idx) {
+ rb_enc_associate_index(obj, idx);
+ }
+ }
+ else {
+ rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static long
+r_encname(VALUE obj, struct load_arg *arg)
+{
+ long len = r_long(arg);
+ if (len > 0) {
+ VALUE sym = r_symbol(arg);
+ VALUE val = r_object(arg);
+ len -= r_ivar_encoding(obj, arg, sym, val);
+ }
+ return len;
+}
+
static void
-r_ivar(obj, arg)
- VALUE obj;
- struct load_arg *arg;
+r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
{
long len;
len = r_long(arg);
if (len > 0) {
- while (len--) {
- ID id = r_symbol(arg);
- VALUE val = r_object(arg);
- rb_ivar_set(obj, id, val);
- }
+ if (RB_TYPE_P(obj, T_MODULE)) {
+ override_ivar_error("module", rb_mod_name(obj));
+ }
+ else if (RB_TYPE_P(obj, T_CLASS)) {
+ override_ivar_error("class", rb_class_name(obj));
+ }
+ do {
+ VALUE sym = r_symbol(arg);
+ VALUE val = r_object(arg);
+ if (r_ivar_encoding(obj, arg, sym, val)) {
+ if (has_encoding) *has_encoding = TRUE;
+ }
+ else if (symname_equal_lit(sym, name_s_ruby2_keywords_flag)) {
+ if (RB_TYPE_P(obj, T_HASH)) {
+ rb_hash_ruby2_keywords(obj);
+ }
+ else {
+ rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
+ }
+ }
+ else {
+ rb_ivar_set(obj, rb_intern_str(sym), val);
+ }
+ } while (--len > 0);
}
}
static VALUE
-path2class(path)
- char *path;
+path2class(VALUE path)
{
- VALUE v = rb_path2class(path);
+ VALUE v = rb_path_to_class(path);
- if (TYPE(v) != T_CLASS) {
- rb_raise(rb_eArgError, "%s does not refer class", path);
+ if (!RB_TYPE_P(v, T_CLASS)) {
+ rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
}
return v;
}
+#define path2module(path) must_be_module(rb_path_to_class(path), path)
+
static VALUE
-path2module(path)
- char *path;
+must_be_module(VALUE v, VALUE path)
{
- VALUE v = rb_path2class(path);
-
- if (TYPE(v) != T_MODULE) {
- rb_raise(rb_eArgError, "%s does not refer module", path);
+ if (!RB_TYPE_P(v, T_MODULE)) {
+ rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
}
return v;
}
static VALUE
-r_object0(arg, proc, ivp, extmod)
- struct load_arg *arg;
- VALUE proc;
- int *ivp;
- VALUE extmod;
+obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
+{
+ st_data_t data;
+ rb_alloc_func_t allocator;
+
+ allocator = rb_get_alloc_func(klass);
+ if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
+ marshal_compat_t *compat = (marshal_compat_t*)data;
+ VALUE real_obj = rb_obj_alloc(klass);
+ VALUE obj = rb_obj_alloc(compat->oldclass);
+ if (oldclass) *oldclass = compat->oldclass;
+
+ if (!arg->compat_tbl) {
+ arg->compat_tbl = rb_init_identtable();
+ }
+ st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
+ return obj;
+ }
+
+ return rb_obj_alloc(klass);
+}
+
+static VALUE
+obj_alloc_by_path(VALUE path, struct load_arg *arg)
+{
+ return obj_alloc_by_klass(path2class(path), arg, 0);
+}
+
+static VALUE
+append_extmod(VALUE obj, VALUE extmod)
+{
+ long i = RARRAY_LEN(extmod);
+ while (i > 0) {
+ VALUE m = RARRAY_AREF(extmod, --i);
+ rb_extend_object(obj, m);
+ }
+ return obj;
+}
+
+#define prohibit_ivar(type, str) do { \
+ if (!ivp || !*ivp) break; \
+ override_ivar_error(type, str); \
+ } while (0)
+
+static VALUE r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE klass, VALUE extmod, int type);
+
+static VALUE
+r_object0(struct load_arg *arg, bool partial, int *ivp, VALUE extmod)
{
- VALUE v = Qnil;
int type = r_byte(arg);
+ return r_object_for(arg, partial, ivp, 0, extmod, type);
+}
+
+static VALUE
+r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE klass, VALUE extmod, int type)
+{
+ VALUE (*hash_new_with_size)(st_index_t) = rb_hash_new_with_size;
+ VALUE v = Qnil;
long id;
+ st_data_t link;
switch (type) {
case TYPE_LINK:
- id = r_long(arg);
- v = rb_hash_aref(arg->data, LONG2FIX(id));
- if (NIL_P(v)) {
- rb_raise(rb_eArgError, "dump format error (unlinked)");
- }
- return v;
+ id = r_long(arg);
+ if (!st_lookup(arg->data, (st_data_t)id, &link)) {
+ rb_raise(rb_eArgError, "dump format error (unlinked)");
+ }
+ v = (VALUE)link;
+ if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
+ if (arg->freeze && RB_TYPE_P(v, T_STRING)) {
+ v = rb_str_to_interned_str(v);
+ }
+ v = r_post_proc(v, arg);
+ }
+ break;
case TYPE_IVAR:
{
- int ivar = Qtrue;
-
- v = r_object0(arg, 0, &ivar, extmod);
- if (ivar) r_ivar(v, arg);
- }
- break;
+ int ivar = TRUE;
+ v = r_object0(arg, true, &ivar, extmod);
+ if (ivar) r_ivar(v, NULL, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_EXTENDED:
- {
- VALUE m = path2module(r_unique(arg));
-
- if (NIL_P(extmod)) extmod = rb_ary_new2(0);
- rb_ary_push(extmod, m);
+ {
+ VALUE path = r_unique(arg);
+ VALUE m = rb_path_to_class(path);
+ if (NIL_P(extmod)) extmod = rb_ary_hidden_new(0);
+
+ if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
+ VALUE c;
+
+ v = r_object0(arg, true, 0, Qnil);
+ c = CLASS_OF(v);
+ if (c != m || FL_TEST(c, FL_SINGLETON)) {
+ rb_raise(rb_eArgError,
+ "prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
+ path, rb_class_name(c));
+ }
+ c = rb_singleton_class(v);
+ while (RARRAY_LEN(extmod) > 0) {
+ m = rb_ary_pop(extmod);
+ rb_prepend_module(c, m);
+ }
+ }
+ else {
+ must_be_module(m, path);
+ rb_ary_push(extmod, m);
- v = r_object0(arg, 0, 0, extmod);
- while (RARRAY(extmod)->len > 0) {
- m = rb_ary_pop(extmod);
- rb_extend_object(v, m);
+ v = r_object0(arg, true, 0, extmod);
+ while (RARRAY_LEN(extmod) > 0) {
+ m = rb_ary_pop(extmod);
+ rb_extend_object(v, m);
+ }
}
- }
- break;
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_UCLASS:
- {
- VALUE c = path2class(r_unique(arg));
-
- if (FL_TEST(c, FL_SINGLETON)) {
- rb_raise(rb_eTypeError, "singleton can't be loaded");
- }
- v = r_object0(arg, 0, 0, extmod);
- if (rb_special_const_p(v) || TYPE(v) == T_OBJECT || TYPE(v) == T_CLASS) {
- format_error:
- rb_raise(rb_eArgError, "dump format error (user class)");
- }
- if (TYPE(v) == T_MODULE || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
- VALUE tmp = rb_obj_alloc(c);
-
- if (TYPE(v) != TYPE(tmp)) goto format_error;
- }
- RBASIC(v)->klass = c;
- }
- break;
+ {
+ VALUE c = path2class(r_unique(arg));
+
+ if (FL_TEST(c, FL_SINGLETON)) {
+ rb_raise(rb_eTypeError, "singleton can't be loaded");
+ }
+ type = r_byte(arg);
+ if ((c == rb_cHash) &&
+ /* Hack for compare_by_identity */
+ (type == TYPE_HASH || type == TYPE_HASH_DEF)) {
+ hash_new_with_size = rb_ident_hash_new_with_size;
+ goto type_hash;
+ }
+ v = r_object_for(arg, partial, 0, c, extmod, type);
+ if (RB_SPECIAL_CONST_P(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
+ goto format_error;
+ }
+ if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
+ VALUE tmp = rb_obj_alloc(c);
+
+ if (TYPE(v) != TYPE(tmp)) goto format_error;
+ }
+ RBASIC_SET_CLASS(v, c);
+ }
+ break;
+
+ format_error:
+ rb_raise(rb_eArgError, "dump format error (user class)");
case TYPE_NIL:
- v = Qnil;
- break;
+ v = Qnil;
+ v = r_leave(v, arg, false);
+ break;
case TYPE_TRUE:
- v = Qtrue;
- break;
+ v = Qtrue;
+ v = r_leave(v, arg, false);
+ break;
case TYPE_FALSE:
- v = Qfalse;
- break;
+ v = Qfalse;
+ v = r_leave(v, arg, false);
+ break;
case TYPE_FIXNUM:
- {
- long i = r_long(arg);
- v = LONG2FIX(i);
- }
- break;
+ {
+ long i = r_long(arg);
+ v = LONG2FIX(i);
+ }
+ v = r_leave(v, arg, false);
+ break;
case TYPE_FLOAT:
- {
- double d, t = 0.0;
- VALUE str = r_bytes(arg);
- const char *ptr = RSTRING(str)->ptr;
-
- if (strcmp(ptr, "nan") == 0) {
- d = t / t;
- }
- else if (strcmp(ptr, "inf") == 0) {
- d = 1.0 / t;
- }
- else if (strcmp(ptr, "-inf") == 0) {
- d = -1.0 / t;
- }
- else {
- char *e;
- d = strtod(ptr, &e);
- d = load_mantissa(d, e, RSTRING(str)->len - (e - ptr));
- }
- v = rb_float_new(d);
- r_entry(v, arg);
- }
- break;
+ {
+ double d;
+ VALUE str = r_bytes(arg);
+ const char *ptr = RSTRING_PTR(str);
+
+ if (strcmp(ptr, "nan") == 0) {
+ d = nan("");
+ }
+ else if (strcmp(ptr, "inf") == 0) {
+ d = HUGE_VAL;
+ }
+ else if (strcmp(ptr, "-inf") == 0) {
+ d = -HUGE_VAL;
+ }
+ else {
+ char *e;
+ d = strtod(ptr, &e);
+ d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
+ }
+ v = DBL2NUM(d);
+ v = r_entry(v, arg);
+ v = r_leave(v, arg, false);
+ }
+ break;
case TYPE_BIGNUM:
- {
- long len;
- BDIGIT *digits;
- volatile VALUE data;
-
- NEWOBJ(big, struct RBignum);
- OBJSETUP(big, rb_cBignum, T_BIGNUM);
- big->sign = (r_byte(arg) == '+');
- len = r_long(arg);
- data = r_bytes0(len * 2, arg);
-#if SIZEOF_BDIGITS == SIZEOF_SHORT
- big->len = len;
-#else
- big->len = (len + 1) * 2 / sizeof(BDIGIT);
-#endif
- big->digits = digits = ALLOC_N(BDIGIT, big->len);
- MEMCPY(digits, RSTRING(data)->ptr, char, len * 2);
-#if SIZEOF_BDIGITS > SIZEOF_SHORT
- MEMZERO((char *)digits + len * 2, char,
- big->len * sizeof(BDIGIT) - len * 2);
-#endif
- len = big->len;
- while (len > 0) {
- unsigned char *p = (unsigned char *)digits;
- BDIGIT num = 0;
-#if SIZEOF_BDIGITS > SIZEOF_SHORT
- int shift = 0;
- int i;
-
- for (i=0; i<SIZEOF_BDIGITS; i++) {
- num |= (int)p[i] << shift;
- shift += 8;
- }
+ {
+ long len;
+ VALUE data;
+ int sign;
+
+ sign = r_byte(arg);
+ if (sign != '+' && sign != '-') {
+ rb_raise(rb_eArgError, "invalid Bignum sign");
+ }
+ len = r_keep_readable(arg, r_long(arg), 2);
+
+ if (SIZEOF_VALUE >= 8 && len <= 4) {
+ // Representable within uintptr, likely FIXNUM
+ VALUE num = 0;
+ for (int i = 0; i < len; i++) {
+ num |= (VALUE)r_byte(arg) << (i * 16);
+ num |= (VALUE)r_byte(arg) << (i * 16 + 8);
+ }
+#if SIZEOF_VALUE == SIZEOF_LONG
+ v = ULONG2NUM(num);
#else
- num = p[0] | (p[1] << 8);
+ v = ULL2NUM(num);
#endif
- *digits++ = num;
- len--;
- }
- v = rb_big_norm((VALUE)big);
- r_entry(v, arg);
- }
- break;
+ if (sign == '-') {
+ v = rb_int_uminus(v);
+ }
+ }
+ else {
+ data = r_bytes0(len * 2, arg);
+ v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
+ INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
+ rb_str_resize(data, 0L);
+ }
+ v = r_entry(v, arg);
+ v = r_leave(v, arg, false);
+ }
+ break;
case TYPE_STRING:
- v = r_entry(r_string(arg), arg);
- break;
+ v = r_entry(r_string(arg), arg);
+ v = r_leave(v, arg, partial);
+ break;
case TYPE_REGEXP:
- {
- volatile VALUE str = r_bytes(arg);
- int options = r_byte(arg);
- v = r_entry(rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len, options), arg);
- }
- break;
+ {
+ VALUE str = r_bytes(arg);
+ int options = r_byte(arg);
+ int has_encoding = FALSE;
+ st_index_t idx = r_prepare(arg);
+
+ if (ivp) {
+ r_ivar(str, &has_encoding, arg);
+ *ivp = FALSE;
+ }
+ if (!has_encoding) {
+ /* 1.8 compatibility; remove escapes undefined in 1.8 */
+ char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
+ long len = RSTRING_LEN(str);
+ long bs = 0;
+ for (; len-- > 0; *dst++ = *src++) {
+ switch (*src) {
+ case '\\': bs++; break;
+ case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
+ case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
+ case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
+ case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
+ case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
+ if (bs & 1) --dst;
+ /* fall through */
+ default: bs = 0; break;
+ }
+ }
+ rb_str_set_len(str, dst - ptr);
+ }
+ if (!klass) {
+ klass = rb_cRegexp;
+ }
+ VALUE regexp = rb_reg_init_str(rb_reg_s_alloc(klass), str, options);
+ r_copy_ivar(regexp, str);
- case TYPE_ARRAY:
- {
- volatile long len = r_long(arg); /* gcc 2.7.2.3 -O2 bug?? */
+ v = r_entry0(regexp, idx, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
- v = rb_ary_new2(len);
- r_entry(v, arg);
- while (len--) {
- rb_ary_push(v, r_object(arg));
- }
- }
- break;
+ case TYPE_ARRAY:
+ {
+ long len = r_keep_readable(arg, r_long(arg), 1);
+
+ v = rb_ary_new2(len);
+ v = r_entry(v, arg);
+ arg->readable += len - 1;
+ while (len--) {
+ rb_ary_push(v, r_object(arg));
+ arg->readable--;
+ }
+ v = r_leave(v, arg, partial);
+ arg->readable++;
+ }
+ break;
case TYPE_HASH:
case TYPE_HASH_DEF:
- {
- long len = r_long(arg);
-
- v = rb_hash_new();
- r_entry(v, arg);
- while (len--) {
- VALUE key = r_object(arg);
- VALUE value = r_object(arg);
- rb_hash_aset(v, key, value);
- }
- if (type == TYPE_HASH_DEF) {
- RHASH(v)->ifnone = r_object(arg);
- }
- }
- break;
+ type_hash:
+ {
+ long len = r_keep_readable(arg, r_long(arg), 2);
+
+ v = hash_new_with_size(len);
+ v = r_entry(v, arg);
+ arg->readable += (len - 1) * 2;
+ while (len--) {
+ VALUE key = r_object(arg);
+ VALUE value = r_object(arg);
+ rb_hash_aset(v, key, value);
+ arg->readable -= 2;
+ }
+ arg->readable += 2;
+ if (type == TYPE_HASH_DEF) {
+ RHASH_SET_IFNONE(v, r_object(arg));
+ }
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_STRUCT:
- {
- VALUE klass, mem, values;
- volatile long i; /* gcc 2.7.2.3 -O2 bug?? */
- long len;
- ID slot;
-
- klass = path2class(r_unique(arg));
- mem = rb_struct_s_members(klass);
- if (mem == Qnil) {
- rb_raise(rb_eTypeError, "uninitialized struct");
- }
- len = r_long(arg);
-
- values = rb_ary_new2(len);
- for (i=0; i<len; i++) {
- rb_ary_push(values, Qnil);
- }
- v = rb_struct_alloc(klass, values);
- r_entry(v, arg);
- for (i=0; i<len; i++) {
- slot = r_symbol(arg);
-
- if (RARRAY(mem)->ptr[i] != ID2SYM(slot)) {
- rb_raise(rb_eTypeError, "struct %s not compatible (:%s for :%s)",
- rb_class2name(klass),
- rb_id2name(slot),
- rb_id2name(SYM2ID(RARRAY(mem)->ptr[i])));
- }
- rb_struct_aset(v, LONG2FIX(i), r_object(arg));
- }
- }
- break;
+ {
+ VALUE mem, values;
+ long i;
+ VALUE slot;
+ st_index_t idx = r_prepare(arg);
+ VALUE klass = path2class(r_unique(arg));
+ long len = r_keep_readable(arg, r_long(arg), 2);
+
+ v = rb_obj_alloc(klass);
+ if (!RB_TYPE_P(v, T_STRUCT)) {
+ rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
+ }
+ mem = rb_struct_s_members(klass);
+ if (RARRAY_LEN(mem) != len) {
+ rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
+ rb_class_name(klass));
+ }
+
+ arg->readable += (len - 1) * 2;
+ v = r_entry0(v, idx, arg);
+ values = rb_ary_new2(len);
+ {
+ VALUE keywords = Qfalse;
+ if (RTEST(rb_struct_s_keyword_init(klass))) {
+ keywords = rb_hash_new();
+ rb_ary_push(values, keywords);
+ }
+
+ for (i=0; i<len; i++) {
+ VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
+ slot = r_symbol(arg);
+
+ if (!rb_str_equal(n, slot)) {
+ rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
+ rb_class_name(klass),
+ slot, n);
+ }
+ if (keywords) {
+ rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
+ }
+ else {
+ rb_ary_push(values, r_object(arg));
+ }
+ arg->readable -= 2;
+ }
+ }
+ rb_struct_initialize(v, values);
+ v = r_leave(v, arg, partial);
+ arg->readable += 2;
+ }
+ break;
case TYPE_USERDEF:
{
- VALUE klass = path2class(r_unique(arg));
- VALUE data;
-
- if (!rb_respond_to(klass, s_load)) {
- rb_raise(rb_eTypeError, "class %s needs to have method `_load'",
- rb_class2name(klass));
- }
- data = r_string(arg);
- if (ivp) {
- r_ivar(data, arg);
- *ivp = Qfalse;
- }
- v = rb_funcall(klass, s_load, 1, data);
- r_entry(v, arg);
- }
+ VALUE name = r_unique(arg);
+ VALUE klass = path2class(name);
+ VALUE data;
+ st_data_t d;
+
+ if (!rb_obj_respond_to(klass, s_load, TRUE)) {
+ rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method '_load'",
+ name);
+ }
+ data = r_string(arg);
+ if (ivp) {
+ r_ivar(data, NULL, arg);
+ *ivp = FALSE;
+ }
+ v = load_funcall(arg, klass, s_load, 1, &data);
+ v = r_entry(v, arg);
+ if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
+ marshal_compat_t *compat = (marshal_compat_t*)d;
+ v = compat->loader(klass, v);
+ }
+ if (!partial) {
+ if (arg->freeze) {
+ OBJ_FREEZE(v);
+ }
+ v = r_post_proc(v, arg);
+ }
+ }
break;
case TYPE_USRMARSHAL:
{
- VALUE klass = path2class(r_unique(arg));
- VALUE data;
-
- v = rb_obj_alloc(klass);
- if (! NIL_P(extmod)) {
- while (RARRAY(extmod)->len > 0) {
- VALUE m = rb_ary_pop(extmod);
- rb_extend_object(v, m);
- }
+ VALUE name = r_unique(arg);
+ VALUE klass = path2class(name);
+ VALUE oldclass = 0;
+ VALUE data;
+
+ v = obj_alloc_by_klass(klass, arg, &oldclass);
+ if (!NIL_P(extmod)) {
+ /* for the case marshal_load is overridden */
+ append_extmod(v, extmod);
+ }
+ if (!rb_obj_respond_to(v, s_mload, TRUE)) {
+ rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method 'marshal_load'",
+ name);
+ }
+ v = r_entry(v, arg);
+ data = r_object(arg);
+ load_funcall(arg, v, s_mload, 1, &data);
+ v = r_fixup_compat(v, arg);
+ v = r_copy_ivar(v, data);
+ if (arg->freeze) {
+ OBJ_FREEZE(v);
}
- if (!rb_respond_to(v, s_mload)) {
- rb_raise(rb_eTypeError, "instance of %s needs to have method `marshal_load'",
- rb_class2name(klass));
- }
- r_entry(v, arg);
- data = r_object(arg);
- rb_funcall(v, s_mload, 1, data);
- }
+ v = r_post_proc(v, arg);
+ if (!NIL_P(extmod)) {
+ if (oldclass) append_extmod(v, extmod);
+ rb_ary_clear(extmod);
+ }
+ }
break;
case TYPE_OBJECT:
- {
- VALUE klass = path2class(r_unique(arg));
-
- v = rb_obj_alloc(klass);
- if (TYPE(v) != T_OBJECT) {
- rb_raise(rb_eArgError, "dump format error");
- }
- r_entry(v, arg);
- r_ivar(v, arg);
- }
- break;
+ {
+ st_index_t idx = r_prepare(arg);
+ v = obj_alloc_by_path(r_unique(arg), arg);
+ if (!RB_TYPE_P(v, T_OBJECT)) {
+ rb_raise(rb_eArgError, "dump format error");
+ }
+ v = r_entry0(v, idx, arg);
+ r_ivar(v, NULL, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_DATA:
- {
- VALUE klass = path2class(r_unique(arg));
- if (rb_respond_to(klass, s_alloc)) {
- static int warn = Qtrue;
- if (warn) {
- rb_warn("define `allocate' instead of `_alloc'");
- warn = Qfalse;
- }
- v = rb_funcall(klass, s_alloc, 0);
- }
- else {
- v = rb_obj_alloc(klass);
- }
- if (TYPE(v) != T_DATA) {
- rb_raise(rb_eArgError, "dump format error");
- }
- r_entry(v, arg);
- if (!rb_respond_to(v, s_load_data)) {
- rb_raise(rb_eTypeError,
- "class %s needs to have instance method `_load_data'",
- rb_class2name(klass));
- }
- rb_funcall(v, s_load_data, 1, r_object0(arg, 0, 0, extmod));
- }
- break;
+ {
+ VALUE name = r_unique(arg);
+ VALUE klass = path2class(name);
+ VALUE oldclass = 0;
+ VALUE r;
+
+ v = obj_alloc_by_klass(klass, arg, &oldclass);
+ if (!RB_TYPE_P(v, T_DATA)) {
+ rb_raise(rb_eArgError, "dump format error");
+ }
+ v = r_entry(v, arg);
+ if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
+ rb_raise(rb_eTypeError,
+ "class %"PRIsVALUE" needs to have instance method '_load_data'",
+ name);
+ }
+ r = r_object0(arg, partial, 0, extmod);
+ load_funcall(arg, v, s_load_data, 1, &r);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_MODULE_OLD:
{
- volatile VALUE str = r_bytes(arg);
+ VALUE str = r_bytes(arg);
- v = rb_path2class(RSTRING(str)->ptr);
- r_entry(v, arg);
- }
- break;
+ v = rb_path_to_class(str);
+ prohibit_ivar("class/module", str);
+ v = r_entry(v, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_CLASS:
{
- volatile VALUE str = r_bytes(arg);
-
- v = path2class(RSTRING(str)->ptr);
- r_entry(v, arg);
- }
- break;
+ VALUE str = r_bytes(arg);
+
+ if (ivp && *ivp > 0) *ivp = r_encname(str, arg) > 0;
+ v = path2class(str);
+ prohibit_ivar("class", str);
+ v = r_entry(v, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_MODULE:
{
- volatile VALUE str = r_bytes(arg);
-
- v = path2module(RSTRING(str)->ptr);
- r_entry(v, arg);
- }
- break;
+ VALUE str = r_bytes(arg);
+
+ if (ivp && *ivp > 0) *ivp = r_encname(str, arg) > 0;
+ v = path2module(str);
+ prohibit_ivar("module", str);
+ v = r_entry(v, arg);
+ v = r_leave(v, arg, partial);
+ }
+ break;
case TYPE_SYMBOL:
- v = ID2SYM(r_symreal(arg));
- break;
+ if (ivp) {
+ v = r_symreal(arg, *ivp);
+ *ivp = FALSE;
+ }
+ else {
+ v = r_symreal(arg, 0);
+ }
+ v = rb_str_intern(v);
+ v = r_leave(v, arg, partial);
+ break;
case TYPE_SYMLINK:
- return ID2SYM(r_symlink(arg));
+ v = rb_str_intern(r_symlink(arg));
+ break;
default:
- rb_raise(rb_eArgError, "dump format error(0x%x)", type);
- break;
+ rb_raise(rb_eArgError, "dump format error(0x%x)", type);
+ break;
}
- if (proc) {
- rb_funcall(proc, rb_intern("call"), 1, v);
+
+ if (UNDEF_P(v)) {
+ rb_raise(rb_eArgError, "dump format error (bad link)");
}
- return v;
-}
-static VALUE
-r_object(arg)
- struct load_arg *arg;
-{
- return r_object0(arg, arg->proc, 0, Qnil);
+ return v;
}
static VALUE
-load(arg)
- struct load_arg *arg;
+r_object(struct load_arg *arg)
{
- return r_object(arg);
+ return r_object0(arg, false, 0, Qnil);
}
-static VALUE
-load_ensure(arg)
- struct load_arg *arg;
+static void
+clear_load_arg(struct load_arg *arg)
{
+ ruby_xfree_sized(arg->buf, arg->bufsize);
+ arg->buf = NULL;
+ arg->bufsize = 0;
+ arg->buflen = 0;
+ arg->offset = 0;
+ arg->readable = 0;
+ if (!arg->symbols) return;
st_free_table(arg->symbols);
- return 0;
+ arg->symbols = 0;
+ st_free_table(arg->data);
+ arg->data = 0;
+ st_free_table(arg->partial_objects);
+ arg->partial_objects = 0;
+ if (arg->compat_tbl) {
+ st_free_table(arg->compat_tbl);
+ arg->compat_tbl = 0;
+ }
}
-/*
- * call-seq:
- * load( source [, proc] ) => obj
- * restore( source [, proc] ) => obj
- *
- * Returns the result of converting the serialized data in source into a
- * Ruby object (possibly with associated subordinate objects). source
- * may be either an instance of IO or an object that responds to
- * to_str. If proc is specified, it will be passed each object as it
- * is deserialized.
- */
-static VALUE
-marshal_load(argc, argv)
- int argc;
- VALUE *argv;
+VALUE
+rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze)
{
- VALUE port, proc;
int major, minor;
VALUE v;
- struct load_arg arg;
+ VALUE wrapper; /* used to avoid memory leak in case of exception */
+ struct load_arg *arg;
- rb_scan_args(argc, argv, "11", &port, &proc);
- if (rb_respond_to(port, rb_intern("to_str"))) {
- arg.taint = OBJ_TAINTED(port); /* original taintedness */
- StringValue(port); /* possible conversion */
+ v = rb_check_string_type(port);
+ if (!NIL_P(v)) {
+ port = v;
}
- else if (rb_respond_to(port, s_getc) && rb_respond_to(port, s_read)) {
- if (rb_respond_to(port, s_binmode)) {
- rb_funcall2(port, s_binmode, 0, 0);
- }
- arg.taint = Qtrue;
+ else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
+ rb_check_funcall(port, s_binmode, 0, 0);
}
else {
- rb_raise(rb_eTypeError, "instance of IO needed");
+ io_needed();
+ }
+ wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
+ arg->src = port;
+ arg->offset = 0;
+ arg->symbols = st_init_numtable();
+ arg->data = rb_init_identtable();
+ arg->partial_objects = rb_init_identtable();
+ arg->compat_tbl = 0;
+ arg->proc = 0;
+ arg->readable = 0;
+ arg->freeze = freeze;
+
+ if (NIL_P(v)) {
+ arg->bufsize = BUFSIZ;
+ arg->buf = xmalloc(BUFSIZ);
+ }
+ else {
+ arg->bufsize = 0;
+ arg->buf = 0;
}
- arg.src = port;
- arg.offset = 0;
- major = r_byte(&arg);
- minor = r_byte(&arg);
+ major = r_byte(arg);
+ minor = r_byte(arg);
if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
- rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
+ clear_load_arg(arg);
+ rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
\tformat version %d.%d required; %d.%d given",
- MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
+ MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
}
if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
- rb_warn("incompatible marshal file format (can be read)\n\
+ rb_warn("incompatible marshal file format (can be read)\n\
\tformat version %d.%d required; %d.%d given",
- MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
+ MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
}
- arg.symbols = st_init_numtable();
- arg.data = rb_hash_new();
- if (NIL_P(proc)) arg.proc = 0;
- else arg.proc = proc;
- v = rb_ensure(load, (VALUE)&arg, load_ensure, (VALUE)&arg);
+ if (!NIL_P(proc)) arg->proc = proc;
+ v = r_object(arg);
+ clear_load_arg(arg);
+ RB_GC_GUARD(wrapper);
return v;
}
+static VALUE
+marshal_load(rb_execution_context_t *ec, VALUE mod, VALUE source, VALUE proc, VALUE freeze)
+{
+ return rb_marshal_load_with_proc(source, proc, RTEST(freeze));
+}
+
+#include "marshal.rbinc"
+
/*
* The marshaling library converts collections of Ruby objects into a
* byte stream, allowing them to be stored outside the currently
* active script. This data may subsequently be read and the original
* objects reconstituted.
+ *
* Marshaled data has major and minor version numbers stored along
* with the object information. In normal use, marshaling can only
* load data written with the same major version number and an equal
@@ -1446,65 +2471,195 @@ marshal_load(argc, argv)
* first two bytes of marshaled data.
*
* str = Marshal.dump("thing")
- * RUBY_VERSION #=> "1.8.0"
- * str[0] #=> 4
- * str[1] #=> 8
+ * RUBY_VERSION #=> "1.9.0"
+ * str[0].ord #=> 4
+ * str[1].ord #=> 8
*
* Some objects cannot be dumped: if the objects to be dumped include
* bindings, procedure or method objects, instances of class IO, or
* singleton objects, a TypeError will be raised.
+ *
* If your class has special serialization needs (for example, if you
* want to serialize in some specific format), or if it contains
* objects that would otherwise not be serializable, you can implement
- * your own serialization strategy by defining two methods, _dump and
- * _load:
- * The instance method _dump should return a String object containing
- * all the information necessary to reconstitute objects of this class
- * and all referenced objects up to a maximum depth given as an integer
- * parameter (a value of -1 implies that you should disable depth checking).
- * The class method _load should take a String and return an object of this class.
+ * your own serialization strategy.
+ *
+ * There are two methods of doing this, your object can define either
+ * marshal_dump and marshal_load or _dump and _load. marshal_dump will take
+ * precedence over _dump if both are defined. marshal_dump may result in
+ * smaller Marshal strings.
+ *
+ * == Security considerations
+ *
+ * By design, Marshal.load can deserialize almost any class loaded into the
+ * Ruby process. In many cases this can lead to remote code execution if the
+ * Marshal data is loaded from an untrusted source.
+ *
+ * As a result, Marshal.load is not suitable as a general purpose serialization
+ * format and you should never unmarshal user supplied input or other untrusted
+ * data.
+ *
+ * If you need to deserialize untrusted data, use JSON or another serialization
+ * format that is only able to load simple, 'primitive' types such as String,
+ * Array, Hash, etc. Never allow user input to specify arbitrary types to
+ * deserialize into.
+ *
+ * == marshal_dump and marshal_load
+ *
+ * When dumping an object the method marshal_dump will be called.
+ * marshal_dump must return a result containing the information necessary for
+ * marshal_load to reconstitute the object. The result can be any object.
+ *
+ * When loading an object dumped using marshal_dump the object is first
+ * allocated then marshal_load is called with the result from marshal_dump.
+ * marshal_load must recreate the object from the information in the result.
+ *
+ * Example:
+ *
+ * class MyObj
+ * def initialize name, version, data
+ * @name = name
+ * @version = version
+ * @data = data
+ * end
+ *
+ * def marshal_dump
+ * [@name, @version]
+ * end
+ *
+ * def marshal_load array
+ * @name, @version = array
+ * end
+ * end
+ *
+ * == _dump and _load
+ *
+ * Use _dump and _load when you need to allocate the object you're restoring
+ * yourself.
+ *
+ * When dumping an object the instance method _dump is called with an Integer
+ * which indicates the maximum depth of objects to dump (a value of -1 implies
+ * that you should disable depth checking). _dump must return a String
+ * containing the information necessary to reconstitute the object.
+ *
+ * The class method _load should take a String and use it to return an object
+ * of the same class.
+ *
+ * Example:
+ *
+ * class MyObj
+ * def initialize name, version, data
+ * @name = name
+ * @version = version
+ * @data = data
+ * end
+ *
+ * def _dump level
+ * [@name, @version].join ':'
+ * end
+ *
+ * def self._load args
+ * new(*args.split(':'))
+ * end
+ * end
+ *
+ * Since Marshal.dump outputs a string you can have _dump return a Marshal
+ * string which is Marshal.loaded in _load for complex objects.
*/
void
-Init_marshal()
+Init_marshal(void)
{
VALUE rb_mMarshal = rb_define_module("Marshal");
-
- s_dump = rb_intern("_dump");
- s_load = rb_intern("_load");
- s_mdump = rb_intern("marshal_dump");
- s_mload = rb_intern("marshal_load");
- s_dump_data = rb_intern("_dump_data");
- s_load_data = rb_intern("_load_data");
- s_alloc = rb_intern("_alloc");
- s_getc = rb_intern("getc");
- s_read = rb_intern("read");
- s_write = rb_intern("write");
- s_binmode = rb_intern("binmode");
+#define set_id(sym) sym = rb_intern_const(name_##sym)
+ set_id(s_dump);
+ set_id(s_load);
+ set_id(s_mdump);
+ set_id(s_mload);
+ set_id(s_dump_data);
+ set_id(s_load_data);
+ set_id(s_alloc);
+ set_id(s_call);
+ set_id(s_getbyte);
+ set_id(s_read);
+ set_id(s_write);
+ set_id(s_binmode);
+ set_id(s_encoding_short);
+ set_id(s_ruby2_keywords_flag);
rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
- rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);
- rb_define_module_function(rb_mMarshal, "restore", marshal_load, -1);
+ /* major version */
rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
+ /* minor version */
rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
}
-VALUE
-rb_marshal_dump(obj, port)
- VALUE obj, port;
+static int
+marshal_compat_table_mark_and_move_i(st_data_t key, st_data_t value, st_data_t _)
{
- int argc = 1;
- VALUE argv[2];
+ marshal_compat_t *p = (marshal_compat_t *)value;
+ rb_gc_mark_and_move(&p->newclass);
+ rb_gc_mark_and_move(&p->oldclass);
+ return ST_CONTINUE;
+}
- argv[0] = obj;
- argv[1] = port;
- if (!NIL_P(port)) argc = 2;
- return marshal_dump(argc, argv);
+static void
+marshal_compat_table_mark_and_move(void *tbl)
+{
+ if (!tbl) return;
+ st_foreach(tbl, marshal_compat_table_mark_and_move_i, 0);
+}
+
+static int
+marshal_compat_table_free_i(st_data_t key, st_data_t value, st_data_t _)
+{
+ SIZED_FREE((marshal_compat_t *)value);
+ return ST_CONTINUE;
+}
+
+static void
+marshal_compat_table_free(void *data)
+{
+ st_foreach(data, marshal_compat_table_free_i, 0);
+ st_free_table(data);
+}
+
+static size_t
+marshal_compat_table_memsize(const void *data)
+{
+ return st_memsize(data) + sizeof(marshal_compat_t) * st_table_size(data);
+}
+
+static const rb_data_type_t marshal_compat_type = {
+ .wrap_struct_name = "marshal_compat_table",
+ .function = {
+ .dmark = marshal_compat_table_mark_and_move,
+ .dfree = marshal_compat_table_free,
+ .dsize = marshal_compat_table_memsize,
+ .dcompact = marshal_compat_table_mark_and_move,
+ },
+ .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static st_table *
+compat_allocator_table(void)
+{
+ if (compat_allocator_tbl) return compat_allocator_tbl;
+ compat_allocator_tbl = st_init_numtable();
+ compat_allocator_tbl_wrapper =
+ TypedData_Wrap_Struct(0, &marshal_compat_type, compat_allocator_tbl);
+ rb_vm_register_global_object(compat_allocator_tbl_wrapper);
+ return compat_allocator_tbl;
+}
+
+VALUE
+rb_marshal_dump(VALUE obj, VALUE port)
+{
+ return rb_marshal_dump_limited(obj, port, -1);
}
VALUE
-rb_marshal_load(port)
- VALUE port;
+rb_marshal_load(VALUE port)
{
- return marshal_load(1, &port);
+ return rb_marshal_load_with_proc(port, Qnil, false);
}