Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix bounds check for `error-context.debug-message` (#12879)

Alex Crichton0fbbb7545c1b
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index a20fa34a98..e7bd850d43 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -4205,11 +4205,15 @@ impl Instance {
let lower_cx = &mut LowerContext::new(store, options, self);
let debug_msg_address = usize::try_from(debug_msg_address)?;
- // Lower the string into the component's memory
+ // Lower the string into the component's memory.
+ //
+ // Note that the "8" here is the size of a WIT `string` in linear
+ // memory, the ptr+length. This'll need to be updated when `memory64`
+ // comes along. (FIXME(#4311))
let offset = lower_cx
.as_slice_mut()
.get(debug_msg_address..)
- .and_then(|b| b.get(..debug_msg.bytes().len()))
+ .and_then(|b| b.get(..8))
.map(|_| debug_msg_address)
.ok_or_else(|| crate::format_err!("invalid debug message pointer: out of bounds"))?;
debug_msg
diff --git a/tests/misc_testsuite/component-model/async/error-context.wast b/tests/misc_testsuite/component-model/async/error-context.wast
index c1be6d8011..8f6c00410b 100644
--- a/tests/misc_testsuite/component-model/async/error-context.wast
+++ b/tests/misc_testsuite/component-model/async/error-context.wast
@@ -34,3 +34,53 @@
(core func $error-context-drop (canon error-context.drop))
(core instance $i (instantiate $m (with "" (instance (export "error-context.drop" (func $error-context-drop))))))
)
+
+;; Test edge-case behavior of `error-context.debug-message`.
+(component definition $A
+ (core module $libc
+ (memory (export "memory") 1)
+ (global $bump (mut i32) (i32.const 100))
+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)
+ (local $ret i32)
+ (if (local.get 0) (then unreachable))
+ (if (local.get 1) (then unreachable))
+ (if (i32.ne (local.get 2) (i32.const 1)) (then unreachable))
+ (local.set $ret (global.get $bump))
+ (global.set $bump (i32.add (global.get $bump) (local.get 3)))
+ (local.get $ret)
+ )
+ )
+ (core instance $libc (instantiate $libc))
+
+ (core module $Core
+ (import "" "mem" (memory 1))
+ (import "" "error-context.new" (func $error-context.new (param i32 i32) (result i32)))
+ (import "" "error-context.debug-message" (func $error-context.debug-message (param i32 i32)))
+ (import "" "error-context.drop" (func $error-context.drop (param i32)))
+
+ (func (export "run") (param $dst i32)
+ (local $handle i32)
+ (i32.store8 (i32.const 0) (i32.const 0x61)) ;; 'a'
+ (local.set $handle (call $error-context.new (i32.const 0) (i32.const 1)))
+ (call $error-context.debug-message (local.get $handle) (local.get $dst))
+ (call $error-context.drop (local.get $handle))
+ )
+ )
+
+ (core func $error-context.new (canon error-context.new (memory $libc "memory")))
+ (core func $error-context.debug-message (canon error-context.debug-message (memory $libc "memory") (realloc (func $libc "realloc"))))
+ (core func $error-context.drop (canon error-context.drop))
+
+ (core instance $core (instantiate $Core (with "" (instance
+ (export "mem" (memory $libc "memory"))
+ (export "error-context.new" (func $error-context.new))
+ (export "error-context.debug-message" (func $error-context.debug-message))
+ (export "error-context.drop" (func $error-context.drop))
+ ))))
+
+ (func (export "run") (param "p" u32) (canon lift (core func $core "run")))
+)
+
+(component instance $A $A)
+(assert_return (invoke "run" (u32.const 65528)))
+(assert_trap (invoke "run" (u32.const 65532)) "invalid debug message pointer")