-
Notifications
You must be signed in to change notification settings - Fork 824
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 issue with emscripten memory out of range #698
Merged
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 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 |
---|---|---|
|
@@ -53,15 +53,23 @@ pub fn get_emscripten_memory_size(module: &Module) -> Result<(Pages, Option<Page | |
/// Assumes values start from the end in this order: | ||
/// Last export: Dynamic Base | ||
/// Second-to-Last export: Dynamic top pointer | ||
pub fn get_emscripten_metadata(module: &Module) -> Option<(u32, u32)> { | ||
let max_idx = &module.info().globals.iter().map(|(k, _)| k).max()?; | ||
let snd_max_idx = &module | ||
pub fn get_emscripten_metadata(module: &Module) -> Result<Option<(u32, u32)>, String> { | ||
let max_idx = match module.info().globals.iter().map(|(k, _)| k).max() { | ||
Some(x) => x, | ||
None => return Ok(None), | ||
}; | ||
|
||
let snd_max_idx = match module | ||
.info() | ||
.globals | ||
.iter() | ||
.map(|(k, _)| k) | ||
.filter(|k| k != max_idx) | ||
.max()?; | ||
.filter(|k| *k != max_idx) | ||
.max() | ||
{ | ||
Some(x) => x, | ||
None => return Ok(None), | ||
}; | ||
|
||
use wasmer_runtime_core::types::{GlobalInit, Initializer::Const, Value::I32}; | ||
if let ( | ||
|
@@ -74,15 +82,23 @@ pub fn get_emscripten_metadata(module: &Module) -> Option<(u32, u32)> { | |
.. | ||
}, | ||
) = ( | ||
&module.info().globals[*max_idx], | ||
&module.info().globals[*snd_max_idx], | ||
&module.info().globals[max_idx], | ||
&module.info().globals[snd_max_idx], | ||
) { | ||
Some(( | ||
align_memory(*dynamic_base as u32 - 32), | ||
align_memory(*dynamictop_ptr as u32 - 32), | ||
)) | ||
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. In development profile, there was a subtraction underflow before the index out of bounds error. |
||
let dynamic_base = (*dynamic_base as u32).checked_sub(32).ok_or(format!( | ||
"emscripten unexpected dynamic_base {}", | ||
*dynamic_base as u32 | ||
))?; | ||
let dynamictop_ptr = (*dynamictop_ptr as u32).checked_sub(32).ok_or(format!( | ||
"emscripten unexpected dynamictop_ptr {}", | ||
*dynamictop_ptr as u32 | ||
))?; | ||
Ok(Some(( | ||
align_memory(dynamic_base), | ||
align_memory(dynamictop_ptr), | ||
))) | ||
} else { | ||
None | ||
Ok(None) | ||
} | ||
} | ||
|
||
|
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.
I'm very tired, so I kind of missed this, but the bounds check here is something
WasmPtr
would have done for us and this is kind of new legacy code that we should probably just do right... I suppose I can replace it once this lands on master though