Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
# expected-output/ if any.
SPEC_TESTS_TO_SKIP = [
# Malformed module accepted
'binary-leb128.wast',
'utf8-custom-section-id.wast',
'utf8-import-field.wast',
'utf8-import-module.wast',
Expand Down
27 changes: 16 additions & 11 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,22 @@ template<typename T, typename MiniT> struct LEB {
bool last = !(byte & 128);
T payload = byte & 127;
using mask_type = typename std::make_unsigned<T>::type;
auto shift_mask = 0 == shift
? ~mask_type(0)
: ((mask_type(1) << (sizeof(T) * 8 - shift)) - 1u);
T significant_payload = payload & shift_mask;
if (significant_payload != payload) {
if (!(std::is_signed<T>::value && last)) {
throw ParseException("LEB dropped bits only valid for signed LEB");
auto payload_mask = 0 == shift
? ~mask_type(0)
: ((mask_type(1) << (sizeof(T) * 8 - shift)) - 1u);
T significant_payload = payload_mask & payload;
value |= significant_payload << shift;
T unused_bits_mask = ~payload_mask & 127;
T unused_bits = payload & unused_bits_mask;
if (std::is_signed_v<T> && value < 0) {
if (unused_bits != unused_bits_mask) {
throw ParseException("Unused negative LEB bits must be 1s");
}
} else {
if (unused_bits != 0) {
throw ParseException("Unused non-negative LEB bits must be 0s");
}
}
value |= significant_payload << shift;
if (last) {
break;
}
Expand All @@ -120,9 +126,8 @@ template<typename T, typename MiniT> struct LEB {
throw ParseException("LEB overflow");
}
}
// If signed LEB, then we might need to sign-extend. (compile should
// optimize this out if not needed).
if (std::is_signed<T>::value) {
// If signed LEB, then we might need to sign-extend.
if constexpr (std::is_signed_v<T>) {
shift += 7;
if ((byte & 64) && size_t(shift) < 8 * sizeof(T)) {
size_t sext_bits = 8 * sizeof(T) - size_t(shift);
Expand Down