Skip to content

Commit f59c303

Browse files
Warchantkripken
authored andcommitted
Fix memory leaks (#1925)
Fixes #1921 Signed-off-by: Bogdan Vaneev <[email protected]>
1 parent 1a483a2 commit f59c303

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/passes/LegalizeJSInterface.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// disallow f32s. TODO: an option to not do that, if it matters?
3434
//
3535

36+
#include <utility>
3637
#include "wasm.h"
3738
#include "pass.h"
3839
#include "asm_v_wasm.h"
@@ -85,7 +86,7 @@ struct LegalizeJSInterface : public Pass {
8586
}
8687
}
8788
}
88-
if (illegalImportsToLegal.size() > 0) {
89+
if (!illegalImportsToLegal.empty()) {
8990
for (auto& pair : illegalImportsToLegal) {
9091
module->removeFunction(pair.first);
9192
}
@@ -126,8 +127,7 @@ struct LegalizeJSInterface : public Pass {
126127
for (auto param : t->params) {
127128
if (param == i64 || param == f32) return true;
128129
}
129-
if (t->result == i64 || t->result == f32) return true;
130-
return false;
130+
return t->result == i64 || t->result == f32;
131131
}
132132

133133
// Check if an export should be legalized.
@@ -171,7 +171,7 @@ struct LegalizeJSInterface : public Pass {
171171
if (func->result == i64) {
172172
Function* f = getFunctionOrImport(module, SET_TEMP_RET0, "vi");
173173
legal->result = i32;
174-
auto index = builder.addVar(legal, Name(), i64);
174+
auto index = Builder::addVar(legal, Name(), i64);
175175
auto* block = builder.makeBlock();
176176
block->list.push_back(builder.makeSetLocal(index, call));
177177
block->list.push_back(builder.makeCall(f->name, {I64Utilities::getI64High(builder, index)}, none));
@@ -198,12 +198,12 @@ struct LegalizeJSInterface : public Pass {
198198
Builder builder(*module);
199199
auto type = make_unique<FunctionType>();
200200
type->name = Name(std::string("legaltype$") + im->name.str);
201-
auto* legal = new Function;
201+
auto legal = make_unique<Function>();
202202
legal->name = Name(std::string("legalimport$") + im->name.str);
203203
legal->module = im->module;
204204
legal->base = im->base;
205205
legal->type = type->name;
206-
auto* func = new Function;
206+
auto func = make_unique<Function>();
207207
func->name = Name(std::string("legalfunc$") + im->name.str);
208208

209209
auto* call = module->allocator.alloc<Call>();
@@ -243,18 +243,19 @@ struct LegalizeJSInterface : public Pass {
243243
type->result = imFunctionType->result;
244244
}
245245
func->result = imFunctionType->result;
246-
FunctionTypeUtils::fillFunction(legal, type.get());
246+
FunctionTypeUtils::fillFunction(legal.get(), type.get());
247247

248-
if (!module->getFunctionOrNull(func->name)) {
249-
module->addFunction(func);
248+
const auto& funcName = func->name;
249+
if (!module->getFunctionOrNull(funcName)) {
250+
module->addFunction(std::move(func));
250251
}
251252
if (!module->getFunctionTypeOrNull(type->name)) {
252253
module->addFunctionType(std::move(type));
253254
}
254255
if (!module->getFunctionOrNull(legal->name)) {
255-
module->addFunction(legal);
256+
module->addFunction(std::move(legal));
256257
}
257-
return func->name;
258+
return funcName;
258259
}
259260

260261
static Function* getFunctionOrImport(Module* module, Name name, std::string sig) {
@@ -272,7 +273,7 @@ struct LegalizeJSInterface : public Pass {
272273
import->name = name;
273274
import->module = ENV;
274275
import->base = name;
275-
auto* functionType = ensureFunctionType(sig, module);
276+
auto* functionType = ensureFunctionType(std::move(sig), module);
276277
import->type = functionType->name;
277278
FunctionTypeUtils::fillFunction(import, functionType);
278279
module->addFunction(import);

src/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ class Module {
977977
FunctionType* addFunctionType(std::unique_ptr<FunctionType> curr);
978978
void addExport(Export* curr);
979979
void addFunction(Function* curr);
980+
void addFunction(std::unique_ptr<Function> curr);
980981
void addGlobal(Global* curr);
981982

982983
void addStart(const Name& s);

src/wasm/wasm.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ void Module::addExport(Export* curr) {
866866
exportsMap[curr->name] = curr;
867867
}
868868

869+
// TODO(@warchant): refactor all usages to use variant with unique_ptr
869870
void Module::addFunction(Function* curr) {
870871
if (!curr->name.is()) {
871872
Fatal() << "Module::addFunction: empty name";
@@ -877,6 +878,17 @@ void Module::addFunction(Function* curr) {
877878
functionsMap[curr->name] = curr;
878879
}
879880

881+
void Module::addFunction(std::unique_ptr<Function> curr) {
882+
if (!curr->name.is()) {
883+
Fatal() << "Module::addFunction: empty name";
884+
}
885+
if (getFunctionOrNull(curr->name)) {
886+
Fatal() << "Module::addFunction: " << curr->name << " already exists";
887+
}
888+
functionsMap[curr->name] = curr.get();
889+
functions.push_back(std::move(curr));
890+
}
891+
880892
void Module::addGlobal(Global* curr) {
881893
if (!curr->name.is()) {
882894
Fatal() << "Module::addGlobal: empty name";

0 commit comments

Comments
 (0)