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