Skip to content

Commit dfcabd9

Browse files
committed
More strictly check bounds in FACT trampolines
This commit is a hardening of the various in-bounds checks and such of the FACT compiler, in particular as related to strings. The previous implementation would check bounds in a few places but this was a bit ad-hoc and not uniformly done. There's no known issue with the prior checks, but given the sensitive nature of these checks I feel it's best to make this a bit more rigorous. Specifically the `malloc` helpers, and a newly added `realloc` helper, will internally verify not only alignment but additionally the size of the allocation itself. All manual invocations of `realloc` are switched over to this helper. Additionally all conversion of a guest pointer to a more structured value now additionally goes through helpers which performs these same checks to ensure that everything is in-bounds. The net result is that this should have no behavior change from before. A suite of tests are added for behavior around large strings, specifically exercising the maximum allowable size of strings. This uncovered a few minor issues in transcoding where spec-wise Wasmtime previously transcoded too many bytes before performing a growing `realloc`. Finally a few refactorings were done in FACT to handle some helpers going away, notably around translating the `map<K, V>` type, which cleans up the internals as well.
1 parent 241c014 commit dfcabd9

6 files changed

Lines changed: 798 additions & 387 deletions

File tree

crates/cranelift/src/compiler/component.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,8 @@ impl TrampolineCompiler<'_> {
18601860
args.push(self.len_param(1, from64));
18611861
args.push(self.ptr_param(2, to64, to_base));
18621862
args.push(self.len_param(3, to64));
1863+
let first_pass = self.builder.func.dfg.block_params(self.block0)[6];
1864+
args.push(first_pass);
18631865
}
18641866

18651867
Transcode::Utf8ToCompactUtf16 | Transcode::Utf16ToCompactUtf16 => {

crates/environ/src/component.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ macro_rules! foreach_builtin_component_function {
211211
latin1_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8) -> bool;
212212
latin1_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> bool;
213213
utf8_to_utf16(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u16) -> size;
214-
utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
215-
latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, ret2: ptr_size) -> size;
214+
utf16_to_utf8(vmctx: vmctx, src: ptr_u16, src_len: size, dst: ptr_u8, dst_len: size, first_pass: u32, ret2: ptr_size) -> size;
215+
latin1_to_utf8(vmctx: vmctx, src: ptr_u8, src_len: size, dst: ptr_u8, dst_len: size, first_pass: u32, ret2: ptr_size) -> size;
216216
utf16_to_compact_probably_utf16(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u16) -> size;
217217
utf8_to_latin1(vmctx: vmctx, src: ptr_u8, len: size, dst: ptr_u8, ret2: ptr_size) -> size;
218218
utf16_to_latin1(vmctx: vmctx, src: ptr_u16, len: size, dst: ptr_u8, ret2: ptr_size) -> size;

0 commit comments

Comments
 (0)