Skip to content

Commit 8444466

Browse files
authored
Merge pull request #4822 from libraries/refactor_vmargs
script: refactored vmargs to accept a vector array
2 parents c53044e + 0dcf7dc commit 8444466

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

script/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use crate::scheduler::{Scheduler, ROOT_VM_ID};
1313
pub use crate::syscalls::generator::generate_ckb_syscalls;
1414
pub use crate::types::{
1515
ChunkCommand, CoreMachine, DataLocation, DataPieceId, RunMode, ScriptGroup, ScriptGroupType,
16-
ScriptVersion, TransactionState, TxData, VerifyResult, VmIsa, VmState, VmVersion,
16+
ScriptVersion, TransactionState, TxData, VerifyResult, VmArgs, VmIsa, VmState, VmVersion,
1717
};
1818
pub use crate::verify::TransactionScriptsVerifier;
1919
pub use crate::verify_env::TxVerifyEnv;

script/src/scheduler.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::syscalls::{
66

77
use crate::types::{
88
CoreMachineType, DataLocation, DataPieceId, DebugContext, Fd, FdArgs, FullSuspendedState,
9-
Machine, Message, ReadState, RunMode, SgData, VmContext, VmId, VmState, WriteState,
9+
Machine, Message, ReadState, RunMode, SgData, VmArgs, VmContext, VmId, VmState, WriteState,
1010
FIRST_FD_SLOT, FIRST_VM_ID,
1111
};
1212
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
@@ -240,7 +240,7 @@ where
240240
offset: 0,
241241
length: u64::MAX,
242242
},
243-
None
243+
VmArgs::Vector(vec![]),
244244
)?,
245245
ROOT_VM_ID
246246
);
@@ -401,7 +401,11 @@ where
401401
&mut new_machine,
402402
&args.location,
403403
program,
404-
Some((vm_id, args.argc, args.argv)),
404+
VmArgs::Reader {
405+
vm_id,
406+
argc: args.argc,
407+
argv: args.argv,
408+
},
405409
)?;
406410
// The insert operation removes the old vm instance and adds the new vm instance.
407411
debug_assert!(self.instantiated.contains_key(&vm_id));
@@ -419,8 +423,14 @@ where
419423
machine.machine.set_register(A0, MAX_VMS_SPAWNED as u64);
420424
continue;
421425
}
422-
let spawned_vm_id =
423-
self.boot_vm(&args.location, Some((vm_id, args.argc, args.argv)))?;
426+
let spawned_vm_id = self.boot_vm(
427+
&args.location,
428+
VmArgs::Reader {
429+
vm_id,
430+
argc: args.argc,
431+
argv: args.argv,
432+
},
433+
)?;
424434
// Move passed fds from spawner to spawnee
425435
for fd in &args.fds {
426436
self.fds.insert(*fd, spawned_vm_id);
@@ -811,11 +821,7 @@ where
811821
}
812822

813823
/// Boot a vm by given program and args.
814-
pub fn boot_vm(
815-
&mut self,
816-
location: &DataLocation,
817-
args: Option<(u64, u64, u64)>,
818-
) -> Result<VmId, Error> {
824+
pub fn boot_vm(&mut self, location: &DataLocation, args: VmArgs) -> Result<VmId, Error> {
819825
let id = self.next_vm_id;
820826
self.next_vm_id += 1;
821827
let (context, mut machine) = self.create_dummy_vm(&id)?;
@@ -848,16 +854,18 @@ where
848854
machine: &mut Machine,
849855
location: &DataLocation,
850856
program: Bytes,
851-
args: Option<(u64, u64, u64)>,
857+
args: VmArgs,
852858
) -> Result<u64, Error> {
853859
let metadata = parse_elf::<u64>(&program, machine.machine.version())?;
854860
let bytes = match args {
855-
Some((vm_id, argc, argv)) => {
861+
VmArgs::Reader { vm_id, argc, argv } => {
856862
let (_, machine_from) = self.ensure_get_instantiated(&vm_id)?;
857863
let argv = FlattenedArgsReader::new(machine_from.machine.memory_mut(), argc, argv);
858864
machine.load_program_with_metadata(&program, &metadata, argv)?
859865
}
860-
None => machine.load_program_with_metadata(&program, &metadata, vec![].into_iter())?,
866+
VmArgs::Vector(data) => {
867+
machine.load_program_with_metadata(&program, &metadata, data.into_iter().map(Ok))?
868+
}
861869
};
862870
let mut sc = context.snapshot2_context.lock().expect("lock");
863871
sc.mark_program(

script/src/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,23 @@ where
10121012
}
10131013
}
10141014

1015+
/// When the vm is initialized, arguments are loaded onto the stack.
1016+
/// This enum specifies how to locate these arguments.
1017+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
1018+
pub enum VmArgs {
1019+
/// Represents reading arguments from other vm.
1020+
Reader {
1021+
/// An identifier for the virtual machine/process.
1022+
vm_id: u64,
1023+
/// The number of arguments provided.
1024+
argc: u64,
1025+
/// The pointer of the actual arguments.
1026+
argv: u64,
1027+
},
1028+
/// Represents reading arguments from a vector.
1029+
Vector(Vec<Bytes>),
1030+
}
1031+
10151032
/// Mutable data at virtual machine level
10161033
#[derive(Clone)]
10171034
pub struct VmContext<DL>

0 commit comments

Comments
 (0)