Skip to content

Commit

Permalink
Fix #5: Get ready for Rust 2024 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
Evian-Zhang committed Sep 22, 2024
1 parent 5baeb83 commit 213a5ca
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
65 changes: 33 additions & 32 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
4 changes: 1 addition & 3 deletions docs/en/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down Expand Up @@ -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 } }
Expand Down
4 changes: 1 addition & 3 deletions docs/zh-Hans/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`中加入相应依赖:
Expand Down Expand Up @@ -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 } }
Expand Down
1 change: 0 additions & 1 deletion examples/usage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(asm_goto)]
#![feature(asm_const)]

use static_keys::{define_static_key_false, static_branch_unlikely};

Expand Down
27 changes: 15 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -107,7 +106,10 @@ impl JumpEntry {
/// to assign arbitrary status to the static key generic when using.
pub struct GenericStaticKey<M: CodeManipulator, const S: bool> {
/// 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]
Expand Down Expand Up @@ -138,7 +140,7 @@ pub type StaticFalseKey = StaticKey<false>;
// 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<code_manipulate::DummyCodeManipulator, false> =
static DUMMY_STATIC_KEY: GenericStaticKey<code_manipulate::DummyCodeManipulator, false> =
GenericStaticKey::new(false);

impl<M: CodeManipulator, const S: bool> GenericStaticKey<M, S> {
Expand All @@ -151,7 +153,7 @@ impl<M: CodeManipulator, const S: bool> GenericStaticKey<M, S> {
/// 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,
}
Expand All @@ -170,7 +172,7 @@ impl<M: CodeManipulator, const S: bool> GenericStaticKey<M, S> {
/// 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)
}

Expand All @@ -182,7 +184,7 @@ impl<M: CodeManipulator, const S: bool> GenericStaticKey<M, S> {
/// 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)
}
}
Expand Down Expand Up @@ -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();
};
}

Expand All @@ -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();
};
}

Expand All @@ -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<M: CodeManipulator, const S: bool>(
key: &mut GenericStaticKey<M, S>,
key: &GenericStaticKey<M, S>,
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() {
Expand All @@ -365,7 +368,7 @@ unsafe fn static_key_update<M: CodeManipulator, const S: bool>(
}
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;
}

Expand Down
1 change: 0 additions & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 213a5ca

Please sign in to comment.