Clip OOB values in call data load and copy (#448)

The OOB logic is implemented in the runtime but truncation circumvented
it.

Closes #263

---------

Signed-off-by: xermicus <cyrill@parity.io>
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
xermicus authored and GitHub committed 2026-01-13 10:15:31 +01:00
1 parent c1ce984ca2
commit a1a4f8e6ea
5 files changed
+68 -12

No files matched your search

@@ -1054,6 +1054,28 @@ impl<'ctx> Context<'ctx> {
.into_int_value())
}
/// Clip a memory offset to the maximum value that fits into a register.
pub fn clip_to_xlen(
&self,
value: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::IntValue<'ctx>> {
let clipped = self.xlen_type().const_all_ones();
let is_overflow = self.builder().build_int_compare(
inkwell::IntPredicate::UGT,
value,
self.builder()
.build_int_z_extend(clipped, self.word_type(), "value_clipped")?,
"is_value_overflow",
)?;
let truncated =
self.builder()
.build_int_truncate(value, self.xlen_type(), "value_truncated")?;
Ok(self
.builder()
.build_select(is_overflow, clipped, truncated, "value")?
.into_int_value())
}
/// Build a call to PolkaVM `sbrk` for extending the heap from offset by `size`.
/// The allocation is aligned to 32 bytes.
///