12#include "ruby/internal/config.h"
25#include "internal/array.h"
26#include "internal/bignum.h"
27#include "internal/class.h"
28#include "internal/encoding.h"
29#include "internal/error.h"
30#include "internal/hash.h"
31#include "internal/numeric.h"
32#include "internal/object.h"
33#include "internal/re.h"
34#include "internal/struct.h"
35#include "internal/symbol.h"
36#include "internal/util.h"
37#include "internal/vm.h"
46#define BITSPERSHORT (2*CHAR_BIT)
47#define SHORTMASK ((1<<BITSPERSHORT)-1)
48#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
50#if SIZEOF_SHORT == SIZEOF_BDIGIT
51#define SHORTLEN(x) (x)
54shortlen(
size_t len, BDIGIT *ds)
64 return (
len - 1)*SIZEOF_BDIGIT/2 + offset;
66#define SHORTLEN(x) shortlen((x),d)
69#define MARSHAL_MAJOR 4
70#define MARSHAL_MINOR 8
75#define TYPE_FIXNUM 'i'
77#define TYPE_EXTENDED 'e'
78#define TYPE_UCLASS 'C'
79#define TYPE_OBJECT 'o'
81#define TYPE_USERDEF 'u'
82#define TYPE_USRMARSHAL 'U'
84#define TYPE_BIGNUM 'l'
85#define TYPE_STRING '"'
86#define TYPE_REGEXP '/'
89#define TYPE_HASH_DEF '}'
90#define TYPE_STRUCT 'S'
91#define TYPE_MODULE_OLD 'M'
93#define TYPE_MODULE 'm'
95#define TYPE_SYMBOL ':'
96#define TYPE_SYMLINK ';'
101static ID s_dump, s_load, s_mdump, s_mload;
102static ID s_dump_data, s_load_data, s_alloc, s_call;
103static ID s_getbyte, s_read, s_write, s_binmode;
104static ID s_encoding_short, s_ruby2_keywords_flag;
105#define s_encoding_long rb_id_encoding()
107#define name_s_dump "_dump"
108#define name_s_load "_load"
109#define name_s_mdump "marshal_dump"
110#define name_s_mload "marshal_load"
111#define name_s_dump_data "_dump_data"
112#define name_s_load_data "_load_data"
113#define name_s_alloc "_alloc"
114#define name_s_call "call"
115#define name_s_getbyte "getbyte"
116#define name_s_read "read"
117#define name_s_write "write"
118#define name_s_binmode "binmode"
119#define name_s_encoding_short "E"
120#define name_s_encoding_long "encoding"
121#define name_s_ruby2_keywords_flag "K"
126 VALUE (*dumper)(VALUE);
127 VALUE (*loader)(VALUE, VALUE);
130static st_table *compat_allocator_tbl;
131static VALUE compat_allocator_tbl_wrapper;
132static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit);
133static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze);
135static st_table *compat_allocator_table(void);
138rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
140 marshal_compat_t *compat;
141 rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
144 rb_raise(rb_eTypeError, "no allocator");
147 compat_allocator_table();
148 compat = ALLOC(marshal_compat_t);
149 compat->newclass = newclass;
150 compat->oldclass = oldclass;
151 compat->dumper = dumper;
152 compat->loader = loader;
154 st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
155 RB_OBJ_WRITTEN(compat_allocator_tbl_wrapper, Qundef, newclass);
156 RB_OBJ_WRITTEN(compat_allocator_tbl_wrapper, Qundef, oldclass);
163 st_table *compat_tbl;
166 st_index_t num_entries;
169struct dump_call_arg {
171 struct dump_arg *arg;
176check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
179 rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
186check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
187 struct dump_arg *arg, const char *name)
189 VALUE ret = rb_funcallv(obj, sym, argc, argv);
190 VALUE klass = CLASS_OF(obj);
191 if (CLASS_OF(ret) == klass) {
192 rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
195 return check_dump_arg(ret, arg, name);
198#define dump_funcall(arg, obj, sym, argc, argv) \
199 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
200#define dump_check_funcall(arg, obj, sym, argc, argv) \
201 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
203static void clear_dump_arg(struct dump_arg *arg);
206mark_dump_arg(void *ptr)
208 struct dump_arg *p = ptr;
211 rb_mark_set(p->symbols);
212 rb_mark_set(p->data);
213 rb_mark_hash(p->compat_tbl);
214 rb_mark_set(p->userdefs);
219free_dump_arg(void *ptr)
225memsize_dump_arg(const void *ptr)
227 const struct dump_arg *p = (struct dump_arg *)ptr;
229 if (p->symbols) memsize += rb_st_memsize(p->symbols);
230 if (p->data) memsize += rb_st_memsize(p->data);
231 if (p->compat_tbl) memsize += rb_st_memsize(p->compat_tbl);
232 if (p->userdefs) memsize += rb_st_memsize(p->userdefs);
233 if (p->encodings) memsize += rb_st_memsize(p->encodings);
237static const rb_data_type_t dump_arg_data = {
239 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
240 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
244must_not_be_anonymous(const char *type, VALUE path)
246 char *n = RSTRING_PTR(path);
248 if (!rb_enc_asciicompat(rb_enc_get(path))) {
250 rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
254 rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
261class2path(VALUE klass)
263 VALUE path = rb_class_path(klass);
265 must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path);
266 if (rb_path_to_class(path) != rb_class_real(klass)) {
267 rb_raise(rb_eTypeError, "% "PRIsVALUE" can't be referred to", path);
272int ruby_marshal_write_long(long x, char *buf);
273static void w_long(long, struct dump_arg*);
274static int w_encoding(VALUE encname, struct dump_call_arg *arg);
275static VALUE encoding_name(VALUE obj, struct dump_arg *arg);
278w_nbyte(const char *s, long n, struct dump_arg *arg)
280 VALUE buf = arg->str;
281 rb_str_buf_cat(buf, s, n);
282 if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
283 rb_io_write(arg->dest, buf);
284 rb_str_resize(buf, 0);
289w_byte(char c, struct dump_arg *arg)
295w_bytes(const char *s, long n, struct dump_arg *arg)
301#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
304w_short(int x, struct dump_arg *arg)
306 w_byte((char)((x >> 0) & 0xff), arg);
307 w_byte((char)((x >> 8) & 0xff), arg);
311w_long(long x, struct dump_arg *arg)
313 char buf[sizeof(long)+1];
314 int i = ruby_marshal_write_long(x, buf);
316 rb_raise(rb_eTypeError, "long too big to dump");
318 w_nbyte(buf, i, arg);
322ruby_marshal_write_long(long x, char *buf)
327 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
328 /* big long does not fit in 4 bytes */
337 if (0 < x && x < 123) {
338 buf[0] = (char)(x + 5);
341 if (-124 < x && x < 0) {
342 buf[0] = (char)((x - 5)&0xff);
345 for (i=1;i<(int)sizeof(long)+1;i++) {
346 buf[i] = (char)(x & 0xff);
361#define DECIMAL_MANT (53-16) /* from IEEE754 double precision */
365#elif DBL_MANT_DIG > 24
367#elif DBL_MANT_DIG > 16
374load_mantissa(double d, const char *buf, long len)
377 if (--len > 0 && !*buf++) { /* binary mantissa mark */
378 int e, s = d < 0, dig = 0;
381 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
385 default: m = *buf++ & 0xff; /* fall through */
387 case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
390 case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
393 case 1: m = (m << 8) | (*buf++ & 0xff);
396 dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
397 d += ldexp((double)m, dig);
398 } while ((len -= MANT_BITS / 8) > 0);
399 d = ldexp(d, e - DECIMAL_MANT);
405#define load_mantissa(d, buf, len) (d)
409#define FLOAT_DIG (DBL_DIG+2)
415w_float(double d, struct dump_arg *arg)
417 char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
420 if (d < 0) w_cstr("-inf", arg);
421 else w_cstr("inf", arg);
427 if (signbit(d)) w_cstr("-0", arg);
428 else w_cstr("0", arg);
431 int decpt, sign, digs, len = 0;
432 char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
433 if (sign) buf[len++] = '-';
435 if (decpt < -3 || decpt > digs) {
437 if (--digs > 0) buf[len++] = '.';
438 memcpy(buf + len, p + 1, digs);
440 len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
442 else if (decpt > 0) {
443 memcpy(buf + len, p, decpt);
445 if ((digs -= decpt) > 0) {
447 memcpy(buf + len, p + decpt, digs);
455 memset(buf + len, '0', -decpt);
458 memcpy(buf + len, p, digs);
462 w_bytes(buf, len, arg);
468w_encivar(VALUE str, struct dump_arg *arg)
470 VALUE encname = encoding_name(str, arg);
471 if (NIL_P(encname) ||
472 is_ascii_string(str)) {
475 w_byte(TYPE_IVAR, arg);
480w_encname(VALUE encname, struct dump_arg *arg)
482 if (!NIL_P(encname)) {
483 struct dump_call_arg c_arg;
487 w_encoding(encname, &c_arg);
492w_symbol(VALUE sym, struct dump_arg *arg)
497 if (st_lookup(arg->symbols, sym, &num)) {
498 w_byte(TYPE_SYMLINK, arg);
499 w_long((long)num, arg);
502 const VALUE orig_sym = sym;
503 sym = rb_sym2str(sym);
505 rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
507 encname = w_encivar(sym, arg);
508 w_byte(TYPE_SYMBOL, arg);
509 w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
510 st_add_direct(arg->symbols, orig_sym, arg->symbols->num_entries);
511 w_encname(encname, arg);
516w_unique(VALUE s, struct dump_arg *arg)
518 must_not_be_anonymous("class", s);
519 w_symbol(rb_str_intern(s), arg);
522static void w_object(VALUE,struct dump_arg*,int);
525hash_each(VALUE key, VALUE value, VALUE v)
527 struct dump_call_arg *arg = (void *)v;
528 w_object(key, arg->arg, arg->limit);
529 w_object(value, arg->arg, arg->limit);
533#define SINGLETON_DUMP_UNABLE_P(klass) \
534 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
535 rb_ivar_count(klass) > 0)
538w_extended(VALUE klass, struct dump_arg *arg, int check)
540 if (check && RCLASS_SINGLETON_P(klass)) {
541 VALUE origin = RCLASS_ORIGIN(klass);
542 if (SINGLETON_DUMP_UNABLE_P(klass) ||
543 (origin != klass && SINGLETON_DUMP_UNABLE_P(origin))) {
544 rb_raise(rb_eTypeError, "singleton can't be dumped");
546 klass = RCLASS_SUPER(klass);
548 while (BUILTIN_TYPE(klass) == T_ICLASS) {
549 if (!RICLASS_IS_ORIGIN_P(klass) ||
550 BUILTIN_TYPE(RBASIC(klass)->klass) != T_MODULE) {
551 VALUE path = rb_class_name(RBASIC(klass)->klass);
552 w_byte(TYPE_EXTENDED, arg);
555 klass = RCLASS_SUPER(klass);
560w_class(char type, VALUE obj, struct dump_arg *arg, int check)
566 if (arg->compat_tbl &&
567 st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
568 obj = (VALUE)real_obj;
570 klass = CLASS_OF(obj);
571 w_extended(klass, arg, check);
573 path = class2path(rb_class_real(klass));
578w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
580 VALUE klass = CLASS_OF(obj);
582 w_extended(klass, arg, TRUE);
583 klass = rb_class_real(klass);
584 if (klass != super) {
585 w_byte(TYPE_UCLASS, arg);
586 w_unique(class2path(klass), arg);
591rb_hash_ruby2_keywords_p(VALUE obj)
593 return (RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS) != 0;
597rb_hash_ruby2_keywords(VALUE obj)
599 RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
603 * if instance variable name `id` is a special name to be skipped,
604 * returns the name of it. otherwise it cannot be dumped (unnamed),
605 * returns `name` as-is. returns NULL for ID that can be dumped.
607static inline const char *
608skipping_ivar_name(const ID id, const char *name)
610#define IS_SKIPPED_IVAR(idname) \
611 ((id == idname) && (name = name_##idname, true))
612 if (IS_SKIPPED_IVAR(s_encoding_short)) return name;
613 if (IS_SKIPPED_IVAR(s_ruby2_keywords_flag)) return name;
614 if (IS_SKIPPED_IVAR(s_encoding_long)) return name;
615 if (!rb_id2str(id)) return name;
620 struct dump_call_arg *dump;
625w_obj_each(ID id, VALUE value, st_data_t a)
627 struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
628 struct dump_call_arg *arg = ivarg->dump;
629 const char unnamed[] = "", *ivname = skipping_ivar_name(id, unnamed);
632 if (ivname != unnamed) {
633 rb_warn("instance variable '%s' on class %"PRIsVALUE" is not dumped",
634 ivname, CLASS_OF(arg->obj));
639 w_symbol(ID2SYM(id), arg->arg);
640 w_object(value, arg->arg, arg->limit);
645obj_count_ivars(ID id, VALUE val, st_data_t a)
647 if (!skipping_ivar_name(id, "") && UNLIKELY(!++*(st_index_t *)a)) {
648 rb_raise(rb_eRuntimeError, "too many instance variables");
654encoding_name(VALUE obj, struct dump_arg *arg)
656 if (rb_enc_capable(obj)) {
657 int encidx = rb_enc_get_index(obj);
658 rb_encoding *enc = 0;
661 if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
665 /* special treatment for US-ASCII and UTF-8 */
666 if (encidx == rb_usascii_encindex()) {
669 else if (encidx == rb_utf8_encindex()) {
674 !st_lookup(arg->encodings, (st_data_t)rb_enc_name(enc), &name) :
675 (arg->encodings = st_init_strcasetable(), 1)) {
676 name = (st_data_t)rb_str_new_cstr(rb_enc_name(enc));
677 st_insert(arg->encodings, (st_data_t)rb_enc_name(enc), name);
687w_encoding(VALUE encname, struct dump_call_arg *arg)
689 int limit = arg->limit;
690 if (limit >= 0) ++limit;
694 w_symbol(ID2SYM(s_encoding_short), arg->arg);
695 w_object(encname, arg->arg, limit);
700 w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
701 w_object(encname, arg->arg, limit);
706has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
708 st_index_t num = !NIL_P(encname);
710 if (SPECIAL_CONST_P(obj)) goto generic;
711 switch (BUILTIN_TYPE(obj)) {
715 break; /* counted elsewhere */
717 if (rb_hash_ruby2_keywords_p(obj)) ++num;
721 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
722 if (num) *ivobj = obj;
729w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
731 struct w_ivar_arg ivarg = {arg, num};
733 rb_ivar_foreach_buffered(obj, w_obj_each, (st_data_t)&ivarg);
737w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
739 w_long(num, arg->arg);
740 num -= w_encoding(encname, arg);
741 if (RB_TYPE_P(ivobj, T_HASH) && rb_hash_ruby2_keywords_p(ivobj)) {
742 int limit = arg->limit;
743 if (limit >= 0) ++limit;
744 w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
745 w_object(Qtrue, arg->arg, limit);
748 if (!UNDEF_P(ivobj) && num) {
749 w_ivar_each(ivobj, num, arg);
754w_objivar(VALUE obj, struct dump_call_arg *arg)
758 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
759 w_long(num, arg->arg);
760 w_ivar_each(obj, num, arg);
764// Optimized dump for fixnum larger than 31-bits
766w_bigfixnum(VALUE obj, struct dump_arg *arg)
768 RUBY_ASSERT(FIXNUM_P(obj));
770 w_byte(TYPE_BIGNUM, arg);
772#if SIZEOF_LONG == SIZEOF_VALUE
776 long long num, slen_num;
780 char sign = num < 0 ? '-' : '+';
783 // Guaranteed not to overflow, as FIXNUM is 1-bit less than long
784 if (num < 0) num = -num;
786 // calculate the size in shorts
792 slen_num = SHORTDN(slen_num);
796 RUBY_ASSERT(slen > 0 && slen <= SIZEOF_LONG / 2);
798 w_long((long)slen, arg);
800 for (int i = 0; i < slen; i++) {
801 w_short(num & SHORTMASK, arg);
805 // We aren't adding this object to the link table, but we need to increment
809 RUBY_ASSERT(num == 0);
814w_remember(VALUE obj, struct dump_arg *arg)
816 st_add_direct(arg->data, obj, arg->num_entries++);
820w_object(VALUE obj, struct dump_arg *arg, int limit)
822 struct dump_call_arg c_arg;
823 VALUE ivobj = Qundef;
825 st_index_t hasiv = 0;
826 VALUE encname = Qnil;
829 rb_raise(rb_eArgError, "exceed depth limit");
833 w_byte(TYPE_NIL, arg);
835 else if (obj == Qtrue) {
836 w_byte(TYPE_TRUE, arg);
838 else if (obj == Qfalse) {
839 w_byte(TYPE_FALSE, arg);
841 else if (FIXNUM_P(obj)) {
843 w_byte(TYPE_FIXNUM, arg);
844 w_long(FIX2INT(obj), arg);
846 if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
847 w_byte(TYPE_FIXNUM, arg);
848 w_long(FIX2LONG(obj), arg);
851 w_bigfixnum(obj, arg);
855 else if (SYMBOL_P(obj)) {
859 if (st_lookup(arg->data, obj, &num)) {
860 w_byte(TYPE_LINK, arg);
861 w_long((long)num, arg);
865 if (limit > 0) limit--;
871 w_remember(obj, arg);
872 w_byte(TYPE_FLOAT, arg);
873 w_float(RFLOAT_VALUE(obj), arg);
879 if (!RBASIC_CLASS(obj)) {
880 rb_raise(rb_eTypeError, "can't dump internal %s",
881 rb_builtin_type_name(BUILTIN_TYPE(obj)));
884 if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
885 w_remember(obj, arg);
887 v = dump_funcall(arg, obj, s_mdump, 0, 0);
888 w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
889 w_object(v, arg, limit);
892 if (rb_obj_respond_to(obj, s_dump, TRUE)) {
893 VALUE ivobj2 = Qundef;
897 if (arg->userdefs && st_is_member(arg->userdefs, (st_data_t)obj)) {
898 rb_raise(rb_eRuntimeError, "can't dump recursive object using _dump()");
901 v = dump_funcall(arg, obj, s_dump, 1, &v);
902 if (!RB_TYPE_P(v, T_STRING)) {
903 rb_raise(rb_eTypeError, "_dump() must return string");
905 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
906 hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
912 if (hasiv) w_byte(TYPE_IVAR, arg);
913 w_class(TYPE_USERDEF, obj, arg, FALSE);
914 w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
916 st_data_t userdefs = (st_data_t)obj;
917 if (!arg->userdefs) {
918 arg->userdefs = rb_init_identtable();
920 st_add_direct(arg->userdefs, userdefs, 0);
921 w_ivar(hasiv, ivobj, encname, &c_arg);
922 st_delete(arg->userdefs, &userdefs, NULL);
924 w_remember(obj, arg);
928 w_remember(obj, arg);
930 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
932 st_data_t compat_data;
933 VALUE klass = CLASS_OF(obj);
934 rb_alloc_func_t allocator = RCLASS_SINGLETON_P(klass) ? 0 : rb_get_alloc_func(klass);
935 if (allocator && st_lookup(compat_allocator_tbl,
936 (st_data_t)allocator,
938 marshal_compat_t *compat = (marshal_compat_t*)compat_data;
939 VALUE real_obj = obj;
940 obj = compat->dumper(real_obj);
941 if (!arg->compat_tbl) {
942 arg->compat_tbl = rb_init_identtable();
944 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
945 if (obj != real_obj && UNDEF_P(ivobj)) hasiv = 0;
948 if (hasiv) w_byte(TYPE_IVAR, arg);
950 switch (BUILTIN_TYPE(obj)) {
952 if (FL_TEST(obj, FL_SINGLETON)) {
953 rb_raise(rb_eTypeError, "singleton class can't be dumped");
956 VALUE path = class2path(obj);
957 VALUE encname = w_encivar(path, arg);
958 w_byte(TYPE_CLASS, arg);
959 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
960 w_encname(encname, arg);
967 VALUE path = class2path(obj);
968 VALUE encname = w_encivar(path, arg);
969 w_byte(TYPE_MODULE, arg);
970 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
971 w_encname(encname, arg);
977 w_byte(TYPE_FLOAT, arg);
978 w_float(RFLOAT_VALUE(obj), arg);
982 w_byte(TYPE_BIGNUM, arg);
984 char sign = BIGNUM_SIGN(obj) ? '+' : '-';
985 size_t len = BIGNUM_LEN(obj);
988 BDIGIT *d = BIGNUM_DIGITS(obj);
990 slen = SHORTLEN(len);
991 if (LONG_MAX < slen) {
992 rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
996 w_long((long)slen, arg);
997 for (j = 0; j < len; j++) {
998#if SIZEOF_BDIGIT > SIZEOF_SHORT
1002 for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
1003 w_short(num & SHORTMASK, arg);
1005 if (j == len - 1 && num == 0) break;
1016 w_uclass(obj, rb_cString, arg);
1017 w_byte(TYPE_STRING, arg);
1018 w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
1022 w_uclass(obj, rb_cRegexp, arg);
1023 w_byte(TYPE_REGEXP, arg);
1025 int opts = rb_reg_options(obj);
1026 w_bytes(RREGEXP_SRC_PTR(obj), RREGEXP_SRC_LEN(obj), arg);
1027 w_byte((char)opts, arg);
1032 w_uclass(obj, rb_cArray, arg);
1033 w_byte(TYPE_ARRAY, arg);
1035 long i, len = RARRAY_LEN(obj);
1038 for (i=0; i<RARRAY_LEN(obj); i++) {
1039 w_object(RARRAY_AREF(obj, i), arg, limit);
1040 if (len != RARRAY_LEN(obj)) {
1041 rb_raise(rb_eRuntimeError, "array modified during dump");
1048 w_uclass(obj, rb_cHash, arg);
1049 if (rb_hash_compare_by_id_p(obj)) {
1050 w_byte(TYPE_UCLASS, arg);
1051 w_symbol(rb_sym_intern_ascii_cstr("Hash"), arg);
1053 if (NIL_P(RHASH_IFNONE(obj))) {
1054 w_byte(TYPE_HASH, arg);
1056 else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
1057 rb_raise(rb_eTypeError, "can't dump hash with default proc");
1060 w_byte(TYPE_HASH_DEF, arg);
1062 w_long(rb_hash_size_num(obj), arg);
1063 rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
1064 if (!NIL_P(RHASH_IFNONE(obj))) {
1065 w_object(RHASH_IFNONE(obj), arg, limit);
1070 w_class(TYPE_STRUCT, obj, arg, TRUE);
1072 long len = RSTRUCT_LEN_RAW(obj);
1077 mem = rb_struct_members(obj);
1078 for (i=0; i<len; i++) {
1079 w_symbol(RARRAY_AREF(mem, i), arg);
1080 w_object(RSTRUCT_GET_RAW(obj, i), arg, limit);
1086 w_class(TYPE_OBJECT, obj, arg, TRUE);
1087 w_objivar(obj, &c_arg);
1094 if (!rb_obj_respond_to(obj, s_dump_data, TRUE)) {
1095 rb_raise(rb_eTypeError,
1096 "no _dump_data is defined for class %"PRIsVALUE,
1099 v = dump_funcall(arg, obj, s_dump_data, 0, 0);
1100 w_class(TYPE_DATA, obj, arg, TRUE);
1101 w_object(v, arg, limit);
1106 rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
1113 w_ivar(hasiv, ivobj, encname, &c_arg);
1118clear_dump_arg(struct dump_arg *arg)
1120 if (!arg->symbols) return;
1121 st_free_table(arg->symbols);
1123 st_free_table(arg->data);
1125 arg->num_entries = 0;
1126 if (arg->compat_tbl) {
1127 st_free_table(arg->compat_tbl);
1128 arg->compat_tbl = 0;
1130 if (arg->encodings) {
1131 st_free_table(arg->encodings);
1134 if (arg->userdefs) {
1135 st_free_table(arg->userdefs);
1140NORETURN(static inline void io_needed(void));
1144 rb_raise(rb_eTypeError, "instance of IO needed");
1149 * dump( obj [, anIO] , limit=-1 ) -> anIO
1151 * Serializes obj and all descendant objects. If anIO is
1152 * specified, the serialized data will be written to it, otherwise the
1153 * data will be returned as a String. If limit is specified, the
1154 * traversal of subobjects will be limited to that depth. If limit is
1155 * negative, no checking of depth will be performed.
1158 * def initialize(str)
1166 * (produces no output)
1168 * o = Klass.new("hello\n")
1169 * data = Marshal.dump(o)
1170 * obj = Marshal.load(data)
1171 * obj.say_hello #=> "hello\n"
1173 * Marshal can't dump following objects:
1174 * * anonymous Class/Module.
1175 * * objects which are related to system (ex: Dir, File::Stat, IO, File, Socket
1177 * * an instance of MatchData, Method, UnboundMethod, Proc, Thread,
1178 * ThreadGroup, Continuation
1179 * * objects which define singleton methods
1182marshal_dump(int argc, VALUE *argv, VALUE _)
1184 VALUE obj, port, a1, a2;
1188 rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
1190 if (!NIL_P(a2)) limit = NUM2INT(a2);
1191 if (NIL_P(a1)) io_needed();
1194 else if (argc == 2) {
1195 if (FIXNUM_P(a1)) limit = FIX2INT(a1);
1196 else if (NIL_P(a1)) io_needed();
1199 return rb_marshal_dump_limited(obj, port, limit);
1203rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
1205 struct dump_arg *arg;
1206 VALUE wrapper; /* used to avoid memory leak in case of exception */
1208 wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
1210 arg->symbols = st_init_numtable();
1211 arg->data = rb_init_identtable();
1212 arg->num_entries = 0;
1213 arg->compat_tbl = 0;
1216 arg->str = rb_str_buf_new(0);
1218 if (!rb_respond_to(port, s_write)) {
1222 dump_check_funcall(arg, port, s_binmode, 0, 0);
1228 w_byte(MARSHAL_MAJOR, arg);
1229 w_byte(MARSHAL_MINOR, arg);
1231 w_object(obj, arg, limit);
1233 rb_io_write(arg->dest, arg->str);
1234 rb_str_resize(arg->str, 0);
1236 clear_dump_arg(arg);
1237 RB_GC_GUARD(wrapper);
1250 st_table *partial_objects;
1252 st_table *compat_tbl;
1257check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
1259 if (!arg->symbols) {
1260 rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
1265#define load_funcall(arg, obj, sym, argc, argv) \
1266 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1268static void clear_load_arg(struct load_arg *arg);
1271mark_load_arg(void *ptr)
1273 struct load_arg *p = ptr;
1276 rb_mark_tbl(p->symbols);
1277 rb_mark_tbl(p->data);
1278 rb_mark_tbl(p->partial_objects);
1279 rb_mark_hash(p->compat_tbl);
1283free_load_arg(void *ptr)
1285 clear_load_arg(ptr);
1289memsize_load_arg(const void *ptr)
1291 const struct load_arg *p = (struct load_arg *)ptr;
1293 if (p->symbols) memsize += rb_st_memsize(p->symbols);
1294 if (p->data) memsize += rb_st_memsize(p->data);
1295 if (p->partial_objects) memsize += rb_st_memsize(p->partial_objects);
1296 if (p->compat_tbl) memsize += rb_st_memsize(p->compat_tbl);
1300static const rb_data_type_t load_arg_data = {
1302 {mark_load_arg, free_load_arg, memsize_load_arg,},
1303 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE
1306#define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1307static VALUE r_object(struct load_arg *arg);
1308static VALUE r_symbol(struct load_arg *arg);
1310NORETURN(static void too_short(void));
1314 rb_raise(rb_eArgError, "marshal data too short");
1318r_prepare(struct load_arg *arg)
1320 st_index_t idx = arg->data->num_entries;
1322 st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
1327r_byte1_buffered(struct load_arg *arg)
1329 if (arg->buflen == 0) {
1330 long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ;
1331 VALUE str, n = LONG2NUM(readable);
1333 str = load_funcall(arg, arg->src, s_read, 1, &n);
1334 if (NIL_P(str)) too_short();
1336 memcpy(arg->buf, RSTRING_PTR(str), RSTRING_LEN(str));
1338 arg->buflen = RSTRING_LEN(str);
1341 return arg->buf[arg->offset++];
1345r_byte(struct load_arg *arg)
1349 if (RB_TYPE_P(arg->src, T_STRING)) {
1350 if (RSTRING_LEN(arg->src) > arg->offset) {
1351 c = (unsigned char)RSTRING_PTR(arg->src)[arg->offset++];
1358 if (arg->readable >0 || arg->buflen > 0) {
1359 c = r_byte1_buffered(arg);
1362 VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0);
1363 if (NIL_P(v)) rb_eof_error();
1364 c = (unsigned char)NUM2CHR(v);
1370NORETURN(static void long_toobig(int size));
1373long_toobig(int size)
1375 rb_raise(rb_eTypeError, "long too big for this architecture (size "
1376 STRINGIZE(SIZEOF_LONG)", given %d)", size);
1380r_long(struct load_arg *arg)
1383 int c = (signed char)r_byte(arg);
1386 if (c == 0) return 0;
1388 if (4 < c && c < 128) {
1391 if (c > (int)sizeof(long)) long_toobig(c);
1394 x |= (long)r_byte(arg) << (8*i);
1398 if (-129 < c && c < -4) {
1402 if (c > (int)sizeof(long)) long_toobig(c);
1405 x &= ~((long)0xff << (8*i));
1406 x |= (long)r_byte(arg) << (8*i);
1413ruby_marshal_read_long(const char **buf, long len)
1416 struct RString src = {RBASIC_INIT};
1417 struct load_arg arg;
1418 memset(&arg, 0, sizeof(arg));
1419 arg.src = rb_setup_fake_str(&src, *buf, len, 0);
1426r_bytes1(long len, struct load_arg *arg)
1428 VALUE str, n = LONG2NUM(len);
1430 str = load_funcall(arg, arg->src, s_read, 1, &n);
1431 if (NIL_P(str)) too_short();
1433 if (RSTRING_LEN(str) != len) too_short();
1439r_bytes1_buffered(long len, struct load_arg *arg)
1443 if (len <= arg->buflen) {
1444 str = rb_str_new(arg->buf+arg->offset, len);
1449 long buflen = arg->buflen;
1450 long readable = arg->readable + 1;
1451 long tmp_len, read_len, need_len = len - buflen;
1454 readable = readable < BUFSIZ ? readable : BUFSIZ;
1455 read_len = need_len > readable ? need_len : readable;
1456 n = LONG2NUM(read_len);
1457 tmp = load_funcall(arg, arg->src, s_read, 1, &n);
1458 if (NIL_P(tmp)) too_short();
1461 tmp_len = RSTRING_LEN(tmp);
1463 if (tmp_len < need_len) too_short();
1465 str = rb_str_new(arg->buf+arg->offset, buflen);
1466 rb_str_cat(str, RSTRING_PTR(tmp), need_len);
1468 if (tmp_len > need_len) {
1469 buflen = tmp_len - need_len;
1470 memcpy(arg->buf, RSTRING_PTR(tmp)+need_len, buflen);
1471 arg->buflen = buflen;
1482#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1485r_bytes0(long len, struct load_arg *arg)
1489 if (len == 0) return rb_str_new(0, 0);
1490 if (RB_TYPE_P(arg->src, T_STRING)) {
1491 if (RSTRING_LEN(arg->src) - arg->offset >= len) {
1492 str = rb_str_new(RSTRING_PTR(arg->src)+arg->offset, len);
1500 if (arg->readable > 0 || arg->buflen > 0) {
1501 str = r_bytes1_buffered(len, arg);
1504 str = r_bytes1(len, arg);
1511name_equal(const char *name, size_t nlen, const char *p, long l)
1513 if ((size_t)l != nlen || *p != *name) return 0;
1514 return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
1518sym2encidx(VALUE sym, VALUE val)
1520 RBIMPL_ATTR_NONSTRING() static const char name_encoding[8] = "encoding";
1523 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
1524 RSTRING_GETMEM(sym, p, l);
1525 if (l <= 0) return -1;
1526 if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
1527 int idx = rb_enc_find_index(StringValueCStr(val));
1530 if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
1531 if (val == Qfalse) return rb_usascii_encindex();
1532 else if (val == Qtrue) return rb_utf8_encindex();
1539symname_equal(VALUE sym, const char *name, size_t nlen)
1543 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return 0;
1544 RSTRING_GETMEM(sym, p, l);
1545 return name_equal(name, nlen, p, l);
1548#define BUILD_ASSERT_POSITIVE(n) \
1549 /* make 0 negative to workaround the "zero size array" GCC extension, */ \
1550 ((sizeof(char [2*(ssize_t)(n)-1])+1)/2) /* assuming no overflow */
1551#define symname_equal_lit(sym, sym_name) \
1552 symname_equal(sym, sym_name, BUILD_ASSERT_POSITIVE(rb_strlen_lit(sym_name)))
1555r_symlink(struct load_arg *arg)
1558 long num = r_long(arg);
1560 if (!st_lookup(arg->symbols, num, &sym)) {
1561 rb_raise(rb_eArgError, "bad symbol");
1567r_symreal(struct load_arg *arg, int ivar)
1569 VALUE s = r_bytes(arg);
1572 st_index_t n = arg->symbols->num_entries;
1574 if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
1575 st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
1577 long num = r_long(arg);
1579 sym = r_symbol(arg);
1580 idx = sym2encidx(sym, r_object(arg));
1584 rb_enc_associate_index(s, idx);
1585 if (is_broken_string(s)) {
1586 rb_raise(rb_eArgError, "invalid byte sequence in %s: %+"PRIsVALUE,
1587 rb_enc_name(rb_enc_from_index(idx)), s);
1595r_symbol(struct load_arg *arg)
1600 switch ((type = r_byte(arg))) {
1602 rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
1607 return r_symreal(arg, ivar);
1610 rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
1612 return r_symlink(arg);
1617r_unique(struct load_arg *arg)
1619 return r_symbol(arg);
1623r_string(struct load_arg *arg)
1625 return r_bytes(arg);
1629r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
1631 st_data_t real_obj = (st_data_t)v;
1632 if (arg->compat_tbl) {
1633 /* real_obj is kept if not found */
1634 st_lookup(arg->compat_tbl, v, &real_obj);
1636 st_insert(arg->data, num, real_obj);
1637 st_insert(arg->partial_objects, (st_data_t)real_obj, Qtrue);
1642r_fixup_compat(VALUE v, struct load_arg *arg)
1645 st_data_t key = (st_data_t)v;
1646 if (arg->compat_tbl && st_delete(arg->compat_tbl, &key, &data)) {
1647 VALUE real_obj = (VALUE)data;
1648 rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj));
1649 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1650 marshal_compat_t *compat = (marshal_compat_t*)data;
1651 compat->loader(real_obj, v);
1659r_post_proc(VALUE v, struct load_arg *arg)
1662 v = load_funcall(arg, arg->proc, s_call, 1, &v);
1668r_leave(VALUE v, struct load_arg *arg, bool partial)
1670 v = r_fixup_compat(v, arg);
1673 st_data_t key = (st_data_t)v;
1674 st_delete(arg->partial_objects, &key, &data);
1676 if (RB_TYPE_P(v, T_MODULE) || RB_TYPE_P(v, T_CLASS)) {
1679 else if (RB_TYPE_P(v, T_STRING)) {
1680 v = rb_str_to_interned_str(v);
1686 v = r_post_proc(v, arg);
1692copy_ivar_i(ID vid, VALUE value, st_data_t arg)
1694 VALUE obj = (VALUE)arg;
1696 if (!rb_ivar_defined(obj, vid))
1697 rb_ivar_set(obj, vid, value);
1702r_copy_ivar(VALUE v, VALUE data)
1704 rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
1708#define override_ivar_error(type, str) \
1709 rb_raise(rb_eTypeError, \
1710 "can't override instance variable of "type" '%"PRIsVALUE"'", \
1714r_ivar_encoding(VALUE obj, struct load_arg *arg, VALUE sym, VALUE val)
1716 int idx = sym2encidx(sym, val);
1718 if (rb_enc_capable(obj)) {
1719 // Check if needed to avoid rb_check_frozen() check for Regexps
1720 if (rb_enc_get_index(obj) != idx) {
1721 rb_enc_associate_index(obj, idx);
1725 rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
1733r_encname(VALUE obj, struct load_arg *arg)
1735 long len = r_long(arg);
1737 VALUE sym = r_symbol(arg);
1738 VALUE val = r_object(arg);
1739 len -= r_ivar_encoding(obj, arg, sym, val);
1745r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
1751 if (RB_TYPE_P(obj, T_MODULE)) {
1752 override_ivar_error("module", rb_mod_name(obj));
1754 else if (RB_TYPE_P(obj, T_CLASS)) {
1755 override_ivar_error("class", rb_class_name(obj));
1758 VALUE sym = r_symbol(arg);
1759 VALUE val = r_object(arg);
1760 if (r_ivar_encoding(obj, arg, sym, val)) {
1761 if (has_encoding) *has_encoding = TRUE;
1763 else if (symname_equal_lit(sym, name_s_ruby2_keywords_flag)) {
1764 if (RB_TYPE_P(obj, T_HASH)) {
1765 rb_hash_ruby2_keywords(obj);
1768 rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
1772 rb_ivar_set(obj, rb_intern_str(sym), val);
1774 } while (--len > 0);
1779path2class(VALUE path)
1781 VALUE v = rb_path_to_class(path);
1783 if (!RB_TYPE_P(v, T_CLASS)) {
1784 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
1789#define path2module(path) must_be_module(rb_path_to_class(path), path)
1792must_be_module(VALUE v, VALUE path)
1794 if (!RB_TYPE_P(v, T_MODULE)) {
1795 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
1801obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
1804 rb_alloc_func_t allocator;
1806 allocator = rb_get_alloc_func(klass);
1807 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1808 marshal_compat_t *compat = (marshal_compat_t*)data;
1809 VALUE real_obj = rb_obj_alloc(klass);
1810 VALUE obj = rb_obj_alloc(compat->oldclass);
1811 if (oldclass) *oldclass = compat->oldclass;
1813 if (!arg->compat_tbl) {
1814 arg->compat_tbl = rb_init_identtable();
1816 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
1820 return rb_obj_alloc(klass);
1824obj_alloc_by_path(VALUE path, struct load_arg *arg)
1826 return obj_alloc_by_klass(path2class(path), arg, 0);
1830append_extmod(VALUE obj, VALUE extmod)
1832 long i = RARRAY_LEN(extmod);
1834 VALUE m = RARRAY_AREF(extmod, --i);
1835 rb_extend_object(obj, m);
1840#define prohibit_ivar(type, str) do { \
1841 if (!ivp || !*ivp) break; \
1842 override_ivar_error(type, str); \
1845static VALUE r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE klass, VALUE extmod, int type);
1848r_object0(struct load_arg *arg, bool partial, int *ivp, VALUE extmod)
1850 int type = r_byte(arg);
1851 return r_object_for(arg, partial, ivp, 0, extmod, type);
1855r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE klass, VALUE extmod, int type)
1857 VALUE (*hash_new_with_size)(st_index_t) = rb_hash_new_with_size;
1865 if (!st_lookup(arg->data, (st_data_t)id, &link)) {
1866 rb_raise(rb_eArgError, "dump format error (unlinked)");
1869 if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
1870 if (arg->freeze && RB_TYPE_P(v, T_STRING)) {
1871 v = rb_str_to_interned_str(v);
1873 v = r_post_proc(v, arg);
1880 v = r_object0(arg, true, &ivar, extmod);
1881 if (ivar) r_ivar(v, NULL, arg);
1882 v = r_leave(v, arg, partial);
1888 VALUE path = r_unique(arg);
1889 VALUE m = rb_path_to_class(path);
1890 if (NIL_P(extmod)) extmod = rb_ary_hidden_new(0);
1892 if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
1895 v = r_object0(arg, true, 0, Qnil);
1897 if (c != m || FL_TEST(c, FL_SINGLETON)) {
1898 rb_raise(rb_eArgError,
1899 "prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
1900 path, rb_class_name(c));
1902 c = rb_singleton_class(v);
1903 while (RARRAY_LEN(extmod) > 0) {
1904 m = rb_ary_pop(extmod);
1905 rb_prepend_module(c, m);
1909 must_be_module(m, path);
1910 rb_ary_push(extmod, m);
1912 v = r_object0(arg, true, 0, extmod);
1913 while (RARRAY_LEN(extmod) > 0) {
1914 m = rb_ary_pop(extmod);
1915 rb_extend_object(v, m);
1918 v = r_leave(v, arg, partial);
1924 VALUE c = path2class(r_unique(arg));
1926 if (FL_TEST(c, FL_SINGLETON)) {
1927 rb_raise(rb_eTypeError, "singleton can't be loaded");
1930 if ((c == rb_cHash) &&
1931 /* Hack for compare_by_identity */
1932 (type == TYPE_HASH || type == TYPE_HASH_DEF)) {
1933 hash_new_with_size = rb_ident_hash_new_with_size;
1936 v = r_object_for(arg, partial, 0, c, extmod, type);
1937 if (RB_SPECIAL_CONST_P(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
1940 if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
1941 VALUE tmp = rb_obj_alloc(c);
1943 if (TYPE(v) != TYPE(tmp)) goto format_error;
1945 RBASIC_SET_CLASS(v, c);
1950 rb_raise(rb_eArgError, "dump format error (user class)");
1954 v = r_leave(v, arg, false);
1959 v = r_leave(v, arg, false);
1964 v = r_leave(v, arg, false);
1969 long i = r_long(arg);
1972 v = r_leave(v, arg, false);
1978 VALUE str = r_bytes(arg);
1979 const char *ptr = RSTRING_PTR(str);
1981 if (strcmp(ptr, "nan") == 0) {
1984 else if (strcmp(ptr, "inf") == 0) {
1987 else if (strcmp(ptr, "-inf") == 0) {
1992 d = strtod(ptr, &e);
1993 d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
1996 v = r_entry(v, arg);
1997 v = r_leave(v, arg, false);
2010 if (SIZEOF_VALUE >= 8 && len <= 4) {
2011 // Representable within uintptr, likely FIXNUM
2013 for (int i = 0; i < len; i++) {
2014 num |= (VALUE)r_byte(arg) << (i * 16);
2015 num |= (VALUE)r_byte(arg) << (i * 16 + 8);
2017#if SIZEOF_VALUE == SIZEOF_LONG
2023 v = rb_int_uminus(v);
2027 data = r_bytes0(len * 2, arg);
2028 v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
2029 INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
2030 rb_str_resize(data, 0L);
2032 v = r_entry(v, arg);
2033 v = r_leave(v, arg, false);
2038 v = r_entry(r_string(arg), arg);
2039 v = r_leave(v, arg, partial);
2044 VALUE str = r_bytes(arg);
2045 int options = r_byte(arg);
2046 int has_encoding = FALSE;
2047 st_index_t idx = r_prepare(arg);
2050 r_ivar(str, &has_encoding, arg);
2053 if (!has_encoding) {
2054 /* 1.8 compatibility; remove escapes undefined in 1.8 */
2055 char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
2056 long len = RSTRING_LEN(str);
2058 for (; len-- > 0; *dst++ = *src++) {
2060 case '\\': bs++; break;
2061 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2062 case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
2063 case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
2064 case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
2065 case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
2068 default: bs = 0; break;
2071 rb_str_set_len(str, dst - ptr);
2076 VALUE regexp = rb_reg_init_str(rb_reg_s_alloc(klass), str, options);
2077 r_copy_ivar(regexp, str);
2079 v = r_entry0(regexp, idx, arg);
2080 v = r_leave(v, arg, partial);
2086 long len = r_long(arg);
2088 v = rb_ary_new2(len);
2089 v = r_entry(v, arg);
2090 arg->readable += len - 1;
2092 rb_ary_push(v, r_object(arg));
2095 v = r_leave(v, arg, partial);
2104 long len = r_long(arg);
2106 v = hash_new_with_size(len);
2107 v = r_entry(v, arg);
2108 arg->readable += (len - 1) * 2;
2110 VALUE key = r_object(arg);
2111 VALUE value = r_object(arg);
2112 rb_hash_aset(v, key, value);
2116 if (type == TYPE_HASH_DEF) {
2117 RHASH_SET_IFNONE(v, r_object(arg));
2119 v = r_leave(v, arg, partial);
2128 st_index_t idx = r_prepare(arg);
2129 VALUE klass = path2class(r_unique(arg));
2130 long len = r_long(arg);
2132 v = rb_obj_alloc(klass);
2133 if (!RB_TYPE_P(v, T_STRUCT)) {
2134 rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
2136 mem = rb_struct_s_members(klass);
2137 if (RARRAY_LEN(mem) != len) {
2138 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
2139 rb_class_name(klass));
2142 arg->readable += (len - 1) * 2;
2143 v = r_entry0(v, idx, arg);
2144 values = rb_ary_new2(len);
2146 VALUE keywords = Qfalse;
2147 if (RTEST(rb_struct_s_keyword_init(klass))) {
2148 keywords = rb_hash_new();
2149 rb_ary_push(values, keywords);
2152 for (i=0; i<len; i++) {
2153 VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
2154 slot = r_symbol(arg);
2156 if (!rb_str_equal(n, slot)) {
2157 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
2158 rb_class_name(klass),
2162 rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
2165 rb_ary_push(values, r_object(arg));
2170 rb_struct_initialize(v, values);
2171 v = r_leave(v, arg, partial);
2178 VALUE name = r_unique(arg);
2179 VALUE klass = path2class(name);
2183 if (!rb_obj_respond_to(klass, s_load, TRUE)) {
2184 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method '_load'",
2187 data = r_string(arg);
2189 r_ivar(data, NULL, arg);
2192 v = load_funcall(arg, klass, s_load, 1, &data);
2193 v = r_entry(v, arg);
2194 if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
2195 marshal_compat_t *compat = (marshal_compat_t*)d;
2196 v = compat->loader(klass, v);
2202 v = r_post_proc(v, arg);
2207 case TYPE_USRMARSHAL:
2209 VALUE name = r_unique(arg);
2210 VALUE klass = path2class(name);
2214 v = obj_alloc_by_klass(klass, arg, &oldclass);
2215 if (!NIL_P(extmod)) {
2216 /* for the case marshal_load is overridden */
2217 append_extmod(v, extmod);
2219 if (!rb_obj_respond_to(v, s_mload, TRUE)) {
2220 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method 'marshal_load'",
2223 v = r_entry(v, arg);
2224 data = r_object(arg);
2225 load_funcall(arg, v, s_mload, 1, &data);
2226 v = r_fixup_compat(v, arg);
2227 v = r_copy_ivar(v, data);
2231 v = r_post_proc(v, arg);
2232 if (!NIL_P(extmod)) {
2233 if (oldclass) append_extmod(v, extmod);
2234 rb_ary_clear(extmod);
2241 st_index_t idx = r_prepare(arg);
2242 v = obj_alloc_by_path(r_unique(arg), arg);
2243 if (!RB_TYPE_P(v, T_OBJECT)) {
2244 rb_raise(rb_eArgError, "dump format error");
2246 v = r_entry0(v, idx, arg);
2247 r_ivar(v, NULL, arg);
2248 v = r_leave(v, arg, partial);
2254 VALUE name = r_unique(arg);
2255 VALUE klass = path2class(name);
2259 v = obj_alloc_by_klass(klass, arg, &oldclass);
2260 if (!RB_TYPE_P(v, T_DATA)) {
2261 rb_raise(rb_eArgError, "dump format error");
2263 v = r_entry(v, arg);
2264 if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
2265 rb_raise(rb_eTypeError,
2266 "class %"PRIsVALUE" needs to have instance method '_load_data'",
2269 r = r_object0(arg, partial, 0, extmod);
2270 load_funcall(arg, v, s_load_data, 1, &r);
2271 v = r_leave(v, arg, partial);
2275 case TYPE_MODULE_OLD:
2277 VALUE str = r_bytes(arg);
2279 v = rb_path_to_class(str);
2280 prohibit_ivar("class/module", str);
2281 v = r_entry(v, arg);
2282 v = r_leave(v, arg, partial);
2288 VALUE str = r_bytes(arg);
2290 if (ivp && *ivp > 0) *ivp = r_encname(str, arg) > 0;
2291 v = path2class(str);
2292 prohibit_ivar("class", str);
2293 v = r_entry(v, arg);
2294 v = r_leave(v, arg, partial);
2300 VALUE str = r_bytes(arg);
2302 if (ivp && *ivp > 0) *ivp = r_encname(str, arg) > 0;
2303 v = path2module(str);
2304 prohibit_ivar("module", str);
2305 v = r_entry(v, arg);
2306 v = r_leave(v, arg, partial);
2312 v = r_symreal(arg, *ivp);
2316 v = r_symreal(arg, 0);
2318 v = rb_str_intern(v);
2319 v = r_leave(v, arg, partial);
2323 v = rb_str_intern(r_symlink(arg));
2327 rb_raise(rb_eArgError, "dump format error(0x%x)", type);
2332 rb_raise(rb_eArgError, "dump format error (bad link)");
2339r_object(struct load_arg *arg)
2341 return r_object0(arg, false, 0, Qnil);
2345clear_load_arg(struct load_arg *arg)
2347 ruby_xfree_sized(arg->buf, BUFSIZ);
2352 if (!arg->symbols) return;
2353 st_free_table(arg->symbols);
2355 st_free_table(arg->data);
2357 st_free_table(arg->partial_objects);
2358 arg->partial_objects = 0;
2359 if (arg->compat_tbl) {
2360 st_free_table(arg->compat_tbl);
2361 arg->compat_tbl = 0;
2366rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze)
2370 VALUE wrapper; /* used to avoid memory leak in case of exception */
2371 struct load_arg *arg;
2373 v = rb_check_string_type(port);
2377 else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
2378 rb_check_funcall(port, s_binmode, 0, 0);
2383 wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
2386 arg->symbols = st_init_numtable();
2387 arg->data = rb_init_identtable();
2388 arg->partial_objects = rb_init_identtable();
2389 arg->compat_tbl = 0;
2392 arg->freeze = freeze;
2395 arg->buf = xmalloc(BUFSIZ);
2399 major = r_byte(arg);
2400 minor = r_byte(arg);
2401 if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
2402 clear_load_arg(arg);
2403 rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
2404\tformat version %d.%d required; %d.%d given",
2405 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2407 if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
2408 rb_warn("incompatible marshal file format (can be read)\n\
2409\tformat version %d.%d required; %d.%d given",
2410 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2413 if (!NIL_P(proc)) arg->proc = proc;
2415 clear_load_arg(arg);
2416 RB_GC_GUARD(wrapper);
2422marshal_load(rb_execution_context_t *ec, VALUE mod, VALUE source, VALUE proc, VALUE freeze)
2424 return rb_marshal_load_with_proc(source, proc, RTEST(freeze));
2427#include "marshal.rbinc"
2430 * The marshaling library converts collections of Ruby objects into a
2431 * byte stream, allowing them to be stored outside the currently
2432 * active script. This data may subsequently be read and the original
2433 * objects reconstituted.
2435 * Marshaled data has major and minor version numbers stored along
2436 * with the object information. In normal use, marshaling can only
2437 * load data written with the same major version number and an equal
2438 * or lower minor version number. If Ruby's ``verbose'' flag is set
2439 * (normally using -d, -v, -w, or --verbose) the major and minor
2440 * numbers must match exactly. Marshal versioning is independent of
2441 * Ruby's version numbers. You can extract the version by reading the
2442 * first two bytes of marshaled data.
2444 * str = Marshal.dump("thing")
2445 * RUBY_VERSION #=> "1.9.0"
2449 * Some objects cannot be dumped: if the objects to be dumped include
2450 * bindings, procedure or method objects, instances of class IO, or
2451 * singleton objects, a TypeError will be raised.
2453 * If your class has special serialization needs (for example, if you
2454 * want to serialize in some specific format), or if it contains
2455 * objects that would otherwise not be serializable, you can implement
2456 * your own serialization strategy.
2458 * There are two methods of doing this, your object can define either
2459 * marshal_dump and marshal_load or _dump and _load. marshal_dump will take
2460 * precedence over _dump if both are defined. marshal_dump may result in
2461 * smaller Marshal strings.
2463 * == Security considerations
2465 * By design, Marshal.load can deserialize almost any class loaded into the
2466 * Ruby process. In many cases this can lead to remote code execution if the
2467 * Marshal data is loaded from an untrusted source.
2469 * As a result, Marshal.load is not suitable as a general purpose serialization
2470 * format and you should never unmarshal user supplied input or other untrusted
2473 * If you need to deserialize untrusted data, use JSON or another serialization
2474 * format that is only able to load simple, 'primitive' types such as String,
2475 * Array, Hash, etc. Never allow user input to specify arbitrary types to
2478 * == marshal_dump and marshal_load
2480 * When dumping an object the method marshal_dump will be called.
2481 * marshal_dump must return a result containing the information necessary for
2482 * marshal_load to reconstitute the object. The result can be any object.
2484 * When loading an object dumped using marshal_dump the object is first
2485 * allocated then marshal_load is called with the result from marshal_dump.
2486 * marshal_load must recreate the object from the information in the result.
2491 * def initialize name, version, data
2493 * @version = version
2501 * def marshal_load array
2502 * @name, @version = array
2506 * == _dump and _load
2508 * Use _dump and _load when you need to allocate the object you're restoring
2511 * When dumping an object the instance method _dump is called with an Integer
2512 * which indicates the maximum depth of objects to dump (a value of -1 implies
2513 * that you should disable depth checking). _dump must return a String
2514 * containing the information necessary to reconstitute the object.
2516 * The class method _load should take a String and use it to return an object
2517 * of the same class.
2522 * def initialize name, version, data
2524 * @version = version
2529 * [@name, @version].join ':'
2532 * def self._load args
2533 * new(*args.split(':'))
2537 * Since Marshal.dump outputs a string you can have _dump return a Marshal
2538 * string which is Marshal.loaded in _load for complex objects.
2543 VALUE rb_mMarshal = rb_define_module("Marshal");
2544#define set_id(sym) sym = rb_intern_const(name_##sym)
2549 set_id(s_dump_data);
2550 set_id(s_load_data);
2557 set_id(s_encoding_short);
2558 set_id(s_ruby2_keywords_flag);
2560 rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
2563 rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
2565 rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
2569marshal_compat_table_mark_and_move_i(st_data_t key, st_data_t value, st_data_t _)
2571 marshal_compat_t *p = (marshal_compat_t *)value;
2572 rb_gc_mark_and_move(&p->newclass);
2573 rb_gc_mark_and_move(&p->oldclass);
2578marshal_compat_table_mark_and_move(void *tbl)
2581 st_foreach(tbl, marshal_compat_table_mark_and_move_i, 0);
2585marshal_compat_table_free_i(st_data_t key, st_data_t value, st_data_t _)
2587 SIZED_FREE((marshal_compat_t *)value);
2592marshal_compat_table_free(void *data)
2594 st_foreach(data, marshal_compat_table_free_i, 0);
2595 st_free_table(data);
2599marshal_compat_table_memsize(const void *data)
2601 return st_memsize(data) + sizeof(marshal_compat_t) * st_table_size(data);
2604static const rb_data_type_t marshal_compat_type = {
2605 .wrap_struct_name = "marshal_compat_table",
2607 .dmark = marshal_compat_table_mark_and_move,
2608 .dfree = marshal_compat_table_free,
2609 .dsize = marshal_compat_table_memsize,
2610 .dcompact = marshal_compat_table_mark_and_move,
2612 .flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY,
2616compat_allocator_table(void)
2618 if (compat_allocator_tbl) return compat_allocator_tbl;
2619 compat_allocator_tbl = st_init_numtable();
2620 compat_allocator_tbl_wrapper =
2621 TypedData_Wrap_Struct(0, &marshal_compat_type, compat_allocator_tbl);
2622 rb_vm_register_global_object(compat_allocator_tbl_wrapper);
2623 return compat_allocator_tbl;
2627rb_marshal_dump(VALUE obj, VALUE port)
2629 return rb_marshal_dump_limited(obj, port, -1);
2633rb_marshal_load(VALUE port)
2635 return rb_marshal_load_with_proc(port, Qnil, false);
Defines RBIMPL_HAS_BUILTIN.
int len
Length of the buffer.
Defines RBIMPL_ATTR_NONSTRING.