Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
marshal.c
1/**********************************************************************
2
3 marshal.c -
4
5 $Author$
6 created at: Thu Apr 27 16:30:01 JST 1995
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <math.h>
15#ifdef HAVE_FLOAT_H
16#include <float.h>
17#endif
18#ifdef HAVE_IEEEFP_H
19#include <ieeefp.h>
20#endif
21
22#include "encindex.h"
23#include "id_table.h"
24#include "internal.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/struct.h"
34#include "internal/symbol.h"
35#include "internal/util.h"
36#include "internal/vm.h"
37#include "ruby/io.h"
38#include "ruby/ruby.h"
39#include "ruby/st.h"
40#include "ruby/util.h"
41#include "builtin.h"
42#include "shape.h"
43
44#define BITSPERSHORT (2*CHAR_BIT)
45#define SHORTMASK ((1<<BITSPERSHORT)-1)
46#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
47
48#if SIZEOF_SHORT == SIZEOF_BDIGIT
49#define SHORTLEN(x) (x)
50#else
51static size_t
52shortlen(size_t len, BDIGIT *ds)
53{
54 BDIGIT num;
55 int offset = 0;
56
57 num = ds[len-1];
58 while (num) {
59 num = SHORTDN(num);
60 offset++;
61 }
62 return (len - 1)*SIZEOF_BDIGIT/2 + offset;
63}
64#define SHORTLEN(x) shortlen((x),d)
65#endif
66
67#define MARSHAL_MAJOR 4
68#define MARSHAL_MINOR 8
69
70#define TYPE_NIL '0'
71#define TYPE_TRUE 'T'
72#define TYPE_FALSE 'F'
73#define TYPE_FIXNUM 'i'
74
75#define TYPE_EXTENDED 'e'
76#define TYPE_UCLASS 'C'
77#define TYPE_OBJECT 'o'
78#define TYPE_DATA 'd'
79#define TYPE_USERDEF 'u'
80#define TYPE_USRMARSHAL 'U'
81#define TYPE_FLOAT 'f'
82#define TYPE_BIGNUM 'l'
83#define TYPE_STRING '"'
84#define TYPE_REGEXP '/'
85#define TYPE_ARRAY '['
86#define TYPE_HASH '{'
87#define TYPE_HASH_DEF '}'
88#define TYPE_STRUCT 'S'
89#define TYPE_MODULE_OLD 'M'
90#define TYPE_CLASS 'c'
91#define TYPE_MODULE 'm'
92
93#define TYPE_SYMBOL ':'
94#define TYPE_SYMLINK ';'
95
96#define TYPE_IVAR 'I'
97#define TYPE_LINK '@'
98
99static ID s_dump, s_load, s_mdump, s_mload;
100static ID s_dump_data, s_load_data, s_alloc, s_call;
101static ID s_getbyte, s_read, s_write, s_binmode;
102static ID s_encoding_short, s_ruby2_keywords_flag;
103
104#define name_s_dump "_dump"
105#define name_s_load "_load"
106#define name_s_mdump "marshal_dump"
107#define name_s_mload "marshal_load"
108#define name_s_dump_data "_dump_data"
109#define name_s_load_data "_load_data"
110#define name_s_alloc "_alloc"
111#define name_s_call "call"
112#define name_s_getbyte "getbyte"
113#define name_s_read "read"
114#define name_s_write "write"
115#define name_s_binmode "binmode"
116#define name_s_encoding_short "E"
117#define name_s_ruby2_keywords_flag "K"
118
119typedef struct {
120 VALUE newclass;
121 VALUE oldclass;
122 VALUE (*dumper)(VALUE);
123 VALUE (*loader)(VALUE, VALUE);
124} marshal_compat_t;
125
126static st_table *compat_allocator_tbl;
127static VALUE compat_allocator_tbl_wrapper;
128static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit);
129static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze);
130
131static int
132mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _)
133{
134 marshal_compat_t *p = (marshal_compat_t *)value;
135 rb_gc_mark(p->newclass);
136 rb_gc_mark(p->oldclass);
137 return ST_CONTINUE;
138}
139
140static void
141mark_marshal_compat_t(void *tbl)
142{
143 if (!tbl) return;
144 st_foreach(tbl, mark_marshal_compat_i, 0);
145}
146
147static st_table *compat_allocator_table(void);
148
149void
150rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
151{
152 marshal_compat_t *compat;
153 rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
154
155 if (!allocator) {
156 rb_raise(rb_eTypeError, "no allocator");
157 }
158
159 compat = ALLOC(marshal_compat_t);
160 compat->newclass = Qnil;
161 compat->oldclass = Qnil;
162 compat->newclass = newclass;
163 compat->oldclass = oldclass;
164 compat->dumper = dumper;
165 compat->loader = loader;
166
167 st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
168}
169
170struct dump_arg {
171 VALUE str, dest;
172 st_table *symbols;
173 st_table *data;
174 st_table *compat_tbl;
175 st_table *encodings;
176 unsigned long num_entries;
177};
178
179struct dump_call_arg {
180 VALUE obj;
181 struct dump_arg *arg;
182 int limit;
183};
184
185static VALUE
186check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
187{
188 if (!arg->symbols) {
189 rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
190 name);
191 }
192 return ret;
193}
194
195static VALUE
196check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
197 struct dump_arg *arg, const char *name)
198{
199 VALUE ret = rb_funcallv(obj, sym, argc, argv);
200 VALUE klass = CLASS_OF(obj);
201 if (CLASS_OF(ret) == klass) {
202 rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
203 klass, name);
204 }
205 return check_dump_arg(ret, arg, name);
206}
207
208#define dump_funcall(arg, obj, sym, argc, argv) \
209 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
210#define dump_check_funcall(arg, obj, sym, argc, argv) \
211 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
212
213static void clear_dump_arg(struct dump_arg *arg);
214
215static void
216mark_dump_arg(void *ptr)
217{
218 struct dump_arg *p = ptr;
219 if (!p->symbols)
220 return;
221 rb_mark_set(p->symbols);
222 rb_mark_set(p->data);
223 rb_mark_hash(p->compat_tbl);
224 rb_gc_mark(p->str);
225}
226
227static void
228free_dump_arg(void *ptr)
229{
230 clear_dump_arg(ptr);
231 xfree(ptr);
232}
233
234static size_t
235memsize_dump_arg(const void *ptr)
236{
237 return sizeof(struct dump_arg);
238}
239
240static const rb_data_type_t dump_arg_data = {
241 "dump_arg",
242 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
243 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
244};
245
246static VALUE
247must_not_be_anonymous(const char *type, VALUE path)
248{
249 char *n = RSTRING_PTR(path);
250
251 if (!rb_enc_asciicompat(rb_enc_get(path))) {
252 /* cannot occur? */
253 rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
254 type, path);
255 }
256 if (n[0] == '#') {
257 rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
258 type, path);
259 }
260 return path;
261}
262
263static VALUE
264class2path(VALUE klass)
265{
266 VALUE path = rb_class_path(klass);
267
268 must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path);
269 if (rb_path_to_class(path) != rb_class_real(klass)) {
270 rb_raise(rb_eTypeError, "% "PRIsVALUE" can't be referred to", path);
271 }
272 return path;
273}
274
275int ruby_marshal_write_long(long x, char *buf);
276static void w_long(long, struct dump_arg*);
277static int w_encoding(VALUE encname, struct dump_call_arg *arg);
278static VALUE encoding_name(VALUE obj, struct dump_arg *arg);
279
280static void
281w_nbyte(const char *s, long n, struct dump_arg *arg)
282{
283 VALUE buf = arg->str;
284 rb_str_buf_cat(buf, s, n);
285 if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
286 rb_io_write(arg->dest, buf);
287 rb_str_resize(buf, 0);
288 }
289}
290
291static void
292w_byte(char c, struct dump_arg *arg)
293{
294 w_nbyte(&c, 1, arg);
295}
296
297static void
298w_bytes(const char *s, long n, struct dump_arg *arg)
299{
300 w_long(n, arg);
301 w_nbyte(s, n, arg);
302}
303
304#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
305
306static void
307w_short(int x, struct dump_arg *arg)
308{
309 w_byte((char)((x >> 0) & 0xff), arg);
310 w_byte((char)((x >> 8) & 0xff), arg);
311}
312
313static void
314w_long(long x, struct dump_arg *arg)
315{
316 char buf[sizeof(long)+1];
317 int i = ruby_marshal_write_long(x, buf);
318 if (i < 0) {
319 rb_raise(rb_eTypeError, "long too big to dump");
320 }
321 w_nbyte(buf, i, arg);
322}
323
324int
325ruby_marshal_write_long(long x, char *buf)
326{
327 int i;
328
329#if SIZEOF_LONG > 4
330 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
331 /* big long does not fit in 4 bytes */
332 return -1;
333 }
334#endif
335
336 if (x == 0) {
337 buf[0] = 0;
338 return 1;
339 }
340 if (0 < x && x < 123) {
341 buf[0] = (char)(x + 5);
342 return 1;
343 }
344 if (-124 < x && x < 0) {
345 buf[0] = (char)((x - 5)&0xff);
346 return 1;
347 }
348 for (i=1;i<(int)sizeof(long)+1;i++) {
349 buf[i] = (char)(x & 0xff);
350 x = RSHIFT(x,8);
351 if (x == 0) {
352 buf[0] = i;
353 break;
354 }
355 if (x == -1) {
356 buf[0] = -i;
357 break;
358 }
359 }
360 return i+1;
361}
362
363#ifdef DBL_MANT_DIG
364#define DECIMAL_MANT (53-16) /* from IEEE754 double precision */
365
366#if DBL_MANT_DIG > 32
367#define MANT_BITS 32
368#elif DBL_MANT_DIG > 24
369#define MANT_BITS 24
370#elif DBL_MANT_DIG > 16
371#define MANT_BITS 16
372#else
373#define MANT_BITS 8
374#endif
375
376static double
377load_mantissa(double d, const char *buf, long len)
378{
379 if (!len) return d;
380 if (--len > 0 && !*buf++) { /* binary mantissa mark */
381 int e, s = d < 0, dig = 0;
382 unsigned long m;
383
384 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
385 do {
386 m = 0;
387 switch (len) {
388 default: m = *buf++ & 0xff; /* fall through */
389#if MANT_BITS > 24
390 case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
391#endif
392#if MANT_BITS > 16
393 case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
394#endif
395#if MANT_BITS > 8
396 case 1: m = (m << 8) | (*buf++ & 0xff);
397#endif
398 }
399 dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
400 d += ldexp((double)m, dig);
401 } while ((len -= MANT_BITS / 8) > 0);
402 d = ldexp(d, e - DECIMAL_MANT);
403 if (s) d = -d;
404 }
405 return d;
406}
407#else
408#define load_mantissa(d, buf, len) (d)
409#endif
410
411#ifdef DBL_DIG
412#define FLOAT_DIG (DBL_DIG+2)
413#else
414#define FLOAT_DIG 17
415#endif
416
417static void
418w_float(double d, struct dump_arg *arg)
419{
420 char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
421
422 if (isinf(d)) {
423 if (d < 0) w_cstr("-inf", arg);
424 else w_cstr("inf", arg);
425 }
426 else if (isnan(d)) {
427 w_cstr("nan", arg);
428 }
429 else if (d == 0.0) {
430 if (signbit(d)) w_cstr("-0", arg);
431 else w_cstr("0", arg);
432 }
433 else {
434 int decpt, sign, digs, len = 0;
435 char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
436 if (sign) buf[len++] = '-';
437 digs = (int)(e - p);
438 if (decpt < -3 || decpt > digs) {
439 buf[len++] = p[0];
440 if (--digs > 0) buf[len++] = '.';
441 memcpy(buf + len, p + 1, digs);
442 len += digs;
443 len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
444 }
445 else if (decpt > 0) {
446 memcpy(buf + len, p, decpt);
447 len += decpt;
448 if ((digs -= decpt) > 0) {
449 buf[len++] = '.';
450 memcpy(buf + len, p + decpt, digs);
451 len += digs;
452 }
453 }
454 else {
455 buf[len++] = '0';
456 buf[len++] = '.';
457 if (decpt) {
458 memset(buf + len, '0', -decpt);
459 len -= decpt;
460 }
461 memcpy(buf + len, p, digs);
462 len += digs;
463 }
464 xfree(p);
465 w_bytes(buf, len, arg);
466 }
467}
468
469static void
470w_symbol(VALUE sym, struct dump_arg *arg)
471{
472 st_data_t num;
473 VALUE encname;
474
475 if (st_lookup(arg->symbols, sym, &num)) {
476 w_byte(TYPE_SYMLINK, arg);
477 w_long((long)num, arg);
478 }
479 else {
480 const VALUE orig_sym = sym;
481 sym = rb_sym2str(sym);
482 if (!sym) {
483 rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
484 }
485 encname = encoding_name(sym, arg);
486 if (NIL_P(encname) ||
487 is_ascii_string(sym)) {
488 encname = Qnil;
489 }
490 else {
491 w_byte(TYPE_IVAR, arg);
492 }
493 w_byte(TYPE_SYMBOL, arg);
494 w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
495 st_add_direct(arg->symbols, orig_sym, arg->symbols->num_entries);
496 if (!NIL_P(encname)) {
497 struct dump_call_arg c_arg;
498 c_arg.limit = 1;
499 c_arg.arg = arg;
500 w_long(1L, arg);
501 w_encoding(encname, &c_arg);
502 }
503 }
504}
505
506static void
507w_unique(VALUE s, struct dump_arg *arg)
508{
509 must_not_be_anonymous("class", s);
510 w_symbol(rb_str_intern(s), arg);
511}
512
513static void w_object(VALUE,struct dump_arg*,int);
514
515static int
516hash_each(VALUE key, VALUE value, VALUE v)
517{
518 struct dump_call_arg *arg = (void *)v;
519 w_object(key, arg->arg, arg->limit);
520 w_object(value, arg->arg, arg->limit);
521 return ST_CONTINUE;
522}
523
524#define SINGLETON_DUMP_UNABLE_P(klass) \
525 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
526 rb_ivar_count(klass) > 1)
527
528static void
529w_extended(VALUE klass, struct dump_arg *arg, int check)
530{
531 if (check && FL_TEST(klass, FL_SINGLETON)) {
532 VALUE origin = RCLASS_ORIGIN(klass);
533 if (SINGLETON_DUMP_UNABLE_P(klass) ||
534 (origin != klass && SINGLETON_DUMP_UNABLE_P(origin))) {
535 rb_raise(rb_eTypeError, "singleton can't be dumped");
536 }
537 klass = RCLASS_SUPER(klass);
538 }
539 while (BUILTIN_TYPE(klass) == T_ICLASS) {
540 if (!FL_TEST(klass, RICLASS_IS_ORIGIN) ||
541 BUILTIN_TYPE(RBASIC(klass)->klass) != T_MODULE) {
542 VALUE path = rb_class_name(RBASIC(klass)->klass);
543 w_byte(TYPE_EXTENDED, arg);
544 w_unique(path, arg);
545 }
546 klass = RCLASS_SUPER(klass);
547 }
548}
549
550static void
551w_class(char type, VALUE obj, struct dump_arg *arg, int check)
552{
553 VALUE path;
554 st_data_t real_obj;
555 VALUE klass;
556
557 if (arg->compat_tbl &&
558 st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
559 obj = (VALUE)real_obj;
560 }
561 klass = CLASS_OF(obj);
562 w_extended(klass, arg, check);
563 w_byte(type, arg);
564 path = class2path(rb_class_real(klass));
565 w_unique(path, arg);
566}
567
568static void
569w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
570{
571 VALUE klass = CLASS_OF(obj);
572
573 w_extended(klass, arg, TRUE);
574 klass = rb_class_real(klass);
575 if (klass != super) {
576 w_byte(TYPE_UCLASS, arg);
577 w_unique(class2path(klass), arg);
578 }
579}
580
581static bool
582rb_hash_ruby2_keywords_p(VALUE obj)
583{
584 return (RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS) != 0;
585}
586
587static void
588rb_hash_ruby2_keywords(VALUE obj)
589{
590 RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
591}
592
593static inline bool
594to_be_skipped_id(const ID id)
595{
596 if (id == s_encoding_short) return true;
597 if (id == s_ruby2_keywords_flag) return true;
598 if (id == rb_id_encoding()) return true;
599 return !rb_id2str(id);
600}
601
602struct w_ivar_arg {
603 struct dump_call_arg *dump;
604 st_data_t num_ivar;
605};
606
607static int
608w_obj_each(st_data_t key, st_data_t val, st_data_t a)
609{
610 ID id = (ID)key;
611 VALUE value = (VALUE)val;
612 struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
613 struct dump_call_arg *arg = ivarg->dump;
614
615 if (to_be_skipped_id(id)) {
616 if (id == s_encoding_short) {
617 rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped",
618 CLASS_OF(arg->obj));
619 }
620 if (id == s_ruby2_keywords_flag) {
621 rb_warn("instance variable `"name_s_ruby2_keywords_flag"' on class %"PRIsVALUE" is not dumped",
622 CLASS_OF(arg->obj));
623 }
624 return ST_CONTINUE;
625 }
626 --ivarg->num_ivar;
627 w_symbol(ID2SYM(id), arg->arg);
628 w_object(value, arg->arg, arg->limit);
629 return ST_CONTINUE;
630}
631
632static int
633obj_count_ivars(st_data_t key, st_data_t val, st_data_t a)
634{
635 ID id = (ID)key;
636 if (!to_be_skipped_id(id) && UNLIKELY(!++*(st_index_t *)a)) {
637 rb_raise(rb_eRuntimeError, "too many instance variables");
638 }
639 return ST_CONTINUE;
640}
641
642static VALUE
643encoding_name(VALUE obj, struct dump_arg *arg)
644{
645 if (rb_enc_capable(obj)) {
646 int encidx = rb_enc_get_index(obj);
647 rb_encoding *enc = 0;
648 st_data_t name;
649
650 if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
651 return Qnil;
652 }
653
654 /* special treatment for US-ASCII and UTF-8 */
655 if (encidx == rb_usascii_encindex()) {
656 return Qfalse;
657 }
658 else if (encidx == rb_utf8_encindex()) {
659 return Qtrue;
660 }
661
662 if (arg->encodings ?
663 !st_lookup(arg->encodings, (st_data_t)rb_enc_name(enc), &name) :
664 (arg->encodings = st_init_strcasetable(), 1)) {
665 name = (st_data_t)rb_str_new_cstr(rb_enc_name(enc));
666 st_insert(arg->encodings, (st_data_t)rb_enc_name(enc), name);
667 }
668 return (VALUE)name;
669 }
670 else {
671 return Qnil;
672 }
673}
674
675static int
676w_encoding(VALUE encname, struct dump_call_arg *arg)
677{
678 int limit = arg->limit;
679 if (limit >= 0) ++limit;
680 switch (encname) {
681 case Qfalse:
682 case Qtrue:
683 w_symbol(ID2SYM(s_encoding_short), arg->arg);
684 w_object(encname, arg->arg, limit);
685 return 1;
686 case Qnil:
687 return 0;
688 }
689 w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
690 w_object(encname, arg->arg, limit);
691 return 1;
692}
693
694static st_index_t
695has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
696{
697 st_index_t num = !NIL_P(encname);
698
699 if (SPECIAL_CONST_P(obj)) goto generic;
700 switch (BUILTIN_TYPE(obj)) {
701 case T_OBJECT:
702 case T_CLASS:
703 case T_MODULE:
704 break; /* counted elsewhere */
705 case T_HASH:
706 if (rb_hash_ruby2_keywords_p(obj)) ++num;
707 /* fall through */
708 default:
709 generic:
710 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
711 if (num) *ivobj = obj;
712 }
713
714 return num;
715}
716
717static void
718w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
719{
720 shape_id_t shape_id = rb_shape_get_shape_id(arg->obj);
721 struct w_ivar_arg ivarg = {arg, num};
722 if (!num) return;
723 rb_ivar_foreach(obj, w_obj_each, (st_data_t)&ivarg);
724
725 if (shape_id != rb_shape_get_shape_id(arg->obj)) {
726 rb_shape_t * expected_shape = rb_shape_get_shape_by_id(shape_id);
727 rb_shape_t * actual_shape = rb_shape_get_shape(arg->obj);
728
729 // If the shape tree got _shorter_ then we probably removed an IV
730 // If the shape tree got longer, then we probably added an IV.
731 // The exception message might not be accurate when someone adds and
732 // removes the same number of IVs, but they will still get an exception
733 if (rb_shape_depth(expected_shape) > rb_shape_depth(actual_shape)) {
734 rb_raise(rb_eRuntimeError, "instance variable removed from %"PRIsVALUE" instance",
735 CLASS_OF(arg->obj));
736 }
737 else {
738 rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
739 CLASS_OF(arg->obj));
740 }
741 }
742}
743
744static void
745w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
746{
747 w_long(num, arg->arg);
748 num -= w_encoding(encname, arg);
749 if (RB_TYPE_P(ivobj, T_HASH) && rb_hash_ruby2_keywords_p(ivobj)) {
750 int limit = arg->limit;
751 if (limit >= 0) ++limit;
752 w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
753 w_object(Qtrue, arg->arg, limit);
754 num--;
755 }
756 if (!UNDEF_P(ivobj) && num) {
757 w_ivar_each(ivobj, num, arg);
758 }
759}
760
761static void
762w_objivar(VALUE obj, struct dump_call_arg *arg)
763{
764 st_data_t num = 0;
765
766 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
767 w_long(num, arg->arg);
768 w_ivar_each(obj, num, arg);
769}
770
771#if SIZEOF_LONG > 4
772// Optimized dump for fixnum larger than 31-bits
773static void
774w_bigfixnum(VALUE obj, struct dump_arg *arg)
775{
776 RUBY_ASSERT(FIXNUM_P(obj));
777
778 w_byte(TYPE_BIGNUM, arg);
779
780#if SIZEOF_LONG == SIZEOF_VALUE
781 long num, slen_num;
782 num = FIX2LONG(obj);
783#else
784 long long num, slen_num;
785 num = NUM2LL(obj);
786#endif
787
788 char sign = num < 0 ? '-' : '+';
789 w_byte(sign, arg);
790
791 // Guaranteed not to overflow, as FIXNUM is 1-bit less than long
792 if (num < 0) num = -num;
793
794 // calculate the size in shorts
795 int slen = 0;
796 {
797 slen_num = num;
798 while (slen_num) {
799 slen++;
800 slen_num = SHORTDN(slen_num);
801 }
802 }
803
804 RUBY_ASSERT(slen > 0 && slen <= SIZEOF_LONG / 2);
805
806 w_long((long)slen, arg);
807
808 for (int i = 0; i < slen; i++) {
809 w_short(num & SHORTMASK, arg);
810 num = SHORTDN(num);
811 }
812
813 // We aren't adding this object to the link table, but we need to increment
814 // the index.
815 arg->num_entries++;
816
817 RUBY_ASSERT(num == 0);
818}
819#endif
820
821static void
822w_remember(VALUE obj, struct dump_arg *arg)
823{
824 st_add_direct(arg->data, obj, arg->num_entries++);
825}
826
827static void
828w_object(VALUE obj, struct dump_arg *arg, int limit)
829{
830 struct dump_call_arg c_arg;
831 VALUE ivobj = Qundef;
832 st_data_t num;
833 st_index_t hasiv = 0;
834 VALUE encname = Qnil;
835
836 if (limit == 0) {
837 rb_raise(rb_eArgError, "exceed depth limit");
838 }
839
840 if (NIL_P(obj)) {
841 w_byte(TYPE_NIL, arg);
842 }
843 else if (obj == Qtrue) {
844 w_byte(TYPE_TRUE, arg);
845 }
846 else if (obj == Qfalse) {
847 w_byte(TYPE_FALSE, arg);
848 }
849 else if (FIXNUM_P(obj)) {
850#if SIZEOF_LONG <= 4
851 w_byte(TYPE_FIXNUM, arg);
852 w_long(FIX2INT(obj), arg);
853#else
854 if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
855 w_byte(TYPE_FIXNUM, arg);
856 w_long(FIX2LONG(obj), arg);
857 }
858 else {
859 w_bigfixnum(obj, arg);
860 }
861#endif
862 }
863 else if (SYMBOL_P(obj)) {
864 w_symbol(obj, arg);
865 }
866 else {
867 if (st_lookup(arg->data, obj, &num)) {
868 w_byte(TYPE_LINK, arg);
869 w_long((long)num, arg);
870 return;
871 }
872
873 if (limit > 0) limit--;
874 c_arg.limit = limit;
875 c_arg.arg = arg;
876 c_arg.obj = obj;
877
878 if (FLONUM_P(obj)) {
879 w_remember(obj, arg);
880 w_byte(TYPE_FLOAT, arg);
881 w_float(RFLOAT_VALUE(obj), arg);
882 return;
883 }
884
885 VALUE v;
886
887 if (!RBASIC_CLASS(obj)) {
888 rb_raise(rb_eTypeError, "can't dump internal %s",
889 rb_builtin_type_name(BUILTIN_TYPE(obj)));
890 }
891
892 if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
893 w_remember(obj, arg);
894
895 v = dump_funcall(arg, obj, s_mdump, 0, 0);
896 w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
897 w_object(v, arg, limit);
898 return;
899 }
900 if (rb_obj_respond_to(obj, s_dump, TRUE)) {
901 VALUE ivobj2 = Qundef;
902 st_index_t hasiv2;
903 VALUE encname2;
904
905 v = INT2NUM(limit);
906 v = dump_funcall(arg, obj, s_dump, 1, &v);
907 if (!RB_TYPE_P(v, T_STRING)) {
908 rb_raise(rb_eTypeError, "_dump() must return string");
909 }
910 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
911 hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
912 if (hasiv2) {
913 hasiv = hasiv2;
914 ivobj = ivobj2;
915 encname = encname2;
916 }
917 if (hasiv) w_byte(TYPE_IVAR, arg);
918 w_class(TYPE_USERDEF, obj, arg, FALSE);
919 w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
920 if (hasiv) {
921 w_ivar(hasiv, ivobj, encname, &c_arg);
922 }
923 w_remember(obj, arg);
924 return;
925 }
926
927 w_remember(obj, arg);
928
929 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
930 {
931 st_data_t compat_data;
932 rb_alloc_func_t allocator = rb_get_alloc_func(RBASIC(obj)->klass);
933 if (st_lookup(compat_allocator_tbl,
934 (st_data_t)allocator,
935 &compat_data)) {
936 marshal_compat_t *compat = (marshal_compat_t*)compat_data;
937 VALUE real_obj = obj;
938 obj = compat->dumper(real_obj);
939 if (!arg->compat_tbl) {
940 arg->compat_tbl = rb_init_identtable();
941 }
942 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
943 if (obj != real_obj && UNDEF_P(ivobj)) hasiv = 0;
944 }
945 }
946 if (hasiv) w_byte(TYPE_IVAR, arg);
947
948 switch (BUILTIN_TYPE(obj)) {
949 case T_CLASS:
950 if (FL_TEST(obj, FL_SINGLETON)) {
951 rb_raise(rb_eTypeError, "singleton class can't be dumped");
952 }
953 w_byte(TYPE_CLASS, arg);
954 {
955 VALUE path = class2path(obj);
956 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
957 RB_GC_GUARD(path);
958 }
959 break;
960
961 case T_MODULE:
962 w_byte(TYPE_MODULE, arg);
963 {
964 VALUE path = class2path(obj);
965 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
966 RB_GC_GUARD(path);
967 }
968 break;
969
970 case T_FLOAT:
971 w_byte(TYPE_FLOAT, arg);
972 w_float(RFLOAT_VALUE(obj), arg);
973 break;
974
975 case T_BIGNUM:
976 w_byte(TYPE_BIGNUM, arg);
977 {
978 char sign = BIGNUM_SIGN(obj) ? '+' : '-';
979 size_t len = BIGNUM_LEN(obj);
980 size_t slen;
981 size_t j;
982 BDIGIT *d = BIGNUM_DIGITS(obj);
983
984 slen = SHORTLEN(len);
985 if (LONG_MAX < slen) {
986 rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
987 }
988
989 w_byte(sign, arg);
990 w_long((long)slen, arg);
991 for (j = 0; j < len; j++) {
992#if SIZEOF_BDIGIT > SIZEOF_SHORT
993 BDIGIT num = *d;
994 int i;
995
996 for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
997 w_short(num & SHORTMASK, arg);
998 num = SHORTDN(num);
999 if (j == len - 1 && num == 0) break;
1000 }
1001#else
1002 w_short(*d, arg);
1003#endif
1004 d++;
1005 }
1006 }
1007 break;
1008
1009 case T_STRING:
1010 w_uclass(obj, rb_cString, arg);
1011 w_byte(TYPE_STRING, arg);
1012 w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
1013 break;
1014
1015 case T_REGEXP:
1016 w_uclass(obj, rb_cRegexp, arg);
1017 w_byte(TYPE_REGEXP, arg);
1018 {
1019 int opts = rb_reg_options(obj);
1020 w_bytes(RREGEXP_SRC_PTR(obj), RREGEXP_SRC_LEN(obj), arg);
1021 w_byte((char)opts, arg);
1022 }
1023 break;
1024
1025 case T_ARRAY:
1026 w_uclass(obj, rb_cArray, arg);
1027 w_byte(TYPE_ARRAY, arg);
1028 {
1029 long i, len = RARRAY_LEN(obj);
1030
1031 w_long(len, arg);
1032 for (i=0; i<RARRAY_LEN(obj); i++) {
1033 w_object(RARRAY_AREF(obj, i), arg, limit);
1034 if (len != RARRAY_LEN(obj)) {
1035 rb_raise(rb_eRuntimeError, "array modified during dump");
1036 }
1037 }
1038 }
1039 break;
1040
1041 case T_HASH:
1042 w_uclass(obj, rb_cHash, arg);
1043 if (rb_hash_compare_by_id_p(obj)) {
1044 w_byte(TYPE_UCLASS, arg);
1045 w_symbol(rb_sym_intern_ascii_cstr("Hash"), arg);
1046 }
1047 if (NIL_P(RHASH_IFNONE(obj))) {
1048 w_byte(TYPE_HASH, arg);
1049 }
1050 else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
1051 rb_raise(rb_eTypeError, "can't dump hash with default proc");
1052 }
1053 else {
1054 w_byte(TYPE_HASH_DEF, arg);
1055 }
1056 w_long(rb_hash_size_num(obj), arg);
1057 rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
1058 if (!NIL_P(RHASH_IFNONE(obj))) {
1059 w_object(RHASH_IFNONE(obj), arg, limit);
1060 }
1061 break;
1062
1063 case T_STRUCT:
1064 w_class(TYPE_STRUCT, obj, arg, TRUE);
1065 {
1066 long len = RSTRUCT_LEN(obj);
1067 VALUE mem;
1068 long i;
1069
1070 w_long(len, arg);
1071 mem = rb_struct_members(obj);
1072 for (i=0; i<len; i++) {
1073 w_symbol(RARRAY_AREF(mem, i), arg);
1074 w_object(RSTRUCT_GET(obj, i), arg, limit);
1075 }
1076 }
1077 break;
1078
1079 case T_OBJECT:
1080 w_class(TYPE_OBJECT, obj, arg, TRUE);
1081 w_objivar(obj, &c_arg);
1082 break;
1083
1084 case T_DATA:
1085 {
1086 VALUE v;
1087
1088 if (!rb_obj_respond_to(obj, s_dump_data, TRUE)) {
1089 rb_raise(rb_eTypeError,
1090 "no _dump_data is defined for class %"PRIsVALUE,
1091 rb_obj_class(obj));
1092 }
1093 v = dump_funcall(arg, obj, s_dump_data, 0, 0);
1094 w_class(TYPE_DATA, obj, arg, TRUE);
1095 w_object(v, arg, limit);
1096 }
1097 break;
1098
1099 default:
1100 rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
1101 rb_obj_class(obj));
1102 break;
1103 }
1104 RB_GC_GUARD(obj);
1105 }
1106 if (hasiv) {
1107 w_ivar(hasiv, ivobj, encname, &c_arg);
1108 }
1109}
1110
1111static void
1112clear_dump_arg(struct dump_arg *arg)
1113{
1114 if (!arg->symbols) return;
1115 st_free_table(arg->symbols);
1116 arg->symbols = 0;
1117 st_free_table(arg->data);
1118 arg->data = 0;
1119 arg->num_entries = 0;
1120 if (arg->compat_tbl) {
1121 st_free_table(arg->compat_tbl);
1122 arg->compat_tbl = 0;
1123 }
1124 if (arg->encodings) {
1125 st_free_table(arg->encodings);
1126 arg->encodings = 0;
1127 }
1128}
1129
1130NORETURN(static inline void io_needed(void));
1131static inline void
1132io_needed(void)
1133{
1134 rb_raise(rb_eTypeError, "instance of IO needed");
1135}
1136
1137/*
1138 * call-seq:
1139 * dump( obj [, anIO] , limit=-1 ) -> anIO
1140 *
1141 * Serializes obj and all descendant objects. If anIO is
1142 * specified, the serialized data will be written to it, otherwise the
1143 * data will be returned as a String. If limit is specified, the
1144 * traversal of subobjects will be limited to that depth. If limit is
1145 * negative, no checking of depth will be performed.
1146 *
1147 * class Klass
1148 * def initialize(str)
1149 * @str = str
1150 * end
1151 * def say_hello
1152 * @str
1153 * end
1154 * end
1155 *
1156 * (produces no output)
1157 *
1158 * o = Klass.new("hello\n")
1159 * data = Marshal.dump(o)
1160 * obj = Marshal.load(data)
1161 * obj.say_hello #=> "hello\n"
1162 *
1163 * Marshal can't dump following objects:
1164 * * anonymous Class/Module.
1165 * * objects which are related to system (ex: Dir, File::Stat, IO, File, Socket
1166 * and so on)
1167 * * an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread,
1168 * ThreadGroup, Continuation
1169 * * objects which define singleton methods
1170 */
1171static VALUE
1172marshal_dump(int argc, VALUE *argv, VALUE _)
1173{
1174 VALUE obj, port, a1, a2;
1175 int limit = -1;
1176
1177 port = Qnil;
1178 rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
1179 if (argc == 3) {
1180 if (!NIL_P(a2)) limit = NUM2INT(a2);
1181 if (NIL_P(a1)) io_needed();
1182 port = a1;
1183 }
1184 else if (argc == 2) {
1185 if (FIXNUM_P(a1)) limit = FIX2INT(a1);
1186 else if (NIL_P(a1)) io_needed();
1187 else port = a1;
1188 }
1189 return rb_marshal_dump_limited(obj, port, limit);
1190}
1191
1192VALUE
1193rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
1194{
1195 struct dump_arg *arg;
1196 VALUE wrapper; /* used to avoid memory leak in case of exception */
1197
1198 wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
1199 arg->dest = 0;
1200 arg->symbols = st_init_numtable();
1201 arg->data = rb_init_identtable();
1202 arg->num_entries = 0;
1203 arg->compat_tbl = 0;
1204 arg->encodings = 0;
1205 arg->str = rb_str_buf_new(0);
1206 if (!NIL_P(port)) {
1207 if (!rb_respond_to(port, s_write)) {
1208 io_needed();
1209 }
1210 arg->dest = port;
1211 dump_check_funcall(arg, port, s_binmode, 0, 0);
1212 }
1213 else {
1214 port = arg->str;
1215 }
1216
1217 w_byte(MARSHAL_MAJOR, arg);
1218 w_byte(MARSHAL_MINOR, arg);
1219
1220 w_object(obj, arg, limit);
1221 if (arg->dest) {
1222 rb_io_write(arg->dest, arg->str);
1223 rb_str_resize(arg->str, 0);
1224 }
1225 clear_dump_arg(arg);
1226 RB_GC_GUARD(wrapper);
1227
1228 return port;
1229}
1230
1231struct load_arg {
1232 VALUE src;
1233 char *buf;
1234 long buflen;
1235 long readable;
1236 long offset;
1237 st_table *symbols;
1238 st_table *data;
1239 st_table *partial_objects;
1240 VALUE proc;
1241 st_table *compat_tbl;
1242 bool freeze;
1243};
1244
1245static VALUE
1246check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
1247{
1248 if (!arg->symbols) {
1249 rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
1250 name);
1251 }
1252 return ret;
1253}
1254#define load_funcall(arg, obj, sym, argc, argv) \
1255 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1256
1257static void clear_load_arg(struct load_arg *arg);
1258
1259static void
1260mark_load_arg(void *ptr)
1261{
1262 struct load_arg *p = ptr;
1263 if (!p->symbols)
1264 return;
1265 rb_mark_tbl(p->symbols);
1266 rb_mark_tbl(p->data);
1267 rb_mark_tbl(p->partial_objects);
1268 rb_mark_hash(p->compat_tbl);
1269}
1270
1271static void
1272free_load_arg(void *ptr)
1273{
1274 clear_load_arg(ptr);
1275 xfree(ptr);
1276}
1277
1278static size_t
1279memsize_load_arg(const void *ptr)
1280{
1281 return sizeof(struct load_arg);
1282}
1283
1284static const rb_data_type_t load_arg_data = {
1285 "load_arg",
1286 {mark_load_arg, free_load_arg, memsize_load_arg,},
1287 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1288};
1289
1290#define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1291static VALUE r_object(struct load_arg *arg);
1292static VALUE r_symbol(struct load_arg *arg);
1293
1294NORETURN(static void too_short(void));
1295static void
1296too_short(void)
1297{
1298 rb_raise(rb_eArgError, "marshal data too short");
1299}
1300
1301static st_index_t
1302r_prepare(struct load_arg *arg)
1303{
1304 st_index_t idx = arg->data->num_entries;
1305
1306 st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
1307 return idx;
1308}
1309
1310static unsigned char
1311r_byte1_buffered(struct load_arg *arg)
1312{
1313 if (arg->buflen == 0) {
1314 long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ;
1315 VALUE str, n = LONG2NUM(readable);
1316
1317 str = load_funcall(arg, arg->src, s_read, 1, &n);
1318 if (NIL_P(str)) too_short();
1319 StringValue(str);
1320 memcpy(arg->buf, RSTRING_PTR(str), RSTRING_LEN(str));
1321 arg->offset = 0;
1322 arg->buflen = RSTRING_LEN(str);
1323 }
1324 arg->buflen--;
1325 return arg->buf[arg->offset++];
1326}
1327
1328static int
1329r_byte(struct load_arg *arg)
1330{
1331 int c;
1332
1333 if (RB_TYPE_P(arg->src, T_STRING)) {
1334 if (RSTRING_LEN(arg->src) > arg->offset) {
1335 c = (unsigned char)RSTRING_PTR(arg->src)[arg->offset++];
1336 }
1337 else {
1338 too_short();
1339 }
1340 }
1341 else {
1342 if (arg->readable >0 || arg->buflen > 0) {
1343 c = r_byte1_buffered(arg);
1344 }
1345 else {
1346 VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0);
1347 if (NIL_P(v)) rb_eof_error();
1348 c = (unsigned char)NUM2CHR(v);
1349 }
1350 }
1351 return c;
1352}
1353
1354NORETURN(static void long_toobig(int size));
1355
1356static void
1357long_toobig(int size)
1358{
1359 rb_raise(rb_eTypeError, "long too big for this architecture (size "
1360 STRINGIZE(SIZEOF_LONG)", given %d)", size);
1361}
1362
1363static long
1364r_long(struct load_arg *arg)
1365{
1366 register long x;
1367 int c = (signed char)r_byte(arg);
1368 long i;
1369
1370 if (c == 0) return 0;
1371 if (c > 0) {
1372 if (4 < c && c < 128) {
1373 return c - 5;
1374 }
1375 if (c > (int)sizeof(long)) long_toobig(c);
1376 x = 0;
1377 for (i=0;i<c;i++) {
1378 x |= (long)r_byte(arg) << (8*i);
1379 }
1380 }
1381 else {
1382 if (-129 < c && c < -4) {
1383 return c + 5;
1384 }
1385 c = -c;
1386 if (c > (int)sizeof(long)) long_toobig(c);
1387 x = -1;
1388 for (i=0;i<c;i++) {
1389 x &= ~((long)0xff << (8*i));
1390 x |= (long)r_byte(arg) << (8*i);
1391 }
1392 }
1393 return x;
1394}
1395
1396long
1397ruby_marshal_read_long(const char **buf, long len)
1398{
1399 long x;
1400 struct RString src;
1401 struct load_arg arg;
1402 memset(&arg, 0, sizeof(arg));
1403 arg.src = rb_setup_fake_str(&src, *buf, len, 0);
1404 x = r_long(&arg);
1405 *buf += arg.offset;
1406 return x;
1407}
1408
1409static VALUE
1410r_bytes1(long len, struct load_arg *arg)
1411{
1412 VALUE str, n = LONG2NUM(len);
1413
1414 str = load_funcall(arg, arg->src, s_read, 1, &n);
1415 if (NIL_P(str)) too_short();
1416 StringValue(str);
1417 if (RSTRING_LEN(str) != len) too_short();
1418
1419 return str;
1420}
1421
1422static VALUE
1423r_bytes1_buffered(long len, struct load_arg *arg)
1424{
1425 VALUE str;
1426
1427 if (len <= arg->buflen) {
1428 str = rb_str_new(arg->buf+arg->offset, len);
1429 arg->offset += len;
1430 arg->buflen -= len;
1431 }
1432 else {
1433 long buflen = arg->buflen;
1434 long readable = arg->readable + 1;
1435 long tmp_len, read_len, need_len = len - buflen;
1436 VALUE tmp, n;
1437
1438 readable = readable < BUFSIZ ? readable : BUFSIZ;
1439 read_len = need_len > readable ? need_len : readable;
1440 n = LONG2NUM(read_len);
1441 tmp = load_funcall(arg, arg->src, s_read, 1, &n);
1442 if (NIL_P(tmp)) too_short();
1443 StringValue(tmp);
1444
1445 tmp_len = RSTRING_LEN(tmp);
1446
1447 if (tmp_len < need_len) too_short();
1448
1449 str = rb_str_new(arg->buf+arg->offset, buflen);
1450 rb_str_cat(str, RSTRING_PTR(tmp), need_len);
1451
1452 if (tmp_len > need_len) {
1453 buflen = tmp_len - need_len;
1454 memcpy(arg->buf, RSTRING_PTR(tmp)+need_len, buflen);
1455 arg->buflen = buflen;
1456 }
1457 else {
1458 arg->buflen = 0;
1459 }
1460 arg->offset = 0;
1461 }
1462
1463 return str;
1464}
1465
1466#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1467
1468static VALUE
1469r_bytes0(long len, struct load_arg *arg)
1470{
1471 VALUE str;
1472
1473 if (len == 0) return rb_str_new(0, 0);
1474 if (RB_TYPE_P(arg->src, T_STRING)) {
1475 if (RSTRING_LEN(arg->src) - arg->offset >= len) {
1476 str = rb_str_new(RSTRING_PTR(arg->src)+arg->offset, len);
1477 arg->offset += len;
1478 }
1479 else {
1480 too_short();
1481 }
1482 }
1483 else {
1484 if (arg->readable > 0 || arg->buflen > 0) {
1485 str = r_bytes1_buffered(len, arg);
1486 }
1487 else {
1488 str = r_bytes1(len, arg);
1489 }
1490 }
1491 return str;
1492}
1493
1494static inline int
1495name_equal(const char *name, size_t nlen, const char *p, long l)
1496{
1497 if ((size_t)l != nlen || *p != *name) return 0;
1498 return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
1499}
1500
1501static int
1502sym2encidx(VALUE sym, VALUE val)
1503{
1504 static const char name_encoding[8] = "encoding";
1505 const char *p;
1506 long l;
1507 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
1508 RSTRING_GETMEM(sym, p, l);
1509 if (l <= 0) return -1;
1510 if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
1511 int idx = rb_enc_find_index(StringValueCStr(val));
1512 return idx;
1513 }
1514 if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
1515 if (val == Qfalse) return rb_usascii_encindex();
1516 else if (val == Qtrue) return rb_utf8_encindex();
1517 /* bogus ignore */
1518 }
1519 return -1;
1520}
1521
1522static int
1523symname_equal(VALUE sym, const char *name, size_t nlen)
1524{
1525 const char *p;
1526 long l;
1527 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return 0;
1528 RSTRING_GETMEM(sym, p, l);
1529 return name_equal(name, nlen, p, l);
1530}
1531
1532#define BUILD_ASSERT_POSITIVE(n) \
1533 /* make 0 negative to workaround the "zero size array" GCC extension, */ \
1534 ((sizeof(char [2*(ssize_t)(n)-1])+1)/2) /* assuming no overflow */
1535#define symname_equal_lit(sym, sym_name) \
1536 symname_equal(sym, sym_name, BUILD_ASSERT_POSITIVE(rb_strlen_lit(sym_name)))
1537
1538static VALUE
1539r_symlink(struct load_arg *arg)
1540{
1541 st_data_t sym;
1542 long num = r_long(arg);
1543
1544 if (!st_lookup(arg->symbols, num, &sym)) {
1545 rb_raise(rb_eArgError, "bad symbol");
1546 }
1547 return (VALUE)sym;
1548}
1549
1550static VALUE
1551r_symreal(struct load_arg *arg, int ivar)
1552{
1553 VALUE s = r_bytes(arg);
1554 VALUE sym;
1555 int idx = -1;
1556 st_index_t n = arg->symbols->num_entries;
1557
1558 if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
1559 st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
1560 if (ivar) {
1561 long num = r_long(arg);
1562 while (num-- > 0) {
1563 sym = r_symbol(arg);
1564 idx = sym2encidx(sym, r_object(arg));
1565 }
1566 }
1567 if (idx > 0) {
1568 rb_enc_associate_index(s, idx);
1569 if (is_broken_string(s)) {
1570 rb_raise(rb_eArgError, "invalid byte sequence in %s: %+"PRIsVALUE,
1571 rb_enc_name(rb_enc_from_index(idx)), s);
1572 }
1573 }
1574
1575 return s;
1576}
1577
1578static VALUE
1579r_symbol(struct load_arg *arg)
1580{
1581 int type, ivar = 0;
1582
1583 again:
1584 switch ((type = r_byte(arg))) {
1585 default:
1586 rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
1587 case TYPE_IVAR:
1588 ivar = 1;
1589 goto again;
1590 case TYPE_SYMBOL:
1591 return r_symreal(arg, ivar);
1592 case TYPE_SYMLINK:
1593 if (ivar) {
1594 rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
1595 }
1596 return r_symlink(arg);
1597 }
1598}
1599
1600static VALUE
1601r_unique(struct load_arg *arg)
1602{
1603 return r_symbol(arg);
1604}
1605
1606static VALUE
1607r_string(struct load_arg *arg)
1608{
1609 return r_bytes(arg);
1610}
1611
1612static VALUE
1613r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
1614{
1615 st_data_t real_obj = (st_data_t)v;
1616 if (arg->compat_tbl) {
1617 /* real_obj is kept if not found */
1618 st_lookup(arg->compat_tbl, v, &real_obj);
1619 }
1620 st_insert(arg->data, num, real_obj);
1621 st_insert(arg->partial_objects, (st_data_t)real_obj, Qtrue);
1622 return v;
1623}
1624
1625static VALUE
1626r_fixup_compat(VALUE v, struct load_arg *arg)
1627{
1628 st_data_t data;
1629 st_data_t key = (st_data_t)v;
1630 if (arg->compat_tbl && st_delete(arg->compat_tbl, &key, &data)) {
1631 VALUE real_obj = (VALUE)data;
1632 rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj));
1633 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1634 marshal_compat_t *compat = (marshal_compat_t*)data;
1635 compat->loader(real_obj, v);
1636 }
1637 v = real_obj;
1638 }
1639 return v;
1640}
1641
1642static VALUE
1643r_post_proc(VALUE v, struct load_arg *arg)
1644{
1645 if (arg->proc) {
1646 v = load_funcall(arg, arg->proc, s_call, 1, &v);
1647 }
1648 return v;
1649}
1650
1651static VALUE
1652r_leave(VALUE v, struct load_arg *arg, bool partial)
1653{
1654 v = r_fixup_compat(v, arg);
1655 if (!partial) {
1656 st_data_t data;
1657 st_data_t key = (st_data_t)v;
1658 st_delete(arg->partial_objects, &key, &data);
1659 if (arg->freeze) {
1660 if (RB_TYPE_P(v, T_MODULE) || RB_TYPE_P(v, T_CLASS)) {
1661 // noop
1662 }
1663 else if (RB_TYPE_P(v, T_STRING)) {
1664 v = rb_str_to_interned_str(v);
1665 }
1666 else {
1667 OBJ_FREEZE(v);
1668 }
1669 }
1670 v = r_post_proc(v, arg);
1671 }
1672 return v;
1673}
1674
1675static int
1676copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
1677{
1678 VALUE obj = (VALUE)arg, value = (VALUE)val;
1679 ID vid = (ID)key;
1680
1681 if (!rb_ivar_defined(obj, vid))
1682 rb_ivar_set(obj, vid, value);
1683 return ST_CONTINUE;
1684}
1685
1686static VALUE
1687r_copy_ivar(VALUE v, VALUE data)
1688{
1689 rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
1690 return v;
1691}
1692
1693static void
1694r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
1695{
1696 long len;
1697
1698 len = r_long(arg);
1699 if (len > 0) {
1700 do {
1701 VALUE sym = r_symbol(arg);
1702 VALUE val = r_object(arg);
1703 int idx = sym2encidx(sym, val);
1704 if (idx >= 0) {
1705 if (rb_enc_capable(obj)) {
1706 rb_enc_associate_index(obj, idx);
1707 }
1708 else {
1709 rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
1710 }
1711 if (has_encoding) *has_encoding = TRUE;
1712 }
1713 else if (symname_equal_lit(sym, name_s_ruby2_keywords_flag)) {
1714 if (RB_TYPE_P(obj, T_HASH)) {
1715 rb_hash_ruby2_keywords(obj);
1716 }
1717 else {
1718 rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
1719 }
1720 }
1721 else {
1722 rb_ivar_set(obj, rb_intern_str(sym), val);
1723 }
1724 } while (--len > 0);
1725 }
1726}
1727
1728static VALUE
1729path2class(VALUE path)
1730{
1731 VALUE v = rb_path_to_class(path);
1732
1733 if (!RB_TYPE_P(v, T_CLASS)) {
1734 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
1735 }
1736 return v;
1737}
1738
1739#define path2module(path) must_be_module(rb_path_to_class(path), path)
1740
1741static VALUE
1742must_be_module(VALUE v, VALUE path)
1743{
1744 if (!RB_TYPE_P(v, T_MODULE)) {
1745 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
1746 }
1747 return v;
1748}
1749
1750static VALUE
1751obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
1752{
1753 st_data_t data;
1754 rb_alloc_func_t allocator;
1755
1756 allocator = rb_get_alloc_func(klass);
1757 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1758 marshal_compat_t *compat = (marshal_compat_t*)data;
1759 VALUE real_obj = rb_obj_alloc(klass);
1760 VALUE obj = rb_obj_alloc(compat->oldclass);
1761 if (oldclass) *oldclass = compat->oldclass;
1762
1763 if (!arg->compat_tbl) {
1764 arg->compat_tbl = rb_init_identtable();
1765 }
1766 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
1767 return obj;
1768 }
1769
1770 return rb_obj_alloc(klass);
1771}
1772
1773static VALUE
1774obj_alloc_by_path(VALUE path, struct load_arg *arg)
1775{
1776 return obj_alloc_by_klass(path2class(path), arg, 0);
1777}
1778
1779static VALUE
1780append_extmod(VALUE obj, VALUE extmod)
1781{
1782 long i = RARRAY_LEN(extmod);
1783 while (i > 0) {
1784 VALUE m = RARRAY_AREF(extmod, --i);
1785 rb_extend_object(obj, m);
1786 }
1787 return obj;
1788}
1789
1790#define prohibit_ivar(type, str) do { \
1791 if (!ivp || !*ivp) break; \
1792 rb_raise(rb_eTypeError, \
1793 "can't override instance variable of "type" `%"PRIsVALUE"'", \
1794 (str)); \
1795 } while (0)
1796
1797static VALUE r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type);
1798
1799static VALUE
1800r_object0(struct load_arg *arg, bool partial, int *ivp, VALUE extmod)
1801{
1802 int type = r_byte(arg);
1803 return r_object_for(arg, partial, ivp, extmod, type);
1804}
1805
1806static VALUE
1807r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type)
1808{
1809 VALUE (*hash_new_with_size)(st_index_t) = rb_hash_new_with_size;
1810 VALUE v = Qnil;
1811 long id;
1812 st_data_t link;
1813
1814 switch (type) {
1815 case TYPE_LINK:
1816 id = r_long(arg);
1817 if (!st_lookup(arg->data, (st_data_t)id, &link)) {
1818 rb_raise(rb_eArgError, "dump format error (unlinked)");
1819 }
1820 v = (VALUE)link;
1821 if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
1822 v = r_post_proc(v, arg);
1823 }
1824 break;
1825
1826 case TYPE_IVAR:
1827 {
1828 int ivar = TRUE;
1829
1830 v = r_object0(arg, true, &ivar, extmod);
1831 if (ivar) r_ivar(v, NULL, arg);
1832 v = r_leave(v, arg, partial);
1833 }
1834 break;
1835
1836 case TYPE_EXTENDED:
1837 {
1838 VALUE path = r_unique(arg);
1839 VALUE m = rb_path_to_class(path);
1840 if (NIL_P(extmod)) extmod = rb_ary_hidden_new(0);
1841
1842 if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
1843 VALUE c;
1844
1845 v = r_object0(arg, true, 0, Qnil);
1846 c = CLASS_OF(v);
1847 if (c != m || FL_TEST(c, FL_SINGLETON)) {
1848 rb_raise(rb_eArgError,
1849 "prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
1850 path, rb_class_name(c));
1851 }
1852 c = rb_singleton_class(v);
1853 while (RARRAY_LEN(extmod) > 0) {
1854 m = rb_ary_pop(extmod);
1855 rb_prepend_module(c, m);
1856 }
1857 }
1858 else {
1859 must_be_module(m, path);
1860 rb_ary_push(extmod, m);
1861
1862 v = r_object0(arg, true, 0, extmod);
1863 while (RARRAY_LEN(extmod) > 0) {
1864 m = rb_ary_pop(extmod);
1865 rb_extend_object(v, m);
1866 }
1867 }
1868 }
1869 break;
1870
1871 case TYPE_UCLASS:
1872 {
1873 VALUE c = path2class(r_unique(arg));
1874
1875 if (FL_TEST(c, FL_SINGLETON)) {
1876 rb_raise(rb_eTypeError, "singleton can't be loaded");
1877 }
1878 type = r_byte(arg);
1879 if ((c == rb_cHash) &&
1880 /* Hack for compare_by_identify */
1881 (type == TYPE_HASH || type == TYPE_HASH_DEF)) {
1882 hash_new_with_size = rb_ident_hash_new_with_size;
1883 goto type_hash;
1884 }
1885 v = r_object_for(arg, partial, 0, extmod, type);
1886 if (rb_special_const_p(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
1887 goto format_error;
1888 }
1889 if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
1890 VALUE tmp = rb_obj_alloc(c);
1891
1892 if (TYPE(v) != TYPE(tmp)) goto format_error;
1893 }
1894 RBASIC_SET_CLASS(v, c);
1895 }
1896 break;
1897
1898 format_error:
1899 rb_raise(rb_eArgError, "dump format error (user class)");
1900
1901 case TYPE_NIL:
1902 v = Qnil;
1903 v = r_leave(v, arg, false);
1904 break;
1905
1906 case TYPE_TRUE:
1907 v = Qtrue;
1908 v = r_leave(v, arg, false);
1909 break;
1910
1911 case TYPE_FALSE:
1912 v = Qfalse;
1913 v = r_leave(v, arg, false);
1914 break;
1915
1916 case TYPE_FIXNUM:
1917 {
1918 long i = r_long(arg);
1919 v = LONG2FIX(i);
1920 }
1921 v = r_leave(v, arg, false);
1922 break;
1923
1924 case TYPE_FLOAT:
1925 {
1926 double d;
1927 VALUE str = r_bytes(arg);
1928 const char *ptr = RSTRING_PTR(str);
1929
1930 if (strcmp(ptr, "nan") == 0) {
1931 d = nan("");
1932 }
1933 else if (strcmp(ptr, "inf") == 0) {
1934 d = HUGE_VAL;
1935 }
1936 else if (strcmp(ptr, "-inf") == 0) {
1937 d = -HUGE_VAL;
1938 }
1939 else {
1940 char *e;
1941 d = strtod(ptr, &e);
1942 d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
1943 }
1944 v = DBL2NUM(d);
1945 v = r_entry(v, arg);
1946 v = r_leave(v, arg, false);
1947 }
1948 break;
1949
1950 case TYPE_BIGNUM:
1951 {
1952 long len;
1953 VALUE data;
1954 int sign;
1955
1956 sign = r_byte(arg);
1957 len = r_long(arg);
1958
1959 if (SIZEOF_VALUE >= 8 && len <= 4) {
1960 // Representable within uintptr, likely FIXNUM
1961 VALUE num = 0;
1962 for (int i = 0; i < len; i++) {
1963 num |= (VALUE)r_byte(arg) << (i * 16);
1964 num |= (VALUE)r_byte(arg) << (i * 16 + 8);
1965 }
1966#if SIZEOF_VALUE == SIZEOF_LONG
1967 v = ULONG2NUM(num);
1968#else
1969 v = ULL2NUM(num);
1970#endif
1971 if (sign == '-') {
1972 v = rb_int_uminus(v);
1973 }
1974 }
1975 else {
1976 data = r_bytes0(len * 2, arg);
1977 v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
1978 INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
1979 rb_str_resize(data, 0L);
1980 }
1981 v = r_entry(v, arg);
1982 v = r_leave(v, arg, false);
1983 }
1984 break;
1985
1986 case TYPE_STRING:
1987 v = r_entry(r_string(arg), arg);
1988 v = r_leave(v, arg, partial);
1989 break;
1990
1991 case TYPE_REGEXP:
1992 {
1993 VALUE str = r_bytes(arg);
1994 int options = r_byte(arg);
1995 int has_encoding = FALSE;
1996 st_index_t idx = r_prepare(arg);
1997
1998 if (ivp) {
1999 r_ivar(str, &has_encoding, arg);
2000 *ivp = FALSE;
2001 }
2002 if (!has_encoding) {
2003 /* 1.8 compatibility; remove escapes undefined in 1.8 */
2004 char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
2005 long len = RSTRING_LEN(str);
2006 long bs = 0;
2007 for (; len-- > 0; *dst++ = *src++) {
2008 switch (*src) {
2009 case '\\': bs++; break;
2010 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2011 case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
2012 case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
2013 case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
2014 case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
2015 if (bs & 1) --dst;
2016 /* fall through */
2017 default: bs = 0; break;
2018 }
2019 }
2020 rb_str_set_len(str, dst - ptr);
2021 }
2022 v = r_entry0(rb_reg_new_str(str, options), idx, arg);
2023 v = r_leave(v, arg, partial);
2024 }
2025 break;
2026
2027 case TYPE_ARRAY:
2028 {
2029 long len = r_long(arg);
2030
2031 v = rb_ary_new2(len);
2032 v = r_entry(v, arg);
2033 arg->readable += len - 1;
2034 while (len--) {
2035 rb_ary_push(v, r_object(arg));
2036 arg->readable--;
2037 }
2038 v = r_leave(v, arg, partial);
2039 arg->readable++;
2040 }
2041 break;
2042
2043 case TYPE_HASH:
2044 case TYPE_HASH_DEF:
2045 type_hash:
2046 {
2047 long len = r_long(arg);
2048
2049 v = hash_new_with_size(len);
2050 v = r_entry(v, arg);
2051 arg->readable += (len - 1) * 2;
2052 while (len--) {
2053 VALUE key = r_object(arg);
2054 VALUE value = r_object(arg);
2055 rb_hash_aset(v, key, value);
2056 arg->readable -= 2;
2057 }
2058 arg->readable += 2;
2059 if (type == TYPE_HASH_DEF) {
2060 RHASH_SET_IFNONE(v, r_object(arg));
2061 }
2062 v = r_leave(v, arg, partial);
2063 }
2064 break;
2065
2066 case TYPE_STRUCT:
2067 {
2068 VALUE mem, values;
2069 long i;
2070 VALUE slot;
2071 st_index_t idx = r_prepare(arg);
2072 VALUE klass = path2class(r_unique(arg));
2073 long len = r_long(arg);
2074
2075 v = rb_obj_alloc(klass);
2076 if (!RB_TYPE_P(v, T_STRUCT)) {
2077 rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
2078 }
2079 mem = rb_struct_s_members(klass);
2080 if (RARRAY_LEN(mem) != len) {
2081 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
2082 rb_class_name(klass));
2083 }
2084
2085 arg->readable += (len - 1) * 2;
2086 v = r_entry0(v, idx, arg);
2087 values = rb_ary_new2(len);
2088 {
2089 VALUE keywords = Qfalse;
2090 if (RTEST(rb_struct_s_keyword_init(klass))) {
2091 keywords = rb_hash_new();
2092 rb_ary_push(values, keywords);
2093 }
2094
2095 for (i=0; i<len; i++) {
2096 VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
2097 slot = r_symbol(arg);
2098
2099 if (!rb_str_equal(n, slot)) {
2100 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
2101 rb_class_name(klass),
2102 slot, n);
2103 }
2104 if (keywords) {
2105 rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
2106 }
2107 else {
2108 rb_ary_push(values, r_object(arg));
2109 }
2110 arg->readable -= 2;
2111 }
2112 }
2113 rb_struct_initialize(v, values);
2114 v = r_leave(v, arg, partial);
2115 arg->readable += 2;
2116 }
2117 break;
2118
2119 case TYPE_USERDEF:
2120 {
2121 VALUE name = r_unique(arg);
2122 VALUE klass = path2class(name);
2123 VALUE data;
2124 st_data_t d;
2125
2126 if (!rb_obj_respond_to(klass, s_load, TRUE)) {
2127 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method `_load'",
2128 name);
2129 }
2130 data = r_string(arg);
2131 if (ivp) {
2132 r_ivar(data, NULL, arg);
2133 *ivp = FALSE;
2134 }
2135 v = load_funcall(arg, klass, s_load, 1, &data);
2136 v = r_entry(v, arg);
2137 if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
2138 marshal_compat_t *compat = (marshal_compat_t*)d;
2139 v = compat->loader(klass, v);
2140 }
2141 if (!partial) v = r_post_proc(v, arg);
2142 }
2143 break;
2144
2145 case TYPE_USRMARSHAL:
2146 {
2147 VALUE name = r_unique(arg);
2148 VALUE klass = path2class(name);
2149 VALUE oldclass = 0;
2150 VALUE data;
2151
2152 v = obj_alloc_by_klass(klass, arg, &oldclass);
2153 if (!NIL_P(extmod)) {
2154 /* for the case marshal_load is overridden */
2155 append_extmod(v, extmod);
2156 }
2157 if (!rb_obj_respond_to(v, s_mload, TRUE)) {
2158 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method `marshal_load'",
2159 name);
2160 }
2161 v = r_entry(v, arg);
2162 data = r_object(arg);
2163 load_funcall(arg, v, s_mload, 1, &data);
2164 v = r_fixup_compat(v, arg);
2165 v = r_copy_ivar(v, data);
2166 v = r_post_proc(v, arg);
2167 if (!NIL_P(extmod)) {
2168 if (oldclass) append_extmod(v, extmod);
2169 rb_ary_clear(extmod);
2170 }
2171 }
2172 break;
2173
2174 case TYPE_OBJECT:
2175 {
2176 st_index_t idx = r_prepare(arg);
2177 v = obj_alloc_by_path(r_unique(arg), arg);
2178 if (!RB_TYPE_P(v, T_OBJECT)) {
2179 rb_raise(rb_eArgError, "dump format error");
2180 }
2181 v = r_entry0(v, idx, arg);
2182 r_ivar(v, NULL, arg);
2183 v = r_leave(v, arg, partial);
2184 }
2185 break;
2186
2187 case TYPE_DATA:
2188 {
2189 VALUE name = r_unique(arg);
2190 VALUE klass = path2class(name);
2191 VALUE oldclass = 0;
2192 VALUE r;
2193
2194 v = obj_alloc_by_klass(klass, arg, &oldclass);
2195 if (!RB_TYPE_P(v, T_DATA)) {
2196 rb_raise(rb_eArgError, "dump format error");
2197 }
2198 v = r_entry(v, arg);
2199 if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
2200 rb_raise(rb_eTypeError,
2201 "class %"PRIsVALUE" needs to have instance method `_load_data'",
2202 name);
2203 }
2204 r = r_object0(arg, partial, 0, extmod);
2205 load_funcall(arg, v, s_load_data, 1, &r);
2206 v = r_leave(v, arg, partial);
2207 }
2208 break;
2209
2210 case TYPE_MODULE_OLD:
2211 {
2212 VALUE str = r_bytes(arg);
2213
2214 v = rb_path_to_class(str);
2215 prohibit_ivar("class/module", str);
2216 v = r_entry(v, arg);
2217 v = r_leave(v, arg, partial);
2218 }
2219 break;
2220
2221 case TYPE_CLASS:
2222 {
2223 VALUE str = r_bytes(arg);
2224
2225 v = path2class(str);
2226 prohibit_ivar("class", str);
2227 v = r_entry(v, arg);
2228 v = r_leave(v, arg, partial);
2229 }
2230 break;
2231
2232 case TYPE_MODULE:
2233 {
2234 VALUE str = r_bytes(arg);
2235
2236 v = path2module(str);
2237 prohibit_ivar("module", str);
2238 v = r_entry(v, arg);
2239 v = r_leave(v, arg, partial);
2240 }
2241 break;
2242
2243 case TYPE_SYMBOL:
2244 if (ivp) {
2245 v = r_symreal(arg, *ivp);
2246 *ivp = FALSE;
2247 }
2248 else {
2249 v = r_symreal(arg, 0);
2250 }
2251 v = rb_str_intern(v);
2252 v = r_leave(v, arg, partial);
2253 break;
2254
2255 case TYPE_SYMLINK:
2256 v = rb_str_intern(r_symlink(arg));
2257 break;
2258
2259 default:
2260 rb_raise(rb_eArgError, "dump format error(0x%x)", type);
2261 break;
2262 }
2263
2264 if (UNDEF_P(v)) {
2265 rb_raise(rb_eArgError, "dump format error (bad link)");
2266 }
2267
2268 return v;
2269}
2270
2271static VALUE
2272r_object(struct load_arg *arg)
2273{
2274 return r_object0(arg, false, 0, Qnil);
2275}
2276
2277static void
2278clear_load_arg(struct load_arg *arg)
2279{
2280 if (arg->buf) {
2281 xfree(arg->buf);
2282 arg->buf = 0;
2283 }
2284 arg->buflen = 0;
2285 arg->offset = 0;
2286 arg->readable = 0;
2287 if (!arg->symbols) return;
2288 st_free_table(arg->symbols);
2289 arg->symbols = 0;
2290 st_free_table(arg->data);
2291 arg->data = 0;
2292 st_free_table(arg->partial_objects);
2293 arg->partial_objects = 0;
2294 if (arg->compat_tbl) {
2295 st_free_table(arg->compat_tbl);
2296 arg->compat_tbl = 0;
2297 }
2298}
2299
2300VALUE
2301rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze)
2302{
2303 int major, minor;
2304 VALUE v;
2305 VALUE wrapper; /* used to avoid memory leak in case of exception */
2306 struct load_arg *arg;
2307
2308 v = rb_check_string_type(port);
2309 if (!NIL_P(v)) {
2310 port = v;
2311 }
2312 else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
2313 rb_check_funcall(port, s_binmode, 0, 0);
2314 }
2315 else {
2316 io_needed();
2317 }
2318 wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
2319 arg->src = port;
2320 arg->offset = 0;
2321 arg->symbols = st_init_numtable();
2322 arg->data = rb_init_identtable();
2323 arg->partial_objects = rb_init_identtable();
2324 arg->compat_tbl = 0;
2325 arg->proc = 0;
2326 arg->readable = 0;
2327 arg->freeze = freeze;
2328
2329 if (NIL_P(v))
2330 arg->buf = xmalloc(BUFSIZ);
2331 else
2332 arg->buf = 0;
2333
2334 major = r_byte(arg);
2335 minor = r_byte(arg);
2336 if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
2337 clear_load_arg(arg);
2338 rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
2339\tformat version %d.%d required; %d.%d given",
2340 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2341 }
2342 if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
2343 rb_warn("incompatible marshal file format (can be read)\n\
2344\tformat version %d.%d required; %d.%d given",
2345 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2346 }
2347
2348 if (!NIL_P(proc)) arg->proc = proc;
2349 v = r_object(arg);
2350 clear_load_arg(arg);
2351 RB_GC_GUARD(wrapper);
2352
2353 return v;
2354}
2355
2356static VALUE
2357marshal_load(rb_execution_context_t *ec, VALUE mod, VALUE source, VALUE proc, VALUE freeze)
2358{
2359 return rb_marshal_load_with_proc(source, proc, RTEST(freeze));
2360}
2361
2362#include "marshal.rbinc"
2363
2364/*
2365 * The marshaling library converts collections of Ruby objects into a
2366 * byte stream, allowing them to be stored outside the currently
2367 * active script. This data may subsequently be read and the original
2368 * objects reconstituted.
2369 *
2370 * Marshaled data has major and minor version numbers stored along
2371 * with the object information. In normal use, marshaling can only
2372 * load data written with the same major version number and an equal
2373 * or lower minor version number. If Ruby's ``verbose'' flag is set
2374 * (normally using -d, -v, -w, or --verbose) the major and minor
2375 * numbers must match exactly. Marshal versioning is independent of
2376 * Ruby's version numbers. You can extract the version by reading the
2377 * first two bytes of marshaled data.
2378 *
2379 * str = Marshal.dump("thing")
2380 * RUBY_VERSION #=> "1.9.0"
2381 * str[0].ord #=> 4
2382 * str[1].ord #=> 8
2383 *
2384 * Some objects cannot be dumped: if the objects to be dumped include
2385 * bindings, procedure or method objects, instances of class IO, or
2386 * singleton objects, a TypeError will be raised.
2387 *
2388 * If your class has special serialization needs (for example, if you
2389 * want to serialize in some specific format), or if it contains
2390 * objects that would otherwise not be serializable, you can implement
2391 * your own serialization strategy.
2392 *
2393 * There are two methods of doing this, your object can define either
2394 * marshal_dump and marshal_load or _dump and _load. marshal_dump will take
2395 * precedence over _dump if both are defined. marshal_dump may result in
2396 * smaller Marshal strings.
2397 *
2398 * == Security considerations
2399 *
2400 * By design, Marshal.load can deserialize almost any class loaded into the
2401 * Ruby process. In many cases this can lead to remote code execution if the
2402 * Marshal data is loaded from an untrusted source.
2403 *
2404 * As a result, Marshal.load is not suitable as a general purpose serialization
2405 * format and you should never unmarshal user supplied input or other untrusted
2406 * data.
2407 *
2408 * If you need to deserialize untrusted data, use JSON or another serialization
2409 * format that is only able to load simple, 'primitive' types such as String,
2410 * Array, Hash, etc. Never allow user input to specify arbitrary types to
2411 * deserialize into.
2412 *
2413 * == marshal_dump and marshal_load
2414 *
2415 * When dumping an object the method marshal_dump will be called.
2416 * marshal_dump must return a result containing the information necessary for
2417 * marshal_load to reconstitute the object. The result can be any object.
2418 *
2419 * When loading an object dumped using marshal_dump the object is first
2420 * allocated then marshal_load is called with the result from marshal_dump.
2421 * marshal_load must recreate the object from the information in the result.
2422 *
2423 * Example:
2424 *
2425 * class MyObj
2426 * def initialize name, version, data
2427 * @name = name
2428 * @version = version
2429 * @data = data
2430 * end
2431 *
2432 * def marshal_dump
2433 * [@name, @version]
2434 * end
2435 *
2436 * def marshal_load array
2437 * @name, @version = array
2438 * end
2439 * end
2440 *
2441 * == _dump and _load
2442 *
2443 * Use _dump and _load when you need to allocate the object you're restoring
2444 * yourself.
2445 *
2446 * When dumping an object the instance method _dump is called with an Integer
2447 * which indicates the maximum depth of objects to dump (a value of -1 implies
2448 * that you should disable depth checking). _dump must return a String
2449 * containing the information necessary to reconstitute the object.
2450 *
2451 * The class method _load should take a String and use it to return an object
2452 * of the same class.
2453 *
2454 * Example:
2455 *
2456 * class MyObj
2457 * def initialize name, version, data
2458 * @name = name
2459 * @version = version
2460 * @data = data
2461 * end
2462 *
2463 * def _dump level
2464 * [@name, @version].join ':'
2465 * end
2466 *
2467 * def self._load args
2468 * new(*args.split(':'))
2469 * end
2470 * end
2471 *
2472 * Since Marshal.dump outputs a string you can have _dump return a Marshal
2473 * string which is Marshal.loaded in _load for complex objects.
2474 */
2475void
2476Init_marshal(void)
2477{
2478 VALUE rb_mMarshal = rb_define_module("Marshal");
2479#define set_id(sym) sym = rb_intern_const(name_##sym)
2480 set_id(s_dump);
2481 set_id(s_load);
2482 set_id(s_mdump);
2483 set_id(s_mload);
2484 set_id(s_dump_data);
2485 set_id(s_load_data);
2486 set_id(s_alloc);
2487 set_id(s_call);
2488 set_id(s_getbyte);
2489 set_id(s_read);
2490 set_id(s_write);
2491 set_id(s_binmode);
2492 set_id(s_encoding_short);
2493 set_id(s_ruby2_keywords_flag);
2494
2495 rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
2496
2497 /* major version */
2498 rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
2499 /* minor version */
2500 rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
2501}
2502
2503static st_table *
2504compat_allocator_table(void)
2505{
2506 if (compat_allocator_tbl) return compat_allocator_tbl;
2507 compat_allocator_tbl = st_init_numtable();
2508#undef RUBY_UNTYPED_DATA_WARNING
2509#define RUBY_UNTYPED_DATA_WARNING 0
2510 compat_allocator_tbl_wrapper =
2511 Data_Wrap_Struct(0, mark_marshal_compat_t, 0, compat_allocator_tbl);
2512 rb_gc_register_mark_object(compat_allocator_tbl_wrapper);
2513 return compat_allocator_tbl;
2514}
2515
2516VALUE
2517rb_marshal_dump(VALUE obj, VALUE port)
2518{
2519 return rb_marshal_dump_limited(obj, port, -1);
2520}
2521
2522VALUE
2523rb_marshal_load(VALUE port)
2524{
2525 return rb_marshal_load_with_proc(port, Qnil, false);
2526}
Defines RBIMPL_HAS_BUILTIN.