Skip to content

Commit a77ff7a

Browse files
authored
Merge pull request #5348 from igor-casper/core-205
[VM2] Move WasmerEnv's execution stack to context
2 parents 74c1a46 + 9ccd38d commit a77ff7a

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

executor/wasm/src/lib.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod install;
22

33
use std::{
4-
collections::{BTreeMap, BTreeSet, VecDeque},
4+
collections::{BTreeMap, BTreeSet},
55
sync::Arc,
66
};
77

@@ -61,7 +61,6 @@ use casper_types::{
6161
NAME_FOR_V2_CONTRACT_MAIN_PURSE,
6262
};
6363
use install::{InstallContractError, InstallContractRequest, InstallContractResult};
64-
use parking_lot::RwLock;
6564
use tracing::{debug, error, warn};
6665

6766
#[cfg(any(feature = "testing", test))]
@@ -187,7 +186,6 @@ impl ExecutorConfigBuilder {
187186
pub struct ExecutorV2 {
188187
config: ExecutorConfig,
189188
compiled_wasm_engine: Arc<WasmerEngine>,
190-
execution_stack: Arc<RwLock<VecDeque<ExecutionKind>>>,
191189
execution_engine_v1: ExecutionEngineV1,
192190
}
193191

@@ -590,6 +588,7 @@ impl ExecutorV2 {
590588
sandboxed,
591589
runtime_native_config,
592590
authorization_keys,
591+
execution_stack,
593592
} = execute_request;
594593

595594
let (entity_addr, source_purse) = get_purse_for_entity(&mut tracking_copy, caller_key)?;
@@ -998,6 +997,7 @@ impl ExecutorV2 {
998997
block_height,
999998
authorization_keys,
1000999
ffi_call_costs,
1000+
execution_stack: Arc::clone(&execution_stack),
10011001
};
10021002

10031003
// Check that the input argument size does not exceed the VM memory limit
@@ -1023,10 +1023,16 @@ impl ExecutorV2 {
10231023
.instantiate(wasm_bytes, self.clone(), context, wasm_instance_config)
10241024
.map_err(ExecuteError::WasmPreparation)?;
10251025

1026-
self.push_execution_stack(execution_kind.clone());
1026+
{
1027+
let mut stack = execution_stack.write();
1028+
stack.push_back(execution_kind.clone());
1029+
}
10271030
let (vm_result, gas_usage) = instance.call_export(export_name);
1028-
1029-
let top_execution_kind = self.pop_execution_stack().ok_or({
1031+
let top_execution_kind = {
1032+
let mut stack = execution_stack.write();
1033+
stack.pop_back()
1034+
}
1035+
.ok_or({
10301036
//This shouldn't happen since we just pushed
10311037
ExecuteError::Fatal(FatalHostError::CorruptExecutionState(
10321038
"Unexpected empty execution stack".to_owned(),
@@ -1393,22 +1399,9 @@ impl ExecutorV2 {
13931399
ExecutorV2 {
13941400
config,
13951401
compiled_wasm_engine: Arc::new(wasm_engine),
1396-
execution_stack: Default::default(),
13971402
execution_engine_v1,
13981403
}
13991404
}
1400-
1401-
/// Push the execution stack.
1402-
pub(crate) fn push_execution_stack(&self, execution_kind: ExecutionKind) {
1403-
let mut execution_stack = self.execution_stack.write();
1404-
execution_stack.push_back(execution_kind);
1405-
}
1406-
1407-
/// Pop the execution stack.
1408-
pub(crate) fn pop_execution_stack(&self) -> Option<ExecutionKind> {
1409-
let mut execution_stack = self.execution_stack.write();
1410-
execution_stack.pop_back()
1411-
}
14121405
}
14131406

14141407
impl Executor for ExecutorV2 {

executor/wasm_host/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::{
2-
collections::{BTreeMap, BTreeSet},
2+
collections::{BTreeMap, BTreeSet, VecDeque},
33
sync::Arc,
44
};
55

66
use bytes::Bytes;
7+
use casper_executor_wasm_interface::executor::ExecutionKind;
78
use casper_storage::{
89
global_state::GlobalStateReader, AddressGenerator, RuntimeNativeConfig, TrackingCopy,
910
};
@@ -50,4 +51,6 @@ pub struct Context<S: GlobalStateReader> {
5051
pub authorization_keys: BTreeSet<AccountHash>,
5152
/// Map of ffi menu options to their respective cost entries
5253
pub ffi_call_costs: BTreeMap<u32, HostFFIFunctionCost>,
54+
/// Shared execution stack across nested calls
55+
pub execution_stack: Arc<RwLock<VecDeque<ExecutionKind>>>,
5356
}

executor/wasm_interface/src/executor.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{collections::BTreeSet, sync::Arc};
1+
use std::{
2+
collections::{BTreeSet, VecDeque},
3+
sync::Arc,
4+
};
25

36
use borsh::BorshSerialize;
47
use bytes::Bytes;
@@ -68,6 +71,8 @@ pub struct ExecuteRequest {
6871
pub runtime_native_config: RuntimeNativeConfig,
6972
/// Authorization keys for this execution.
7073
pub authorization_keys: BTreeSet<AccountHash>,
74+
/// Shared execution stack across nested calls.
75+
pub execution_stack: Arc<RwLock<VecDeque<ExecutionKind>>>,
7176
}
7277

7378
/// Builder for `ExecuteRequest`.
@@ -89,6 +94,7 @@ pub struct ExecuteRequestBuilder {
8994
sandboxed: Option<bool>,
9095
runtime_native_config: Option<RuntimeNativeConfig>,
9196
authorization_keys: Option<BTreeSet<AccountHash>>,
97+
execution_stack: Option<Arc<RwLock<VecDeque<ExecutionKind>>>>,
9298
}
9399

94100
impl ExecuteRequestBuilder {
@@ -229,6 +235,15 @@ impl ExecuteRequestBuilder {
229235
self
230236
}
231237

238+
/// Set the shared execution stack used to track nested calls.
239+
pub fn with_execution_stack(
240+
mut self,
241+
execution_stack: Arc<RwLock<VecDeque<ExecutionKind>>>,
242+
) -> Self {
243+
self.execution_stack = Some(execution_stack);
244+
self
245+
}
246+
232247
/// Build the `ExecuteRequest`.
233248
pub fn build(self) -> Result<ExecuteRequest, &'static str> {
234249
let initiator = self.initiator.ok_or("Initiator is not set")?;
@@ -255,6 +270,9 @@ impl ExecuteRequestBuilder {
255270
let authorization_keys = self
256271
.authorization_keys
257272
.ok_or("Authorization keys are not set")?;
273+
let execution_stack = self
274+
.execution_stack
275+
.unwrap_or_else(|| Arc::new(RwLock::new(VecDeque::new())));
258276
Ok(ExecuteRequest {
259277
initiator,
260278
caller_key,
@@ -272,6 +290,7 @@ impl ExecuteRequestBuilder {
272290
sandboxed,
273291
runtime_native_config,
274292
authorization_keys,
293+
execution_stack,
275294
})
276295
}
277296
}

executor/wasmer_backend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ where
528528
block_height: data.context.block_height,
529529
authorization_keys: data.context.authorization_keys.clone(),
530530
ffi_call_costs: data.context.ffi_call_costs.clone(),
531+
execution_stack: Arc::clone(&data.context.execution_stack),
531532
}
532533
}
533534
}

0 commit comments

Comments
 (0)