mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-09-06 18:01:08 +00:00
Fix out of bounds issue in is_native_addr_in_shared_heap function (#3886)
When checking for integer overflow, you may often write tests like p + i < p. This works fine if p and i are unsigned integers, since any overflow in the addition will cause the value to simply "wrap around." However, using this pattern when p is a pointer is problematic because pointer overflow has undefined behavior according to the C and C++ standards. If the addition overflows and has an undefined result, the comparison will likewise be undefined; it may produce an unintended result, or may be deleted entirely by an optimizing compiler.
This commit is contained in:
parent
1138435455
commit
c7b2683f17
|
@ -420,15 +420,33 @@ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
|
||||||
uint8 *addr, uint32 bytes)
|
uint8 *addr, uint32 bytes)
|
||||||
{
|
{
|
||||||
WASMSharedHeap *heap = get_shared_heap(module_inst);
|
WASMSharedHeap *heap = get_shared_heap(module_inst);
|
||||||
|
uintptr_t base_addr;
|
||||||
|
uintptr_t addr_int;
|
||||||
|
uintptr_t end_addr;
|
||||||
|
|
||||||
if (heap && addr >= heap->base_addr
|
if (!heap) {
|
||||||
&& addr + bytes <= heap->base_addr + heap->size
|
|
||||||
&& addr + bytes > addr) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
base_addr = (uintptr_t)heap->base_addr;
|
||||||
|
addr_int = (uintptr_t)addr;
|
||||||
|
if (addr_int < base_addr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
end_addr = addr_int + bytes;
|
||||||
|
/* Check for overflow */
|
||||||
|
if (end_addr <= addr_int) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end_addr > base_addr + heap->size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
uint64
|
uint64
|
||||||
wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
|
wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
|
||||||
uint64_t size, void **p_native_addr)
|
uint64_t size, void **p_native_addr)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user