Skip to content

Commit

Permalink
Extensible name section (#933)
Browse files Browse the repository at this point in the history
See #914.

* extensible name section support: read function names, too

* c-api-unused-mem.txt: change expected size to match new name section

* * check subsection size matches

* print warning for unknown name subsections (including the local
  section)
  • Loading branch information
pipcet authored and dschuff committed Apr 13, 2017
1 parent 5fb669a commit 57a2bb9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ enum EncodedType {

namespace UserSections {
extern const char* Name;

enum Subsection {
NameFunction = 1,
NameLocal = 2,
};
}

enum ASTNodes {
Expand Down Expand Up @@ -545,6 +550,8 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
void writeResizableLimits(Address initial, Address maximum, bool hasMaximum);
int32_t startSection(BinaryConsts::Section code);
void finishSection(int32_t start);
int32_t startSubsection(BinaryConsts::UserSections::Subsection code);
void finishSubsection(int32_t start);
void writeStart();
void writeMemory();
void writeTypes();
Expand Down Expand Up @@ -721,7 +728,7 @@ class WasmBinaryBuilder {

void readFunctionTableDeclaration();
void readTableElements();
void readNames();
void readNames(size_t);

// AST reading
int depth = 0; // only for debugging
Expand Down
62 changes: 43 additions & 19 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ void WasmBinaryWriter::finishSection(int32_t start) {
o.writeAt(start, U32LEB(size));
}

int32_t WasmBinaryWriter::startSubsection(BinaryConsts::UserSections::Subsection code) {
o << U32LEB(code);
return writeU32LEBPlaceholder(); // section size to be filled in later
}

void WasmBinaryWriter::finishSubsection(int32_t start) {
int32_t size = o.size() - start - 5; // section size does not include the 5 bytes of the size field itself
o.writeAt(start, U32LEB(size));
}

void WasmBinaryWriter::writeStart() {
if (!wasm->start.is()) return;
if (debug) std::cerr << "== writeStart" << std::endl;
Expand Down Expand Up @@ -389,21 +399,24 @@ void WasmBinaryWriter::writeNames() {
if (debug) std::cerr << "== writeNames" << std::endl;
auto start = startSection(BinaryConsts::Section::User);
writeInlineString(BinaryConsts::UserSections::Name);
auto substart = startSubsection(BinaryConsts::UserSections::Subsection::NameFunction);
o << U32LEB(mappedFunctions.size());
Index emitted = 0;
for (auto& import : wasm->imports) {
if (import->kind == ExternalKind::Function) {
o << U32LEB(emitted);
writeInlineString(import->name.str);
o << U32LEB(0); // TODO: locals
emitted++;
}
}
for (auto& curr : wasm->functions) {
o << U32LEB(emitted);
writeInlineString(curr->name.str);
o << U32LEB(0); // TODO: locals
emitted++;
}
assert(emitted == mappedFunctions.size());
finishSubsection(substart);
/* TODO: locals */
finishSection(start);
}

Expand Down Expand Up @@ -969,7 +982,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
auto oldPos = pos;
Name sectionName = getInlineString();
if (sectionName.equals(BinaryConsts::UserSections::Name)) {
readNames();
readNames(payloadLen - (pos - oldPos));
} else {
// an unfamiliar custom section
wasm.userSections.resize(wasm.userSections.size() + 1);
Expand Down Expand Up @@ -1510,25 +1523,36 @@ void WasmBinaryBuilder::readTableElements() {
}
}

void WasmBinaryBuilder::readNames() {
void WasmBinaryBuilder::readNames(size_t payloadLen) {
if (debug) std::cerr << "== readNames" << std::endl;
auto num = getU32LEB();
if (num == 0) return;
for (auto& import : wasm.imports) {
if (import->kind == ExternalKind::Function) {
getInlineString(); // TODO: use this
auto numLocals = getU32LEB();
WASM_UNUSED(numLocals);
assert(numLocals == 0); // TODO
if (--num == 0) return;
auto sectionPos = pos;
while (pos < sectionPos + payloadLen) {
auto nameType = getU32LEB();
auto subsectionSize = getU32LEB();
auto subsectionPos = pos;
if (nameType != BinaryConsts::UserSections::Subsection::NameFunction) {
// TODO: locals
std::cerr << "unknown name subsection at " << pos << std::endl;
pos = subsectionPos + subsectionSize;
continue;
}
auto num = getU32LEB();
uint32_t importedFunctions = 0;
for (auto& import : wasm.imports) {
if (import->kind != ExternalKind::Function) continue;
importedFunctions++;
}
for (size_t i = 0; i < num; i++) {
auto index = getU32LEB();
if (index < importedFunctions) {
getInlineString(); // TODO: use this
} else if (index - importedFunctions < functions.size()) {
functions[index - importedFunctions]->name = getInlineString();
}
}
assert(pos == subsectionPos + subsectionSize);
}
for (size_t i = 0; i < num; i++) {
functions[i]->name = getInlineString();
auto numLocals = getU32LEB();
WASM_UNUSED(numLocals);
assert(numLocals == 0); // TODO
}
assert(pos == sectionPos + payloadLen);
}

BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
Expand Down
2 changes: 1 addition & 1 deletion test/example/c-api-unused-mem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(call $main)
)
)
207
213
(module
(type $0 (func))
(type $1 (func))
Expand Down

0 comments on commit 57a2bb9

Please sign in to comment.