FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/simulate/simulate_gc.cpp at master · rburkholder/daScript · GitHub
rburkholder
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
simulate
/
simulate_gc.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1241 lines (1206 loc) · 50.8 KB
Breadcrumbs
daScript
/
src
/
simulate
/
simulate_gc.cpp
Copy path
File metadata and controls
1241 lines (1206 loc) · 50.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/simulate/simulate.h
"
#
include
"
daScript/simulate/data_walker.h
"
#
include
"
daScript/simulate/debug_print.h
"
namespace
das
{
char
*
presentStr
(
char
* buf,
char
* ch,
int
size );
struct
PtrRange
{
char
* from =
nullptr
;
char
* to =
nullptr
;
PtrRange
() {}
PtrRange
(
char
* F,
size_t
size ) : from(F), to(F+size) {}
void
clear
() { from = to =
nullptr
; }
__forceinline
bool
empty
()
const
{
return
from==to; }
__forceinline
bool
contains
(
const
PtrRange & r ) {
return
!
empty
() && !r.
empty
() && from<=r.
from
&& to>=r.
to
; }
};
using
loop_point = pair<
void
*,
uint64_t
>;
struct
BaseGcDataWalker
: DataWalker {
int32_t
gcFlags = TypeInfo::flag_stringHeapGC | TypeInfo::flag_heapGC;
int32_t
gcStructFlags = StructInfo::flag_stringHeapGC | StructInfo::flag_heapGC;
vector<loop_point> visited;
vector<loop_point> visited_handles;
virtual
bool
canVisitStructure
(
char
* ps, StructInfo * info )
override
{
if
( !(info->
flags
& gcStructFlags) )
return
false
;
return
find_if
(visited.
begin
(),visited.
end
(),[&](
const
loop_point & t ){
return
t.
first
==ps && t.
second
==info->
hash
;
}) == visited.
end
();
}
virtual
bool
canVisitHandle
(
char
* ps, TypeInfo * info )
override
{
if
( !(info->
flags
& gcFlags) )
return
false
;
return
find_if
(visited_handles.
begin
(),visited_handles.
end
(),[&](
const
loop_point & t ){
return
t.
first
==ps && t.
second
==info->
hash
;
}) == visited_handles.
end
();
}
virtual
bool
canVisitArrayData
( TypeInfo * ti,
uint32_t
)
override
{
return
ti->
flags
& gcFlags;
}
virtual
bool
canVisitTableData
( TypeInfo * ti )
override
{
return
(ti->
firstType
->
flags
& gcFlags) || (ti->
secondType
->
flags
& gcFlags);
}
virtual
void
walk_struct
(
char
* ps, StructInfo * si )
override
{
if
( ps && (si->
flags
& StructInfo::flag_class) ) {
auto
ti = *(TypeInfo **) ps;
DAS_ASSERT
(ti);
si = ti->
structType
;
}
if
(
canVisitStructure
(ps, si) ) {
beforeStructure
(ps, si);
for
(
uint32_t
i=si->
firstGcField
, is=si->
count
; i!=is; ) {
VarInfo * vi = si->
fields
[i];
char
* pf = ps + vi->
offset
;
walk
(pf, vi);
i = vi->
nextGcField
;
}
afterStructure
(ps, si);
}
}
virtual
void
walk_tuple
(
char
* ps, TypeInfo * ti )
override
{
beforeTuple
(ps, ti);
int
fieldOffset =
0
;
for
(
uint32_t
i=
0
, is=ti->
argCount
; i!=is; ++i ) {
TypeInfo * vi = ti->
argTypes
[i];
auto
fa =
getTypeAlign
(vi) -
1
;
fieldOffset = (fieldOffset + fa) & ~fa;
char
* pf = ps + fieldOffset;
walk
(pf, vi);
fieldOffset += vi->
size
;
}
afterTuple
(ps, ti);
}
virtual
void
walk_variant
(
char
* ps, TypeInfo * ti )
override
{
beforeVariant
(ps, ti);
int32_t
fidx = *((
int32_t
*)ps);
DAS_ASSERTF
(
uint32_t
(fidx)<ti->
argCount
,
"
invalid variant index
"
);
int
fieldOffset =
getTypeBaseSize
(Type::tInt);
TypeInfo * vi = ti->
argTypes
[fidx];
auto
fa =
getTypeAlign
(ti) -
1
;
fieldOffset = (fieldOffset + fa) & ~fa;
char
* pf = ps + fieldOffset;
walk
(pf, vi);
afterVariant
(ps, ti);
}
virtual
void
walk_array
(
char
* pa,
uint32_t
stride,
uint32_t
count, TypeInfo * ti )
override
{
if
( !
canVisitArrayData
(ti,count) )
return
;
char
* pe = pa;
for
(
uint32_t
i=
0
; i!=count; ++i ) {
walk
(pe, ti);
pe += stride;
}
}
virtual
void
walk_dim
(
char
* pa, TypeInfo * ti )
override
{
beforeDim
(pa, ti);
TypeInfo copyInfo = *ti;
DAS_ASSERT
(copyInfo.
dimSize
);
copyInfo.
size
= ti->
dim
[
0
] ? copyInfo.
size
/ ti->
dim
[
0
] : copyInfo.
size
;
copyInfo.
dimSize
--;
vector<
uint32_t
> udim;
if
( copyInfo.
dimSize
) {
for
(
uint32_t
i=
0
, is=copyInfo.
dimSize
; i!=is; ++i) {
udim.
push_back
(ti->
dim
[i+
1
]);
}
copyInfo.
dim
= udim.
data
();
}
else
{
copyInfo.
dim
=
nullptr
;
}
uint32_t
stride = copyInfo.
size
;
uint32_t
count = ti->
dim
[
0
];
walk_array
(pa, stride, count, ©Info);
afterDim
(pa, ti);
}
virtual
void
walk_table
( Table * tab, TypeInfo * info )
override
{
if
( !
canVisitTableData
(info) )
return
;
int
keySize = info->
firstType
->
size
;
int
valueSize = info->
secondType
->
size
;
if
(info->
firstType
->
flags
& gcFlags) {
if
(info->
secondType
->
flags
& gcFlags) {
for
(
uint32_t
i=
0
, is=tab->
capacity
; i!=is; ++i ) {
if
( tab->
hashes
[i] >
HASH_KILLED64
) {
//
key
char
* key = tab->
keys
+ i*keySize;
walk
( key, info->
firstType
);
//
value
char
* value = tab->
data
+ i*valueSize;
walk
( value, info->
secondType
);
}
}
}
else
{
for
(
uint32_t
i=
0
, is=tab->
capacity
; i!=is; ++i ) {
if
( tab->
hashes
[i] >
HASH_KILLED64
) {
//
key
char
* key = tab->
keys
+ i*keySize;
walk
( key, info->
firstType
);
}
}
}
}
else
{
for
(
uint32_t
i=
0
, is=tab->
capacity
; i!=is; ++i ) {
if
( tab->
hashes
[i] >
HASH_KILLED64
) {
//
value
char
* value = tab->
data
+ i*valueSize;
walk
( value, info->
secondType
);
}
}
}
}
using
DataWalker::walk;
virtual
void
walk
(
char
* pa, TypeInfo * info )
override
{
if
( pa ==
nullptr
) {
}
else
if
( info->
flags
& TypeInfo::flag_ref ) {
beforeRef
(pa,info);
TypeInfo ti = *info;
ti.
flags
&= ~TypeInfo::flag_ref;
walk
(*(
char
**)pa, &ti);
ti.
flags
|= TypeInfo::flag_ref;
afterRef
(pa,info);
}
else
if
( info->
dimSize
) {
walk_dim
(pa, info);
}
else
{
switch
( info->
type
) {
case
Type::tArray: {
auto
arr = (Array *) pa;
beforeArray
(arr, info);
walk_array
(arr->
data
, info->
firstType
->
size
, arr->
size
, info->
firstType
);
afterArray
(arr, info);
}
break
;
case
Type::tTable: {
auto
tab = (Table *) pa;
beforeTable
(tab, info);
walk_table
(tab, info);
afterTable
(tab, info);
}
break
;
case
Type::tString:
String
(*((
char
**)pa));
break
;
case
Type::tPointer: {
if
( info->
firstType
&& info->
firstType
->
type
!=Type::tVoid ) {
beforePtr
(pa, info);
walk
(*(
char
**)pa, info->
firstType
);
afterPtr
(pa, info);
}
}
break
;
case
Type::tStructure:
walk_struct
(pa, info->
structType
);
break
;
case
Type::tTuple:
walk_tuple
(pa, info);
break
;
case
Type::tVariant:
walk_variant
(pa, info);
break
;
case
Type::tLambda: {
auto
ll = (Lambda *) pa;
walk
( ll->
capture
, ll->
getTypeInfo
() );
}
break
;
case
Type::tIterator: {
auto
ll = (Sequence *) pa;
if
( ll->
iter
) {
ll->
iter
->
walk
(*
this
);
}
}
break
;
case
Type::tHandle:
if
(
canVisitHandle
(pa, info) ) {
beforeHandle
(pa, info);
info->
getAnnotation
()->
walk
(*
this
, pa);
afterHandle
(pa, info);
}
break
;
default
:
break
;
}
}
}
};
struct
HeapReporter
final
: BaseGcDataWalker {
bool
reportStringHeap =
true
;
bool
reportHeap =
true
;
bool
heapOnly =
false
;
bool
errorsOnly =
false
;
vector<string> history;
vector<string> keys;
vector<PtrRange> ptrRangeStack;
PtrRange currentRange;
LOG
tp;
int32_t
gcAlways = TypeInfo::flag_stringHeapGC | TypeInfo::flag_heapGC;
void
prepare
(
const
string & walk_from ) {
DAS_ASSERT
(keys.
size
()==
0
);
DAS_ASSERT
(ptrRangeStack.
size
()==
0
);
gcFlags = TypeInfo::flag_stringHeapGC | TypeInfo::flag_heapGC;
gcStructFlags = StructInfo::flag_stringHeapGC | StructInfo::flag_heapGC;
history.
clear
();
history.
push_back
(walk_from);
currentRange.
clear
();
}
bool
markRange
(
const
PtrRange & r ) {
if
( currentRange.
empty
() )
return
true
;
if
( currentRange.
contains
(r) )
return
false
;
if
( heapOnly ) {
int
ssize =
int
(r.
to
-r.
from
);
ssize = (ssize +
15
) & ~
15
;
return
context->
heap
->
isOwnPtr
(r.
from
, ssize);
}
return
true
;
}
void
pushRange
(
const
PtrRange & rdata ) {
ptrRangeStack.
push_back
(currentRange);
currentRange = rdata;
}
void
popRange
() {
currentRange = ptrRangeStack.
back
();
ptrRangeStack.
pop_back
();
}
bool
describe_ptr
(
char
* pa,
int
tsize,
bool
isHandle =
false
) {
auto
ssize = (tsize+
15
) & ~
15
;
bool
show = !errorsOnly;
if
( context->
stack
.
is_stack_ptr
(pa) ) {
if
( show ) tp <<
"
\t
STACK
"
;
}
else
if
( context->
isGlobalPtr
(pa) ) {
if
( show ) tp <<
"
\t
GLOBAL
"
;
}
else
if
( context->
isSharedPtr
(pa) ) {
if
( show ) tp <<
"
\t
SHAREDGLOBAL
"
;
}
else
if
( context->
heap
->
isOwnPtr
(pa, ssize) ) {
if
( context->
heap
->
isValidPtr
(pa, ssize) ) {
if
( show ) tp <<
"
\t
HEAP
"
;
}
else
{
tp <<
"
\t
HEAP FREE!!!
"
;
show =
true
;
}
}
else
if
( context->
stringHeap
->
isOwnPtr
(pa, ssize) ) {
if
( context->
stringHeap
->
isValidPtr
(pa, ssize) ) {
if
( show ) tp <<
"
\t
STRINGHEAP
"
;
}
else
{
tp <<
"
\t
STRINGHEAP FREE!!!
"
;
show =
true
;
}
}
else
if
( context->
constStringHeap
->
isOwnPtr
(pa) ) {
if
( show ) tp <<
"
\t
CONSTSTRINGHEAP
"
;
}
else
if
( context->
code
->
isOwnPtr
(pa) ) {
if
( show ) tp <<
"
\t
CODE
"
;
}
else
if
( context->
debugInfo
->
isOwnPtr
(pa) ) {
if
( show ) tp <<
"
\t
DEBUGINFO
"
;
}
else
if
( isHandle ) {
if
( show ) tp <<
"
\t
HANDLED
"
;
}
else
{
tp <<
"
UNCATEGORIZED!!!
"
;
show =
true
;
}
if
( show ) {
tp <<
"
"
<< tsize <<
"
bytes, at 0x
"
<<
HEX
<<
uint64_t
(pa) <<
DEC
<<
"
"
;
if
( pa==
nullptr
) {
tp <<
"
= NULL
"
;
}
else
{
auto
bpa = (
uint8_t
*) pa;
tp <<
HEX
;
char
tohex[] =
"
0123456789abcdef
"
;
for
(
int
i=
0
; i<
8
; ++i ) {
auto
t = bpa[i];
tp <<
"
"
<< tohex[t>>
4
] << tohex[t&
15
];
}
tp <<
DEC
;
}
}
return
show;
}
void
describeInfo
( TypeInfo * ti ) {
if
( ti->
flags
& (TypeInfo::flag_stringHeapGC | TypeInfo::flag_heapGC) ) tp <<
"
"
;
if
( ti->
flags
& TypeInfo::flag_stringHeapGC ) tp <<
"
<S>
"
;
if
( ti->
flags
& TypeInfo::flag_heapGC ) tp <<
"
<H>
"
;
}
void
describeStructInfo
( StructInfo * ti ) {
if
( ti->
flags
& (StructInfo::flag_stringHeapGC | StructInfo::flag_heapGC) ) tp <<
"
"
;
if
( ti->
flags
& StructInfo::flag_stringHeapGC ) tp <<
"
<S>
"
;
if
( ti->
flags
& StructInfo::flag_heapGC ) tp <<
"
<H>
"
;
}
virtual
void
beforeHandle
(
char
* pa, TypeInfo * ti )
override
{
visited_handles.
emplace_back
(
make_pair
(pa,ti->
hash
));
auto
tsize = ti->
size
;
DAS_ASSERT
(tsize==
uint32_t
(
getTypeSize
(ti)));
PtrRange
rdata
(pa, tsize );
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize,
true
) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
HANDLE
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterHandle
(
char
*, TypeInfo * )
override
{
popRange
();
visited_handles.
pop_back
();
}
virtual
void
beforeDim
(
char
* pa, TypeInfo * ti )
override
{
auto
tsize = ti->
size
;
DAS_ASSERT
(tsize==
uint32_t
(
getTypeSize
(ti)));
PtrRange
rdata
(pa, tsize );
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize, (ti->
flags
& TypeInfo::flag_isHandled)!=
0
) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
DIM
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterDim
(
char
*, TypeInfo * )
override
{
popRange
();
}
virtual
bool
canVisitArrayData
( TypeInfo * ti,
uint32_t
)
override
{
return
(ti->
flags
| gcAlways) & gcFlags;
}
virtual
bool
canVisitTableData
( TypeInfo * ti )
override
{
return
((ti->
firstType
->
flags
| gcAlways) & gcFlags) || ((ti->
secondType
->
flags
| gcAlways) & gcFlags);
}
virtual
void
beforeArray
( Array *
PA
, TypeInfo * ti )
override
{
auto
tsize = ti->
firstType
->
size
*
PA
->
capacity
;
DAS_ASSERT
(tsize==
getTypeSize
(ti->
firstType
)*
PA
->
capacity
);
char
* pa =
PA
->
data
;
PtrRange
rdata
(pa, tsize);
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
ARRAY
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterArray
( Array *, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeTable
( Table *
PT
, TypeInfo * ti )
override
{
auto
tsize = (ti->
firstType
->
size
+ ti->
secondType
->
size
+
sizeof
(
uint64_t
)) *
PT
->
capacity
;
DAS_ASSERT
(tsize==(
getTypeSize
(ti->
firstType
)+
getTypeSize
(ti->
secondType
)+
sizeof
(
uint64_t
))*
PT
->
capacity
);
char
* pa =
PT
->
data
;
PtrRange
rdata
(pa, tsize);
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa,
int
(tsize)) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
TABLE
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterTable
( Table *, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeRef
(
char
* pa, TypeInfo * ti )
override
{
auto
tsize = ti->
size
;
DAS_ASSERT
(tsize==
uint32_t
(
getTypeSize
(ti)));
PtrRange
rdata
(pa, tsize );
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
&
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterRef
(
char
*, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeStructure
(
char
* ps, StructInfo * si )
override
{
visited.
emplace_back
(
make_pair
(ps,si->
hash
));
char
* pa = ps;
auto
tsize = si->
size
;
if
( si->
flags
& StructInfo::flag_lambda ) {
pa -=
16
;
tsize +=
16
;
}
PtrRange
rdata
(pa, tsize);
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize) ) {
describeStructInfo
(si);
ReportHistory
();
tp <<
"
STRUCTURE
"
<< si->
module_name
<<
"
::
"
<< si->
name
<<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterStructure
(
char
*, StructInfo * )
override
{
popRange
();
visited.
pop_back
();
}
virtual
void
beforeStructureField
(
char
*, StructInfo *,
char
*, VarInfo * vi,
bool
)
override
{
history.
push_back
(vi->
name
);
}
virtual
void
afterStructureField
(
char
*, StructInfo *,
char
*, VarInfo *,
bool
)
override
{
history.
pop_back
();
}
virtual
void
beforeVariant
(
char
* ps, TypeInfo * ti )
override
{
char
* pa = ps;
auto
tsize = ti->
size
;
DAS_ASSERT
(tsize==
uint32_t
(
getTypeSize
(ti)));
PtrRange
rdata
(pa, tsize);
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize) ) {
describeInfo
(ti);
ReportHistory
();
tp <<
"
VARIANT
"
<<
getTypeInfoMangledName
(ti) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterVariant
(
char
*, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeTuple
(
char
* ps, TypeInfo * si )
override
{
char
* pa = ps;
auto
tsize = si->
size
;
DAS_ASSERT
(tsize==
uint32_t
(
getTypeSize
(si)));
PtrRange
rdata
(pa, tsize);
if
( reportHeap && tsize &&
markRange
(rdata) ) {
if
(
describe_ptr
(pa, tsize) ) {
describeInfo
(si);
ReportHistory
();
tp <<
"
TUPLE
"
<<
getTypeInfoMangledName
(si) <<
"
\n
"
;
}
}
pushRange
(rdata);
}
virtual
void
afterTuple
(
char
*, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeTupleEntry
(
char
*, TypeInfo * ti,
char
*, TypeInfo * vi,
bool
)
override
{
uint32_t
VI
= -
1u
;
for
(
uint32_t
i=
0
, is=ti->
argCount
; i!=is; ++i ) {
if
( ti->
argTypes
[i]==vi ) {
VI
= i;
break
;
}
}
history.
push_back
(
to_string
(
VI
));
}
virtual
void
afterTupleEntry
(
char
*, TypeInfo *,
char
*, TypeInfo *,
bool
)
override
{
history.
pop_back
();
}
virtual
void
beforeArrayElement
(
char
*, TypeInfo *,
char
*,
uint32_t
index,
bool
)
override
{
history.
push_back
(
"
[
"
+
to_string
(index)+
"
]
"
);
}
virtual
void
afterArrayElement
(
char
*, TypeInfo *,
char
*,
uint32_t
,
bool
)
override
{
history.
pop_back
();
}
virtual
void
beforeTableKey
( Table *, TypeInfo *,
char
* pk, TypeInfo * ki,
uint32_t
,
bool
)
override
{
string keyText =
debug_value
( pk, ki, PrintFlags::none );
keys.
push_back
(keyText);
history.
push_back
(
"
=>key=>
"
);
}
virtual
void
afterTableKey
( Table *, TypeInfo *,
char
*, TypeInfo *,
uint32_t
,
bool
)
override
{
history.
pop_back
();
}
virtual
void
beforeTableValue
( Table *, TypeInfo *,
char
*, TypeInfo *,
uint32_t
,
bool
)
override
{
string keyText = keys.
back
();
history.
push_back
(
"
[
\"
"
+
escapeString
(keyText)+
"
\"
]
"
);
}
virtual
void
afterTableValue
( Table *, TypeInfo *,
char
*, TypeInfo *,
uint32_t
,
bool
)
override
{
keys.
pop_back
();
history.
pop_back
();
}
virtual
bool
canVisitStructure
(
char
* ps, StructInfo * info )
override
{
if
( !((info->
flags
| gcAlways) & gcStructFlags) )
return
false
;
return
find_if
(visited.
begin
(),visited.
end
(),[&](
const
loop_point & t ){
return
t.
first
==ps && t.
second
==info->
hash
;
}) == visited.
end
();
}
virtual
bool
canVisitHandle
(
char
* ps, TypeInfo * info )
override
{
if
( !((info->
flags
| gcAlways) & gcFlags) )
return
false
;
return
find_if
(visited_handles.
begin
(),visited_handles.
end
(),[&](
const
loop_point & t ){
return
t.
first
==ps && t.
second
==info->
hash
;
}) == visited_handles.
end
();
}
virtual
void
String
(
char
* & st )
override
{
if
( !reportStringHeap )
return
;
if
( !st )
return
;
if
( context->
constStringHeap
->
isOwnPtr
(st) )
return
;
bool
show = !errorsOnly;
char
buf[
32
];
uint32_t
ulen =
uint32_t
(
strlen
(st)) +
1
;
uint32_t
len = (ulen +
15
) & ~
15
;
if
( context->
stringHeap
->
isOwnPtr
(st,len) ) {
if
( context->
stringHeap
->
isValidPtr
(st,len) ) {
if
( show ) tp <<
"
\t\t
STRING
"
;
}
else
{
tp <<
"
\t\t
STRING FREE!!!
"
;
show =
true
;
}
}
else
{
if
( show ) tp <<
"
\t\t
STRING#!!!
"
;
}
if
( show ) {
ReportHistory
();
tp <<
presentStr
(buf, st,
32
) <<
"
, len=
"
<< ulen <<
"
(
"
<< len <<
"
) at 0x
"
<<
HEX
<<
uint64_t
(st) <<
DEC
<<
"
\n
"
;
}
}
void
ReportHistory
(
void
) {
tp <<
"
\t
"
;
for
(
size_t
i=
0
, is=history.
size
(); i!=is; ++i ) {
if
( i!=
0
&& (history[i].
empty
() || history[i][
0
]!=
'
[
'
) ) tp <<
"
.
"
;
tp << history[i];
}
tp <<
"
:
"
;
}
//
extended version, with entry callbacks
virtual
void
walk_tuple
(
char
* ps, TypeInfo * ti )
override
{
if
(
canVisitTuple
(ps,ti) ) {
beforeTuple
(ps, ti);
int
fieldOffset =
0
;
for
(
uint32_t
i=
0
, is=ti->
argCount
; i!=is; ++i ) {
bool
last = i==(ti->
argCount
-
1
);
TypeInfo * vi = ti->
argTypes
[i];
auto
fa =
getTypeAlign
(vi) -
1
;
fieldOffset = (fieldOffset + fa) & ~fa;
char
* pf = ps + fieldOffset;
beforeTupleEntry
(ps, ti, pf, vi, last);
walk
(pf, vi);
afterTupleEntry
(ps, ti, pf, vi, last);
fieldOffset += vi->
size
;
}
afterTuple
(ps, ti);
}
}
virtual
void
walk_struct
(
char
* ps, StructInfo * si )
override
{
if
( ps && (si->
flags
& StructInfo::flag_class) ) {
auto
ti = *(TypeInfo **) ps;
DAS_ASSERT
(ti);
si = ti->
structType
;
}
if
(
canVisitStructure
(ps, si) ) {
beforeStructure
(ps, si);
for
(
uint32_t
i=
0
, is=si->
count
; i!=is; ++i ) {
bool
last = i==(si->
count
-
1
);
VarInfo * vi = si->
fields
[i];
char
* pf = ps + vi->
offset
;
beforeStructureField
(ps, si, pf, vi, last);
walk
(pf, vi);
afterStructureField
(ps, si, pf, vi, last);
}
afterStructure
(ps, si);
}
}
virtual
void
walk_table
( Table * tab, TypeInfo * info )
override
{
if
( !
canVisitTableData
(info) )
return
;
int
keySize = info->
firstType
->
size
;
int
valueSize = info->
secondType
->
size
;
uint32_t
count =
0
;
for
(
uint32_t
i=
0
, is=tab->
capacity
; i!=is; ++i ) {
if
( tab->
hashes
[i] >
HASH_KILLED64
) {
bool
last = (count == (tab->
size
-
1
));
//
key
char
* key = tab->
keys
+ i*keySize;
beforeTableKey
(tab, info, key, info->
firstType
, count, last);
walk
( key, info->
firstType
);
afterTableKey
(tab, info, key, info->
firstType
, count, last);
//
value
char
* value = tab->
data
+ i*valueSize;
beforeTableValue
(tab, info, value, info->
secondType
, count, last);
walk
( value, info->
secondType
);
afterTableValue
(tab, info, value, info->
secondType
, count, last);
//
next
count ++;
}
}
}
};
void
Context::reportAnyHeap
(LineInfo * at,
bool
sth,
bool
rgh,
bool
rghOnly,
bool
errorsOnly) {
LOG
tp
(LogLevel::debug);
//
now
HeapReporter walker;
walker.
context
=
this
;
walker.
reportStringHeap
= sth;
walker.
reportHeap
= rgh;
walker.
heapOnly
= rghOnly;
walker.
errorsOnly
= errorsOnly;
//
mark globals
if
( sharedOwner ) {
tp <<
"
SHARED GLOBALS:
\n
"
;
for
(
int
i=
0
, is=totalVariables; i!=is; ++i ) {
auto
& pv = globalVariables[i];
if
( !pv.
shared
)
continue
;
if
( (pv.
debugInfo
->
flags
| walker.
gcAlways
) & walker.
gcFlags
) {
walker.
prepare
(pv.
name
);
walker.
walk
(shared + pv.
offset
, pv.
debugInfo
);
}
}
}
tp <<
"
GLOBALS:
\n
"
;
for
(
int
i=
0
, is=totalVariables; i!=is; ++i ) {
auto
& pv = globalVariables[i];
if
( pv.
shared
)
continue
;
if
( (pv.
debugInfo
->
flags
| walker.
gcAlways
) & walker.
gcFlags
) {
walker.
prepare
(pv.
name
);
walker.
walk
(globals + pv.
offset
, pv.
debugInfo
);
}
}
//
mark stack
char
* sp = stack.
ap
();
const
LineInfo * lineAt = at;
while
( sp < stack.
top
() ) {
Prologue * pp = (Prologue *) sp;
Block * block =
nullptr
;
FuncInfo * info =
nullptr
;
char
*
SP
= sp;
if
( pp->
info
) {
intptr_t
iblock =
intptr_t
(pp->
block
);
if
( iblock &
1
) {
block = (Block *) (iblock & ~
1
);
info = block->
info
;
SP
= stack.
bottom
() + block->
stackOffset
;
}
else
{
info = pp->
info
;
}
tp <<
"
FUNCTION
"
<< info->
name
<<
"
\n
"
;
}
if
( info ) {
if
( info->
count
) {
tp <<
"
ARGUMENTS:
\n
"
;
for
(
uint32_t
i=
0
, is=info->
count
; i!=is; ++i ) {
if
( (info->
fields
[i]->
flags
| walker.
gcAlways
) & walker.
gcFlags
) {
walker.
prepare
(info->
fields
[i]->
name
);
walker.
walk
(pp->
arguments
[i], info->
fields
[i]);
}
}
}
if
( info->
locals
) {
tp <<
"
LOCALS:
\n
"
;
for
(
uint32_t
i=
0
, is=info->
localCount
; i!=is; ++i ) {
auto
lv = info->
locals
[i];
bool
inScope = lineAt ? lineAt->
inside
(lv->
visibility
) :
false
;
if
( !inScope )
continue
;
char
* addr =
nullptr
;
if
( lv->
cmres
) {
addr = (
char
*)pp->
cmres
;
}
else
{
addr =
SP
+ lv->
stackTop
;
}
if
( addr ) {
if
( (lv->
flags
| walker.
gcAlways
) & walker.
gcFlags
) {
walker.
prepare
(lv->
name
);
walker.
walk
(addr, lv);
}
}
}
}
}
lineAt = info ? pp->
line
:
nullptr
;
sp += info ? info->
stackSize
: pp->
stackSize
;
}
}
struct
GcMarkStringHeap
final
: BaseGcDataWalker {
bool
validate =
false
;
GcMarkStringHeap
() {
gcFlags = TypeInfo::flag_stringHeapGC;
gcStructFlags = StructInfo::flag_stringHeapGC;
}
using
loop_point = pair<
void
*,
uint64_t
>;
das_set<
char
*> failed;
virtual
void
beforeStructure
(
char
* ps, StructInfo * info )
override
{
visited.
emplace_back
(
make_pair
(ps,info->
hash
));
}
virtual
void
afterStructure
(
char
*, StructInfo * )
override
{
visited.
pop_back
();
}
virtual
void
beforeHandle
(
char
* ps, TypeInfo * ti )
override
{
visited_handles.
emplace_back
(
make_pair
(ps,ti->
hash
));
}
virtual
void
afterHandle
(
char
*, TypeInfo * )
override
{
visited_handles.
pop_back
();
}
virtual
void
String
(
char
* & st )
override
{
DataWalker::String
(st);
if
( !st )
return
;
if
( context->
constStringHeap
->
isOwnPtr
(st) )
return
;
uint32_t
len =
uint32_t
(
strlen
(st)) +
1
;
len = (len +
15
) & ~
15
;
if
( validate ) {
if
( context->
stringHeap
->
isOwnPtr
(st, len) ) {
if
( context->
stringHeap
->
isValidPtr
(st, len) ) {
context->
stringHeap
->
mark
(st, len);
}
else
{
failed.
insert
(st);
}
}
}
else
{
context->
stringHeap
->
mark
(st, len);
}
}
//
fastest versions, without any before/after
virtual
void
walk_tuple
(
char
* ps, TypeInfo * ti )
override
{
int
fieldOffset =
0
;
for
(
uint32_t
i=
0
, is=ti->
argCount
; i!=is; ++i ) {
TypeInfo * vi = ti->
argTypes
[i];
auto
fa =
getTypeAlign
(vi) -
1
;
fieldOffset = (fieldOffset + fa) & ~fa;
char
* pf = ps + fieldOffset;
walk
(pf, vi);
fieldOffset += vi->
size
;
}
}
virtual
void
walk_variant
(
char
* ps, TypeInfo * ti )
override
{
int32_t
fidx = *((
int32_t
*)ps);
DAS_ASSERTF
(
uint32_t
(fidx)<ti->
argCount
,
"
invalid variant index
"
);
int
fieldOffset =
getTypeBaseSize
(Type::tInt);
TypeInfo * vi = ti->
argTypes
[fidx];
auto
fa =
getTypeAlign
(ti) -
1
;
fieldOffset = (fieldOffset + fa) & ~fa;
char
* pf = ps + fieldOffset;
walk
(pf, vi);
}
virtual
void
walk_dim
(
char
* pa, TypeInfo * ti )
override
{
TypeInfo copyInfo = *ti;
DAS_ASSERT
(copyInfo.
dimSize
);
copyInfo.
size
= ti->
dim
[
0
] ? copyInfo.
size
/ ti->
dim
[
0
] : copyInfo.
size
;
copyInfo.
dimSize
--;
vector<
uint32_t
> udim;
if
( copyInfo.
dimSize
) {
for
(
uint32_t
i=
0
, is=copyInfo.
dimSize
; i!=is; ++i) {
udim.
push_back
(ti->
dim
[i+
1
]);
}
copyInfo.
dim
= udim.
data
();
}
else
{
copyInfo.
dim
=
nullptr
;
}
uint32_t
stride = copyInfo.
size
;
uint32_t
count = ti->
dim
[
0
];
walk_array
(pa, stride, count, ©Info);
}
using
DataWalker::walk;
virtual
void
walk
(
char
* pa, TypeInfo * info )
override
{
if
( pa ==
nullptr
) {
}
else
if
( info->
flags
& TypeInfo::flag_ref ) {
beforeRef
(pa,info);
TypeInfo ti = *info;
ti.
flags
&= ~TypeInfo::flag_ref;
walk
(*(
char
**)pa, &ti);
ti.
flags
|= TypeInfo::flag_ref;
afterRef
(pa,info);
}
else
if
( info->
dimSize
) {
walk_dim
(pa, info);
}
else
{
switch
( info->
type
) {
case
Type::tArray: {
auto
arr = (Array *) pa;
walk_array
(arr->
data
, info->
firstType
->
size
, arr->
size
, info->
firstType
);
}
break
;
case
Type::tTable: {
auto
tab = (Table *) pa;
walk_table
(tab, info);
}
break
;
case
Type::tString:
String
(*((
char
**)pa));
break
;
case
Type::tPointer: {
if
( info->
firstType
&& info->
firstType
->
type
!=Type::tVoid ) {
walk
(*(
char
**)pa, info->
firstType
);
}
}
break
;
case
Type::tStructure:
walk_struct
(pa, info->
structType
);
break
;
case
Type::tTuple:
walk_tuple
(pa, info);
break
;
case
Type::tVariant:
walk_variant
(pa, info);
break
;
case
Type::tLambda: {
auto
ll = (Lambda *) pa;
walk
( ll->
capture
, ll->
getTypeInfo
() );
}
break
;
case
Type::tIterator: {
auto
ll = (Sequence *) pa;
if
( ll->
iter
) {
ll->
iter
->
walk
(*
this
);
}
}
break
;
case
Type::tHandle:
if
(
canVisitHandle
(pa, info) ) {
beforeHandle
(pa, info);
info->
getAnnotation
()->
walk
(*
this
, pa);
afterHandle
(pa, info);
}
break
;
default
:
break
;
}
}
}
};
void
Context::collectStringHeap
( LineInfo * at,
bool
validate ) {
//
clean up, so that all small allocations are marked as 'free'
if
( !stringHeap->
mark
() )
return
;
//
now
GcMarkStringHeap walker;
walker.
context
=
this
;
walker.
validate
= validate;
//
mark globals
if
( sharedOwner ) {
for
(
int
i=
0
, is=totalVariables; i!=is; ++i ) {
auto
& pv = globalVariables[i];
if
( !pv.
shared
)
continue
;
walker.
walk
(shared + pv.
offset
, pv.
debugInfo
);
}
}
for
(
int
i=
0
, is=totalVariables; i!=is; ++i ) {
auto
& pv = globalVariables[i];
if
( pv.
shared
)
continue
;
walker.
walk
(globals + pv.
offset
, pv.
debugInfo
);
}
//
mark stack
char
* sp = stack.
ap
();
const
LineInfo * lineAt = at;
while
( sp < stack.
top
() ) {
Prologue * pp = (Prologue *) sp;
Block * block =
nullptr
;
FuncInfo * info =
nullptr
;
char
*
SP
= sp;
if
( pp->
info
) {
intptr_t
iblock =
intptr_t
(pp->
block
);
if
( iblock &
1
) {
block = (Block *) (iblock & ~
1
);
info = block->
info
;
SP
= stack.
bottom
() + block->
stackOffset
;
}
else
{
info = pp->
info
;
}
}
if
( info ) {
for
(
uint32_t
i=
0
, is=info->
count
; i!=is; ++i ) {
walker.
walk
(pp->
arguments
[i], info->
fields
[i]);
}
if
( info->
locals
) {
for
(
uint32_t
i=
0
, is=info->
localCount
; i!=is; ++i ) {
auto
lv = info->
locals
[i];
bool
inScope = lineAt ? lineAt->
inside
(lv->
visibility
) :
false
;
if
( !inScope )
continue
;
char
* addr =
nullptr
;
if
( lv->
cmres
) {
addr = (
char
*)pp->
cmres
;
}
else
{
addr =
SP
+ lv->
stackTop
;
}
if
( addr ) {
walker.
walk
(addr, lv);
}
}
}
}
lineAt = info ? pp->
line
:
nullptr
;
sp += info ? info->
stackSize
: pp->
stackSize
;
}
//
sweep
stringHeap->
sweep
();
//
report errors
if
( !walker.
failed
.
empty
() ) {
reportAnyHeap
(at,
true
,
false
,
false
,
true
);
TextWriter tw;
tw <<
"
string GC failed on the following dangling pointers:
"
<<
HEX
;
for
(
auto
f : walker.
failed
) {
tw <<
"
"
<<
uint64_t
(f);
}
auto
etext = stringHeap->
allocateString
(tw.
str
());
throw_error_at
(at,
"
%s
"
, etext);
}
}
struct
GcMarkAnyHeap
final
: BaseGcDataWalker {
vector<PtrRange> ptrRangeStack;
PtrRange currentRange;
das_set<
char
*> failed;
bool
markStringHeap =
true
;
bool
validate =
false
;
void
prepare
() {
currentRange.
clear
();
gcFlags = TypeInfo::flag_heapGC;
gcStructFlags = StructInfo::flag_heapGC;
if
( markStringHeap ) {
gcFlags |= TypeInfo::flag_stringHeapGC;
gcStructFlags |= StructInfo::flag_stringHeapGC;
}
}
bool
markAndPushRange
(
const
PtrRange & r ) {
bool
result =
true
;
ptrRangeStack.
push_back
(currentRange);
if
( !r.
empty
() && !currentRange.
contains
(r) ) {
int
ssize =
int
(r.
to
-r.
from
);
ssize = (ssize +
15
) & ~
15
;
if
( validate ) {
if
( context->
heap
->
isOwnPtr
(r.
from
, ssize) ) {
if
( context->
heap
->
isValidPtr
(r.
from
, ssize) ) {
result = context->
heap
->
mark
(r.
from
, ssize);
}
else
{
failed.
insert
(r.
from
);
}
}
}
else
{
result = context->
heap
->
mark
(r.
from
, ssize);
}
currentRange = r;
}
return
result;
}
void
popRange
() {
currentRange = ptrRangeStack.
back
();
ptrRangeStack.
pop_back
();
}
virtual
void
beforeHandle
(
char
* pa, TypeInfo * ti )
override
{
visited_handles.
emplace_back
(
make_pair
(pa,ti->
hash
));
PtrRange
rdata
(pa, ti->
size
);
markAndPushRange
(rdata);
}
virtual
void
afterHandle
(
char
*, TypeInfo * )
override
{
popRange
();
visited_handles.
pop_back
();
}
virtual
void
beforeDim
(
char
* pa, TypeInfo * ti )
override
{
PtrRange
rdata
(pa, ti->
size
);
markAndPushRange
(rdata);
}
virtual
void
afterDim
(
char
*, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeArray
( Array *
PA
, TypeInfo * ti )
override
{
PtrRange
rdata
(
PA
->
data
,
size_t
(ti->
firstType
->
size
) *
size_t
(
PA
->
capacity
));
markAndPushRange
(rdata);
}
virtual
void
afterArray
( Array *, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeTable
( Table *
PT
, TypeInfo * ti )
override
{
PtrRange
rdata
(
PT
->
data
, (ti->
firstType
->
size
+ti->
secondType
->
size
+
sizeof
(
uint64_t
))*
size_t
(
PT
->
capacity
));
markAndPushRange
(rdata);
}
virtual
void
afterTable
( Table *, TypeInfo * )
override
{
popRange
();
}
virtual
void
beforeRef
(
char
* pa, TypeInfo * ti )
override
{
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL