Skip to content

Instantly share code, notes, and snippets.

@stphnt
Created July 19, 2023 06:40
Show Gist options
  • Save stphnt/6194deaa75603f658058e4eb7ae6f974 to your computer and use it in GitHub Desktop.
Save stphnt/6194deaa75603f658058e4eb7ae6f974 to your computer and use it in GitHub Desktop.
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