diff options
| author | null <marek8788@proton.me> | 2026-11-23 19:42:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 12:42:41 -0600 |
| commit | 0c6ff794c21fe59d4622e9366ca9d8d3d23d6301 (patch) | |
| tree | d57be8441ef7e4a8bc71be9536fde0799cf41eff /runtime/src | |
| parent | bf69af7c7f22d1986e126a2e13b732c28b3037d8 (diff) | |
resource_detile: prevent uint32/size_t size wraparound and reject overlapping spans (#6567)
Squash-merged after diff review of PR #6567
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/resource_detile.h | 134 |
1 files changed, 84 insertions, 50 deletions
diff --git a/runtime/src/resource_detile.h b/runtime/src/resource_detile.h index 35ba8ba7c..74f22aac5 100644 --- a/runtime/src/resource_detile.h +++ b/runtime/src/resource_detile.h | |||
| @@ -17,6 +17,8 @@ | |||
| 17 | #include <cstddef> | 17 | #include <cstddef> |
| 18 | #include <cstdint> | 18 | #include <cstdint> |
| 19 | #include <cstring> | 19 | #include <cstring> |
| 20 | #include <functional> | ||
| 21 | #include <limits> | ||
| 20 | #include <span> | 22 | #include <span> |
| 21 | 23 | ||
| 22 | namespace leonida { | 24 | namespace leonida { |
| @@ -91,26 +93,76 @@ inline constexpr SwizzleLut64K<4> swizzle_lut_64k_4bpe{}; | |||
| 91 | inline constexpr SwizzleLut64K<8> swizzle_lut_64k_8bpe{}; | 93 | inline constexpr SwizzleLut64K<8> swizzle_lut_64k_8bpe{}; |
| 92 | inline constexpr SwizzleLut64K<16> swizzle_lut_64k_16bpe{}; | 94 | inline constexpr SwizzleLut64K<16> swizzle_lut_64k_16bpe{}; |
| 93 | 95 | ||
| 94 | // Compute required tiled byte size for a 2D surface of the given dimensions. | 96 | namespace detail { |
| 97 | |||
| 98 | constexpr bool checked_mul(std::size_t a, std::size_t b, std::size_t& out) { | ||
| 99 | if (a != 0 && b > (std::numeric_limits<std::size_t>::max)() / a) return false; | ||
| 100 | out = a * b; | ||
| 101 | return true; | ||
| 102 | } | ||
| 103 | |||
| 104 | struct SurfaceLayout64K { | ||
| 105 | std::uint32_t blocks_w = 0; | ||
| 106 | std::uint32_t blocks_h = 0; | ||
| 107 | std::uint32_t tiles_x = 0; | ||
| 108 | std::uint32_t tiles_y = 0; | ||
| 109 | std::size_t linear_bytes = 0; | ||
| 110 | std::size_t tiled_bytes = 0; | ||
| 111 | }; | ||
| 112 | |||
| 113 | constexpr bool compute_surface_layout_64k(std::uint32_t width, std::uint32_t height, | ||
| 114 | std::uint32_t element_bytes, std::uint32_t block_dim, | ||
| 115 | SurfaceLayout64K& out) { | ||
| 116 | if (!width || !height || (block_dim != 1 && block_dim != 4)) return false; | ||
| 117 | const TileShape shape = element_tile_shape(false, element_bytes); | ||
| 118 | if (!shape.width || !shape.height) return false; | ||
| 119 | |||
| 120 | // Overflow-free ceiling division for positive uint32_t operands: 1 + (n - 1) / d. | ||
| 121 | const std::uint32_t blocks_w = 1u + (width - 1u) / block_dim; | ||
| 122 | const std::uint32_t blocks_h = 1u + (height - 1u) / block_dim; | ||
| 123 | const std::uint32_t tiles_x = 1u + (blocks_w - 1u) / shape.width; | ||
| 124 | const std::uint32_t tiles_y = 1u + (blocks_h - 1u) / shape.height; | ||
| 125 | |||
| 126 | std::size_t block_count = 0; | ||
| 127 | std::size_t linear_bytes = 0; | ||
| 128 | std::size_t tile_count = 0; | ||
| 129 | std::size_t tiled_bytes = 0; | ||
| 130 | if (!checked_mul(blocks_w, blocks_h, block_count) || | ||
| 131 | !checked_mul(block_count, element_bytes, linear_bytes) || | ||
| 132 | !checked_mul(tiles_x, tiles_y, tile_count) || | ||
| 133 | !checked_mul(tile_count, tile_size_in_bytes, tiled_bytes)) { | ||
| 134 | return false; | ||
| 135 | } | ||
| 136 | |||
| 137 | out = {blocks_w, blocks_h, tiles_x, tiles_y, linear_bytes, tiled_bytes}; | ||
| 138 | return true; | ||
| 139 | } | ||
| 140 | |||
| 141 | inline bool spans_overlap(const std::byte* a, std::size_t a_len, | ||
| 142 | const std::byte* b, std::size_t b_len) { | ||
| 143 | if (!a_len || !b_len) return false; | ||
| 144 | const std::less<const std::byte*> lt{}; | ||
| 145 | return lt(a, b + b_len) && lt(b, a + a_len); | ||
| 146 | } | ||
| 147 | |||
| 148 | } // namespace detail | ||
| 149 | |||
| 150 | // Compute required tiled byte size for a 2D surface of the given dimensions (0 on invalid input or overflow). | ||
| 95 | constexpr std::size_t tiled_surface_size_64k(std::uint32_t width, std::uint32_t height, | 151 | constexpr std::size_t tiled_surface_size_64k(std::uint32_t width, std::uint32_t height, |
| 96 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { | 152 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { |
| 97 | if (!width || !height || (block_dim != 1 && block_dim != 4)) return 0; | 153 | detail::SurfaceLayout64K layout{}; |
| 98 | const TileShape shape = element_tile_shape(false, element_bytes); | 154 | return detail::compute_surface_layout_64k(width, height, element_bytes, block_dim, layout) |
| 99 | if (!shape.width || !shape.height) return 0; | 155 | ? layout.tiled_bytes |
| 100 | const std::uint32_t blocks_w = (width + block_dim - 1) / block_dim; | 156 | : 0; |
| 101 | const std::uint32_t blocks_h = (height + block_dim - 1) / block_dim; | ||
| 102 | const std::uint32_t tiles_x = (blocks_w + shape.width - 1) / shape.width; | ||
| 103 | const std::uint32_t tiles_y = (blocks_h + shape.height - 1) / shape.height; | ||
| 104 | return static_cast<std::size_t>(tiles_x) * tiles_y * tile_size_in_bytes; | ||
| 105 | } | 157 | } |
| 106 | 158 | ||
| 107 | // Compute required linear byte size for a 2D surface of the given dimensions. | 159 | // Compute required linear byte size for a 2D surface of the given dimensions (0 on invalid input or overflow). |
| 108 | constexpr std::size_t linear_surface_size(std::uint32_t width, std::uint32_t height, | 160 | constexpr std::size_t linear_surface_size(std::uint32_t width, std::uint32_t height, |
| 109 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { | 161 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { |
| 110 | if (!width || !height || (block_dim != 1 && block_dim != 4)) return 0; | 162 | detail::SurfaceLayout64K layout{}; |
| 111 | const std::uint32_t blocks_w = (width + block_dim - 1) / block_dim; | 163 | return detail::compute_surface_layout_64k(width, height, element_bytes, block_dim, layout) |
| 112 | const std::uint32_t blocks_h = (height + block_dim - 1) / block_dim; | 164 | ? layout.linear_bytes |
| 113 | return static_cast<std::size_t>(blocks_w) * blocks_h * element_bytes; | 165 | : 0; |
| 114 | } | 166 | } |
| 115 | 167 | ||
| 116 | namespace detail { | 168 | namespace detail { |
| @@ -183,35 +235,26 @@ void tile_typed(std::span<const std::byte> linear, std::span<std::byte> tiled, | |||
| 183 | inline bool detile_surface_64k(std::span<const std::byte> tiled, std::span<std::byte> linear, | 235 | inline bool detile_surface_64k(std::span<const std::byte> tiled, std::span<std::byte> linear, |
| 184 | std::uint32_t width, std::uint32_t height, | 236 | std::uint32_t width, std::uint32_t height, |
| 185 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { | 237 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { |
| 186 | if (!width || !height || (block_dim != 1 && block_dim != 4)) return false; | 238 | detail::SurfaceLayout64K layout{}; |
| 187 | const TileShape shape = element_tile_shape(false, element_bytes); | 239 | if (!detail::compute_surface_layout_64k(width, height, element_bytes, block_dim, layout)) return false; |
| 188 | if (!shape.width || !shape.height) return false; | 240 | if (linear.size() < layout.linear_bytes || tiled.size() < layout.tiled_bytes) return false; |
| 189 | 241 | if (detail::spans_overlap(tiled.data(), layout.tiled_bytes, linear.data(), layout.linear_bytes)) return false; | |
| 190 | const std::uint32_t blocks_w = (width + block_dim - 1) / block_dim; | ||
| 191 | const std::uint32_t blocks_h = (height + block_dim - 1) / block_dim; | ||
| 192 | const std::uint32_t tiles_x = (blocks_w + shape.width - 1) / shape.width; | ||
| 193 | const std::uint32_t tiles_y = (blocks_h + shape.height - 1) / shape.height; | ||
| 194 | |||
| 195 | const std::size_t expected_linear = static_cast<std::size_t>(blocks_w) * blocks_h * element_bytes; | ||
| 196 | const std::size_t expected_tiled = static_cast<std::size_t>(tiles_x) * tiles_y * tile_size_in_bytes; | ||
| 197 | |||
| 198 | if (linear.size() < expected_linear || tiled.size() < expected_tiled) return false; | ||
| 199 | 242 | ||
| 200 | switch (element_bytes) { | 243 | switch (element_bytes) { |
| 201 | case 1: | 244 | case 1: |
| 202 | detail::detile_typed<1>(tiled, linear, blocks_w, blocks_h, tiles_x); | 245 | detail::detile_typed<1>(tiled, linear, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 203 | return true; | 246 | return true; |
| 204 | case 2: | 247 | case 2: |
| 205 | detail::detile_typed<2>(tiled, linear, blocks_w, blocks_h, tiles_x); | 248 | detail::detile_typed<2>(tiled, linear, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 206 | return true; | 249 | return true; |
| 207 | case 4: | 250 | case 4: |
| 208 | detail::detile_typed<4>(tiled, linear, blocks_w, blocks_h, tiles_x); | 251 | detail::detile_typed<4>(tiled, linear, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 209 | return true; | 252 | return true; |
| 210 | case 8: | 253 | case 8: |
| 211 | detail::detile_typed<8>(tiled, linear, blocks_w, blocks_h, tiles_x); | 254 | detail::detile_typed<8>(tiled, linear, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 212 | return true; | 255 | return true; |
| 213 | case 16: | 256 | case 16: |
| 214 | detail::detile_typed<16>(tiled, linear, blocks_w, blocks_h, tiles_x); | 257 | detail::detile_typed<16>(tiled, linear, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 215 | return true; | 258 | return true; |
| 216 | default: | 259 | default: |
| 217 | return false; | 260 | return false; |
| @@ -222,35 +265,26 @@ inline bool detile_surface_64k(std::span<const std::byte> tiled, std::span<std:: | |||
| 222 | inline bool tile_surface_64k(std::span<const std::byte> linear, std::span<std::byte> tiled, | 265 | inline bool tile_surface_64k(std::span<const std::byte> linear, std::span<std::byte> tiled, |
| 223 | std::uint32_t width, std::uint32_t height, | 266 | std::uint32_t width, std::uint32_t height, |
| 224 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { | 267 | std::uint32_t element_bytes, std::uint32_t block_dim = 1) { |
| 225 | if (!width || !height || (block_dim != 1 && block_dim != 4)) return false; | 268 | detail::SurfaceLayout64K layout{}; |
| 226 | const TileShape shape = element_tile_shape(false, element_bytes); | 269 | if (!detail::compute_surface_layout_64k(width, height, element_bytes, block_dim, layout)) return false; |
| 227 | if (!shape.width || !shape.height) return false; | 270 | if (linear.size() < layout.linear_bytes || tiled.size() < layout.tiled_bytes) return false; |
| 228 | 271 | if (detail::spans_overlap(linear.data(), layout.linear_bytes, tiled.data(), layout.tiled_bytes)) return false; | |
| 229 | const std::uint32_t blocks_w = (width + block_dim - 1) / block_dim; | ||
| 230 | const std::uint32_t blocks_h = (height + block_dim - 1) / block_dim; | ||
| 231 | const std::uint32_t tiles_x = (blocks_w + shape.width - 1) / shape.width; | ||
| 232 | const std::uint32_t tiles_y = (blocks_h + shape.height - 1) / shape.height; | ||
| 233 | |||
| 234 | const std::size_t expected_linear = static_cast<std::size_t>(blocks_w) * blocks_h * element_bytes; | ||
| 235 | const std::size_t expected_tiled = static_cast<std::size_t>(tiles_x) * tiles_y * tile_size_in_bytes; | ||
| 236 | |||
| 237 | if (linear.size() < expected_linear || tiled.size() < expected_tiled) return false; | ||
| 238 | 272 | ||
| 239 | switch (element_bytes) { | 273 | switch (element_bytes) { |
| 240 | case 1: | 274 | case 1: |
| 241 | detail::tile_typed<1>(linear, tiled, blocks_w, blocks_h, tiles_x); | 275 | detail::tile_typed<1>(linear, tiled, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 242 | return true; | 276 | return true; |
| 243 | case 2: | 277 | case 2: |
| 244 | detail::tile_typed<2>(linear, tiled, blocks_w, blocks_h, tiles_x); | 278 | detail::tile_typed<2>(linear, tiled, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 245 | return true; | 279 | return true; |
| 246 | case 4: | 280 | case 4: |
| 247 | detail::tile_typed<4>(linear, tiled, blocks_w, blocks_h, tiles_x); | 281 | detail::tile_typed<4>(linear, tiled, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 248 | return true; | 282 | return true; |
| 249 | case 8: | 283 | case 8: |
| 250 | detail::tile_typed<8>(linear, tiled, blocks_w, blocks_h, tiles_x); | 284 | detail::tile_typed<8>(linear, tiled, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 251 | return true; | 285 | return true; |
| 252 | case 16: | 286 | case 16: |
| 253 | detail::tile_typed<16>(linear, tiled, blocks_w, blocks_h, tiles_x); | 287 | detail::tile_typed<16>(linear, tiled, layout.blocks_w, layout.blocks_h, layout.tiles_x); |
| 254 | return true; | 288 | return true; |
| 255 | default: | 289 | default: |
| 256 | return false; | 290 | return false; |