-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathdump.c
3348 lines (3160 loc) · 137 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
saving and restoring precompiled modules (.ji files)
*/
#include <stdlib.h>
#include <string.h>
#include "julia.h"
#include "julia_internal.h"
#include "julia_gcext.h"
#include "builtin_proto.h"
#include "serialize.h"
#ifndef _OS_WINDOWS_
#include <dlfcn.h>
#endif
#include "valgrind.h"
#include "julia_assert.h"
#ifdef __cplusplus
extern "C" {
#endif
// This file, together with ircode.c, allows (de)serialization between
// modules and *.ji cache files. `jl_save_incremental` gets called as the final step
// during package precompilation, and `_jl_restore_incremental` by `using SomePkg`
// whenever `SomePkg` has not yet been loaded.
// Types, methods, and method instances form a graph that may have cycles, so
// serialization has to break these cycles. This is handled via "backreferences,"
// referring to already (de)serialized items by an index. It is critial to ensure
// that the indexes of these backreferences align precisely during serialization
// and deserialization, to ensure that these integer indexes mean the same thing
// under both circumstances. Consequently, if you are modifying this file, be
// careful to match the sequence, if necessary reserving space for something that will
// be updated later.
// It is also necessary to save & restore references to externally-defined
// objects, e.g., for package methods that call methods defined in Base or
// elsewhere. Consequently during deserialization there's a distinction between
// "reference" types, methods, and method instances (essentially like a
// GlobalRef), and "recached" version that refer to the actual entity in the
// running session. As a concrete example, types have a module in which they are
// defined, but once defined those types can be used by any dependent package.
// We don't store the full type definition again in that dependent package, we
// just encode a reference to that type. In the running session, such references
// are merely pointers to the type-cache, but the specific address is obviously
// not likely to be reproducible across sessions (it will differ between the
// session in which you precompile and the session in which you're using the
// package). Hence, during serialization we recode them as "verbose" references
// (that follow Julia syntax to allow them to be reconstructed), but on
// deserialization we have to replace those verbose references with the
// appropriate pointer in the user's running session. We complete
// deserialization before beginning the process of recaching, because we need
// the backreferences during deserialization and the actual objects during
// recaching.
// Finally, because our backedge graph is not bidirectional, special handling is
// required to identify backedges from external methods that call internal methods.
// These get set aside and restored at the end of deserialization.
// In broad terms, the major steps in serialization are:
// - starting from a "worklist" of modules, write the header. This stores things
// like the Julia build this was precompiled for, the package dependencies,
// the list of include files, file modification times, etc.
// - gather the collection of items to be written to this precompile file. This
// includes accessible from the module's binding table (if they are owned by a
// worklist module), but also includes things like methods added to external
// functions, instances of external methods that were newly type-inferred
// while precompiling a worklist module, and backedges of callees that were
// called by methods in this package. By and large, these latter items are not
// referenced by the module(s) in the package, and so these have to be
// extracted by traversing the entire system searching for things that do link
// back to a module in the worklist.
// - serialize all the items. The first time we encounter an item, we serialized
// it, and on future references (pointers) to that item we replace them with
// with a backreference. `jl_serialize_*` functions handle this work.
// - write source text for the files that defined the package. This is primarily
// to support Revise.jl.
// Deserialization is the mirror image of serialization, but in some ways is
// trickier:
// - we have to merge items into the running session (recaching as described
// above) and handle cases like having two dependent packages caching the same
// MethodInstance of a dependency
// - we have to check for invalidation---the user might have loaded other
// packages that define methods that supersede some of the dispatches chosen
// when the package was precompiled, or this package might define methods that
// supercede dispatches for previously-loaded packages. These two
// possibilities are checked during backedge and method insertion,
// respectively.
// Both of these mean that deserialization requires one to look up a lot of
// things in the running session; for example, for invalidation checks we have
// to do type-intersection between signatures used for MethodInstances and the
// current session's full MethodTable. In practice, such steps dominate package
// loading time (it has very little to do with I/O or deserialization
// performance). Paradoxically, sometimes storing more code in a package can
// lead to faster performance: references to things in the same .ji file can be
// precomputed, but external references have to be looked up. You can see this
// effect in the benchmarks for #43990, where storing external MethodInstances
// and CodeInstances (more code than was stored previously) actually decreased
// load times for many packages.
// Note that one should prioritize deserialization performance over serialization performance,
// since deserialization may be performed much more often than serialization.
// Certain items are preprocessed during serialization to save work when they are
// later deserialized.
// TODO: put WeakRefs on the weak_refs list during deserialization
// TODO: handle finalizers
// type => tag hash for a few core types (e.g., Expr, PhiNode, etc)
static htable_t ser_tag;
// tag => type mapping, the reverse of ser_tag
static jl_value_t *deser_tag[256];
// hash of some common symbols, encoded as CommonSym_tag plus 1 byte
static htable_t common_symbol_tag;
static jl_value_t *deser_symbols[256];
// table of all objects that have been deserialized, indexed by pos
// (the order in the serializer stream). the low
// bit is reserved for flagging certain entries and pos is
// left shift by 1
static htable_t backref_table; // pos = backref_table[obj]
static int backref_table_numel;
static arraylist_t backref_list; // obj = backref_list[pos]
// set of all CodeInstances yet to be (in)validated
static htable_t new_code_instance_validate;
// list of (jl_value_t **loc, size_t pos) entries
// for anything that was flagged by the deserializer for later
// type-rewriting of some sort. pos is the index in backref_list.
static arraylist_t flagref_list;
// ref => value hash for looking up the "real" entity from
// the deserialized ref. Used for entities that must be unique,
// like types, methods, and method instances
static htable_t uniquing_table;
// list of (size_t pos, itemkey) entries
// for the serializer to mark values in need of rework
// during deserialization later
// This includes items that need rehashing (IdDict, TypeMapLevels)
// and modules.
static arraylist_t reinit_list;
// list of modules being serialized
// This is not quite globally rooted, but we take care to only
// ever assigned rooted values here.
static jl_array_t *serializer_worklist JL_GLOBALLY_ROOTED;
// The set of external MethodInstances we want to serialize
// (methods owned by other modules that were first inferred for a
// module currently being serialized)
static htable_t external_mis;
// Inference tracks newly-inferred MethodInstances during precompilation
// and registers them by calling jl_set_newly_inferred
static jl_array_t *newly_inferred JL_GLOBALLY_ROOTED;
// New roots to add to Methods. These can't be added until after
// recaching is complete, so we have to hold on to them separately
// Stored as method => (worklist_key, newroots)
// The worklist_key is the uuid of the module that triggered addition
// of `newroots`. This is needed because CodeInstances reference
// their roots by "index", and we use a bipartite index
// (module_uuid, integer_index) to make indexes "relocatable"
// (meaning that users can load modules in different orders and
// so the absolute integer index of a root is not reproducible).
// See the "root blocks" section of method.c for more detail.
static htable_t queued_method_roots;
// inverse of backedges graph (caller=>callees hash)
htable_t edges_map;
// list of requested ccallable signatures
static arraylist_t ccallable_list;
typedef struct {
ios_t *s;
jl_ptls_t ptls;
jl_array_t *loaded_modules_array;
} jl_serializer_state;
static jl_value_t *jl_idtable_type = NULL;
static jl_typename_t *jl_idtable_typename = NULL;
static jl_value_t *jl_bigint_type = NULL;
static int gmp_limb_size = 0;
static void write_uint64(ios_t *s, uint64_t i) JL_NOTSAFEPOINT
{
ios_write(s, (char*)&i, 8);
}
static void write_float64(ios_t *s, double x) JL_NOTSAFEPOINT
{
write_uint64(s, *((uint64_t*)&x));
}
void *jl_lookup_ser_tag(jl_value_t *v)
{
return ptrhash_get(&ser_tag, v);
}
void *jl_lookup_common_symbol(jl_value_t *v)
{
return ptrhash_get(&common_symbol_tag, v);
}
jl_value_t *jl_deser_tag(uint8_t tag)
{
return deser_tag[tag];
}
jl_value_t *jl_deser_symbol(uint8_t tag)
{
return deser_symbols[tag];
}
uint64_t jl_worklist_key(jl_array_t *worklist)
{
assert(jl_is_array(worklist));
size_t len = jl_array_len(worklist);
if (len > 0) {
jl_module_t *topmod = (jl_module_t*)jl_array_ptr_ref(worklist, len-1);
assert(jl_is_module(topmod));
return topmod->build_id;
}
return 0;
}
// --- serialize ---
#define jl_serialize_value(s, v) jl_serialize_value_((s), (jl_value_t*)(v), 0)
static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_literal) JL_GC_DISABLED;
static void jl_serialize_cnull(jl_serializer_state *s, jl_value_t *t)
{
backref_table_numel++;
write_uint8(s->s, TAG_CNULL);
jl_serialize_value(s, t);
}
static int module_in_worklist(jl_module_t *mod) JL_NOTSAFEPOINT
{
int i, l = jl_array_len(serializer_worklist);
for (i = 0; i < l; i++) {
jl_module_t *workmod = (jl_module_t*)jl_array_ptr_ref(serializer_worklist, i);
if (jl_is_module(workmod) && jl_is_submodule(mod, workmod))
return 1;
}
return 0;
}
static int method_instance_in_queue(jl_method_instance_t *mi)
{
return ptrhash_get(&external_mis, mi) != HT_NOTFOUND;
}
// compute whether a type references something internal to worklist
// and thus could not have existed before deserialize
// and thus does not need delayed unique-ing
static int type_in_worklist(jl_datatype_t *dt) JL_NOTSAFEPOINT
{
if (module_in_worklist(dt->name->module))
return 1;
int i, l = jl_svec_len(dt->parameters);
for (i = 0; i < l; i++) {
jl_value_t *p = jl_unwrap_unionall(jl_tparam(dt, i));
// TODO: what about Union and TypeVar??
if (type_in_worklist((jl_datatype_t*)(jl_is_datatype(p) ? p : jl_typeof(p))))
return 1;
}
return 0;
}
static int type_recursively_external(jl_datatype_t *dt);
static int type_parameter_recursively_external(jl_value_t *p0) JL_NOTSAFEPOINT
{
if (!jl_is_concrete_type(p0))
return 0;
jl_datatype_t *p = (jl_datatype_t*)p0;
//while (jl_is_unionall(p)) {
// if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->lb))
// return 0;
// if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->ub))
// return 0;
// p = (jl_datatype_t*)((jl_unionall_t*)p)->body;
//}
if (module_in_worklist(p->name->module))
return 0;
if (p->name->wrapper != (jl_value_t*)p0) {
if (!type_recursively_external(p))
return 0;
}
return 1;
}
// returns true if all of the parameters are tag 6 or 7
static int type_recursively_external(jl_datatype_t *dt) JL_NOTSAFEPOINT
{
if (!dt->isconcretetype)
return 0;
if (jl_svec_len(dt->parameters) == 0)
return 1;
int i, l = jl_svec_len(dt->parameters);
for (i = 0; i < l; i++) {
if (!type_parameter_recursively_external(jl_tparam(dt, i)))
return 0;
}
return 1;
}
// When we infer external method instances, ensure they link back to the
// package. Otherwise they might be, e.g., for external macros
static int has_backedge_to_worklist(jl_method_instance_t *mi, htable_t *visited)
{
void **bp = ptrhash_bp(visited, mi);
// HT_NOTFOUND: not yet analyzed
// HT_NOTFOUND + 1: doesn't link back
// HT_NOTFOUND + 2: does link back
if (*bp != HT_NOTFOUND)
return (char*)*bp - (char*)HT_NOTFOUND - 1;
*bp = (void*)((char*)HT_NOTFOUND + 1); // preliminarily mark as "not found"
jl_module_t *mod = mi->def.module;
if (jl_is_method(mod))
mod = ((jl_method_t*)mod)->module;
assert(jl_is_module(mod));
if (mi->precompiled || module_in_worklist(mod)) {
*bp = (void*)((char*)HT_NOTFOUND + 2); // found
return 1;
}
if (!mi->backedges) {
return 0;
}
size_t i, n = jl_array_len(mi->backedges);
for (i = 0; i < n; i++) {
jl_method_instance_t *be = (jl_method_instance_t*)jl_array_ptr_ref(mi->backedges, i);
if (has_backedge_to_worklist(be, visited)) {
bp = ptrhash_bp(visited, mi); // re-acquire since rehashing might change the location
*bp = (void*)((char*)HT_NOTFOUND + 2); // found
return 1;
}
}
return 0;
}
// given the list of MethodInstances that were inferred during the
// build, select those that are external and have at least one
// relocatable CodeInstance.
static size_t queue_external_mis(jl_array_t *list)
{
size_t i, n = 0;
htable_t visited;
if (list) {
assert(jl_is_array(list));
size_t n0 = jl_array_len(list);
htable_new(&visited, n0);
for (i = 0; i < n0; i++) {
jl_method_instance_t *mi = (jl_method_instance_t*)jl_array_ptr_ref(list, i);
assert(jl_is_method_instance(mi));
if (jl_is_method(mi->def.value)) {
jl_method_t *m = mi->def.method;
if (!module_in_worklist(m->module)) {
jl_code_instance_t *ci = mi->cache;
int relocatable = 0;
while (ci) {
relocatable |= ci->relocatability;
ci = ci->next;
}
if (relocatable && ptrhash_get(&external_mis, mi) == HT_NOTFOUND) {
if (has_backedge_to_worklist(mi, &visited)) {
ptrhash_put(&external_mis, mi, mi);
n++;
}
}
}
}
}
htable_free(&visited);
}
return n;
}
static void jl_serialize_datatype(jl_serializer_state *s, jl_datatype_t *dt) JL_GC_DISABLED
{
int tag = 0;
int internal = module_in_worklist(dt->name->module);
if (!internal && jl_unwrap_unionall(dt->name->wrapper) == (jl_value_t*)dt) {
tag = 6; // external primary type
}
else if (jl_is_tuple_type(dt) ? !dt->isconcretetype : dt->hasfreetypevars) {
tag = 0; // normal struct
}
else if (internal) {
if (jl_unwrap_unionall(dt->name->wrapper) == (jl_value_t*)dt) // comes up often since functions create types
tag = 5; // internal, and not in the typename cache
else
tag = 10; // anything else that's internal (just may need recaching)
}
else if (type_recursively_external(dt)) {
tag = 7; // external type that can be immediately recreated (with apply_type)
}
else if (type_in_worklist(dt)) {
tag = 11; // external, but definitely new (still needs caching, but not full unique-ing)
}
else {
// this is eligible for (and possibly requires) unique-ing later,
// so flag this in the backref table as special
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, dt);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1;
tag = 12;
}
char *dtname = jl_symbol_name(dt->name->name);
size_t dtnl = strlen(dtname);
if (dtnl > 4 && strcmp(&dtname[dtnl - 4], "##kw") == 0 && !internal && tag != 0) {
/* XXX: yuck, this is horrible, but the auto-generated kw types from the serializer isn't a real type, so we *must* be very careful */
assert(tag == 6); // other struct types should never exist
tag = 9;
if (jl_type_type_mt->kwsorter != NULL && dt == (jl_datatype_t*)jl_typeof(jl_type_type_mt->kwsorter)) {
dt = jl_datatype_type; // any representative member with this MethodTable
}
else if (jl_nonfunction_mt->kwsorter != NULL && dt == (jl_datatype_t*)jl_typeof(jl_nonfunction_mt->kwsorter)) {
dt = jl_symbol_type; // any representative member with this MethodTable
}
else {
// search for the representative member of this MethodTable
jl_methtable_t *mt = dt->name->mt;
size_t l = strlen(jl_symbol_name(mt->name));
char *prefixed;
prefixed = (char*)malloc_s(l + 2);
prefixed[0] = '#';
strcpy(&prefixed[1], jl_symbol_name(mt->name));
// remove ##kw suffix
prefixed[l-3] = 0;
jl_sym_t *tname = jl_symbol(prefixed);
free(prefixed);
jl_value_t *primarydt = jl_get_global(mt->module, tname);
if (!primarydt)
primarydt = jl_get_global(mt->module, mt->name);
primarydt = jl_unwrap_unionall(primarydt);
assert(jl_is_datatype(primarydt));
assert(primarydt == (jl_value_t*)jl_any_type || jl_typeof(((jl_datatype_t*)primarydt)->name->mt->kwsorter) == (jl_value_t*)dt);
dt = (jl_datatype_t*)primarydt;
}
}
write_uint8(s->s, TAG_DATATYPE);
write_uint8(s->s, tag);
if (tag == 6 || tag == 7) {
// for tag==6, copy its typevars in case there are references to them elsewhere
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->parameters);
return;
}
if (tag == 9) {
jl_serialize_value(s, dt);
return;
}
write_int32(s->s, dt->size);
int has_instance = (dt->instance != NULL);
int has_layout = (dt->layout != NULL);
write_uint8(s->s, has_layout | (has_instance << 1));
write_uint8(s->s, dt->hasfreetypevars
| (dt->isconcretetype << 1)
| (dt->isdispatchtuple << 2)
| (dt->isbitstype << 3)
| (dt->zeroinit << 4)
| (dt->has_concrete_subtype << 5)
| (dt->cached_by_hash << 6));
write_int32(s->s, dt->hash);
if (has_layout) {
uint8_t layout = 0;
if (dt->layout == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_array_type))->layout) {
layout = 1;
}
else if (dt->layout == jl_nothing_type->layout) {
layout = 2;
}
else if (dt->layout == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_pointer_type))->layout) {
layout = 3;
}
write_uint8(s->s, layout);
if (layout == 0) {
uint32_t nf = dt->layout->nfields;
uint32_t np = dt->layout->npointers;
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
ios_write(s->s, (const char*)dt->layout, sizeof(*dt->layout));
size_t fldsize = nf * fieldsize;
if (dt->layout->first_ptr != -1)
fldsize += np << dt->layout->fielddesc_type;
ios_write(s->s, (const char*)(dt->layout + 1), fldsize);
}
}
if (has_instance)
jl_serialize_value(s, dt->instance);
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->parameters);
jl_serialize_value(s, dt->super);
jl_serialize_value(s, dt->types);
}
static void jl_serialize_module(jl_serializer_state *s, jl_module_t *m)
{
write_uint8(s->s, TAG_MODULE);
jl_serialize_value(s, m->name);
size_t i;
if (!module_in_worklist(m)) {
if (m == m->parent) {
// top-level module
write_int8(s->s, 2);
int j = 0;
for (i = 0; i < jl_array_len(s->loaded_modules_array); i++) {
jl_module_t *mi = (jl_module_t*)jl_array_ptr_ref(s->loaded_modules_array, i);
if (!module_in_worklist(mi)) {
if (m == mi) {
write_int32(s->s, j);
return;
}
j++;
}
}
assert(0 && "top level module not found in modules array");
}
else {
write_int8(s->s, 1);
jl_serialize_value(s, m->parent);
}
return;
}
write_int8(s->s, 0);
jl_serialize_value(s, m->parent);
void **table = m->bindings.table;
for (i = 0; i < m->bindings.size; i += 2) {
if (table[i+1] != HT_NOTFOUND) {
jl_serialize_value(s, (jl_value_t*)table[i]);
jl_binding_t *b = (jl_binding_t*)table[i+1];
jl_serialize_value(s, b->name);
jl_value_t *e = jl_atomic_load_relaxed(&b->value);
if (!b->constp && e && jl_is_cpointer(e) && jl_unbox_voidpointer(e) != (void*)-1 && jl_unbox_voidpointer(e) != NULL)
// reset Ptr fields to C_NULL (but keep MAP_FAILED / INVALID_HANDLE)
jl_serialize_cnull(s, jl_typeof(e));
else
jl_serialize_value(s, e);
jl_serialize_value(s, jl_atomic_load_relaxed(&b->globalref));
jl_serialize_value(s, b->owner);
jl_serialize_value(s, jl_atomic_load_relaxed(&b->ty));
write_int8(s->s, (b->deprecated<<3) | (b->constp<<2) | (b->exportp<<1) | (b->imported));
}
}
jl_serialize_value(s, NULL);
write_int32(s->s, m->usings.len);
for(i=0; i < m->usings.len; i++) {
jl_serialize_value(s, (jl_value_t*)m->usings.items[i]);
}
write_uint8(s->s, m->istopmod);
write_uint64(s->s, m->uuid.hi);
write_uint64(s->s, m->uuid.lo);
write_uint64(s->s, m->build_id);
write_int32(s->s, m->counter);
write_int32(s->s, m->nospecialize);
write_uint8(s->s, m->optlevel);
write_uint8(s->s, m->compile);
write_uint8(s->s, m->infer);
write_uint8(s->s, m->max_methods);
}
static int jl_serialize_generic(jl_serializer_state *s, jl_value_t *v) JL_GC_DISABLED
{
if (v == NULL) {
write_uint8(s->s, TAG_NULL);
return 1;
}
void *tag = ptrhash_get(&ser_tag, v);
if (tag != HT_NOTFOUND) {
uint8_t t8 = (intptr_t)tag;
if (t8 <= LAST_TAG)
write_uint8(s->s, 0);
write_uint8(s->s, t8);
return 1;
}
if (jl_is_symbol(v)) {
void *idx = ptrhash_get(&common_symbol_tag, v);
if (idx != HT_NOTFOUND) {
write_uint8(s->s, TAG_COMMONSYM);
write_uint8(s->s, (uint8_t)(size_t)idx);
return 1;
}
}
else if (v == (jl_value_t*)jl_core_module) {
write_uint8(s->s, TAG_CORE);
return 1;
}
else if (v == (jl_value_t*)jl_base_module) {
write_uint8(s->s, TAG_BASE);
return 1;
}
if (jl_typeis(v, jl_string_type) && jl_string_len(v) == 0) {
jl_serialize_value(s, jl_an_empty_string);
return 1;
}
else if (!jl_is_uint8(v)) {
void **bp = ptrhash_bp(&backref_table, v);
if (*bp != HT_NOTFOUND) {
uintptr_t pos = (char*)*bp - (char*)HT_NOTFOUND - 1;
if (pos < 65536) {
write_uint8(s->s, TAG_SHORT_BACKREF);
write_uint16(s->s, pos);
}
else {
write_uint8(s->s, TAG_BACKREF);
write_int32(s->s, pos);
}
return 1;
}
intptr_t pos = backref_table_numel++;
if (((jl_datatype_t*)(jl_typeof(v)))->name == jl_idtable_typename) {
// will need to rehash this, later (after types are fully constructed)
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)1);
}
if (jl_is_module(v)) {
jl_module_t *m = (jl_module_t*)v;
if (module_in_worklist(m) && !module_in_worklist(m->parent)) {
// will need to reinsert this into parent bindings, later (in case of any errors during reinsert)
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)2);
}
}
// TypeMapLevels need to be rehashed
if (jl_is_mtable(v)) {
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)3);
}
pos <<= 1;
ptrhash_put(&backref_table, v, (char*)HT_NOTFOUND + pos + 1);
}
return 0;
}
static void jl_serialize_code_instance(jl_serializer_state *s, jl_code_instance_t *codeinst, int skip_partial_opaque, int internal) JL_GC_DISABLED
{
if (internal > 2) {
while (codeinst && !codeinst->relocatability)
codeinst = codeinst->next;
}
if (jl_serialize_generic(s, (jl_value_t*)codeinst)) {
return;
}
assert(codeinst != NULL); // handle by jl_serialize_generic, but this makes clang-sa happy
int validate = 0;
if (codeinst->max_world == ~(size_t)0)
validate = 1; // can check on deserialize if this cache entry is still valid
int flags = validate << 0;
if (codeinst->invoke == jl_fptr_const_return)
flags |= 1 << 2;
if (codeinst->precompile)
flags |= 1 << 3;
// CodeInstances with PartialOpaque return type are currently not allowed
// to be cached. We skip them in serialization here, forcing them to
// be re-infered on reload.
int write_ret_type = validate || codeinst->min_world == 0;
if (write_ret_type && codeinst->rettype_const &&
jl_typeis(codeinst->rettype_const, jl_partial_opaque_type)) {
if (skip_partial_opaque) {
jl_serialize_code_instance(s, codeinst->next, skip_partial_opaque, internal);
return;
}
else {
jl_error("Cannot serialize CodeInstance with PartialOpaque rettype");
}
}
write_uint8(s->s, TAG_CODE_INSTANCE);
write_uint8(s->s, flags);
write_uint32(s->s, codeinst->ipo_purity_bits);
write_uint32(s->s, codeinst->purity_bits);
jl_serialize_value(s, (jl_value_t*)codeinst->def);
if (write_ret_type) {
jl_serialize_value(s, codeinst->inferred);
jl_serialize_value(s, codeinst->rettype_const);
jl_serialize_value(s, codeinst->rettype);
jl_serialize_value(s, codeinst->argescapes);
}
else {
// skip storing useless data
jl_serialize_value(s, NULL);
jl_serialize_value(s, NULL);
jl_serialize_value(s, jl_any_type);
jl_serialize_value(s, jl_nothing);
}
write_uint8(s->s, codeinst->relocatability);
jl_serialize_code_instance(s, codeinst->next, skip_partial_opaque, internal);
}
enum METHOD_SERIALIZATION_MODE {
METHOD_INTERNAL = 1,
METHOD_EXTERNAL_MT = 2,
METHOD_HAS_NEW_ROOTS = 4,
};
static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_literal) JL_GC_DISABLED
{
if (jl_serialize_generic(s, v)) {
return;
}
size_t i;
if (jl_is_svec(v)) {
size_t l = jl_svec_len(v);
if (l <= 255) {
write_uint8(s->s, TAG_SVEC);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_SVEC);
write_int32(s->s, l);
}
for (i = 0; i < l; i++) {
jl_serialize_value(s, jl_svecref(v, i));
}
}
else if (jl_is_symbol(v)) {
size_t l = strlen(jl_symbol_name((jl_sym_t*)v));
if (l <= 255) {
write_uint8(s->s, TAG_SYMBOL);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_SYMBOL);
write_int32(s->s, l);
}
ios_write(s->s, jl_symbol_name((jl_sym_t*)v), l);
}
else if (jl_is_array(v)) {
jl_array_t *ar = (jl_array_t*)v;
jl_value_t *et = jl_tparam0(jl_typeof(ar));
int isunion = jl_is_uniontype(et);
if (ar->flags.ndims == 1 && ar->elsize <= 0x1f) {
write_uint8(s->s, TAG_ARRAY1D);
write_uint8(s->s, (ar->flags.ptrarray << 7) | (ar->flags.hasptr << 6) | (isunion << 5) | (ar->elsize & 0x1f));
}
else {
write_uint8(s->s, TAG_ARRAY);
write_uint16(s->s, ar->flags.ndims);
write_uint16(s->s, (ar->flags.ptrarray << 15) | (ar->flags.hasptr << 14) | (isunion << 13) | (ar->elsize & 0x1fff));
}
for (i = 0; i < ar->flags.ndims; i++)
jl_serialize_value(s, jl_box_long(jl_array_dim(ar,i)));
jl_serialize_value(s, jl_typeof(ar));
size_t l = jl_array_len(ar);
if (ar->flags.ptrarray) {
for (i = 0; i < l; i++) {
jl_value_t *e = jl_array_ptr_ref(v, i);
if (e && jl_is_cpointer(e) && jl_unbox_voidpointer(e) != (void*)-1 && jl_unbox_voidpointer(e) != NULL)
// reset Ptr elements to C_NULL (but keep MAP_FAILED / INVALID_HANDLE)
jl_serialize_cnull(s, jl_typeof(e));
else
jl_serialize_value(s, e);
}
}
else if (ar->flags.hasptr) {
const char *data = (const char*)jl_array_data(ar);
uint16_t elsz = ar->elsize;
size_t j, np = ((jl_datatype_t*)et)->layout->npointers;
for (i = 0; i < l; i++) {
const char *start = data;
for (j = 0; j < np; j++) {
uint32_t ptr = jl_ptr_offset((jl_datatype_t*)et, j);
const jl_value_t *const *fld = &((const jl_value_t *const *)data)[ptr];
if ((const char*)fld != start)
ios_write(s->s, start, (const char*)fld - start);
JL_GC_PROMISE_ROOTED(*fld);
jl_serialize_value(s, *fld);
start = (const char*)&fld[1];
}
data += elsz;
if (data != start)
ios_write(s->s, start, data - start);
}
}
else if (jl_is_cpointer_type(et)) {
// reset Ptr elements to C_NULL
const void **data = (const void**)jl_array_data(ar);
for (i = 0; i < l; i++) {
const void *e = data[i];
if (e != (void*)-1)
e = NULL;
ios_write(s->s, (const char*)&e, sizeof(e));
}
}
else {
ios_write(s->s, (char*)jl_array_data(ar), l * ar->elsize);
if (jl_array_isbitsunion(ar))
ios_write(s->s, jl_array_typetagdata(ar), l);
}
}
else if (jl_is_datatype(v)) {
jl_serialize_datatype(s, (jl_datatype_t*)v);
}
else if (jl_is_unionall(v)) {
write_uint8(s->s, TAG_UNIONALL);
jl_datatype_t *d = (jl_datatype_t*)jl_unwrap_unionall(v);
if (jl_is_datatype(d) && d->name->wrapper == v &&
!module_in_worklist(d->name->module)) {
write_uint8(s->s, 1);
jl_serialize_value(s, d->name->module);
jl_serialize_value(s, d->name->name);
}
else {
write_uint8(s->s, 0);
jl_serialize_value(s, ((jl_unionall_t*)v)->var);
jl_serialize_value(s, ((jl_unionall_t*)v)->body);
}
}
else if (jl_is_typevar(v)) {
write_uint8(s->s, TAG_TVAR);
jl_serialize_value(s, ((jl_tvar_t*)v)->name);
jl_serialize_value(s, ((jl_tvar_t*)v)->lb);
jl_serialize_value(s, ((jl_tvar_t*)v)->ub);
}
else if (jl_is_method(v)) {
write_uint8(s->s, TAG_METHOD);
jl_method_t *m = (jl_method_t*)v;
uint64_t key = 0;
int serialization_mode = 0, nwithkey = 0;
if (m->is_for_opaque_closure || module_in_worklist(m->module))
serialization_mode |= METHOD_INTERNAL;
if (!(serialization_mode & METHOD_INTERNAL)) {
key = jl_worklist_key(serializer_worklist);
nwithkey = nroots_with_key(m, key);
if (nwithkey > 0)
serialization_mode |= METHOD_HAS_NEW_ROOTS;
}
if (!(serialization_mode & METHOD_INTERNAL)) {
// flag this in the backref table as special
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, v);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1;
}
jl_serialize_value(s, (jl_value_t*)m->sig);
jl_serialize_value(s, (jl_value_t*)m->module);
if (m->external_mt != NULL) {
assert(jl_typeis(m->external_mt, jl_methtable_type));
jl_methtable_t *mt = (jl_methtable_t*)m->external_mt;
if (!module_in_worklist(mt->module)) {
serialization_mode |= METHOD_EXTERNAL_MT;
}
}
write_uint8(s->s, serialization_mode);
if (serialization_mode & METHOD_EXTERNAL_MT) {
// We reference this method table by module and binding
jl_methtable_t *mt = (jl_methtable_t*)m->external_mt;
jl_serialize_value(s, mt->module);
jl_serialize_value(s, mt->name);
}
else {
jl_serialize_value(s, (jl_value_t*)m->external_mt);
}
if (!(serialization_mode & METHOD_INTERNAL)) {
if (serialization_mode & METHOD_HAS_NEW_ROOTS) {
// Serialize the roots that belong to key
write_uint64(s->s, key);
write_int32(s->s, nwithkey);
rle_iter_state rootiter = rle_iter_init(0);
uint64_t *rletable = NULL;
size_t nblocks2 = 0, nroots = jl_array_len(m->roots);
if (m->root_blocks) {
rletable = (uint64_t*)jl_array_data(m->root_blocks);
nblocks2 = jl_array_len(m->root_blocks);
}
// this visits every item, if it becomes a bottlneck we could hop blocks
while (rle_iter_increment(&rootiter, nroots, rletable, nblocks2))
if (rootiter.key == key)
jl_serialize_value(s, jl_array_ptr_ref(m->roots, rootiter.i));
}
return;
}
jl_serialize_value(s, m->specializations);
jl_serialize_value(s, jl_atomic_load_relaxed(&m->speckeyset));
jl_serialize_value(s, (jl_value_t*)m->name);
jl_serialize_value(s, (jl_value_t*)m->file);
write_int32(s->s, m->line);
write_int32(s->s, m->called);
write_int32(s->s, m->nargs);
write_int32(s->s, m->nospecialize);
write_int32(s->s, m->nkw);
write_int8(s->s, m->isva);
write_int8(s->s, m->pure);
write_int8(s->s, m->is_for_opaque_closure);
write_int8(s->s, m->constprop);
write_uint8(s->s, m->purity.bits);
jl_serialize_value(s, (jl_value_t*)m->slot_syms);
jl_serialize_value(s, (jl_value_t*)m->roots);
jl_serialize_value(s, (jl_value_t*)m->root_blocks);
write_int32(s->s, m->nroots_sysimg);
jl_serialize_value(s, (jl_value_t*)m->ccallable);
jl_serialize_value(s, (jl_value_t*)m->source);
jl_serialize_value(s, (jl_value_t*)m->unspecialized);
jl_serialize_value(s, (jl_value_t*)m->generator);
jl_serialize_value(s, (jl_value_t*)m->invokes);
jl_serialize_value(s, (jl_value_t*)m->recursion_relation);
}
else if (jl_is_method_instance(v)) {
jl_method_instance_t *mi = (jl_method_instance_t*)v;
if (jl_is_method(mi->def.value) && mi->def.method->is_for_opaque_closure) {
jl_error("unimplemented: serialization of MethodInstances for OpaqueClosure");
}
write_uint8(s->s, TAG_METHOD_INSTANCE);
int internal = 0;
if (!jl_is_method(mi->def.method))
internal = 1;
else if (module_in_worklist(mi->def.method->module))
internal = 2;
else if (ptrhash_get(&external_mis, (void*)mi) != HT_NOTFOUND)
internal = 3;
write_uint8(s->s, internal);
if (!internal) {
// also flag this in the backref table as special
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, v);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1;
}
if (internal == 1)
jl_serialize_value(s, (jl_value_t*)mi->uninferred);
jl_serialize_value(s, (jl_value_t*)mi->specTypes);
jl_serialize_value(s, mi->def.value);
if (!internal)
return;
jl_serialize_value(s, (jl_value_t*)mi->sparam_vals);
jl_array_t *backedges = mi->backedges;
if (backedges) {
// filter backedges to only contain pointers
// to items that we will actually store (internal >= 2)
size_t ins, i, l = jl_array_len(backedges);
jl_method_instance_t **b_edges = (jl_method_instance_t**)jl_array_data(backedges);
for (ins = i = 0; i < l; i++) {
jl_method_instance_t *backedge = b_edges[i];
if (module_in_worklist(backedge->def.method->module) || method_instance_in_queue(backedge)) {
b_edges[ins++] = backedge;
}
}
if (ins != l)
jl_array_del_end(backedges, l - ins);
if (ins == 0)
backedges = NULL;
}
jl_serialize_value(s, (jl_value_t*)backedges);
jl_serialize_value(s, (jl_value_t*)NULL); //callbacks
jl_serialize_code_instance(s, mi->cache, 1, internal);
}
else if (jl_is_code_instance(v)) {
jl_serialize_code_instance(s, (jl_code_instance_t*)v, 0, 2);
}
else if (jl_typeis(v, jl_module_type)) {
jl_serialize_module(s, (jl_module_t*)v);
}
else if (jl_typeis(v, jl_task_type)) {
jl_error("Task cannot be serialized");
}
else if (jl_typeis(v, jl_opaque_closure_type)) {
jl_error("Live opaque closures cannot be serialized");
}
else if (jl_typeis(v, jl_string_type)) {
write_uint8(s->s, TAG_STRING);
write_int32(s->s, jl_string_len(v));
ios_write(s->s, jl_string_data(v), jl_string_len(v));
}
else if (jl_typeis(v, jl_int64_type)) {
void *data = jl_data_ptr(v);
if (*(int64_t*)data >= INT16_MIN && *(int64_t*)data <= INT16_MAX) {
write_uint8(s->s, TAG_SHORTER_INT64);
write_uint16(s->s, (uint16_t)*(int64_t*)data);
}
else if (*(int64_t*)data >= S32_MIN && *(int64_t*)data <= S32_MAX) {
write_uint8(s->s, TAG_SHORT_INT64);
write_int32(s->s, (int32_t)*(int64_t*)data);
}
else {
write_uint8(s->s, TAG_INT64);
write_int64(s->s, *(int64_t*)data);
}
}
else if (jl_typeis(v, jl_int32_type)) {
void *data = jl_data_ptr(v);
if (*(int32_t*)data >= INT16_MIN && *(int32_t*)data <= INT16_MAX) {