Ruby: marshal.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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
119 typedef 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
150 rb_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
170 struct 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
179 struct 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
602 struct 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
1231 struct 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 int
1807r_move_ivar(st_data_t k, st_data_t v, st_data_t d)
1808{
1809 ID key = (ID)k;
1810 VALUE value = (VALUE)v;
1811 VALUE dest = (VALUE)d;
1812
1813 if (rb_is_instance_id(key)) {
1814 rb_ivar_set(dest, key, value);
1815 return ST_DELETE;
1816 }
1817 return ST_CONTINUE;
1818}
1819
1820static VALUE
1821r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type)
1822{
1823 VALUE (*hash_new_with_size)(st_index_t) = rb_hash_new_with_size;
1824 VALUE v = Qnil;
1825 long id;
1826 st_data_t link;
1827
1828 switch (type) {
1829 case TYPE_LINK:
1830 id = r_long(arg);
1831 if (!st_lookup(arg->data, (st_data_t)id, &link)) {
1832 rb_raise(rb_eArgError, "dump format error (unlinked)");
1833 }
1834 v = (VALUE)link;
1835 if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
1836 v = r_post_proc(v, arg);
1837 }
1838 break;
1839
1840 case TYPE_IVAR:
1841 {
1842 int ivar = TRUE;
1843 v = r_object0(arg, true, &ivar, extmod);
1844 if (ivar) r_ivar(v, NULL, arg);
1845 v = r_leave(v, arg, partial);
1846 }
1847 break;
1848
1849 case TYPE_EXTENDED:
1850 {
1851 VALUE path = r_unique(arg);
1852 VALUE m = rb_path_to_class(path);
1853 if (NIL_P(extmod)) extmod = rb_ary_hidden_new(0);
1854
1855 if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
1856 VALUE c;
1857
1858 v = r_object0(arg, true, 0, Qnil);
1859 c = CLASS_OF(v);
1860 if (c != m || FL_TEST(c, FL_SINGLETON)) {
1861 rb_raise(rb_eArgError,
1862 "prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
1863 path, rb_class_name(c));
1864 }
1865 c = rb_singleton_class(v);
1866 while (RARRAY_LEN(extmod) > 0) {
1867 m = rb_ary_pop(extmod);
1868 rb_prepend_module(c, m);
1869 }
1870 }
1871 else {
1872 must_be_module(m, path);
1873 rb_ary_push(extmod, m);
1874
1875 v = r_object0(arg, true, 0, extmod);
1876 while (RARRAY_LEN(extmod) > 0) {
1877 m = rb_ary_pop(extmod);
1878 rb_extend_object(v, m);
1879 }
1880 }
1881 }
1882 break;
1883
1884 case TYPE_UCLASS:
1885 {
1886 VALUE c = path2class(r_unique(arg));
1887
1888 if (FL_TEST(c, FL_SINGLETON)) {
1889 rb_raise(rb_eTypeError, "singleton can't be loaded");
1890 }
1891 type = r_byte(arg);
1892 if ((c == rb_cHash) &&
1893 /* Hack for compare_by_identify */
1894 (type == TYPE_HASH || type == TYPE_HASH_DEF)) {
1895 hash_new_with_size = rb_ident_hash_new_with_size;
1896 goto type_hash;
1897 }
1898 v = r_object_for(arg, partial, 0, extmod, type);
1899 if (rb_special_const_p(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
1900 goto format_error;
1901 }
1902 if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
1903 VALUE tmp = rb_obj_alloc(c);
1904
1905 if (TYPE(v) != TYPE(tmp)) goto format_error;
1906 }
1907 RBASIC_SET_CLASS(v, c);
1908 }
1909 break;
1910
1911 format_error:
1912 rb_raise(rb_eArgError, "dump format error (user class)");
1913
1914 case TYPE_NIL:
1915 v = Qnil;
1916 v = r_leave(v, arg, false);
1917 break;
1918
1919 case TYPE_TRUE:
1920 v = Qtrue;
1921 v = r_leave(v, arg, false);
1922 break;
1923
1924 case TYPE_FALSE:
1925 v = Qfalse;
1926 v = r_leave(v, arg, false);
1927 break;
1928
1929 case TYPE_FIXNUM:
1930 {
1931 long i = r_long(arg);
1932 v = LONG2FIX(i);
1933 }
1934 v = r_leave(v, arg, false);
1935 break;
1936
1937 case TYPE_FLOAT:
1938 {
1939 double d;
1940 VALUE str = r_bytes(arg);
1941 const char *ptr = RSTRING_PTR(str);
1942
1943 if (strcmp(ptr, "nan") == 0) {
1944 d = nan("");
1945 }
1946 else if (strcmp(ptr, "inf") == 0) {
1947 d = HUGE_VAL;
1948 }
1949 else if (strcmp(ptr, "-inf") == 0) {
1950 d = -HUGE_VAL;
1951 }
1952 else {
1953 char *e;
1954 d = strtod(ptr, &e);
1955 d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
1956 }
1957 v = DBL2NUM(d);
1958 v = r_entry(v, arg);
1959 v = r_leave(v, arg, false);
1960 }
1961 break;
1962
1963 case TYPE_BIGNUM:
1964 {
1965 long len;
1966 VALUE data;
1967 int sign;
1968
1969 sign = r_byte(arg);
1970 len = r_long(arg);
1971
1972 if (SIZEOF_VALUE >= 8 && len <= 4) {
1973 // Representable within uintptr, likely FIXNUM
1974 VALUE num = 0;
1975 for (int i = 0; i < len; i++) {
1976 num |= (VALUE)r_byte(arg) << (i * 16);
1977 num |= (VALUE)r_byte(arg) << (i * 16 + 8);
1978 }
1979#if SIZEOF_VALUE == SIZEOF_LONG
1980 v = ULONG2NUM(num);
1981#else
1982 v = ULL2NUM(num);
1983#endif
1984 if (sign == '-') {
1985 v = rb_int_uminus(v);
1986 }
1987 }
1988 else {
1989 data = r_bytes0(len * 2, arg);
1990 v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
1991 INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
1992 rb_str_resize(data, 0L);
1993 }
1994 v = r_entry(v, arg);
1995 v = r_leave(v, arg, false);
1996 }
1997 break;
1998
1999 case TYPE_STRING:
2000 v = r_entry(r_string(arg), arg);
2001 v = r_leave(v, arg, partial);
2002 break;
2003
2004 case TYPE_REGEXP:
2005 {
2006 VALUE str = r_bytes(arg);
2007 int options = r_byte(arg);
2008 int has_encoding = FALSE;
2009 st_index_t idx = r_prepare(arg);
2010
2011 if (ivp) {
2012 r_ivar(str, &has_encoding, arg);
2013 *ivp = FALSE;
2014 }
2015 if (!has_encoding) {
2016 /* 1.8 compatibility; remove escapes undefined in 1.8 */
2017 char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
2018 long len = RSTRING_LEN(str);
2019 long bs = 0;
2020 for (; len-- > 0; *dst++ = *src++) {
2021 switch (*src) {
2022 case '\\': bs++; break;
2023 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2024 case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
2025 case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
2026 case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
2027 case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
2028 if (bs & 1) --dst;
2029 /* fall through */
2030 default: bs = 0; break;
2031 }
2032 }
2033 rb_str_set_len(str, dst - ptr);
2034 }
2035 VALUE regexp = rb_reg_new_str(str, options);
2036 rb_ivar_foreach(str, r_move_ivar, regexp);
2037
2038 v = r_entry0(regexp, idx, arg);
2039 v = r_leave(v, arg, partial);
2040 }
2041 break;
2042
2043 case TYPE_ARRAY:
2044 {
2045 long len = r_long(arg);
2046
2047 v = rb_ary_new2(len);
2048 v = r_entry(v, arg);
2049 arg->readable += len - 1;
2050 while (len--) {
2051 rb_ary_push(v, r_object(arg));
2052 arg->readable--;
2053 }
2054 v = r_leave(v, arg, partial);
2055 arg->readable++;
2056 }
2057 break;
2058
2059 case TYPE_HASH:
2060 case TYPE_HASH_DEF:
2061 type_hash:
2062 {
2063 long len = r_long(arg);
2064
2065 v = hash_new_with_size(len);
2066 v = r_entry(v, arg);
2067 arg->readable += (len - 1) * 2;
2068 while (len--) {
2069 VALUE key = r_object(arg);
2070 VALUE value = r_object(arg);
2071 rb_hash_aset(v, key, value);
2072 arg->readable -= 2;
2073 }
2074 arg->readable += 2;
2075 if (type == TYPE_HASH_DEF) {
2076 RHASH_SET_IFNONE(v, r_object(arg));
2077 }
2078 v = r_leave(v, arg, partial);
2079 }
2080 break;
2081
2082 case TYPE_STRUCT:
2083 {
2084 VALUE mem, values;
2085 long i;
2086 VALUE slot;
2087 st_index_t idx = r_prepare(arg);
2088 VALUE klass = path2class(r_unique(arg));
2089 long len = r_long(arg);
2090
2091 v = rb_obj_alloc(klass);
2092 if (!RB_TYPE_P(v, T_STRUCT)) {
2093 rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
2094 }
2095 mem = rb_struct_s_members(klass);
2096 if (RARRAY_LEN(mem) != len) {
2097 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
2098 rb_class_name(klass));
2099 }
2100
2101 arg->readable += (len - 1) * 2;
2102 v = r_entry0(v, idx, arg);
2103 values = rb_ary_new2(len);
2104 {
2105 VALUE keywords = Qfalse;
2106 if (RTEST(rb_struct_s_keyword_init(klass))) {
2107 keywords = rb_hash_new();
2108 rb_ary_push(values, keywords);
2109 }
2110
2111 for (i=0; i<len; i++) {
2112 VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
2113 slot = r_symbol(arg);
2114
2115 if (!rb_str_equal(n, slot)) {
2116 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
2117 rb_class_name(klass),
2118 slot, n);
2119 }
2120 if (keywords) {
2121 rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
2122 }
2123 else {
2124 rb_ary_push(values, r_object(arg));
2125 }
2126 arg->readable -= 2;
2127 }
2128 }
2129 rb_struct_initialize(v, values);
2130 v = r_leave(v, arg, partial);
2131 arg->readable += 2;
2132 }
2133 break;
2134
2135 case TYPE_USERDEF:
2136 {
2137 VALUE name = r_unique(arg);
2138 VALUE klass = path2class(name);
2139 VALUE data;
2140 st_data_t d;
2141
2142 if (!rb_obj_respond_to(klass, s_load, TRUE)) {
2143 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method `_load'",
2144 name);
2145 }
2146 data = r_string(arg);
2147 if (ivp) {
2148 r_ivar(data, NULL, arg);
2149 *ivp = FALSE;
2150 }
2151 v = load_funcall(arg, klass, s_load, 1, &data);
2152 v = r_entry(v, arg);
2153 if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
2154 marshal_compat_t *compat = (marshal_compat_t*)d;
2155 v = compat->loader(klass, v);
2156 }
2157 if (!partial) v = r_post_proc(v, arg);
2158 }
2159 break;
2160
2161 case TYPE_USRMARSHAL:
2162 {
2163 VALUE name = r_unique(arg);
2164 VALUE klass = path2class(name);
2165 VALUE oldclass = 0;
2166 VALUE data;
2167
2168 v = obj_alloc_by_klass(klass, arg, &oldclass);
2169 if (!NIL_P(extmod)) {
2170 /* for the case marshal_load is overridden */
2171 append_extmod(v, extmod);
2172 }
2173 if (!rb_obj_respond_to(v, s_mload, TRUE)) {
2174 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method `marshal_load'",
2175 name);
2176 }
2177 v = r_entry(v, arg);
2178 data = r_object(arg);
2179 load_funcall(arg, v, s_mload, 1, &data);
2180 v = r_fixup_compat(v, arg);
2181 v = r_copy_ivar(v, data);
2182 v = r_post_proc(v, arg);
2183 if (!NIL_P(extmod)) {
2184 if (oldclass) append_extmod(v, extmod);
2185 rb_ary_clear(extmod);
2186 }
2187 }
2188 break;
2189
2190 case TYPE_OBJECT:
2191 {
2192 st_index_t idx = r_prepare(arg);
2193 v = obj_alloc_by_path(r_unique(arg), arg);
2194 if (!RB_TYPE_P(v, T_OBJECT)) {
2195 rb_raise(rb_eArgError, "dump format error");
2196 }
2197 v = r_entry0(v, idx, arg);
2198 r_ivar(v, NULL, arg);
2199 v = r_leave(v, arg, partial);
2200 }
2201 break;
2202
2203 case TYPE_DATA:
2204 {
2205 VALUE name = r_unique(arg);
2206 VALUE klass = path2class(name);
2207 VALUE oldclass = 0;
2208 VALUE r;
2209
2210 v = obj_alloc_by_klass(klass, arg, &oldclass);
2211 if (!RB_TYPE_P(v, T_DATA)) {
2212 rb_raise(rb_eArgError, "dump format error");
2213 }
2214 v = r_entry(v, arg);
2215 if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
2216 rb_raise(rb_eTypeError,
2217 "class %"PRIsVALUE" needs to have instance method `_load_data'",
2218 name);
2219 }
2220 r = r_object0(arg, partial, 0, extmod);
2221 load_funcall(arg, v, s_load_data, 1, &r);
2222 v = r_leave(v, arg, partial);
2223 }
2224 break;
2225
2226 case TYPE_MODULE_OLD:
2227 {
2228 VALUE str = r_bytes(arg);
2229
2230 v = rb_path_to_class(str);
2231 prohibit_ivar("class/module", str);
2232 v = r_entry(v, arg);
2233 v = r_leave(v, arg, partial);
2234 }
2235 break;
2236
2237 case TYPE_CLASS:
2238 {
2239 VALUE str = r_bytes(arg);
2240
2241 v = path2class(str);
2242 prohibit_ivar("class", str);
2243 v = r_entry(v, arg);
2244 v = r_leave(v, arg, partial);
2245 }
2246 break;
2247
2248 case TYPE_MODULE:
2249 {
2250 VALUE str = r_bytes(arg);
2251
2252 v = path2module(str);
2253 prohibit_ivar("module", str);
2254 v = r_entry(v, arg);
2255 v = r_leave(v, arg, partial);
2256 }
2257 break;
2258
2259 case TYPE_SYMBOL:
2260 if (ivp) {
2261 v = r_symreal(arg, *ivp);
2262 *ivp = FALSE;
2263 }
2264 else {
2265 v = r_symreal(arg, 0);
2266 }
2267 v = rb_str_intern(v);
2268 v = r_leave(v, arg, partial);
2269 break;
2270
2271 case TYPE_SYMLINK:
2272 v = rb_str_intern(r_symlink(arg));
2273 break;
2274
2275 default:
2276 rb_raise(rb_eArgError, "dump format error(0x%x)", type);
2277 break;
2278 }
2279
2280 if (UNDEF_P(v)) {
2281 rb_raise(rb_eArgError, "dump format error (bad link)");
2282 }
2283
2284 return v;
2285}
2286
2287static VALUE
2288r_object(struct load_arg *arg)
2289{
2290 return r_object0(arg, false, 0, Qnil);
2291}
2292
2293static void
2294clear_load_arg(struct load_arg *arg)
2295{
2296 if (arg->buf) {
2297 xfree(arg->buf);
2298 arg->buf = 0;
2299 }
2300 arg->buflen = 0;
2301 arg->offset = 0;
2302 arg->readable = 0;
2303 if (!arg->symbols) return;
2304 st_free_table(arg->symbols);
2305 arg->symbols = 0;
2306 st_free_table(arg->data);
2307 arg->data = 0;
2308 st_free_table(arg->partial_objects);
2309 arg->partial_objects = 0;
2310 if (arg->compat_tbl) {
2311 st_free_table(arg->compat_tbl);
2312 arg->compat_tbl = 0;
2313 }
2314}
2315
2316VALUE
2317rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze)
2318{
2319 int major, minor;
2320 VALUE v;
2321 VALUE wrapper; /* used to avoid memory leak in case of exception */
2322 struct load_arg *arg;
2323
2324 v = rb_check_string_type(port);
2325 if (!NIL_P(v)) {
2326 port = v;
2327 }
2328 else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
2329 rb_check_funcall(port, s_binmode, 0, 0);
2330 }
2331 else {
2332 io_needed();
2333 }
2334 wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
2335 arg->src = port;
2336 arg->offset = 0;
2337 arg->symbols = st_init_numtable();
2338 arg->data = rb_init_identtable();
2339 arg->partial_objects = rb_init_identtable();
2340 arg->compat_tbl = 0;
2341 arg->proc = 0;
2342 arg->readable = 0;
2343 arg->freeze = freeze;
2344
2345 if (NIL_P(v))
2346 arg->buf = xmalloc(BUFSIZ);
2347 else
2348 arg->buf = 0;
2349
2350 major = r_byte(arg);
2351 minor = r_byte(arg);
2352 if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
2353 clear_load_arg(arg);
2354 rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
2355\tformat version %d.%d required; %d.%d given",
2356 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2357 }
2358 if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
2359 rb_warn("incompatible marshal file format (can be read)\n\
2360\tformat version %d.%d required; %d.%d given",
2361 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2362 }
2363
2364 if (!NIL_P(proc)) arg->proc = proc;
2365 v = r_object(arg);
2366 clear_load_arg(arg);
2367 RB_GC_GUARD(wrapper);
2368
2369 return v;
2370}
2371
2372static VALUE
2373marshal_load(rb_execution_context_t *ec, VALUE mod, VALUE source, VALUE proc, VALUE freeze)
2374{
2375 return rb_marshal_load_with_proc(source, proc, RTEST(freeze));
2376}
2377
2378#include "marshal.rbinc"
2379
2380/*
2381 * The marshaling library converts collections of Ruby objects into a
2382 * byte stream, allowing them to be stored outside the currently
2383 * active script. This data may subsequently be read and the original
2384 * objects reconstituted.
2385 *
2386 * Marshaled data has major and minor version numbers stored along
2387 * with the object information. In normal use, marshaling can only
2388 * load data written with the same major version number and an equal
2389 * or lower minor version number. If Ruby's ``verbose'' flag is set
2390 * (normally using -d, -v, -w, or --verbose) the major and minor
2391 * numbers must match exactly. Marshal versioning is independent of
2392 * Ruby's version numbers. You can extract the version by reading the
2393 * first two bytes of marshaled data.
2394 *
2395 * str = Marshal.dump("thing")
2396 * RUBY_VERSION #=> "1.9.0"
2397 * str[0].ord #=> 4
2398 * str[1].ord #=> 8
2399 *
2400 * Some objects cannot be dumped: if the objects to be dumped include
2401 * bindings, procedure or method objects, instances of class IO, or
2402 * singleton objects, a TypeError will be raised.
2403 *
2404 * If your class has special serialization needs (for example, if you
2405 * want to serialize in some specific format), or if it contains
2406 * objects that would otherwise not be serializable, you can implement
2407 * your own serialization strategy.
2408 *
2409 * There are two methods of doing this, your object can define either
2410 * marshal_dump and marshal_load or _dump and _load. marshal_dump will take
2411 * precedence over _dump if both are defined. marshal_dump may result in
2412 * smaller Marshal strings.
2413 *
2414 * == Security considerations
2415 *
2416 * By design, Marshal.load can deserialize almost any class loaded into the
2417 * Ruby process. In many cases this can lead to remote code execution if the
2418 * Marshal data is loaded from an untrusted source.
2419 *
2420 * As a result, Marshal.load is not suitable as a general purpose serialization
2421 * format and you should never unmarshal user supplied input or other untrusted
2422 * data.
2423 *
2424 * If you need to deserialize untrusted data, use JSON or another serialization
2425 * format that is only able to load simple, 'primitive' types such as String,
2426 * Array, Hash, etc. Never allow user input to specify arbitrary types to
2427 * deserialize into.
2428 *
2429 * == marshal_dump and marshal_load
2430 *
2431 * When dumping an object the method marshal_dump will be called.
2432 * marshal_dump must return a result containing the information necessary for
2433 * marshal_load to reconstitute the object. The result can be any object.
2434 *
2435 * When loading an object dumped using marshal_dump the object is first
2436 * allocated then marshal_load is called with the result from marshal_dump.
2437 * marshal_load must recreate the object from the information in the result.
2438 *
2439 * Example:
2440 *
2441 * class MyObj
2442 * def initialize name, version, data
2443 * @name = name
2444 * @version = version
2445 * @data = data
2446 * end
2447 *
2448 * def marshal_dump
2449 * [@name, @version]
2450 * end
2451 *
2452 * def marshal_load array
2453 * @name, @version = array
2454 * end
2455 * end
2456 *
2457 * == _dump and _load
2458 *
2459 * Use _dump and _load when you need to allocate the object you're restoring
2460 * yourself.
2461 *
2462 * When dumping an object the instance method _dump is called with an Integer
2463 * which indicates the maximum depth of objects to dump (a value of -1 implies
2464 * that you should disable depth checking). _dump must return a String
2465 * containing the information necessary to reconstitute the object.
2466 *
2467 * The class method _load should take a String and use it to return an object
2468 * of the same class.
2469 *
2470 * Example:
2471 *
2472 * class MyObj
2473 * def initialize name, version, data
2474 * @name = name
2475 * @version = version
2476 * @data = data
2477 * end
2478 *
2479 * def _dump level
2480 * [@name, @version].join ':'
2481 * end
2482 *
2483 * def self._load args
2484 * new(*args.split(':'))
2485 * end
2486 * end
2487 *
2488 * Since Marshal.dump outputs a string you can have _dump return a Marshal
2489 * string which is Marshal.loaded in _load for complex objects.
2490 */
2491void
2492Init_marshal(void)
2493{
2494 VALUE rb_mMarshal = rb_define_module("Marshal");
2495#define set_id(sym) sym = rb_intern_const(name_##sym)
2496 set_id(s_dump);
2497 set_id(s_load);
2498 set_id(s_mdump);
2499 set_id(s_mload);
2500 set_id(s_dump_data);
2501 set_id(s_load_data);
2502 set_id(s_alloc);
2503 set_id(s_call);
2504 set_id(s_getbyte);
2505 set_id(s_read);
2506 set_id(s_write);
2507 set_id(s_binmode);
2508 set_id(s_encoding_short);
2509 set_id(s_ruby2_keywords_flag);
2510
2511 rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
2512
2513 /* major version */
2514 rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
2515 /* minor version */
2516 rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
2517}
2518
2519static st_table *
2520compat_allocator_table(void)
2521{
2522 if (compat_allocator_tbl) return compat_allocator_tbl;
2523 compat_allocator_tbl = st_init_numtable();
2524#undef RUBY_UNTYPED_DATA_WARNING
2525#define RUBY_UNTYPED_DATA_WARNING 0
2526 compat_allocator_tbl_wrapper =
2527 Data_Wrap_Struct(0, mark_marshal_compat_t, 0, compat_allocator_tbl);
2528 rb_gc_register_mark_object(compat_allocator_tbl_wrapper);
2529 return compat_allocator_tbl;
2530}
2531
2532VALUE
2533 rb_marshal_dump(VALUE obj, VALUE port)
2534{
2535 return rb_marshal_dump_limited(obj, port, -1);
2536}
2537
2538VALUE
2539 rb_marshal_load(VALUE port)
2540{
2541 return rb_marshal_load_with_proc(port, Qnil, false);
2542}
io.h
util.h
ruby.h