Repository: bytecodealliance/wasmtime
Pull request #8630 (2024-05-15 21:27:33)

Page align the static memory maximum size
This fixes an accidental regression from #8616 where page alignment was implicitly happening due to how configuration was processed but it wasn't re-added in the refactoring.

<!--
Please make sure you include the following information:

- If this work has been discussed elsewhere, please include a link to that
  conversation. If it was discussed in an issue, just mention "issue #...".

- Explain why this change is needed. If the details are in an issue already,
  this can be brief.

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->



Patch:
diff --git a/cranelift/codegen/src/isle_prelude.rs b/cranelift/codegen/src/isle_prelude.rs
index 39bc55027..424531d64 100644
--- a/cranelift/codegen/src/isle_prelude.rs
+++ b/cranelift/codegen/src/isle_prelude.rs
@@ -953,5 +953,29 @@ macro_rules! isle_common_prelude_methods {
                 None
             }
         }
+
+        fn f32_neg(&mut self, n: Ieee32) -> Ieee32 {
+            n.neg()
+        }
+
+        fn f32_abs(&mut self, n: Ieee32) -> Ieee32 {
+            n.abs()
+        }
+
+        fn f32_copysign(&mut self, a: Ieee32, b: Ieee32) -> Ieee32 {
+            a.copysign(b)
+        }
+
+        fn f64_neg(&mut self, n: Ieee64) -> Ieee64 {
+            n.neg()
+        }
+
+        fn f64_abs(&mut self, n: Ieee64) -> Ieee64 {
+            n.abs()
+        }
+
+        fn f64_copysign(&mut self, a: Ieee64, b: Ieee64) -> Ieee64 {
+            a.copysign(b)
+        }
     };
 }
diff --git a/cranelift/codegen/src/opts/cprop.isle b/cranelift/codegen/src/opts/cprop.isle
index 8c86ff2c5..d6b56543b 100644
--- a/cranelift/codegen/src/opts/cprop.isle
+++ b/cranelift/codegen/src/opts/cprop.isle
@@ -279,3 +279,19 @@
 (extern constructor u64_bswap32 u64_bswap32)
 (decl pure u64_bswap64 (u64) u64)
 (extern constructor u64_bswap64 u64_bswap64)
+
+;; Constant fold bitwise float operations (fneg/fabs/fcopysign)
+(rule (simplify (fneg $F32 (f32const $F32 n)))
+      (subsume (f32const $F32 (f32_neg n))))
+(rule (simplify (fneg $F64 (f64const $F64 n)))
+      (subsume (f64const $F64 (f64_neg n))))
+
+(rule (simplify (fabs $F32 (f32const $F32 n)))
+      (subsume (f32const $F32 (f32_abs n))))
+(rule (simplify (fabs $F64 (f64const $F64 n)))
+      (subsume (f64const $F64 (f64_abs n))))
+
+(rule (simplify (fcopysign $F32 (f32const $F32 n) (f32const $F32 m)))
+      (subsume (f32const $F32 (f32_copysign n m))))
+(rule (simplify (fcopysign $F64 (f64const $F64 n) (f64const $F64 m)))
+      (subsume (f64const $F64 (f64_copysign n m))))
diff --git a/cranelift/codegen/src/prelude.isle b/cranelift/codegen/src/prelude.isle
index e7517fc41..1df50f950 100644
--- a/cranelift/codegen/src/prelude.isle
+++ b/cranelift/codegen/src/prelude.isle
@@ -234,6 +234,21 @@
 (decl u16_replicated_u8 (u8) u64)
 (extern extractor u16_replicated_u8 u16_replicated_u8)
 
