diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-04-27 19:25:15 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-04-27 19:25:15 -0700 |
commit | 6a6bdc1cef5ec35cb9f7caf6e10ec76ee1107d0d (patch) | |
tree | 9ed1b23b55c86f8fa1aee285bfdde9d47c2f1ae8 /src/wasm-binary.h | |
parent | 4a85f62e8a83117a081e9691d8830b6a7a876d1d (diff) | |
parent | f0a4f15dc27ffff9505503a8168854b7662b2657 (diff) | |
download | binaryen-6a6bdc1cef5ec35cb9f7caf6e10ec76ee1107d0d.tar.gz binaryen-6a6bdc1cef5ec35cb9f7caf6e10ec76ee1107d0d.tar.bz2 binaryen-6a6bdc1cef5ec35cb9f7caf6e10ec76ee1107d0d.zip |
Merge pull request #403 from WebAssembly/leaks
Fix leaks and enable leak checks
Diffstat (limited to 'src/wasm-binary.h')
-rw-r--r-- | src/wasm-binary.h | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 3f7ad44d4..91fe91b49 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -447,9 +447,9 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> { void prepare() { // we need function types for all our functions - for (auto* func : wasm->functions) { + for (auto& func : wasm->functions) { if (func->type.isNull()) { - func->type = ensureFunctionType(getSig(func), wasm)->name; + func->type = ensureFunctionType(getSig(func.get()), wasm)->name; } } } @@ -523,7 +523,7 @@ public: if (debug) std::cerr << "== writeSignatures" << std::endl; auto start = startSection(BinaryConsts::Section::Signatures); o << U32LEB(wasm->functionTypes.size()); - for (auto* type : wasm->functionTypes) { + for (auto& type : wasm->functionTypes) { if (debug) std::cerr << "write one" << std::endl; o << int8_t(BinaryConsts::TypeForms::Basic); o << U32LEB(type->params.size()); @@ -553,7 +553,7 @@ public: if (debug) std::cerr << "== writeImports" << std::endl; auto start = startSection(BinaryConsts::Section::ImportTable); o << U32LEB(wasm->imports.size()); - for (auto* import : wasm->imports) { + for (auto& import : wasm->imports) { if (debug) std::cerr << "write one" << std::endl; o << U32LEB(getFunctionTypeIndex(import->type->name)); writeInlineString(import->module.str); @@ -606,7 +606,7 @@ public: if (debug) std::cerr << "== writeFunctionSignatures" << std::endl; auto start = startSection(BinaryConsts::Section::FunctionSignatures); o << U32LEB(wasm->functions.size()); - for (auto* curr : wasm->functions) { + for (auto& curr : wasm->functions) { if (debug) std::cerr << "write one" << std::endl; o << U32LEB(getFunctionTypeIndex(curr->type)); } @@ -623,7 +623,7 @@ public: if (debug) std::cerr << "write one at" << o.size() << std::endl; size_t sizePos = writeU32LEBPlaceholder(); size_t start = o.size(); - Function* function = wasm->functions[i]; + Function* function = wasm->getFunction(i); mappedLocals.clear(); numLocalsByType.clear(); if (debug) std::cerr << "writing" << function->name << std::endl; @@ -654,7 +654,7 @@ public: if (debug) std::cerr << "== writeexports" << std::endl; auto start = startSection(BinaryConsts::Section::ExportTable); o << U32LEB(wasm->exports.size()); - for (auto* curr : wasm->exports) { + for (auto& curr : wasm->exports) { if (debug) std::cerr << "write one" << std::endl; o << U32LEB(getFunctionIndex(curr->value)); writeInlineString(curr->name.str); @@ -666,14 +666,14 @@ public: if (wasm->memory.segments.size() == 0) return; uint32_t num = 0; for (auto& segment : wasm->memory.segments) { - if (segment.size > 0) num++; + if (segment.data.size() > 0) num++; } auto start = startSection(BinaryConsts::Section::DataSegments); o << U32LEB(num); for (auto& segment : wasm->memory.segments) { - if (segment.size == 0) continue; + if (segment.data.size() == 0) continue; o << U32LEB(segment.offset); - writeInlineBuffer(segment.data, segment.size); + writeInlineBuffer(&segment.data[0], segment.data.size()); } finishSection(start); } @@ -720,7 +720,7 @@ public: if (debug) std::cerr << "== writeNames" << std::endl; auto start = startSection(BinaryConsts::Section::Names); o << U32LEB(wasm->functions.size()); - for (auto* curr : wasm->functions) { + for (auto& curr : wasm->functions) { writeInlineString(curr->name.str); o << U32LEB(0); // TODO: locals } @@ -1354,7 +1354,7 @@ public: if (debug) std::cerr << "num: " << numTypes << std::endl; for (size_t i = 0; i < numTypes; i++) { if (debug) std::cerr << "read one" << std::endl; - auto curr = allocator.alloc<FunctionType>(); + auto curr = new FunctionType; auto form = getInt8(); assert(form == BinaryConsts::TypeForms::Basic); size_t numParams = getU32LEB(); @@ -1379,11 +1379,11 @@ public: if (debug) std::cerr << "num: " << num << std::endl; for (size_t i = 0; i < num; i++) { if (debug) std::cerr << "read one" << std::endl; - auto curr = allocator.alloc<Import>(); + auto curr = new Import; curr->name = Name(std::string("import$") + std::to_string(i)); auto index = getU32LEB(); assert(index < wasm.functionTypes.size()); - curr->type = wasm.functionTypes[index]; + curr->type = wasm.getFunctionType(index); assert(curr->type->name.is()); curr->module = getInlineString(); curr->base = getInlineString(); @@ -1400,8 +1400,7 @@ public: for (size_t i = 0; i < num; i++) { if (debug) std::cerr << "read one" << std::endl; auto index = getU32LEB(); - assert(index < wasm.functionTypes.size()); - functionTypes.push_back(wasm.functionTypes[index]); + functionTypes.push_back(wasm.getFunctionType(index)); } } @@ -1481,7 +1480,7 @@ public: if (debug) std::cerr << "num: " << num << std::endl; for (size_t i = 0; i < num; i++) { if (debug) std::cerr << "read one" << std::endl; - auto curr = allocator.alloc<Export>(); + auto curr = new Export; auto index = getU32LEB(); assert(index < functionTypes.size()); curr->name = getInlineString(); @@ -1549,15 +1548,13 @@ public: auto num = getU32LEB(); for (size_t i = 0; i < num; i++) { Memory::Segment curr; - curr.offset = getU32LEB(); + auto offset = getU32LEB(); auto size = getU32LEB(); - auto buffer = (char*)malloc(size); + char buffer[size]; for (size_t j = 0; j < size; j++) { buffer[j] = char(getInt8()); } - curr.data = (const char*)buffer; - curr.size = size; - wasm.memory.segments.push_back(curr); + wasm.memory.segments.emplace_back(offset, (const char*)buffer, size); } } @@ -1774,7 +1771,7 @@ public: void visitCallIndirect(CallIndirect *curr) { if (debug) std::cerr << "zz node: CallIndirect" << std::endl; auto arity = getU32LEB(); - curr->fullType = wasm.functionTypes[getU32LEB()]; + curr->fullType = wasm.getFunctionType(getU32LEB()); auto num = curr->fullType->params.size(); assert(num == arity); curr->operands.resize(num); |