|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Bounds checks of the RR runtime's model loaders (src/rr_model.cpp), CPU only, no GPU and no model data: crafted files with |
| 3 | +// a correct whole-file SHA-256 (the hash is unkeyed, so it only catches accidents) whose offsets, sizes and shapes wrap |
| 4 | +// around 2^64. Driven by test_model_bounds.py, one case per run: |
| 5 | +// |
| 6 | +// test_model_bounds <case> <scratch folder> |
| 7 | +// |
| 8 | +// Prints "OK" or "FAIL <what>". A loader that reads through a wrapped range may also crash: that is a failure too. |
| 9 | +#define WIN32_LEAN_AND_MEAN |
| 10 | +#define NOMINMAX |
| 11 | +#include <windows.h> |
| 12 | + |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <string.h> |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "../src/rr_model.h" |
| 21 | + |
| 22 | +using namespace wgrr_rt; |
| 23 | + |
| 24 | +// load_model refers to the packer of rr_pack.cpp, which needs the generated tables; the loaders under test do not use it. |
| 25 | +namespace wgrr_rt { |
| 26 | +bool pack_wgcm(const std::vector<WgcmGroup>&, Model*, std::string* err) { |
| 27 | + *err = "packing is not part of this test"; |
| 28 | + return false; |
| 29 | +} |
| 30 | +} // namespace wgrr_rt |
| 31 | + |
| 32 | +typedef std::vector<uint8_t> Bytes; |
| 33 | + |
| 34 | +static void put(Bytes& b, uint64_t v, int n) { |
| 35 | + for (int i = 0; i < n; ++i) b.push_back((uint8_t)(v >> (8 * i))); |
| 36 | +} |
| 37 | +static void put_at(Bytes& b, size_t at, uint64_t v, int n) { |
| 38 | + for (int i = 0; i < n; ++i) b[at + i] = (uint8_t)(v >> (8 * i)); |
| 39 | +} |
| 40 | +static void pad_to(Bytes& b, size_t n) { |
| 41 | + while (b.size() < n) b.push_back(0); |
| 42 | +} |
| 43 | +static void put_str(Bytes& b, const std::string& s) { |
| 44 | + put(b, s.size(), 2); |
| 45 | + b.insert(b.end(), s.begin(), s.end()); |
| 46 | +} |
| 47 | +static Bytes digest(const Bytes& data) { |
| 48 | + const std::string h = sha256_hex(data.data(), data.size()); |
| 49 | + Bytes out; |
| 50 | + for (size_t i = 0; i + 1 < h.size(); i += 2) out.push_back((uint8_t)strtoul(h.substr(i, 2).c_str(), nullptr, 16)); |
| 51 | + return out; |
| 52 | +} |
| 53 | +static void seal(Bytes& b) { // the trailing whole-file hash |
| 54 | + const Bytes h = digest(b); |
| 55 | + b.insert(b.end(), h.begin(), h.end()); |
| 56 | +} |
| 57 | + |
| 58 | +// ---- model.wgcm: header (88), index, data at 64-byte offsets, trailing hash ---------------------------------------- |
| 59 | +struct WgcmTensorSpec { |
| 60 | + uint8_t dtype; |
| 61 | + std::vector<uint32_t> dims; |
| 62 | + uint64_t size; |
| 63 | + bool at_data_start; // offset = the first 64-aligned offset past the index |
| 64 | + uint64_t off; // else this offset |
| 65 | + Bytes data; // placed at the data start, when it is used |
| 66 | +}; |
| 67 | + |
| 68 | +// One group "g" with one tensor "t". `own_digest`: the group digest is the right one for `data` (else zeros). |
| 69 | +static Bytes make_wgcm(const WgcmTensorSpec& t) { |
| 70 | + Bytes key(32); |
| 71 | + for (int i = 0; i < 32; ++i) key[i] = (uint8_t)(0x40 + i); |
| 72 | + Bytes index; |
| 73 | + put_str(index, "g"); |
| 74 | + Bytes keyed = key; |
| 75 | + keyed.insert(keyed.end(), t.data.begin(), t.data.end()); |
| 76 | + const Bytes dg = digest(keyed); |
| 77 | + index.insert(index.end(), dg.begin(), dg.end()); |
| 78 | + put(index, 1, 4); // one tensor |
| 79 | + put_str(index, "t"); |
| 80 | + put(index, t.dtype, 1); |
| 81 | + put(index, t.dims.size(), 1); |
| 82 | + for (uint32_t d : t.dims) put(index, d, 4); |
| 83 | + const uint64_t end = 88 + index.size() + 16; // the index ends after this tensor's offset and size |
| 84 | + const uint64_t start = (end + 63) / 64 * 64; |
| 85 | + put(index, t.at_data_start ? start : t.off, 8); |
| 86 | + put(index, t.size, 8); |
| 87 | + Bytes f; |
| 88 | + const char magic[] = "WGCMODEL"; |
| 89 | + f.insert(f.end(), magic, magic + 8); |
| 90 | + put(f, 2, 4); // container version 2 |
| 91 | + put(f, 1, 4); // groups |
| 92 | + f.insert(f.end(), key.begin(), key.end()); |
| 93 | + pad_to(f, 80); |
| 94 | + put(f, index.size(), 8); |
| 95 | + f.insert(f.end(), index.begin(), index.end()); |
| 96 | + pad_to(f, (size_t)start); |
| 97 | + f.insert(f.end(), t.data.begin(), t.data.end()); |
| 98 | + pad_to(f, (size_t)start + ((t.data.size() + 63) / 64 * 64)); |
| 99 | + seal(f); |
| 100 | + return f; |
| 101 | +} |
| 102 | + |
| 103 | +// ---- WGRRMOD1: header (64), blob table (64 each), scalar table (52 each), image at a 256-byte offset, hash ---------- |
| 104 | +static Bytes own_header(uint32_t nb, uint32_t ns, uint64_t isize) { |
| 105 | + Bytes f; |
| 106 | + const char magic[] = "WGRRMOD1"; |
| 107 | + f.insert(f.end(), magic, magic + 8); |
| 108 | + put(f, 1, 4); |
| 109 | + put(f, 0, 4); |
| 110 | + put(f, nb, 4); |
| 111 | + put(f, ns, 4); |
| 112 | + put(f, isize, 8); |
| 113 | + pad_to(f, 64); |
| 114 | + return f; |
| 115 | +} |
| 116 | +static void own_blob(Bytes& f, const char* name, uint64_t off, uint64_t size) { |
| 117 | + const size_t at = f.size(); |
| 118 | + pad_to(f, at + 48); |
| 119 | + memcpy(&f[at], name, strlen(name)); |
| 120 | + put(f, off, 8); |
| 121 | + put(f, size, 8); |
| 122 | +} |
| 123 | +static void own_scalar(Bytes& f, const char* name, uint32_t v) { |
| 124 | + const size_t at = f.size(); |
| 125 | + pad_to(f, at + 48); |
| 126 | + memcpy(&f[at], name, strlen(name)); |
| 127 | + put(f, v, 4); |
| 128 | +} |
| 129 | + |
| 130 | +static bool load_file(const std::wstring& dir, const Bytes& file, std::string* err, Model* m) { |
| 131 | + const std::wstring path = dir + L"\\model_bounds_case.bin"; |
| 132 | + FILE* fp = _wfopen(path.c_str(), L"wb"); |
| 133 | + if (!fp) { |
| 134 | + *err = "cannot write the scratch file"; |
| 135 | + return false; |
| 136 | + } |
| 137 | + fwrite(file.data(), 1, file.size(), fp); |
| 138 | + fclose(fp); |
| 139 | + const bool ok = load_model(path, m, err); |
| 140 | + DeleteFileW(path.c_str()); |
| 141 | + return ok; |
| 142 | +} |
| 143 | + |
| 144 | +static int fail(const std::string& what) { |
| 145 | + printf("FAIL %s\n", what.c_str()); |
| 146 | + return 0; |
| 147 | +} |
| 148 | +static bool has(const std::string& s, const char* part) { return s.find(part) != std::string::npos; } |
| 149 | + |
| 150 | +int wmain(int argc, wchar_t** argv) { |
| 151 | + if (argc < 3) { |
| 152 | + printf("FAIL usage\n"); |
| 153 | + return 2; |
| 154 | + } |
| 155 | + const std::wstring which = argv[1], dir = argv[2]; |
| 156 | + std::string err; |
| 157 | + std::vector<WgcmGroup> groups; |
| 158 | + |
| 159 | + if (which == L"wgcm_valid") { |
| 160 | + // control: a well-formed file is still accepted (f32, shape 2x3, 24 bytes of data) |
| 161 | + WgcmTensorSpec t{0, {2, 3}, 24, true, 0, Bytes(24, 0x11)}; |
| 162 | + if (!read_wgcm(make_wgcm(t), &groups, &err)) return fail("a valid file was refused: " + err); |
| 163 | + if (groups.size() != 1 || groups[0].tensors.size() != 1 || groups[0].tensors[0].size != 24) return fail("wrong content"); |
| 164 | + } else if (which == L"wgcm_offset_wraps") { |
| 165 | + // off + size wraps around 2^64 to a value inside the file: the range must still be refused, before anything reads it |
| 166 | + WgcmTensorSpec t{1, {64}, 64, false, 0xFFFFFFFFFFFFFFC0ull, Bytes()}; |
| 167 | + if (read_wgcm(make_wgcm(t), &groups, &err)) return fail("accepted a wrapped data range"); |
| 168 | + if (!has(err, "bad data range")) return fail("wrong message: " + err); |
| 169 | + } else if (which == L"wgcm_size_past_end_after_wrap") { |
| 170 | + // the same with a size that is itself the product of the shape (a 2^32-1 square) and an offset above 2^33 |
| 171 | + WgcmTensorSpec t{1, {0xFFFFFFFFu, 0xFFFFFFFFu}, 0xFFFFFFFE00000001ull, false, 1ull << 33, Bytes()}; |
| 172 | + if (read_wgcm(make_wgcm(t), &groups, &err)) return fail("accepted a wrapped data range"); |
| 173 | + if (!has(err, "bad data range")) return fail("wrong message: " + err); |
| 174 | + } else if (which == L"wgcm_shape_wraps") { |
| 175 | + // the shape's element count wraps around 2^64 (2^30 * 2^30 * 4 elements of 4 bytes = 0 bytes): refused |
| 176 | + WgcmTensorSpec t{0, {1u << 30, 1u << 30, 4}, 0, true, 0, Bytes()}; |
| 177 | + if (read_wgcm(make_wgcm(t), &groups, &err)) return fail("accepted a shape whose element count wraps"); |
| 178 | + if (!has(err, "bad data range")) return fail("wrong message: " + err); |
| 179 | + } else if (which == L"own_valid") { |
| 180 | + // control: a well-formed WGRRMOD1 with one blob and one scalar is still accepted |
| 181 | + Bytes f = own_header(1, 1, 256); |
| 182 | + own_blob(f, "a", 0, 256); |
| 183 | + own_scalar(f, "s", 7); |
| 184 | + pad_to(f, 256 + 256); |
| 185 | + seal(f); |
| 186 | + Model m; |
| 187 | + if (!load_file(dir, f, &err, &m)) return fail("a valid file was refused: " + err); |
| 188 | + if (m.image.size() != 256 || m.blob("a") != 0 || m.scalar("s") != 7) return fail("wrong content"); |
| 189 | + } else if (which == L"own_blob_wraps") { |
| 190 | + // off + size wraps around 2^64 to a small value: the blob lies outside the image and must be refused |
| 191 | + Bytes f = own_header(1, 0, 256); |
| 192 | + own_blob(f, "a", 0xFFFFFFFFFFFFFFF0ull, 32); |
| 193 | + pad_to(f, 256 + 256); |
| 194 | + seal(f); |
| 195 | + Model m; |
| 196 | + if (load_file(dir, f, &err, &m)) return fail("accepted a blob whose range wraps"); |
| 197 | + if (!has(err, "blob a")) return fail("wrong message: " + err); |
| 198 | + } else if (which == L"own_image_start_past_end") { |
| 199 | + // the tables end (rounded up to 256) behind the end of the file, and isize wraps so that offset + isize + 32 equals the |
| 200 | + // file size: the image's range would be reversed |
| 201 | + Bytes f = own_header(1, 3, 0xFFFFFFFFFFFFFF00ull); |
| 202 | + own_blob(f, "a", 0, 0); |
| 203 | + own_scalar(f, "s0", 0); |
| 204 | + own_scalar(f, "s1", 0); |
| 205 | + own_scalar(f, "s2", 0); |
| 206 | + f.resize(256); // the file is 256 bytes and its hash: 288 in all (the scalar table runs into the hash, as it may) |
| 207 | + seal(f); |
| 208 | + Model m; |
| 209 | + if (load_file(dir, f, &err, &m)) return fail("accepted an image that starts behind the end of the file"); |
| 210 | + if (!has(err, "image size")) return fail("wrong message: " + err); |
| 211 | + } else { |
| 212 | + return fail("unknown case"); |
| 213 | + } |
| 214 | + printf("OK\n"); |
| 215 | + return 0; |
| 216 | +} |
0 commit comments