-
Notifications
You must be signed in to change notification settings - Fork 354
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
boxed the wasmer_instance field of Instance to make it safe to reference #306
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
97f828b
Add wasmer_instance to ContextData
webmaster128 4383235
Add with_func_from_context
webmaster128 3758c42
Try calling allocate from the do_read_db implementation
webmaster128 cd0b1b8
boxed the wasmer_instance field of Instance to make it safe to reference
reuvenpo 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use std::marker::PhantomData; | ||
use std::ptr::NonNull; | ||
|
||
use snafu::ResultExt; | ||
pub use wasmer_runtime_core::typed_func::Func; | ||
|
@@ -14,7 +15,8 @@ use cosmwasm_std::{Api, Extern, Querier, Storage}; | |
use crate::backends::{compile, get_gas, set_gas}; | ||
use crate::context::{ | ||
do_canonicalize_address, do_humanize_address, do_query_chain, do_read, do_remove, do_write, | ||
move_from_context, move_into_context, setup_context, with_storage_from_context, | ||
move_from_context, move_into_context, set_wasmer_instance, setup_context, | ||
with_storage_from_context, | ||
}; | ||
#[cfg(feature = "iterator")] | ||
use crate::context::{do_next, do_scan}; | ||
|
@@ -23,7 +25,7 @@ use crate::errors::{ResolveErr, Result, WasmerErr, WasmerRuntimeErr}; | |
use crate::memory::{read_region, write_region}; | ||
|
||
pub struct Instance<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static> { | ||
wasmer_instance: wasmer_runtime_core::instance::Instance, | ||
wasmer_instance: Box<wasmer_runtime_core::instance::Instance>, | ||
pub api: A, | ||
// This does not store data but only fixes type information | ||
type_storage: PhantomData<S>, | ||
|
@@ -53,8 +55,8 @@ where | |
// Returns 0 on success. Returns negative value on error. An incomplete list of error codes is: | ||
// value region too small: -1000002 | ||
// Ownership of both input and output pointer is not transferred to the host. | ||
"db_read" => Func::new(move |ctx: &mut Ctx, key_ptr: u32, value_ptr: u32| -> i32 { | ||
do_read::<S, Q>(ctx, key_ptr, value_ptr) | ||
"db_read" => Func::new(move |ctx: &mut Ctx, key_ptr: u32| -> i32 { | ||
do_read::<S, Q>(ctx, key_ptr) | ||
}), | ||
// Writes the given value into the database entry at the given key. | ||
// Ownership of both input and output pointer is not transferred to the host. | ||
|
@@ -119,6 +121,15 @@ where | |
gas_limit: u64, | ||
) -> Self { | ||
set_gas(&mut wasmer_instance, gas_limit); | ||
|
||
// The pointer shenanigans below are sound because: | ||
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. Seems like a safe way to get a reference, but not tested with actually calling it. 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. true. still requires work, of course |
||
// 1. Boxing the wasmer instance gives it a constant address, | ||
// 2. We never move out of this Box before we finish using this `Instance` | ||
// 3. We provide the context a mut pointer to the wasmer context, | ||
// and we only use it when we actually have unique access to it. | ||
let mut wasmer_instance = Box::new(wasmer_instance); | ||
let instance_ptr = NonNull::from(&mut *wasmer_instance); | ||
set_wasmer_instance::<S, Q>(wasmer_instance.context(), instance_ptr); | ||
move_from_context(wasmer_instance.context(), deps.storage, deps.querier); | ||
Instance { | ||
wasmer_instance, | ||
|
@@ -142,7 +153,7 @@ where | |
} else { | ||
None | ||
}; | ||
(instance.wasmer_instance, ext) | ||
(*instance.wasmer_instance, ext) | ||
} | ||
|
||
/// Returns the currently remaining gas | ||
|
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.
why did you remove this?
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.
The imports under it are needed in your PR, but they are not available without this feature enabled. this caused a compilation fail when not using this feature.