Skip to content

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

trap on ref count overflow in `error_context_transfer` (#12980)

Joel Dice42d271b90d76
diff --git a/crates/c-api/include/wasmtime/trap.h b/crates/c-api/include/wasmtime/trap.h
index 6c29b991a5..f709ff1380 100644
--- a/crates/c-api/include/wasmtime/trap.h
+++ b/crates/c-api/include/wasmtime/trap.h
@@ -134,6 +134,8 @@ enum wasmtime_trap_code_enum {
/// Cannot issue a read/write on a future/stream while there is a
/// pending operation already.
WASMTIME_TRAP_CODE_CONCURRENT_FUTURE_STREAM_OP = 45,
+ /// A reference count (for e.g. an `error-context`) overflowed.
+ WASMTIME_TRAP_CODE_REFERENCE_COUNT_OVERFLOW = 46,
};
/**
diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs
index 9af88d3671..70654c0d62 100644
--- a/crates/c-api/src/trap.rs
+++ b/crates/c-api/src/trap.rs
@@ -52,6 +52,7 @@ const _: () = {
assert!(Trap::UnsupportedCallbackCode as u8 == 43);
assert!(Trap::CannotResumeThread as u8 == 44);
assert!(Trap::ConcurrentFutureStreamOp as u8 == 45);
+ assert!(Trap::ReferenceCountOverflow as u8 == 46);
};
#[repr(C)]
diff --git a/crates/environ/src/trap_encoding.rs b/crates/environ/src/trap_encoding.rs
index 936aab7f06..e7a8743d5d 100644
--- a/crates/environ/src/trap_encoding.rs
+++ b/crates/environ/src/trap_encoding.rs
@@ -220,6 +220,9 @@ generate_trap_type! {
/// pending operation already.
ConcurrentFutureStreamOp = "cannot have concurrent operations active on a future/stream",
+ /// A reference count (for e.g. an `error-context`) overflowed.
+ ReferenceCountOverflow = "reference count overflow",
+
// if adding a variant here be sure to update `trap.rs` and `trap.h` as
// mentioned above
}
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 5ce49e1daa..608b3b23bf 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -4472,7 +4472,11 @@ impl Instance {
.global_error_context_ref_counts
.get_mut(&TypeComponentGlobalErrorContextTableIndex::from_u32(rep))
.context("global ref count present for existing (sub)component error context")?;
- global_ref_count.0 += 1;
+
+ global_ref_count.0 = global_ref_count
+ .0
+ .checked_add(1)
+ .ok_or_else(|| format_err!(Trap::ReferenceCountOverflow))?;
Ok(dst_idx)
}