diff --git a/Cargo.lock b/Cargo.lock index f541d92..d458643 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,9 +32,9 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "indexmap" -version = "2.2.6" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" dependencies = [ "equivalent", "hashbrown", @@ -48,9 +48,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "libc" -version = "0.2.155" +version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" [[package]] name = "mach2" @@ -78,9 +78,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -93,18 +93,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.204" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.204" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -113,20 +113,21 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.120" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] [[package]] name = "serde_spanned" -version = "0.6.6" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" +checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" dependencies = [ "serde", ] @@ -144,9 +145,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.72" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", @@ -164,9 +165,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.15" +version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac2caab0bf757388c6c0ae23b3293fdb463fee59434529014f85e3263b995c28" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" dependencies = [ "serde", "serde_spanned", @@ -176,18 +177,18 @@ dependencies = [ [[package]] name = "toml_datetime" -version = "0.6.6" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" dependencies = [ "serde", ] [[package]] name = "toml_edit" -version = "0.22.16" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "278f3d518e152219c994ce877758516bca5e118eaed6996192a774fb9fbf0788" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", "serde", @@ -198,9 +199,9 @@ dependencies = [ [[package]] name = "trybuild" -version = "1.0.97" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b1e5645f2ee8025c2f1d75e1138f2dd034d74e6ba54620f3c569ba2a2a1ea06" +checksum = "207aa50d36c4be8d8c6ea829478be44a372c6a77669937bb39c698e52f1491e8" dependencies = [ "glob", "serde", @@ -212,15 +213,15 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "winapi-util" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ "windows-sys", ] @@ -291,9 +292,9 @@ dependencies = [ [[package]] name = "windows-sys" -version = "0.52.0" +version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ "windows-targets", ] @@ -364,9 +365,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.15" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "557404e450152cd6795bb558bca69e43c585055f4606e3bcae5894fc6dac9ba0" +checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" dependencies = [ "memchr", ] diff --git a/README.md b/README.md index ccce190..3d28cd8 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,10 @@ Although replacing a `test`-`jnz` pair to `nop` may be minor improvement, howeve ## Usage -To use this crate, currently nightly Rust is required. And in the crate root top, you should declare usage of unstable features `asm_goto` and `asm_const`. +To use this crate, currently nightly Rust is required. And in the crate root top, you should declare usage of unstable features `asm_goto`. ```rust #![feature(asm_goto)] -#![feature(asm_const)] ``` First, add this crate to your `Cargo.toml`: diff --git a/docs/en/src/README.md b/docs/en/src/README.md index c45de4e..a026bee 100644 --- a/docs/en/src/README.md +++ b/docs/en/src/README.md @@ -87,11 +87,10 @@ Although replacing a `test`-`jnz` pair to `nop` may be minor improvement, howeve ## Usage -To use this crate, currently nightly Rust is required. And in the crate root top, you should declare usage of unstable features `asm_goto` and `asm_const`. +To use this crate, currently nightly Rust is required. And in the crate root top, you should declare usage of unstable feature `asm_goto`. ```rust #![feature(asm_goto)] -#![feature(asm_const)] ``` First, add this crate to your `Cargo.toml`: @@ -135,7 +134,6 @@ After the definition, you can use this static key at `if`-check as usual (you ca ```rust # #![feature(asm_goto)] -# #![feature(asm_const)] # use static_keys::{define_static_key_false, static_branch_unlikely}; # struct CommandlineArgs {} # impl CommandlineArgs { fn parse() -> bool { true } } diff --git a/docs/zh-Hans/src/README.md b/docs/zh-Hans/src/README.md index b5e9bee..d29404b 100644 --- a/docs/zh-Hans/src/README.md +++ b/docs/zh-Hans/src/README.md @@ -87,11 +87,10 @@ do_something: ## 使用方式 -目前需要nightly版本的Rust来使用这个库。在使用者的代码中,需要声明对unstable特性`asm_goto`和`asm_const`的使用。 +目前需要nightly版本的Rust来使用这个库。在使用者的代码中,需要声明对unstable特性`asm_goto`的使用。 ```rust #![feature(asm_goto)] -#![feature(asm_const)] ``` 首先,在`Cargo.toml`中加入相应依赖: @@ -135,7 +134,6 @@ fn application_initialize() { ```rust # #![feature(asm_goto)] -# #![feature(asm_const)] # use static_keys::{define_static_key_false, static_branch_unlikely}; # struct CommandlineArgs {} # impl CommandlineArgs { fn parse() -> bool { true } } diff --git a/examples/usage.rs b/examples/usage.rs index 02df234..65410f1 100644 --- a/examples/usage.rs +++ b/examples/usage.rs @@ -1,5 +1,4 @@ #![feature(asm_goto)] -#![feature(asm_const)] use static_keys::{define_static_key_false, static_branch_unlikely}; diff --git a/src/lib.rs b/src/lib.rs index 9dda30a..e8b4c66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![doc = include_str!("../docs/en/src/README.md")] #![no_std] #![feature(asm_goto)] -#![feature(asm_const)] #![allow(clippy::needless_doctest_main)] mod arch; @@ -107,7 +106,10 @@ impl JumpEntry { /// to assign arbitrary status to the static key generic when using. pub struct GenericStaticKey { /// Whether current key is true or false - enabled: bool, + /// + /// This field is defined as `AtomicBool` to allow interior mutability of static variables to avoid + /// creating mutable static. + enabled: core::sync::atomic::AtomicBool, /// Start address of associated jump entries. /// /// The jump entries are sorted based on associated static key address in [`global_init`][Self::global_init] @@ -138,7 +140,7 @@ pub type StaticFalseKey = StaticKey; // however, it seems a Rust bug to erase sections marked with "R" (retained). If we specify // --print-gc-sections for linker options, it's strange that linker itself does not // erase it. IT IS SO STRANGE. -static mut DUMMY_STATIC_KEY: GenericStaticKey = +static DUMMY_STATIC_KEY: GenericStaticKey = GenericStaticKey::new(false); impl GenericStaticKey { @@ -151,7 +153,7 @@ impl GenericStaticKey { /// Create a new static key with given default value. const fn new(enabled: bool) -> Self { Self { - enabled, + enabled: core::sync::atomic::AtomicBool::new(enabled), entries: 0, phantom: core::marker::PhantomData, } @@ -170,7 +172,7 @@ impl GenericStaticKey { /// there are multi-threads running. Spawn threads after this method is called. This method may manipulate /// code region memory protection, and if other threads are executing codes in the same code page, it may /// lead to unexpected behaviors. - pub unsafe fn enable(&mut self) { + pub unsafe fn enable(&self) { static_key_update(self, true) } @@ -182,7 +184,7 @@ impl GenericStaticKey { /// there are multi-threads running. Spawn threads after this method is called. This method may manipulate /// code region memory protection, and if other threads are executing codes in the same code page, it may /// lead to unexpected behaviors. - pub unsafe fn disable(&mut self) { + pub unsafe fn disable(&self) { static_key_update(self, false) } } @@ -310,7 +312,7 @@ pub const fn new_static_true_key() -> StaticTrueKey { macro_rules! define_static_key_false { ($key: ident) => { #[used] - static mut $key: $crate::StaticFalseKey = $crate::new_static_false_key(); + static $key: $crate::StaticFalseKey = $crate::new_static_false_key(); }; } @@ -330,7 +332,7 @@ macro_rules! define_static_key_false { macro_rules! define_static_key_true { ($key: ident) => { #[used] - static mut $key: $crate::StaticTrueKey = $crate::new_static_true_key(); + static $key: $crate::StaticTrueKey = $crate::new_static_true_key(); }; } @@ -346,13 +348,14 @@ macro_rules! define_static_key_true { /// code region memory protection, and if other threads are executing codes in the same code page, it may /// lead to unexpected behaviors. unsafe fn static_key_update( - key: &mut GenericStaticKey, + key: &GenericStaticKey, enabled: bool, ) { - if key.enabled == enabled { + if key.enabled.load(core::sync::atomic::Ordering::Relaxed) == enabled { return; } - key.enabled = enabled; + key.enabled + .store(enabled, core::sync::atomic::Ordering::Relaxed); let jump_entry_stop_addr = core::ptr::addr_of!(os::JUMP_ENTRY_STOP); let mut jump_entry_addr = key.entries(); if jump_entry_addr.is_null() { @@ -365,7 +368,7 @@ unsafe fn static_key_update( } let jump_entry = &*jump_entry_addr; // Not the same key - if key as *mut _ as usize != jump_entry.key_addr() { + if key as *const _ as usize != jump_entry.key_addr() { break; } diff --git a/tests/basic.rs b/tests/basic.rs index 920b1da..ad197c8 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,7 +1,6 @@ //! This test is designed to be run in single thread. Always pass `--test-threads=1`! #![feature(asm_goto)] -#![feature(asm_const)] use std::sync::Once;