summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBohdan <bogdan@soramitsu.co.jp>2019-03-01 01:52:49 +0200
committerAlon Zakai <alonzakai@gmail.com>2019-02-28 15:52:49 -0800
commitf59c3033e678ced61bc8c78e8ac9fbee31ef0210 (patch)
tree05a097dfa363ea156dd69cd20c6e847eb2e131ee /src
parent1a483a28bb7c58349d668ad3f54ef0e9f9607cad (diff)
downloadbinaryen-f59c3033e678ced61bc8c78e8ac9fbee31ef0210.tar.gz
binaryen-f59c3033e678ced61bc8c78e8ac9fbee31ef0210.tar.bz2
binaryen-f59c3033e678ced61bc8c78e8ac9fbee31ef0210.zip
Fix memory leaks (#1925)
Fixes #1921 Signed-off-by: Bogdan Vaneev <warchantua@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/passes/LegalizeJSInterface.cpp25
-rw-r--r--src/wasm.h1
-rw-r--r--src/wasm/wasm.cpp12
3 files changed, 26 insertions, 12 deletions
diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp
index cef4e38ea..63e4b1a6b 100644
--- a/src/passes/LegalizeJSInterface.cpp
+++ b/src/passes/LegalizeJSInterface.cpp
@@ -33,6 +33,7 @@
// disallow f32s. TODO: an option to not do that, if it matters?
//
+#include <utility>
#include "wasm.h"
#include "pass.h"
#include "asm_v_wasm.h"
@@ -85,7 +86,7 @@ struct LegalizeJSInterface : public Pass {
}
}
}
- if (illegalImportsToLegal.size() > 0) {
+ if (!illegalImportsToLegal.empty()) {
for (auto& pair : illegalImportsToLegal) {
module->removeFunction(pair.first);
}
@@ -126,8 +127,7 @@ private:
for (auto param : t->params) {
if (param == i64 || param == f32) return true;
}
- if (t->result == i64 || t->result == f32) return true;
- return false;
+ return t->result == i64 || t->result == f32;
}
// Check if an export should be legalized.
@@ -171,7 +171,7 @@ private:
if (func->result == i64) {
Function* f = getFunctionOrImport(module, SET_TEMP_RET0, "vi");
legal->result = i32;
- auto index = builder.addVar(legal, Name(), i64);
+ auto index = Builder::addVar(legal, Name(), i64);
auto* block = builder.makeBlock();
block->list.push_back(builder.makeSetLocal(index, call));
block->list.push_back(builder.makeCall(f->name, {I64Utilities::getI64High(builder, index)}, none));
@@ -198,12 +198,12 @@ private:
Builder builder(*module);
auto type = make_unique<FunctionType>();
type->name = Name(std::string("legaltype$") + im->name.str);
- auto* legal = new Function;
+ auto legal = make_unique<Function>();
legal->name = Name(std::string("legalimport$") + im->name.str);
legal->module = im->module;
legal->base = im->base;
legal->type = type->name;
- auto* func = new Function;
+ auto func = make_unique<Function>();
func->name = Name(std::string("legalfunc$") + im->name.str);
auto* call = module->allocator.alloc<Call>();
@@ -243,18 +243,19 @@ private:
type->result = imFunctionType->result;
}
func->result = imFunctionType->result;
- FunctionTypeUtils::fillFunction(legal, type.get());
+ FunctionTypeUtils::fillFunction(legal.get(), type.get());
- if (!module->getFunctionOrNull(func->name)) {
- module->addFunction(func);
+ const auto& funcName = func->name;
+ if (!module->getFunctionOrNull(funcName)) {
+ module->addFunction(std::move(func));
}
if (!module->getFunctionTypeOrNull(type->name)) {
module->addFunctionType(std::move(type));
}
if (!module->getFunctionOrNull(legal->name)) {
- module->addFunction(legal);
+ module->addFunction(std::move(legal));
}
- return func->name;
+ return funcName;
}
static Function* getFunctionOrImport(Module* module, Name name, std::string sig) {
@@ -272,7 +273,7 @@ private:
import->name = name;
import->module = ENV;
import->base = name;
- auto* functionType = ensureFunctionType(sig, module);
+ auto* functionType = ensureFunctionType(std::move(sig), module);
import->type = functionType->name;
FunctionTypeUtils::fillFunction(import, functionType);
module->addFunction(import);
diff --git a/src/wasm.h b/src/wasm.h
index e9109d938..a1761a430 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -977,6 +977,7 @@ public:
FunctionType* addFunctionType(std::unique_ptr<FunctionType> curr);
void addExport(Export* curr);
void addFunction(Function* curr);
+ void addFunction(std::unique_ptr<Function> curr);
void addGlobal(Global* curr);
void addStart(const Name& s);
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp
index 847df5ce6..c12481760 100644
--- a/src/wasm/wasm.cpp
+++ b/src/wasm/wasm.cpp
@@ -866,6 +866,7 @@ void Module::addExport(Export* curr) {
exportsMap[curr->name] = curr;
}
+// TODO(@warchant): refactor all usages to use variant with unique_ptr
void Module::addFunction(Function* curr) {
if (!curr->name.is()) {
Fatal() << "Module::addFunction: empty name";
@@ -877,6 +878,17 @@ void Module::addFunction(Function* curr) {
functionsMap[curr->name] = curr;
}
+void Module::addFunction(std::unique_ptr<Function> curr) {
+ if (!curr->name.is()) {
+ Fatal() << "Module::addFunction: empty name";
+ }
+ if (getFunctionOrNull(curr->name)) {
+ Fatal() << "Module::addFunction: " << curr->name << " already exists";
+ }
+ functionsMap[curr->name] = curr.get();
+ functions.push_back(std::move(curr));
+}
+
void Module::addGlobal(Global* curr) {
if (!curr->name.is()) {
Fatal() << "Module::addGlobal: empty name";