Skip to content

Commit

Permalink
Merge pull request #4822 from libraries/refactor_vmargs
Browse files Browse the repository at this point in the history
script: refactored vmargs to accept a vector array
  • Loading branch information
zhangsoledad authored Feb 26, 2025
2 parents c53044e + 0dcf7dc commit 8444466
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use crate::scheduler::{Scheduler, ROOT_VM_ID};
pub use crate::syscalls::generator::generate_ckb_syscalls;
pub use crate::types::{
ChunkCommand, CoreMachine, DataLocation, DataPieceId, RunMode, ScriptGroup, ScriptGroupType,
ScriptVersion, TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion,
ScriptVersion, TransactionState, TxData, VerifyResult, VmArgs, VmIsa, VmState, VmVersion,
};
pub use crate::verify::TransactionScriptsVerifier;
pub use crate::verify_env::TxVerifyEnv;
34 changes: 21 additions & 13 deletions script/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::syscalls::{

use crate::types::{
CoreMachineType, DataLocation, DataPieceId, DebugContext, Fd, FdArgs, FullSuspendedState,
Machine, Message, ReadState, RunMode, SgData, VmContext, VmId, VmState, WriteState,
Machine, Message, ReadState, RunMode, SgData, VmArgs, VmContext, VmId, VmState, WriteState,
FIRST_FD_SLOT, FIRST_VM_ID,
};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
Expand Down Expand Up @@ -240,7 +240,7 @@ where
offset: 0,
length: u64::MAX,
},
None
VmArgs::Vector(vec![]),
)?,
ROOT_VM_ID
);
Expand Down Expand Up @@ -401,7 +401,11 @@ where
&mut new_machine,
&args.location,
program,
Some((vm_id, args.argc, args.argv)),
VmArgs::Reader {
vm_id,
argc: args.argc,
argv: args.argv,
},
)?;
// The insert operation removes the old vm instance and adds the new vm instance.
debug_assert!(self.instantiated.contains_key(&vm_id));
Expand All @@ -419,8 +423,14 @@ where
machine.machine.set_register(A0, MAX_VMS_SPAWNED as u64);
continue;
}
let spawned_vm_id =
self.boot_vm(&args.location, Some((vm_id, args.argc, args.argv)))?;
let spawned_vm_id = self.boot_vm(
&args.location,
VmArgs::Reader {
vm_id,
argc: args.argc,
argv: args.argv,
},
)?;
// Move passed fds from spawner to spawnee
for fd in &args.fds {
self.fds.insert(*fd, spawned_vm_id);
Expand Down Expand Up @@ -811,11 +821,7 @@ where
}

/// Boot a vm by given program and args.
pub fn boot_vm(
&mut self,
location: &DataLocation,
args: Option<(u64, u64, u64)>,
) -> Result<VmId, Error> {
pub fn boot_vm(&mut self, location: &DataLocation, args: VmArgs) -> Result<VmId, Error> {
let id = self.next_vm_id;
self.next_vm_id += 1;
let (context, mut machine) = self.create_dummy_vm(&id)?;
Expand Down Expand Up @@ -848,16 +854,18 @@ where
machine: &mut Machine,
location: &DataLocation,
program: Bytes,
args: Option<(u64, u64, u64)>,
args: VmArgs,
) -> Result<u64, Error> {
let metadata = parse_elf::<u64>(&program, machine.machine.version())?;
let bytes = match args {
Some((vm_id, argc, argv)) => {
VmArgs::Reader { vm_id, argc, argv } => {
let (_, machine_from) = self.ensure_get_instantiated(&vm_id)?;
let argv = FlattenedArgsReader::new(machine_from.machine.memory_mut(), argc, argv);
machine.load_program_with_metadata(&program, &metadata, argv)?
}
None => machine.load_program_with_metadata(&program, &metadata, vec![].into_iter())?,
VmArgs::Vector(data) => {
machine.load_program_with_metadata(&program, &metadata, data.into_iter().map(Ok))?
}
};
let mut sc = context.snapshot2_context.lock().expect("lock");
sc.mark_program(
Expand Down
17 changes: 17 additions & 0 deletions script/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,23 @@ where
}
}

/// When the vm is initialized, arguments are loaded onto the stack.
/// This enum specifies how to locate these arguments.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum VmArgs {
/// Represents reading arguments from other vm.
Reader {
/// An identifier for the virtual machine/process.
vm_id: u64,
/// The number of arguments provided.
argc: u64,
/// The pointer of the actual arguments.
argv: u64,
},
/// Represents reading arguments from a vector.
Vector(Vec<Bytes>),
}

/// Mutable data at virtual machine level
#[derive(Clone)]
pub struct VmContext<DL>
Expand Down

0 comments on commit 8444466

Please sign in to comment.