Skip to content

Commit

Permalink
Bug 1829268 - Update wasm2c compiler used with RLBox to support MinGW…
Browse files Browse the repository at this point in the history
… r=glandium

Differential Revision: https://phabricator.services.mozilla.com/D176254
  • Loading branch information
shravanrn committed Apr 25, 2023
1 parent 8abc8bd commit df63667
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions config/external/wasm2c_sandbox_compiler/moz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ origin:
description: wasm2c fork used for rlbox sandboxing
url: https://github.com/WebAssembly/wabt

release: ad5f1385fa7afe29e98d69b6167132162675228f (2023-04-13T05:14:45Z).
revision: ad5f1385fa7afe29e98d69b6167132162675228f
release: 3ec45c662746a7094914fccaebae4648765ef56e (2023-04-24T08:07:03Z).
revision: 3ec45c662746a7094914fccaebae4648765ef56e

license: Apache-2.0
license-file: LICENSE
Expand Down
2 changes: 1 addition & 1 deletion third_party/wasm2c/include/wabt/binary-reader-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
Address alignment,
uint32_t flags) override;
Result OnInitFunctionCount(Index count) override;
Result OnInitFunction(uint32_t priority, Index function_index) override;
Result OnInitFunction(uint32_t priority, Index symbol_index) override;
Result OnComdatCount(Index count) override;
Result OnComdatBegin(std::string_view name,
uint32_t flags,
Expand Down
2 changes: 1 addition & 1 deletion third_party/wasm2c/include/wabt/binary-reader-nop.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ class BinaryReaderNop : public BinaryReaderDelegate {
return Result::Ok;
}
Result OnInitFunctionCount(Index count) override { return Result::Ok; }
Result OnInitFunction(uint32_t priority, Index function_index) override {
Result OnInitFunction(uint32_t priority, Index symbol_index) override {
return Result::Ok;
}
Result OnComdatCount(Index count) override { return Result::Ok; }
Expand Down
2 changes: 1 addition & 1 deletion third_party/wasm2c/include/wabt/binary-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ class BinaryReaderDelegate {
Address alignment_log2,
uint32_t flags) = 0;
virtual Result OnInitFunctionCount(Index count) = 0;
virtual Result OnInitFunction(uint32_t priority, Index function_index) = 0;
virtual Result OnInitFunction(uint32_t priority, Index symbol_index) = 0;
virtual Result OnComdatCount(Index count) = 0;
virtual Result OnComdatBegin(std::string_view name,
uint32_t flags,
Expand Down
6 changes: 3 additions & 3 deletions third_party/wasm2c/src/binary-reader-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ Result BinaryReaderLogging::OnSegmentInfo(Index index,
}

Result BinaryReaderLogging::OnInitFunction(uint32_t priority,
Index func_index) {
LOGF("OnInitFunction(%d priority: %d)\n", func_index, priority);
return reader_->OnInitFunction(priority, func_index);
Index symbol_index) {
LOGF("OnInitFunction(%d priority: %d)\n", symbol_index, priority);
return reader_->OnInitFunction(priority, symbol_index);
}

Result BinaryReaderLogging::OnComdatBegin(std::string_view name,
Expand Down
12 changes: 9 additions & 3 deletions third_party/wasm2c/src/binary-reader-objdump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
Address alignment_log2,
uint32_t flags) override;
Result OnInitFunctionCount(Index count) override;
Result OnInitFunction(uint32_t priority, Index function_index) override;
Result OnInitFunction(uint32_t priority, Index symbol_index) override;
Result OnComdatCount(Index count) override;
Result OnComdatBegin(std::string_view name,
uint32_t flags,
Expand Down Expand Up @@ -2264,10 +2264,16 @@ Result BinaryReaderObjdump::OnInitFunctionCount(Index count) {
}

Result BinaryReaderObjdump::OnInitFunction(uint32_t priority,
Index function_index) {
PrintDetails(" - %d: priority=%d\n", function_index, priority);
Index symbol_index) {
PrintDetails(" - %d: priority=%d", symbol_index, priority);
auto name = GetSymbolName(symbol_index);
if (!name.empty()) {
PrintDetails(" <" PRIstringview ">", WABT_PRINTF_STRING_VIEW_ARG(name));
}
PrintDetails("\n");
return Result::Ok;
}

Result BinaryReaderObjdump::OnComdatCount(Index count) {
PrintDetails(" - comdat groups [count=%d]\n", count);
return Result::Ok;
Expand Down
6 changes: 3 additions & 3 deletions third_party/wasm2c/src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2256,10 +2256,10 @@ Result BinaryReader::ReadLinkingSection(Offset section_size) {
CALLBACK(OnInitFunctionCount, count);
while (count--) {
uint32_t priority;
uint32_t func;
uint32_t symbol;
CHECK_RESULT(ReadU32Leb128(&priority, "priority"));
CHECK_RESULT(ReadU32Leb128(&func, "function index"));
CALLBACK(OnInitFunction, priority, func);
CHECK_RESULT(ReadU32Leb128(&symbol, "symbol index"));
CALLBACK(OnInitFunction, priority, symbol);
}
break;
case LinkingEntryType::ComdatInfo:
Expand Down
27 changes: 16 additions & 11 deletions third_party/wasm2c/src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class CWriter {
void Write(Newline);
void Write(OpenBrace);
void Write(CloseBrace);
void Write(Index);
void Write(uint64_t);
void Write(std::string_view);
void Write(const ParamName&);
void Write(const LabelName&);
Expand Down Expand Up @@ -1012,8 +1012,8 @@ void CWriter::Write(CloseBrace) {
Write("}");
}

void CWriter::Write(Index index) {
Writef("%" PRIindex, index);
void CWriter::Write(uint64_t val) {
Writef("%" PRIu64, val);
}

void CWriter::Write(std::string_view s) {
Expand Down Expand Up @@ -1086,15 +1086,15 @@ void CWriter::Write(const GotoLabel& goto_label) {
StackVar(amount - i - 1), "; ");
}
}
}

assert(try_catch_stack_.size() >= label->try_catch_stack_size);
assert(try_catch_stack_.size() >= label->try_catch_stack_size);

if (try_catch_stack_.size() != label->try_catch_stack_size) {
const std::string& name =
try_catch_stack_.at(label->try_catch_stack_size).name;
if (try_catch_stack_.size() != label->try_catch_stack_size) {
const std::string& name =
try_catch_stack_.at(label->try_catch_stack_size).name;

Write("wasm_rt_set_unwind_target(", name, "_outer_target);", Newline());
}
Write("wasm_rt_set_unwind_target(", name, "_outer_target);", Newline());
}

if (goto_label.var.is_name()) {
Expand Down Expand Up @@ -1972,8 +1972,13 @@ void CWriter::WriteDataInitializers() {
Index memory_idx = module_->num_memory_imports;
for (Index i = memory_idx; i < module_->memories.size(); i++) {
const Memory* memory = module_->memories[i];
uint32_t max =
memory->page_limits.has_max ? memory->page_limits.max : 65536;
uint64_t max;
if (memory->page_limits.has_max) {
max = memory->page_limits.max;
} else {
max = memory->page_limits.is_64 ? (static_cast<uint64_t>(1) << 48)
: 65536;
}
Write("wasm_rt_allocate_memory(",
ExternalInstancePtr(ModuleFieldType::Memory, memory->name), ", ",
memory->page_limits.initial, ", ", max, ", ",
Expand Down
10 changes: 9 additions & 1 deletion third_party/wasm2c/src/prebuilt/wasm2c_source_includes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ R"w2c_template(#include <stddef.h>
)w2c_template"
R"w2c_template(#include <string.h>
)w2c_template"
R"w2c_template(#if defined(_MSC_VER)
R"w2c_template(#if defined(__MINGW32__)
)w2c_template"
R"w2c_template(#include <malloc.h>
)w2c_template"
R"w2c_template(#elif defined(_MSC_VER)
)w2c_template"
R"w2c_template(#include <intrin.h>
)w2c_template"
R"w2c_template(#include <malloc.h>
)w2c_template"
R"w2c_template(#define alloca _alloca
)w2c_template"
R"w2c_template(#elif defined(__FreeBSD__)
)w2c_template"
R"w2c_template(#include <stdlib.h>
)w2c_template"
R"w2c_template(#else
)w2c_template"
R"w2c_template(#include <alloca.h>
Expand Down
1 change: 1 addition & 0 deletions third_party/wasm2c/wasm2c/wasm-rt-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ bool wasm_rt_is_initialized(void) {
void wasm_rt_free(void) {
#if WASM_RT_INSTALL_SIGNAL_HANDLER
os_cleanup_signal_handler();
g_signal_handler_installed = false;
#endif
}

Expand Down

0 comments on commit df63667

Please sign in to comment.