Skip to content

Commit

Permalink
Write HeapKind and BlockType as signed values (#32)
Browse files Browse the repository at this point in the history
The heap and block type encodings both use signed values, where negative
values represent well-known values, and non-negative values are indexes
into the type section. This commit fixes a bug where the indexes were
written with LEB128 encoding (instead of SLEB128 encoding).

The difference is first noticeable at index 64, where the unsigned
encoding is `0x40`, but the unsigned encoding is `0xc0 0x00`.
  • Loading branch information
binji authored Nov 19, 2020
1 parent 61de0ea commit 5242b44
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/wasp/binary/write.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Iterator Write(HeapType value, Iterator out) {
return Write(encoding::HeapKind::Encode(value.heap_kind()), out);
} else {
assert(value.is_index());
return Write(value.index(), out);
return Write(static_cast<s32>(value.index()), out);
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ Iterator Write(BlockType value, Iterator out) {
return Write(encoding::BlockType::Void, out);
} else {
assert(value.is_index());
return Write(value.index(), out);
return Write(static_cast<s32>(value.index()), out);
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/binary/write_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ TEST(BinaryWriteTest, BlockType_MVP) {

TEST(BinaryWriteTest, BlockType_multi_value) {
ExpectWrite("\x01"_su8, BlockType{Index{1}});
ExpectWrite("\xc0\x00"_su8, BlockType{Index{64}});
ExpectWrite("\xc0\x03"_su8, BlockType{Index{448}});
}

Expand Down Expand Up @@ -476,6 +477,7 @@ TEST(BinaryWriteTest, HeapType_function_references) {
ExpectWrite("\x70"_su8, HT_Func);
ExpectWrite("\x6f"_su8, HT_Extern);
ExpectWrite("\x00"_su8, HT_0);
ExpectWrite("\xc0\x00"_su8, HeapType{At{"\xc0\x00"_su8, Index{64}}});
}

TEST(BinaryWriteTest, HeapType_gc) {
Expand Down

0 comments on commit 5242b44

Please sign in to comment.