-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy paththread.h
1882 lines (1649 loc) · 69.6 KB
/
thread.h
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
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_THREAD_H_
#define RUNTIME_VM_THREAD_H_
#if defined(SHOULD_NOT_INCLUDE_RUNTIME)
#error "Should not include runtime"
#endif
#include <setjmp.h>
#include "include/dart_api.h"
#include "platform/assert.h"
#include "platform/atomic.h"
#include "platform/safe_stack.h"
#include "vm/bitfield.h"
#include "vm/compiler/runtime_api.h"
#include "vm/constants.h"
#include "vm/globals.h"
#include "vm/handles.h"
#include "vm/heap/pointer_block.h"
#include "vm/heap/sampler.h"
#include "vm/os_thread.h"
#include "vm/pending_deopts.h"
#include "vm/random.h"
#include "vm/runtime_entry_list.h"
#include "vm/tags.h"
#include "vm/thread_stack_resource.h"
#include "vm/thread_state.h"
namespace dart {
class AbstractType;
class ApiLocalScope;
class Array;
class CompilerState;
class CompilerTimings;
class Class;
class Code;
class Bytecode;
class Error;
class ExceptionHandlers;
class Field;
class FieldTable;
class Function;
class GrowableObjectArray;
class HandleScope;
class Heap;
class HierarchyInfo;
class Instance;
class Interpreter;
class Isolate;
class IsolateGroup;
class Library;
class LocalHandle;
class Object;
class OSThread;
class JSONObject;
class NoActiveIsolateScope;
class PcDescriptors;
class RuntimeEntry;
class Smi;
class StackResource;
class StackTrace;
class StreamInfo;
class String;
class TimelineStream;
class TypeArguments;
class TypeParameter;
class TypeUsageInfo;
class Zone;
namespace bytecode {
class BytecodeLoader;
}
namespace compiler {
namespace target {
class Thread;
} // namespace target
} // namespace compiler
#define REUSABLE_HANDLE_LIST(V) \
V(AbstractType) \
V(Array) \
V(Class) \
V(Code) \
V(Bytecode) \
V(Error) \
V(ExceptionHandlers) \
V(Field) \
V(Function) \
V(GrowableObjectArray) \
V(Instance) \
V(Library) \
V(LoadingUnit) \
V(Object) \
V(PcDescriptors) \
V(Smi) \
V(String) \
V(TypeParameters) \
V(TypeArguments) \
V(TypeParameter) \
V(WeakArray)
#define CACHED_VM_STUBS_LIST(V) \
V(CodePtr, fix_callers_target_code_, StubCode::FixCallersTarget().ptr(), \
nullptr) \
V(CodePtr, fix_allocation_stub_code_, \
StubCode::FixAllocationStubTarget().ptr(), nullptr) \
V(CodePtr, invoke_dart_code_stub_, StubCode::InvokeDartCode().ptr(), \
nullptr) \
V(CodePtr, invoke_dart_code_from_bytecode_stub_, \
StubCode::InvokeDartCodeFromBytecode().ptr(), nullptr) \
V(CodePtr, call_to_runtime_stub_, StubCode::CallToRuntime().ptr(), nullptr) \
V(CodePtr, late_initialization_error_shared_without_fpu_regs_stub_, \
StubCode::LateInitializationErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, late_initialization_error_shared_with_fpu_regs_stub_, \
StubCode::LateInitializationErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, null_error_shared_without_fpu_regs_stub_, \
StubCode::NullErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, null_error_shared_with_fpu_regs_stub_, \
StubCode::NullErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, null_arg_error_shared_without_fpu_regs_stub_, \
StubCode::NullArgErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, null_arg_error_shared_with_fpu_regs_stub_, \
StubCode::NullArgErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, null_cast_error_shared_without_fpu_regs_stub_, \
StubCode::NullCastErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, null_cast_error_shared_with_fpu_regs_stub_, \
StubCode::NullCastErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, range_error_shared_without_fpu_regs_stub_, \
StubCode::RangeErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, range_error_shared_with_fpu_regs_stub_, \
StubCode::RangeErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, write_error_shared_without_fpu_regs_stub_, \
StubCode::WriteErrorSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, write_error_shared_with_fpu_regs_stub_, \
StubCode::WriteErrorSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, allocate_mint_with_fpu_regs_stub_, \
StubCode::AllocateMintSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, allocate_mint_without_fpu_regs_stub_, \
StubCode::AllocateMintSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, allocate_object_stub_, StubCode::AllocateObject().ptr(), nullptr) \
V(CodePtr, allocate_object_parameterized_stub_, \
StubCode::AllocateObjectParameterized().ptr(), nullptr) \
V(CodePtr, allocate_object_slow_stub_, StubCode::AllocateObjectSlow().ptr(), \
nullptr) \
V(CodePtr, async_exception_handler_stub_, \
StubCode::AsyncExceptionHandler().ptr(), nullptr) \
V(CodePtr, resume_stub_, StubCode::Resume().ptr(), nullptr) \
V(CodePtr, return_async_stub_, StubCode::ReturnAsync().ptr(), nullptr) \
V(CodePtr, return_async_not_future_stub_, \
StubCode::ReturnAsyncNotFuture().ptr(), nullptr) \
V(CodePtr, return_async_star_stub_, StubCode::ReturnAsyncStar().ptr(), \
nullptr) \
V(CodePtr, stack_overflow_shared_without_fpu_regs_stub_, \
StubCode::StackOverflowSharedWithoutFPURegs().ptr(), nullptr) \
V(CodePtr, stack_overflow_shared_with_fpu_regs_stub_, \
StubCode::StackOverflowSharedWithFPURegs().ptr(), nullptr) \
V(CodePtr, switchable_call_miss_stub_, StubCode::SwitchableCallMiss().ptr(), \
nullptr) \
V(CodePtr, throw_stub_, StubCode::Throw().ptr(), nullptr) \
V(CodePtr, re_throw_stub_, StubCode::Throw().ptr(), nullptr) \
V(CodePtr, optimize_stub_, StubCode::OptimizeFunction().ptr(), nullptr) \
V(CodePtr, deoptimize_stub_, StubCode::Deoptimize().ptr(), nullptr) \
V(CodePtr, lazy_deopt_from_return_stub_, \
StubCode::DeoptimizeLazyFromReturn().ptr(), nullptr) \
V(CodePtr, lazy_deopt_from_throw_stub_, \
StubCode::DeoptimizeLazyFromThrow().ptr(), nullptr) \
V(CodePtr, slow_type_test_stub_, StubCode::SlowTypeTest().ptr(), nullptr) \
V(CodePtr, lazy_specialize_type_test_stub_, \
StubCode::LazySpecializeTypeTest().ptr(), nullptr) \
V(CodePtr, enter_safepoint_stub_, StubCode::EnterSafepoint().ptr(), nullptr) \
V(CodePtr, exit_safepoint_stub_, StubCode::ExitSafepoint().ptr(), nullptr) \
V(CodePtr, call_native_through_safepoint_stub_, \
StubCode::CallNativeThroughSafepoint().ptr(), nullptr)
#define CACHED_NON_VM_STUB_LIST(V) \
V(ObjectPtr, object_null_, Object::null(), nullptr) \
V(BoolPtr, bool_true_, Object::bool_true().ptr(), nullptr) \
V(BoolPtr, bool_false_, Object::bool_false().ptr(), nullptr) \
V(ArrayPtr, empty_array_, Object::empty_array().ptr(), nullptr) \
V(TypeArgumentsPtr, empty_type_arguments_, \
Object::empty_type_arguments().ptr(), nullptr) \
V(TypePtr, dynamic_type_, Type::dynamic_type().ptr(), nullptr)
// List of VM-global objects/addresses cached in each Thread object.
// Important: constant false must immediately follow constant true.
#define CACHED_VM_OBJECTS_LIST(V) \
CACHED_NON_VM_STUB_LIST(V) \
CACHED_VM_STUBS_LIST(V)
#define CACHED_FUNCTION_ENTRY_POINTS_LIST(V) \
V(suspend_state_init_async) \
V(suspend_state_await) \
V(suspend_state_await_with_type_check) \
V(suspend_state_return_async) \
V(suspend_state_return_async_not_future) \
V(suspend_state_init_async_star) \
V(suspend_state_yield_async_star) \
V(suspend_state_return_async_star) \
V(suspend_state_init_sync_star) \
V(suspend_state_suspend_sync_star_at_start) \
V(suspend_state_handle_exception)
// This assertion marks places which assume that boolean false immediate
// follows bool true in the CACHED_VM_OBJECTS_LIST
#define ASSERT_BOOL_FALSE_FOLLOWS_BOOL_TRUE() \
ASSERT((Thread::bool_true_offset() + kWordSize) == \
Thread::bool_false_offset());
#define CACHED_VM_STUBS_ADDRESSES_LIST(V) \
V(uword, write_barrier_entry_point_, StubCode::WriteBarrier().EntryPoint(), \
0) \
V(uword, array_write_barrier_entry_point_, \
StubCode::ArrayWriteBarrier().EntryPoint(), 0) \
V(uword, call_to_runtime_entry_point_, \
StubCode::CallToRuntime().EntryPoint(), 0) \
V(uword, allocate_mint_with_fpu_regs_entry_point_, \
StubCode::AllocateMintSharedWithFPURegs().EntryPoint(), 0) \
V(uword, allocate_mint_without_fpu_regs_entry_point_, \
StubCode::AllocateMintSharedWithoutFPURegs().EntryPoint(), 0) \
V(uword, allocate_object_entry_point_, \
StubCode::AllocateObject().EntryPoint(), 0) \
V(uword, allocate_object_parameterized_entry_point_, \
StubCode::AllocateObjectParameterized().EntryPoint(), 0) \
V(uword, allocate_object_slow_entry_point_, \
StubCode::AllocateObjectSlow().EntryPoint(), 0) \
V(uword, stack_overflow_shared_without_fpu_regs_entry_point_, \
StubCode::StackOverflowSharedWithoutFPURegs().EntryPoint(), 0) \
V(uword, stack_overflow_shared_with_fpu_regs_entry_point_, \
StubCode::StackOverflowSharedWithFPURegs().EntryPoint(), 0) \
V(uword, megamorphic_call_checked_entry_, \
StubCode::MegamorphicCall().EntryPoint(), 0) \
V(uword, switchable_call_miss_entry_, \
StubCode::SwitchableCallMiss().EntryPoint(), 0) \
V(uword, optimize_entry_, StubCode::OptimizeFunction().EntryPoint(), 0) \
V(uword, deoptimize_entry_, StubCode::Deoptimize().EntryPoint(), 0) \
V(uword, call_native_through_safepoint_entry_point_, \
StubCode::CallNativeThroughSafepoint().EntryPoint(), 0) \
V(uword, jump_to_frame_entry_point_, StubCode::JumpToFrame().EntryPoint(), \
0) \
V(uword, slow_type_test_entry_point_, StubCode::SlowTypeTest().EntryPoint(), \
0) \
V(uword, resume_interpreter_adjusted_entry_point_, \
StubCode::ResumeInterpreter().EntryPoint() + \
SuspendStubABI::kResumePcDistance, \
0)
#define CACHED_ADDRESSES_LIST(V) \
CACHED_VM_STUBS_ADDRESSES_LIST(V) \
V(uword, bootstrap_native_wrapper_entry_point_, \
NativeEntry::BootstrapNativeCallWrapperEntry(), 0) \
V(uword, no_scope_native_wrapper_entry_point_, \
NativeEntry::NoScopeNativeCallWrapperEntry(), 0) \
V(uword, auto_scope_native_wrapper_entry_point_, \
NativeEntry::AutoScopeNativeCallWrapperEntry(), 0) \
V(uword, interpret_call_entry_point_, RuntimeEntry::InterpretCallEntry(), 0) \
V(StringPtr*, predefined_symbols_address_, Symbols::PredefinedAddress(), \
nullptr) \
V(uword, double_nan_address_, reinterpret_cast<uword>(&double_nan_constant), \
0) \
V(uword, double_negate_address_, \
reinterpret_cast<uword>(&double_negate_constant), 0) \
V(uword, double_abs_address_, reinterpret_cast<uword>(&double_abs_constant), \
0) \
V(uword, float_not_address_, reinterpret_cast<uword>(&float_not_constant), \
0) \
V(uword, float_negate_address_, \
reinterpret_cast<uword>(&float_negate_constant), 0) \
V(uword, float_absolute_address_, \
reinterpret_cast<uword>(&float_absolute_constant), 0) \
V(uword, float_zerow_address_, \
reinterpret_cast<uword>(&float_zerow_constant), 0)
#define CACHED_CONSTANTS_LIST(V) \
CACHED_VM_OBJECTS_LIST(V) \
CACHED_ADDRESSES_LIST(V)
enum class ValidationPolicy {
kValidateFrames = 0,
kDontValidateFrames = 1,
};
enum class RuntimeCallDeoptAbility {
// There was no leaf call or a leaf call that can cause deoptimization
// after-call.
kCanLazyDeopt,
// There was a leaf call and the VM cannot cause deoptimize after-call.
kCannotLazyDeopt,
};
// The safepoint level a thread is on or a safepoint operation is requested for
//
// The higher the number the stronger the guarantees:
// * the time-to-safepoint latency increases with level
// * the frequency of hitting possible safe points decreases with level
enum SafepointLevel {
// Safe to GC
kGC,
// Safe to GC as well as Deopt.
kGCAndDeopt,
// Safe to GC, Deopt as well as Reload.
kGCAndDeoptAndReload,
// Number of levels.
kNumLevels,
// No safepoint.
kNoSafepoint,
};
// Accessed from generated code.
struct TsanUtils {
// Used to allow unwinding runtime C frames using longjmp() when throwing
// exceptions. This allows triggering the normal TSAN shadow stack unwinding
// implementation.
// -> See https://dartbug.com/47472#issuecomment-948235479 for details.
#if defined(USING_THREAD_SANITIZER)
void* setjmp_function = reinterpret_cast<void*>(&DART_SETJMP);
#else
// MSVC (on Windows) is not happy with getting address of purely intrinsic.
void* setjmp_function = nullptr;
#endif
jmp_buf* setjmp_buffer = nullptr;
uword exception_pc = 0;
uword exception_sp = 0;
uword exception_fp = 0;
static intptr_t setjmp_function_offset() {
return OFFSET_OF(TsanUtils, setjmp_function);
}
static intptr_t setjmp_buffer_offset() {
return OFFSET_OF(TsanUtils, setjmp_buffer);
}
static intptr_t exception_pc_offset() {
return OFFSET_OF(TsanUtils, exception_pc);
}
static intptr_t exception_sp_offset() {
return OFFSET_OF(TsanUtils, exception_sp);
}
static intptr_t exception_fp_offset() {
return OFFSET_OF(TsanUtils, exception_fp);
}
};
// A VM thread; may be executing Dart code or performing helper tasks like
// garbage collection or compilation. The Thread structure associated with
// a thread is allocated by EnsureInit before entering an isolate, and destroyed
// automatically when the underlying OS thread exits. NOTE: On Windows, CleanUp
// must currently be called manually (issue 23474).
class Thread : public ThreadState {
public:
// The kind of task this thread is performing. Sampled by the profiler.
enum TaskKind {
kUnknownTask = 0,
kMutatorTask,
kCompilerTask,
kMarkerTask,
kSweeperTask,
kCompactorTask,
kScavengerTask,
kSampleBlockTask,
kIncrementalCompactorTask,
kSpawnTask,
};
~Thread();
// The currently executing thread, or nullptr if not yet initialized.
static Thread* Current() {
return static_cast<Thread*>(OSThread::CurrentVMThread());
}
// Whether there's any active state on the [thread] that needs to be preserved
// across `Thread::ExitIsolate()` and `Thread::EnterIsolate()`.
bool HasActiveState();
void AssertNonMutatorInvariants();
void AssertNonDartMutatorInvariants();
void AssertEmptyStackInvariants();
void AssertEmptyThreadInvariants();
// Makes the current thread enter 'isolate'.
static void EnterIsolate(Isolate* isolate);
// Makes the current thread exit its isolate.
static void ExitIsolate(bool isolate_shutdown = false);
static bool EnterIsolateGroupAsHelper(IsolateGroup* isolate_group,
TaskKind kind,
bool bypass_safepoint);
static void ExitIsolateGroupAsHelper(bool bypass_safepoint);
static bool EnterIsolateGroupAsNonMutator(IsolateGroup* isolate_group,
TaskKind kind);
static void ExitIsolateGroupAsNonMutator();
// Empties the store buffer block into the isolate.
void ReleaseStoreBuffer();
void AcquireMarkingStack();
void ReleaseMarkingStack();
void SetStackLimit(uword value);
void ClearStackLimit();
// Access to the current stack limit for generated code. Either the true OS
// thread's stack limit minus some headroom, or a special value to trigger
// interrupts.
uword stack_limit_address() const {
return reinterpret_cast<uword>(&stack_limit_);
}
static intptr_t stack_limit_offset() {
return OFFSET_OF(Thread, stack_limit_);
}
// The true stack limit for this OS thread.
static intptr_t saved_stack_limit_offset() {
return OFFSET_OF(Thread, saved_stack_limit_);
}
uword saved_stack_limit() const { return saved_stack_limit_; }
#if defined(USING_SAFE_STACK)
uword saved_safestack_limit() const { return saved_safestack_limit_; }
void set_saved_safestack_limit(uword limit) {
saved_safestack_limit_ = limit;
}
#endif
uword saved_shadow_call_stack() const { return saved_shadow_call_stack_; }
static uword saved_shadow_call_stack_offset() {
return OFFSET_OF(Thread, saved_shadow_call_stack_);
}
// Stack overflow flags
enum {
kOsrRequest = 0x1, // Current stack overflow caused by OSR request.
};
uword write_barrier_mask() const { return write_barrier_mask_; }
uword heap_base() const {
#if defined(DART_COMPRESSED_POINTERS)
return heap_base_;
#else
return 0;
#endif
}
static intptr_t write_barrier_mask_offset() {
return OFFSET_OF(Thread, write_barrier_mask_);
}
#if defined(DART_COMPRESSED_POINTERS)
static intptr_t heap_base_offset() { return OFFSET_OF(Thread, heap_base_); }
#endif
static intptr_t stack_overflow_flags_offset() {
return OFFSET_OF(Thread, stack_overflow_flags_);
}
int32_t IncrementAndGetStackOverflowCount() {
return ++stack_overflow_count_;
}
uint32_t IncrementAndGetRuntimeCallCount() { return ++runtime_call_count_; }
static uword stack_overflow_shared_stub_entry_point_offset(bool fpu_regs) {
return fpu_regs
? stack_overflow_shared_with_fpu_regs_entry_point_offset()
: stack_overflow_shared_without_fpu_regs_entry_point_offset();
}
static intptr_t safepoint_state_offset() {
return OFFSET_OF(Thread, safepoint_state_);
}
// Tag state is maintained on transitions.
enum {
// Always true in generated state.
kDidNotExit = 0,
// The VM exited the generated state through FFI.
// This can be true in both native and VM state.
kExitThroughFfi = 1,
// The VM exited the generated state through a runtime call.
// This can be true in both native and VM state.
kExitThroughRuntimeCall = 2,
};
uword exit_through_ffi() { return exit_through_ffi_; }
static intptr_t exit_through_ffi_offset() {
return OFFSET_OF(Thread, exit_through_ffi_);
}
TaskKind task_kind() const { return task_kind_; }
void set_task_kind(TaskKind kind) { task_kind_ = kind; }
// Retrieves and clears the stack overflow flags. These are set by
// the generated code before the slow path runtime routine for a
// stack overflow is called.
uword GetAndClearStackOverflowFlags();
// Interrupt bits.
enum {
kVMInterrupt = 0x1, // Internal VM checks: safepoints, store buffers, etc.
kMessageInterrupt = 0x2, // An interrupt to process an out of band message.
kInterruptsMask = (kVMInterrupt | kMessageInterrupt),
};
void ScheduleInterrupts(uword interrupt_bits);
ErrorPtr HandleInterrupts();
ErrorPtr HandleInterrupts(uword interrupt_bits);
uword GetAndClearInterrupts();
bool HasScheduledInterrupts() const {
return (stack_limit_.load() & kInterruptsMask) != 0;
}
// Monitor corresponding to this thread.
Monitor* thread_lock() const { return &thread_lock_; }
// The reusable api local scope for this thread.
ApiLocalScope* api_reusable_scope() const { return api_reusable_scope_; }
void set_api_reusable_scope(ApiLocalScope* value) {
ASSERT(value == nullptr || api_reusable_scope_ == nullptr);
api_reusable_scope_ = value;
}
// The api local scope for this thread, this where all local handles
// are allocated.
ApiLocalScope* api_top_scope() const { return api_top_scope_; }
void set_api_top_scope(ApiLocalScope* value) { api_top_scope_ = value; }
static intptr_t api_top_scope_offset() {
return OFFSET_OF(Thread, api_top_scope_);
}
void EnterApiScope();
void ExitApiScope();
static intptr_t double_truncate_round_supported_offset() {
return OFFSET_OF(Thread, double_truncate_round_supported_);
}
static intptr_t tsan_utils_offset() { return OFFSET_OF(Thread, tsan_utils_); }
#if defined(USING_THREAD_SANITIZER)
uword exit_through_ffi() const { return exit_through_ffi_; }
TsanUtils* tsan_utils() const { return tsan_utils_; }
#endif // defined(USING_THREAD_SANITIZER)
// The isolate that this thread is operating on, or nullptr if none.
Isolate* isolate() const { return isolate_; }
static intptr_t isolate_offset() { return OFFSET_OF(Thread, isolate_); }
static intptr_t isolate_group_offset() {
return OFFSET_OF(Thread, isolate_group_);
}
// The isolate group that this thread is operating on, or nullptr if none.
IsolateGroup* isolate_group() const { return isolate_group_; }
static intptr_t field_table_values_offset() {
return OFFSET_OF(Thread, field_table_values_);
}
static intptr_t shared_field_table_values_offset() {
return OFFSET_OF(Thread, shared_field_table_values_);
}
bool IsDartMutatorThread() const {
return scheduled_dart_mutator_isolate_ != nullptr;
}
// Returns the dart mutator [Isolate] this thread belongs to or nullptr.
//
// `isolate()` in comparison can return
// - `nullptr` for dart mutators (e.g. if the mutator runs under
// [NoActiveIsolateScope])
// - an incorrect isolate (e.g. if [ActiveIsolateScope] is used to seemingly
// enter another isolate)
Isolate* scheduled_dart_mutator_isolate() const {
return scheduled_dart_mutator_isolate_;
}
#if defined(DEBUG)
bool IsInsideCompiler() const { return inside_compiler_; }
#endif
// Offset of Dart TimelineStream object.
static intptr_t dart_stream_offset() {
return OFFSET_OF(Thread, dart_stream_);
}
// Offset of the Dart VM Service Extension StreamInfo object.
static intptr_t service_extension_stream_offset() {
return OFFSET_OF(Thread, service_extension_stream_);
}
// Is |this| executing Dart code?
bool IsExecutingDartCode() const;
// Has |this| exited Dart code?
bool HasExitedDartCode() const;
bool HasCompilerState() const { return compiler_state_ != nullptr; }
CompilerState& compiler_state() {
ASSERT(HasCompilerState());
return *compiler_state_;
}
HierarchyInfo* hierarchy_info() const {
ASSERT(isolate_group_ != nullptr);
return hierarchy_info_;
}
void set_hierarchy_info(HierarchyInfo* value) {
ASSERT(isolate_group_ != nullptr);
ASSERT((hierarchy_info_ == nullptr && value != nullptr) ||
(hierarchy_info_ != nullptr && value == nullptr));
hierarchy_info_ = value;
}
TypeUsageInfo* type_usage_info() const {
ASSERT(isolate_group_ != nullptr);
return type_usage_info_;
}
void set_type_usage_info(TypeUsageInfo* value) {
ASSERT(isolate_group_ != nullptr);
ASSERT((type_usage_info_ == nullptr && value != nullptr) ||
(type_usage_info_ != nullptr && value == nullptr));
type_usage_info_ = value;
}
CompilerTimings* compiler_timings() const { return compiler_timings_; }
void set_compiler_timings(CompilerTimings* stats) {
compiler_timings_ = stats;
}
int32_t no_callback_scope_depth() const { return no_callback_scope_depth_; }
void IncrementNoCallbackScopeDepth() {
ASSERT(no_callback_scope_depth_ < INT_MAX);
no_callback_scope_depth_ += 1;
}
void DecrementNoCallbackScopeDepth() {
ASSERT(no_callback_scope_depth_ > 0);
no_callback_scope_depth_ -= 1;
}
bool force_growth() const { return force_growth_scope_depth_ != 0; }
void IncrementForceGrowthScopeDepth() {
ASSERT(force_growth_scope_depth_ < INT_MAX);
force_growth_scope_depth_ += 1;
}
void DecrementForceGrowthScopeDepth() {
ASSERT(force_growth_scope_depth_ > 0);
force_growth_scope_depth_ -= 1;
}
bool is_unwind_in_progress() const { return is_unwind_in_progress_; }
void StartUnwindError() {
is_unwind_in_progress_ = true;
SetUnwindErrorInProgress(true);
}
#if defined(DEBUG)
void EnterCompiler() {
ASSERT(!IsInsideCompiler());
inside_compiler_ = true;
}
void LeaveCompiler() {
ASSERT(IsInsideCompiler());
inside_compiler_ = false;
}
#endif
void StoreBufferAddObject(ObjectPtr obj);
void StoreBufferAddObjectGC(ObjectPtr obj);
#if defined(TESTING)
bool StoreBufferContains(ObjectPtr obj) const {
return store_buffer_block_->Contains(obj);
}
#endif
void StoreBufferBlockProcess(StoreBuffer::ThresholdPolicy policy);
void StoreBufferReleaseGC();
void StoreBufferAcquireGC();
static intptr_t store_buffer_block_offset() {
return OFFSET_OF(Thread, store_buffer_block_);
}
bool is_marking() const { return old_marking_stack_block_ != nullptr; }
void MarkingStackAddObject(ObjectPtr obj);
void OldMarkingStackAddObject(ObjectPtr obj);
void NewMarkingStackAddObject(ObjectPtr obj);
void DeferredMarkingStackAddObject(ObjectPtr obj);
void OldMarkingStackBlockProcess();
void NewMarkingStackBlockProcess();
void DeferredMarkingStackBlockProcess();
static intptr_t old_marking_stack_block_offset() {
return OFFSET_OF(Thread, old_marking_stack_block_);
}
static intptr_t new_marking_stack_block_offset() {
return OFFSET_OF(Thread, new_marking_stack_block_);
}
uword top_exit_frame_info() const { return top_exit_frame_info_; }
void set_top_exit_frame_info(uword top_exit_frame_info) {
top_exit_frame_info_ = top_exit_frame_info;
}
static intptr_t top_exit_frame_info_offset() {
return OFFSET_OF(Thread, top_exit_frame_info_);
}
Heap* heap() const;
// The TLAB memory boundaries.
//
// When the heap sampling profiler is enabled, we use the TLAB boundary to
// trigger slow path allocations so we can take a sample. This means that
// true_end() >= end(), where true_end() is the actual end address of the
// TLAB and end() is the chosen sampling boundary for the thread.
//
// When the heap sampling profiler is disabled, true_end() == end().
uword top() const { return top_.load(std::memory_order_relaxed); }
uword end() const { return end_; }
uword true_end() const { return true_end_; }
void set_top(uword top) { top_.store(top, std::memory_order_relaxed); }
void set_end(uword end) { end_ = end; }
void set_true_end(uword true_end) { true_end_ = true_end; }
static intptr_t top_offset() { return OFFSET_OF(Thread, top_); }
static intptr_t end_offset() { return OFFSET_OF(Thread, end_); }
int32_t no_safepoint_scope_depth() const {
#if defined(DEBUG)
return no_safepoint_scope_depth_;
#else
return 0;
#endif
}
void IncrementNoSafepointScopeDepth() {
#if defined(DEBUG)
ASSERT(no_safepoint_scope_depth_ < INT_MAX);
no_safepoint_scope_depth_ += 1;
#endif
}
void DecrementNoSafepointScopeDepth() {
#if defined(DEBUG)
ASSERT(no_safepoint_scope_depth_ > 0);
no_safepoint_scope_depth_ -= 1;
#endif
}
bool IsInNoReloadScope() const { return no_reload_scope_depth_ > 0; }
bool IsInStoppedMutatorsScope() const {
return stopped_mutators_scope_depth_ > 0;
}
bool IsInNoThrowOOMScope() const { return no_throw_oom_scope_depth_ > 0; }
#define DEFINE_OFFSET_METHOD(type_name, member_name, expr, default_init_value) \
static intptr_t member_name##offset() { \
return OFFSET_OF(Thread, member_name); \
}
CACHED_CONSTANTS_LIST(DEFINE_OFFSET_METHOD)
#undef DEFINE_OFFSET_METHOD
static intptr_t write_barrier_wrappers_thread_offset(Register reg) {
ASSERT((kDartAvailableCpuRegs & (1 << reg)) != 0);
intptr_t index = 0;
for (intptr_t i = 0; i < kNumberOfCpuRegisters; ++i) {
if ((kDartAvailableCpuRegs & (1 << i)) == 0) continue;
if (i == reg) break;
++index;
}
return OFFSET_OF(Thread, write_barrier_wrappers_entry_points_) +
index * sizeof(uword);
}
static intptr_t WriteBarrierWrappersOffsetForRegister(Register reg) {
intptr_t index = 0;
for (intptr_t i = 0; i < kNumberOfCpuRegisters; ++i) {
if ((kDartAvailableCpuRegs & (1 << i)) == 0) continue;
if (i == reg) {
return index * kStoreBufferWrapperSize;
}
++index;
}
UNREACHABLE();
return 0;
}
#define DEFINE_OFFSET_METHOD(name) \
static intptr_t name##_entry_point_offset() { \
return OFFSET_OF(Thread, name##_entry_point_); \
}
RUNTIME_ENTRY_LIST(DEFINE_OFFSET_METHOD)
#undef DEFINE_OFFSET_METHOD
#define DEFINE_OFFSET_METHOD(returntype, name, ...) \
static intptr_t name##_entry_point_offset() { \
return OFFSET_OF(Thread, name##_entry_point_); \
}
LEAF_RUNTIME_ENTRY_LIST(DEFINE_OFFSET_METHOD)
#undef DEFINE_OFFSET_METHOD
ObjectPoolPtr global_object_pool() const { return global_object_pool_; }
void set_global_object_pool(ObjectPoolPtr raw_value) {
global_object_pool_ = raw_value;
}
const uword* dispatch_table_array() const { return dispatch_table_array_; }
void set_dispatch_table_array(const uword* array) {
dispatch_table_array_ = array;
}
static bool CanLoadFromThread(const Object& object);
static intptr_t OffsetFromThread(const Object& object);
static bool ObjectAtOffset(intptr_t offset, Object* object);
static intptr_t OffsetFromThread(const RuntimeEntry* runtime_entry);
#define DEFINE_OFFSET_METHOD(name) \
static intptr_t name##_entry_point_offset() { \
return OFFSET_OF(Thread, name##_entry_point_); \
}
CACHED_FUNCTION_ENTRY_POINTS_LIST(DEFINE_OFFSET_METHOD)
#undef DEFINE_OFFSET_METHOD
#if defined(DEBUG)
// For asserts only. Has false positives when running with a simulator or
// SafeStack.
bool TopErrorHandlerIsSetJump() const;
bool TopErrorHandlerIsExitFrame() const;
#endif
uword vm_tag() const { return vm_tag_; }
void set_vm_tag(uword tag) { vm_tag_ = tag; }
static intptr_t vm_tag_offset() { return OFFSET_OF(Thread, vm_tag_); }
int64_t unboxed_int64_runtime_arg() const {
return unboxed_runtime_arg_.int64_storage[0];
}
void set_unboxed_int64_runtime_arg(int64_t value) {
unboxed_runtime_arg_.int64_storage[0] = value;
}
int64_t unboxed_int64_runtime_second_arg() const {
return unboxed_runtime_arg_.int64_storage[1];
}
void set_unboxed_int64_runtime_second_arg(int64_t value) {
unboxed_runtime_arg_.int64_storage[1] = value;
}
double unboxed_double_runtime_arg() const {
return unboxed_runtime_arg_.double_storage[0];
}
void set_unboxed_double_runtime_arg(double value) {
unboxed_runtime_arg_.double_storage[0] = value;
}
simd128_value_t unboxed_simd128_runtime_arg() const {
return unboxed_runtime_arg_;
}
void set_unboxed_simd128_runtime_arg(simd128_value_t value) {
unboxed_runtime_arg_ = value;
}
static intptr_t unboxed_runtime_arg_offset() {
return OFFSET_OF(Thread, unboxed_runtime_arg_);
}
static intptr_t global_object_pool_offset() {
return OFFSET_OF(Thread, global_object_pool_);
}
static intptr_t dispatch_table_array_offset() {
return OFFSET_OF(Thread, dispatch_table_array_);
}
ObjectPtr active_exception() const { return active_exception_; }
void set_active_exception(const Object& value);
void set_active_exception(LocalHandle* value);
static intptr_t active_exception_offset() {
return OFFSET_OF(Thread, active_exception_);
}
ObjectPtr active_stacktrace() const { return active_stacktrace_; }
void set_active_stacktrace(const Object& value);
static intptr_t active_stacktrace_offset() {
return OFFSET_OF(Thread, active_stacktrace_);
}
uword resume_pc() const { return resume_pc_; }
void set_resume_pc(uword value) { resume_pc_ = value; }
static uword resume_pc_offset() { return OFFSET_OF(Thread, resume_pc_); }
ErrorPtr sticky_error() const;
void set_sticky_error(const Error& value);
void ClearStickyError();
DART_WARN_UNUSED_RESULT ErrorPtr StealStickyError();
#if defined(DEBUG)
#define REUSABLE_HANDLE_SCOPE_ACCESSORS(object) \
void set_reusable_##object##_handle_scope_active(bool value) { \
reusable_##object##_handle_scope_active_ = value; \
} \
bool reusable_##object##_handle_scope_active() const { \
return reusable_##object##_handle_scope_active_; \
}
REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_SCOPE_ACCESSORS)
#undef REUSABLE_HANDLE_SCOPE_ACCESSORS
bool IsAnyReusableHandleScopeActive() const {
#define IS_REUSABLE_HANDLE_SCOPE_ACTIVE(object) \
if (reusable_##object##_handle_scope_active_) { \
return true; \
}
REUSABLE_HANDLE_LIST(IS_REUSABLE_HANDLE_SCOPE_ACTIVE)
return false;
#undef IS_REUSABLE_HANDLE_SCOPE_ACTIVE
}
#endif // defined(DEBUG)
void ClearReusableHandles();
#define REUSABLE_HANDLE(object) \
object& object##Handle() const { return *object##_handle_; }
REUSABLE_HANDLE_LIST(REUSABLE_HANDLE)
#undef REUSABLE_HANDLE
static bool IsAtSafepoint(SafepointLevel level, uword state) {
const uword mask = AtSafepointBits(level);
return (state & mask) == mask;
}
// Whether the current thread is owning any safepoint level.
bool IsAtSafepoint() const {
// Owning a higher level safepoint implies owning the lower levels as well.
return IsAtSafepoint(SafepointLevel::kGC);
}
bool IsAtSafepoint(SafepointLevel level) const {
return IsAtSafepoint(level, safepoint_state_.load());
}
void SetAtSafepoint(bool value, SafepointLevel level) {
ASSERT(thread_lock()->IsOwnedByCurrentThread());
ASSERT(level <= current_safepoint_level());
if (value) {
safepoint_state_ |= AtSafepointBits(level);
} else {
safepoint_state_ &= ~AtSafepointBits(level);
}
}
bool IsSafepointRequestedLocked(SafepointLevel level) const {
ASSERT(thread_lock()->IsOwnedByCurrentThread());
return IsSafepointRequested(level);
}
bool IsSafepointRequested() const {
return IsSafepointRequested(current_safepoint_level());
}
bool IsSafepointRequested(SafepointLevel level) const {
const uword state = safepoint_state_.load();
for (intptr_t i = level; i >= 0; --i) {
if (IsSafepointLevelRequested(state, static_cast<SafepointLevel>(i)))
return true;
}
return false;
}
bool IsSafepointLevelRequestedLocked(SafepointLevel level) const {
ASSERT(thread_lock()->IsOwnedByCurrentThread());
if (level > current_safepoint_level()) return false;
const uword state = safepoint_state_.load();
return IsSafepointLevelRequested(state, level);
}
static bool IsSafepointLevelRequested(uword state, SafepointLevel level) {
switch (level) {
case SafepointLevel::kGC:
return SafepointRequestedField::decode(state);
case SafepointLevel::kGCAndDeopt:
return DeoptSafepointRequestedField::decode(state);
case SafepointLevel::kGCAndDeoptAndReload:
return ReloadSafepointRequestedField::decode(state);
default:
UNREACHABLE();
}
}
void BlockForSafepoint();
uword SetSafepointRequested(SafepointLevel level, bool value) {
ASSERT(thread_lock()->IsOwnedByCurrentThread());
uword mask = 0;
switch (level) {
case SafepointLevel::kGC:
mask = SafepointRequestedField::mask_in_place();
break;
case SafepointLevel::kGCAndDeopt:
mask = DeoptSafepointRequestedField::mask_in_place();
break;
case SafepointLevel::kGCAndDeoptAndReload:
mask = ReloadSafepointRequestedField::mask_in_place();
break;