static_keys/
code_manipulate.rs

1//! Utilities to manipulate memory protection.
2//!
3//! Since we need to make the code region writable and restore it during jump entry update,
4//! we need to provide utility functions here.
5
6/// Manipulate memory protection in code region.
7pub trait CodeManipulator {
8    /// Write `data` as code instruction to `addr`.
9    ///
10    /// The `addr` is not aligned, you need to align it you self. The length is not too long, usually
11    /// 5 bytes.
12    ///
13    /// # Safety
14    ///
15    /// This method will do best effort to make the code region writable, and then write the data into
16    /// the code region. If the code region is still not writable, the data writing will become a UB.
17    /// Never call this method when there are multi-threads running. Spawn threads after this method
18    /// is called. This method may manipulate code region memory protection, and if other threads are
19    /// executing codes in the same code page, it may lead to unexpected behaviors.
20    unsafe fn write_code<const L: usize>(addr: *mut core::ffi::c_void, data: &[u8; L]);
21}
22
23/// Dummy code manipulator. Do nothing. Used to declare a dummy static key which is never modified
24pub(crate) struct DummyCodeManipulator;
25
26impl CodeManipulator for DummyCodeManipulator {
27    unsafe fn write_code<const L: usize>(_addr: *mut core::ffi::c_void, _data: &[u8; L]) {}
28}