Skip to content

Commit

Permalink
validate only against heap memory
Browse files Browse the repository at this point in the history
  • Loading branch information
venkkatesh-sekar committed Sep 20, 2024
1 parent 862b7be commit 5e8e27e
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/limit_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,32 @@ fn limit_heap_memory(m: &mut Module, limit: u32) {
// In that case, we don't restrict the heap memory limit as it could
// have undefined behaviour.

if m.data.iter().all(|data| {
let offset = match data.kind {
DataKind::Passive => {
return true;
}
DataKind::Active { memory: _, offset } => {
match offset {
ConstExpr::Value(Value::I32(offset)) => offset as u64,
ConstExpr::Value(Value::I64(offset)) => offset as u64,
_ => {
// It wouldn't pass IC wasm validation
return false;
if m.data
.iter()
.filter_map(|data| {
match data.kind {
DataKind::Passive => None,
DataKind::Active {
memory: data_memory_id,
offset,
} => {
if data_memory_id == memory_id {
match offset {
ConstExpr::Value(Value::I32(offset)) => Some(offset as u64),
ConstExpr::Value(Value::I64(offset)) => Some(offset as u64),
_ => {
// It wouldn't pass IC wasm validation
None
}
}
} else {
None
}
}
}
};
offset < limit * 65536
}) {
})
.all(|offset| offset < limit * 65536)
{
memory.initial = limit;
} else {
eprintln!("Unable to restrict Wasm heap memory to {} pages", limit);
Expand Down

0 comments on commit 5e8e27e

Please sign in to comment.