-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
Expand file tree
/
Copy pathwasm-engine.cc
More file actions
2029 lines (1825 loc) Β· 80.1 KB
/
wasm-engine.cc
File metadata and controls
2029 lines (1825 loc) Β· 80.1 KB
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 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/wasm-engine.h"
#include <optional>
#include "src/base/functional.h"
#include "src/base/platform/time.h"
#include "src/base/small-vector.h"
#include "src/common/assert-scope.h"
#include "src/common/globals.h"
#include "src/debug/debug.h"
#include "src/diagnostics/code-tracer.h"
#include "src/diagnostics/compilation-statistics.h"
#include "src/execution/frames.h"
#include "src/execution/v8threads.h"
#include "src/handles/global-handles-inl.h"
#include "src/logging/counters.h"
#include "src/logging/metrics.h"
#include "src/objects/heap-number.h"
#include "src/objects/managed-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/objects.h"
#include "src/objects/primitive-heap-object.h"
#include "src/utils/ostreams.h"
#include "src/wasm/function-compiler.h"
#include "src/wasm/module-compiler.h"
#include "src/wasm/module-decoder.h"
#include "src/wasm/module-instantiate.h"
#include "src/wasm/pgo.h"
#include "src/wasm/stacks.h"
#include "src/wasm/std-object-sizes.h"
#include "src/wasm/streaming-decoder.h"
#include "src/wasm/wasm-debug.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-objects-inl.h"
#if V8_ENABLE_DRUMBRAKE
#include "src/wasm/interpreter/wasm-interpreter-inl.h"
#endif // V8_ENABLE_DRUMBRAKE
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
#include "src/debug/wasm/gdb-server/gdb-server.h"
#endif // V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
namespace v8::internal::wasm {
#define TRACE_CODE_GC(...) \
do { \
if (v8_flags.trace_wasm_code_gc) PrintF("[wasm-gc] " __VA_ARGS__); \
} while (false)
// This class exists in order to solve a shutdown ordering problem.
// The basic situation is that the process-global WasmEngine has, for each
// Isolate that it knows about, a map from NativeModule to Script, using
// WeakScriptHandles to make sure that the NativeModules, which are shared
// across the process, don't keep the (Isolate-specific) Scripts alive.
// In the other direction, the Scripts keep the NativeModule alive, IOW
// usually the Scripts die first, and the WeakScriptHandles are cleared
// before being freed.
// In case of asm.js modules and in case of Isolate shutdown, it can happen
// that the NativeModule dies first, so the WeakScriptHandles are no longer
// needed and should be destroyed. That can only happen on the main thread of
// the Isolate they belong to, whereas the last thread that releases a
// NativeModule might be any other thread, so we post a
// ClearWeakScriptHandleTask to that isolate's foreground task runner.
// In case of Isolate shutdown at an inconvenient moment, this task runner can
// destroy all waiting tasks; and *afterwards* global handles are freed, which
// writes to the memory location backing the handle, so this bit of memory must
// not be owned by (and die with) the ClearWeakScriptHandleTask.
// The solution is this class here: its instances form a linked list owned by
// the Isolate to which the referenced Scripts belong. Its name refers to the
// fact that it stores global handles that used to have a purpose but are now
// just waiting for the right thread to destroy them.
// If the ClearWeakScriptHandleTask gets to run (i.e. in the regular case),
// it destroys the weak global handle and then the WasmOrphanedGlobalHandle
// container, removing it from the isolate's list.
// If the ClearWeakScriptHandleTask is destroyed before it runs, the isolate's
// list of WasmOrphanedGlobalHandles isn't modified, so the indirection cell
// is still around when all remaining global handles are freed; nevertheless
// it won't leak because the Isolate owns it and will free it.
class WasmOrphanedGlobalHandle {
public:
WasmOrphanedGlobalHandle() = default;
void InitializeLocation(std::unique_ptr<Address*> location) {
location_ = std::move(location);
}
static void Destroy(WasmOrphanedGlobalHandle* that) {
// Destroy the global handle if it still exists.
Address** location = that->location_.get();
if (location) GlobalHandles::Destroy(*location);
that->location_.reset();
// Unlink and free the container.
*that->prev_ptr_ = that->next_;
if (that->next_ != nullptr) that->next_->prev_ptr_ = that->prev_ptr_;
// This function could be a non-static method, but then the next line
// would read "delete this", which is UB.
delete that;
}
private:
friend class WasmEngine;
// This is a doubly linked list with a twist: the {next_} pointer is just
// what you would expect, whereas {prev_ptr_} points at the slot inside
// the previous element that's pointing at the current element. The purpose
// of this design is to make it possible for the previous element to be
// the {Isolate::wasm_orphaned_handle_} field, without requiring any
// special-casing in the insert and delete operations.
WasmOrphanedGlobalHandle* next_ = nullptr;
WasmOrphanedGlobalHandle** prev_ptr_ = nullptr;
std::unique_ptr<Address*> location_;
};
// static
WasmOrphanedGlobalHandle* WasmEngine::NewOrphanedGlobalHandle(
WasmOrphanedGlobalHandle** pointer) {
// No need for additional locking: this is only ever called indirectly
// from {WasmEngine::ClearWeakScriptHandle()}, which holds the engine-wide
// {mutex_}.
WasmOrphanedGlobalHandle* orphan = new WasmOrphanedGlobalHandle();
orphan->next_ = *pointer;
orphan->prev_ptr_ = pointer;
if (orphan->next_ != nullptr) orphan->next_->prev_ptr_ = &orphan->next_;
*pointer = orphan;
return orphan;
}
// static
void WasmEngine::FreeAllOrphanedGlobalHandles(WasmOrphanedGlobalHandle* start) {
// This is meant to be called from ~Isolate, so we no longer care about
// maintaining invariants: the only task is to free memory to prevent leaks.
while (start != nullptr) {
WasmOrphanedGlobalHandle* next = start->next_;
delete start;
start = next;
}
}
// A task to log a set of {WasmCode} objects in an isolate. It does not own any
// data itself, since it is owned by the platform, so lifetime is not really
// bound to the wasm engine.
class WasmEngine::LogCodesTask : public CancelableTask {
friend class WasmEngine;
public:
explicit LogCodesTask(Isolate* isolate)
: CancelableTask(isolate), isolate_(isolate) {}
void RunInternal() override {
GetWasmEngine()->LogOutstandingCodesForIsolate(isolate_);
}
private:
Isolate* const isolate_;
};
namespace {
void CheckNoArchivedThreads(Isolate* isolate) {
class ArchivedThreadsVisitor : public ThreadVisitor {
void VisitThread(Isolate* isolate, ThreadLocalTop* top) override {
// Archived threads are rarely used, and not combined with Wasm at the
// moment. Implement this and test it properly once we have a use case for
// that.
FATAL("archived threads in combination with wasm not supported");
}
} archived_threads_visitor;
isolate->thread_manager()->IterateArchivedThreads(&archived_threads_visitor);
}
class WasmGCForegroundTask : public CancelableTask {
public:
explicit WasmGCForegroundTask(Isolate* isolate)
: CancelableTask(isolate->cancelable_task_manager()), isolate_(isolate) {}
void RunInternal() final {
// The stack can contain live frames, for instance when this is invoked
// during a pause or a breakpoint.
GetWasmEngine()->ReportLiveCodeFromStackForGC(isolate_);
}
private:
Isolate* isolate_;
};
class ClearWeakScriptHandleTask : public CancelableTask {
public:
explicit ClearWeakScriptHandleTask(Isolate* isolate,
std::unique_ptr<Address*> location)
: CancelableTask(isolate->cancelable_task_manager()) {
handle_ = isolate->NewWasmOrphanedGlobalHandle();
handle_->InitializeLocation(std::move(location));
}
// We don't override the destructor, because there is nothing to do:
// if the task is deleted before it was run, then everything is shutting
// down anyway, so destroying the GlobalHandle is no longer relevant (and
// it might well be too late to do that safely).
void RunInternal() override {
WasmOrphanedGlobalHandle::Destroy(handle_);
handle_ = nullptr;
}
private:
// This is owned by the Isolate to ensure correct shutdown ordering.
WasmOrphanedGlobalHandle* handle_;
};
class WeakScriptHandle {
public:
WeakScriptHandle(DirectHandle<Script> script, Isolate* isolate)
: script_id_(script->id()), isolate_(isolate) {
DCHECK(IsString(script->name()) || IsUndefined(script->name()));
if (IsString(script->name())) {
source_url_ = Cast<String>(script->name())->ToCString();
}
auto global_handle =
script->GetIsolate()->global_handles()->Create(*script);
location_ = std::make_unique<Address*>(global_handle.location());
GlobalHandles::MakeWeak(location_.get());
}
~WeakScriptHandle() {
// Usually the destructor of this class is called after the weak callback,
// because the Script keeps the NativeModule alive. In that case,
// {location_} is already cleared, and there is nothing to do.
if (location_ == nullptr || *location_ == nullptr) return;
// For asm.js modules, the Script usually outlives the NativeModule.
// We must destroy the GlobalHandle before freeing the memory that's
// backing {location_}, so that when the Script does die eventually, there
// is no lingering weak GlobalHandle that would try to clear {location_}.
// We can't do that from arbitrary threads, so we must post a task to the
// main thread.
GetWasmEngine()->ClearWeakScriptHandle(isolate_, std::move(location_));
}
WeakScriptHandle(WeakScriptHandle&&) V8_NOEXCEPT = default;
Handle<Script> handle() const { return Handle<Script>(*location_); }
// Called by ~IsolateInfo. When the Isolate is shutting down, cleaning
// up properly is both no longer necessary and no longer safe to do.
void Clear() { location_.reset(); }
int script_id() const { return script_id_; }
const std::shared_ptr<const char[]>& source_url() const {
return source_url_;
}
private:
// Store the location in a unique_ptr so that its address stays the same even
// when this object is moved/copied.
std::unique_ptr<Address*> location_;
// Store the script ID independent of the weak handle, such that it's always
// available.
int script_id_;
// Similar for the source URL. We cannot dereference the Handle from arbitrary
// threads, but we need the URL available for code logging.
// The shared pointer is kept alive by unlogged code, even if this entry is
// collected in the meantime.
// TODO(chromium:1132260): Revisit this for huge URLs.
std::shared_ptr<const char[]> source_url_;
// The Isolate that the handled script belongs to.
Isolate* isolate_;
};
// If PGO data is being collected, keep all native modules alive, so repeated
// runs of a benchmark (with different configuration) all use the same module.
// This vector is protected by the global WasmEngine's mutex, but not defined in
// the header because it's a private implementation detail.
std::vector<std::shared_ptr<NativeModule>>* native_modules_kept_alive_for_pgo;
} // namespace
std::shared_ptr<NativeModule> NativeModuleCache::MaybeGetNativeModule(
ModuleOrigin origin, base::Vector<const uint8_t> wire_bytes,
const CompileTimeImports& compile_imports) {
if (!v8_flags.wasm_native_module_cache_enabled) return nullptr;
if (origin != kWasmOrigin) return nullptr;
base::MutexGuard lock(&mutex_);
size_t prefix_hash = PrefixHash(wire_bytes);
NativeModuleCache::Key key{prefix_hash, compile_imports, wire_bytes};
while (true) {
auto it = map_.find(key);
if (it == map_.end()) {
// Even though this exact key is not in the cache, there might be a
// matching prefix hash indicating that a streaming compilation is
// currently compiling a module with the same prefix. {OnFinishedStream}
// happens on the main thread too, so waiting for streaming compilation to
// finish would create a deadlock. Instead, compile the module twice and
// handle the conflict in {UpdateNativeModuleCache}.
// Insert a {nullopt} entry to let other threads know that this
// {NativeModule} is already being created on another thread.
[[maybe_unused]] auto [iterator, inserted] =
map_.emplace(key, std::nullopt);
DCHECK(inserted);
return nullptr;
}
if (it->second.has_value()) {
if (auto shared_native_module = it->second.value().lock()) {
DCHECK_EQ(
shared_native_module->compile_imports().compare(compile_imports),
0);
DCHECK_EQ(shared_native_module->wire_bytes(), wire_bytes);
return shared_native_module;
}
}
// TODO(11858): This deadlocks in predictable mode, because there is only a
// single thread.
cache_cv_.Wait(&mutex_);
}
}
bool NativeModuleCache::GetStreamingCompilationOwnership(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
base::MutexGuard lock(&mutex_);
auto it = map_.lower_bound(Key{prefix_hash, compile_imports, {}});
if (it != map_.end() && it->first.prefix_hash == prefix_hash) {
DCHECK_IMPLIES(!it->first.bytes.empty(),
PrefixHash(it->first.bytes) == prefix_hash);
return false;
}
Key key{prefix_hash, compile_imports, {}};
DCHECK_EQ(0, map_.count(key));
map_.emplace(key, std::nullopt);
return true;
}
void NativeModuleCache::StreamingCompilationFailed(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
base::MutexGuard lock(&mutex_);
Key key{prefix_hash, compile_imports, {}};
map_.erase(key);
cache_cv_.NotifyAll();
}
std::shared_ptr<NativeModule> NativeModuleCache::Update(
std::shared_ptr<NativeModule> native_module, bool error) {
DCHECK_NOT_NULL(native_module);
if (!v8_flags.wasm_native_module_cache_enabled) return native_module;
if (native_module->module()->origin != kWasmOrigin) return native_module;
base::Vector<const uint8_t> wire_bytes = native_module->wire_bytes();
DCHECK(!wire_bytes.empty());
size_t prefix_hash = PrefixHash(native_module->wire_bytes());
base::MutexGuard lock(&mutex_);
const CompileTimeImports& compile_imports = native_module->compile_imports();
map_.erase(Key{prefix_hash, compile_imports, {}});
const Key key{prefix_hash, compile_imports, wire_bytes};
auto it = map_.find(key);
if (it != map_.end()) {
if (it->second.has_value()) {
auto conflicting_module = it->second.value().lock();
if (conflicting_module != nullptr) {
DCHECK_EQ(conflicting_module->wire_bytes(), wire_bytes);
// This return might delete {native_module} if we were the last holder.
// That in turn can call {NativeModuleCache::Erase}, which takes the
// mutex. This is not a problem though, since the {MutexGuard} above is
// released before the {native_module}, per the definition order.
return conflicting_module;
}
}
map_.erase(it);
}
if (!error) {
// The key now points to the new native module's owned copy of the bytes,
// so that it stays valid until the native module is freed and erased from
// the map.
[[maybe_unused]] auto [iterator, inserted] = map_.emplace(
key, std::optional<std::weak_ptr<NativeModule>>(native_module));
DCHECK(inserted);
}
cache_cv_.NotifyAll();
return native_module;
}
void NativeModuleCache::Erase(NativeModule* native_module) {
if (!v8_flags.wasm_native_module_cache_enabled) return;
if (native_module->module()->origin != kWasmOrigin) return;
// Happens in some tests where bytes are set directly.
if (native_module->wire_bytes().empty()) return;
base::MutexGuard lock(&mutex_);
size_t prefix_hash = PrefixHash(native_module->wire_bytes());
map_.erase(Key{prefix_hash, native_module->compile_imports(),
native_module->wire_bytes()});
cache_cv_.NotifyAll();
}
// static
size_t NativeModuleCache::PrefixHash(base::Vector<const uint8_t> wire_bytes) {
// Compute the hash as a combined hash of the sections up to the code section
// header, to mirror the way streaming compilation does it.
Decoder decoder(wire_bytes.begin(), wire_bytes.end());
decoder.consume_bytes(8, "module header");
size_t hash = GetWireBytesHash(wire_bytes.SubVector(0, 8));
SectionCode section_id = SectionCode::kUnknownSectionCode;
while (decoder.ok() && decoder.more()) {
section_id = static_cast<SectionCode>(decoder.consume_u8());
uint32_t section_size = decoder.consume_u32v("section size");
if (section_id == SectionCode::kCodeSectionCode) {
hash = base::hash_combine(hash, section_size);
break;
}
const uint8_t* payload_start = decoder.pc();
decoder.consume_bytes(section_size, "section payload");
size_t section_hash =
GetWireBytesHash(base::VectorOf(payload_start, section_size));
hash = base::hash_combine(hash, section_hash);
}
return hash;
}
struct WasmEngine::CurrentGCInfo {
explicit CurrentGCInfo(int8_t gc_sequence_index)
: gc_sequence_index(gc_sequence_index) {
DCHECK_NE(0, gc_sequence_index);
}
// Set of isolates that did not scan their stack yet for used WasmCode, and
// their scheduled foreground task.
std::unordered_map<Isolate*, WasmGCForegroundTask*> outstanding_isolates;
// Set of dead code. Filled with all potentially dead code on initialization.
// Code that is still in-use is removed by the individual isolates.
std::unordered_set<WasmCode*> dead_code;
// The number of GCs triggered in the native module that triggered this GC.
// This is stored in the histogram for each participating isolate during
// execution of that isolate's foreground task.
const int8_t gc_sequence_index;
// If during this GC, another GC was requested, we skipped that other GC (we
// only run one GC at a time). Remember though to trigger another one once
// this one finishes. {next_gc_sequence_index} is 0 if no next GC is needed,
// and >0 otherwise. It stores the {num_code_gcs_triggered} of the native
// module which triggered the next GC.
int8_t next_gc_sequence_index = 0;
// The start time of this GC; used for tracing and sampled via {Counters}.
// Can be null ({TimeTicks::IsNull()}) if timer is not high resolution.
base::TimeTicks start_time;
};
struct WasmEngine::IsolateInfo {
explicit IsolateInfo(Isolate* isolate)
: log_codes(WasmCode::ShouldBeLogged(isolate)),
async_counters(isolate->async_counters()),
wrapper_compilation_barrier_(std::make_shared<OperationsBarrier>()) {
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
v8::Platform* platform = V8::GetCurrentPlatform();
foreground_task_runner = platform->GetForegroundTaskRunner(v8_isolate);
}
~IsolateInfo() {
// Before destructing, the {WasmEngine} must have cleared outstanding code
// to log.
DCHECK_EQ(0, code_to_log.size());
// We need the {~WeakScriptHandle} destructor in {scripts} to behave
// differently depending on whether the Isolate is in the process of
// being destroyed. That's the only situation where we would run the
// {~IsolateInfo} destructor, and in that case, we can no longer post
// the task that would destroy the {WeakScriptHandle}'s {GlobalHandle};
// whereas if only individual entries of {scripts} get deleted, then
// we can and should post such tasks.
for (auto& [native_module, script_handle] : scripts) {
script_handle.Clear();
}
}
// All native modules that are being used by this Isolate.
std::unordered_set<NativeModule*> native_modules;
// Scripts created for each native module in this isolate.
std::unordered_map<NativeModule*, WeakScriptHandle> scripts;
// Caches whether code needs to be logged on this isolate.
bool log_codes;
// Maps script ID to vector of code objects that still need to be logged, and
// the respective source URL.
struct CodeToLogPerScript {
std::vector<WasmCode*> code;
std::shared_ptr<const char[]> source_url;
};
std::unordered_map<int, CodeToLogPerScript> code_to_log;
// The foreground task runner of the isolate (can be called from background).
std::shared_ptr<v8::TaskRunner> foreground_task_runner;
const std::shared_ptr<Counters> async_counters;
// Keep new modules in debug state.
bool keep_in_debug_state = false;
// Keep track whether we already added a sample for PKU support (we only want
// one sample per Isolate).
bool pku_support_sampled = false;
// Operations barrier to synchronize on wrapper compilation on isolate
// shutdown.
// TODO(wasm): Remove this once we can use the generic js-to-wasm wrapper
// everywhere.
std::shared_ptr<OperationsBarrier> wrapper_compilation_barrier_;
};
void WasmEngine::ClearWeakScriptHandle(Isolate* isolate,
std::unique_ptr<Address*> location) {
// This function is designed for one targeted use case, which always
// acquires a lock on {mutex_} before calling here.
mutex_.AssertHeld();
IsolateInfo* isolate_info = isolates_[isolate].get();
std::shared_ptr<TaskRunner> runner = isolate_info->foreground_task_runner;
runner->PostTask(std::make_unique<ClearWeakScriptHandleTask>(
isolate, std::move(location)));
}
struct WasmEngine::NativeModuleInfo {
explicit NativeModuleInfo(std::weak_ptr<NativeModule> native_module)
: weak_ptr(std::move(native_module)) {}
// Weak pointer, to gain back a shared_ptr if needed.
std::weak_ptr<NativeModule> weak_ptr;
// Set of isolates using this NativeModule.
std::unordered_set<Isolate*> isolates;
};
WasmEngine::WasmEngine() : call_descriptors_(&allocator_) {}
WasmEngine::~WasmEngine() {
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
// Synchronize on the GDB-remote thread, if running.
gdb_server_.reset();
#endif // V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
// Free all modules that were kept alive for collecting PGO. This is to avoid
// memory leaks.
if (V8_UNLIKELY(native_modules_kept_alive_for_pgo)) {
delete native_modules_kept_alive_for_pgo;
}
operations_barrier_->CancelAndWait();
// All AsyncCompileJobs have been canceled.
DCHECK(async_compile_jobs_.empty());
// All Isolates have been deregistered.
DCHECK(isolates_.empty());
// All NativeModules did die.
DCHECK(native_modules_.empty());
// Native module cache does not leak.
DCHECK(native_module_cache_.empty());
}
bool WasmEngine::SyncValidate(Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports,
ModuleWireBytes bytes) {
TRACE_EVENT0("v8.wasm", "wasm.SyncValidate");
if (bytes.length() == 0) return false;
auto result = DecodeWasmModule(
enabled, bytes.module_bytes(), true, kWasmOrigin, isolate->counters(),
isolate->metrics_recorder(),
isolate->GetOrRegisterRecorderContextId(isolate->native_context()),
DecodingMethod::kSync);
if (result.failed()) return false;
WasmError link_error = ValidateAndSetBuiltinImports(
result.value().get(), bytes.module_bytes(), compile_imports);
return !link_error.has_error();
}
MaybeHandle<AsmWasmData> WasmEngine::SyncCompileTranslatedAsmJs(
Isolate* isolate, ErrorThrower* thrower, ModuleWireBytes bytes,
DirectHandle<Script> script,
base::Vector<const uint8_t> asm_js_offset_table_bytes,
DirectHandle<HeapNumber> uses_bitset, LanguageMode language_mode) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.SyncCompileTranslatedAsmJs", "id",
compilation_id);
ModuleOrigin origin = language_mode == LanguageMode::kSloppy
? kAsmJsSloppyOrigin
: kAsmJsStrictOrigin;
// TODO(leszeks): If we want asm.js in UKM, we should figure out a way to pass
// the context id in here.
v8::metrics::Recorder::ContextId context_id =
v8::metrics::Recorder::ContextId::Empty();
ModuleResult result = DecodeWasmModule(
WasmEnabledFeatures::ForAsmjs(), bytes.module_bytes(), false, origin,
isolate->counters(), isolate->metrics_recorder(), context_id,
DecodingMethod::kSync);
if (result.failed()) {
// This happens once in a while when we have missed some limit check
// in the asm parser. Output an error message to help diagnose, but crash.
std::cout << result.error().message();
UNREACHABLE();
}
result.value()->asm_js_offset_information =
std::make_unique<AsmJsOffsetInformation>(asm_js_offset_table_bytes);
// Transfer ownership of the WasmModule to the {Managed<WasmModule>} generated
// in {CompileToNativeModule}.
constexpr ProfileInformation* kNoProfileInformation = nullptr;
std::shared_ptr<NativeModule> native_module = CompileToNativeModule(
isolate, WasmEnabledFeatures::ForAsmjs(), CompileTimeImports{}, thrower,
std::move(result).value(), bytes, compilation_id, context_id,
kNoProfileInformation);
if (!native_module) return {};
native_module->LogWasmCodes(isolate, *script);
{
// Register the script with the isolate. We do this unconditionally for
// consistency; it is in particular required for logging lazy-compiled code.
base::MutexGuard guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
auto& scripts = isolates_[isolate]->scripts;
// If the same asm.js module is instantiated repeatedly, then we
// deduplicate the NativeModule, so the script exists already.
if (scripts.count(native_module.get()) == 0) {
scripts.emplace(native_module.get(), WeakScriptHandle(script, isolate));
}
}
return AsmWasmData::New(isolate, std::move(native_module), uses_bitset);
}
Handle<WasmModuleObject> WasmEngine::FinalizeTranslatedAsmJs(
Isolate* isolate, DirectHandle<AsmWasmData> asm_wasm_data,
DirectHandle<Script> script) {
std::shared_ptr<NativeModule> native_module =
asm_wasm_data->managed_native_module()->get();
Handle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate, std::move(native_module), script);
return module_object;
}
MaybeHandle<WasmModuleObject> WasmEngine::SyncCompile(
Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports, ErrorThrower* thrower,
ModuleWireBytes bytes) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.SyncCompile", "id", compilation_id);
v8::metrics::Recorder::ContextId context_id =
isolate->GetOrRegisterRecorderContextId(isolate->native_context());
std::shared_ptr<WasmModule> module;
{
// Normally modules are validated in {CompileToNativeModule} but in jitless
// mode the only opportunity of validatiom is during decoding.
bool validate_module = v8_flags.wasm_jitless;
ModuleResult result = DecodeWasmModule(
enabled, bytes.module_bytes(), validate_module, kWasmOrigin,
isolate->counters(), isolate->metrics_recorder(), context_id,
DecodingMethod::kSync);
if (result.failed()) {
thrower->CompileFailed(result.error());
return {};
}
module = std::move(result).value();
if (WasmError error = ValidateAndSetBuiltinImports(
module.get(), bytes.module_bytes(), compile_imports)) {
thrower->LinkError("%s @+%u", error.message().c_str(), error.offset());
return {};
}
}
// If experimental PGO via files is enabled, load profile information now.
std::unique_ptr<ProfileInformation> pgo_info;
if (V8_UNLIKELY(v8_flags.experimental_wasm_pgo_from_file)) {
pgo_info = LoadProfileFromFile(module.get(), bytes.module_bytes());
}
// Transfer ownership of the WasmModule to the {Managed<WasmModule>} generated
// in {CompileToNativeModule}.
std::shared_ptr<NativeModule> native_module = CompileToNativeModule(
isolate, enabled, std::move(compile_imports), thrower, std::move(module),
bytes, compilation_id, context_id, pgo_info.get());
if (!native_module) return {};
#ifdef DEBUG
// Ensure that code GC will check this isolate for live code.
{
base::MutexGuard lock(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
DCHECK_EQ(1, isolates_[isolate]->native_modules.count(native_module.get()));
DCHECK_EQ(1, native_modules_.count(native_module.get()));
DCHECK_EQ(1, native_modules_[native_module.get()]->isolates.count(isolate));
}
#endif
constexpr base::Vector<const char> kNoSourceUrl;
DirectHandle<Script> script =
GetOrCreateScript(isolate, native_module, kNoSourceUrl);
native_module->LogWasmCodes(isolate, *script);
// Create the compiled module object and populate with compiled functions
// and information needed at instantiation time. This object needs to be
// serializable. Instantiation may occur off a deserialized version of this
// object.
Handle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate, std::move(native_module), script);
// Finish the Wasm script now and make it public to the debugger.
isolate->debug()->OnAfterCompile(script);
return module_object;
}
MaybeHandle<WasmInstanceObject> WasmEngine::SyncInstantiate(
Isolate* isolate, ErrorThrower* thrower,
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports,
MaybeHandle<JSArrayBuffer> memory) {
TRACE_EVENT0("v8.wasm", "wasm.SyncInstantiate");
return InstantiateToInstanceObject(isolate, thrower, module_object, imports,
memory);
}
void WasmEngine::AsyncInstantiate(
Isolate* isolate, std::unique_ptr<InstantiationResultResolver> resolver,
Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports) {
ErrorThrower thrower(isolate, "WebAssembly.instantiate()");
TRACE_EVENT0("v8.wasm", "wasm.AsyncInstantiate");
// Instantiate a TryCatch so that caught exceptions won't progagate out.
// They will still be set as exceptions on the isolate.
// TODO(clemensb): Avoid TryCatch, use Execution::TryCall internally to invoke
// start function and report thrown exception explicitly via out argument.
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
catcher.SetVerbose(false);
catcher.SetCaptureMessage(false);
MaybeHandle<WasmInstanceObject> instance_object = SyncInstantiate(
isolate, &thrower, module_object, imports, Handle<JSArrayBuffer>::null());
if (!instance_object.is_null()) {
resolver->OnInstantiationSucceeded(instance_object.ToHandleChecked());
return;
}
if (isolate->has_exception()) {
// The JS code executed during instantiation has thrown an exception.
// We have to move the exception to the promise chain.
Handle<Object> exception(isolate->exception(), isolate);
isolate->clear_exception();
resolver->OnInstantiationFailed(exception);
thrower.Reset();
} else {
DCHECK(thrower.error());
resolver->OnInstantiationFailed(thrower.Reify());
}
}
void WasmEngine::AsyncCompile(
Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports,
std::shared_ptr<CompilationResultResolver> resolver, ModuleWireBytes bytes,
bool is_shared, const char* api_method_name_for_errors) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.AsyncCompile", "id", compilation_id);
if (!v8_flags.wasm_async_compilation || v8_flags.wasm_jitless) {
// Asynchronous compilation disabled; fall back on synchronous compilation.
ErrorThrower thrower(isolate, api_method_name_for_errors);
MaybeHandle<WasmModuleObject> module_object;
if (is_shared) {
// Make a copy of the wire bytes to avoid concurrent modification.
std::unique_ptr<uint8_t[]> copy(new uint8_t[bytes.length()]);
memcpy(copy.get(), bytes.start(), bytes.length());
ModuleWireBytes bytes_copy(copy.get(), copy.get() + bytes.length());
module_object = SyncCompile(isolate, enabled, std::move(compile_imports),
&thrower, bytes_copy);
} else {
// The wire bytes are not shared, OK to use them directly.
module_object = SyncCompile(isolate, enabled, std::move(compile_imports),
&thrower, bytes);
}
if (thrower.error()) {
resolver->OnCompilationFailed(thrower.Reify());
return;
}
Handle<WasmModuleObject> module = module_object.ToHandleChecked();
resolver->OnCompilationSucceeded(module);
return;
}
if (v8_flags.wasm_test_streaming) {
std::shared_ptr<StreamingDecoder> streaming_decoder =
StartStreamingCompilation(isolate, enabled, std::move(compile_imports),
handle(isolate->context(), isolate),
api_method_name_for_errors,
std::move(resolver));
auto* rng = isolate->random_number_generator();
base::SmallVector<base::Vector<const uint8_t>, 16> ranges;
if (!bytes.module_bytes().empty()) ranges.push_back(bytes.module_bytes());
// Split into up to 16 ranges (2^4).
for (int round = 0; round < 4; ++round) {
for (auto it = ranges.begin(); it != ranges.end(); ++it) {
auto range = *it;
if (range.size() < 2 || !rng->NextBool()) continue; // Do not split.
// Choose split point within [1, range.size() - 1].
static_assert(kV8MaxWasmModuleSize <= kMaxInt);
size_t split_point =
1 + rng->NextInt(static_cast<int>(range.size() - 1));
// Insert first sub-range *before* {it} and make {it} point after it.
it = ranges.insert(it, range.SubVector(0, split_point)) + 1;
*it = range.SubVectorFrom(split_point);
}
}
for (auto range : ranges) {
streaming_decoder->OnBytesReceived(range);
}
streaming_decoder->Finish();
return;
}
// Make a copy of the wire bytes in case the user program changes them
// during asynchronous compilation.
base::OwnedVector<const uint8_t> copy =
base::OwnedVector<const uint8_t>::Of(bytes.module_bytes());
AsyncCompileJob* job = CreateAsyncCompileJob(
isolate, enabled, std::move(compile_imports), std::move(copy),
isolate->native_context(), api_method_name_for_errors,
std::move(resolver), compilation_id);
job->Start();
}
std::shared_ptr<StreamingDecoder> WasmEngine::StartStreamingCompilation(
Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports, Handle<Context> context,
const char* api_method_name,
std::shared_ptr<CompilationResultResolver> resolver) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.StartStreamingCompilation", "id",
compilation_id);
if (v8_flags.wasm_async_compilation) {
AsyncCompileJob* job = CreateAsyncCompileJob(
isolate, enabled, std::move(compile_imports), {}, context,
api_method_name, std::move(resolver), compilation_id);
return job->CreateStreamingDecoder();
}
return StreamingDecoder::CreateSyncStreamingDecoder(
isolate, enabled, std::move(compile_imports), context, api_method_name,
std::move(resolver));
}
void WasmEngine::CompileFunction(Counters* counters,
NativeModule* native_module,
uint32_t function_index, ExecutionTier tier) {
DCHECK(!v8_flags.wasm_jitless);
// Note we assume that "one-off" compilations can discard detected features.
WasmDetectedFeatures detected;
WasmCompilationUnit::CompileWasmFunction(
counters, native_module, &detected,
&native_module->module()->functions[function_index], tier);
}
void WasmEngine::EnterDebuggingForIsolate(Isolate* isolate) {
if (v8_flags.wasm_jitless) return;
std::vector<std::shared_ptr<NativeModule>> native_modules;
// {mutex_} gets taken both here and in {RemoveCompiledCode} in
// {AddPotentiallyDeadCode}. Therefore {RemoveCompiledCode} has to be
// called outside the lock.
{
base::MutexGuard lock(&mutex_);
if (isolates_[isolate]->keep_in_debug_state) return;
isolates_[isolate]->keep_in_debug_state = true;
for (auto* native_module : isolates_[isolate]->native_modules) {
DCHECK_EQ(1, native_modules_.count(native_module));
if (auto shared_ptr = native_modules_[native_module]->weak_ptr.lock()) {
native_modules.emplace_back(std::move(shared_ptr));
}
native_module->SetDebugState(kDebugging);
}
}
WasmCodeRefScope ref_scope;
for (auto& native_module : native_modules) {
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveNonDebugCode);
}
}
void WasmEngine::LeaveDebuggingForIsolate(Isolate* isolate) {
// Only trigger recompilation after releasing the mutex, otherwise we risk
// deadlocks because of lock inversion. The bool tells whether the module
// needs recompilation for tier up.
std::vector<std::pair<std::shared_ptr<NativeModule>, bool>> native_modules;
{
base::MutexGuard lock(&mutex_);
isolates_[isolate]->keep_in_debug_state = false;
auto can_remove_debug_code = [this](NativeModule* native_module) {
DCHECK_EQ(1, native_modules_.count(native_module));
for (auto* isolate : native_modules_[native_module]->isolates) {
DCHECK_EQ(1, isolates_.count(isolate));
if (isolates_[isolate]->keep_in_debug_state) return false;
}
return true;
};
for (auto* native_module : isolates_[isolate]->native_modules) {
DCHECK_EQ(1, native_modules_.count(native_module));
auto shared_ptr = native_modules_[native_module]->weak_ptr.lock();
if (!shared_ptr) continue; // The module is not used any more.
if (!native_module->IsInDebugState()) continue;
// Only start tier-up if no other isolate needs this module in tiered
// down state.
bool remove_debug_code = can_remove_debug_code(native_module);
if (remove_debug_code) native_module->SetDebugState(kNotDebugging);
native_modules.emplace_back(std::move(shared_ptr), remove_debug_code);
}
}
for (auto& entry : native_modules) {
auto& native_module = entry.first;
bool remove_debug_code = entry.second;
// Remove all breakpoints set by this isolate.
if (native_module->HasDebugInfo()) {
native_module->GetDebugInfo()->RemoveIsolate(isolate);
}
if (remove_debug_code) {
WasmCodeRefScope ref_scope;
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveDebugCode);
}
}
}
namespace {
Handle<Script> CreateWasmScript(Isolate* isolate,
std::shared_ptr<NativeModule> native_module,
base::Vector<const char> source_url) {
base::Vector<const uint8_t> wire_bytes = native_module->wire_bytes();
// The source URL of the script is
// - the original source URL if available (from the streaming API),
// - wasm://wasm/<module name>-<hash> if a module name has been set, or
// - wasm://wasm/<hash> otherwise.
const WasmModule* module = native_module->module();
Handle<String> url_str;
if (!source_url.empty()) {
url_str = isolate->factory()
->NewStringFromUtf8(source_url, AllocationType::kOld)
.ToHandleChecked();
} else {
// Limit the printed hash to 8 characters.
uint32_t hash = static_cast<uint32_t>(GetWireBytesHash(wire_bytes));
base::EmbeddedVector<char, 32> buffer;
if (module->name.is_empty()) {
// Build the URL in the form "wasm://wasm/<hash>".
int url_len = SNPrintF(buffer, "wasm://wasm/%08x", hash);
DCHECK(url_len >= 0 && url_len < buffer.length());
url_str = isolate->factory()
->NewStringFromUtf8(buffer.SubVector(0, url_len),
AllocationType::kOld)
.ToHandleChecked();
} else {
// Build the URL in the form "wasm://wasm/<module name>-<hash>".
int hash_len = SNPrintF(buffer, "-%08x", hash);
DCHECK(hash_len >= 0 && hash_len < buffer.length());
Handle<String> prefix =
isolate->factory()->NewStringFromStaticChars("wasm://wasm/");
Handle<String> module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, wire_bytes, module->name, kNoInternalize);
Handle<String> hash_str =
isolate->factory()
->NewStringFromUtf8(buffer.SubVector(0, hash_len))
.ToHandleChecked();
// Concatenate the three parts.
url_str = isolate->factory()
->NewConsString(prefix, module_name)
.ToHandleChecked();
url_str = isolate->factory()
->NewConsString(url_str, hash_str)
.ToHandleChecked();
}
}
DirectHandle<PrimitiveHeapObject> source_map_url =
isolate->factory()->undefined_value();
const WasmDebugSymbols& debug_symbols = module->debug_symbols;
if (debug_symbols.type == WasmDebugSymbols::Type::SourceMap &&
!debug_symbols.external_url.is_empty()) {
base::Vector<const char> external_url =
ModuleWireBytes(wire_bytes).GetNameOrNull(debug_symbols.external_url);
MaybeHandle<String> src_map_str = isolate->factory()->NewStringFromUtf8(
external_url, AllocationType::kOld);
source_map_url = src_map_str.ToHandleChecked();
}
// Use the given shared {NativeModule}, but increase its reference count by
// allocating a new {Managed<T>} that the {Script} references.
size_t code_size_estimate = native_module->committed_code_space();
size_t memory_estimate =
code_size_estimate +