summaryrefslogtreecommitdiff
path: root/src/passes/SafeHeap.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2019-09-06 11:06:04 -0700
committerGitHub <noreply@github.com>2019-09-06 11:06:04 -0700
commit849ea2105919dbd65c003439fb8d16c55692c247 (patch)
treee22cc56c8643f65509f09d58252839a46167b829 /src/passes/SafeHeap.cpp
parent77fa3988c7e272425c72bcc04c8a10e02b3a12ec (diff)
downloadbinaryen-849ea2105919dbd65c003439fb8d16c55692c247.tar.gz
binaryen-849ea2105919dbd65c003439fb8d16c55692c247.tar.bz2
binaryen-849ea2105919dbd65c003439fb8d16c55692c247.zip
Handle sbrk import by emscripten+upstream in SafeHeap (#2332)
To allow #2331 to roll, I forgot that upstream and fastcomp handle sbrk differently. This fixes that, and handles the upstream case where we import sbrk itself from JS. We can simplify this after emscripten-core/emscripten#9397 lands, however, it may also be nice to keep the backwards compatibility for running on existing wasm files in the wild.
Diffstat (limited to 'src/passes/SafeHeap.cpp')
-rw-r--r--src/passes/SafeHeap.cpp30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/passes/SafeHeap.cpp b/src/passes/SafeHeap.cpp
index 6957478cf..d22ddea03 100644
--- a/src/passes/SafeHeap.cpp
+++ b/src/passes/SafeHeap.cpp
@@ -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_";
@@ -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);
@@ -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;
@@ -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(
@@ -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));
}
};