Skip to content

Commit 57a2bb9

Browse files
pipcetdschuff
authored andcommitted
Extensible name section (#933)
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)
1 parent 5fb669a commit 57a2bb9

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

src/wasm-binary.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ enum EncodedType {
303303

304304
namespace UserSections {
305305
extern const char* Name;
306+
307+
enum Subsection {
308+
NameFunction = 1,
309+
NameLocal = 2,
310+
};
306311
}
307312

308313
enum ASTNodes {
@@ -545,6 +550,8 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
545550
void writeResizableLimits(Address initial, Address maximum, bool hasMaximum);
546551
int32_t startSection(BinaryConsts::Section code);
547552
void finishSection(int32_t start);
553+
int32_t startSubsection(BinaryConsts::UserSections::Subsection code);
554+
void finishSubsection(int32_t start);
548555
void writeStart();
549556
void writeMemory();
550557
void writeTypes();
@@ -721,7 +728,7 @@ class WasmBinaryBuilder {
721728

722729
void readFunctionTableDeclaration();
723730
void readTableElements();
724-
void readNames();
731+
void readNames(size_t);
725732

726733
// AST reading
727734
int depth = 0; // only for debugging

src/wasm/wasm-binary.cpp

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ void WasmBinaryWriter::finishSection(int32_t start) {
8383
o.writeAt(start, U32LEB(size));
8484
}
8585

86+
int32_t WasmBinaryWriter::startSubsection(BinaryConsts::UserSections::Subsection code) {
87+
o << U32LEB(code);
88+
return writeU32LEBPlaceholder(); // section size to be filled in later
89+
}
90+
91+
void WasmBinaryWriter::finishSubsection(int32_t start) {
92+
int32_t size = o.size() - start - 5; // section size does not include the 5 bytes of the size field itself
93+
o.writeAt(start, U32LEB(size));
94+
}
95+
8696
void WasmBinaryWriter::writeStart() {
8797
if (!wasm->start.is()) return;
8898
if (debug) std::cerr << "== writeStart" << std::endl;
@@ -389,21 +399,24 @@ void WasmBinaryWriter::writeNames() {
389399
if (debug) std::cerr << "== writeNames" << std::endl;
390400
auto start = startSection(BinaryConsts::Section::User);
391401
writeInlineString(BinaryConsts::UserSections::Name);
402+
auto substart = startSubsection(BinaryConsts::UserSections::Subsection::NameFunction);
392403
o << U32LEB(mappedFunctions.size());
393404
Index emitted = 0;
394405
for (auto& import : wasm->imports) {
395406
if (import->kind == ExternalKind::Function) {
407+
o << U32LEB(emitted);
396408
writeInlineString(import->name.str);
397-
o << U32LEB(0); // TODO: locals
398409
emitted++;
399410
}
400411
}
401412
for (auto& curr : wasm->functions) {
413+
o << U32LEB(emitted);
402414
writeInlineString(curr->name.str);
403-
o << U32LEB(0); // TODO: locals
404415
emitted++;
405416
}
406417
assert(emitted == mappedFunctions.size());
418+
finishSubsection(substart);
419+
/* TODO: locals */
407420
finishSection(start);
408421
}
409422

@@ -969,7 +982,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
969982
auto oldPos = pos;
970983
Name sectionName = getInlineString();
971984
if (sectionName.equals(BinaryConsts::UserSections::Name)) {
972-
readNames();
985+
readNames(payloadLen - (pos - oldPos));
973986
} else {
974987
// an unfamiliar custom section
975988
wasm.userSections.resize(wasm.userSections.size() + 1);
@@ -1510,25 +1523,36 @@ void WasmBinaryBuilder::readTableElements() {
15101523
}
15111524
}
15121525

1513-
void WasmBinaryBuilder::readNames() {
1526+
void WasmBinaryBuilder::readNames(size_t payloadLen) {
15141527
if (debug) std::cerr << "== readNames" << std::endl;
1515-
auto num = getU32LEB();
1516-
if (num == 0) return;
1517-
for (auto& import : wasm.imports) {
1518-
if (import->kind == ExternalKind::Function) {
1519-
getInlineString(); // TODO: use this
1520-
auto numLocals = getU32LEB();
1521-
WASM_UNUSED(numLocals);
1522-
assert(numLocals == 0); // TODO
1523-
if (--num == 0) return;
1528+
auto sectionPos = pos;
1529+
while (pos < sectionPos + payloadLen) {
1530+
auto nameType = getU32LEB();
1531+
auto subsectionSize = getU32LEB();
1532+
auto subsectionPos = pos;
1533+
if (nameType != BinaryConsts::UserSections::Subsection::NameFunction) {
1534+
// TODO: locals
1535+
std::cerr << "unknown name subsection at " << pos << std::endl;
1536+
pos = subsectionPos + subsectionSize;
1537+
continue;
15241538
}
1539+
auto num = getU32LEB();
1540+
uint32_t importedFunctions = 0;
1541+
for (auto& import : wasm.imports) {
1542+
if (import->kind != ExternalKind::Function) continue;
1543+
importedFunctions++;
1544+
}
1545+
for (size_t i = 0; i < num; i++) {
1546+
auto index = getU32LEB();
1547+
if (index < importedFunctions) {
1548+
getInlineString(); // TODO: use this
1549+
} else if (index - importedFunctions < functions.size()) {
1550+
functions[index - importedFunctions]->name = getInlineString();
1551+
}
1552+
}
1553+
assert(pos == subsectionPos + subsectionSize);
15251554
}
1526-
for (size_t i = 0; i < num; i++) {
1527-
functions[i]->name = getInlineString();
1528-
auto numLocals = getU32LEB();
1529-
WASM_UNUSED(numLocals);
1530-
assert(numLocals == 0); // TODO
1531-
}
1555+
assert(pos == sectionPos + payloadLen);
15321556
}
15331557

15341558
BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {

test/example/c-api-unused-mem.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
(call $main)
4747
)
4848
)
49-
207
49+
213
5050
(module
5151
(type $0 (func))
5252
(type $1 (func))

0 commit comments

Comments
 (0)