Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix warning: cargo clippy
Browse files Browse the repository at this point in the history
joii2020 committed Sep 29, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent cbf704d commit 24e5295
Showing 6 changed files with 88 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
@@ -16,3 +16,5 @@ jobs:
- uses: actions/checkout@v3
- name: Run Cargo Test
run: cargo test
- name: Run Cargo clippy
run: cargo clippy --all
35 changes: 15 additions & 20 deletions src/global_data.rs
Original file line number Diff line number Diff line change
@@ -17,14 +17,14 @@ impl From<u64> for TxID {
Self(value)
}
}
impl Into<u64> for TxID {
fn into(self) -> u64 {
self.0
impl From<TxID> for u64 {
fn from(value: TxID) -> Self {
value.0
}
}
impl TxID {
fn next(&mut self) -> Self {
self.0 = self.0 + 1;
self.0 += 1;
self.clone()
}
}
@@ -36,15 +36,15 @@ impl From<u64> for ProcID {
Self(value)
}
}
impl Into<u64> for ProcID {
fn into(self) -> u64 {
self.0
impl From<ProcID> for u64 {
fn from(value: ProcID) -> Self {
value.0
}
}
impl ProcID {
pub fn next(&mut self) -> Self {
let id = self.clone();
self.0 = self.0 + 1;
self.0 += 1;
id
}
}
@@ -68,20 +68,15 @@ impl GlobalData {
if unsafe { GLOBAL_DATA_PTR.is_null() } {
&GLOBAL_DATA
} else {
unsafe {
let ptr = GLOBAL_DATA_PTR as *mut Mutex<Self>;
let ptr2: &mut Mutex<Self> = &mut *ptr;

ptr2
}
unsafe { &mut *GLOBAL_DATA_PTR as &mut Mutex<Self> }
}
}
pub fn locked() -> MutexGuard<'static, Self> {
Self::get().lock().unwrap()
}
pub fn get_ptr() -> *const c_void {
if unsafe { GLOBAL_DATA_PTR.is_null() } {
let infos_ref: &Mutex<Self> = &*GLOBAL_DATA;
let infos_ref: &Mutex<Self> = &GLOBAL_DATA;
infos_ref as *const Mutex<Self> as *const c_void
} else {
unsafe { GLOBAL_DATA_PTR as *const c_void }
@@ -100,12 +95,12 @@ impl GlobalData {
pub fn get_tx(&self, id: &TxID) -> &TxContext {
self.tx_ctx
.get(id)
.expect(&format!("unknow tx context: {:?}", id))
.unwrap_or_else(|| panic!("unknow tx context: {:?}", id))
}
pub fn get_tx_mut(&mut self, id: &TxID) -> &mut TxContext {
self.tx_ctx
.get_mut(&id)
.expect(&format!("unknow mut tx context: {:?}", id))
.get_mut(id)
.unwrap_or_else(|| panic!("unknow mut tx context: {:?}", id))
}
}

@@ -134,7 +129,7 @@ macro_rules! get_proc {
macro_rules! get_cur_proc {
() => {
GlobalData::locked()
.get_tx(&crate::process_info::TxContext::ctx_id())
.process(&crate::process_info::Process::ctx_id())
.get_tx(&TxContext::ctx_id())
.process(&Process::ctx_id())
};
}
57 changes: 37 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ use constants::{
};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::os::raw::{c_char, c_int, c_void};

#[derive(Clone, Serialize, Deserialize)]
@@ -104,12 +104,8 @@ pub extern "C" fn ckb_exec_cell(
) -> c_int {
assert_vm_version();

let sim_path = utils::get_simulator_path(
unsafe { std::slice::from_raw_parts(code_hash, 32) },
hash_type,
offset,
length,
);
let sim_path =
utils::get_simulator_path(utils::to_array(code_hash, 32), hash_type, offset, length);
let sim_path = sim_path.expect("cannot locate native binary for ckb_exec syscall!");

match SETUP.run_type.as_ref().unwrap_or(&RunningType::Executable) {
@@ -174,7 +170,7 @@ pub extern "C" fn ckb_load_script(ptr: *mut c_void, len: *mut u64, offset: u64)

#[no_mangle]
pub extern "C" fn ckb_debug(s: *const c_char) {
let message = unsafe { CStr::from_ptr(s) }.to_str().expect("UTF8 error!");
let message = utils::to_c_str(s).to_str().expect("UTF8 error!");
println!("Debug message: {}", message);
}

@@ -387,6 +383,29 @@ extern "C" {
) -> c_int;
}

// TO fix clippy error: clippy::not_unsafe_ptr_arg_deref
fn rs_simulator_internal_dlopen2(
native_library_path: *const u8,
code: *const u8,
length: u64,
aligned_addr: *mut u8,
aligned_size: u64,
handle: *mut *mut c_void,
consumed_size: *mut u64,
) -> c_int {
unsafe {
simulator_internal_dlopen2(
native_library_path,
code,
length,
aligned_addr,
aligned_size,
handle,
consumed_size,
)
}
}

#[no_mangle]
pub extern "C" fn ckb_dlopen2(
dep_cell_hash: *const u8,
@@ -396,7 +415,7 @@ pub extern "C" fn ckb_dlopen2(
handle: *mut *mut c_void,
consumed_size: *mut u64,
) -> c_int {
let dep_cell_hash = unsafe { std::slice::from_raw_parts(dep_cell_hash, 32) };
let dep_cell_hash = utils::to_array(dep_cell_hash, 32);
let mut buffer = vec![];
buffer.extend_from_slice(dep_cell_hash);
buffer.push(hash_type);
@@ -423,17 +442,15 @@ pub extern "C" fn ckb_dlopen2(
})
.expect("cannot locate cell dep");
let cell_data = cell_dep.data.as_ref();
unsafe {
simulator_internal_dlopen2(
filename.as_str().as_ptr(),
cell_data.as_ptr(),
cell_data.len() as u64,
aligned_addr,
aligned_size,
handle,
consumed_size,
)
}
rs_simulator_internal_dlopen2(
filename.as_str().as_ptr(),
cell_data.as_ptr(),
cell_data.len() as u64,
aligned_addr,
aligned_size,
handle,
consumed_size,
)
}

#[no_mangle]
31 changes: 16 additions & 15 deletions src/process_info.rs
Original file line number Diff line number Diff line change
@@ -90,12 +90,10 @@ impl Process {
pub fn wait_by_pid(&self, id: &ProcID) -> Event {
if id == &self.id {
self.event_wait.clone()
} else if let Some(c) = self.get_child_by_id(id) {
c.event_wait.clone()
} else {
if let Some(c) = self.get_child_by_id(id) {
c.event_wait.clone()
} else {
panic!("notify unknow pid {:?}", id);
}
panic!("notify unknow pid {:?}", id);
}
}
}
@@ -137,7 +135,7 @@ impl TxContext {
let p = self
.proc_info
.get_mut(parent_id.as_ref().unwrap())
.expect(&format!("unknow pid: {:?}", parent_id));
.unwrap_or_else(|| panic!("unknow pid: {:?}", parent_id));
let e_wait = Event::default();
let e_notify = Event::default();

@@ -171,7 +169,7 @@ impl TxContext {
pub fn process(&self, id: &ProcID) -> &Process {
self.proc_info
.get(id)
.expect(&format!("unknow process id: {:?}", id))
.unwrap_or_else(|| panic!("unknow process id: {:?}", id))
}

pub fn new_pipe(&mut self) -> (Fd, Fd) {
@@ -195,23 +193,26 @@ impl TxContext {
self.fds.len()
}
pub fn move_pipe(&mut self, fd: &Fd, pid: ProcID) {
let f = self.fds.get_mut(fd).expect(&format!("unknow fd: {:?}", fd));
let f = self
.fds
.get_mut(fd)
.unwrap_or_else(|| panic!("unknow fd: {:?}", fd));
*f = pid;
}

pub fn has_fd(&self, fd: &Fd) -> bool {
if let Some(pid) = self.fds.get(&fd) {
if let Some(pid) = self.fds.get(fd) {
&Process::ctx_id() == pid
} else {
false
}
}
pub fn chech_other_fd(&self, fd: &Fd) -> bool {
self.fds.get(&fd.other_fd()).is_some()
self.fds.contains_key(&fd.other_fd())
}

pub fn read_data(&mut self, fd: &Fd, len: usize) -> Vec<u8> {
let data = self.bufs.get(&fd);
let data = self.bufs.get(fd);
if data.is_none() {
return Vec::new();
}
@@ -233,7 +234,7 @@ impl TxContext {
}
}
pub fn has_data(&self, fd: &Fd) -> bool {
return self.bufs.get(fd).is_some() || self.bufs.get(&fd.other_fd()).is_some();
self.bufs.contains_key(fd) || self.bufs.contains_key(&fd.other_fd())
}
}

@@ -277,9 +278,9 @@ impl From<u64> for Fd {
Self(value)
}
}
impl Into<u64> for Fd {
fn into(self) -> u64 {
self.0
impl From<Fd> for u64 {
fn from(value: Fd) -> Self {
value.0
}
}
impl Fd {
28 changes: 8 additions & 20 deletions src/spawn.rs
Original file line number Diff line number Diff line change
@@ -30,17 +30,10 @@ pub extern "C" fn ckb_spawn_cell(
inherited_fds: *const u64,
pid: *mut u64,
) -> c_int {
let (rc, id) = Spawn::default().spawn_cell(
code_hash,
hash_type,
offset,
length,
argc,
argv,
inherited_fds,
);
let ckb_sim = utils::CkbNativeSimulator::new_by_hash(code_hash, hash_type, offset, length);
let (rc, id) = Spawn::default().spawn_cell(ckb_sim, argc, argv, inherited_fds);
if let Some(id) = id {
unsafe { *pid = id.into() }
unsafe { *({ pid }) = id.into() };
}

rc
@@ -133,23 +126,18 @@ impl Spawn {

fn spawn_cell(
&self,
code_hash: *const u8,
hash_type: u8,
offset: u32,
length: u32,
ckb_sim: utils::CkbNativeSimulator,
argc: i32,
argv: *const *const u8,
inherited_fds: *const u64,
) -> (i32, Option<ProcID>) {
let new_id = self.new_process_id(inherited_fds);

utils::CkbNativeSimulator::new_by_hash(code_hash, hash_type, offset, length)
.ckb_std_main_async(argc, argv, &new_id);
ckb_sim.ckb_std_main_async(argc, argv, &new_id);

let event = crate::get_cur_proc!().wait_by_pid(&new_id);
event.wait();

(0, Some(new_id.into()))
(0, Some(new_id))
}
fn inherited_fds(&self, fds: *mut u64, length: *mut usize) -> i32 {
let out_fds = get_tx!(&TxContext::ctx_id())
@@ -249,10 +237,10 @@ impl Spawn {
}
let g = GlobalData::locked();
let tx_ctx = g.get_tx(&TxContext::ctx_id());
if !tx_ctx.has_fd(&fd) {
if !tx_ctx.has_fd(fd) {
return Err(6); // CKB_INVALID_FD
}
if !tx_ctx.chech_other_fd(&fd) {
if !tx_ctx.chech_other_fd(fd) {
return Err(7); // OTHER_END_CLOSED
}
Ok(())
12 changes: 10 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ pub fn get_simulator_path(
break;
}
}
filename.map(|f| f.clone())
filename.cloned()
}

pub struct CkbNativeSimulator {
@@ -107,11 +107,19 @@ impl CkbNativeSimulator {
pub fn to_vec_args(argc: c_int, argv: *const *const i8) -> Vec<String> {
let mut args = Vec::with_capacity(argc as usize);
for i in 0..argc {
let c_str = unsafe { std::ffi::CStr::from_ptr(*argv.add(i as usize) as *const i8) };
let c_str = unsafe { std::ffi::CStr::from_ptr(*argv.add(i as usize)) };
let str_slice = c_str
.to_str()
.expect("Failed to convert C string to Rust string");
args.push(str_slice.to_owned());
}
args
}

pub fn to_array(ptr: *const u8, len: usize) -> &'static [u8] {
unsafe { std::slice::from_raw_parts(ptr, len) }
}

pub fn to_c_str(ptr: *const std::ffi::c_char) -> &'static core::ffi::CStr {
unsafe { core::ffi::CStr::from_ptr(ptr) }
}

0 comments on commit 24e5295

Please sign in to comment.