Skip to content

Commit cbb24eb

Browse files
authored
Misc minor ASAN fixes (#1869)
* handle end of input in skipWhitespace in s-parser. fixes #1863 * ignore debug locations when not in a function ; fixes #1867 * error properly on invalid user section sizes ; fixes #1866 * throw a proper error on invalid call offsets in binary reading ; fixes #1865
1 parent d24427d commit cbb24eb

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,13 @@ void WasmBinaryBuilder::read() {
709709
void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
710710
auto oldPos = pos;
711711
Name sectionName = getInlineString();
712+
size_t read = pos - oldPos;
713+
if (read > payloadLen) {
714+
throwError("bad user section size");
715+
}
716+
payloadLen -= read;
712717
if (sectionName.equals(BinaryConsts::UserSections::Name)) {
713-
readNames(payloadLen - (pos - oldPos));
718+
readNames(payloadLen);
714719
} else {
715720
// an unfamiliar custom section
716721
if (sectionName.equals(BinaryConsts::UserSections::Linking)) {
@@ -719,7 +724,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
719724
wasm.userSections.resize(wasm.userSections.size() + 1);
720725
auto& section = wasm.userSections.back();
721726
section.name = sectionName.str;
722-
auto sectionSize = payloadLen - (pos - oldPos);
727+
auto sectionSize = payloadLen;
723728
section.data.resize(sectionSize);
724729
for (size_t i = 0; i < sectionSize; i++) {
725730
section.data[i] = getInt8();
@@ -1950,7 +1955,10 @@ void WasmBinaryBuilder::visitCall(Call* curr) {
19501955
auto* import = functionImports[index];
19511956
type = wasm.getFunctionType(import->type);
19521957
} else {
1953-
auto adjustedIndex = index - functionImports.size();
1958+
Index adjustedIndex = index - functionImports.size();
1959+
if (adjustedIndex >= functionTypes.size()) {
1960+
throwError("invalid call index");
1961+
}
19541962
type = functionTypes[adjustedIndex];
19551963
}
19561964
assert(type);

src/wasm/wasm-s-parser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,14 @@ void SExpressionParser::skipWhitespace() {
185185
}
186186
while (input[0] && input[0] != '\n') input++;
187187
line++;
188+
if (!input[0]) return;
188189
lineStart = ++input;
189190
} else if (input[0] == '(' && input[1] == ';') {
190191
// Skip nested block comments.
191192
input += 2;
192193
int depth = 1;
193194
while (1) {
194-
if (input[0] == 0) {
195-
return;
196-
}
195+
if (!input[0]) return;
197196
if (input[0] == '(' && input[1] == ';') {
198197
input += 2;
199198
depth++;
@@ -656,7 +655,7 @@ Function::DebugLocation SExpressionWasmBuilder::getDebugLocation(const SourceLoc
656655

657656
Expression* SExpressionWasmBuilder::parseExpression(Element& s) {
658657
Expression* result = makeExpression(s);
659-
if (s.startLoc) {
658+
if (s.startLoc && currFunction) {
660659
currFunction->debugLocations[result] = getDebugLocation(*s.startLoc);
661660
}
662661
return result;

0 commit comments

Comments
 (0)