diff --git a/examples/always_failure.rs b/examples/always_failure.rs new file mode 100644 index 0000000..a2283f3 --- /dev/null +++ b/examples/always_failure.rs @@ -0,0 +1,11 @@ +#![no_std] +#![no_main] + +use ckb_std::{default_alloc, entry}; + +entry!(main); +default_alloc!(); + +fn main() -> i8 { + 1 +} diff --git a/examples/always_success.rs b/examples/always_success.rs new file mode 100644 index 0000000..066ac16 --- /dev/null +++ b/examples/always_success.rs @@ -0,0 +1,11 @@ +#![no_std] +#![no_main] + +use ckb_std::{default_alloc, entry}; + +entry!(main); +default_alloc!(); + +fn main() -> i8 { + 0 +} diff --git a/examples/atomic.rs b/examples/atomic.rs new file mode 100644 index 0000000..e8344dc --- /dev/null +++ b/examples/atomic.rs @@ -0,0 +1,91 @@ +#![no_main] +#![no_std] + +// Copied from https://github.com/rust-lang/rust/blob/master/library/core/tests/atomic.rs. + +use ckb_std::{default_alloc, entry}; +use core::sync::atomic::Ordering::SeqCst; +use core::sync::atomic::*; + +entry!(main); +default_alloc!(); + +fn bool_compare_exchange() { + let a = AtomicBool::new(false); + assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Ok(false)); + assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Err(true)); + + a.store(false, SeqCst); + assert_eq!(a.compare_exchange(false, true, SeqCst, SeqCst), Ok(false)); +} + +fn bool_and() { + let a = AtomicBool::new(true); + assert_eq!(a.fetch_and(false, SeqCst), true); + assert_eq!(a.load(SeqCst), false); +} + +fn bool_nand() { + let a = AtomicBool::new(false); + assert_eq!(a.fetch_nand(false, SeqCst), false); + assert_eq!(a.load(SeqCst), true); + assert_eq!(a.fetch_nand(false, SeqCst), true); + assert_eq!(a.load(SeqCst), true); + assert_eq!(a.fetch_nand(true, SeqCst), true); + assert_eq!(a.load(SeqCst), false); + assert_eq!(a.fetch_nand(true, SeqCst), false); + assert_eq!(a.load(SeqCst), true); +} + +fn uint_and() { + let x = AtomicUsize::new(0xf731); + assert_eq!(x.fetch_and(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), 0xf731 & 0x137f); +} + +fn uint_nand() { + let x = AtomicUsize::new(0xf731); + assert_eq!(x.fetch_nand(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), !(0xf731 & 0x137f)); +} + +fn uint_or() { + let x = AtomicUsize::new(0xf731); + assert_eq!(x.fetch_or(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), 0xf731 | 0x137f); +} + +fn uint_xor() { + let x = AtomicUsize::new(0xf731); + assert_eq!(x.fetch_xor(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f); +} + +fn uint_min() { + let x = AtomicUsize::new(0xf731); + assert_eq!(x.fetch_min(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), 0x137f); + assert_eq!(x.fetch_min(0xf731, SeqCst), 0x137f); + assert_eq!(x.load(SeqCst), 0x137f); +} + +fn uint_max() { + let x = AtomicUsize::new(0x137f); + assert_eq!(x.fetch_max(0xf731, SeqCst), 0x137f); + assert_eq!(x.load(SeqCst), 0xf731); + assert_eq!(x.fetch_max(0x137f, SeqCst), 0xf731); + assert_eq!(x.load(SeqCst), 0xf731); +} + +fn main() -> i8 { + bool_compare_exchange(); + bool_and(); + bool_nand(); + uint_and(); + uint_nand(); + uint_or(); + uint_xor(); + uint_min(); + uint_max(); + return 0; +} diff --git a/examples/main.rs b/examples/main.rs deleted file mode 100644 index ce6b818..0000000 --- a/examples/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![no_std] -#![no_main] - -use alloc::vec; -use ckb_std::{debug, default_alloc, entry}; - -fn main() -> i8 { - let v = vec![0u8; 42]; - debug!("{:?}", v.len()); - 0 -} - -entry!(main); -default_alloc!(); diff --git a/src/syscalls/native.rs b/src/syscalls/native.rs index 2106567..7201569 100644 --- a/src/syscalls/native.rs +++ b/src/syscalls/native.rs @@ -559,7 +559,8 @@ pub fn exec(index: usize, source: Source, place: usize, bounds: usize, argv: &[& // https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html let argc = argv.len(); // On some platforms, CStr may be u8, adding as *const i8 is used to reduce such warnings - let argv_ptr: alloc::vec::Vec<*const i8> = argv.iter().map(|e| e.as_ptr() as *const i8).collect(); + let argv_ptr: alloc::vec::Vec<*const i8> = + argv.iter().map(|e| e.as_ptr() as *const i8).collect(); unsafe { syscall( index as u64, diff --git a/test/Makefile b/test/Makefile index 55a2180..f80f504 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,8 +9,10 @@ build: cd ../contracts && RUSTFLAGS="-C target-feature=-a" cargo build --target riscv64imac-unknown-none-elf build-examples: + cd ../examples && RUSTFLAGS="-C target-feature=-a" cargo build --features build-with-clang --example always_failure --target riscv64imac-unknown-none-elf + cd ../examples && RUSTFLAGS="-C target-feature=-a" cargo build --features build-with-clang --example always_success --target riscv64imac-unknown-none-elf + cd ../examples && RUSTFLAGS="-C target-feature=-a" cargo build --features build-with-clang --example atomic --target riscv64imac-unknown-none-elf cd ../examples && RUSTFLAGS="-C target-feature=-a" cargo build --features build-with-clang --example type_id --target riscv64imac-unknown-none-elf --features "type-id" - cd ../examples && RUSTFLAGS="-C target-feature=-a" cargo build --features build-with-clang --example main --target riscv64imac-unknown-none-elf clean: rm -rf ../build diff --git a/test/src/type_id.rs b/test/src/type_id.rs index 11abb18..45f99e5 100644 --- a/test/src/type_id.rs +++ b/test/src/type_id.rs @@ -9,7 +9,7 @@ const MAX_CYCLES: u64 = 1000_0000; fn build_bins() -> (Bytes, Bytes) { let always_success_bin = { let mut buf = Vec::new(); - File::open("../target/riscv64imac-unknown-none-elf/debug/examples/main") + File::open("../target/riscv64imac-unknown-none-elf/debug/examples/always_success") .unwrap() .read_to_end(&mut buf) .expect("read code");