Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/passes/SafeHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@

namespace wasm {

const Name DYNAMICTOP_PTR_IMPORT("DYNAMICTOP_PTR");
const Name GET_SBRK_PTR_IMPORT("emscripten_get_sbrk_ptr");
const Name SEGFAULT_IMPORT("segfault");
const Name ALIGNFAULT_IMPORT("alignfault");
static const Name DYNAMICTOP_PTR_IMPORT("DYNAMICTOP_PTR");
static const Name GET_SBRK_PTR_IMPORT("emscripten_get_sbrk_ptr");
static const Name SBRK("sbrk");
static const Name SEGFAULT_IMPORT("segfault");
static const Name ALIGNFAULT_IMPORT("alignfault");

static Name getLoadName(Load* curr) {
std::string ret = "SAFE_HEAP_LOAD_";
Expand Down Expand Up @@ -112,7 +113,7 @@ struct SafeHeap : public Pass {
addGlobals(module, module->features);
}

Name dynamicTopPtr, getSbrkPtr, segfault, alignfault;
Name dynamicTopPtr, getSbrkPtr, sbrk, segfault, alignfault;

void addImports(Module* module) {
ImportInfo info(*module);
Expand All @@ -124,6 +125,8 @@ struct SafeHeap : public Pass {
} else if (auto* existing =
info.getImportedFunction(ENV, GET_SBRK_PTR_IMPORT)) {
getSbrkPtr = existing->name;
} else if (auto* existing = info.getImportedFunction(ENV, SBRK)) {
sbrk = existing->name;
} else {
auto* import = new Function;
import->name = getSbrkPtr = GET_SBRK_PTR_IMPORT;
Expand Down Expand Up @@ -324,11 +327,18 @@ struct SafeHeap : public Pass {
makeBoundsCheck(Type type, Builder& builder, Index local, Index bytes) {
auto upperOp = options.lowMemoryUnused ? LtUInt32 : EqInt32;
auto upperBound = options.lowMemoryUnused ? PassOptions::LowMemoryBound : 0;
Expression* sbrkPtr;
if (dynamicTopPtr.is()) {
sbrkPtr = builder.makeGlobalGet(dynamicTopPtr, i32);
Expression* brkLocation;
if (sbrk.is()) {
brkLocation =
builder.makeCall(sbrk, {builder.makeConst(Literal(int32_t(0)))}, i32);
} else {
sbrkPtr = builder.makeCall(getSbrkPtr, {}, i32);
Expression* sbrkPtr;
if (dynamicTopPtr.is()) {
sbrkPtr = builder.makeGlobalGet(dynamicTopPtr, i32);
} else {
sbrkPtr = builder.makeCall(getSbrkPtr, {}, i32);
}
brkLocation = builder.makeLoad(4, false, 0, 4, sbrkPtr, i32);
}
return builder.makeIf(
builder.makeBinary(
Expand All @@ -341,7 +351,7 @@ struct SafeHeap : public Pass {
builder.makeBinary(AddInt32,
builder.makeLocalGet(local, i32),
builder.makeConst(Literal(int32_t(bytes)))),
builder.makeLoad(4, false, 0, 4, sbrkPtr, i32))),
brkLocation)),
builder.makeCall(segfault, {}, none));
}
};
Expand Down