-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cranelift-wasm: Stop using table_addr instructions #8063
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
Merged
jameysharp
merged 1 commit into
bytecodealliance:main
from
jameysharp:stop-using-tables
Mar 13, 2024
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| use cranelift_codegen::ir::{self, condcodes::IntCC, InstBuilder}; | ||
| use cranelift_frontend::FunctionBuilder; | ||
|
|
||
| /// An implementation of a WebAssembly table. | ||
| #[derive(Clone)] | ||
| pub struct TableData { | ||
| /// Global value giving the address of the start of the table. | ||
| pub base_gv: ir::GlobalValue, | ||
|
|
||
| /// Global value giving the current bound of the table, in elements. | ||
| pub bound_gv: ir::GlobalValue, | ||
|
|
||
| /// The size of a table element, in bytes. | ||
| pub element_size: u32, | ||
| } | ||
|
|
||
| impl TableData { | ||
| /// Return a CLIF value containing a native pointer to the beginning of the | ||
| /// given index within this table. | ||
| pub fn prepare_table_addr( | ||
| &self, | ||
| pos: &mut FunctionBuilder, | ||
| mut index: ir::Value, | ||
| addr_ty: ir::Type, | ||
| enable_table_access_spectre_mitigation: bool, | ||
| ) -> ir::Value { | ||
| let index_ty = pos.func.dfg.value_type(index); | ||
|
|
||
| // Start with the bounds check. Trap if `index + 1 > bound`. | ||
| let bound = pos.ins().global_value(index_ty, self.bound_gv); | ||
|
|
||
| // `index > bound - 1` is the same as `index >= bound`. | ||
| let oob = pos | ||
| .ins() | ||
| .icmp(IntCC::UnsignedGreaterThanOrEqual, index, bound); | ||
| pos.ins().trapnz(oob, ir::TrapCode::TableOutOfBounds); | ||
|
|
||
| // If Spectre mitigations are enabled, we will use a comparison to | ||
| // short-circuit the computed table element address to the start | ||
| // of the table on the misspeculation path when out-of-bounds. | ||
| let spectre_oob_cmp = if enable_table_access_spectre_mitigation { | ||
| Some((index, bound)) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| // Convert `index` to `addr_ty`. | ||
| if index_ty != addr_ty { | ||
| index = pos.ins().uextend(addr_ty, index); | ||
| } | ||
|
|
||
| // Add the table base address base | ||
| let base = pos.ins().global_value(addr_ty, self.base_gv); | ||
|
|
||
| let element_size = self.element_size; | ||
| let offset = if element_size == 1 { | ||
| index | ||
| } else if element_size.is_power_of_two() { | ||
| pos.ins() | ||
| .ishl_imm(index, i64::from(element_size.trailing_zeros())) | ||
| } else { | ||
| pos.ins().imul_imm(index, element_size as i64) | ||
| }; | ||
|
|
||
| let element_addr = pos.ins().iadd(base, offset); | ||
|
|
||
| if let Some((index, bound)) = spectre_oob_cmp { | ||
| let cond = pos | ||
| .ins() | ||
| .icmp(IntCC::UnsignedGreaterThanOrEqual, index, bound); | ||
| // If out-of-bounds, choose the table base on the misspeculation path. | ||
| pos.ins().select_spectre_guard(cond, base, element_addr) | ||
| } else { | ||
| element_addr | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Not 100% convinced it is worth adding the power-of-2 optimization here, since our mid-end rules should definitely already handle this. I think this file should focus on just (a) the correctness of the bounds checks and (b) optimizations to the bounds checks that rely on knowledge of the Wasm table and its limits that the mid-end can't possibly know.
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.
And apologies if tihs already existed. I think we can clean up a tiny bit of complexity here, if that was the case.
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.
Yeah, I considered removing that in favor of relying on the corresponding egraph optimizations, but decided to keep it since it was in the original. I'd prefer to land it in this form to avoid bigger changes to the filetests, where I specifically have not turned on the egraph pass so it's more clear what's being generated, but I wouldn't be upset about removing this later.