Skip to content

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

Add a `enabled: bool` parameter to `MemFlags` modifiers (#14907)

Alex Crichton25fbc445d73a
diff --git a/cranelift/codegen/src/ir/memflags.rs b/cranelift/codegen/src/ir/memflags.rs
index e5880979a1..60ca090d5e 100644
--- a/cranelift/codegen/src/ir/memflags.rs
+++ b/cranelift/codegen/src/ir/memflags.rs
@@ -177,7 +177,7 @@ impl MemFlagsData {
/// Create a set of flags representing an access from a "trusted" address, meaning it's
/// known to be aligned and non-trapping.
pub const fn trusted() -> Self {
- Self::new().with_notrap().with_aligned()
+ Self::new().with_notrap().with_aligned(true)
}
/// Reads the alias region that this memory operation works with.
@@ -208,8 +208,8 @@ impl MemFlagsData {
pub fn set_by_name(&mut self, name: &str) -> Result<bool, &'static str> {
*self = match name {
"notrap" => self.with_trap_code(None),
- "aligned" => self.with_aligned(),
- "readonly" => self.with_readonly(),
+ "aligned" => self.with_aligned(true),
+ "readonly" => self.with_readonly(true),
"little" => {
if self.flags.explicit_endianness() == Some(Endianness::Big) {
return Err("cannot set both big and little endian bits");
@@ -222,7 +222,7 @@ impl MemFlagsData {
}
self.with_endianness(Endianness::Big)
}
- "can_move" => self.with_can_move(),
+ "can_move" => self.with_can_move(true),
other => match TrapCode::from_str(other) {
Ok(code) => self.with_trap_code(Some(code)),
@@ -310,14 +310,14 @@ impl MemFlagsData {
self.flags.can_move()
}
- /// Set the `can_move` flag.
- pub const fn set_can_move(&mut self) {
- *self = self.with_can_move();
+ /// Set or clear the `can_move` flag.
+ pub const fn set_can_move(&mut self, enabled: bool) {
+ *self = self.with_can_move(enabled);
}
- /// Set the `can_move` flag, returning new flags.
- pub const fn with_can_move(mut self) -> Self {
- self.flags = self.flags.with_can_move();
+ /// Set or clear the `can_move` flag, returning new flags.
+ pub const fn with_can_move(mut self, enabled: bool) -> Self {
+ self.flags = self.flags.with_can_move(enabled);
self
}
@@ -330,14 +330,14 @@ impl MemFlagsData {
self.flags.aligned()
}
- /// Set the `aligned` flag.
- pub fn set_aligned(&mut self) {
- *self = self.with_aligned();
+ /// Set or clear the `aligned` flag.
+ pub fn set_aligned(&mut self, enabled: bool) {
+ *self = self.with_aligned(enabled);
}
- /// Set the `aligned` flag, returning new flags.
- pub const fn with_aligned(mut self) -> Self {
- self.flags = self.flags.with_aligned();
+ /// Set or clear the `aligned` flag, returning new flags.
+ pub const fn with_aligned(mut self, enabled: bool) -> Self {
+ self.flags = self.flags.with_aligned(enabled);
self
}
@@ -350,14 +350,14 @@ impl MemFlagsData {
self.flags.readonly()
}
- /// Set the `readonly` flag.
- pub fn set_readonly(&mut self) {
- *self = self.with_readonly();
+ /// Set or clear the `readonly` flag.
+ pub fn set_readonly(&mut self, enabled: bool) {
+ *self = self.with_readonly(enabled);
}
- /// Set the `readonly` flag, returning new flags.
- pub const fn with_readonly(mut self) -> Self {
- self.flags = self.flags.with_readonly();
+ /// Set or clear the `readonly` flag, returning new flags.
+ pub const fn with_readonly(mut self, enabled: bool) -> Self {
+ self.flags = self.flags.with_readonly(enabled);
self
}
/// Get the trap code to report if this memory access traps.
diff --git a/cranelift/codegen/src/machinst/mod.rs b/cranelift/codegen/src/machinst/mod.rs
index 42fab7cd8c..e321045449 100644
--- a/cranelift/codegen/src/machinst/mod.rs
+++ b/cranelift/codegen/src/machinst/mod.rs
@@ -115,15 +115,19 @@ impl MachMemFlags {
/// Create a set of flags representing an access from a "trusted" address.
pub const fn trusted() -> Self {
- Self::new().with_notrap().with_aligned()
+ Self::new().with_notrap().with_aligned(true)
}
const fn read_bit(self, bit: u16) -> bool {
self.bits & bit != 0
}
- const fn with_bit(mut self, bit: u16) -> Self {
- self.bits |= bit;
+ const fn with_bit(mut self, bit: u16, enabled: bool) -> Self {
+ if enabled {
+ self.bits |= bit;
+ } else {
+ self.bits &= !bit;
+ }
self
}
@@ -152,8 +156,8 @@ impl MachMemFlags {
/// Set endianness of the memory access, returning new flags.
pub const fn with_endianness(self, endianness: Endianness) -> Self {
let res = match endianness {
- Endianness::Little => self.with_bit(BIT_LITTLE_ENDIAN),
- Endianness::Big => self.with_bit(BIT_BIG_ENDIAN),
+ Endianness::Little => self.with_bit(BIT_LITTLE_ENDIAN, true),
+ Endianness::Big => self.with_bit(BIT_BIG_ENDIAN, true),
};
assert!(!(res.read_bit(BIT_LITTLE_ENDIAN) && res.read_bit(BIT_BIG_ENDIAN)));
res
@@ -174,9 +178,9 @@ impl MachMemFlags {
self.read_bit(BIT_CAN_MOVE)
}
- /// Set the `can_move` flag, returning new flags.
- pub const fn with_can_move(self) -> Self {
- self.with_bit(BIT_CAN_MOVE)
+ /// Set or clear the `can_move` flag, returning new flags.
+ pub const fn with_can_move(self, enabled: bool) -> Self {
+ self.with_bit(BIT_CAN_MOVE, enabled)
}
/// Test if the `aligned` flag is set.
@@ -184,9 +188,9 @@ impl MachMemFlags {
self.read_bit(BIT_ALIGNED)
}
- /// Set the `aligned` flag, returning new flags.
- pub const fn with_aligned(self) -> Self {
- self.with_bit(BIT_ALIGNED)
+ /// Set or clear the `aligned` flag, returning new flags.
+ pub const fn with_aligned(self, enabled: bool) -> Self {
+ self.with_bit(BIT_ALIGNED, enabled)
}
/// Test if the `readonly` flag is set.
@@ -194,9 +198,9 @@ impl MachMemFlags {
self.read_bit(BIT_READONLY)
}
- /// Set the `readonly` flag, returning new flags.
- pub const fn with_readonly(self) -> Self {
- self.with_bit(BIT_READONLY)
+ /// Set or clear the `readonly` flag, returning new flags.
+ pub const fn with_readonly(self, enabled: bool) -> Self {
+ self.with_bit(BIT_READONLY, enabled)
}
/// Get the trap code to report if this memory access traps.
diff --git a/cranelift/frontend/src/frontend.rs b/cranelift/frontend/src/frontend.rs
index 195de37b8c..5056cedc13 100644
--- a/cranelift/frontend/src/frontend.rs
+++ b/cranelift/frontend/src/frontend.rs
@@ -920,7 +920,7 @@ impl<'a> FunctionBuilder<'a> {
}
if u64::from(src_align) >= access_size && u64::from(dest_align) >= access_size {
- flags.set_aligned();
+ flags.set_aligned(true);
}
// Load all of the memory first. This is necessary in case `dest` overlaps.
@@ -1011,7 +1011,7 @@ impl<'a> FunctionBuilder<'a> {
self.call_memset(config, buffer, ch, size);
} else {
if u64::from(buffer_align) >= access_size {
- flags.set_aligned();
+ flags.set_aligned(true);
}
let ch = u64::from(ch);
@@ -1153,11 +1153,11 @@ impl<'a> FunctionBuilder<'a> {
if let Equal | NotEqual = zero_cc {
let mut left_flags = flags;
if size == left_align.get() as u64 {
- left_flags.set_aligned();
+ left_flags.set_aligned(true);
}
let mut right_flags = flags;
if size == right_align.get() as u64 {
- right_flags.set_aligned();
+ right_flags.set_aligned(true);
}
let left_val = self.ins().load(small_type, left_flags, left, 0);
let right_val = self.ins().load(small_type, right_flags, right, 0);
@@ -1168,7 +1168,7 @@ impl<'a> FunctionBuilder<'a> {
// But for now, just handle single bytes where we don't need to worry.
let mut aligned_flags = flags;
- aligned_flags.set_aligned();
+ aligned_flags.set_aligned(true);
let left_val = self.ins().load(small_type, aligned_flags, left, 0);
let right_val = self.ins().load(small_type, aligned_flags, right, 0);
return self.ins().icmp(int_cc, left_val, right_val);
diff --git a/cranelift/fuzzgen/src/function_generator.rs b/cranelift/fuzzgen/src/function_generator.rs
index 8a8a897da0..ec1bf6f78b 100644
--- a/cranelift/fuzzgen/src/function_generator.rs
+++ b/cranelift/fuzzgen/src/function_generator.rs
@@ -1291,7 +1291,7 @@ where
let mut flags = MemFlagsData::new();
// Even if we picked an aligned address, we can always generate unaligned memflags
if aligned && bool::arbitrary(self.u)? {
- flags.set_aligned();
+ flags.set_aligned(true);
}
// If the address is aligned, then we know it won't trap
if aligned && bool::arbitrary(self.u)? {
diff --git a/crates/cranelift/src/alias_region.rs b/crates/cranelift/src/alias_region.rs
index 2637c18784..31b4c8d0a8 100644
--- a/crates/cranelift/src/alias_region.rs
+++ b/crates/cranelift/src/alias_region.rs
@@ -462,7 +462,7 @@ impl<'a, Offsets> Field<'a, Offsets> {
/// than being a static property of the field; this method allows callers
/// to mark the load as `readonly` in these cases.
pub fn readonly(mut self) -> Self {
- self.flags.set_readonly();
+ self.flags.set_readonly(true);
self
}
@@ -478,7 +478,7 @@ impl<'a, Offsets> Field<'a, Offsets> {
///
/// See the note on [`Field::readonly`].
pub fn can_move(mut self) -> Self {
- self.flags = self.flags.with_can_move();
+ self.flags = self.flags.with_can_move(true);
self
}
@@ -664,8 +664,8 @@ macro_rules! define_vm_type_alias_region_helpers {
// `#[can_move]` markers map to the corresponding `MemFlagsData` builder
// methods; doc comments have no effect on flags; any other attribute is a
// compile error.
- (@apply_attr $flags:expr, [readonly]) => { $flags.with_readonly() };
- (@apply_attr $flags:expr, [can_move]) => { $flags.with_can_move() };
+ (@apply_attr $flags:expr, [readonly]) => { $flags.with_readonly(true) };
+ (@apply_attr $flags:expr, [can_move]) => { $flags.with_can_move(true) };
(@apply_attr $flags:expr, [doc = $d:literal]) => { $flags };
// Emit the accessor method for a single ordinary (scalar) field.
@@ -894,8 +894,8 @@ macro_rules! define_vmctx_alias_region_helpers {
};
// Apply a field attribute to its access flags.
- (@apply_attr $flags:expr, [readonly]) => { $flags.with_readonly() };
- (@apply_attr $flags:expr, [can_move]) => { $flags.with_can_move() };
+ (@apply_attr $flags:expr, [readonly]) => { $flags.with_readonly(true) };
+ (@apply_attr $flags:expr, [can_move]) => { $flags.with_can_move(true) };
(@apply_attr $flags:expr, [access_as = $($t:tt)*]) => { $flags };
(@apply_attr $flags:expr, [ptr_size_offset]) => { $flags };
(@apply_attr $flags:expr, [pointee( $($t:tt)* )]) => { $flags };
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 44d5da3bd6..303d42a905 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -3426,7 +3426,9 @@ impl FuncEnvironment<'_> {
if global_ty.mutability || gc::gc_compiler(self)?.is_moving_collector() {
ir::MemFlagsData::trusted()
} else {
- ir::MemFlagsData::trusted().with_readonly().with_can_move()
+ ir::MemFlagsData::trusted()
+ .with_readonly(true)
+ .with_can_move(true)
};
flags.set_alias_region(Some(region));
gc::gc_compiler(self)?
diff --git a/crates/cranelift/src/func_environ/gc.rs b/crates/cranelift/src/func_environ/gc.rs
index 126291c906..3187bf27b3 100644
--- a/crates/cranelift/src/func_environ/gc.rs
+++ b/crates/cranelift/src/func_environ/gc.rs
@@ -292,7 +292,7 @@ fn emit_gc_kind_assert(
);
let flags = func_env
.gc_memflags(&mut builder.func, GcAccess::HeaderKind)
- .with_readonly();
+ .with_readonly(true);
let kind_and_reserved_bits = builder.ins().load(ir::types::I32, flags, kind_addr, 0);
let kind_mask = builder
.ins()
@@ -1044,7 +1044,7 @@ pub fn translate_array_len(
);
let flags = func_env
.gc_memflags(&mut builder.func, GcAccess::ArrayLength)
- .with_readonly();
+ .with_readonly(true);
let result = builder.ins().load(ir::types::I32, flags, len_field, 0);
log::trace!("translate_array_len(..) -> {result:?}");
Ok(result)
@@ -1376,7 +1376,7 @@ pub fn translate_ref_test(
);
let kind_flags = func_env
.gc_memflags(&mut builder.func, GcAccess::HeaderKind)
- .with_readonly();
+ .with_readonly(true);
let actual_kind = builder.ins().load(ir::types::I32, kind_flags, kind_addr, 0);
let expected_kind = builder
.ins()
@@ -1457,7 +1457,7 @@ pub fn translate_ref_test(
);
let ty_flags = func_env
.gc_memflags(&mut builder.func, GcAccess::HeaderTypeIndex)
- .with_readonly();
+ .with_readonly(true);
let actual_shared_ty = builder.ins().load(ir::types::I32, ty_flags, ty_addr, 0);
func_env.is_subtype(
@@ -1692,8 +1692,8 @@ impl FuncEnvironment<'_> {
.gc_heap_memory_type()
.memory_may_move(&memory_tunables)
{
- flags.set_readonly();
- flags.set_can_move();
+ flags.set_readonly(true);
+ flags.set_can_move(true);
}
let store_context_ptr = self.get_vmstore_context_ptr(builder);
@@ -1737,8 +1737,8 @@ impl FuncEnvironment<'_> {
.gc_heap_memory_type()
.memory_may_move(&memory_tunables)
{
- base_flags.set_readonly();
- base_flags.set_can_move();
+ base_flags.set_readonly(true);
+ base_flags.set_can_move(true);
}
let base = self
.alias_regions
diff --git a/crates/cranelift/src/translate/table.rs b/crates/cranelift/src/translate/table.rs
index b16bd5ea7b..9555709d54 100644
--- a/crates/cranelift/src/translate/table.rs
+++ b/crates/cranelift/src/translate/table.rs
@@ -109,7 +109,7 @@ impl TableData {
let region = env.table_alias_region(pos.func, table_index);
let base_flags = ir::MemFlagsData::new()
- .with_aligned()
+ .with_aligned(true)
.with_alias_region(Some(region));
if spectre_mitigations_enabled {
// Short-circuit the computed table element address to a null pointer