Skip to content

Commit

Permalink
Add atomic examples (#111)
Browse files Browse the repository at this point in the history
* Add atomic examples

* Remove #[no_mangle]
  • Loading branch information
mohanson authored Sep 25, 2024
1 parent b8f1f46 commit f7c7b7f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 17 deletions.
11 changes: 11 additions & 0 deletions examples/always_failure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_std]
#![no_main]

use ckb_std::{default_alloc, entry};

entry!(main);
default_alloc!();

fn main() -> i8 {
1
}
11 changes: 11 additions & 0 deletions examples/always_success.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_std]
#![no_main]

use ckb_std::{default_alloc, entry};

entry!(main);
default_alloc!();

fn main() -> i8 {
0
}
91 changes: 91 additions & 0 deletions examples/atomic.rs
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 0 additions & 14 deletions examples/main.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/syscalls/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/src/type_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit f7c7b7f

Please sign in to comment.