-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathexternal.c
6839 lines (6126 loc) · 198 KB
/
external.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2024. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/* Implementation of the erlang external format
*
* And a nice cache mechanism which is used just to send a
* index indicating a specific atom to a remote node instead of the
* entire atom.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#define ERTS_WANT_EXTERNAL_TAGS
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "external.h"
#include "bif.h"
#include "big.h"
#include "dist.h"
#include "erl_binary.h"
#include "erl_bits.h"
#include "erl_zlib.h"
#include "erl_map.h"
#include "erl_proc_sig_queue.h"
#include "erl_trace.h"
#include "erl_global_literals.h"
#include "erl_term_hashing.h"
#define PASS_THROUGH 'p'
#define in_area(ptr,start,nbytes) ((UWord)((char*)(ptr) - (char*)(start)) < (nbytes))
#define MAX_STRING_LEN 0xffff
/*
* MAX value for the creation field in pid, port and reference
* for the old PID_EXT, PORT_EXT, REFERENCE_EXT and NEW_REFERENCE_EXT.
* Older nodes (OTP 19-22) will send us these so we must be able to decode them.
*
* From OTP 23 DFLAG_BIG_CREATION is mandatory so this node will always
* encode with new big 32-bit creations using NEW_PID_EXT, NEW_PORT_EXT
* and NEWER_REFERENCE_EXT.
*/
#define ERTS_MAX_TINY_CREATION (3)
#define is_tiny_creation(Cre) ((unsigned)(Cre) <= ERTS_MAX_TINY_CREATION)
#undef ERTS_DEBUG_USE_DIST_SEP
#ifdef DEBUG
# if 0
/*
* Enabling ERTS_DEBUG_USE_DIST_SEP can be useful when debugging, but the
* result refuses to talk to nodes without it!
*/
# define ERTS_DEBUG_USE_DIST_SEP
# endif
# define IF_DEBUG(X) X
#else
# define IF_DEBUG(X)
#endif
/* Does Sint fit in Sint32?
*/
#define IS_SSMALL32(x) (((Uint) (((x) >> (32-1)) + 1)) < 2)
static Export term_to_binary_trap_export;
static byte* enc_term(ErtsAtomCacheMap *, Eterm, byte*, Uint64, struct erl_off_heap_header** off_heap);
struct TTBEncodeContext_;
static int enc_term_int(struct TTBEncodeContext_*,ErtsAtomCacheMap *acmp, Eterm obj, byte* ep, Uint64 dflags,
struct erl_off_heap_header** off_heap, Sint *reds, byte **res);
static int is_external_string(Eterm obj, Uint* lenp);
static byte* enc_atom(ErtsAtomCacheMap *, Eterm, byte*, Uint64);
static byte* enc_pid(ErtsAtomCacheMap *, Eterm, byte*, Uint64);
struct B2TContext_t;
static const byte* dec_term(ErtsDistExternal*, ErtsHeapFactory*, const byte*, Eterm*, struct B2TContext_t*, int);
static const byte* dec_atom(ErtsDistExternal *, const byte*, Eterm*, int);
static const byte* dec_pid(ErtsDistExternal *, ErtsHeapFactory*, const byte*, Eterm*, byte tag, int);
static Sint decoded_size(const byte *ep, const byte* endp, int internal_tags, struct B2TContext_t*);
static BIF_RETTYPE term_to_binary_trap_1(BIF_ALIST_1);
static Eterm erts_term_to_binary_int(Process* p, Sint bif_ix, Eterm Term, Eterm opts, int level,
Uint64 dflags, Binary *context_b, int iovec,
Uint fragment_size);
static ErtsExtSzRes encode_size_struct_int(TTBSizeContext*, ErtsAtomCacheMap *acmp,
Eterm obj, Uint64 dflags, Sint *reds, Uint *res);
static Export binary_to_term_trap_export;
static BIF_RETTYPE binary_to_term_trap_1(BIF_ALIST_1);
static Sint transcode_dist_obuf(ErtsDistOutputBuf*, DistEntry*, Uint64 dflags, Sint reds);
static void store_in_vec(TTBEncodeContext *ctx, byte *ep, Binary *ohbin, Eterm ohpb,
byte *ohp, Uint ohsz);
static Uint32 calc_iovec_fun_size(SysIOVec* iov, Uint32 fun_high_ix, byte* size_p);
void erts_init_external(void) {
erts_init_trap_export(&term_to_binary_trap_export,
am_erts_internal, am_term_to_binary_trap, 1,
&term_to_binary_trap_1);
erts_init_trap_export(&binary_to_term_trap_export,
am_erts_internal, am_binary_to_term_trap, 1,
&binary_to_term_trap_1);
return;
}
static Uint32 local_node_hash;
void erts_late_init_external(void)
{
char hname[256], pidstr[21];
size_t hname_len, pidstr_len;
ErtsMonotonicTime mtime, toffs;
ErtsBlockHashState hstate;
byte *lnid;
Uint lnid_ix, chunk_size;
int res;
res = sys_get_hostname(&hname[0], sizeof(hname));
if (res == 0) {
hname_len = strlen(hname);
}
else {
hname[0] = '\0';
hname_len = 0;
}
sys_get_pid(&pidstr[0], sizeof(pidstr));
pidstr[20] = '\0';
pidstr_len = strlen(pidstr);
toffs = erts_get_time_offset();
mtime = erts_get_monotonic_time(NULL);
lnid = (byte *) erts_alloc(ERTS_ALC_T_TMP, 8 + hname_len + pidstr_len);
lnid_ix = 0;
/* time offset... */
lnid[lnid_ix++] = (byte) toffs & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 8) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 16) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 24) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 32) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 40) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 48) & 0xff;
lnid[lnid_ix++] = (byte) (toffs >> 56) & 0xff;
/* hostname... */
sys_memcpy(&lnid[lnid_ix], &hname[0], hname_len);
lnid_ix += hname_len;
/* pid... */
memcpy(&lnid[lnid_ix], &pidstr[0], pidstr_len);
lnid_ix += pidstr_len;
/*
* Use least significant 32 bits of monotonic time as initial
* value to hash...
*/
erts_block_hash_init(&hstate, &lnid[0], lnid_ix,
(Uint32) (mtime & 0xffffffff));
chunk_size = ERTS_UINT_MAX;
res = erts_block_hash(&local_node_hash, &chunk_size, &hstate);
ASSERT(res); (void) res;
ASSERT(chunk_size == lnid_ix);
erts_free(ERTS_ALC_T_TMP, lnid);
}
#define ERTS_MAX_INTERNAL_ATOM_CACHE_ENTRIES 255
#define ERTS_DIST_HDR_ATOM_CACHE_FLAG_BYTE_IX(IIX) \
(((((Uint32) (IIX)) >> 1) & 0x7fffffff))
#define ERTS_DIST_HDR_ATOM_CACHE_FLAG_BIT_IX(IIX) \
(((IIX) << 2) & 7)
#define ERTS_DIST_HDR_ATOM_CACHE_FLAG_BYTES(NO_ATOMS) \
(((((Uint32) (NO_ATOMS)) >> 1) & 0x7fffffff)+1)
#define ERTS_DIST_HDR_LONG_ATOMS_FLG (1 << 0)
/* #define ERTS_ATOM_CACHE_HASH */
#define ERTS_USE_ATOM_CACHE_SIZE 2039
#if ERTS_ATOM_CACHE_SIZE < ERTS_USE_ATOM_CACHE_SIZE
#error "ERTS_USE_ATOM_CACHE_SIZE too large"
#endif
static ERTS_INLINE int
atom2cix(Eterm atom)
{
Uint val;
ASSERT(is_atom(atom));
val = atom_val(atom);
#ifdef ERTS_ATOM_CACHE_HASH
val = atom_tab(val)->slot.bucket.hvalue;
#endif
#if ERTS_USE_ATOM_CACHE_SIZE == 256
return (int) (val & ((Uint) 0xff));
#else
return (int) (val % ERTS_USE_ATOM_CACHE_SIZE);
#endif
}
int erts_debug_max_atom_out_cache_index(void)
{
return ERTS_USE_ATOM_CACHE_SIZE-1;
}
int
erts_debug_atom_to_out_cache_index(Eterm atom)
{
return atom2cix(atom);
}
void
erts_init_atom_cache_map(ErtsAtomCacheMap *acmp)
{
if (acmp) {
int ix;
acmp->long_atoms = 0;
for (ix = 0; ix < ERTS_ATOM_CACHE_SIZE; ix++)
acmp->cache[ix].iix = -1;
acmp->sz = 0;
acmp->hdr_sz = -1;
}
}
void
erts_reset_atom_cache_map(ErtsAtomCacheMap *acmp)
{
if (acmp) {
int i;
acmp->long_atoms = 0;
for (i = 0; i < acmp->sz; i++) {
ASSERT(0 <= acmp->cix[i] && acmp->cix[i] < ERTS_ATOM_CACHE_SIZE);
acmp->cache[acmp->cix[i]].iix = -1;
}
acmp->sz = 0;
acmp->hdr_sz = -1;
#ifdef DEBUG
for (i = 0; i < ERTS_ATOM_CACHE_SIZE; i++) {
ASSERT(acmp->cache[i].iix < 0);
}
#endif
}
}
void
erts_destroy_atom_cache_map(ErtsAtomCacheMap *acmp)
{
}
static ERTS_INLINE void
insert_acache_map(ErtsAtomCacheMap *acmp, Eterm atom, Uint64 dflags)
{
if (acmp && acmp->sz < ERTS_MAX_INTERNAL_ATOM_CACHE_ENTRIES) {
int ix;
ASSERT(acmp->hdr_sz < 0);
ix = atom2cix(atom);
if (acmp->cache[ix].iix < 0) {
acmp->cache[ix].iix = acmp->sz;
acmp->cix[acmp->sz++] = ix;
acmp->cache[ix].atom = atom;
}
}
}
static ERTS_INLINE int
get_iix_acache_map(ErtsAtomCacheMap *acmp, Eterm atom, Uint64 dflags)
{
if (!acmp)
return -1;
else {
int ix;
ASSERT(is_atom(atom));
ix = atom2cix(atom);
if (acmp->cache[ix].iix < 0) {
ASSERT(acmp->sz == ERTS_MAX_INTERNAL_ATOM_CACHE_ENTRIES);
return -1;
}
else {
ASSERT(acmp->cache[ix].iix < ERTS_ATOM_CACHE_SIZE);
return acmp->cache[ix].atom == atom ? acmp->cache[ix].iix : -1;
}
}
}
void
erts_finalize_atom_cache_map(ErtsAtomCacheMap *acmp, Uint64 dflags)
{
if (acmp) {
int long_atoms = 0; /* !0 if one or more atoms are longer than 255. */
int i;
int sz = 0;
int min_sz;
ASSERT(acmp->hdr_sz < 0);
/* Make sure cache update instructions fit */
min_sz = (2+4)*acmp->sz;
for (i = 0; i < acmp->sz; i++) {
Atom *a;
Eterm atom;
int len;
atom = acmp->cache[acmp->cix[i]].atom;
ASSERT(is_atom(atom));
a = atom_tab(atom_val(atom));
len = (int) a->len;
ASSERT(len >= 0);
if (!long_atoms && len > 255)
long_atoms = 1;
/* Enough for a new atom cache value */
sz += 1 /* cix */ + 1 /* length */ + len /* text */;
}
if (long_atoms) {
acmp->long_atoms = 1;
sz += acmp->sz; /* we need 2 bytes per atom for length */
}
/* Dynamically sized flag field */
sz += ERTS_DIST_HDR_ATOM_CACHE_FLAG_BYTES(acmp->sz);
if (sz < min_sz)
sz = min_sz;
acmp->hdr_sz = sz;
}
}
Uint
erts_encode_ext_dist_header_size(TTBEncodeContext *ctx,
ErtsAtomCacheMap *acmp,
Uint fragments)
{
if (ctx->dflags & DFLAG_PENDING_CONNECT) {
/* HOPEFUL_DATA + hopefull flags + hopefull ix + payload ix */
return 1 + 8 + 4 + 4;
}
else if (!acmp && !(ctx->dflags & DFLAG_FRAGMENTS))
return 1; /* pass through */
else {
int fix_sz
= 1 /* VERSION_MAGIC */
+ 1 /* DIST_HEADER */
+ 1 /* dist header flags */
+ 1 /* number of internal cache entries */
;
if (fragments > 1)
fix_sz += 8 /* sequence id */
+ 8 /* number of fragments */
;
if (acmp) {
ASSERT(acmp->hdr_sz >= 0);
fix_sz += acmp->hdr_sz;
} else {
ASSERT(ctx->dflags & DFLAG_FRAGMENTS);
}
return fix_sz;
}
}
byte *erts_encode_ext_dist_header_setup(TTBEncodeContext *ctx,
byte *ctl_ext, ErtsAtomCacheMap *acmp,
Uint fragments, Eterm from)
{
/* Maximum number of atom must be less than the maximum of a 32 bits
unsigned integer. Check is done in erl_init.c, erl_start function. */
if (ctx->dflags & DFLAG_PENDING_CONNECT) {
byte *ep = ctl_ext;
ep -= 4;
ctx->payload_ixp = ep;
put_int32(0, ep);
ep -= 4;
ctx->hopefull_ixp = ep;
put_int32(ERTS_NO_HIX, ep);
ep -= 8;
ctx->hopefull_flagsp = ep;
put_int64(0, ep);
*--ep = HOPEFUL_DATA;
return ep;
}
else if (!acmp && !(ctx->dflags & DFLAG_FRAGMENTS)) {
byte *ep = ctl_ext;
*--ep = PASS_THROUGH;
return ep;
}
else {
int i;
byte *ep = ctl_ext;
byte dist_hdr_flags = acmp && acmp->long_atoms ? ERTS_DIST_HDR_LONG_ATOMS_FLG : 0;
ASSERT(!acmp || acmp->hdr_sz >= 0);
if (acmp) {
/*
* Write cache update instructions. Note that this is a purely
* internal format, never seen on the wire. This section is later
* rewritten by erts_encode_ext_dist_header_finalize() while updating
* the cache. We write the header backwards just before the
* actual term(s).
*/
for (i = acmp->sz-1; i >= 0; i--) {
Uint32 aval;
ASSERT(0 <= acmp->cix[i] && acmp->cix[i] < ERTS_ATOM_CACHE_SIZE);
ASSERT(i == acmp->cache[acmp->cix[i]].iix);
ASSERT(is_atom(acmp->cache[acmp->cix[i]].atom));
aval = (Uint32) atom_val(acmp->cache[acmp->cix[i]].atom);
ep -= 4;
put_int32(aval, ep);
ep -= 2;
put_int16(acmp->cix[i], ep);
}
--ep;
put_int8(acmp->sz, ep);
} else {
ASSERT(ctx->dflags & DFLAG_FRAGMENTS);
/* If we don't have an atom cache but are using a dist header we just put 0
in the atom cache size slot */
--ep;
put_int8(0, ep);
}
--ep;
put_int8(dist_hdr_flags, ep);
if (fragments > 1) {
ASSERT(is_pid(from));
ep -= 8;
put_int64(fragments, ep);
ep -= 8;
put_int64(from, ep);
*--ep = DIST_FRAG_HEADER;
} else {
*--ep = DIST_HEADER;
}
*--ep = VERSION_MAGIC;
return ep;
}
}
byte *erts_encode_ext_dist_header_fragment(byte **hdrpp,
Uint fragment,
Eterm from)
{
byte *ep = *hdrpp, *start = ep;
ASSERT(is_pid(from));
*ep++ = VERSION_MAGIC;
*ep++ = DIST_FRAG_CONT;
put_int64(from, ep);
ep += 8;
put_int64(fragment, ep);
ep += 8;
*hdrpp = ep;
return start;
}
Sint erts_encode_ext_dist_header_finalize(ErtsDistOutputBuf* ob,
DistEntry* dep,
Uint64 dflags,
Sint reds)
{
byte *ip;
byte instr_buf[(2+4)*ERTS_ATOM_CACHE_SIZE];
int ci, sz;
byte dist_hdr_flags;
int long_atoms;
Uint64 seq_id = 0, frag_id = 0;
register byte *ep = ob->eiov->iov[1].iov_base;
/*
* The buffer can have different layouts at this point depending on
* what was known when encoded:
*
* Pending connection: HOPEFUL_DATA, HFlgs, HIX, PIX, CtrlTerm [, MsgTerm]
* With atom cache : VERSION_MAGIC, DIST_HEADER, ..., CtrlTerm [, MsgTerm]
* No atom cache : VERSION_MAGIC, CtrlTerm [, VERSION_MAGIC, MsgTerm]
*/
if (ep[0] == HOPEFUL_DATA)
return transcode_dist_obuf(ob, dep, dflags, reds);
if (ep[0] == PASS_THROUGH) {
ASSERT(!(dflags & (DFLAG_DIST_HDR_ATOM_CACHE|DFLAG_FRAGMENTS)));
ASSERT(ob->eiov->iov[1].iov_len == 1);
return reds;
}
if (ep[1] == DIST_FRAG_CONT) {
ASSERT(ep[0] == VERSION_MAGIC);
ASSERT(ob->eiov->iov[1].iov_len == 18);
return reds;
}
if (ep[1] == DIST_FRAG_HEADER) {
/* skip the seq id and frag id */
seq_id = get_int64(&ep[2]);
ep += 8;
frag_id = get_int64(&ep[2]);
ep += 8;
}
dist_hdr_flags = ep[2];
long_atoms = ERTS_DIST_HDR_LONG_ATOMS_FLG & ((int) dist_hdr_flags);
/*
* Update output atom cache and write the external version of
* the dist header. We write the header backwards just
* before the actual term(s).
*/
ep += 3;
ci = (int) get_int8(ep);
ASSERT(0 <= ci && ci < ERTS_ATOM_CACHE_SIZE);
ep += 1;
sz = (2+4)*ci;
ip = &instr_buf[0];
sys_memcpy((void *) ip, (void *) ep, sz);
ep += sz;
ASSERT(ep == &((byte *)ob->eiov->iov[1].iov_base)[ob->eiov->iov[1].iov_len]);
if (ci > 0) {
Uint32 flgs_buf[((ERTS_DIST_HDR_ATOM_CACHE_FLAG_BYTES(
ERTS_MAX_INTERNAL_ATOM_CACHE_ENTRIES)-1)
/ sizeof(Uint32))+1];
register Uint32 flgs;
int iix, flgs_bytes, flgs_buf_ix, used_half_bytes;
ErtsAtomCache* cache = dep->cache;
#ifdef DEBUG
int tot_used_half_bytes, top_buf_ix;
#endif
flgs_bytes = ERTS_DIST_HDR_ATOM_CACHE_FLAG_BYTES(ci);
ASSERT(flgs_bytes <= sizeof(flgs_buf));
flgs = (Uint32) dist_hdr_flags;
flgs_buf_ix = 0;
if ((ci & 1) == 0)
used_half_bytes = 2;
else
used_half_bytes = 1;
#ifdef DEBUG
tot_used_half_bytes = used_half_bytes;
#endif
iix = ci-1;
while (iix >= 0) {
int cix;
Eterm atom;
if (used_half_bytes != 8)
flgs <<= 4;
else {
flgs_buf[flgs_buf_ix++] = flgs;
flgs = 0;
used_half_bytes = 0;
}
ip = &instr_buf[0] + (2+4)*iix;
cix = (int) get_int16(&ip[0]);
ASSERT(0 <= cix && cix < ERTS_ATOM_CACHE_SIZE);
atom = make_atom((Uint) get_uint32(&ip[2]));
if (cache->out_arr[cix] == atom) {
--ep;
put_int8(cix, ep);
flgs |= ((cix >> 8) & 7);
}
else {
Atom *a;
cache->out_arr[cix] = atom;
a = atom_tab(atom_val(atom));
sz = a->len;
ep -= sz;
sys_memcpy((void *) ep, (void *) erts_atom_get_name(a), sz);
if (long_atoms) {
ep -= 2;
put_int16(sz, ep);
}
else {
ASSERT(0 <= sz && sz <= 255);
--ep;
put_int8(sz, ep);
}
--ep;
put_int8(cix, ep);
flgs |= (8 | ((cix >> 8) & 7));
}
iix--;
used_half_bytes++;
#ifdef DEBUG
tot_used_half_bytes++;
#endif
}
ASSERT(tot_used_half_bytes == 2*flgs_bytes);
flgs_buf[flgs_buf_ix] = flgs;
#ifdef DEBUG
top_buf_ix = flgs_buf_ix;
#endif
flgs_buf_ix = 0;
while (1) {
ASSERT(flgs_buf_ix <= top_buf_ix);
flgs = flgs_buf[flgs_buf_ix];
if (flgs_bytes > 4) {
*--ep = (byte) ((flgs >> 24) & 0xff);
*--ep = (byte) ((flgs >> 16) & 0xff);
*--ep = (byte) ((flgs >> 8) & 0xff);
*--ep = (byte) (flgs & 0xff);
flgs_buf_ix++;
flgs_bytes -= 4;
}
else {
ASSERT(flgs_buf_ix == top_buf_ix);
switch (flgs_bytes) {
case 4:
*--ep = (byte) ((flgs >> 24) & 0xff);
ERTS_FALLTHROUGH();
case 3:
*--ep = (byte) ((flgs >> 16) & 0xff);
ERTS_FALLTHROUGH();
case 2:
*--ep = (byte) ((flgs >> 8) & 0xff);
ERTS_FALLTHROUGH();
case 1:
*--ep = (byte) (flgs & 0xff);
}
break;
}
}
reds -= 3; /*was ERTS_PORT_REDS_DIST_CMD_FINALIZE*/
}
--ep;
put_int8(ci, ep);
if (seq_id) {
ep -= 8;
put_int64(frag_id, ep);
ep -= 8;
put_int64(seq_id, ep);
*--ep = DIST_FRAG_HEADER;
} else {
*--ep = DIST_HEADER;
}
*--ep = VERSION_MAGIC;
sz = ((byte *) ob->eiov->iov[1].iov_base) - ep;
ob->eiov->size += sz;
ob->eiov->iov[1].iov_len += sz;
ob->eiov->iov[1].iov_base = ep;
return reds < 0 ? 0 : reds;
}
ErtsExtSzRes
erts_encode_dist_ext_size(Eterm term,
ErtsAtomCacheMap *acmp,
TTBSizeContext* ctx,
Uint* szp, Sint *redsp,
Sint *vlenp, Uint *fragmentsp)
{
Uint sz;
ErtsExtSzRes res;
ASSERT(ctx);
ASSERT(szp);
ASSERT(vlenp);
ASSERT(fragmentsp);
sz = *szp;
if (!ctx->wstack.wstart) {
/*
* First call for this 'term'. We might however encode
* multiple terms and this might not be the first term
* in the sequence. 'ctx' should contain valid info about
* about previous terms regarding fragments, and vlen.
* 'szp' should contain valid info about the total size
* of previous terms.
*/
if (ctx->vlen < 0) {
/* First term as well */
ctx->vlen = 0;
if (ctx->dflags & DFLAG_FRAGMENTS)
ctx->fragment_size = ERTS_DIST_FRAGMENT_SIZE;
}
#ifndef ERTS_DEBUG_USE_DIST_SEP
if (!(ctx->dflags & (DFLAG_DIST_HDR_ATOM_CACHE|DFLAG_FRAGMENTS)))
#endif
sz++ /* VERSION_MAGIC */;
}
res = encode_size_struct_int(ctx, acmp, term, ctx->dflags, redsp, &sz);
if (res == ERTS_EXT_SZ_OK) {
Uint total_size, fragments;
/*
* Each fragment use
* - one element for driver header
* - one element for fragment header
* - and (at least) one for data
*/
total_size = sz + ctx->extra_size;
fragments = (total_size - 1)/ctx->fragment_size + 1;
*szp = sz;
*fragmentsp = fragments;
*vlenp = ctx->vlen + 3*fragments;
}
return res;
}
ErtsExtSzRes erts_encode_ext_size_2(Eterm term, Uint64 dflags, Uint *szp)
{
ErtsExtSzRes res;
*szp = 0;
res = encode_size_struct_int(NULL, NULL, term, dflags, NULL, szp);
(*szp)++ /* VERSION_MAGIC */;
return res;
}
ErtsExtSzRes erts_encode_ext_size(Eterm term, Uint *szp)
{
return erts_encode_ext_size_2(term, TERM_TO_BINARY_DFLAGS, szp);
}
Uint erts_encode_ext_size_ets(Eterm term)
{
ErtsExtSzRes res;
Uint sz = 0;
res = encode_size_struct_int(NULL, NULL, term,
TERM_TO_BINARY_DFLAGS|DFLAG_ETS_COMPRESSED,
NULL, &sz);
ASSERT(res == ERTS_EXT_SZ_OK); (void) res;
return sz;
}
int erts_encode_dist_ext(Eterm term, byte **ext, Uint64 flags, ErtsAtomCacheMap *acmp,
TTBEncodeContext* ctx, Uint *fragmentsp, Sint* reds)
{
int res;
ASSERT(ctx);
if (!ctx->wstack.wstart) {
ctx->cptr = *ext;
#ifndef ERTS_DEBUG_USE_DIST_SEP
if (!(flags & (DFLAG_DIST_HDR_ATOM_CACHE|DFLAG_PENDING_CONNECT|DFLAG_FRAGMENTS)))
#endif
*(*ext)++ = VERSION_MAGIC;
#ifndef ERTS_DEBUG_USE_DIST_SEP
if (flags & DFLAG_PENDING_CONNECT) {
Sint payload_ix = ctx->vlen;
ASSERT(ctx->payload_ixp);
if (payload_ix) {
/* we potentially need a version magic on the payload... */
(*ext)++;
ctx->cptr = *ext;
put_int32(payload_ix, ctx->payload_ixp);
}
}
#endif
}
res = enc_term_int(ctx, acmp, term, *ext, flags, NULL, reds, ext);
if (fragmentsp)
*fragmentsp = res == 0 ? ctx->frag_ix + 1 : ctx->frag_ix;
if (flags & DFLAG_PENDING_CONNECT) {
ASSERT(ctx->hopefull_flagsp);
put_int64(ctx->hopefull_flags, ctx->hopefull_flagsp);
}
return res;
}
void erts_encode_ext(Eterm term, byte **ext)
{
byte *ep = *ext;
*ep++ = VERSION_MAGIC;
ep = enc_term(NULL, term, ep, TERM_TO_BINARY_DFLAGS, NULL);
if (!ep)
erts_exit(ERTS_ABORT_EXIT,
"%s:%d:erts_encode_ext(): Internal data structure error\n",
__FILE__, __LINE__);
*ext = ep;
}
byte* erts_encode_ext_ets(Eterm term, byte *ep, struct erl_off_heap_header** off_heap)
{
return enc_term(NULL, term, ep, TERM_TO_BINARY_DFLAGS|DFLAG_ETS_COMPRESSED,
off_heap);
}
static Uint
dist_ext_size(ErtsDistExternal *edep)
{
Uint sz = sizeof(ErtsDistExternal);
ASSERT(edep->data->ext_endp && edep->data->extp);
ASSERT(edep->data->ext_endp >= edep->data->extp);
if (edep->flags & ERTS_DIST_EXT_ATOM_TRANS_TAB) {
ASSERT(0 <= edep->attab.size \
&& edep->attab.size <= ERTS_ATOM_CACHE_SIZE);
sz -= sizeof(Eterm)*(ERTS_ATOM_CACHE_SIZE - edep->attab.size);
} else {
sz -= sizeof(ErtsAtomTranslationTable);
}
ASSERT(sz % 4 == 0);
return sz;
}
Uint
erts_dist_ext_size(ErtsDistExternal *edep)
{
Uint sz = dist_ext_size(edep);
sz += 4; /* may need to pad to 8-byte-align ErtsDistExternalData */
sz += edep->data[0].frag_id * sizeof(ErtsDistExternalData);
return sz;
}
Uint
erts_dist_ext_data_size(ErtsDistExternal *edep)
{
Uint sz = 0, i;
for (i = 0; i < edep->data->frag_id; i++)
sz += edep->data[i].ext_endp - edep->data[i].extp;
return sz;
}
void
erts_dist_ext_frag(ErtsDistExternalData *ede_datap, ErtsDistExternal *edep)
{
ErtsDistExternalData *new_ede_datap = &edep->data[edep->data->frag_id - ede_datap->frag_id];
sys_memcpy(new_ede_datap, ede_datap, sizeof(ErtsDistExternalData));
/* If the data is not backed by a binary, we create one here to keep
things simple. Only custom distribution drivers should use lists. */
if (new_ede_datap->binp == NULL) {
size_t ext_sz = ede_datap->ext_endp - ede_datap->extp;
new_ede_datap->binp = erts_bin_nrml_alloc(ext_sz);
sys_memcpy(new_ede_datap->binp->orig_bytes, (void *) ede_datap->extp, ext_sz);
new_ede_datap->extp = (byte*)new_ede_datap->binp->orig_bytes;
new_ede_datap->ext_endp = (byte*)new_ede_datap->binp->orig_bytes + ext_sz;
} else {
erts_refc_inc(&new_ede_datap->binp->intern.refc, 2);
}
}
void
erts_make_dist_ext_copy(ErtsDistExternal *edep, ErtsDistExternal *new_edep)
{
size_t dist_ext_sz = dist_ext_size(edep);
byte *ep;
ep = (byte *) new_edep;
sys_memcpy((void *) ep, (void *) edep, dist_ext_sz);
erts_ref_dist_entry(new_edep->dep);
ep += dist_ext_sz;
ep += (UWord)ep & 4; /* 8-byte alignment for ErtsDistExternalData */
ASSERT((UWord)ep % 8 == 0);
new_edep->data = (ErtsDistExternalData*)ep;
sys_memzero(new_edep->data, sizeof(ErtsDistExternalData) * edep->data->frag_id);
new_edep->data->frag_id = edep->data->frag_id;
erts_dist_ext_frag(edep->data, new_edep);
}
void
erts_free_dist_ext_copy(ErtsDistExternal *edep)
{
int i;
erts_deref_dist_entry(edep->dep);
for (i = 0; i < edep->data->frag_id; i++)
if (edep->data[i].binp)
erts_bin_release(edep->data[i].binp);
}
ErtsPrepDistExtRes
erts_prepare_dist_ext(ErtsDistExternal *edep,
const byte *ext,
Uint size,
Binary *binp,
DistEntry *dep,
Uint32 conn_id,
ErtsAtomCache *cache)
{
register const byte *ep;
ASSERT(dep);
erts_de_rlock(dep);
if ((dep->state != ERTS_DE_STATE_CONNECTED &&
dep->state != ERTS_DE_STATE_PENDING)
|| dep->connection_id != conn_id) {
erts_de_runlock(dep);
return ERTS_PREP_DIST_EXT_CLOSED;
}
if (!(dep->dflags & (DFLAG_DIST_HDR_ATOM_CACHE|DFLAG_FRAGMENTS))) {
/* Skip PASS_THROUGH */
ext++;
size--;
}
ep = ext;
if (size < 2)
goto fail;
if (ep[0] != VERSION_MAGIC) {
erts_dsprintf_buf_t *dsbufp = erts_create_logger_dsbuf();
erts_dsprintf(dsbufp,
"** Got message from incompatible erlang on "
"channel %d\n",
dist_entry_channel_no(dep));
erts_send_error_to_logger_nogl(dsbufp);
goto fail;
}
edep->heap_size = -1;
edep->flags = 0;
edep->dep = dep;
edep->mld = dep->mld;
edep->connection_id = conn_id;
edep->data->ext_endp = ext+size;
edep->data->binp = binp;
edep->data->seq_id = 0;
edep->data->frag_id = 1;
if (dep->dflags & (DFLAG_DIST_HDR_ATOM_CACHE|DFLAG_FRAGMENTS))
edep->flags |= ERTS_DIST_EXT_DFLAG_HDR;
if (ep[1] != DIST_HEADER && ep[1] != DIST_FRAG_HEADER && ep[1] != DIST_FRAG_CONT) {
if (edep->flags & ERTS_DIST_EXT_DFLAG_HDR)
goto bad_hdr;
edep->attab.size = 0;
edep->data->extp = ext;
}
else if (ep[1] == DIST_FRAG_CONT) {
if (!(dep->dflags & DFLAG_FRAGMENTS))
goto bad_hdr;
edep->attab.size = 0;
edep->data->extp = ext + 1 + 1 + 8 + 8;
edep->data->seq_id = get_int64(&ep[2]);
edep->data->frag_id = get_int64(&ep[2+8]);
erts_de_runlock(dep);
return ERTS_PREP_DIST_EXT_FRAG_CONT;
}
else {
int tix;
int no_atoms;
if (!(edep->flags & ERTS_DIST_EXT_DFLAG_HDR))
goto bad_hdr;
if (ep[1] == DIST_FRAG_HEADER) {
if (!(dep->dflags & DFLAG_FRAGMENTS))
goto bad_hdr;
edep->data->seq_id = get_int64(&ep[2]);
edep->data->frag_id = get_int64(&ep[2+8]);
ep += 16;
}
#undef CHKSIZE
#define CHKSIZE(SZ) \
do { if ((SZ) > edep->data->ext_endp - ep) goto bad_hdr; } while(0)
CHKSIZE(1+1+1);
ep += 2;
no_atoms = (int) get_int8(ep);
if (no_atoms < 0 || ERTS_ATOM_CACHE_SIZE < no_atoms)
goto bad_hdr;
ep++;
if (no_atoms) {
int long_atoms = 0;
#ifdef DEBUG
const byte *flgs_buf = ep;