-
Notifications
You must be signed in to change notification settings - Fork 710
Fix potential overflow in memory size calculation #4549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to make the same modification for aot_runtime.c
too. And if this overflow can happen only when the SHRUNK_MEMORY is enabled, maybe consider to add the macro around the condition.
resolved. refers to:https://github.com/bytecodealliance/wasm-micro-runtime/blob/6b51c61f5e50573f672916a7df953a2704b823f4/core/iwasm/interpreter/wasm_loader.c#L6559C1-L6560C61. this check happened when MULTI_MODULE is disabled or SHRUNK_MEMORY enabled |
Signed-off-by: zhenweijin <[email protected]>
IIUC, this reveals a more common question: which is better, pre-additional-check or post-additional-check to fix a potential integer overflow? A pre-additional-check involves using a check to prevent integer overflow from the very beginning. A post-additional-check involves using a check after addition to see if there is an overflow. Clearly, UBSan strongly prefers the pre-additional-check, and I believe GCC compilation warnings do the same. The issue is that post-additional-check is widely used in source code. Should we conduct a comprehensive check to replace post-additional-check with pre-additional-check? Should this approach be extended to all four integer arithmetic operations? Please let me know if I am overthinking this. |
for C/C++ spec, unsigned integer overflow is well defined, and overflow of signed integers invokes undefined behavior. so the above code will perform well in GCC compiler, So overflow of signed integers pre-additional-check should be better, and for overflow of unsigned integers, UBSan suggests it's not the intended value. |
FYI: from https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
|
After resizing memory size,
num_bytes_per_page
might be greater thanDEFAULT_NUM_BYTES_PER_PAGE
,hence check for integer overflow before
num_bytes_per_page += heap_size
.Here is the detail error log: