Skip to content

Commit

Permalink
feat: support ckb2021 syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWaWaR committed Sep 29, 2021
1 parent 9c6af1b commit 5155b27
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ int ckb_load_input_by_field(void* addr, uint64_t* len, size_t offset,
size_t index, size_t source, size_t field);
int ckb_load_cell_data(void* addr, uint64_t* len, size_t offset, size_t index,
size_t source);
int ckb_vm_version();
uint64_t ckb_current_cycles();
int ckb_exec_cell(const uint8_t* code_hash, uint8_t hash_type, uint32_t offset,
uint32_t length, int argc, const char* argv[]);

int ckb_dlopen2(const uint8_t* dep_cell_hash, uint8_t hash_type,
uint8_t* aligned_addr, size_t aligned_size, void** handle,
Expand Down
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub const SYS_EXIT: u64 = 93;
pub const SYS_VM_VERSION: u64 = 2041;
pub const SYS_CURRENT_CYCLES: u64 = 2042;
pub const SYS_EXEC: u64 = 2043;
pub const SYS_LOAD_TRANSACTION: u64 = 2051;
pub const SYS_LOAD_SCRIPT: u64 = 2052;
pub const SYS_LOAD_TX_HASH: u64 = 2061;
Expand Down
69 changes: 68 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ use constants::{
};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ffi::CStr;
use std::ffi::{CStr, OsStr};
use std::os::raw::{c_char, c_int, c_void};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::process::CommandExt;
use std::process::Command;

#[derive(Clone, Serialize, Deserialize)]
pub struct RunningSetup {
pub is_lock_script: bool,
pub is_output: bool,
pub script_index: u64,
pub vm_version: i32,
pub native_binaries: HashMap<String, String>,
}

Expand All @@ -47,11 +51,74 @@ lazy_static! {
};
}

fn assert_vm_version() {
if SETUP.vm_version != 1 {
panic!(
"Currently running setup vm_version({}) not support this syscall",
SETUP.vm_version
);
}
}

#[no_mangle]
pub extern "C" fn ckb_exit(code: i8) -> i32 {
std::process::exit(code.into());
}

#[no_mangle]
pub extern "C" fn ckb_vm_version() -> c_int {
assert_vm_version();
SETUP.vm_version
}

#[no_mangle]
pub extern "C" fn ckb_current_cycles() -> u64 {
assert_vm_version();
// NOTE: return a fake number since this value is meaningless in simulator
333
}

/// The binary key string is 0x{code_hash + hash_type + offset.to_be_bytes() + length.to_be_bytes()}
#[no_mangle]
pub extern "C" fn ckb_exec_cell(
code_hash: *const u8,
hash_type: u8,
offset: u32,
length: u32,
argc: i32,
argv: *const *const u8,
) -> c_int {
assert_vm_version();

let code_hash = unsafe {
let ptr = code_hash.as_ref().expect("casting pointer");
std::slice::from_raw_parts(ptr, 32)
};
let mut buffer = vec![];
buffer.extend_from_slice(code_hash);
buffer.push(hash_type);
buffer.extend_from_slice(&offset.to_be_bytes()[..]);
buffer.extend_from_slice(&length.to_be_bytes()[..]);
let key = format!("0x{}", faster_hex::hex_string(&buffer));
let filename = SETUP
.native_binaries
.get(&key)
.expect("cannot locate native binary for ckb_exec syscall!");
let args: &[&CStr] = unsafe {
let ptr = argv.as_ref().expect("casting pointer");
std::mem::transmute::<&[*const u8], &[&CStr]>(std::slice::from_raw_parts(
ptr,
argc as usize,
))
};
let owned_args = args
.iter()
.map(|cstr| OsStr::from_bytes(cstr.to_bytes()))
.collect::<Vec<_>>();
let err = Command::new(filename).args(owned_args).exec();
panic!("ckb_exec error: {:?}", err);
}

#[no_mangle]
pub extern "C" fn ckb_load_tx_hash(ptr: *mut c_void, len: *mut u64, offset: u64) -> c_int {
let view = TRANSACTION.tx.clone().into_view();
Expand Down

0 comments on commit 5155b27

Please sign in to comment.