Skip to content

Commit

Permalink
Remove unnecessary null in argv's end (#88)
Browse files Browse the repository at this point in the history
* Clean imports

* Remove unnecessary null in argv's end

* Update ckb-gen-types

* Fixed rust-toolchain to v1.75.0

* Fix exec_cell in simulator

* Remove test/src/spawn.rs
  • Loading branch information
mohanson committed May 21, 2024
1 parent d63fe58 commit c6296b3
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ ckb2023 = []
cc = "1.0"

[dependencies]
ckb-types = { package = "ckb-gen-types", version = "0.112", default-features = false, optional = true }
ckb-types = { package = "ckb-gen-types", version = "0.116", default-features = false, optional = true }
buddy-alloc = { version = "0.5.0", optional = true }
ckb-x64-simulator = { version = "0.8", optional = true }
gcd = "2.3.0"

[workspace]
exclude = ["test"]
resolver = "2"
resolver = "2"
8 changes: 4 additions & 4 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
stable
1.75.0
1 change: 0 additions & 1 deletion contracts/spawn-caller-by-code-hash/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Import from `core` instead of from `std` since we are in no-std mode
use crate::error::Error;
use ckb_std::ckb_constants::Source;
use ckb_std::ckb_types::core::ScriptHashType;
use ckb_std::high_level::{load_script, spawn_cell};
use ckb_std::syscalls;
Expand Down
10 changes: 4 additions & 6 deletions contracts/spawn-caller/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use core::result::Result;

pub fn main() -> Result<(), Error> {
let argc: u64 = 2;
let argv = {
let mut argv = alloc::vec![core::ptr::null(); argc as usize + 1];
argv[0] = CStr::from_bytes_with_nul(b"hello\0").unwrap().as_ptr();
argv[1] = CStr::from_bytes_with_nul(b"world\0").unwrap().as_ptr();
argv
};
let argv = [
CStr::from_bytes_with_nul(b"hello\0").unwrap().as_ptr(),
CStr::from_bytes_with_nul(b"world\0").unwrap().as_ptr(),
];
let mut std_fds: [u64; 2] = [0, 0];
let mut son_fds: [u64; 3] = [0, 0, 0];
let (r0, w0) = syscalls::pipe()?;
Expand Down
5 changes: 1 addition & 4 deletions src/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,7 @@ pub fn spawn_cell(
let index = look_for_dep_with_hash2(code_hash, hash_type)?;
let argc = argv.len();
let mut process_id: u64 = 0;
let mut argv_ptr = alloc::vec![core::ptr::null(); argc + 1];
for (idx, cstr) in argv.into_iter().enumerate() {
argv_ptr[idx] = cstr.as_ptr();
}
let argv_ptr: Vec<*const i8> = argv.iter().map(|e| e.as_ptr()).collect();
let mut spgs = syscalls::SpawnArgs {
argc: argc as u64,
argv: argv_ptr.as_ptr(),
Expand Down
5 changes: 1 addition & 4 deletions src/syscalls/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,7 @@ pub fn current_cycles() -> u64 {
pub fn exec(index: usize, source: Source, place: usize, bounds: usize, argv: &[&CStr]) -> u64 {
// https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html
let argc = argv.len();
let mut argv_ptr = alloc::vec![core::ptr::null(); argc + 1];
for (idx, cstr) in argv.into_iter().enumerate() {
argv_ptr[idx] = cstr.as_ptr();
}
let argv_ptr: alloc::vec::Vec<*const i8> = argv.iter().map(|e| e.as_ptr()).collect();
unsafe {
syscall(
index as u64,
Expand Down
9 changes: 4 additions & 5 deletions src/syscalls/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,16 @@ pub fn exec_cell(
argv: &[&CStr],
) -> Result<Infallible, SysError> {
let argc = argv.len();
let mut argv_ptr = alloc::vec![core::ptr::null(); argc + 1];
for (idx, cstr) in argv.into_iter().enumerate() {
argv_ptr[idx] = cstr.to_bytes_with_nul().as_ptr();
}
let mut argv_vec: alloc::vec::Vec<*const u8> =
argv.iter().map(|e| e.as_ptr() as *const u8).collect();
argv_vec.push(core::ptr::null());
let ret = sim::ckb_exec_cell(
code_hash.as_ptr(),
hash_type as u8,
0,
0,
argc as i32,
argv_ptr.as_ptr(),
argv_vec.as_ptr(),
);
Err(SysError::Unknown(ret as u64))
}

0 comments on commit c6296b3

Please sign in to comment.