summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-06-18 11:13:05 -0700
committerGitHub <noreply@github.com>2024-06-18 18:13:05 +0000
commitc3b9cde9d99efe46fc34af1d5b27351f34962e94 (patch)
tree4b82a3198e329cdffcab3b8c6d16df3922b82632
parent0f9f2dc21a913164c5a8681b93b7db4d383a8c47 (diff)
downloadbinaryen-c3b9cde9d99efe46fc34af1d5b27351f34962e94.tar.gz
binaryen-c3b9cde9d99efe46fc34af1d5b27351f34962e94.tar.bz2
binaryen-c3b9cde9d99efe46fc34af1d5b27351f34962e94.zip
Reject invalid section IDs (#6675)
Rather than treating them as custom sections. Also fix UB where invalid `Section` enum values could be used as keys in a map. Use the raw `uint8_t` section IDs as keys instead. Re-enable a disabled spec test that was failing because of this bug and UB.
-rw-r--r--scripts/test/shared.py1
-rw-r--r--src/wasm-binary.h2
-rw-r--r--src/wasm/wasm-binary.cpp13
3 files changed, 8 insertions, 8 deletions
diff --git a/scripts/test/shared.py b/scripts/test/shared.py
index 847dd9de5..f5fda92a5 100644
--- a/scripts/test/shared.py
+++ b/scripts/test/shared.py
@@ -408,7 +408,6 @@ SPEC_TESTS_TO_SKIP = [
'utf8-invalid-encoding.wast',
'const.wast',
'address.wast',
- 'custom.wast', # invalid section ID accepted as Custom, triggering UBSan
# Unlinkable module accepted
'linking.wast',
diff --git a/src/wasm-binary.h b/src/wasm-binary.h
index 9a79a4c8b..f7d57f5c9 100644
--- a/src/wasm-binary.h
+++ b/src/wasm-binary.h
@@ -1453,7 +1453,7 @@ class WasmBinaryReader {
Index startIndex = -1;
std::set<Function::DebugLocation> debugLocation;
size_t codeSectionLocation;
- std::set<BinaryConsts::Section> seenSections;
+ std::unordered_set<uint8_t> seenSections;
// All types defined in the type section
std::vector<HeapType> types;
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index e08644eae..b05dd5237 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -1784,11 +1784,8 @@ void WasmBinaryReader::read() {
// note the section in the list of seen sections, as almost no sections can
// appear more than once, and verify those that shouldn't do not.
if (sectionCode != BinaryConsts::Section::Custom &&
- sectionCode != BinaryConsts::Section::Code) {
- if (!seenSections.insert(BinaryConsts::Section(sectionCode)).second) {
- throwError("section seen more than once: " +
- std::to_string(sectionCode));
- }
+ !seenSections.insert(sectionCode).second) {
+ throwError("section seen more than once: " + std::to_string(sectionCode));
}
switch (sectionCode) {
@@ -1837,7 +1834,7 @@ void WasmBinaryReader::read() {
case BinaryConsts::Section::Tag:
readTags();
break;
- default: {
+ case BinaryConsts::Section::Custom: {
readCustomSection(payloadLen);
if (pos > oldPos + payloadLen) {
throwError("bad user section size, started at " +
@@ -1846,7 +1843,11 @@ void WasmBinaryReader::read() {
" not being equal to new position " + std::to_string(pos));
}
pos = oldPos + payloadLen;
+ break;
}
+ default:
+ throwError(std::string("unrecognized section ID: ") +
+ std::to_string(sectionCode));
}
// make sure we advanced exactly past this section