Created
July 19, 2023 06:40
-
-
Save stphnt/6194deaa75603f658058e4eb7ae6f974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use linkme::distributed_slice; | |
pub struct Item { | |
pub name: &'static str, | |
} | |
impl Item { | |
#[inline(never)] | |
fn len(&self) -> usize { | |
self.name.len() | |
} | |
} | |
#[distributed_slice] | |
pub static ITEMS: [Item] = [..]; | |
macro_rules! make_items { | |
($($name:ident = $str:literal),* $(,)?) => { | |
$( | |
#[distributed_slice(ITEMS)] | |
static $name: Item = Item { | |
name: $str, | |
}; | |
)* | |
} | |
} | |
make_items! { | |
ITEM1 = "item1", | |
ITEM2 = "item2", | |
ITEM3 = "item3", | |
} | |
#[test] | |
fn test_failure() { | |
let mut last_address = None; | |
for item in ITEMS { | |
// These 4 lines required to trigger the bug | |
let address = item as *const Item as usize; | |
if let Some(last) = last_address { | |
assert_eq!(address - last, std::mem::size_of::<Item>()); | |
} | |
last_address = Some(address); | |
println!("{} {}", item.len(), item.name); | |
// assert_eq!(item.len(), 5); // Causes STATUS_ILLEGAL_INSTRUCTION | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment