-
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
Fix fast-interp "pre-compiled label offset out of range" issue #2659
Merged
wenyongh
merged 2 commits into
bytecodealliance:main
from
wenyongh:dev/fix_labels_as_values
Oct 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5389,21 +5389,27 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, | |
LOG_OP("\ndelete last op\n"); \ | ||
} while (0) | ||
#else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ | ||
#if UINTPTR_MAX == UINT64_MAX | ||
#define emit_label(opcode) \ | ||
do { \ | ||
int32 offset = \ | ||
(int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \ | ||
if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ | ||
set_error_buf(error_buf, error_buf_size, \ | ||
"pre-compiled label offset out of range"); \ | ||
goto fail; \ | ||
} \ | ||
wasm_loader_emit_int16(loader_ctx, offset); \ | ||
/* emit int32 relative offset in 64-bit target */ \ | ||
wasm_loader_emit_uint32(loader_ctx, offset); \ | ||
LOG_OP("\nemit_op [%02x]\t", opcode); \ | ||
} while (0) | ||
#else | ||
#define emit_label(opcode) \ | ||
do { \ | ||
uint32 offset = (uint32)(uintptr_t)handle_table[opcode]; \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. had better rename offset to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks. |
||
/* emit uint32 label address in 32-bit target */ \ | ||
wasm_loader_emit_uint32(loader_ctx, offset); \ | ||
LOG_OP("\nemit_op [%02x]\t", opcode); \ | ||
} while (0) | ||
#endif | ||
#define skip_label() \ | ||
do { \ | ||
wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \ | ||
wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \ | ||
LOG_OP("\ndelete last op\n"); \ | ||
} while (0) | ||
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ | ||
|
@@ -5733,12 +5739,6 @@ preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode, | |
(void)error_buf; | ||
(void)error_buf_size; | ||
return true; | ||
#if WASM_ENABLE_LABELS_AS_VALUES != 0 | ||
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 | ||
fail: | ||
return false; | ||
#endif | ||
#endif | ||
} | ||
|
||
static bool | ||
|
@@ -7793,6 +7793,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, | |
uint8 ref_type; | ||
BranchBlock *cur_block = loader_ctx->frame_csp - 1; | ||
int32 available_stack_cell; | ||
#if WASM_ENABLE_FAST_INTERP != 0 | ||
uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled; | ||
#endif | ||
|
||
POP_I32(); | ||
|
||
|
@@ -7821,26 +7824,26 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, | |
#if WASM_ENABLE_FAST_INTERP != 0 | ||
if (loader_ctx->p_code_compiled) { | ||
uint8 opcode_tmp = WASM_OP_SELECT_64; | ||
uint8 *p_code_compiled_tmp = | ||
loader_ctx->p_code_compiled - 2; | ||
#if WASM_ENABLE_LABELS_AS_VALUES != 0 | ||
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 | ||
*(void **)(p_code_compiled_tmp | ||
- sizeof(void *)) = | ||
handle_table[opcode_tmp]; | ||
#else | ||
#if UINTPTR_MAX == UINT64_MAX | ||
/* emit int32 relative offset in 64-bit target | ||
*/ | ||
int32 offset = | ||
(int32)((uint8 *)handle_table[opcode_tmp] | ||
- (uint8 *)handle_table[0]); | ||
if (!(offset >= INT16_MIN | ||
&& offset < INT16_MAX)) { | ||
set_error_buf(error_buf, error_buf_size, | ||
"pre-compiled label offset " | ||
"out of range"); | ||
goto fail; | ||
} | ||
*(int16 *)(p_code_compiled_tmp | ||
- sizeof(int16)) = (int16)offset; | ||
*(int32 *)(p_code_compiled_tmp | ||
- sizeof(int32)) = offset; | ||
#else | ||
/* emit uint32 label address in 32-bit target */ | ||
*(uint32 *)(p_code_compiled_tmp | ||
- sizeof(uint32)) = | ||
(uint32)(uintptr_t)handle_table[opcode_tmp]; | ||
#endif | ||
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ | ||
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ | ||
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 | ||
|
@@ -7934,16 +7937,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, | |
*(void **)(p_code_compiled_tmp - sizeof(void *)) = | ||
handle_table[opcode_tmp]; | ||
#else | ||
#if UINTPTR_MAX == UINT64_MAX | ||
/* emit int32 relative offset in 64-bit target */ | ||
int32 offset = (int32)((uint8 *)handle_table[opcode_tmp] | ||
- (uint8 *)handle_table[0]); | ||
if (!(offset >= INT16_MIN && offset < INT16_MAX)) { | ||
set_error_buf( | ||
error_buf, error_buf_size, | ||
"pre-compiled label offset out of range"); | ||
goto fail; | ||
} | ||
*(int16 *)(p_code_compiled_tmp - sizeof(int16)) = | ||
(int16)offset; | ||
*(int32 *)(p_code_compiled_tmp - sizeof(int32)) = | ||
offset; | ||
#else | ||
/* emit uint32 label address in 32-bit target */ | ||
*(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) = | ||
(uint32)(uintptr_t)handle_table[opcode_tmp]; | ||
#endif | ||
#endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */ | ||
#else /* else of WASM_ENABLE_LABELS_AS_VALUES */ | ||
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should
& 3
?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.
No, the frame_ip must be 2-byte aligned, but not necessarily 4-byte aligned. If we force the frame_ip to be 4-byte aligned in wasm_loader, we need to add padding byte if needed, and here, we need to add code like
frame_ip = align_uint(frame_ip, 4)
orif (frame_ip & 2) frame_ip += 2
, I had a try, it impacts performance a lot.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.
Got it, Thanks!
I originally thought the CPU requires 4-bytes align, if 2-bytes align is OK, then no problem here.