diff options
Diffstat (limited to 'runtime/src/root_signature.cpp')
| -rw-r--r-- | runtime/src/root_signature.cpp | 456 |
1 files changed, 456 insertions, 0 deletions
diff --git a/runtime/src/root_signature.cpp b/runtime/src/root_signature.cpp new file mode 100644 index 000000000..79a633b61 --- /dev/null +++ b/runtime/src/root_signature.cpp | |||
| @@ -0,0 +1,456 @@ | |||
| 1 | // Portable root-signature packer, see root_signature.h. | ||
| 2 | #include "root_signature.h" | ||
| 3 | |||
| 4 | #include <cstring> | ||
| 5 | |||
| 6 | namespace rt::rootsig { | ||
| 7 | |||
| 8 | PackResult pack(const RootSignature& sig) { | ||
| 9 | PackResult out; | ||
| 10 | if (sig.params.size() > 64) { | ||
| 11 | out.error = "root signature has more than 64 root parameters"; | ||
| 12 | return out; | ||
| 13 | } | ||
| 14 | uint32_t cost = 0; | ||
| 15 | for (const RootParameter& p : sig.params) { | ||
| 16 | if (p.type == ParamType::Constants && p.num_values == 0) { | ||
| 17 | out.error = "root constant parameter needs at least 1 dword"; | ||
| 18 | return out; | ||
| 19 | } | ||
| 20 | const uint32_t dwords = p.type == ParamType::Table ? 2 | ||
| 21 | : p.type == ParamType::Descriptor ? 2 | ||
| 22 | : p.num_values; | ||
| 23 | cost += param_cost(p.type, p.num_values); | ||
| 24 | if (cost > kMaxD3d12Cost) { | ||
| 25 | out.error = "root signature exceeds the 64-dword D3D12 cost limit"; | ||
| 26 | return out; | ||
| 27 | } | ||
| 28 | if (out.dword_count + dwords > kMaxUserSgprs) { | ||
| 29 | // Spill layout is unknown (see __XB_GetRootSignatureSpillPointer): | ||
| 30 | // refuse loudly instead of silently truncating every draw. | ||
| 31 | out.error = "root signature needs more than 32 user SGPRs (spill unsupported)"; | ||
| 32 | return out; | ||
| 33 | } | ||
| 34 | const uint32_t param_index = | ||
| 35 | static_cast<uint32_t>(&p - sig.params.data()); | ||
| 36 | for (uint32_t i = 0; i < dwords; ++i) { | ||
| 37 | out.dwords[out.dword_count] = 0; // filled at bind time | ||
| 38 | out.provenance.push_back({param_index, i}); | ||
| 39 | ++out.dword_count; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | out.d3d12_cost = cost; | ||
| 43 | out.ok = true; | ||
| 44 | return out; | ||
| 45 | } | ||
| 46 | |||
| 47 | namespace { | ||
| 48 | |||
| 49 | void put_u32(std::vector<uint8_t>& out, uint32_t v) { | ||
| 50 | out.push_back(static_cast<uint8_t>(v)); | ||
| 51 | out.push_back(static_cast<uint8_t>(v >> 8)); | ||
| 52 | out.push_back(static_cast<uint8_t>(v >> 16)); | ||
| 53 | out.push_back(static_cast<uint8_t>(v >> 24)); | ||
| 54 | } | ||
| 55 | |||
| 56 | void put_f32(std::vector<uint8_t>& out, float v) { | ||
| 57 | uint32_t bits = 0; | ||
| 58 | static_assert(sizeof(bits) == sizeof(v)); | ||
| 59 | std::memcpy(&bits, &v, sizeof(bits)); | ||
| 60 | put_u32(out, bits); | ||
| 61 | } | ||
| 62 | |||
| 63 | class Reader { | ||
| 64 | public: | ||
| 65 | Reader(const uint8_t* data, size_t size) : data_(data), size_(size) {} | ||
| 66 | |||
| 67 | [[nodiscard]] bool u32(uint32_t& v) { | ||
| 68 | if (pos_ + 4 > size_) return false; | ||
| 69 | v = static_cast<uint32_t>(data_[pos_]) | | ||
| 70 | (static_cast<uint32_t>(data_[pos_ + 1]) << 8) | | ||
| 71 | (static_cast<uint32_t>(data_[pos_ + 2]) << 16) | | ||
| 72 | (static_cast<uint32_t>(data_[pos_ + 3]) << 24); | ||
| 73 | pos_ += 4; | ||
| 74 | return true; | ||
| 75 | } | ||
| 76 | |||
| 77 | [[nodiscard]] bool f32(float& v) { | ||
| 78 | uint32_t bits = 0; | ||
| 79 | if (!u32(bits)) return false; | ||
| 80 | std::memcpy(&v, &bits, sizeof(v)); | ||
| 81 | return true; | ||
| 82 | } | ||
| 83 | |||
| 84 | [[nodiscard]] bool at_end() const { return pos_ == size_; } | ||
| 85 | |||
| 86 | private: | ||
| 87 | const uint8_t* data_ = nullptr; | ||
| 88 | size_t size_ = 0; | ||
| 89 | size_t pos_ = 0; | ||
| 90 | }; | ||
| 91 | |||
| 92 | constexpr uint32_t kCanonicalMagic = 0x3153524Cu; // "LRS1" little-endian | ||
| 93 | constexpr uint32_t kCanonicalVersion = 1; | ||
| 94 | |||
| 95 | } // namespace | ||
| 96 | |||
| 97 | std::vector<uint8_t> encode_canonical(const RootSignature& sig) { | ||
| 98 | std::vector<uint8_t> out; | ||
| 99 | put_u32(out, kCanonicalMagic); | ||
| 100 | put_u32(out, kCanonicalVersion); | ||
| 101 | put_u32(out, static_cast<uint32_t>(sig.version)); | ||
| 102 | put_u32(out, static_cast<uint32_t>(sig.params.size())); | ||
| 103 | put_u32(out, static_cast<uint32_t>(sig.static_samplers.size())); | ||
| 104 | put_u32(out, sig.flags); | ||
| 105 | for (const RootParameter& p : sig.params) { | ||
| 106 | put_u32(out, static_cast<uint32_t>(p.type)); | ||
| 107 | put_u32(out, static_cast<uint32_t>(p.visibility)); | ||
| 108 | put_u32(out, p.num_values); | ||
| 109 | put_u32(out, p.shader_register); | ||
| 110 | put_u32(out, p.register_space); | ||
| 111 | put_u32(out, p.descriptor_register); | ||
| 112 | put_u32(out, p.descriptor_space); | ||
| 113 | put_u32(out, static_cast<uint32_t>(p.ranges.size())); | ||
| 114 | for (const DescriptorRange& r : p.ranges) { | ||
| 115 | put_u32(out, r.range_type); | ||
| 116 | put_u32(out, r.num_descriptors); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | for (const StaticSampler& s : sig.static_samplers) { | ||
| 120 | put_u32(out, s.filter); | ||
| 121 | put_u32(out, s.address_u); | ||
| 122 | put_u32(out, s.address_v); | ||
| 123 | put_u32(out, s.address_w); | ||
| 124 | put_f32(out, s.mip_lod_bias); | ||
| 125 | put_u32(out, s.max_anisotropy); | ||
| 126 | put_u32(out, s.comparison_func); | ||
| 127 | put_u32(out, s.border_color); | ||
| 128 | put_f32(out, s.min_lod); | ||
| 129 | put_f32(out, s.max_lod); | ||
| 130 | put_u32(out, s.shader_register); | ||
| 131 | put_u32(out, s.register_space); | ||
| 132 | put_u32(out, static_cast<uint32_t>(s.visibility)); | ||
| 133 | } | ||
| 134 | return out; | ||
| 135 | } | ||
| 136 | |||
| 137 | DecodeResult decode_canonical(const uint8_t* data, size_t size) { | ||
| 138 | DecodeResult out; | ||
| 139 | if (data == nullptr && size != 0) { | ||
| 140 | out.error = "null data with nonzero size"; | ||
| 141 | return out; | ||
| 142 | } | ||
| 143 | Reader r(data == nullptr ? reinterpret_cast<const uint8_t*>("") : data, size); | ||
| 144 | uint32_t magic = 0, version = 0, nparams = 0, nsamplers = 0; | ||
| 145 | if (!r.u32(magic) || magic != kCanonicalMagic) { | ||
| 146 | out.error = "bad canonical magic"; | ||
| 147 | return out; | ||
| 148 | } | ||
| 149 | if (!r.u32(version) || version != kCanonicalVersion) { | ||
| 150 | out.error = "unsupported canonical version"; | ||
| 151 | return out; | ||
| 152 | } | ||
| 153 | uint32_t ver = 0; | ||
| 154 | if (!r.u32(ver) || !r.u32(nparams) || !r.u32(nsamplers) || !r.u32(out.sig.flags)) { | ||
| 155 | out.error = "truncated canonical header"; | ||
| 156 | return out; | ||
| 157 | } | ||
| 158 | if (ver != 1 && ver != 2) { | ||
| 159 | out.error = "unknown root-signature version"; | ||
| 160 | return out; | ||
| 161 | } | ||
| 162 | if (nparams > 64) { | ||
| 163 | out.error = "more than 64 root parameters"; | ||
| 164 | return out; | ||
| 165 | } | ||
| 166 | if (nsamplers > 4096) { | ||
| 167 | out.error = "absurd sampler count"; | ||
| 168 | return out; | ||
| 169 | } | ||
| 170 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; | ||
| 171 | out.sig.params.reserve(nparams); | ||
| 172 | for (uint32_t i = 0; i < nparams; ++i) { | ||
| 173 | RootParameter p; | ||
| 174 | uint32_t type = 0, vis = 0, nranges = 0; | ||
| 175 | if (!r.u32(type) || !r.u32(vis) || !r.u32(p.num_values) || !r.u32(p.shader_register) || | ||
| 176 | !r.u32(p.register_space) || !r.u32(p.descriptor_register) || | ||
| 177 | !r.u32(p.descriptor_space) || !r.u32(nranges)) { | ||
| 178 | out.error = "truncated canonical parameter"; | ||
| 179 | return out; | ||
| 180 | } | ||
| 181 | if (type > 2) { | ||
| 182 | out.error = "unknown parameter type"; | ||
| 183 | return out; | ||
| 184 | } | ||
| 185 | p.type = static_cast<ParamType>(type); | ||
| 186 | p.visibility = static_cast<Visibility>(vis); | ||
| 187 | if (nranges > 1024) { | ||
| 188 | out.error = "absurd range count"; | ||
| 189 | return out; | ||
| 190 | } | ||
| 191 | p.ranges.reserve(nranges); | ||
| 192 | for (uint32_t j = 0; j < nranges; ++j) { | ||
| 193 | DescriptorRange range; | ||
| 194 | if (!r.u32(range.range_type) || !r.u32(range.num_descriptors)) { | ||
| 195 | out.error = "truncated canonical range"; | ||
| 196 | return out; | ||
| 197 | } | ||
| 198 | p.ranges.push_back(range); | ||
| 199 | } | ||
| 200 | out.sig.params.push_back(std::move(p)); | ||
| 201 | } | ||
| 202 | out.sig.static_samplers.reserve(nsamplers); | ||
| 203 | for (uint32_t i = 0; i < nsamplers; ++i) { | ||
| 204 | StaticSampler s; | ||
| 205 | uint32_t vis = 0; | ||
| 206 | if (!r.u32(s.filter) || !r.u32(s.address_u) || !r.u32(s.address_v) || !r.u32(s.address_w) || | ||
| 207 | !r.f32(s.mip_lod_bias) || !r.u32(s.max_anisotropy) || !r.u32(s.comparison_func) || | ||
| 208 | !r.u32(s.border_color) || !r.f32(s.min_lod) || !r.f32(s.max_lod) || | ||
| 209 | !r.u32(s.shader_register) || !r.u32(s.register_space) || !r.u32(vis)) { | ||
| 210 | out.error = "truncated canonical sampler"; | ||
| 211 | return out; | ||
| 212 | } | ||
| 213 | s.visibility = static_cast<Visibility>(vis); | ||
| 214 | out.sig.static_samplers.push_back(s); | ||
| 215 | } | ||
| 216 | if (!r.at_end()) { | ||
| 217 | out.error = "trailing bytes after canonical root signature"; | ||
| 218 | return out; | ||
| 219 | } | ||
| 220 | out.ok = true; | ||
| 221 | return out; | ||
| 222 | } | ||
| 223 | |||
| 224 | // RTS0 payload hypothesis: u32 version (1 = 1.0, 2 = 1.1), u32 num_params, | ||
| 225 | // u32 num_static_samplers, u32 flags, then per-parameter fields in d3d12.h | ||
| 226 | // order. Static samplers follow the same field order as D3D12_STATIC_SAMPLER_DESC. | ||
| 227 | // Anything else (v1.2 blobs, extra dwords, truncation) is a hard error. | ||
| 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) { | ||
| 230 | DecodeResult out; | ||
| 231 | if (data == nullptr && size != 0) { | ||
| 232 | out.error = "null data with nonzero size"; | ||
| 233 | return out; | ||
| 234 | } | ||
| 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)) { | ||
| 264 | out.error = "truncated RTS0 header"; | ||
| 265 | return out; | ||
| 266 | } | ||
| 267 | if (ver != 1 && ver != 2) { | ||
| 268 | out.error = "unknown RTS0 version (not 1.0/1.1)"; | ||
| 269 | return out; | ||
| 270 | } | ||
| 271 | if (nparams > 64) { | ||
| 272 | out.error = "RTS0 with more than 64 root parameters"; | ||
| 273 | return out; | ||
| 274 | } | ||
| 275 | if (nsamplers > 4096) { | ||
| 276 | out.error = "RTS0 with absurd sampler count"; | ||
| 277 | return out; | ||
| 278 | } | ||
| 279 | out.sig.version = ver == 1 ? Version::V1_0 : Version::V1_1; | ||
| 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 | } | ||
| 290 | out.sig.params.reserve(nparams); | ||
| 291 | for (uint32_t i = 0; i < nparams; ++i) { | ||
| 292 | const size_t eoff = static_cast<size_t>(params_off) + i * kEntryBytes; | ||
| 293 | uint32_t dtype = 0, vis = 0, poff = 0; | ||
| 294 | if (!u32_at(eoff, dtype) || !u32_at(eoff + 4, vis) || !u32_at(eoff + 8, poff)) { | ||
| 295 | out.error = "truncated RTS0 parameter entry"; | ||
| 296 | return out; | ||
| 297 | } | ||
| 298 | RootParameter p; | ||
| 299 | p.visibility = static_cast<Visibility>(vis); | ||
| 300 | if (dtype == 1) { | ||
| 301 | p.type = ParamType::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"; | ||
| 305 | return out; | ||
| 306 | } | ||
| 307 | } else if (dtype == 2 || dtype == 3 || dtype == 4) { | ||
| 308 | p.type = ParamType::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"; | ||
| 313 | return out; | ||
| 314 | } | ||
| 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; | ||
| 326 | uint32_t nranges = 0; | ||
| 327 | if (!u32_at(poff, nranges)) { | ||
| 328 | out.error = "truncated RTS0 table payload"; | ||
| 329 | return out; | ||
| 330 | } | ||
| 331 | if (nranges > 1024) { | ||
| 332 | out.error = "absurd RTS0 range count"; | ||
| 333 | return out; | ||
| 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 | } | ||
| 342 | p.ranges.reserve(nranges); | ||
| 343 | for (uint32_t j = 0; j < nranges; ++j) { | ||
| 344 | const size_t roff = static_cast<size_t>(poff) + 4 + j * stride; | ||
| 345 | DescriptorRange range; | ||
| 346 | uint32_t base_reg = 0, space = 0, disp_off = 0, rflags = 0; | ||
| 347 | if (!u32_at(roff, range.range_type) || !u32_at(roff + 4, range.num_descriptors) || | ||
| 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))) { | ||
| 351 | out.error = "truncated RTS0 range"; | ||
| 352 | return out; | ||
| 353 | } | ||
| 354 | (void)base_reg; | ||
| 355 | (void)space; | ||
| 356 | (void)disp_off; | ||
| 357 | (void)rflags; | ||
| 358 | p.ranges.push_back(range); | ||
| 359 | } | ||
| 360 | } else { | ||
| 361 | out.error = "unknown RTS0 parameter type"; | ||
| 362 | return out; | ||
| 363 | } | ||
| 364 | out.sig.params.push_back(std::move(p)); | ||
| 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 | } | ||
| 373 | out.sig.static_samplers.reserve(nsamplers); | ||
| 374 | for (uint32_t i = 0; i < nsamplers; ++i) { | ||
| 375 | const size_t soff = static_cast<size_t>(samplers_off) + i * kSamplerBytes; | ||
| 376 | StaticSampler s; | ||
| 377 | uint32_t vis = 0; | ||
| 378 | if (!u32_at(soff, s.filter) || !u32_at(soff + 4, s.address_u) || | ||
| 379 | !u32_at(soff + 8, s.address_v) || !u32_at(soff + 12, s.address_w) || | ||
| 380 | !f32_at(soff + 16, s.mip_lod_bias) || !u32_at(soff + 20, s.max_anisotropy) || | ||
| 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)) { | ||
| 385 | out.error = "truncated RTS0 sampler"; | ||
| 386 | return out; | ||
| 387 | } | ||
| 388 | s.visibility = static_cast<Visibility>(vis); | ||
| 389 | out.sig.static_samplers.push_back(s); | ||
| 390 | } | ||
| 391 | out.ok = true; | ||
| 392 | return out; | ||
| 393 | } | ||
| 394 | |||
| 395 | DecodeResult parse_rts0(const uint8_t* data, size_t size) { | ||
| 396 | DecodeResult out; | ||
| 397 | if ((data == nullptr && size != 0) || (data != nullptr && size < 4)) { | ||
| 398 | out.error = "blob too small for a DXBC container"; | ||
| 399 | return out; | ||
| 400 | } | ||
| 401 | if (data == nullptr) { | ||
| 402 | out.error = "null blob"; | ||
| 403 | return out; | ||
| 404 | } | ||
| 405 | // A bare payload (unit-test path) has no DXBC magic: decode directly. | ||
| 406 | if (!(data[0] == 'D' && data[1] == 'X' && data[2] == 'B' && data[3] == 'C')) { | ||
| 407 | return parse_rts0_payload(data, size); | ||
| 408 | } | ||
| 409 | // DXBC container walk (mirrors input_signature in tools/rdna2spirv.py): | ||
| 410 | // +28: chunk count, +32: chunk offsets, each chunk is FourCC + u32 size. | ||
| 411 | auto at = [&](size_t off, uint32_t& v) { | ||
| 412 | if (off + 4 > size) return false; | ||
| 413 | v = static_cast<uint32_t>(data[off]) | (static_cast<uint32_t>(data[off + 1]) << 8) | | ||
| 414 | (static_cast<uint32_t>(data[off + 2]) << 16) | (static_cast<uint32_t>(data[off + 3]) << 24); | ||
| 415 | return true; | ||
| 416 | }; | ||
| 417 | uint32_t nchunks = 0; | ||
| 418 | if (!at(28, nchunks)) { | ||
| 419 | out.error = "truncated DXBC header"; | ||
| 420 | return out; | ||
| 421 | } | ||
| 422 | if (nchunks > 1024) { | ||
| 423 | out.error = "absurd DXBC chunk count"; | ||
| 424 | return out; | ||
| 425 | } | ||
| 426 | for (uint32_t k = 0; k < nchunks; ++k) { | ||
| 427 | uint32_t off = 0; | ||
| 428 | // NOTE: off and payload_size are u32; promote to size_t BEFORE adding | ||
| 429 | // or a hostile container wraps the check and turns it into an OOB read. | ||
| 430 | if (!at(32 + 4 * static_cast<size_t>(k), off) || | ||
| 431 | static_cast<size_t>(off) + 8 > size) { | ||
| 432 | out.error = "truncated DXBC chunk table"; | ||
| 433 | return out; | ||
| 434 | } | ||
| 435 | if (data[off] == 'R' && data[off + 1] == 'T' && data[off + 2] == 'S' && data[off + 3] == '0') { | ||
| 436 | uint32_t payload_size = 0; | ||
| 437 | if (static_cast<size_t>(off) + 8 > size) { | ||
| 438 | out.error = "truncated RTS0 chunk header"; | ||
| 439 | return out; | ||
| 440 | } | ||
| 441 | payload_size = static_cast<uint32_t>(data[off + 4]) | | ||
| 442 | (static_cast<uint32_t>(data[off + 5]) << 8) | | ||
| 443 | (static_cast<uint32_t>(data[off + 6]) << 16) | | ||
| 444 | (static_cast<uint32_t>(data[off + 7]) << 24); | ||
| 445 | if (static_cast<size_t>(off) + 8 + payload_size > size) { | ||
| 446 | out.error = "RTS0 chunk overruns its container"; | ||
| 447 | return out; | ||
| 448 | } | ||
| 449 | return parse_rts0_payload(data + off + 8, payload_size); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | out.error = "no RTS0 chunk in DXBC container"; | ||
| 453 | return out; | ||
| 454 | } | ||
| 455 | |||
| 456 | } // namespace rt::rootsig | ||