mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 21:18:38 +00:00
Per maintainer feedback: the real blob is a DXBC container whose RTS0
chunk starts version, numParameters, parametersOffset, numStaticSamplers,
staticSamplersOffset, flags, with parameters as a {type, visibility,
offset} table and payloads at their offsets (d3d types follow
D3D12_ROOT_PARAMETER_TYPE numbering, not the model's enum order).
Also fixes the oracle segfault mechanism: return early when parsing fails
instead of indexing an empty model, and bounds every offset addition in
size_t so hostile containers cannot wrap the guard into an OOB read.
Adds a wild-offset control plus a DXBC-walk test (19 portable checks green
under ASan+UBSan).
222 lines
8.2 KiB
C++
222 lines
8.2 KiB
C++
// Portable tests for runtime/src/root_signature.{h,cpp} (no windows.h).
|
|
//
|
|
// Own main + PASS/FAIL lines like the other runtime tests; returns nonzero
|
|
// on any failure so CTest gates on it. Everything here is hand-built: no
|
|
// game data, no DXBC, no serializer needed.
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <vector>
|
|
|
|
#include "root_signature.h"
|
|
|
|
namespace {
|
|
|
|
int g_failures = 0;
|
|
|
|
void check(bool condition, const char* what) {
|
|
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
|
|
if (!condition) ++g_failures;
|
|
}
|
|
|
|
rt::rootsig::RootParameter consts(uint32_t n) {
|
|
rt::rootsig::RootParameter p;
|
|
p.type = rt::rootsig::ParamType::Constants;
|
|
p.num_values = n;
|
|
return p;
|
|
}
|
|
|
|
rt::rootsig::RootParameter view() {
|
|
rt::rootsig::RootParameter p;
|
|
p.type = rt::rootsig::ParamType::Descriptor;
|
|
return p;
|
|
}
|
|
|
|
rt::rootsig::RootParameter table() {
|
|
rt::rootsig::RootParameter p;
|
|
p.type = rt::rootsig::ParamType::Table;
|
|
p.ranges.push_back({0, 8});
|
|
return p;
|
|
}
|
|
|
|
void put_u32(std::vector<uint8_t>& out, uint32_t v) {
|
|
out.push_back(static_cast<uint8_t>(v));
|
|
out.push_back(static_cast<uint8_t>(v >> 8));
|
|
out.push_back(static_cast<uint8_t>(v >> 16));
|
|
out.push_back(static_cast<uint8_t>(v >> 24));
|
|
}
|
|
|
|
// Hand-built RTS0 payload in the offset-table layout the maintainer
|
|
// described: header, parameter table, then payloads at their offsets.
|
|
std::vector<uint8_t> build_payload() {
|
|
std::vector<uint8_t> p;
|
|
put_u32(p, 2); // version 1.1
|
|
put_u32(p, 2); // 2 params
|
|
put_u32(p, 24); // params table offset
|
|
put_u32(p, 0); // 0 samplers
|
|
put_u32(p, 0); // samplers offset
|
|
put_u32(p, 0x03000000u); // Xbox flags
|
|
// entry 0: constants at 48; entry 1: CBV descriptor at 60.
|
|
put_u32(p, 1);
|
|
put_u32(p, 0);
|
|
put_u32(p, 48);
|
|
put_u32(p, 2);
|
|
put_u32(p, 4);
|
|
put_u32(p, 60);
|
|
put_u32(p, 7); // register (ignored by packer)
|
|
put_u32(p, 0); // space
|
|
put_u32(p, 3); // 3 values
|
|
put_u32(p, 0); // register
|
|
put_u32(p, 0); // space
|
|
put_u32(p, 0); // 1.1 descriptor flags
|
|
return p;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
using namespace rt::rootsig;
|
|
|
|
// Mixed signature: 4 constants + CBV + table + 1 static sampler.
|
|
{
|
|
RootSignature sig;
|
|
sig.params = {consts(4), view(), table()};
|
|
StaticSampler s;
|
|
s.shader_register = 3;
|
|
sig.static_samplers = {s};
|
|
const PackResult r = pack(sig);
|
|
check(r.ok, "mixed signature packs");
|
|
check(r.dword_count == 8, "mixed signature uses 8 dwords");
|
|
check(r.d3d12_cost == 4 + 2 + 1, "mixed signature costs 7");
|
|
check(r.provenance.size() == 8 && r.provenance[0].param_index == 0 &&
|
|
r.provenance[0].sub_index == 0 && r.provenance[4].param_index == 1 &&
|
|
r.provenance[4].sub_index == 0 && r.provenance[5].sub_index == 1 &&
|
|
r.provenance[6].param_index == 2,
|
|
"provenance tracks param and sub-dword");
|
|
}
|
|
|
|
// Exactly 32 dwords still fits.
|
|
{
|
|
RootSignature sig;
|
|
for (int i = 0; i < 16; ++i) sig.params.push_back(view());
|
|
const PackResult r = pack(sig);
|
|
check(r.ok && r.dword_count == 32, "16 views fill 32 dwords");
|
|
}
|
|
|
|
// 34 dwords: spill is refused loudly, never truncated.
|
|
{
|
|
RootSignature sig;
|
|
for (int i = 0; i < 17; ++i) sig.params.push_back(view());
|
|
const PackResult r = pack(sig);
|
|
check(!r.ok && r.error.find("spill") != std::string::npos,
|
|
"17 views refused as spill");
|
|
}
|
|
|
|
// 65 constants exceed the 64-dword D3D12 cost limit.
|
|
{
|
|
RootSignature sig;
|
|
sig.params = {consts(65)};
|
|
const PackResult r = pack(sig);
|
|
check(!r.ok && r.error.find("64-dword") != std::string::npos,
|
|
"65 constants refused by cost limit");
|
|
}
|
|
|
|
// Canonical codec round-trips byte-exactly, sampler included.
|
|
{
|
|
RootSignature sig;
|
|
sig.version = Version::V1_0;
|
|
sig.params = {consts(2), table()};
|
|
sig.flags = 0x03000000u; // Xbox high byte survives the model
|
|
StaticSampler s;
|
|
s.filter = 21;
|
|
s.mip_lod_bias = 0.5f;
|
|
sig.static_samplers = {s};
|
|
const std::vector<uint8_t> bytes = encode_canonical(sig);
|
|
const DecodeResult d = decode_canonical(bytes.data(), bytes.size());
|
|
check(d.ok, "canonical round-trip decodes");
|
|
check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 2 &&
|
|
d.sig.params[1].ranges.size() == 1 &&
|
|
d.sig.static_samplers.size() == 1 && d.sig.flags == 0x03000000u &&
|
|
d.sig.static_samplers[0].filter == 21,
|
|
"canonical round-trip preserves model");
|
|
}
|
|
|
|
// Canonical decoder controls: bad magic, truncation, trailing bytes.
|
|
{
|
|
const std::vector<uint8_t> bad = {0, 1, 2, 3};
|
|
check(!decode_canonical(bad.data(), bad.size()).ok, "bad magic rejected");
|
|
RootSignature sig;
|
|
sig.params = {consts(1)};
|
|
const std::vector<uint8_t> bytes = encode_canonical(sig);
|
|
check(!decode_canonical(bytes.data(), bytes.size() - 1).ok,
|
|
"truncated canonical rejected");
|
|
std::vector<uint8_t> trailed = bytes;
|
|
trailed.push_back(0);
|
|
check(!decode_canonical(trailed.data(), trailed.size()).ok,
|
|
"trailing bytes rejected");
|
|
}
|
|
|
|
// RTS0 hypothesis: a hand-built blob per the documented layout parses.
|
|
// Hand-built RTS0 payload in the offset-table layout the maintainer
|
|
// described: header, parameter table, then payloads at their offsets.
|
|
{
|
|
const std::vector<uint8_t> payload = build_payload();
|
|
const DecodeResult d = parse_rts0(payload.data(), payload.size());
|
|
check(d.ok, "hand-built RTS0 parses");
|
|
check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3 &&
|
|
d.sig.flags == 0x03000000u,
|
|
"RTS0 model matches hand-built blob");
|
|
const PackResult r = pack(d.sig);
|
|
check(r.ok && r.dword_count == 3 + 2, "parsed RTS0 packs to 5 dwords");
|
|
}
|
|
|
|
// RTS0 controls: unknown version, wild offsets and truncation fail loudly.
|
|
{
|
|
std::vector<uint8_t> blob;
|
|
put_u32(blob, 9); // unknown version
|
|
put_u32(blob, 0);
|
|
put_u32(blob, 0);
|
|
put_u32(blob, 0);
|
|
put_u32(blob, 0);
|
|
put_u32(blob, 0);
|
|
const DecodeResult d = parse_rts0(blob.data(), blob.size());
|
|
check(!d.ok, "unknown RTS0 version rejected");
|
|
std::vector<uint8_t> cut = {1, 0};
|
|
check(!parse_rts0(cut.data(), cut.size()).ok, "truncated RTS0 rejected");
|
|
std::vector<uint8_t> wild = build_payload();
|
|
// Corrupt the params offset to point past the end.
|
|
wild[8] = 0xFF;
|
|
wild[9] = 0xFF;
|
|
wild[10] = 0xFF;
|
|
wild[11] = 0xFF;
|
|
check(!parse_rts0(wild.data(), wild.size()).ok, "wild offset rejected");
|
|
}
|
|
|
|
// DXBC container walk: a hand-built container with one RTS0 chunk parses
|
|
// through the chunk table (same path the MS serializer blob takes).
|
|
{
|
|
const std::vector<uint8_t> payload = build_payload();
|
|
std::vector<uint8_t> blob;
|
|
blob.insert(blob.end(), {'D', 'X', 'B', 'C'});
|
|
blob.insert(blob.end(), 16, 0); // hash
|
|
put_u32(blob, 1); // version
|
|
put_u32(blob, 0); // file size (patched below)
|
|
put_u32(blob, 1); // 1 chunk
|
|
put_u32(blob, 36); // chunk offset: 4 magic + 16 hash + 4 ver + 4 size + 4 count + 4 offset
|
|
blob.insert(blob.end(), {'R', 'T', 'S', '0'});
|
|
put_u32(blob, static_cast<uint32_t>(payload.size()));
|
|
blob.insert(blob.end(), payload.begin(), payload.end());
|
|
const uint32_t total = static_cast<uint32_t>(blob.size());
|
|
blob[24] = static_cast<uint8_t>(total);
|
|
blob[25] = static_cast<uint8_t>(total >> 8);
|
|
blob[26] = static_cast<uint8_t>(total >> 16);
|
|
blob[27] = static_cast<uint8_t>(total >> 24);
|
|
const DecodeResult d = parse_rts0(blob.data(), blob.size());
|
|
check(d.ok, "DXBC container walk finds RTS0");
|
|
check(d.sig.params.size() == 2 && d.sig.params[0].num_values == 3,
|
|
"walked RTS0 model matches payload");
|
|
}
|
|
|
|
if (g_failures != 0) std::printf("%d FAILURES\n", g_failures);
|
|
return g_failures == 0 ? 0 : 1;
|
|
}
|