Ruby: marshal.c Source File

Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)

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