archmage 0.9.21

Safely invoke your intrinsic power, using the tokens granted to you by the CPU. Cast primitive magics faster than any mage alive.
Documentation
// SOUNDNESS EXPLOIT: Token aliasing bypasses CPU feature checks.
//
// This file renames a lower-tier token to a higher-tier name via `use ... as`.
// The #[arcane] macro does name-based feature lookup, so it generates
// #[target_feature(enable = "avx2,fma,...")] (V3 features) even though
// the actual token is X64V2Token (only proves SSE4.2 + POPCNT).
//
// On a CPU with SSE4.2 but no AVX2, calling `evil()` would be undefined behavior.
//
// THIS TEST COMPILING IS A BUG. When fixed, move to compile_fail/.

use archmage::{arcane, SimdToken};
use archmage::X64V2Token as X64V3Token;

#[arcane]
fn evil(token: X64V3Token, data: &[f32; 8]) -> f32 {
    // Macro sees "X64V3Token", generates #[target_feature(enable = "avx2,fma,...")]
    // But the token is actually X64V2Token — it only proves SSE4.2.
    // The unsafe wrapper trusts the name, not the type.
    data.iter().sum()
}

fn main() {
    // Summon a V2 token — only proves SSE4.2
    if let Some(token) = archmage::X64V2Token::summon() {
        // Pass it as "X64V3Token" (aliased) — compiles, but the CPU
        // may not have AVX2. The #[target_feature] wrapper is unsound.
        let _ = evil(token, &[1.0; 8]);
    }
}