-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathinstance.rs
More file actions
696 lines (618 loc) · 26.8 KB
/
instance.rs
File metadata and controls
696 lines (618 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use std::collections::BTreeMap;
use std::time::Instant;
use anyhow::Error;
use graph::futures03::FutureExt as _;
use graph::futures03::future::BoxFuture;
use graph::slog::SendSyncRefUnwindSafeKV;
use semver::Version;
use wasmtime::{AsContextMut, Linker, Store, Trap};
use graph::blockchain::{Blockchain, HostFnCtx};
use graph::data::store;
use graph::data::subgraph::schema::SubgraphError;
use graph::data_source::{MappingTrigger, TriggerWithHandler};
use graph::prelude::*;
use graph::runtime::{
HostExportError, asc_new,
gas::{Gas, GasCounter},
};
use graph::{components::subgraph::MappingError, runtime::AscPtr};
use super::IntoWasmRet;
use super::{IntoTrap, WasmInstanceContext};
use crate::ExperimentalFeatures;
use crate::error::DeterminismLevel;
use crate::mapping::MappingContext;
use crate::mapping::ValidModule;
use crate::module::WasmInstanceData;
use super::{AscHeapCtx, ToAscPtr, is_trap_deterministic};
/// Handle to a WASM instance, which is terminated if and only if this is dropped.
pub struct WasmInstance {
pub instance: wasmtime::Instance,
pub store: wasmtime::Store<WasmInstanceData>,
// A reference to the gas counter used for reporting the gas used.
pub gas: GasCounter,
}
#[cfg(debug_assertions)]
mod impl_for_tests {
use graph::runtime::{
AscIndexId, AscPtr, AscType, DeterministicHostError, FromAscObj, HostExportError, ToAscObj,
asc_new,
};
use crate::module::{WasmInstanceContext, asc_get};
impl super::WasmInstance {
pub fn asc_get<T, P>(&mut self, asc_ptr: AscPtr<P>) -> Result<T, DeterministicHostError>
where
P: AscType + AscIndexId,
T: FromAscObj<P>,
{
let ctx = WasmInstanceContext::new(&mut self.store);
asc_get(&ctx, asc_ptr, &self.gas)
}
pub async fn asc_new<P, T>(&mut self, rust_obj: &T) -> Result<AscPtr<P>, HostExportError>
where
P: AscType + AscIndexId,
T: ToAscObj<P> + ?Sized,
{
let mut ctx = WasmInstanceContext::new(&mut self.store);
asc_new(&mut ctx, rust_obj, &self.gas).await
}
}
}
impl WasmInstance {
pub(crate) async fn handle_json_callback(
mut self,
handler_name: &str,
value: &serde_json::Value,
user_data: &store::Value,
) -> Result<BlockState, anyhow::Error> {
let gas_metrics = self.store.data().host_metrics.gas_metrics.clone();
let gas = GasCounter::new(gas_metrics);
let mut ctx = self.instance_ctx();
let (value, user_data) = {
let value = asc_new(&mut ctx, value, &gas).await;
let user_data = asc_new(&mut ctx, user_data, &gas).await;
(value, user_data)
};
self.instance_ctx().as_mut().ctx.state.enter_handler();
// Invoke the callback
self.instance
.get_func(self.store.as_context_mut(), handler_name)
.with_context(|| format!("function {} not found", handler_name))?
.typed::<(u32, u32), ()>(self.store.as_context_mut())?
.call_async(
self.store.as_context_mut(),
(value?.wasm_ptr(), user_data?.wasm_ptr()),
)
.await
.with_context(|| format!("Failed to handle callback '{}'", handler_name))?;
let mut wasm_ctx = self.store.into_data();
wasm_ctx.ctx.state.exit_handler();
Ok(wasm_ctx.take_state())
}
pub(crate) async fn handle_trigger<C: Blockchain>(
mut self,
trigger: TriggerWithHandler<MappingTrigger<C>>,
) -> Result<(BlockState, Gas), MappingError>
where
<C as Blockchain>::MappingTrigger: ToAscPtr,
{
let handler_name = trigger.handler_name().to_owned();
let gas = self.gas.clone();
let logging_extras = trigger.logging_extras().cheap_clone();
let error_context = trigger.trigger.error_context();
let mut ctx = self.instance_ctx();
let asc_trigger = trigger.to_asc_ptr(&mut ctx, &gas).await?;
self.invoke_handler(&handler_name, asc_trigger, logging_extras, error_context)
.await
}
pub fn take_ctx(self) -> WasmInstanceData {
self.store.into_data()
}
pub(crate) fn instance_ctx(&mut self) -> WasmInstanceContext<'_> {
WasmInstanceContext::new(&mut self.store)
}
#[cfg(debug_assertions)]
pub fn get_func(&mut self, func_name: &str) -> wasmtime::Func {
self.instance
.get_func(self.store.as_context_mut(), func_name)
.unwrap()
}
#[cfg(debug_assertions)]
pub fn gas_used(&self) -> u64 {
self.gas.get().value()
}
async fn invoke_handler<T>(
mut self,
handler: &str,
arg: AscPtr<T>,
logging_extras: Arc<dyn SendSyncRefUnwindSafeKV>,
error_context: Option<String>,
) -> Result<(BlockState, Gas), MappingError> {
let func = self
.instance
.get_func(self.store.as_context_mut(), handler)
.with_context(|| format!("function {} not found", handler))?;
let func = func
.typed(self.store.as_context_mut())
.context("wasm function has incorrect signature")?;
// Caution: Make sure all exit paths from this function call `exit_handler`.
self.instance_ctx().as_mut().ctx.state.enter_handler();
// This `match` will return early if there was a non-deterministic trap.
let deterministic_error: Option<Error> = match func
.call_async(self.store.as_context_mut(), arg.wasm_ptr())
.await
{
Ok(()) => {
assert!(!self.instance_ctx().as_ref().possible_reorg);
assert!(!self.instance_ctx().as_ref().deterministic_host_trap);
None
}
Err(trap) if self.instance_ctx().as_ref().possible_reorg => {
self.instance_ctx().as_mut().ctx.state.exit_handler();
return Err(MappingError::PossibleReorg(trap));
}
// Treat timeouts anywhere in the error chain as a special case to have a better error
// message. Any `TrapCode::Interrupt` is assumed to be a timeout.
// See also: runtime-timeouts
Err(trap)
if trap
.chain()
.any(|e| e.downcast_ref::<Trap>() == Some(&Trap::Interrupt)) =>
{
self.instance_ctx().as_mut().ctx.state.exit_handler();
return Err(MappingError::Unknown(trap.context(format!(
"Handler '{}' hit the timeout of '{}' seconds",
handler,
self.instance_ctx().as_ref().valid_module.timeout.unwrap().as_secs()
))));
}
Err(trap) => {
let trap_is_deterministic = is_trap_deterministic(&trap)
|| self.instance_ctx().as_ref().deterministic_host_trap;
match trap_is_deterministic {
true => Some(trap),
false => {
self.instance_ctx().as_mut().ctx.state.exit_handler();
return Err(MappingError::Unknown(trap));
}
}
}
};
if let Some(deterministic_error) = deterministic_error {
let deterministic_error = match error_context {
Some(error_context) => deterministic_error.context(error_context),
None => deterministic_error,
};
let message = format!("{:#}", deterministic_error).replace('\n', "\t");
// Log the error and restore the updates snapshot, effectively reverting the handler.
error!(&self.instance_ctx().as_ref().ctx.logger,
"Handler skipped due to execution failure";
"handler" => handler,
"error" => &message,
logging_extras
);
let subgraph_error = SubgraphError {
subgraph_id: self
.instance_ctx()
.as_ref()
.ctx
.host_exports
.subgraph_id
.clone(),
message,
block_ptr: Some(self.instance_ctx().as_ref().ctx.block_ptr.cheap_clone()),
handler: Some(handler.to_string()),
deterministic: true,
};
self.instance_ctx()
.as_mut()
.ctx
.state
.exit_handler_and_discard_changes_due_to_error(subgraph_error);
} else {
self.instance_ctx().as_mut().ctx.state.exit_handler();
}
let gas = self.gas.get();
Ok((self.take_ctx().take_state(), gas))
}
}
/// Register a chain-specific host function dispatcher for the given import name.
/// The registered closure looks up the `HostFn` by name from `caller.data().ctx.host_fns`
/// at call time. If the module doesn't import this function, this is a no-op.
fn link_chain_host_fn(
linker: &mut Linker<WasmInstanceData>,
import_name_to_modules: &BTreeMap<String, Vec<String>>,
name: &'static str,
) -> Result<(), anyhow::Error> {
let modules = match import_name_to_modules.get(name) {
Some(m) => m,
None => return Ok(()),
};
let name_for_metrics = name.replace('.', "_");
let section_name = format!("host_export_{}", name_for_metrics);
for module in modules {
let name_for_metrics = name_for_metrics.clone();
let section_name = section_name.clone();
linker.func_wrap_async(
module,
name,
move |mut caller: wasmtime::Caller<'_, WasmInstanceData>, (call_ptr,): (u32,)| {
let name_for_metrics = name_for_metrics.clone();
let section_name = section_name.clone();
Box::new(async move {
let host_fn = caller
.data()
.ctx
.host_fns
.iter()
.find(|hf| hf.name == name)
.ok_or_else(|| {
anyhow::anyhow!(
"chain host function '{}' is not available for this chain",
name
)
})?
.cheap_clone();
let start = Instant::now();
let gas = caller.data().gas.cheap_clone();
let host_metrics = caller.data().host_metrics.cheap_clone();
let stopwatch = host_metrics.stopwatch.cheap_clone();
let _section = stopwatch.start_section(§ion_name);
let ctx = HostFnCtx {
logger: caller.data().ctx.logger.cheap_clone(),
block_ptr: caller.data().ctx.block_ptr.cheap_clone(),
gas: gas.cheap_clone(),
metrics: host_metrics.cheap_clone(),
heap: &mut WasmInstanceContext::new(&mut caller),
};
let ret = (host_fn.func)(ctx, call_ptr).await.map_err(|e| match e {
HostExportError::Deterministic(e) => {
caller.data_mut().deterministic_host_trap = true;
e
}
HostExportError::PossibleReorg(e) => {
caller.data_mut().possible_reorg = true;
e
}
HostExportError::Unknown(e) => e,
})?;
host_metrics.observe_host_fn_execution_time(
start.elapsed().as_secs_f64(),
&name_for_metrics,
);
Ok(ret)
})
},
)?;
}
Ok(())
}
/// Build a pre-linked `Linker` for a WASM module. This linker can be reused across triggers by
/// calling `linker.instantiate_pre()` once and then `instance_pre.instantiate_async()` per trigger.
///
/// All host functions (builtins + chain-specific) are registered here. Chain-specific host functions
/// are dispatched generically by looking up the `HostFn` by name from `caller.data().ctx.host_fns`
/// at call time rather than capturing concrete closures at link time.
pub(crate) fn build_linker(
engine: &wasmtime::Engine,
import_name_to_modules: &BTreeMap<String, Vec<String>>,
) -> Result<Linker<WasmInstanceData>, anyhow::Error> {
let mut linker: Linker<WasmInstanceData> = wasmtime::Linker::new(engine);
// Helper to turn a parameter name into 'u32' for a tuple type
// (param1, parma2, ..) : (u32, u32, ..)
macro_rules! param_u32 {
($param:ident) => {
u32
};
}
// The difficulty with this macro is that it needs to turn a list of
// parameter names into a tuple declaration (param1, parma2, ..) :
// (u32, u32, ..), but also for an empty parameter list, it needs to
// produce '(): ()'. In the first case we need a trailing comma, in
// the second case we don't. That's why there are two separate
// expansions, one with and one without params
macro_rules! link {
($wasm_name:expr, $rust_name:ident, $($param:ident),*) => {
link!($wasm_name, $rust_name, "host_export_other",$($param),*)
};
($wasm_name:expr, $rust_name:ident, $section:expr, $($param:ident),+) => {
let modules = import_name_to_modules
.get($wasm_name)
.into_iter()
.flatten();
// link an import with all the modules that require it.
for module in modules {
linker.func_wrap_async(
module,
$wasm_name,
move |mut caller: wasmtime::Caller<'_, WasmInstanceData>,
($($param),*,) : ($(param_u32!($param)),*,)| {
Box::new(async move {
let gas = caller.data().gas.cheap_clone();
let host_metrics = caller.data().host_metrics.cheap_clone();
let _section = host_metrics.stopwatch.start_section($section);
#[allow(unused_mut)]
let mut ctx = std::pin::pin!(WasmInstanceContext::new(&mut caller));
let result = ctx.$rust_name(
&gas,
$($param.into()),*
).await;
let ctx = ctx.get_mut();
match result {
Ok(result) => Ok(result.into_wasm_ret()),
Err(e) => {
match IntoTrap::determinism_level(&e) {
DeterminismLevel::Deterministic => {
ctx.as_mut().deterministic_host_trap = true;
}
DeterminismLevel::PossibleReorg => {
ctx.as_mut().possible_reorg = true;
}
DeterminismLevel::Unimplemented
| DeterminismLevel::NonDeterministic => {}
}
Err(e.into())
}
}
}) },
)?;
}
};
($wasm_name:expr, $rust_name:ident, $section:expr,) => {
let modules = import_name_to_modules
.get($wasm_name)
.into_iter()
.flatten();
// link an import with all the modules that require it.
for module in modules {
linker.func_wrap_async(
module,
$wasm_name,
move |mut caller: wasmtime::Caller<'_, WasmInstanceData>,
_ : ()| {
Box::new(async move {
let gas = caller.data().gas.cheap_clone();
let host_metrics = caller.data().host_metrics.cheap_clone();
let _section = host_metrics.stopwatch.start_section($section);
#[allow(unused_mut)]
let mut ctx = WasmInstanceContext::new(&mut caller);
let result = ctx.$rust_name(&gas).await;
match result {
Ok(result) => Ok(result.into_wasm_ret()),
Err(e) => {
match IntoTrap::determinism_level(&e) {
DeterminismLevel::Deterministic => {
ctx.as_mut().deterministic_host_trap = true;
}
DeterminismLevel::PossibleReorg => {
ctx.as_mut().possible_reorg = true;
}
DeterminismLevel::Unimplemented
| DeterminismLevel::NonDeterministic => {}
}
Err(e.into())
}
}
}) },
)?;
}
};
}
// Chain-specific host functions. Each is registered explicitly rather than
// discovered dynamically from imports.
link_chain_host_fn(&mut linker, import_name_to_modules, "ethereum.call")?;
link_chain_host_fn(&mut linker, import_name_to_modules, "ethereum.getBalance")?;
link_chain_host_fn(&mut linker, import_name_to_modules, "ethereum.hasCode")?;
link!("ethereum.encode", ethereum_encode, params_ptr);
link!("ethereum.decode", ethereum_decode, params_ptr, data_ptr);
link!("abort", abort, message_ptr, file_name_ptr, line, column);
link!("store.get", store_get, "host_export_store_get", entity, id);
link!(
"store.loadRelated",
store_load_related,
"host_export_store_load_related",
entity,
id,
field
);
link!(
"store.get_in_block",
store_get_in_block,
"host_export_store_get_in_block",
entity,
id
);
link!(
"store.set",
store_set,
"host_export_store_set",
entity,
id,
data
);
// All IPFS-related functions exported by the host WASM runtime should be listed in the
// graph::data::subgraph::features::IPFS_ON_ETHEREUM_CONTRACTS_FUNCTION_NAMES array for
// automatic feature detection to work.
//
// For reference, search this codebase for: ff652476-e6ad-40e4-85b8-e815d6c6e5e2
link!("ipfs.cat", ipfs_cat, "host_export_ipfs_cat", hash_ptr);
link!(
"ipfs.map",
ipfs_map,
"host_export_ipfs_map",
link_ptr,
callback,
user_data,
flags
);
// ipfs.getBlock checks the experimental_features flag internally via
// caller.data().experimental_features, so it can be linked unconditionally.
link!(
"ipfs.getBlock",
ipfs_get_block,
"host_export_ipfs_get_block",
hash_ptr
);
link!("store.remove", store_remove, entity_ptr, id_ptr);
link!("typeConversion.bytesToString", bytes_to_string, ptr);
link!("typeConversion.bytesToHex", bytes_to_hex, ptr);
link!("typeConversion.bigIntToString", big_int_to_string, ptr);
link!("typeConversion.bigIntToHex", big_int_to_hex, ptr);
link!("typeConversion.stringToH160", string_to_h160, ptr);
link!("typeConversion.bytesToBase58", bytes_to_base58, ptr);
link!("json.fromBytes", json_from_bytes, ptr);
link!("json.try_fromBytes", json_try_from_bytes, ptr);
link!("json.toI64", json_to_i64, ptr);
link!("json.toU64", json_to_u64, ptr);
link!("json.toF64", json_to_f64, ptr);
link!("json.toBigInt", json_to_big_int, ptr);
link!("yaml.fromBytes", yaml_from_bytes, ptr);
link!("yaml.try_fromBytes", yaml_try_from_bytes, ptr);
link!("crypto.keccak256", crypto_keccak_256, ptr);
link!("bigInt.plus", big_int_plus, x_ptr, y_ptr);
link!("bigInt.minus", big_int_minus, x_ptr, y_ptr);
link!("bigInt.times", big_int_times, x_ptr, y_ptr);
link!("bigInt.dividedBy", big_int_divided_by, x_ptr, y_ptr);
link!("bigInt.dividedByDecimal", big_int_divided_by_decimal, x, y);
link!("bigInt.mod", big_int_mod, x_ptr, y_ptr);
link!("bigInt.pow", big_int_pow, x_ptr, exp);
link!("bigInt.fromString", big_int_from_string, ptr);
link!("bigInt.bitOr", big_int_bit_or, x_ptr, y_ptr);
link!("bigInt.bitAnd", big_int_bit_and, x_ptr, y_ptr);
link!("bigInt.leftShift", big_int_left_shift, x_ptr, bits);
link!("bigInt.rightShift", big_int_right_shift, x_ptr, bits);
link!("bigDecimal.toString", big_decimal_to_string, ptr);
link!("bigDecimal.fromString", big_decimal_from_string, ptr);
link!("bigDecimal.plus", big_decimal_plus, x_ptr, y_ptr);
link!("bigDecimal.minus", big_decimal_minus, x_ptr, y_ptr);
link!("bigDecimal.times", big_decimal_times, x_ptr, y_ptr);
link!("bigDecimal.dividedBy", big_decimal_divided_by, x, y);
link!("bigDecimal.equals", big_decimal_equals, x_ptr, y_ptr);
link!("dataSource.create", data_source_create, name, params);
link!(
"dataSource.createWithContext",
data_source_create_with_context,
name,
params,
context
);
link!("dataSource.address", data_source_address,);
link!("dataSource.network", data_source_network,);
link!("dataSource.context", data_source_context,);
link!("ens.nameByHash", ens_name_by_hash, ptr);
link!("log.log", log_log, level, msg_ptr);
// `arweave` and `box` functionality was removed. The implementations return deterministic
// errors. Linked unconditionally since import_name_to_modules ensures they're only
// registered if the module actually imports them.
link!("arweave.transactionData", arweave_transaction_data, ptr);
link!("box.profile", box_profile, ptr);
// link the `gas` function
// See also e3f03e62-40e4-4f8c-b4a1-d0375cca0b76
linker.func_wrap(
"gas",
"gas",
|mut caller: wasmtime::Caller<'_, WasmInstanceData>, gas_used: u64| -> anyhow::Result<()> {
// Gas metering has a relevant execution cost cost, being called tens of thousands
// of times per handler, but it's not worth having a stopwatch section here because
// the cost of measuring would be greater than the cost of `consume_host_fn`. Last
// time this was benchmarked it took < 100ns to run.
if let Err(e) = caller
.data()
.gas
.consume_host_fn_with_metrics(Gas::new(gas_used), "gas")
{
caller.data_mut().deterministic_host_trap = true;
return Err(e.into());
}
Ok(())
},
)?;
Ok(linker)
}
impl WasmInstance {
/// Instantiates the module and sets it to be interrupted after `timeout`.
pub async fn from_valid_module_with_ctx(
valid_module: Arc<ValidModule>,
ctx: MappingContext,
host_metrics: Arc<HostMetrics>,
experimental_features: ExperimentalFeatures,
) -> Result<WasmInstance, anyhow::Error> {
let engine = valid_module.module.engine();
let api_version = ctx.host_exports.data_source.api_version.clone();
let gas = GasCounter::new(host_metrics.gas_metrics.clone());
let wasm_ctx = WasmInstanceData::from_instance(
ctx,
valid_module.cheap_clone(),
host_metrics.cheap_clone(),
gas.cheap_clone(),
experimental_features,
);
let mut store = Store::new(engine, wasm_ctx);
// The epoch on the engine will only ever be incremeted if increment_epoch() is explicitly
// called, we only do so if a timeout has been set, it will run forever. When a timeout is
// set, the timeout duration is used as the duration of one epoch.
//
// Therefore, the setting of 2 here means that if a `timeout` is provided, then this
// interrupt will be triggered between a duration of `timeout` and `timeout * 2`.
//
// See also: runtime-timeouts
store.set_epoch_deadline(2);
let instance = valid_module
.instance_pre
.instantiate_async(store.as_context_mut())
.await?;
let asc_heap = AscHeapCtx::new(
&instance,
&mut WasmInstanceContext::new(&mut store),
api_version.clone(),
)?;
store.data_mut().set_asc_heap(asc_heap);
// See start_function comment for more information
// TL;DR; we need the wasmtime::Instance to create the heap, therefore
// we cannot execute anything that requires access to the heap before it's created.
if let Some(start_func) = valid_module.start_function.as_ref() {
instance
.get_func(store.as_context_mut(), start_func)
.context(format!("`{start_func}` function not found"))?
.typed::<(), ()>(store.as_context_mut())?
.call_async(store.as_context_mut(), ())
.await?;
}
match api_version {
version if version <= Version::new(0, 0, 4) => {}
_ => {
instance
.get_func(store.as_context_mut(), "_start")
.context("`_start` function not found")?
.typed::<(), ()>(store.as_context_mut())?
.call_async(store.as_context_mut(), ())
.await?;
}
}
Ok(WasmInstance {
instance,
gas,
store,
})
}
/// Similar to `from_valid_module_with_ctx` but returns a boxed future.
/// This is needed to allow mutually recursive calls of futures, e.g.,
/// in `ipfs_map` as that is a host function that calls back into WASM
/// code which in turn might call back into host functions.
pub fn from_valid_module_with_ctx_boxed(
valid_module: Arc<ValidModule>,
ctx: MappingContext,
host_metrics: Arc<HostMetrics>,
experimental_features: ExperimentalFeatures,
) -> BoxFuture<'static, Result<WasmInstance, anyhow::Error>> {
async move {
WasmInstance::from_valid_module_with_ctx(
valid_module,
ctx,
host_metrics,
experimental_features,
)
.await
}
.boxed()
}
}