@@ -33,7 +33,6 @@ use cranelift_wasm::DefinedFuncIndex;
33
33
use std:: cell:: RefCell ;
34
34
use std:: collections:: HashMap ;
35
35
use std:: convert:: TryFrom ;
36
- use std:: mem;
37
36
use std:: rc:: Rc ;
38
37
use wasm_interface:: { HostFunctions , Pointer , WordSize } ;
39
38
use wasmtime_environ:: { Module , translate_signature} ;
@@ -128,15 +127,18 @@ pub fn create_instance<E: Externalities>(ext: &mut E, code: &[u8], heap_pages: u
128
127
fn create_compiled_unit ( code : & [ u8 ] )
129
128
-> std:: result:: Result < ( CompiledModule , Context ) , WasmError >
130
129
{
131
- let isa = target_isa ( ) ?;
132
- let mut context = Context :: with_isa ( isa, CompilationStrategy :: Cranelift ) ;
130
+ let compilation_strategy = CompilationStrategy :: Cranelift ;
131
+
132
+ let compiler = new_compiler ( compilation_strategy) ?;
133
+ let mut context = Context :: new ( Box :: new ( compiler) ) ;
133
134
134
135
// Enable/disable producing of debug info.
135
136
context. set_debug_info ( false ) ;
136
137
137
138
// Instantiate and link the env module.
138
139
let global_exports = context. get_global_exports ( ) ;
139
- let env_module = instantiate_env_module ( global_exports) ?;
140
+ let compiler = new_compiler ( compilation_strategy) ?;
141
+ let env_module = instantiate_env_module ( global_exports, compiler) ?;
140
142
context. name_instance ( "env" . to_owned ( ) , env_module) ;
141
143
142
144
// Compile the wasm module.
@@ -173,15 +175,8 @@ fn call_method(
173
175
grow_memory ( & mut instance, heap_pages) ?;
174
176
175
177
// Initialize the function executor state.
176
- let executor_state = FunctionExecutorState :: new (
177
- // Unsafely extend the reference lifetime to static. This is necessary because the
178
- // host state must be `Any`. This is OK as we will drop this object either before
179
- // exiting the function in the happy case or in the worst case on the next runtime
180
- // call, which also resets the host state. Thus this reference can never actually
181
- // outlive the Context.
182
- unsafe { mem:: transmute :: < _ , & ' static mut Compiler > ( context. compiler_mut ( ) ) } ,
183
- get_heap_base ( & instance) ?,
184
- ) ;
178
+ let heap_base = get_heap_base ( & instance) ?;
179
+ let executor_state = FunctionExecutorState :: new ( heap_base) ;
185
180
reset_env_state ( context, Some ( executor_state) ) ?;
186
181
187
182
// Write the input data into guest memory.
@@ -222,8 +217,10 @@ fn call_method(
222
217
}
223
218
224
219
/// The implementation is based on wasmtime_wasi::instantiate_wasi.
225
- fn instantiate_env_module ( global_exports : Rc < RefCell < HashMap < String , Option < Export > > > > )
226
- -> std:: result:: Result < InstanceHandle , WasmError >
220
+ fn instantiate_env_module (
221
+ global_exports : Rc < RefCell < HashMap < String , Option < Export > > > > ,
222
+ compiler : Compiler ,
223
+ ) -> std:: result:: Result < InstanceHandle , WasmError >
227
224
{
228
225
let isa = target_isa ( ) ?;
229
226
let pointer_type = isa. pointer_type ( ) ;
@@ -260,7 +257,7 @@ fn instantiate_env_module(global_exports: Rc<RefCell<HashMap<String, Option<Expo
260
257
let imports = Imports :: none ( ) ;
261
258
let data_initializers = Vec :: new ( ) ;
262
259
let signatures = PrimaryMap :: new ( ) ;
263
- let env_state = EnvState :: new :: < SubstrateExternals > ( code_memory) ;
260
+ let env_state = EnvState :: new :: < SubstrateExternals > ( code_memory, compiler ) ;
264
261
265
262
let result = InstanceHandle :: new (
266
263
Rc :: new ( module) ,
@@ -283,6 +280,11 @@ fn target_isa() -> std::result::Result<Box<dyn TargetIsa>, WasmError> {
283
280
Ok ( isa_builder. finish ( cranelift_codegen:: settings:: Flags :: new ( flag_builder) ) )
284
281
}
285
282
283
+ fn new_compiler ( strategy : CompilationStrategy ) -> std:: result:: Result < Compiler , WasmError > {
284
+ let isa = target_isa ( ) ?;
285
+ Ok ( Compiler :: new ( isa, strategy) )
286
+ }
287
+
286
288
fn clear_globals ( global_exports : & mut HashMap < String , Option < Export > > ) {
287
289
global_exports. remove ( "memory" ) ;
288
290
global_exports. remove ( "__heap_base" ) ;
0 commit comments