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 shadowing bypasses CPU feature checks.
//
// This file creates a local struct with the same name as an archmage token.
// The #[arcane] macro does name-based feature lookup, so it generates
// #[target_feature(enable = "avx2,fma,...")] for this impostor.
// The unsafe trampoline executes without any CPU feature proof.
//
// On a CPU without AVX2, calling `evil()` would be undefined behavior.
//
// THIS TEST COMPILING IS A BUG. When fixed, move to compile_fail/.

use archmage::arcane;

// Shadow the real X64V3Token with a local imposter
#[derive(Clone, Copy)]
struct X64V3Token;

#[arcane]
fn evil(_token: X64V3Token, data: &[f32; 8]) -> f32 {
    // Macro sees "X64V3Token", generates #[target_feature(enable = "avx2,fma,...")]
    // But our fake token has no CPU check — the unsafe wrapper is unsound.
    data.iter().sum()
}

fn main() {
    // Construct the fake token trivially — no summon(), no CPU check.
    let fake = X64V3Token;
    // This call compiles and runs. On a non-AVX2 CPU, the #[target_feature]
    // function would execute AVX2 instructions → undefined behavior.
    let _ = evil(fake, &[1.0; 8]);
}