+;; Floating point operations
+
+(decl pure f32_neg (Ieee32) Ieee32)
+(extern constructor f32_neg f32_neg)
+(decl pure f32_abs (Ieee32) Ieee32)
+(extern constructor f32_abs f32_abs)
+(decl pure f32_copysign (Ieee32 Ieee32) Ieee32)
+(extern constructor f32_copysign f32_copysign)
+(decl pure f64_neg (Ieee64) Ieee64)
+(extern constructor f64_neg f64_neg)
+(decl pure f64_abs (Ieee64) Ieee64)
+(extern constructor f64_abs f64_abs)
+(decl pure f64_copysign (Ieee64 Ieee64) Ieee64)
+(extern constructor f64_copysign f64_copysign)
+
 ;;;; `cranelift_codegen::ir::Type` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (extern const $I8 Type)
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs
index 7afc00bbf..5522e561e 100644
--- a/crates/wasmtime/src/config.rs
+++ b/crates/wasmtime/src/config.rs
@@ -1368,7 +1368,7 @@ impl Config {
     /// for pooling allocation by using memory protection; see
     /// `PoolingAllocatorConfig::memory_protection_keys` for details.
     pub fn static_memory_maximum_size(&mut self, max_size: u64) -> &mut Self {
-        self.tunables.static_memory_reservation = Some(max_size);
+        self.tunables.static_memory_reservation = Some(round_up_to_pages(max_size));
         self
     }
 


Test patch:
diff --git a/cranelift/filetests/filetests/egraph/cprop.clif b/cranelift/filetests/filetests/egraph/cprop.clif
index 95f902d7b..df1ac116f 100644
--- a/cranelift/filetests/filetests/egraph/cprop.clif
+++ b/cranelift/filetests/filetests/egraph/cprop.clif
@@ -312,3 +312,65 @@ block0:
 
 ; check: v2 = iconst.i64 0xf0de_bc9a_7856_3412
 ; nextln: return v2
+
+function %f32_fneg() -> f32 {
+block0:
+    v1 = f32const 0.0
+    v2 = fneg v1
+    return v2
+}
+
+; check: v3 = f32const -0.0
+; check: return v3  ; v3 = -0.0
+
+function %f32_fabs() -> f32 {
+block0:
+    v1 = f32const -0.0
+    v2 = fabs v1
+    return v2
+}
+
+; check: v3 = f32const 0.0
+; check: return v3  ; v3 = 0.0
+
+function %f32_fabs() -> f32 {
+block0:
+    v1 = f32const -0.0
+    v2 = f32const NaN
+    v3 = fcopysign v2, v1
+    return v3
+}
+
+; check: v4 = f32const -NaN
+; check: return v4  ; v4 = -NaN
+
+function %f64_fneg() -> f64 {
+block0:
+    v1 = f64const 0.0
+    v2 = fneg v1
+    return v2
+}
+
+; check: v3 = f64const -0.0
+; check: return v3  ; v3 = -0.0
+
+function %f64_fabs() -> f64 {
+block0:
+    v1 = f64const -0.0
+    v2 = fabs v1
+    return v2
+}
+
+; check: v3 = f64const 0.0
+; check: return v3  ; v3 = 0.0
+
+function %f64_fabs() -> f64 {
+block0:
+    v1 = f64const -0.0
+    v2 = f64const NaN
+    v3 = fcopysign v2, v1
+    return v3
+}
+
+; check: v4 = f64const -NaN
+; check: return v4  ; v4 = -NaN
diff --git a/tests/all/memory.rs b/tests/all/memory.rs
index 6625cf919..6bbd3c2ce 100644
--- a/tests/all/memory.rs
+++ b/tests/all/memory.rs
@@ -658,3 +658,14 @@ fn init_with_negative_segment() -> Result<()> {
     Instance::new(&mut store, &module, &[])?;
     Ok(())
 }
+
+#[test]
+fn non_page_aligned_static_memory() -> Result<()> {
+    let mut config = Config::new();
+    config.static_memory_maximum_size(100_000);
+    config.static_memory_forced(true);
+    let engine = Engine::new(&config)?;
+    let ty = MemoryType::new(1, None);
+    Memory::new(&mut Store::new(&engine, ()), ty)?;
+    Ok(())
+}