Red: a speculated arm's load through a wide pointer reads outside a short binding
engine_hoist_contain gains a third kernel, `wide`, in two forms: a short arm beside a quad_broadcast_first, which canon's arm speculation runs in every lane of the branch's block, loads `k`, a constant reference (a 4-byte word, or an 8-byte pointer whose address the arm truncates), where a select joins k's pointer with one loaded from memory. That select makes k a wide pointer, so the arm's speculative load goes through the wide-pointer family, which checks no access: the load reads k's first bytes in every lane whatever k's binding. Two tests: - a_speculated_arms_load_through_a_wide_pointer_is_checked: on every committed profile under the identity domains, with and without the bound form's query, k's slot reads its SlotBounds and the module the zero range, and the arm's load is not a wide-pointer load (one access.dynamic.load, the select's own, and no access.dynamic.load_pointer.wide). - a_speculated_arms_load_through_a_wide_pointer_reads_nothing_outside_a_short_binding: on the CPU interpreter (lavapipe's address form), with the arm's condition off and k bound half as long as its load where its mapping ends, the load must not trap; with k whole or unbound, and with the condition on, out holds the program's words. Expected red at this commit: no SlotBounds for k's slot, and the interpreter's OutOfBounds trap at k's address in the short case of both forms. The unbound case is a control: the nil buffer's 64 KiB holds the load.
This commit is contained in:
1 parent
43b9dc279e
commit
0dcd3230e8
1 file changed
+248
-1
@@ -28,7 +28,15 @@
|
||||
//! every lane of the branch's block, and in the address form `k`'s slot reads its `SlotBounds` and
|
||||
//! the load reads through a selected address.
|
||||
//!
|
||||
//! `llvm-as` assembles the kernel; without it the tests skip visibly.
|
||||
//! A third kernel (`wide`, generated, in two forms) loads `k` in such an arm where a `select` joins
|
||||
//! `k`'s pointer with one loaded from memory, which makes it a wide pointer: the arm's load, of a
|
||||
//! word or of a pointer, goes through a wide pointer, and is checked against `k`'s slot as a
|
||||
//! static-root one is, on every committed profile, a bound-form one too (a slot a wide pointer
|
||||
//! reaches keeps its device address). On the CPU interpreter, lavapipe's address form, with the
|
||||
//! arm's condition off and `k` bound shorter than the load (its mapping ending there), the load
|
||||
//! reads nothing outside it.
|
||||
//!
|
||||
//! `llvm-as` assembles the kernels; without it the tests skip visibly.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
@@ -124,6 +132,58 @@ join:\n\
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
/// `kernel wide(device uint* out, constant K& k, device Src* src, uint t)`, with `Src` a flag word
|
||||
/// at byte 0 and a `constant uint*` at byte 8, and `K` a `uint`, or where `pointer` a `device
|
||||
/// uint*` (8 bytes): `constant uint* s = src->flag ? (constant uint*)&k : src->p; uint w = *s;
|
||||
/// uint r = 7777; if (src->flag) r = quad_broadcast_first(x + t); out[t] = r + w;`, `x` being
|
||||
/// `k`, or its pointer's address truncated to 32 bits. The `select` of `k`'s pointer with one
|
||||
/// loaded from memory makes `k` a wide pointer, and keeps the hoist away (another root shares it).
|
||||
fn wide_kernel(pointer: bool) -> String {
|
||||
let (bytes, arm) = match pointer {
|
||||
false => ("4", " %x = load i32, ptr addrspace(2) %1, align 4\n"),
|
||||
true => (
|
||||
"8",
|
||||
" %kp = load ptr addrspace(1), ptr addrspace(2) %1, align 8\n\
|
||||
%ki = ptrtoint ptr addrspace(1) %kp to i64\n\
|
||||
%x = trunc i64 %ki to i32\n",
|
||||
),
|
||||
};
|
||||
"target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32\"\n\
|
||||
target triple = \"air64_v28-apple-macosx26.0.0\"\n\n\
|
||||
declare i32 @air.quad_broadcast_first.u.i32(i32)\n\n\
|
||||
define void @wide(ptr addrspace(1) noundef \"air-buffer-no-alias\" %0, ptr addrspace(2) noundef align BYTES dereferenceable(BYTES) %1, ptr addrspace(1) noundef %2, <3 x i32> noundef %3) {\n\
|
||||
entry:\n\
|
||||
%t = extractelement <3 x i32> %3, i64 0\n\
|
||||
%te = zext i32 %t to i64\n\
|
||||
%q = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %te\n\
|
||||
%f = load i32, ptr addrspace(1) %2, align 8\n\
|
||||
%on = icmp ne i32 %f, 0\n\
|
||||
%pa = getelementptr inbounds i8, ptr addrspace(1) %2, i64 8\n\
|
||||
%pp = load ptr addrspace(2), ptr addrspace(1) %pa, align 8\n\
|
||||
%s = select i1 %on, ptr addrspace(2) %1, ptr addrspace(2) %pp\n\
|
||||
%w = load i32, ptr addrspace(2) %s, align 4\n\
|
||||
br i1 %on, label %arm, label %join\n\
|
||||
arm:\n\
|
||||
ARM %y = add i32 %x, %t\n\
|
||||
%z = call i32 @air.quad_broadcast_first.u.i32(i32 %y)\n\
|
||||
br label %join\n\
|
||||
join:\n\
|
||||
%v = phi i32 [ %z, %arm ], [ 7777, %entry ]\n\
|
||||
%o = add i32 %v, %w\n\
|
||||
store i32 %o, ptr addrspace(1) %q, align 4\n\
|
||||
ret void\n\
|
||||
}\n\n\
|
||||
!air.kernel = !{!9}\n!air.version = !{!17}\n!air.language_version = !{!18}\n\
|
||||
!9 = !{ptr @wide, !10, !11}\n!10 = !{}\n!11 = !{!12, !13, !14, !15}\n\
|
||||
!12 = !{i32 0, !\"air.buffer\", !\"air.location_index\", i32 0, i32 1, !\"air.read_write\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 4, !\"air.arg_type_align_size\", i32 4, !\"air.arg_type_name\", !\"uint\", !\"air.arg_name\", !\"out\"}\n\
|
||||
!13 = !{i32 1, !\"air.buffer\", !\"air.buffer_size\", i32 BYTES, !\"air.location_index\", i32 1, i32 1, !\"air.read\", !\"air.address_space\", i32 2, !\"air.arg_type_size\", i32 BYTES, !\"air.arg_type_align_size\", i32 BYTES, !\"air.arg_type_name\", !\"K\", !\"air.arg_name\", !\"k\"}\n\
|
||||
!14 = !{i32 2, !\"air.buffer\", !\"air.location_index\", i32 2, i32 1, !\"air.read\", !\"air.address_space\", i32 1, !\"air.arg_type_size\", i32 16, !\"air.arg_type_align_size\", i32 8, !\"air.arg_type_name\", !\"Src\", !\"air.arg_name\", !\"src\"}\n\
|
||||
!15 = !{i32 3, !\"air.thread_position_in_grid\", !\"air.arg_type_name\", !\"uint3\", !\"air.arg_name\", !\"tid\"}\n\
|
||||
!17 = !{i32 2, i32 8, i32 0}\n!18 = !{!\"Metal\", i32 4, i32 0, i32 0}\n"
|
||||
.replace("BYTES", bytes)
|
||||
.replace("ARM", arm)
|
||||
}
|
||||
|
||||
/// The hoist kernel assembled in a scratch directory of its own, removed once `llvm-as` is done.
|
||||
fn assemble() -> Option<Vec<u8>> {
|
||||
assemble_text("hoist", &hoist_kernel())
|
||||
@@ -371,3 +431,190 @@ fn a_speculated_arms_constant_reference_load_is_checked_in_the_address_form() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The `wide` kernel of each form: its name and its bitcode, `None` where `llvm-as` is missing.
|
||||
fn wide_kernels() -> Option<Vec<(&'static str, Vec<u8>)>> {
|
||||
[("wide, word", false), ("wide, pointer", true)]
|
||||
.into_iter()
|
||||
.map(|(what, pointer)| {
|
||||
let name = if pointer { "wide-pointer" } else { "wide-word" };
|
||||
assemble_text(name, &wide_kernel(pointer)).map(|bc| (what, bc))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A speculated arm's load of `k` through a wide pointer, of a word or of a pointer, is checked
|
||||
/// against `k`'s slot on every committed profile under the identity domains, with and without the
|
||||
/// bound form's query: `k`'s slot reads its `SlotBounds`, the module reads the zero range, and the
|
||||
/// load is the slot's static-root load (no load of the wide-pointer family but the `select`'s, and
|
||||
/// no pointer loaded through a wide pointer).
|
||||
#[test]
|
||||
fn a_speculated_arms_load_through_a_wide_pointer_is_checked() {
|
||||
let Some(kernels) = wide_kernels() else {
|
||||
return;
|
||||
};
|
||||
let profiles: Vec<(String, _)> = (identity_profiles().into_iter())
|
||||
.chain(
|
||||
(bound_identity_profiles().into_iter())
|
||||
.filter(|(_, p)| p.binds_kernel_buffers())
|
||||
.map(|(stem, p)| (format!("{stem}, bound form"), p)),
|
||||
)
|
||||
.collect();
|
||||
assert!(profiles.iter().any(|(_, p)| p.binds_kernel_buffers()));
|
||||
for (what, bc) in &kernels {
|
||||
for (stem, p) in &profiles {
|
||||
let at = format!("{what}, {stem}");
|
||||
let (a, plan, _) = compile_kept(bc, "wide", p).unwrap_or_else(|e| panic!("{at}: {e}"));
|
||||
link_and_validate(&a, p).unwrap_or_else(|e| panic!("{at}: L3: {e}"));
|
||||
let fields: BTreeSet<PushField> = plan.push.fields.iter().map(|f| f.field).collect();
|
||||
for f in [
|
||||
PushField::SlotGuestBase(slot(1)),
|
||||
PushField::SlotBounds(slot(1)),
|
||||
PushField::ZeroAddress,
|
||||
] {
|
||||
assert!(fields.contains(&f), "{at}: no {f:?}");
|
||||
}
|
||||
let census = plan.census();
|
||||
let uses = |name: &str| census.get(name).map_or(0, |(n, _)| *n);
|
||||
assert_eq!(uses("access.dynamic.load"), 1, "{at}: {census:#?}");
|
||||
assert_eq!(
|
||||
uses("access.dynamic.load_pointer.wide"),
|
||||
0,
|
||||
"{at}: {census:#?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How `k` is bound for a dispatch of the `wide` kernel.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum K {
|
||||
/// The whole object: `K_WORD` in its low word.
|
||||
Whole,
|
||||
/// Bound half as long as the load, where its mapping ends.
|
||||
Short,
|
||||
/// Unbound: the runtime's nil buffer and bounds of 0 and 0.
|
||||
Nil,
|
||||
}
|
||||
|
||||
/// `k`'s low word where it is whole, and the word `src`'s pointer points to.
|
||||
const K_WORD: u32 = 0x0000_1234;
|
||||
const W_WORD: u32 = 0x0005_0000;
|
||||
/// The addresses of `src`, `k` and the word `src` points to.
|
||||
const SRC: u64 = 0x1_0000_0000;
|
||||
const K_AT: u64 = 0x2_0000_0000;
|
||||
const W_AT: u64 = 0x4_0000_0000;
|
||||
|
||||
/// One dispatch of `wide` kernel `bc` (`k` of `bytes` bytes) on lavapipe's address form, with
|
||||
/// `src`'s flag `flag` and `k` bound as `k`: `out`'s words, or the trap the interpreter caught.
|
||||
fn dispatch_wide(bc: &[u8], bytes: usize, flag: u32, k: K) -> Result<Vec<u32>, String> {
|
||||
let (stem, p) = identity_profiles()
|
||||
.into_iter()
|
||||
.find(|(stem, _)| stem == "lavapipe")
|
||||
.expect("lavapipe");
|
||||
let (_, plan, module) = compile_kept(bc, "wide", &p).unwrap_or_else(|e| panic!("{e}"));
|
||||
let (k_at, k_bytes) = match k {
|
||||
K::Whole => {
|
||||
let mut b = vec![0u8; bytes];
|
||||
b[..4].copy_from_slice(&K_WORD.to_le_bytes());
|
||||
(K_AT, b)
|
||||
}
|
||||
K::Short => (K_AT, vec![0x5A; bytes / 2]),
|
||||
K::Nil => (NIL, vec![0; NIL_BYTES]),
|
||||
};
|
||||
let k_len = match k {
|
||||
K::Nil => 0,
|
||||
_ => k_bytes.len() as u32,
|
||||
};
|
||||
let out_bytes = 4 * THREADS;
|
||||
let mut push = vec![0u8; plan.push.push_range.map_or(0, |(_, end)| end as usize)];
|
||||
for f in &plan.push.fields {
|
||||
let PushLoc::Push { offset } = f.loc else {
|
||||
panic!("{stem}: {f:?} spilled");
|
||||
};
|
||||
let o = offset as usize;
|
||||
match f.field {
|
||||
PushField::SlotGuestBase(s) => {
|
||||
let at = [OUT, k_at, SRC][s.index as usize];
|
||||
push[o..o + 8].copy_from_slice(&slot_word(at).to_le_bytes());
|
||||
}
|
||||
PushField::SlotBounds(s) => {
|
||||
let length = [out_bytes, k_len, 16][s.index as usize];
|
||||
push[o..o + 4].copy_from_slice(&0u32.to_le_bytes());
|
||||
push[o + 4..o + 8].copy_from_slice(&length.to_le_bytes());
|
||||
}
|
||||
PushField::ZeroAddress => push[o..o + 8].copy_from_slice(&ZERO.to_le_bytes()),
|
||||
PushField::DispatchRegion => {
|
||||
let region = region_bytes([0; 3], [THREADS, 1, 1], [THREADS, 1, 1]).expect("packs");
|
||||
push[o..o + 32].copy_from_slice(®ion);
|
||||
}
|
||||
_ => panic!("{stem}: unexpected push field {f:?}"),
|
||||
}
|
||||
}
|
||||
let buffer = |address: u64, bytes: Vec<u8>| Buffer {
|
||||
slot: None,
|
||||
address: Some(address),
|
||||
bytes,
|
||||
};
|
||||
let mut src = flag.to_le_bytes().to_vec();
|
||||
src.extend([0; 4]);
|
||||
src.extend(W_AT.to_le_bytes());
|
||||
let out: Vec<u8> = (0..THREADS).flat_map(|_| SENTINEL.to_le_bytes()).collect();
|
||||
let res = Resources {
|
||||
buffers: vec![
|
||||
buffer(SRC, src),
|
||||
buffer(k_at, k_bytes),
|
||||
buffer(OUT, out),
|
||||
buffer(W_AT, W_WORD.to_le_bytes().to_vec()),
|
||||
buffer(ZERO, vec![0; 16]),
|
||||
],
|
||||
push,
|
||||
spec: BTreeMap::from(crate::pipeline::threadgroup_constants([THREADS, 1, 1])),
|
||||
};
|
||||
let got = run(&module, &Config::default(), res).map_err(|e| format!("{e:?}"))?;
|
||||
assert_eq!(got.buffers[4], vec![0; 16], "the zero range");
|
||||
Ok(got.buffers[2]
|
||||
.chunks(4)
|
||||
.map(|w| u32::from_le_bytes([w[0], w[1], w[2], w[3]]))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// On the CPU interpreter, with the arm's condition off, the speculated arm's load through a wide
|
||||
/// pointer reads nothing outside `k`'s binding, short or unbound, and `out` holds `7777` plus the
|
||||
/// word `src` points to; with it on and `k` whole, the arm reads `k` (twice its low word: the arm's
|
||||
/// and the `select`'s load).
|
||||
#[test]
|
||||
fn a_speculated_arms_load_through_a_wide_pointer_reads_nothing_outside_a_short_binding() {
|
||||
let Some(kernels) = wide_kernels() else {
|
||||
return;
|
||||
};
|
||||
let off = 7777 + W_WORD;
|
||||
let cases = [
|
||||
("flag on, k whole", 1, K::Whole, 2 * K_WORD),
|
||||
("flag off, k whole", 0, K::Whole, off),
|
||||
(
|
||||
"flag off, k bound half as long as its load",
|
||||
0,
|
||||
K::Short,
|
||||
off,
|
||||
),
|
||||
("flag off, k unbound", 0, K::Nil, off),
|
||||
];
|
||||
// Every case of every form runs, and each that fails is named.
|
||||
let mut failed = Vec::new();
|
||||
for (what, bc) in &kernels {
|
||||
let bytes = if what.ends_with("pointer") { 8 } else { 4 };
|
||||
for (case, flag, k, word) in cases {
|
||||
match dispatch_wide(bc, bytes, flag, k) {
|
||||
Err(trap) => failed.push(format!(
|
||||
"{what}, {case}: the arm's load read outside: {trap}"
|
||||
)),
|
||||
Ok(got) if got != vec![word; THREADS as usize] => {
|
||||
failed.push(format!("{what}, {case}: out {got:x?}"))
|
||||
}
|
||||
Ok(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(failed.is_empty(), "{failed:#?}");
|
||||
}
|
||||
Reference in new issue
Block a user