diff options
| author | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-21 17:40:40 -0300 |
|---|---|---|
| committer | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-21 17:40:40 -0300 |
| commit | 71d39cefb9b513364bdbbfce75210d8f895596a2 (patch) | |
| tree | 4fd88d42c98731729db7c7cfc326dd0e83ac1b59 /runtime/src/root_signature.cpp | |
| parent | 87989cea5f1080dac729d5407aa3fa19cb3f8fd3 (diff) | |
Rework RTS0 parser to offset-table layout; harden oracle test
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).
Diffstat (limited to 'runtime/src/root_signature.cpp')
| -rw-r--r-- | runtime/src/root_signature.cpp | 133 |
1 files changed, 100 insertions, 33 deletions
diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp index 99446c155..79a633b61 100644 --- a/runtime/src/root_signature.cpp +++ b/runtime/src/root_signature.cpp | |||
| @@ -228,13 +228,39 @@ DecodeResult decode_canonical(const uint8_t* data, size_t size) { | |||
| 228 | // Payload decoder: the RTS0 chunk contents (see header for the hypothesis). | 228 | // Payload decoder: the RTS0 chunk contents (see header for the hypothesis). |
| 229 | static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { | 229 | static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { |
| 230 | DecodeResult out; | 230 | DecodeResult out; |
| 231 | Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size); | ||
| 232 | uint32_t ver = 0, nparams = 0, nsamplers = 0, flags = 0; | ||
| 233 | if (data == nullptr && size != 0) { | 231 | if (data == nullptr && size != 0) { |
| 234 | out.error = "null data with nonzero size"; | 232 | out.error = "null data with nonzero size"; |
| 235 | return out; | 233 | return out; |
| 236 | } | 234 | } |
| 237 | if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(flags)) { | 235 | if (data == nullptr) { |
| 236 | out.error = "null RTS0 payload"; | ||
| 237 | return out; | ||
| 238 | } | ||
| 239 | // Random access into the payload: every (offset, byte-count) pair is | ||
| 240 | // validated before any byte is touched. Offsets count from payload start. | ||
| 241 | auto at = [&](size_t off, size_t bytes) -> const uint8_t* { | ||
| 242 | if (bytes > size || off > size - bytes) return nullptr; | ||
| 243 | return data + off; | ||
| 244 | }; | ||
| 245 | auto u32_at = [&](size_t off, uint32_t& v) { | ||
| 246 | const uint8_t* p = at(off, 4); | ||
| 247 | if (p == nullptr) return false; | ||
| 248 | v = static_cast<uint32_t>(p[0]) | (static_cast<uint32_t>(p[1]) << 8) | | ||
| 249 | (static_cast<uint32_t>(p[2]) << 16) | (static_cast<uint32_t>(p[3]) << 24); | ||
| 250 | return true; | ||
| 251 | }; | ||
| 252 | auto f32_at = [&](size_t off, float& v) { | ||
| 253 | uint32_t bits = 0; | ||
| 254 | if (!u32_at(off, bits)) return false; | ||
| 255 | static_assert(sizeof(bits) == sizeof(v)); | ||
| 256 | std::memcpy(&v, &bits, sizeof(v)); | ||
| 257 | return true; | ||
| 258 | }; | ||
| 259 | // Header: version, num_params, params_offset, num_samplers, | ||
| 260 | // samplers_offset, flags. | ||
| 261 | uint32_t ver = 0, nparams = 0, params_off = 0, nsamplers = 0, samplers_off = 0, flags = 0; | ||
| 262 | if (!u32_at(0, ver) || !u32_at(4, nparams) || !u32_at(8, params_off) || | ||
| 263 | !u32_at(12, nsamplers) || !u32_at(16, samplers_off) || !u32_at(20, flags)) { | ||
| 238 | out.error = "truncated RTS0 header"; | 264 | out.error = "truncated RTS0 header"; |
| 239 | return out; | 265 | return out; |
| 240 | } | 266 | } |
| @@ -252,75 +278,116 @@ static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) { | |||
| 252 | } | 278 | } |
| 253 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; | 279 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; |
| 254 | out.sig.flags = flags; | 280 | out.sig.flags = flags; |
| 281 | // Parameter table at params_off: num_params x {u32 d3d_type, u32 | ||
| 282 | // visibility, u32 offset}. d3d_type follows D3D12_ROOT_PARAMETER_TYPE | ||
| 283 | // (0 table, 1 constants, 2/3/4 CBV/SRV/UAV root descriptors). | ||
| 284 | constexpr size_t kEntryBytes = 12; | ||
| 285 | if (nparams > 0 && | ||
| 286 | (params_off > size || nparams > (size - params_off) / kEntryBytes)) { | ||
| 287 | out.error = "RTS0 parameter table overruns its payload"; | ||
| 288 | return out; | ||
| 289 | } | ||
| 255 | out.sig.params.reserve(nparams); | 290 | out.sig.params.reserve(nparams); |
| 256 | for (uint32_t i = 0; i < nparams; ++i) { | 291 | for (uint32_t i = 0; i < nparams; ++i) { |
| 257 | RootParameter p; | 292 | const size_t eoff = static_cast<size_t>(params_off) + i * kEntryBytes; |
| 258 | uint32_t type = 0, vis = 0; | 293 | uint32_t dtype = 0, vis = 0, poff = 0; |
| 259 | if (!r.u32(type) || !r.u32(vis)) { | 294 | if (!u32_at(eoff, dtype) || !u32_at(eoff + 4, vis) || !u32_at(eoff + 8, poff)) { |
| 260 | out.error = "truncated RTS0 parameter"; | 295 | out.error = "truncated RTS0 parameter entry"; |
| 261 | return out; | ||
| 262 | } | ||
| 263 | if (type > 2) { | ||
| 264 | out.error = "unknown RTS0 parameter type"; | ||
| 265 | return out; | 296 | return out; |
| 266 | } | 297 | } |
| 267 | p.type = static_cast<ParamType>(type); | 298 | RootParameter p; |
| 268 | p.visibility = static_cast<Visibility>(vis); | 299 | p.visibility = static_cast<Visibility>(vis); |
| 269 | if (p.type == ParamType::Constants) { | 300 | if (dtype == 1) { |
| 270 | if (!r.u32(p.shader_register) || !r.u32(p.register_space) || !r.u32(p.num_values)) { | 301 | p.type = ParamType::Constants; |
| 271 | out.error = "truncated RTS0 constants"; | 302 | if (!u32_at(poff, p.shader_register) || !u32_at(poff + 4, p.register_space) || |
| 303 | !u32_at(poff + 8, p.num_values)) { | ||
| 304 | out.error = "truncated RTS0 constants payload"; | ||
| 272 | return out; | 305 | return out; |
| 273 | } | 306 | } |
| 274 | } else if (p.type == ParamType::Descriptor) { | 307 | } else if (dtype == 2 || dtype == 3 || dtype == 4) { |
| 275 | if (!r.u32(p.descriptor_register) || !r.u32(p.descriptor_space)) { | 308 | p.type = ParamType::Descriptor; |
| 276 | out.error = "truncated RTS0 descriptor"; | 309 | uint32_t dflags = 0; |
| 310 | const size_t need = ver == 2 ? 12 : 8; | ||
| 311 | if (poff > size || need > size - static_cast<size_t>(poff)) { | ||
| 312 | out.error = "truncated RTS0 descriptor payload"; | ||
| 277 | return out; | 313 | return out; |
| 278 | } | 314 | } |
| 279 | } else { | 315 | if (!u32_at(poff, p.descriptor_register) || !u32_at(poff + 4, p.descriptor_space)) { |
| 316 | out.error = "truncated RTS0 descriptor payload"; | ||
| 317 | return out; | ||
| 318 | } | ||
| 319 | if (ver == 2 && !u32_at(poff + 8, dflags)) { | ||
| 320 | out.error = "truncated RTS0 descriptor flags"; | ||
| 321 | return out; | ||
| 322 | } | ||
| 323 | (void)dflags; // descriptor flags do not affect the user-SGPR map | ||
| 324 | } else if (dtype == 0) { | ||
| 325 | p.type = ParamType::Table; | ||
| 280 | uint32_t nranges = 0; | 326 | uint32_t nranges = 0; |
| 281 | if (!r.u32(nranges)) { | 327 | if (!u32_at(poff, nranges)) { |
| 282 | out.error = "truncated RTS0 table"; | 328 | out.error = "truncated RTS0 table payload"; |
| 283 | return out; | 329 | return out; |
| 284 | } | 330 | } |
| 285 | if (nranges > 1024) { | 331 | if (nranges > 1024) { |
| 286 | out.error = "absurd RTS0 range count"; | 332 | out.error = "absurd RTS0 range count"; |
| 287 | return out; | 333 | return out; |
| 288 | } | 334 | } |
| 335 | // Range record: {type, num, base_reg, space, offset} (+ flags on 1.1). | ||
| 336 | const size_t stride = ver == 2 ? 24 : 20; | ||
| 337 | if (nranges > 0 && | ||
| 338 | (poff + 4 > size || nranges > (size - (poff + 4)) / stride)) { | ||
| 339 | out.error = "RTS0 ranges overrun their payload"; | ||
| 340 | return out; | ||
| 341 | } | ||
| 289 | p.ranges.reserve(nranges); | 342 | p.ranges.reserve(nranges); |
| 290 | for (uint32_t j = 0; j < nranges; ++j) { | 343 | for (uint32_t j = 0; j < nranges; ++j) { |
| 344 | const size_t roff = static_cast<size_t>(poff) + 4 + j * stride; | ||
| 291 | DescriptorRange range; | 345 | DescriptorRange range; |
| 292 | uint32_t base_reg = 0, space = 0, offset = 0; | 346 | uint32_t base_reg = 0, space = 0, disp_off = 0, rflags = 0; |
| 293 | if (!r.u32(range.range_type) || !r.u32(range.num_descriptors) || !r.u32(base_reg) || | 347 | if (!u32_at(roff, range.range_type) || !u32_at(roff + 4, range.num_descriptors) || |
| 294 | !r.u32(space) || !r.u32(offset)) { | 348 | !u32_at(roff + 8, base_reg) || !u32_at(roff + 12, space) || |
| 349 | !u32_at(roff + 16, disp_off) || | ||
| 350 | (ver == 2 && !u32_at(roff + 20, rflags))) { | ||
| 295 | out.error = "truncated RTS0 range"; | 351 | out.error = "truncated RTS0 range"; |
| 296 | return out; | 352 | return out; |
| 297 | } | 353 | } |
| 298 | (void)base_reg; | 354 | (void)base_reg; |
| 299 | (void)space; | 355 | (void)space; |
| 300 | (void)offset; | 356 | (void)disp_off; |
| 357 | (void)rflags; | ||
| 301 | p.ranges.push_back(range); | 358 | p.ranges.push_back(range); |
| 302 | } | 359 | } |
| 360 | } else { | ||
| 361 | out.error = "unknown RTS0 parameter type"; | ||
| 362 | return out; | ||
| 303 | } | 363 | } |
| 304 | out.sig.params.push_back(std::move(p)); | 364 | out.sig.params.push_back(std::move(p)); |
| 305 | } | 365 | } |
| 366 | // Sampler records mirror D3D12_STATIC_SAMPLER_DESC field order. | ||
| 367 | constexpr size_t kSamplerBytes = 13 * 4; | ||
| 368 | if (nsamplers > 0 && | ||
| 369 | (samplers_off > size || nsamplers > (size - samplers_off) / kSamplerBytes)) { | ||
| 370 | out.error = "RTS0 samplers overrun their payload"; | ||
| 371 | return out; | ||
| 372 | } | ||
| 306 | out.sig.static_samplers.reserve(nsamplers); | 373 | out.sig.static_samplers.reserve(nsamplers); |
| 307 | for (uint32_t i = 0; i < nsamplers; ++i) { | 374 | for (uint32_t i = 0; i < nsamplers; ++i) { |
| 375 | const size_t soff = static_cast<size_t>(samplers_off) + i * kSamplerBytes; | ||
| 308 | StaticSampler s; | 376 | StaticSampler s; |
| 309 | uint32_t vis = 0; | 377 | uint32_t vis = 0; |
| 310 | if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) || | 378 | if (!u32_at(soff, s.filter) || !u32_at(soff + 4, s.address_u) || |
| 311 | !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) || | 379 | !u32_at(soff + 8, s.address_v) || !u32_at(soff + 12, s.address_w) || |
| 312 | !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) || | 380 | !f32_at(soff + 16, s.mip_lod_bias) || !u32_at(soff + 20, s.max_anisotropy) || |
| 313 | !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) { | 381 | !u32_at(soff + 24, s.comparison_func) || !u32_at(soff + 28, s.border_color) || |
| 382 | !f32_at(soff + 32, s.min_lod) || !f32_at(soff + 36, s.max_lod) || | ||
| 383 | !u32_at(soff + 40, s.shader_register) || !u32_at(soff + 44, s.register_space) || | ||
| 384 | !u32_at(soff + 48, vis)) { | ||
| 314 | out.error = "truncated RTS0 sampler"; | 385 | out.error = "truncated RTS0 sampler"; |
| 315 | return out; | 386 | return out; |
| 316 | } | 387 | } |
| 317 | s.visibility = static_cast<Visibility>(vis); | 388 | s.visibility = static_cast<Visibility>(vis); |
| 318 | out.sig.static_samplers.push_back(s); | 389 | out.sig.static_samplers.push_back(s); |
| 319 | } | 390 | } |
| 320 | if (!r.at_end()) { | ||
| 321 | out.error = "trailing bytes after RTS0 root signature"; | ||
| 322 | return out; | ||
| 323 | } | ||
| 324 | out.ok = true; | 391 | out.ok = true; |
| 325 | return out; | 392 | return out; |
| 326 | } | 393 | } |