diff options
author | Alon Zakai <azakai@google.com> | 2020-12-14 14:15:36 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-14 14:15:36 -0800 |
commit | c1ab2b33f63d5e79143e05de4e8e9e0ddd970e77 (patch) | |
tree | c3f2e6bfe57b7c0f8cd6d17081ba721e0987d042 /src/passes/StackCheck.cpp | |
parent | 814e897b4b177518a835a9ef30dd649312a77c78 (diff) | |
download | binaryen-c1ab2b33f63d5e79143e05de4e8e9e0ddd970e77.tar.gz binaryen-c1ab2b33f63d5e79143e05de4e8e9e0ddd970e77.tar.bz2 binaryen-c1ab2b33f63d5e79143e05de4e8e9e0ddd970e77.zip |
[StackCheck] Handle colliding names (#3390)
Add Names::getValidGlobalName calls to ensure we don't collide with an existing name.
Fixes emscripten-core/emscripten#12834
Diffstat (limited to 'src/passes/StackCheck.cpp')
-rw-r--r-- | src/passes/StackCheck.cpp | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/src/passes/StackCheck.cpp b/src/passes/StackCheck.cpp index c25d70635..23fd5be3d 100644 --- a/src/passes/StackCheck.cpp +++ b/src/passes/StackCheck.cpp @@ -22,6 +22,7 @@ #include "abi/js.h" #include "ir/import-utils.h" +#include "ir/names.h" #include "pass.h" #include "shared-constants.h" #include "support/debug.h" @@ -31,11 +32,6 @@ namespace wasm { -// The base is where the stack begins. As it goes down, that is the highest -// valid address. -static Name STACK_BASE("__stack_base"); -// The limit is the farthest it can grow to, which is the lowest valid address. -static Name STACK_LIMIT("__stack_limit"); // Exported function to set the base and the limit. static Name SET_STACK_LIMITS("__set_stack_limits"); @@ -60,18 +56,6 @@ static void addExportedFunction(Module& module, module.addExport(std::move(export_)); } -static void generateSetStackLimitFunctions(Module& module) { - Builder builder(module); - auto limitsFunc = builder.makeFunction( - SET_STACK_LIMITS, Signature({Type::i32, Type::i32}, Type::none), {}); - LocalGet* getBase = builder.makeLocalGet(0, Type::i32); - Expression* storeBase = builder.makeGlobalSet(STACK_BASE, getBase); - LocalGet* getLimit = builder.makeLocalGet(1, Type::i32); - Expression* storeLimit = builder.makeGlobalSet(STACK_LIMIT, getLimit); - limitsFunc->body = builder.makeBlock({storeBase, storeLimit}); - addExportedFunction(module, std::move(limitsFunc)); -} - struct EnforceStackLimits : public WalkerPass<PostWalker<EnforceStackLimits>> { EnforceStackLimits(const Global* stackPointer, const Global* stackBase, @@ -142,6 +126,10 @@ struct StackCheck : public Pass { return; } + // Pick appropriate names. + auto stackBaseName = Names::getValidGlobalName(*module, "__stack_base"); + auto stackLimitName = Names::getValidGlobalName(*module, "__stack_limit"); + Name handler; auto handlerName = runner->options.getArgumentOrDefault("stack-check-handler", ""); @@ -151,22 +139,33 @@ struct StackCheck : public Pass { } Builder builder(*module); - auto stackBase = builder.makeGlobal(STACK_BASE, - stackPointer->type, - builder.makeConst(int32_t(0)), - Builder::Mutable); - - auto stackLimit = builder.makeGlobal(STACK_LIMIT, - stackPointer->type, - builder.makeConst(int32_t(0)), - Builder::Mutable); + + // Add the globals. + auto stackBase = + module->addGlobal(builder.makeGlobal(stackBaseName, + stackPointer->type, + builder.makeConst(int32_t(0)), + Builder::Mutable)); + auto stackLimit = + module->addGlobal(builder.makeGlobal(stackLimitName, + stackPointer->type, + builder.makeConst(int32_t(0)), + Builder::Mutable)); + + // Instrument all the code. PassRunner innerRunner(module); - EnforceStackLimits( - stackPointer, stackBase.get(), stackLimit.get(), builder, handler) + EnforceStackLimits(stackPointer, stackBase, stackLimit, builder, handler) .run(&innerRunner, module); - module->addGlobal(std::move(stackBase)); - module->addGlobal(std::move(stackLimit)); - generateSetStackLimitFunctions(*module); + + // Generate the exported function. + auto limitsFunc = builder.makeFunction( + SET_STACK_LIMITS, Signature({Type::i32, Type::i32}, Type::none), {}); + auto* getBase = builder.makeLocalGet(0, Type::i32); + auto* storeBase = builder.makeGlobalSet(stackBaseName, getBase); + auto* getLimit = builder.makeLocalGet(1, Type::i32); + auto* storeLimit = builder.makeGlobalSet(stackLimitName, getLimit); + limitsFunc->body = builder.makeBlock({storeBase, storeLimit}); + addExportedFunction(*module, std::move(limitsFunc)); } }; |