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