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