diff options
Diffstat (limited to 'src/binary-reader.cc')
-rw-r--r-- | src/binary-reader.cc | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 341e8861..ee8eed49 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -122,6 +122,8 @@ class BinaryReader { [[nodiscard]] Result ReadIndex(Index* index, const char* desc); [[nodiscard]] Result ReadOffset(Offset* offset, const char* desc); [[nodiscard]] Result ReadAlignment(Address* align_log2, const char* desc); + [[nodiscard]] Result CheckAlignment(Address* align_log2, const char* desc); + [[nodiscard]] Result TakeHasMemidx(Address* align_log2, bool* has_memidx); [[nodiscard]] Result ReadMemidx(Index* memidx, const char* desc); [[nodiscard]] Result ReadMemLocation(Address* alignment_log2, Index* memidx, @@ -432,12 +434,24 @@ Result BinaryReader::ReadOffset(Offset* offset, const char* desc) { Result BinaryReader::ReadAlignment(Address* alignment_log2, const char* desc) { uint32_t value; CHECK_RESULT(ReadU32Leb128(&value, desc)); - if (value >= 128 || - (value >= 32 && !options_.features.multi_memory_enabled())) { - PrintError("invalid %s: %u", desc, value); + *alignment_log2 = value; + return Result::Ok; +} + +Result BinaryReader::CheckAlignment(Address* align_log2, const char* desc) { + uint32_t value = *align_log2; + if (value >= 32) { + PrintError("invalid %s: %" PRIu32, desc, value); return Result::Error; } - *alignment_log2 = value; + return Result::Ok; +} + +Result BinaryReader::TakeHasMemidx(Address* align_log2, bool* has_memidx) { + // extract the has_memidx flag + *has_memidx = (*align_log2 >> 6) & 1; + // then clear it + *align_log2 &= ~(1 << 6); return Result::Ok; } @@ -453,12 +467,14 @@ Result BinaryReader::ReadMemLocation(Address* alignment_log2, const char* desc_memidx, const char* desc_offset, uint8_t* lane_val) { + bool has_memidx = false; CHECK_RESULT(ReadAlignment(alignment_log2, desc_align)); + CHECK_RESULT(TakeHasMemidx(alignment_log2, &has_memidx)); + CHECK_RESULT(CheckAlignment(alignment_log2, desc_align)); *memidx = 0; - if (*alignment_log2 >> 6) { + if (has_memidx) { ERROR_IF(!options_.features.multi_memory_enabled(), "multi_memory not allowed"); - *alignment_log2 = *alignment_log2 & ((1 << 6) - 1); CHECK_RESULT(ReadMemidx(memidx, desc_memidx)); } CHECK_RESULT(ReadAddress(offset, 0, desc_offset)); @@ -2267,6 +2283,7 @@ Result BinaryReader::ReadLinkingSection(Offset section_size) { uint32_t flags; CHECK_RESULT(ReadStr(&name, "segment name")); CHECK_RESULT(ReadAlignment(&alignment_log2, "segment alignment")); + CHECK_RESULT(CheckAlignment(&alignment_log2, "segment alignment")); CHECK_RESULT(ReadU32Leb128(&flags, "segment flags")); CALLBACK(OnSegmentInfo, i, name, alignment_log2, flags); } |