Commit 960c672c authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Cleanup calls to CalculateGlobalOffsets

CalculateGlobalOffsets has to be called once after all globals got
defined but before global offsets get accessed, e.g. during code
generation. It is not clear though when exactly CalculateGlobalOffsets
should be called. The globals section may not exist, so at the end of
the globals section is not enough (globals can also be defined in the
import section). At the beginning of the code section is also not good
enough, because the code section may not exist. At the end of the module
may be too late.

With this CL, CalculateGlobalOffsets is called after the global section,
before the code section, and at the end of the module. Additionally the CL
checks if CalculateGlobalOffsets has already been called, so that it is
not executed a second time.

R=manoskouk@chromium.org

Bug: v8:11185
Change-Id: I922b9f60a4a17a09d2527fd9ab35cda71226030c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2551100
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarManos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71314}
parent dbe1b9d8
......@@ -2482,6 +2482,7 @@ bool AsyncStreamingProcessor::ProcessCodeSectionHeader(
before_code_section_ = false;
TRACE_STREAMING("Start the code section with %d functions...\n",
num_functions);
decoder_.StartCodeSection();
if (!decoder_.CheckFunctionsCount(static_cast<uint32_t>(num_functions),
offset)) {
FinishAsyncCompileJobWithError(decoder_.FinishDecoding(false).error());
......
......@@ -930,6 +930,7 @@ class ModuleDecoderImpl : public Decoder {
}
void DecodeCodeSection(bool verify_functions) {
StartCodeSection();
uint32_t pos = pc_offset();
uint32_t functions_count = consume_u32v("functions count");
CheckFunctionsCount(functions_count, pos);
......@@ -950,6 +951,14 @@ class ModuleDecoderImpl : public Decoder {
set_code_section(pos, pc_offset() - pos);
}
void StartCodeSection() {
if (ok()) {
// Make sure global offset were calculated before they get accessed during
// function compilation.
CalculateGlobalOffsets(module_.get());
}
}
bool CheckFunctionsCount(uint32_t functions_count, uint32_t offset) {
if (functions_count != module_->num_declared_functions) {
Reset(nullptr, nullptr, offset);
......@@ -1207,6 +1216,10 @@ class ModuleDecoderImpl : public Decoder {
ModuleResult FinishDecoding(bool verify_functions = true) {
if (ok() && CheckMismatchedCounts()) {
// We calculate the global offsets here, because there may not be a global
// section and code section that would have triggered the calculation
// before. Even without the globals section the calculation is needed
// because globals can also be defined in the import section.
CalculateGlobalOffsets(module_.get());
}
......@@ -1407,7 +1420,18 @@ class ModuleDecoderImpl : public Decoder {
}
// Calculate individual global offsets and total size of globals table.
// This function should be called after all globals have been defined, which
// is after the import section and the global section, but before the global
// offsets are accessed, e.g. by the function compilers. The moment when this
// function should be called is not well-defined, as the global section may
// not exist. Therefore this function is called multiple times.
void CalculateGlobalOffsets(WasmModule* module) {
if (module->globals.empty() || module->untagged_globals_buffer_size != 0 ||
module->tagged_globals_buffer_size != 0) {
// This function has already been executed before, so we don't have to
// execute it again.
return;
}
uint32_t untagged_offset = 0;
uint32_t tagged_offset = 0;
uint32_t num_imported_mutable_globals = 0;
......@@ -2240,6 +2264,8 @@ void ModuleDecoder::DecodeFunctionBody(uint32_t index, uint32_t length,
impl_->DecodeFunctionBody(index, length, offset, verify_functions);
}
void ModuleDecoder::StartCodeSection() { impl_->StartCodeSection(); }
bool ModuleDecoder::CheckFunctionsCount(uint32_t functions_count,
uint32_t offset) {
return impl_->CheckFunctionsCount(functions_count, offset);
......
......@@ -210,6 +210,8 @@ class ModuleDecoder {
void DecodeSection(SectionCode section_code, Vector<const uint8_t> bytes,
uint32_t offset, bool verify_functions = true);
void StartCodeSection();
bool CheckFunctionsCount(uint32_t functions_count, uint32_t offset);
void DecodeFunctionBody(uint32_t index, uint32_t size, uint32_t offset,
......@@ -220,6 +222,7 @@ class ModuleDecoder {
void set_code_section(uint32_t offset, uint32_t size);
const std::shared_ptr<WasmModule>& shared_module() const;
WasmModule* module() const { return shared_module().get(); }
bool ok();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment