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