Skip to content

Commit d1dc413

Browse files
authored
Alternative syscall implementations (#136)
* Add a way to stub syscalls with a trait impl * Add SyscallExecutor trait that implements SyscallImpls via a single syscall func * Add syscall_to_impls utility function
1 parent 95e6cfc commit d1dc413

File tree

8 files changed

+1069
-71
lines changed

8 files changed

+1069
-71
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ default = ["allocator", "calc-hash", "ckb-types", "dummy-atomic", "libc"]
2727
calc-hash = ["ckb-types/calc-hash"]
2828
allocator = ["buddy-alloc"]
2929
native-simulator = ["ckb-x64-simulator"]
30+
stub-syscalls = []
31+
stub-c-syscalls = ["stub-syscalls"]
3032
dlopen-c = ["libc"]
3133
build-with-clang = []
3234
libc = []
@@ -50,6 +52,7 @@ buddy-alloc = { version = "0.6", optional = true }
5052
ckb-x64-simulator = { version = "0.11.1", optional = true }
5153
gcd = "2.3"
5254
log = { version = "0.4", optional = true, default-features = false }
55+
int-enum = "1.2.0"
5356

5457
[workspace]
5558
exclude = ["test"]

src/ckb_constants.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use int_enum::IntEnum;
2+
13
pub const SYS_EXIT: u64 = 93;
24
pub const SYS_VM_VERSION: u64 = 2041;
35
pub const SYS_CURRENT_CYCLES: u64 = 2042;
@@ -27,7 +29,7 @@ pub const SYS_CLOSE: u64 = 2608;
2729
pub const SYS_LOAD_BLOCK_EXTENSION: u64 = 2104;
2830

2931
pub const CKB_SUCCESS: u64 = 0;
30-
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
32+
#[derive(Eq, PartialEq, Debug, Clone, Copy, IntEnum)]
3133
#[repr(u64)]
3234
pub enum Source {
3335
Input = 1,
@@ -38,7 +40,7 @@ pub enum Source {
3840
GroupOutput = 0x0100000000000002,
3941
}
4042

41-
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
43+
#[derive(Eq, PartialEq, Debug, Clone, Copy, IntEnum)]
4244
#[repr(u64)]
4345
pub enum CellField {
4446
Capacity = 0,
@@ -50,15 +52,15 @@ pub enum CellField {
5052
OccupiedCapacity = 6,
5153
}
5254

53-
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
55+
#[derive(Eq, PartialEq, Debug, Clone, Copy, IntEnum)]
5456
#[repr(u64)]
5557
pub enum HeaderField {
5658
EpochNumber = 0,
5759
EpochStartBlockNumber = 1,
5860
EpochLength = 2,
5961
}
6062

61-
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
63+
#[derive(Eq, PartialEq, Debug, Clone, Copy, IntEnum)]
6264
#[repr(u64)]
6365
pub enum InputField {
6466
OutPoint = 0,

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum SysError {
2828
}
2929

3030
impl SysError {
31+
#[allow(dead_code)]
3132
pub(crate) fn build_syscall_result(
3233
errno: u64,
3334
load_len: usize,

src/syscalls/internal.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[repr(C)]
2+
pub struct SpawnArgs {
3+
/// argc contains the number of arguments passed to the program.
4+
pub argc: u64,
5+
/// argv is a one-dimensional array of strings.
6+
pub argv: *const *const i8,
7+
/// a pointer used to save the process_id of the child process.
8+
pub process_id: *mut u64,
9+
/// an array representing the file descriptors passed to the child process. It must end with zero.
10+
pub inherited_fds: *const u64,
11+
}

src/syscalls/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// re-export to maintain compatible with old versions
22
pub use crate::error::SysError;
33

4-
#[cfg(not(feature = "native-simulator"))]
4+
mod internal;
5+
pub mod traits;
6+
7+
#[cfg(not(any(feature = "native-simulator", feature = "stub-syscalls")))]
58
mod native;
6-
#[cfg(not(feature = "native-simulator"))]
9+
#[cfg(not(any(feature = "native-simulator", feature = "stub-syscalls")))]
710
pub use native::*;
811

912
#[cfg(feature = "native-simulator")]
1013
mod simulator;
1114
#[cfg(feature = "native-simulator")]
1215
pub use simulator::*;
16+
17+
#[cfg(feature = "stub-syscalls")]
18+
mod stub;
19+
#[cfg(feature = "stub-syscalls")]
20+
pub use stub::*;

0 commit comments

Comments
 (0)