mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 13:30:43 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09cf27186c |
No files matched your search
@@ -13,6 +13,7 @@ remains unsupported.
|
||||
|
||||
- Added a native Metal blend pass, `NativeCompute::render_blend`: up to 16 flat-coloured triangles drawn in order into an RGBA8 target, each with its own colour write mask (any subset of R, G, B, A) and either blending disabled or separate RGB and alpha blend factors (Zero, One, source and destination colour and alpha, and their one-minus forms) and operations (Add, Subtract, ReverseSubtract, and Min and Max, which take the unweighted values). Checked on the GPU with Metal validation in 39 passes over two original overlapping triangles: a clear-only pass; blending disabled; each source factor with the destination term zeroed and each destination factor with the source term zeroed; every operation; additive clamping; separate alpha factors; a separate alpha operation; eight write masks (each channel alone, RGB, none, RGBA, and RG with blending); and a pass whose first draw is itself blended and masked. Colours are compared per channel against images derived independently (in exact fractions) from the public Metal blend equations, which a new CPU reference in `xenora-ref` also reproduces. The tolerance was fixed before measuring: channels whose exact result is a whole 8-bit value must match exactly, and channels that need rounding may differ by 1. On an Apple M5 Pro every rounded channel matched round-to-nearest exactly. Synthetic fixtures only; no console blend state is involved.
|
||||
- Added indexed triangle draws. Native Metal: `NativeCompute::render_indexed` issues one real `drawIndexedPrimitives` call per draw (up to 16) on a 16- or 32-bit index buffer with a start index and a signed base vertex, into an RGBA8 target; a draw can sample an original BC1 texture uploaded natively. Before encoding anything it rejects the whole pass for a bad index count, an index range past the buffer, any `index + base vertex` outside the vertex buffer, and, as this API's policy, a raw all-ones index (the primitive-restart value, which Metal does not define for triangle lists). Loader: a synthetic, CPU-side index buffer binding (resource handle and offset, the view running to the end of the resource) with `cmd_set_index_buffer` and `cmd_draw_indexed_instanced`, which fetch one triangle, drawn once, through the index buffer, start index and base vertex and pass it to the existing one-triangle backend; each drawn triangle replaces the previous output. Recording rejects formats other than DXGI `R16_UINT` and `R32_UINT`, offsets not aligned to the index size or past the resource, and unknown resources. Execution checks each draw in turn, so earlier draws have already rendered when a later one fails: index counts other than 0 or 3, more than one instance or a nonzero start instance, a missing index buffer, indices past the buffer and an `index + base vertex` outside the vertex buffer are typed errors; a valid zero index or instance count draws nothing. All-ones indices are ordinary indices there. Checked on the GPU with Metal validation in 16 passes (both index sizes, 32-bit indices above 16 bits, odd 16-bit start, positive and negative base vertex, reversed winding, flat and textured two-triangle draws, draw order, BC1-textured draws), compared per pixel against images derived independently from the geometry, the public D3D12 and Metal index semantics and the public BC1 rules, which a new CPU reference in `xenora-ref` also reproduces; the loader's fetch is checked against literal positions with synthetic guest-backed buffers and through the native Metal renderer. Synthetic fixtures only; no console index-buffer views, guest programs or console draw state are involved.
|
||||
- Added a `cpu_decode` cargo-fuzz target for the static x86-64 decoder used by the read-only PE probe. It places up to 4,096 fuzz bytes as the code section of a fixed, original PE32+ header and runs `probe_pe_bytes` twice with a bounded `ProbeConfig` (4,096 bytes, 2,048 instructions, 1,024 worklist entries); nothing is executed. It checks that the two reports are identical and that every decoded instruction is 1 to 15 bytes long, lies inside the code and matches the input bytes. A startup self-check aborts the run if the synthetic header stops reaching the decoder. A 600-second local campaign (nightly cargo-fuzz, one fork-mode worker, seeded from instruction bytes in the loader's probe tests; about 2.38 million executions, coverage 1,350 edges) found one decoder defect, reported to the CPU owner on #74 and not fixed here: the probe accepts unlimited FS/GS segment prefixes and reports instructions longer than 15 bytes (minimized input: 16 bytes of `0x65`). Run it with `rustup run nightly cargo fuzz run cpu_decode`; `scripts/fuzz.sh` does not list it yet.
|
||||
- Added a `pe_parse` cargo-fuzz target that feeds malformed bytes to the PE32+ parser (`ParsedPe::parse`, `rva_to_file_offset`) and, only when the summed section mapping size and `SizeOfImage` are both at most 64 MiB, to the checked guest-memory mapping step (`load_into_memory`); nothing is executed. A 600-second local campaign (nightly cargo-fuzz, fork mode, seeded from the repository's synthetic PE fixtures, about 120,000 executions, coverage 267 edges) found two loader panics, reported to the CPU owner and not fixed here: truncated optional headers panic on out-of-range slices in `parse`, and `rva_to_file_offset` overflows `u32` when a section's address plus size exceeds 4 GiB. Run it with `rustup run nightly cargo fuzz run pe_parse`; `scripts/fuzz.sh` does not list it yet.
|
||||
- Added `NativeCompute::run_words_with_group_width` and `max_group_width` to run a trusted kernel at an explicit threadgroup width, rejecting zero and widths above the device or pipeline limit. Native tests check 91 legal width and grid-size pairs (widths 1 to 1,024, including non-divisible grids) against independently computed outputs, and confirm the dispatched group sizes, including trimmed last groups. On an Apple M5 Pro, in three release runs of a 4,096-thread integer-mixing kernel (30 samples per width), widths 64, 128 and 256 were within 0.3 % of each other in two runs, at a median of 58.8 µs of GPU time. Width 512 was 5 to 6 % slower and width 1,024 about twice as slow in all three runs. The first run was about 4.7 times slower overall for reasons not determined, and in it 128 and 256 were 3 to 4 % faster than 64. The default width of 64 is unchanged. This was measured on one device and one workload.
|
||||
- Added a native Metal depth pass, `NativeCompute::render_depth`: up to 16 triangles drawn in order into an RGBA8 target with an optional `Depth32Float` attachment, a chosen clear depth, a per-draw depth compare function (all eight) and depth write enable. A pass without an attachment accepts only `DepthState::DISABLED`. Colour and the stored depth are both read back; the depth values are returned as the texture read gives them, without numeric conversion. A draw can sample an original BC1 texture whose blocks are uploaded to a native `BC1_RGBA` texture without CPU decoding, gated on `supportsBCTextureCompression`. Checked on the GPU with Metal validation in 30 passes of original overlapping triangles: clear, every compare function at three depths, write enabled and disabled, depth test disabled (`Always` without writes) with an attachment and no attachment at all, and a BC1-textured draw. Colour and depth are compared per pixel against expectations derived independently from the geometry, the Metal compare semantics and the public BC1 rules, which a new CPU reference in `xenora-ref` also reproduces. These are synthetic fixtures only; no console depth formats or guest render state are involved.
|
||||
|
||||
@@ -17,6 +17,12 @@ xenora-shader = { path = "../crates/shader" }
|
||||
|
||||
[workspace]
|
||||
|
||||
[[bin]]
|
||||
name = "cpu_decode"
|
||||
path = "fuzz_targets/cpu_decode.rs"
|
||||
test = false
|
||||
doc = false
|
||||
|
||||
[[bin]]
|
||||
name = "instruction_stream"
|
||||
path = "fuzz_targets/instruction_stream.rs"
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#![no_main]
|
||||
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
use std::collections::BTreeSet;
|
||||
use xenora_loader::probe::{ProbeConfig, probe_pe_bytes};
|
||||
|
||||
const MAX_CODE_BYTES: usize = 4096;
|
||||
const CONFIG: ProbeConfig = ProbeConfig {
|
||||
max_disasm_bytes: MAX_CODE_BYTES,
|
||||
max_instructions: 2048,
|
||||
max_worklist_entries: 1024,
|
||||
};
|
||||
|
||||
const PE_OFFSET: usize = 0x40;
|
||||
const COFF: usize = PE_OFFSET + 4;
|
||||
const OPTIONAL: usize = COFF + 20;
|
||||
const OPTIONAL_LEN: u16 = 0xf0;
|
||||
const TEXT: usize = OPTIONAL + OPTIONAL_LEN as usize;
|
||||
const HEADER_LEN: usize = 0x200;
|
||||
const TEXT_RVA: u32 = 0x1000;
|
||||
const SECTION_ALIGNMENT: u32 = 0x1000;
|
||||
|
||||
const HEADER: [u8; HEADER_LEN] = {
|
||||
let mut header = [0; HEADER_LEN];
|
||||
write_field(&mut header, 0, b"MZ");
|
||||
write_field(&mut header, 0x3c, &(PE_OFFSET as u32).to_le_bytes());
|
||||
write_field(&mut header, PE_OFFSET, b"PE\0\0");
|
||||
write_field(&mut header, COFF, &0x8664u16.to_le_bytes()); // Machine
|
||||
write_field(&mut header, COFF + 2, &1u16.to_le_bytes()); // NumberOfSections
|
||||
write_field(&mut header, COFF + 16, &OPTIONAL_LEN.to_le_bytes());
|
||||
write_field(&mut header, OPTIONAL, &0x20bu16.to_le_bytes()); // Magic
|
||||
write_field(&mut header, OPTIONAL + 16, &TEXT_RVA.to_le_bytes()); // AddressOfEntryPoint
|
||||
write_field(&mut header, OPTIONAL + 20, &TEXT_RVA.to_le_bytes()); // BaseOfCode
|
||||
write_field(&mut header, OPTIONAL + 24, &0x1_4000_0000u64.to_le_bytes()); // ImageBase
|
||||
write_field(&mut header, OPTIONAL + 32, &SECTION_ALIGNMENT.to_le_bytes());
|
||||
write_field(&mut header, OPTIONAL + 36, &0x200u32.to_le_bytes()); // FileAlignment
|
||||
write_field(
|
||||
&mut header,
|
||||
OPTIONAL + 60,
|
||||
&(HEADER_LEN as u32).to_le_bytes(),
|
||||
); // SizeOfHeaders
|
||||
write_field(&mut header, OPTIONAL + 108, &16u32.to_le_bytes()); // NumberOfRvaAndSizes
|
||||
write_field(&mut header, TEXT, b".text");
|
||||
write_field(&mut header, TEXT + 12, &TEXT_RVA.to_le_bytes()); // VirtualAddress
|
||||
write_field(&mut header, TEXT + 20, &(HEADER_LEN as u32).to_le_bytes()); // PointerToRawData
|
||||
write_field(&mut header, TEXT + 36, &0x6000_0020u32.to_le_bytes()); // CODE | EXECUTE | READ
|
||||
header
|
||||
};
|
||||
|
||||
const fn write_field(header: &mut [u8], offset: usize, bytes: &[u8]) {
|
||||
let (_, tail) = header.split_at_mut(offset);
|
||||
let (field, _) = tail.split_at_mut(bytes.len());
|
||||
field.copy_from_slice(bytes);
|
||||
}
|
||||
|
||||
fn build_image(payload: &[u8]) -> Vec<u8> {
|
||||
let mut header = HEADER;
|
||||
let code_size = payload.len() as u32;
|
||||
let image_size = TEXT_RVA + code_size.next_multiple_of(SECTION_ALIGNMENT);
|
||||
write_field(&mut header, OPTIONAL + 4, &code_size.to_le_bytes()); // SizeOfCode
|
||||
write_field(&mut header, OPTIONAL + 56, &image_size.to_le_bytes()); // SizeOfImage
|
||||
write_field(&mut header, TEXT + 8, &code_size.to_le_bytes()); // VirtualSize
|
||||
write_field(&mut header, TEXT + 16, &code_size.to_le_bytes()); // SizeOfRawData
|
||||
|
||||
// The probe decodes up to EOF, so leave the payload unpadded.
|
||||
let mut image = Vec::with_capacity(HEADER_LEN + payload.len());
|
||||
image.extend_from_slice(&header);
|
||||
image.extend_from_slice(payload);
|
||||
image
|
||||
}
|
||||
|
||||
fuzz_target!(init: {
|
||||
let image = build_image(&[0x90, 0xc3]);
|
||||
let report = probe_pe_bytes(&image, &CONFIG).expect("startup PE probe succeeds");
|
||||
let records = report.instruction_records;
|
||||
assert!(records.len() == 2, "startup decodes two instructions");
|
||||
assert!(records[0].rva == TEXT_RVA, "startup decodes entry RVA");
|
||||
assert!(records[1].rva == TEXT_RVA + 1, "startup decodes next RVA");
|
||||
}, |data: &[u8]| {
|
||||
let payload = &data[..data.len().min(MAX_CODE_BYTES)];
|
||||
let image = build_image(payload);
|
||||
let a = probe_pe_bytes(&image, &CONFIG);
|
||||
let b = probe_pe_bytes(&image, &CONFIG);
|
||||
assert!(format!("{a:?}") == format!("{b:?}"), "probe is deterministic");
|
||||
|
||||
if let Ok(report) = a {
|
||||
let records = report.instruction_records;
|
||||
assert!(records.len() <= CONFIG.max_instructions, "instruction budget");
|
||||
let code_range = TEXT_RVA..TEXT_RVA + payload.len() as u32;
|
||||
let mut rvas = BTreeSet::new();
|
||||
for record in records {
|
||||
let len = record.bytes.len();
|
||||
assert!((1..=15).contains(&len), "x86 instruction length");
|
||||
assert!(code_range.contains(&record.rva), "RVA within payload");
|
||||
assert!(rvas.insert(record.rva), "RVA is unique");
|
||||
let start = (record.rva - TEXT_RVA) as usize;
|
||||
let end = start + len;
|
||||
assert!(end <= payload.len(), "instruction ends within payload");
|
||||
assert!(record.bytes == payload[start..end], "instruction bytes match payload");
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user