From 548a42b8007a0f56379352ee501d47ef1872cb14 Mon Sep 17 00:00:00 2001 From: Mohanson Date: Mon, 30 Sep 2024 16:40:52 +0800 Subject: [PATCH] Added tips about dynamic libraries (#117) * Added tips about dynamic libraries * Use lazy_static --- contracts/Cargo.lock | 49 ++++++++++++++++++++++++++-- contracts/ckb-std-tests/Cargo.toml | 2 ++ contracts/ckb-std-tests/src/entry.rs | 32 ++++++++++++------ test/simulator/Cargo.toml | 2 ++ 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index fd25ed9..23677ea 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + [[package]] name = "bit-vec" version = "0.6.3" @@ -259,7 +265,9 @@ dependencies = [ "blake2b-ref", "bytes", "ckb-std", + "lazy_static", "log", + "spin", ] [[package]] @@ -450,6 +458,9 @@ name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +dependencies = [ + "spin", +] [[package]] name = "libc" @@ -467,6 +478,16 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.21" @@ -551,9 +572,12 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" +dependencies = [ + "portable-atomic", +] [[package]] name = "paste" @@ -561,6 +585,12 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -632,6 +662,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.204" @@ -701,6 +737,15 @@ dependencies = [ "ckb-std", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/contracts/ckb-std-tests/Cargo.toml b/contracts/ckb-std-tests/Cargo.toml index 94211c0..d5d3464 100644 --- a/contracts/ckb-std-tests/Cargo.toml +++ b/contracts/ckb-std-tests/Cargo.toml @@ -9,4 +9,6 @@ edition = "2021" ckb-std = { path = "../../", features = ["build-with-clang", "dlopen-c", "log"] } blake2b-ref = { version = "0.3", default-features = false } bytes = { version = "1.7", default-features = false } +lazy_static = { version = "1.5.0", default-features = false, features = ["spin_no_std"] } log = { version = "0.4", default-features = false } +spin = { version = "0.9" } diff --git a/contracts/ckb-std-tests/src/entry.rs b/contracts/ckb-std-tests/src/entry.rs index b85dda3..8661279 100644 --- a/contracts/ckb-std-tests/src/entry.rs +++ b/contracts/ckb-std-tests/src/entry.rs @@ -22,6 +22,10 @@ use ckb_std::{dynamic_loading, dynamic_loading_c_impl}; use core::ffi::c_void; #[cfg(target_arch = "riscv64")] use core::mem::size_of_val; +#[cfg(target_arch = "riscv64")] +use lazy_static::lazy_static; +#[cfg(target_arch = "riscv64")] +use spin::Mutex; fn new_blake2b() -> Blake2b { const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash"; @@ -795,6 +799,19 @@ fn test_log() { ckb_std::log::error!("this is error"); } +#[cfg(target_arch = "riscv64")] +lazy_static! { + // Context should not be dropped. + static ref old_context: Mutex = { + #[allow(deprecated)] + Mutex::new(unsafe { ContextTypeOld::new() }) + }; + // Context should not be dropped. + static ref new_context: Mutex = { + Mutex::new(unsafe { ContextType::new() }) + }; +} + pub fn main() -> Result<(), Error> { test_basic(); test_load_data(); @@ -805,6 +822,11 @@ pub fn main() -> Result<(), Error> { test_query(); test_calc_data_hash(); + #[cfg(target_arch = "riscv64")] + test_dynamic_loading(&mut old_context.lock()); + #[cfg(target_arch = "riscv64")] + test_dynamic_loading_c_impl(&mut new_context.lock()); + test_vm_version(); test_current_cycles(); test_since(); @@ -815,15 +837,5 @@ pub fn main() -> Result<(), Error> { test_log(); } - #[cfg(target_arch = "riscv64")] - unsafe { - let mut context = ContextType::new(); - #[allow(deprecated)] - let mut old_context = ContextTypeOld::new(); - - test_dynamic_loading(&mut old_context); - test_dynamic_loading_c_impl(&mut context); - } - Ok(()) } diff --git a/test/simulator/Cargo.toml b/test/simulator/Cargo.toml index e217cd0..a4cf221 100644 --- a/test/simulator/Cargo.toml +++ b/test/simulator/Cargo.toml @@ -19,7 +19,9 @@ path = "src/exec_callee.rs" ckb-std = { path = "../..", default-features=false, features = ["allocator", "calc-hash", "ckb-types", "libc", "native-simulator"] } blake2b-ref = { version = "0.3", default-features = false } bytes = { version = "1.6.0", default-features = false } +lazy_static = { version = "1.5.0", default-features = false, features = ["spin_no_std"] } log = { version = "0.4.17", default-features = false } +spin = { version = "0.9" } [dev-dependencies] libloading = "0.8.4"