From dc541497b23029da4053f80dd3d28f555e5b358d Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Fri, 24 Feb 2023 21:54:04 -0800 Subject: [PATCH] fix overflow --- src/interp.rs | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/interp.rs b/src/interp.rs index b09e689..ab87077 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -946,7 +946,7 @@ pub fn const_eval( (Operator::Unreachable, []) => None, (Operator::I32Load { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -956,7 +956,7 @@ pub fn const_eval( ))) }), (Operator::I64Load { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(8)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -966,7 +966,7 @@ pub fn const_eval( ))) }), (Operator::F32Load { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -976,7 +976,7 @@ pub fn const_eval( ))) }), (Operator::F64Load { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(8)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -986,7 +986,7 @@ pub fn const_eval( ))) }), (Operator::I32Load8S { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(1)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -995,7 +995,7 @@ pub fn const_eval( )) }), (Operator::I32Load8U { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1004,7 +1004,7 @@ pub fn const_eval( )) }), (Operator::I32Load16S { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1013,7 +1013,7 @@ pub fn const_eval( )) }), (Operator::I32Load16U { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1022,7 +1022,7 @@ pub fn const_eval( )) }), (Operator::I64Load8S { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(1)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1031,7 +1031,7 @@ pub fn const_eval( )) }), (Operator::I64Load8U { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(1)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1040,7 +1040,7 @@ pub fn const_eval( )) }), (Operator::I64Load16S { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1049,7 +1049,7 @@ pub fn const_eval( )) }), (Operator::I64Load16U { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1058,7 +1058,7 @@ pub fn const_eval( )) }), (Operator::I64Load32S { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1067,7 +1067,7 @@ pub fn const_eval( )) }), (Operator::I64Load32U { memory }, [ConstVal::I32(addr)]) => ctx.and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1077,7 +1077,7 @@ pub fn const_eval( }), (Operator::I32Store { memory }, [ConstVal::I32(addr), ConstVal::I32(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1086,7 +1086,7 @@ pub fn const_eval( }), (Operator::I64Store { memory }, [ConstVal::I32(addr), ConstVal::I64(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(8)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1095,7 +1095,7 @@ pub fn const_eval( }), (Operator::I32Store8 { memory }, [ConstVal::I32(addr), ConstVal::I32(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(1)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1104,7 +1104,7 @@ pub fn const_eval( }), (Operator::I32Store16 { memory }, [ConstVal::I32(addr), ConstVal::I32(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1113,7 +1113,7 @@ pub fn const_eval( }), (Operator::I64Store8 { memory }, [ConstVal::I32(addr), ConstVal::I64(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(1)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1122,7 +1122,7 @@ pub fn const_eval( }), (Operator::I64Store16 { memory }, [ConstVal::I32(addr), ConstVal::I64(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(2)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1131,7 +1131,7 @@ pub fn const_eval( }), (Operator::I64Store32 { memory }, [ConstVal::I32(addr), ConstVal::I64(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1140,7 +1140,7 @@ pub fn const_eval( }), (Operator::F32Store { memory }, [ConstVal::I32(addr), ConstVal::F32(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(4)? > global.memories[memory.memory].data.len() as u32 { return None; } @@ -1149,7 +1149,7 @@ pub fn const_eval( }), (Operator::F64Store { memory }, [ConstVal::I32(addr), ConstVal::F64(data)]) => ctx .and_then(|global| { - let addr = *addr + memory.offset; + let addr = addr.checked_add(memory.offset)?; if addr.checked_add(8)? > global.memories[memory.memory].data.len() as u32 { return None; }