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