-
Notifications
You must be signed in to change notification settings - Fork 576
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
Unexpected behavior of the instruction memory.init
#3020
Comments
Hi, thanks for spotting this, it really makes confusing, here s is 9, n is 0, and the length of data.data is 9 (note that the data segment index is 2, see below), so Data[3]:
- segment[0] memory=0 size=8 - init i32=8
- 0000008: 0102 0304 0506 0708 ........
- segment[1] memory=0 size=9 - init i32=16
- 0000010: 0102 0304 0506 0708 ff .........
- segment[2] memory=0 size=9 - init i32=32
- 0000020: 0102 0304 0506 0708 ff .........
Code Disassembly:
00011d func[0] <to_test>:
00011e: 01 7f | local[4] type=i32
000120: 01 7d | local[5] type=f32
000122: 01 7e | local[6] type=i64
000124: 01 7c | local[7] type=f64
000126: 41 00 | i32.const 0
000128: 41 09 | i32.const 9
00012a: 41 00 | i32.const 0
00012c: fc 08 02 00 | memory.init 2 0
000130: 0b | end |
Thank you for your nice reply. You are right, |
Hi, I found another possible root cause for the difference. I found the instruction
|
@wenyongh Hi, will WAMR drop the active data section after triggering the start function in the future? |
@erxiaozhou thanks for the detailed investigation, it is really an issue, I submitted PR #3081 and added my understanding in the comments, please help check whether my understanding is correct. The PR makes the wasm file in this issue also throw exception like other runtimes. There may be more scenarios to test, it will be highly appreciated if you can help test them with more cases. |
Thank you for your reply! I will try to test it these days. |
According to the wasm core spec, the checks for the table segments in `table.init` opcode are similar to the checks for `memory.init` opcode: - The size of a passive segment is shrunk to zero after `data.drop` (or `elem.drop`) opcode is executed, and the segment can be used to do `memory.init` (or `table.init`) again - The `memory.init` only traps when `s+n > len(data.data)` or `d+n > len(mem.data)` and `table.init` only traps when `s+n > len(elem.elem)` or `d+n > len(tab.elem)` - The active segment can also be used to do `memory.init` (or `table.init`), while it behaves like a dropped passive segment https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md ``` Segments can also be shrunk to size zero by using the following new instructions: - data.drop: discard the data in an data segment - elem.drop: discard the data in an element segment An active segment is equivalent to a passive segment, but with an implicit memory.init followed by a data.drop (or table.init followed by a elem.drop) that is prepended to the module's start function. ``` ps. https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-memorymathsfmemoryinitx%E2%91%A0 https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-tablemathsftableinitxy%E2%91%A0 #3020
Thanks, as the spec tests pass and this case works, I merged the PR. |
Hi, the bug has not been triggered again in my testing. It looks good. |
…lliance#3081) According to the wasm core spec, the checks for the table segments in `table.init` opcode are similar to the checks for `memory.init` opcode: - The size of a passive segment is shrunk to zero after `data.drop` (or `elem.drop`) opcode is executed, and the segment can be used to do `memory.init` (or `table.init`) again - The `memory.init` only traps when `s+n > len(data.data)` or `d+n > len(mem.data)` and `table.init` only traps when `s+n > len(elem.elem)` or `d+n > len(tab.elem)` - The active segment can also be used to do `memory.init` (or `table.init`), while it behaves like a dropped passive segment https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md ``` Segments can also be shrunk to size zero by using the following new instructions: - data.drop: discard the data in an data segment - elem.drop: discard the data in an element segment An active segment is equivalent to a passive segment, but with an implicit memory.init followed by a data.drop (or table.init followed by a elem.drop) that is prepended to the module's start function. ``` ps. https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-memorymathsfmemoryinitx%E2%91%A0 https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-tablemathsftableinitxy%E2%91%A0 bytecodealliance#3020
Introduction
I executed the test case containing a
memory.init
instruction and found that warm failed to raise an exception as expected.Build commands
I compile the code with commit id 892a94f.
Platform: Ubuntu 20.04
CPU: amd64
Here we take the warm on JIT mode for example. This issue also exists in warm on other modes (classic interpreter mode, fast interpreter mode, fast JIT mode, multi JIT mode).
compile:
Case
all_wamr_memory.init_no_exception2.zip
Actual output:
No exception
Expected output:
There is an exception indicating "out of bounds memory access".
According to line 16 in the specification, there should be an exception because the
d+s
in the test case is 9, and is greater than the length ofdata.data
.https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A4
![image](https://private-user-images.githubusercontent.com/32102519/296829019-593e27bb-8aa8-413d-a85a-6a9536c60493.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2MjM3MDIsIm5iZiI6MTcyMDYyMzQwMiwicGF0aCI6Ii8zMjEwMjUxOS8yOTY4MjkwMTktNTkzZTI3YmItOGFhOC00MTNkLWE4NWEtNmE5NTM2YzYwNDkzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzEwVDE0NTY0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNjYTdhZmZkZjYzODNiNzRjYzBjMWYwOWUxY2Y1MTg0YTE3YTYzMTM5MjU5NjY5MmZhNzRmNzE2NWI2MmI4OWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.UFLdWfYRtAeDv0b8F2ZKKA9dZ8Q6sY5cSjsvAaV7VYQ)
The text was updated successfully, but these errors were encountered: