Ruby: marshal.c Source File

Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)

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