A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Don't use CoW when table expressions may trap (#14925)
Alex Crichton5115b95ca106
diff --git a/crates/environ/src/compile/module_environ.rs b/crates/environ/src/compile/module_environ.rsindex 937cf27e7b..a8234c1c56 100644--- a/crates/environ/src/compile/module_environ.rs+++ b/crates/environ/src/compile/module_environ.rs@@ -1295,7 +1295,15 @@ impl ModuleTranslation<'_> {Some(top) => top,None => return true,};- top > self.module.tables[segment.table_index].limits.min+ if top > self.module.tables[segment.table_index].limits.min {+ return true;+ }+ match &segment.elements {+ TableSegmentElements::Functions(_) => false,+ TableSegmentElements::Expressions { exprs, .. } => exprs+ .iter()+ .any(|expr| expr.ops().iter().any(|op| op.may_trap())),+ }})}diff --git a/crates/environ/src/types.rs b/crates/environ/src/types.rsindex 5e13453544..d08f0885ff 100644--- a/crates/environ/src/types.rs+++ b/crates/environ/src/types.rs@@ -2212,6 +2212,37 @@ impl ConstOp {ConstOp::AnyConvertExtern => Operator::AnyConvertExtern,}}++ /// Returns whether this operator might trap, used to test whether a+ /// constant expression may trap or not as a whole.+ pub fn may_trap(&self) -> bool {+ match self {+ ConstOp::I32Const(_)+ | ConstOp::I64Const(_)+ | ConstOp::F32Const(_)+ | ConstOp::F64Const(_)+ | ConstOp::V128Const(_)+ | ConstOp::GlobalGet(_)+ | ConstOp::RefI31+ | ConstOp::RefNull(_)+ | ConstOp::RefFunc(_)+ | ConstOp::I32Add+ | ConstOp::I32Sub+ | ConstOp::I32Mul+ | ConstOp::I64Add+ | ConstOp::I64Sub+ | ConstOp::I64Mul+ | ConstOp::ExternConvertAny+ | ConstOp::AnyConvertExtern => false,++ // GC allocations can trap due to overflow, out of resources, etc.+ ConstOp::StructNew { .. }+ | ConstOp::StructNewDefault { .. }+ | ConstOp::ArrayNew { .. }+ | ConstOp::ArrayNewDefault { .. }+ | ConstOp::ArrayNewFixed { .. } => true,+ }+ }}/// The type that can be used to index into [Memory] and [Table].diff --git a/tests/misc_testsuite/gc/cow-order-trapping-elem-expr.wast b/tests/misc_testsuite/gc/cow-order-trapping-elem-expr.wastnew file mode 100644index 0000000000..21f92b0fcc--- /dev/null+++ b/tests/misc_testsuite/gc/cow-order-trapping-elem-expr.wast@@ -0,0 +1,24 @@+;;! gc = true++(module $M+ (type $t (func (result i32)))+ (table (export "t") 2 funcref)+ (func (export "call") (param i32) (result i32)+ (call_indirect (type $t) (local.get 0))))+(register "M" $M)++;; Instantiation fails because the `array.new_default` overflows, and this must+;; mean that the `data` segment is not applied.+(assert_trap+ (module+ (type $a (array (mut i64)))+ (import "M" "t" (table 2 funcref))+ (table $t2 1 anyref)+ (memory 1)+ (func $f (result i32) (i32.load8_u (i32.const 0)))+ (elem (table 0) (i32.const 0) func $f)+ (elem (table $t2) (i32.const 0) anyref (array.new_default $a (i32.const 0x7fffffff)))+ (data (i32.const 0) "\2a"))+ "allocation size too large")++(assert_return (invoke $M "call" (i32.const 0)) (i32.const 0))