.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
binary/
tests/
decode.rad
56.8 KiB
cfg.rad
8.2 KiB
collect.rad
4.3 KiB
linkage.rad
4.8 KiB
program.rad
30.5 KiB
reader.rad
28.5 KiB
tests.rad
30.4 KiB
writer.rad
28.4 KiB
link/
printer/
verify/
binary.rad
14.7 KiB
link.rad
11.8 KiB
printer.rad
31.7 KiB
verify.rad
99.5 KiB
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/il/binary/tests/decode.rad
raw
| 1 | //! Package reconstruction, malformed input, and arena rollback tests. |
| 2 | |
| 3 | use std::collections::dict; |
| 4 | use std::hash::blake3; |
| 5 | use std::lang::il; |
| 6 | use std::lang::il::binary; |
| 7 | use std::lang::il::binary::collect; |
| 8 | use std::lang::il::binary::linkage; |
| 9 | use std::lang::il::binary::program; |
| 10 | use std::lang::il::binary::reader; |
| 11 | use std::mem; |
| 12 | use std::mem::heap; |
| 13 | use std::testing; |
| 14 | |
| 15 | /// Decode arena backing storage. Tests reset it before each use. |
| 16 | static MEMORY: [u8; 2048] = [0; 2048]; |
| 17 | |
| 18 | /// Program without declarations. |
| 19 | constant EMPTY: il::Program = il::Program { |
| 20 | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
| 21 | }; |
| 22 | |
| 23 | /// Structural limits for small test programs. |
| 24 | constant LIMITS: binary::Limits = binary::Limits { registers: 16, blocks: 4 }; |
| 25 | |
| 26 | /// Encode `package` into test storage that the writer borrows for the call. |
| 27 | fn encodeInto(buffer: *mut [u8], package: &binary::Package) -> u32 throws (binary::Error) { |
| 28 | let mut out = buffer; |
| 29 | return try program::encode(&mut out, package); |
| 30 | } |
| 31 | |
| 32 | /// Check a package round trip and failure at every truncated byte length. |
| 33 | unsafe fn roundTrip(package: &binary::Package) throws (testing::TestError) { |
| 34 | static buffer: [u8; 2048] = [0; 2048]; |
| 35 | let length = try encodeInto(&mut buffer[..] as *mut [u8], package) catch { |
| 36 | throw testing::TestError::Failed; |
| 37 | }; |
| 38 | let arena = heap::over(&mut MEMORY[..]); |
| 39 | heap::rewind(arena, 8); |
| 40 | let decoded = try program::decode(&buffer[.. length], arena, LIMITS) catch { |
| 41 | throw testing::TestError::Failed; |
| 42 | }; |
| 43 | try testing::expectBytesEq(decoded.name, package.name); |
| 44 | try testing::expect(decoded.dependencies.len == package.dependencies.len); |
| 45 | try testing::expect(decoded.exports.len == package.exports.len); |
| 46 | try testing::expect(decoded.program.fns.len == package.program.fns.len); |
| 47 | static encoded: [u8; 2048] = [0; 2048]; |
| 48 | let repeated = try encodeInto(&mut encoded[..] as *mut [u8], &decoded) catch { |
| 49 | throw testing::TestError::Failed; |
| 50 | }; |
| 51 | try testing::expectBytesEq(&buffer[.. length], &encoded[.. repeated]); |
| 52 | // Decoded names and string initializers retain arena ownership. |
| 53 | for i in 0 .. length { |
| 54 | set buffer[i] = 0; |
| 55 | } |
| 56 | let copied = try encodeInto(&mut buffer[..] as *mut [u8], &decoded) catch { |
| 57 | throw testing::TestError::Failed; |
| 58 | }; |
| 59 | try testing::expectBytesEq(&buffer[.. copied], &encoded[.. repeated]); |
| 60 | for end in 0 .. length { |
| 61 | heap::rewind(arena, 8); |
| 62 | let mut failed = false; |
| 63 | try program::decode(&buffer[.. end], arena, LIMITS) catch err { |
| 64 | try testing::expect(err == binary::Error::Truncated); |
| 65 | set failed = true; |
| 66 | }; |
| 67 | try testing::expect(failed); |
| 68 | try testing::expect(heap::used(arena) == 8); |
| 69 | } |
| 70 | // Exercise every smaller arena extent, including alignment boundaries. |
| 71 | let usedArena = heap::over(&mut MEMORY[..]); |
| 72 | try program::decode(&buffer[.. length], usedArena, LIMITS) catch { |
| 73 | throw testing::TestError::Failed; |
| 74 | }; |
| 75 | let required = heap::used(usedArena); |
| 76 | for capacity in 0 .. required { |
| 77 | let short = heap::withCapacity(&mut MEMORY[..], capacity); |
| 78 | let mut failed = false; |
| 79 | try program::decode(&buffer[.. length], short, LIMITS) catch err { |
| 80 | try testing::expect(err == binary::Error::Storage); |
| 81 | set failed = true; |
| 82 | }; |
| 83 | try testing::expect(failed); |
| 84 | try testing::expect(heap::used(short) == 0); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Check an empty program with a package identity. |
| 89 | @test unsafe fn emptyPackage() throws (testing::TestError) { |
| 90 | let package = binary::Package { |
| 91 | version: "1.2.3", |
| 92 | symbols: &["p"], |
| 93 | name: "p", |
| 94 | dependencies: &[], |
| 95 | exports: &[], |
| 96 | entry: nil, |
| 97 | program: EMPTY, |
| 98 | links: binary::EMPTY_LINKS, |
| 99 | }; |
| 100 | try roundTrip(&package); |
| 101 | static buffer: [u8; 128] = [0; 128]; |
| 102 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
| 103 | throw testing::TestError::Failed; |
| 104 | }; |
| 105 | try testing::expectBytesEq( |
| 106 | &buffer[.. length], |
| 107 | &[ |
| 108 | 82, 73, 76, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, |
| 109 | 112, 0, 0, 0, 0, 1, 5, 0, 0, 0, 49, 46, 50, 46, 51, 0, |
| 110 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 111 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 112 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 113 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 114 | ] |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /// Check functions, dependencies, exports, initializers, and block metadata. |
| 119 | @test unsafe fn fullPackage() throws (testing::TestError) { |
| 120 | let params: *[il::Param] = &[ |
| 121 | il::Param { |
| 122 | value: il::Reg { n: 0 }, |
| 123 | type: il::ValType::Data( |
| 124 | il::Type::W64 |
| 125 | ), |
| 126 | }, |
| 127 | ]; |
| 128 | let blocks: *[il::Block] = &[ |
| 129 | il::Block { |
| 130 | label: il::BlockLabel { base: "entry", suffix: 2 }, |
| 131 | params: &[], |
| 132 | instrs: &[ |
| 133 | il::Instr::Call { |
| 134 | retTy: il::ValType::Data(il::Type::W64), |
| 135 | dst: il::Reg { n: 2 }, |
| 136 | func: il::Val::FnAddr("dep::fn"), |
| 137 | args: il::ArgRange { start: 0, len: 1 }, |
| 138 | }, |
| 139 | il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 2 }) }, |
| 140 | ], |
| 141 | locs: &[], |
| 142 | preds: &[], |
| 143 | loopDepth: 0, |
| 144 | }, |
| 145 | ]; |
| 146 | let func = il::Fn { |
| 147 | name: "p::main", |
| 148 | params, |
| 149 | returnType: il::ValType::Data(il::Type::W64), |
| 150 | isExtern: false, |
| 151 | isLeaf: false, |
| 152 | blocks, |
| 153 | vals: &[il::Val::Reg(il::Reg { n: 0 })], |
| 154 | cases: &[], |
| 155 | borrows: &[], |
| 156 | }; |
| 157 | let external = il::Fn { |
| 158 | name: "dep::fn", |
| 159 | params, |
| 160 | returnType: il::ValType::Data(il::Type::W64), |
| 161 | isExtern: true, |
| 162 | isLeaf: true, |
| 163 | blocks: &[], |
| 164 | vals: &[], |
| 165 | cases: &[], |
| 166 | // Decoding keeps the borrowed parameter, which re-encoding compares. |
| 167 | borrows: &[il::Borrow { target: nil, result: 0, param: 0, source: 0 }], |
| 168 | }; |
| 169 | let data = il::Data { |
| 170 | name: "p::data", |
| 171 | size: 64, |
| 172 | alignment: 8, |
| 173 | layout: 0, |
| 174 | readOnly: false, |
| 175 | heap: nil, |
| 176 | isZeroInit: false, |
| 177 | values: &[ |
| 178 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W8, val: -1 }, count: 2 }, |
| 179 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W16, val: -2 }, count: 1 }, |
| 180 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W32, val: -3 }, count: 1 }, |
| 181 | il::DataValue { item: il::DataItem::Val { typ: il::Type::W64, val: -4 }, count: 1 }, |
| 182 | il::DataValue { item: il::DataItem::Sym("p::data"), count: 1 }, |
| 183 | il::DataValue { item: il::DataItem::Fn("dep::fn"), count: 1 }, |
| 184 | il::DataValue { item: il::DataItem::Str("bytes"), count: 2 }, |
| 185 | il::DataValue { item: il::DataItem::Str(""), count: 0 }, |
| 186 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
| 187 | ], |
| 188 | }; |
| 189 | let zero = il::Data { |
| 190 | name: "p::zero", |
| 191 | size: 4096, |
| 192 | alignment: 4096, |
| 193 | layout: 1, |
| 194 | readOnly: false, |
| 195 | heap: nil, |
| 196 | isZeroInit: true, |
| 197 | values: &[], |
| 198 | }; |
| 199 | static FIXTURE: [u8; 512] = [0; 512]; |
| 200 | let arena = heap::over(&mut FIXTURE[..]); |
| 201 | let mut list: *mut [*il::Fn] = &mut []; |
| 202 | try! list.append(retainFn(func, arena), arena); |
| 203 | try! list.append(retainFn(external, arena), arena); |
| 204 | let functions: *[*il::Fn] = &list[..]; |
| 205 | let package = binary::Package { |
| 206 | version: nil, |
| 207 | symbols: &["p", "dep", "p::main", "dep::fn", "p::data", "p::zero"], |
| 208 | name: "p", |
| 209 | dependencies: &["dep"], |
| 210 | exports: &[ |
| 211 | binary::Export { |
| 212 | name: "p::main", |
| 213 | kind: binary::ExportKind::Function(0), |
| 214 | startable: false, |
| 215 | }, |
| 216 | binary::Export { |
| 217 | name: "p::data", |
| 218 | kind: binary::ExportKind::Data(0), |
| 219 | startable: false, |
| 220 | }, |
| 221 | ], |
| 222 | entry: "p::main", |
| 223 | program: il::Program { |
| 224 | data: retainData(&[data, zero]), |
| 225 | fns: functions, |
| 226 | layouts: &[ |
| 227 | il::Layout { size: 64, alignment: 8, slots: &[], regions: 0 }, |
| 228 | il::Layout { size: 4096, alignment: 4096, slots: &[], regions: 0 }, |
| 229 | ], |
| 230 | signatures: &[], |
| 231 | traits: &[], |
| 232 | }, |
| 233 | links: binary::Links { |
| 234 | metadata: binary::Id { bytes: [0; linkage::ID_BYTES] }, |
| 235 | pins: &[binary::Id { bytes: [1; linkage::ID_BYTES] }], |
| 236 | imports: &[], |
| 237 | image: nil, |
| 238 | verified: false, |
| 239 | }, |
| 240 | }; |
| 241 | try roundTrip(&package); |
| 242 | } |
| 243 | |
| 244 | /// Reject an invalid encoded instruction without a panic. |
| 245 | unsafe fn badInstruction(bytes: *[u8], expected: binary::Error) throws (testing::TestError) { |
| 246 | try badInstructionIn(bytes, 0, expected); |
| 247 | } |
| 248 | |
| 249 | /// Reject an invalid encoded instruction in a package whose layout table has `layouts` entries. |
| 250 | unsafe fn badInstructionIn(bytes: &'wire [u8], layouts: u32, expected: binary::Error) |
| 251 | throws (testing::TestError) |
| 252 | { |
| 253 | let arena = heap::withCapacity(&mut MEMORY[..], 64); |
| 254 | let mut input = reader::new(bytes); |
| 255 | set input.symbols = &["p"]; |
| 256 | set input.registers = 2; |
| 257 | set input.blocks = 1; |
| 258 | set input.layouts = layouts; |
| 259 | let mut failed = false; |
| 260 | try input.instr(arena) catch err { |
| 261 | try testing::expect(err == expected); |
| 262 | set failed = true; |
| 263 | }; |
| 264 | try testing::expect(failed); |
| 265 | } |
| 266 | |
| 267 | /// Check instruction tags, operand tags, types, optional flags, and indices. |
| 268 | @test unsafe fn malformedInstructions() throws (testing::TestError) { |
| 269 | try badInstruction(&[26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], binary::Error::Invalid); |
| 270 | try badInstruction(&[255], binary::Error::Invalid); |
| 271 | try badInstruction(&[5, 0, 0, 0, 0, 255], binary::Error::Invalid); |
| 272 | try badInstruction(&[1, 3], binary::Error::Invalid); |
| 273 | try badInstruction(&[6, 255], binary::Error::Invalid); |
| 274 | try badInstruction(&[7, 255], binary::Error::Invalid); |
| 275 | try badInstruction(&[13, 255], binary::Error::Invalid); |
| 276 | try badInstruction(&[11, 2], binary::Error::Invalid); |
| 277 | try badInstruction(&[10, 8, 2], binary::Error::Invalid); |
| 278 | try badInstruction(&[5, 2, 0, 0, 0], binary::Error::Invalid); |
| 279 | try badInstruction(&[5, 0, 0, 0, 0, 0, 2, 0, 0, 0], binary::Error::Invalid); |
| 280 | try badInstruction(&[12, 1, 0, 0, 0], binary::Error::Invalid); |
| 281 | try badInstruction(&[5, 0, 0, 0, 0, 2, 1, 0, 0, 0], binary::Error::Symbol); |
| 282 | try badInstruction(&[12, 0, 0, 0, 0, 255, 255, 255, 255], binary::Error::Truncated); |
| 283 | // The receiver of a trait call is one of its arguments. |
| 284 | try badInstruction( |
| 285 | &[30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid |
| 286 | ); |
| 287 | // No layout table, so no layout index is valid. |
| 288 | try badInstruction(&[0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid); |
| 289 | try badInstruction( |
| 290 | &[22, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0], binary::Error::Invalid |
| 291 | ); |
| 292 | try badInstruction(&[23, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0], binary::Error::Invalid); |
| 293 | try badInstruction(&[24, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 4, 4], binary::Error::Invalid); |
| 294 | try badInstruction(&[25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4], binary::Error::Invalid); |
| 295 | // Registers and the layout of a blit are range checked. |
| 296 | try badInstruction(&[21, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], binary::Error::Invalid); |
| 297 | try badInstruction(&[4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128], binary::Error::Invalid); |
| 298 | try badInstructionIn(&[4, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0], 2, binary::Error::Invalid); |
| 299 | try badInstructionIn( |
| 300 | &[4, 0, 0, 0, 0, 1, 0, 0, 0, 255, 255, 255, 255], 2, binary::Error::Invalid |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | /// Check header validation and package symbol indices with arena rollback. |
| 305 | @test unsafe fn malformedPackages() throws (testing::TestError) { |
| 306 | let package = binary::Package { |
| 307 | version: nil, |
| 308 | symbols: &["p"], |
| 309 | name: "p", |
| 310 | dependencies: &[], |
| 311 | exports: &[], |
| 312 | entry: nil, |
| 313 | program: EMPTY, |
| 314 | links: binary::EMPTY_LINKS, |
| 315 | }; |
| 316 | static buffer: [u8; 128] = [0; 128]; |
| 317 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
| 318 | throw testing::TestError::Failed; |
| 319 | }; |
| 320 | let arena = heap::withCapacity(&mut MEMORY[..], 64); |
| 321 | for offset in &[0, 4, 13, 17, 21, 58] { |
| 322 | let original = buffer[offset]; |
| 323 | set buffer[offset] = 255; |
| 324 | heap::rewind(arena, 8); |
| 325 | let mut failed = false; |
| 326 | try program::decode(&buffer[.. length], arena, LIMITS) catch { |
| 327 | set failed = true; |
| 328 | }; |
| 329 | try testing::expect(failed); |
| 330 | try testing::expect(heap::used(arena) == 8); |
| 331 | set buffer[offset] = original; |
| 332 | } |
| 333 | let mut trailing = false; |
| 334 | try program::decode(&buffer[.. length + 1], arena, LIMITS) catch err { |
| 335 | try testing::expect(err == binary::Error::Invalid); |
| 336 | set trailing = true; |
| 337 | }; |
| 338 | try testing::expect(trailing); |
| 339 | try testing::expect(heap::used(arena) == 8); |
| 340 | } |
| 341 | |
| 342 | /// Encode a malformed package and check rejection with arena rollback. |
| 343 | unsafe fn rejected(package: &binary::Package, limits: binary::Limits) throws (testing::TestError) { |
| 344 | static buffer: [u8; 512] = [0; 512]; |
| 345 | let length = try encodeInto(&mut buffer[..] as *mut [u8], package) catch { |
| 346 | throw testing::TestError::Failed; |
| 347 | }; |
| 348 | let arena = heap::withCapacity(&mut MEMORY[..], 512); |
| 349 | heap::rewind(arena, 8); |
| 350 | let mut failed = false; |
| 351 | try program::decode(&buffer[.. length], arena, limits) catch err { |
| 352 | try testing::expect(err == binary::Error::Invalid); |
| 353 | set failed = true; |
| 354 | }; |
| 355 | try testing::expect(failed); |
| 356 | try testing::expect(heap::used(arena) == 8); |
| 357 | } |
| 358 | |
| 359 | /// Check initializer extent arithmetic, layout indices, and duplicate symbols. |
| 360 | @test unsafe fn invalidData() throws (testing::TestError) { |
| 361 | let mut item = il::Data { |
| 362 | name: "p", |
| 363 | size: 0xffffffff, |
| 364 | alignment: 8, |
| 365 | layout: 0, |
| 366 | readOnly: false, |
| 367 | heap: nil, |
| 368 | isZeroInit: false, |
| 369 | values: &[ |
| 370 | il::DataValue { |
| 371 | item: il::DataItem::Val { typ: il::Type::W64, val: 0 }, count: 0xffffffff |
| 372 | }, |
| 373 | ], |
| 374 | }; |
| 375 | let mut package = binary::Package { |
| 376 | version: nil, |
| 377 | symbols: &["p"], |
| 378 | name: "p", |
| 379 | dependencies: &[], |
| 380 | exports: &[], |
| 381 | entry: nil, |
| 382 | program: il::Program { |
| 383 | data: retainData(&[item]), |
| 384 | fns: &[], |
| 385 | layouts: &[il::Layout { size: 0xffffffff, alignment: 8, slots: &[], regions: 0 }], |
| 386 | signatures: &[], |
| 387 | traits: &[], |
| 388 | }, |
| 389 | links: binary::EMPTY_LINKS, |
| 390 | }; |
| 391 | try rejected(&package, LIMITS); |
| 392 | set item.values = &[ |
| 393 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
| 394 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
| 395 | ]; |
| 396 | set package.program = il::Program { |
| 397 | data: retainData(&[item]), |
| 398 | fns: &[], |
| 399 | layouts: &[il::Layout { size: 1, alignment: 1, slots: &[], regions: 0 }], |
| 400 | signatures: &[], |
| 401 | traits: &[], |
| 402 | }; |
| 403 | try rejected(&package, LIMITS); |
| 404 | // The layout index must name a layout of the package. |
| 405 | set item.values = &[]; |
| 406 | set item.layout = 1; |
| 407 | set package.program = il::Program { |
| 408 | data: retainData(&[item]), |
| 409 | fns: &[], |
| 410 | layouts: &[il::Layout { size: 1, alignment: 1, slots: &[], regions: 0 }], |
| 411 | signatures: &[], |
| 412 | traits: &[], |
| 413 | }; |
| 414 | try rejected(&package, LIMITS); |
| 415 | set package.program = EMPTY; |
| 416 | set package.symbols = &["p", "p"]; |
| 417 | try rejected(&package, LIMITS); |
| 418 | set package.symbols = &["p", ""]; |
| 419 | try rejected(&package, LIMITS); |
| 420 | } |
| 421 | |
| 422 | /// Store `func` in `arena` and return its address. |
| 423 | unsafe fn retainFn(func: il::Fn, arena: &heap::Heap) -> *il::Fn { |
| 424 | let mut funcs: *mut [il::Fn] = &mut []; |
| 425 | try! funcs.append(func, arena); |
| 426 | return &mut funcs[0]; |
| 427 | } |
| 428 | |
| 429 | /// Encode a package that holds only `func` and check its rejection. |
| 430 | unsafe fn rejectedFn(func: il::Fn, limits: binary::Limits) throws (testing::TestError) { |
| 431 | static FIXTURE: [u8; 512] = [0; 512]; |
| 432 | let arena = heap::over(&mut FIXTURE[..]); |
| 433 | let mut list: *mut [*il::Fn] = &mut []; |
| 434 | try! list.append(retainFn(func, arena), arena); |
| 435 | let package = binary::Package { |
| 436 | version: nil, |
| 437 | symbols: &["p"], |
| 438 | name: "p", |
| 439 | dependencies: &[], |
| 440 | exports: &[], |
| 441 | entry: nil, |
| 442 | program: il::Program { |
| 443 | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
| 444 | }, |
| 445 | links: binary::EMPTY_LINKS, |
| 446 | }; |
| 447 | try rejected(&package, limits); |
| 448 | } |
| 449 | |
| 450 | /// Check that a package holding only `func` cannot be encoded. |
| 451 | unsafe fn unencodable(func: il::Fn) throws (testing::TestError) { |
| 452 | static FIXTURE: [u8; 512] = [0; 512]; |
| 453 | static buffer: [u8; 512] = [0; 512]; |
| 454 | let arena = heap::over(&mut FIXTURE[..]); |
| 455 | let mut list: *mut [*il::Fn] = &mut []; |
| 456 | try! list.append(retainFn(func, arena), arena); |
| 457 | let package = binary::Package { |
| 458 | version: nil, |
| 459 | symbols: &["p"], |
| 460 | name: "p", |
| 461 | dependencies: &[], |
| 462 | exports: &[], |
| 463 | entry: nil, |
| 464 | program: il::Program { |
| 465 | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
| 466 | }, |
| 467 | links: binary::EMPTY_LINKS, |
| 468 | }; |
| 469 | let mut failed = false; |
| 470 | try encodeInto(&mut buffer[..] as *mut [u8], &package) catch err { |
| 471 | try testing::expect(err == binary::Error::Invalid); |
| 472 | set failed = true; |
| 473 | }; |
| 474 | try testing::expect(failed); |
| 475 | } |
| 476 | |
| 477 | /// Check function block limits, parameter counts, external bodies, and branch targets. |
| 478 | @test unsafe fn invalidFunctions() throws (testing::TestError) { |
| 479 | let blocks: *[il::Block] = &[ |
| 480 | il::Block { |
| 481 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 482 | params: &[], |
| 483 | instrs: &[il::Instr::Ret { val: nil }], |
| 484 | locs: &[], |
| 485 | preds: &[], |
| 486 | loopDepth: 0, |
| 487 | }, |
| 488 | ]; |
| 489 | try rejectedFn(il::Fn { |
| 490 | name: "p", |
| 491 | params: &[], |
| 492 | returnType: il::ValType::Data(il::Type::W64), |
| 493 | isExtern: false, |
| 494 | isLeaf: true, |
| 495 | blocks, |
| 496 | vals: &[], |
| 497 | cases: &[], |
| 498 | borrows: &[], |
| 499 | }, binary::Limits { registers: 16, blocks: 0 }); |
| 500 | // An external function has no blocks, and every other function has an entry block. |
| 501 | try unencodable(il::Fn { |
| 502 | name: "p", |
| 503 | params: &[], |
| 504 | returnType: il::ValType::Data(il::Type::W64), |
| 505 | isExtern: true, |
| 506 | isLeaf: true, |
| 507 | blocks, |
| 508 | vals: &[], |
| 509 | cases: &[], |
| 510 | borrows: &[], |
| 511 | }); |
| 512 | try unencodable(il::Fn { |
| 513 | name: "p", |
| 514 | params: &[], |
| 515 | returnType: il::ValType::Data(il::Type::W64), |
| 516 | isExtern: false, |
| 517 | isLeaf: true, |
| 518 | blocks: &[], |
| 519 | vals: &[], |
| 520 | cases: &[], |
| 521 | borrows: &[], |
| 522 | }); |
| 523 | // Function parameters are registers zero through the parameter count. |
| 524 | try unencodable(il::Fn { |
| 525 | name: "p", |
| 526 | params: &[il::Param { value: il::Reg { n: 1 }, type: il::ValType::Data(il::Type::W64) }], |
| 527 | returnType: il::ValType::Data(il::Type::W64), |
| 528 | isExtern: false, |
| 529 | isLeaf: true, |
| 530 | blocks, |
| 531 | vals: &[], |
| 532 | cases: &[], |
| 533 | borrows: &[], |
| 534 | }); |
| 535 | try rejectedFn( |
| 536 | il::Fn { |
| 537 | name: "p", |
| 538 | params: &[ |
| 539 | il::Param { value: il::Reg { n: 0 }, type: il::ValType::Data(il::Type::W64) } |
| 540 | ], |
| 541 | returnType: il::ValType::Data(il::Type::W64), |
| 542 | isExtern: false, |
| 543 | isLeaf: true, |
| 544 | blocks, |
| 545 | vals: &[], |
| 546 | cases: &[], |
| 547 | borrows: &[], |
| 548 | }, |
| 549 | binary::Limits { registers: 0, blocks: 4 } |
| 550 | ); |
| 551 | try rejectedFn( |
| 552 | il::Fn { |
| 553 | name: "p", |
| 554 | params: &[], |
| 555 | returnType: il::ValType::Data(il::Type::W64), |
| 556 | isExtern: false, |
| 557 | isLeaf: true, |
| 558 | blocks: &[ |
| 559 | il::Block { |
| 560 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 561 | params: &[], |
| 562 | instrs: &[ |
| 563 | il::Instr::Jmp { target: 1, args: il::ArgRange { start: 0, len: 0 } } |
| 564 | ], |
| 565 | locs: &[], |
| 566 | preds: &[], |
| 567 | loopDepth: 0, |
| 568 | }, |
| 569 | ], |
| 570 | vals: &[], |
| 571 | cases: &[], |
| 572 | borrows: &[], |
| 573 | }, |
| 574 | LIMITS |
| 575 | ); |
| 576 | } |
| 577 | |
| 578 | /// Indexed headers and streaming functions match a complete package encoding. |
| 579 | /// Encoding errors return buffer and symbol storage to their owners. |
| 580 | @test unsafe fn streamingEncoding() throws (testing::TestError) { |
| 581 | static whole: [u8; 160] = [0; 160]; |
| 582 | static split: [u8; 160] = [0; 160]; |
| 583 | unsafe static slots: [dict::Entry; 8] = [dict::Entry { key: "", value: 0 }; 8]; |
| 584 | static fixture: [u8; 2048] = [0; 2048]; |
| 585 | let arena = heap::over(&mut fixture[..]); |
| 586 | let func = retainFn( |
| 587 | il::Fn { |
| 588 | name: "p::f", |
| 589 | params: &[], |
| 590 | returnType: il::ValType::Data(il::Type::W64), |
| 591 | isExtern: false, |
| 592 | isLeaf: true, |
| 593 | blocks: &[ |
| 594 | il::Block { |
| 595 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 596 | params: &[], |
| 597 | instrs: &[il::Instr::Ret { val: nil }], |
| 598 | locs: &[], |
| 599 | preds: &[], |
| 600 | loopDepth: 0, |
| 601 | }, |
| 602 | ], |
| 603 | vals: &[], |
| 604 | cases: &[], |
| 605 | borrows: &[], |
| 606 | }, |
| 607 | arena |
| 608 | ); |
| 609 | let mut list: *mut [*il::Fn] = &mut []; |
| 610 | try! list.append(func, arena); |
| 611 | let package = binary::Package { |
| 612 | version: nil, |
| 613 | symbols: &["p", "p::f"], |
| 614 | name: "p", |
| 615 | dependencies: &[], |
| 616 | exports: &[], |
| 617 | entry: nil, |
| 618 | program: il::Program { |
| 619 | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
| 620 | }, |
| 621 | links: binary::EMPTY_LINKS, |
| 622 | }; |
| 623 | let length = try! encodeInto(&mut whole[..] as *mut [u8], &package); |
| 624 | let mut index = dict::init(&mut slots[..] as *mut [dict::Entry]); |
| 625 | try! dict::insert(&mut index, "p", 0); |
| 626 | try! dict::insert(&mut index, "p::f", 1); |
| 627 | let mut bytes = &mut split[..] as *mut [u8]; |
| 628 | let head = try! program::encodeHead(&mut bytes, &package, &mut index, 1); |
| 629 | assert bytes.len == 160 and index.count == 2 and dict::get(&index, "p::f") == 1; |
| 630 | |
| 631 | // Streaming collection assigns first-use indices while encoding the same bytes. |
| 632 | let mut names = try! collect::new(8, arena); |
| 633 | try! collect::start(&mut names, "p", arena); |
| 634 | let end = try! program::appendCollected(&mut bytes, head, &mut names, func, arena); |
| 635 | assert names.symbols.len == 2 and mem::eq(names.symbols[1], "p::f"); |
| 636 | assert names.index.count == 2 and bytes.len == 160; |
| 637 | try testing::expectBytesEq(&whole[.. length], &bytes[.. end]); |
| 638 | let mut limited = &mut split[.. head + 8] as *mut [u8]; |
| 639 | let mut failed = false; |
| 640 | try program::appendCollected(&mut limited, head, &mut names, func, arena) catch error { |
| 641 | assert error == binary::Error::Capacity; |
| 642 | set failed = true; |
| 643 | }; |
| 644 | assert failed and limited.len == head + 8 and names.index.count == 2; |
| 645 | |
| 646 | // A short buffer and an unknown name fail, and both owners get their storage back. |
| 647 | let mut short = &mut split[.. head - 1] as *mut [u8]; |
| 648 | set failed = false; |
| 649 | try program::encodeHead(&mut short, &package, &mut index, 1) catch error { |
| 650 | assert error == binary::Error::Capacity; |
| 651 | set failed = true; |
| 652 | }; |
| 653 | assert failed and short.len == head - 1 and dict::get(&index, "p::f") == 1; |
| 654 | let mut missing = dict::init(&mut slots[..] as *mut [dict::Entry]); |
| 655 | set failed = false; |
| 656 | try program::encodeHead(&mut bytes, &package, &mut missing, 1) catch error { |
| 657 | assert error == binary::Error::Symbol; |
| 658 | set failed = true; |
| 659 | }; |
| 660 | assert failed and bytes.len == 160 and missing.count == 0; |
| 661 | } |
| 662 | |
| 663 | /// Reject unknown initializer tags and widths before reading their payloads. |
| 664 | @test unsafe fn malformedInitializers() throws (testing::TestError) { |
| 665 | let arena = heap::withCapacity(&mut MEMORY[..], 8); |
| 666 | let fixtures: [*[u8]; 2] = [&[255], &[0, 3]]; |
| 667 | for bytes in &fixtures[..] { |
| 668 | let wire: &'wire [u8] = &bytes[..] { |
| 669 | let mut input = reader::new(wire); |
| 670 | let mut failed = false; |
| 671 | try input.dataValue(arena) catch err { |
| 672 | try testing::expect(err == binary::Error::Invalid); |
| 673 | set failed = true; |
| 674 | }; |
| 675 | try testing::expect(failed); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /// Collect the names of `local` for package `owner` in `arena`. |
| 681 | unsafe fn collected( |
| 682 | owner: *[u8], dependencies: *[*[u8]], local: il::Program, arena: &heap::Heap |
| 683 | ) -> binary::Package |
| 684 | throws (binary::Error) |
| 685 | { |
| 686 | let mut names = try collect::new(64, arena); |
| 687 | try collect::start(&mut names, owner, arena); |
| 688 | return try collect::finish(&mut names, owner, dependencies, local, &[], nil, arena); |
| 689 | } |
| 690 | |
| 691 | /// Check explicit dependencies, collection storage and package ownership. |
| 692 | @test unsafe fn collectionBounds() throws (testing::TestError) { |
| 693 | let local = il::Program { |
| 694 | data: &[ |
| 695 | il::Data { |
| 696 | name: "p::data", |
| 697 | size: 8, |
| 698 | alignment: 8, |
| 699 | layout: 0, |
| 700 | readOnly: false, |
| 701 | heap: nil, |
| 702 | isZeroInit: false, |
| 703 | values: &[il::DataValue { item: il::DataItem::Fn("dep::fn"), count: 1 }], |
| 704 | }, |
| 705 | ], |
| 706 | fns: &[], |
| 707 | layouts: &[], |
| 708 | signatures: &[], |
| 709 | traits: &[], |
| 710 | }; |
| 711 | let arena = heap::over(&mut MEMORY[..]); |
| 712 | let mut failed = false; |
| 713 | try collected("other", &["provider"], local, arena) catch err { |
| 714 | try testing::expect(err == binary::Error::Invalid); |
| 715 | set failed = true; |
| 716 | }; |
| 717 | try testing::expect(failed); |
| 718 | heap::rewind(arena, 0); |
| 719 | let package = try collected("p", &["provider"], local, arena) catch { |
| 720 | throw testing::TestError::Failed; |
| 721 | }; |
| 722 | assert package.dependencies.len == 1; |
| 723 | assert mem::eq(package.dependencies[0], "provider"); |
| 724 | try testing::expect(package.symbols.len == 4); |
| 725 | let required = heap::used(arena); |
| 726 | for capacity in 0 .. required { |
| 727 | let short = heap::withCapacity(&mut MEMORY[..], capacity); |
| 728 | set failed = false; |
| 729 | try collected("p", &["provider"], local, short) catch err { |
| 730 | try testing::expect(err == binary::Error::Storage); |
| 731 | set failed = true; |
| 732 | }; |
| 733 | try testing::expect(failed); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /// Copy data fixtures into stable storage for the package descriptor. |
| 738 | unsafe fn retainData(items: &[il::Data]) -> *[il::Data] { |
| 739 | unsafe static DATA: [il::Data; 2] = undefined; |
| 740 | assert items.len <= DATA.len; |
| 741 | for item, i in items { |
| 742 | set DATA[i] = item; |
| 743 | } |
| 744 | return &DATA[.. items.len]; |
| 745 | } |
| 746 | |
| 747 | /// Layout of eight bytes of plain data. |
| 748 | constant PLAIN: il::Layout = il::Layout { size: 8, alignment: 8, slots: &[], regions: 0 }; |
| 749 | |
| 750 | /// Mutable reference to layout 1, the type of the pointer parameters of the fixtures. |
| 751 | constant REF_L1: il::PtrType = il::PtrType { |
| 752 | layout: 1, own: false, mutable: true, nullable: false |
| 753 | }; |
| 754 | |
| 755 | /// Owned nullable pointer to layout 0 and to layout 1, as stored in a slot. |
| 756 | constant SLOT_L0: il::PtrType = il::PtrType { |
| 757 | layout: 0, own: true, mutable: false, nullable: true |
| 758 | }; |
| 759 | |
| 760 | /// Layout of a pointer-sized object that holds an owned pointer to layout 0. |
| 761 | constant PTR_LAYOUT: il::Layout = il::Layout { |
| 762 | size: 8, |
| 763 | alignment: 8, |
| 764 | slots: &[ |
| 765 | il::Slot { |
| 766 | offset: 0, |
| 767 | kind: il::SlotKind::Ptr(il::PtrType { |
| 768 | layout: 0, own: true, mutable: true, nullable: true |
| 769 | }), |
| 770 | regions: &[], |
| 771 | }, |
| 772 | ], |
| 773 | regions: 0, |
| 774 | }; |
| 775 | |
| 776 | /// Whether two slots have the same offset, kind and regions. |
| 777 | fn sameSlot(a: il::Slot, b: il::Slot) -> bool { |
| 778 | if a.offset <> b.offset or a.kind <> b.kind or a.regions.len <> b.regions.len { |
| 779 | return false; |
| 780 | } |
| 781 | for region, i in a.regions { |
| 782 | if region <> b.regions[i] { |
| 783 | return false; |
| 784 | } |
| 785 | } |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | /// Decode `package` after encoding it, in a test arena that the caller resets. |
| 790 | unsafe fn reencoded(package: &binary::Package, arena: &heap::Heap) -> binary::Package |
| 791 | throws (testing::TestError) |
| 792 | { |
| 793 | static buffer: [u8; 2048] = [0; 2048]; |
| 794 | let length = try encodeInto(&mut buffer[..] as *mut [u8], package) catch { |
| 795 | throw testing::TestError::Failed; |
| 796 | }; |
| 797 | return try program::decode(&buffer[.. length], arena, LIMITS) catch { |
| 798 | throw testing::TestError::Failed; |
| 799 | }; |
| 800 | } |
| 801 | |
| 802 | /// Check that layouts, signatures, and value types survive a round trip. |
| 803 | @test unsafe fn typedPackage() throws (testing::TestError) { |
| 804 | let layouts: *[il::Layout] = &[ |
| 805 | il::Layout { size: 8, alignment: 8, slots: &[], regions: 0 }, |
| 806 | PTR_LAYOUT, |
| 807 | il::Layout { |
| 808 | size: 48, |
| 809 | alignment: 8, |
| 810 | slots: &[ |
| 811 | il::Slot { offset: 0, kind: il::SlotKind::Sub(1), regions: &[] }, |
| 812 | il::Slot { offset: 16, kind: il::SlotKind::Slice(SLOT_L0), regions: &[] }, |
| 813 | il::Slot { |
| 814 | offset: 32, kind: il::SlotKind::Array { layout: 1, count: 2 }, regions: &[] |
| 815 | }, |
| 816 | ], |
| 817 | regions: 0, |
| 818 | }, |
| 819 | ]; |
| 820 | let signatures: *[il::Signature] = &[ |
| 821 | il::Signature { |
| 822 | params: &[il::ValType::Ptr(REF_L1), il::ValType::Data(il::Type::W32)], |
| 823 | ret: il::ValType::Void, |
| 824 | borrows: &[], |
| 825 | }, |
| 826 | il::Signature { params: &[], ret: il::ValType::Fn(0), borrows: &[] }, |
| 827 | il::Signature { |
| 828 | params: &[il::ValType::Ptr(REF_L1)], |
| 829 | ret: il::ValType::Ptr(REF_L1), |
| 830 | borrows: &[ |
| 831 | il::Borrow { target: nil, result: 0, param: 0, source: 0 }, |
| 832 | il::Borrow { target: nil, result: 1, param: 0, source: nil }, |
| 833 | ], |
| 834 | }, |
| 835 | ]; |
| 836 | let blocks: *[il::Block] = &[ |
| 837 | il::Block { |
| 838 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 839 | params: &[], |
| 840 | instrs: &[ |
| 841 | il::Instr::Reserve { dst: il::Reg { n: 2 }, layout: 2 }, |
| 842 | il::Instr::Blit { dst: il::Reg { n: 2 }, src: il::Reg { n: 0 }, layout: 2 }, |
| 843 | il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 2 }) }, |
| 844 | ], |
| 845 | locs: &[], |
| 846 | preds: &[], |
| 847 | loopDepth: 0, |
| 848 | }, |
| 849 | ]; |
| 850 | static FIXTURE: [u8; 1024] = [0; 1024]; |
| 851 | let fixture = heap::over(&mut FIXTURE[..]); |
| 852 | let func = retainFn( |
| 853 | il::Fn { |
| 854 | name: "p::f", |
| 855 | params: &[ |
| 856 | il::Param { value: il::Reg { n: 0 }, type: il::ValType::Ptr(REF_L1) }, |
| 857 | il::Param { value: il::Reg { n: 1 }, type: il::ValType::Fn(1) }, |
| 858 | ], |
| 859 | returnType: il::ValType::Ptr(il::PtrType { |
| 860 | layout: 2, own: true, mutable: false, nullable: true |
| 861 | }), |
| 862 | isExtern: false, |
| 863 | isLeaf: true, |
| 864 | blocks, |
| 865 | vals: &[], |
| 866 | cases: &[], |
| 867 | borrows: &[], |
| 868 | }, |
| 869 | fixture |
| 870 | ); |
| 871 | let mut list: *mut [*il::Fn] = &mut []; |
| 872 | try! list.append(func, fixture); |
| 873 | let package = binary::Package { |
| 874 | version: nil, |
| 875 | symbols: &["p", "p::f"], |
| 876 | name: "p", |
| 877 | dependencies: &[], |
| 878 | exports: &[], |
| 879 | entry: nil, |
| 880 | program: il::Program { data: &[], fns: &list[..], layouts, signatures, traits: &[] }, |
| 881 | links: binary::EMPTY_LINKS, |
| 882 | }; |
| 883 | try roundTrip(&package); |
| 884 | |
| 885 | let arena = heap::over(&mut MEMORY[..]); |
| 886 | let decoded = try reencoded(&package, arena); |
| 887 | try testing::expect(decoded.program.layouts.len == 3 and decoded.program.signatures.len == 3); |
| 888 | for expected, i in layouts { |
| 889 | let actual = decoded.program.layouts[i]; |
| 890 | try testing::expect( |
| 891 | actual.size == expected.size |
| 892 | and actual.alignment == expected.alignment |
| 893 | and actual.regions == expected.regions |
| 894 | ); |
| 895 | try testing::expect(actual.slots.len == expected.slots.len); |
| 896 | for slot, j in expected.slots { |
| 897 | try testing::expect(sameSlot(actual.slots[j], slot)); |
| 898 | } |
| 899 | } |
| 900 | for expected, i in signatures { |
| 901 | let actual = decoded.program.signatures[i]; |
| 902 | try testing::expect( |
| 903 | actual.ret == expected.ret |
| 904 | and actual.params.len == expected.params.len |
| 905 | and actual.borrows.len == expected.borrows.len |
| 906 | ); |
| 907 | for param, j in expected.params { |
| 908 | try testing::expect(actual.params[j] == param); |
| 909 | } |
| 910 | for source, j in expected.borrows { |
| 911 | assert actual.borrows[j] == source; |
| 912 | } |
| 913 | } |
| 914 | let decodedFn = decoded.program.fns[0]; |
| 915 | try testing::expect(decodedFn.params[0].type == il::ValType::Ptr(REF_L1)); |
| 916 | try testing::expect(decodedFn.returnType == func.returnType); |
| 917 | try testing::expect( |
| 918 | decodedFn.blocks[0].instrs[0] == il::Instr::Reserve { |
| 919 | dst: il::Reg { |
| 920 | n: 2 |
| 921 | }, |
| 922 | layout: 2, |
| 923 | } |
| 924 | ); |
| 925 | } |
| 926 | |
| 927 | /// A trait object slot of trait 0 that borrows a writable receiver. |
| 928 | constant OBJECT_SLOT: il::Slot = il::Slot { |
| 929 | offset: 0, |
| 930 | kind: il::SlotKind::Trait(il::ObjectType { |
| 931 | iface: 0, own: false, mutable: true |
| 932 | }), |
| 933 | regions: &[], |
| 934 | }; |
| 935 | |
| 936 | /// Check that a trait table and trait object slots survive encoding, and that their |
| 937 | /// indices and rights are checked. |
| 938 | @test unsafe fn traitPackage() throws (testing::TestError) { |
| 939 | let object = OBJECT_SLOT; |
| 940 | let layouts: *[il::Layout] = &[ |
| 941 | il::Layout { size: 4, alignment: 4, slots: &[], regions: 0 }, |
| 942 | il::Layout { size: 16, alignment: 8, slots: &[OBJECT_SLOT], regions: 0 }, |
| 943 | ]; |
| 944 | let signatures: *[il::Signature] = &[ |
| 945 | il::Signature { |
| 946 | params: &[], ret: il::ValType::Data(il::Type::W64), borrows: &[] |
| 947 | }, |
| 948 | ]; |
| 949 | let traits: *[il::Trait] = &[ |
| 950 | il::Trait { |
| 951 | methods: &[ |
| 952 | il::Method { sig: 0, receiver: 0, own: false, mutable: true }, |
| 953 | il::Method { sig: 0, receiver: 0, own: true, mutable: false }, |
| 954 | ], |
| 955 | }, |
| 956 | ]; |
| 957 | let methods = traits[0].methods; |
| 958 | let package = binary::Package { |
| 959 | version: nil, |
| 960 | symbols: &["p"], |
| 961 | name: "p", |
| 962 | dependencies: &[], |
| 963 | exports: &[], |
| 964 | entry: nil, |
| 965 | program: il::Program { data: &[], fns: &[], layouts, signatures, traits }, |
| 966 | links: binary::EMPTY_LINKS, |
| 967 | }; |
| 968 | try roundTrip(&package); |
| 969 | |
| 970 | let arena = heap::over(&mut MEMORY[..]); |
| 971 | let decoded = try reencoded(&package, arena); |
| 972 | try testing::expect(decoded.program.traits.len == 1); |
| 973 | try testing::expect(decoded.program.traits[0].methods.len == 2); |
| 974 | for method, i in methods { |
| 975 | try testing::expect(decoded.program.traits[0].methods[i] == method); |
| 976 | } |
| 977 | try testing::expect(sameSlot(decoded.program.layouts[1].slots[0], object)); |
| 978 | |
| 979 | // A trait slot needs a trait, and a method needs a signature. |
| 980 | try rejectedLayouts( |
| 981 | &[il::Layout { size: 16, alignment: 8, slots: &[OBJECT_SLOT], regions: 0 }], &[] |
| 982 | ); |
| 983 | let unsigned = binary::Package { |
| 984 | version: nil, |
| 985 | symbols: &["p"], |
| 986 | name: "p", |
| 987 | dependencies: &[], |
| 988 | exports: &[], |
| 989 | entry: nil, |
| 990 | program: il::Program { |
| 991 | data: &[], |
| 992 | fns: &[], |
| 993 | layouts, |
| 994 | signatures, |
| 995 | traits: &[ |
| 996 | il::Trait { |
| 997 | methods: &[il::Method { sig: 1, receiver: 0, own: false, mutable: false }] |
| 998 | }, |
| 999 | ], |
| 1000 | }, |
| 1001 | links: binary::EMPTY_LINKS, |
| 1002 | }; |
| 1003 | try rejected(&unsigned, LIMITS); |
| 1004 | } |
| 1005 | |
| 1006 | /// Encode a package with the given tables and check that decoding rejects it. |
| 1007 | unsafe fn rejectedLayouts(layouts: *[il::Layout], signatures: *[il::Signature]) throws ( |
| 1008 | testing::TestError |
| 1009 | ) { |
| 1010 | let package = binary::Package { |
| 1011 | version: nil, |
| 1012 | symbols: &["p"], |
| 1013 | name: "p", |
| 1014 | dependencies: &[], |
| 1015 | exports: &[], |
| 1016 | entry: nil, |
| 1017 | program: il::Program { data: &[], fns: &[], layouts, signatures, traits: &[] }, |
| 1018 | links: binary::EMPTY_LINKS, |
| 1019 | }; |
| 1020 | try rejected(&package, LIMITS); |
| 1021 | } |
| 1022 | |
| 1023 | /// Owned nullable pointer to layout 1, as stored in a slot. |
| 1024 | constant SLOT_L1: il::PtrType = il::PtrType { |
| 1025 | layout: 1, own: true, mutable: false, nullable: true |
| 1026 | }; |
| 1027 | |
| 1028 | /// Check the structural rules of layouts: alignment, slot order, extent, and nesting. |
| 1029 | @test unsafe fn invalidLayouts() throws (testing::TestError) { |
| 1030 | // Alignment is a power of two. |
| 1031 | try rejectedLayouts(&[il::Layout { size: 8, alignment: 0, slots: &[], regions: 0 }], &[]); |
| 1032 | try rejectedLayouts(&[il::Layout { size: 8, alignment: 3, slots: &[], regions: 0 }], &[]); |
| 1033 | // Size is a multiple of the alignment, so that array elements stay aligned. |
| 1034 | try rejectedLayouts(&[il::Layout { size: 12, alignment: 8, slots: &[], regions: 0 }], &[]); |
| 1035 | try rejectedLayouts(&[il::Layout { size: 1, alignment: 2, slots: &[], regions: 0 }], &[]); |
| 1036 | // Slots are aligned, sorted, disjoint, inside the size, and fit the alignment. |
| 1037 | try rejectedLayouts(&[ |
| 1038 | il::Layout { |
| 1039 | size: 16, |
| 1040 | alignment: 8, |
| 1041 | slots: &[ |
| 1042 | il::Slot { |
| 1043 | offset: 4, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1044 | }, |
| 1045 | ], |
| 1046 | regions: 0, |
| 1047 | }, |
| 1048 | ], &[]); |
| 1049 | try rejectedLayouts( |
| 1050 | &[ |
| 1051 | il::Layout { |
| 1052 | size: 16, |
| 1053 | alignment: 8, |
| 1054 | slots: &[ |
| 1055 | il::Slot { |
| 1056 | offset: 8, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1057 | }, |
| 1058 | il::Slot { |
| 1059 | offset: 0, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1060 | }, |
| 1061 | ], |
| 1062 | regions: 0, |
| 1063 | }, |
| 1064 | ], |
| 1065 | &[] |
| 1066 | ); |
| 1067 | try rejectedLayouts( |
| 1068 | &[ |
| 1069 | il::Layout { |
| 1070 | size: 16, |
| 1071 | alignment: 8, |
| 1072 | slots: &[ |
| 1073 | il::Slot { |
| 1074 | offset: 0, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1075 | }, |
| 1076 | il::Slot { |
| 1077 | offset: 0, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1078 | }, |
| 1079 | ], |
| 1080 | regions: 0, |
| 1081 | }, |
| 1082 | ], |
| 1083 | &[] |
| 1084 | ); |
| 1085 | try rejectedLayouts( |
| 1086 | &[ |
| 1087 | il::Layout { |
| 1088 | size: 24, |
| 1089 | alignment: 8, |
| 1090 | slots: &[ |
| 1091 | il::Slot { offset: 0, kind: il::SlotKind::Slice(SLOT_L0), regions: &[] }, |
| 1092 | il::Slot { offset: 8, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] }, |
| 1093 | ], |
| 1094 | regions: 0, |
| 1095 | }, |
| 1096 | ], |
| 1097 | &[] |
| 1098 | ); |
| 1099 | try rejectedLayouts(&[ |
| 1100 | il::Layout { |
| 1101 | size: 8, |
| 1102 | alignment: 8, |
| 1103 | slots: &[ |
| 1104 | il::Slot { |
| 1105 | offset: 8, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1106 | }, |
| 1107 | ], |
| 1108 | regions: 0, |
| 1109 | }, |
| 1110 | ], &[]); |
| 1111 | try rejectedLayouts(&[ |
| 1112 | il::Layout { |
| 1113 | size: 12, |
| 1114 | alignment: 8, |
| 1115 | slots: &[ |
| 1116 | il::Slot { |
| 1117 | offset: 8, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1118 | }, |
| 1119 | ], |
| 1120 | regions: 0, |
| 1121 | }, |
| 1122 | ], &[]); |
| 1123 | try rejectedLayouts(&[ |
| 1124 | il::Layout { |
| 1125 | size: 8, |
| 1126 | alignment: 4, |
| 1127 | slots: &[ |
| 1128 | il::Slot { |
| 1129 | offset: 0, kind: il::SlotKind::Ptr(SLOT_L0), regions: &[] |
| 1130 | }, |
| 1131 | ], |
| 1132 | regions: 0, |
| 1133 | }, |
| 1134 | ], &[]); |
| 1135 | // Nested layouts have lower indices. |
| 1136 | try rejectedLayouts(&[ |
| 1137 | il::Layout { |
| 1138 | size: 8, |
| 1139 | alignment: 8, |
| 1140 | slots: &[ |
| 1141 | il::Slot { offset: 0, kind: il::SlotKind::Sub(0), regions: &[] } |
| 1142 | ], |
| 1143 | regions: 0, |
| 1144 | }, |
| 1145 | ], &[]); |
| 1146 | try rejectedLayouts( |
| 1147 | &[ |
| 1148 | il::Layout { |
| 1149 | size: 8, |
| 1150 | alignment: 8, |
| 1151 | slots: &[ |
| 1152 | il::Slot { offset: 0, kind: il::SlotKind::Sub(1), regions: &[] } |
| 1153 | ], |
| 1154 | regions: 0, |
| 1155 | }, |
| 1156 | PLAIN, |
| 1157 | ], |
| 1158 | &[] |
| 1159 | ); |
| 1160 | try rejectedLayouts( |
| 1161 | &[ |
| 1162 | PLAIN, |
| 1163 | il::Layout { |
| 1164 | size: 16, |
| 1165 | alignment: 8, |
| 1166 | slots: &[ |
| 1167 | il::Slot { |
| 1168 | offset: 0, kind: il::SlotKind::Array { layout: 1, count: 2 }, regions: &[] |
| 1169 | }, |
| 1170 | ], |
| 1171 | regions: 0, |
| 1172 | }, |
| 1173 | ], |
| 1174 | &[] |
| 1175 | ); |
| 1176 | // Nested objects fit in the layout, also when their size overflows 32 bits. |
| 1177 | try rejectedLayouts( |
| 1178 | &[ |
| 1179 | PLAIN, |
| 1180 | il::Layout { |
| 1181 | size: 8, |
| 1182 | alignment: 8, |
| 1183 | slots: &[ |
| 1184 | il::Slot { |
| 1185 | offset: 0, kind: il::SlotKind::Array { layout: 0, count: 2 }, regions: &[] |
| 1186 | }, |
| 1187 | ], |
| 1188 | regions: 0, |
| 1189 | }, |
| 1190 | ], |
| 1191 | &[] |
| 1192 | ); |
| 1193 | try rejectedLayouts( |
| 1194 | &[ |
| 1195 | il::Layout { size: 0x10000, alignment: 8, slots: &[], regions: 0 }, |
| 1196 | il::Layout { |
| 1197 | size: 0xffffffff, |
| 1198 | alignment: 8, |
| 1199 | slots: &[ |
| 1200 | il::Slot { |
| 1201 | offset: 0, |
| 1202 | kind: il::SlotKind::Array { layout: 0, count: 0x10000 }, |
| 1203 | regions: &[], |
| 1204 | }, |
| 1205 | ], |
| 1206 | regions: 0, |
| 1207 | }, |
| 1208 | ], |
| 1209 | &[] |
| 1210 | ); |
| 1211 | try rejectedLayouts( |
| 1212 | &[ |
| 1213 | PLAIN, |
| 1214 | il::Layout { |
| 1215 | size: 8, |
| 1216 | alignment: 8, |
| 1217 | slots: &[ |
| 1218 | il::Slot { offset: 4, kind: il::SlotKind::Sub(0), regions: &[] } |
| 1219 | ], |
| 1220 | regions: 0, |
| 1221 | }, |
| 1222 | ], |
| 1223 | &[] |
| 1224 | ); |
| 1225 | // Indices of pointees and signatures name table entries. |
| 1226 | try rejectedLayouts(&[ |
| 1227 | il::Layout { |
| 1228 | size: 8, |
| 1229 | alignment: 8, |
| 1230 | slots: &[ |
| 1231 | il::Slot { |
| 1232 | offset: 0, kind: il::SlotKind::Ptr(SLOT_L1), regions: &[] |
| 1233 | }, |
| 1234 | ], |
| 1235 | regions: 0, |
| 1236 | }, |
| 1237 | ], &[]); |
| 1238 | try rejectedLayouts(&[ |
| 1239 | il::Layout { |
| 1240 | size: 8, |
| 1241 | alignment: 8, |
| 1242 | slots: &[ |
| 1243 | il::Slot { offset: 0, kind: il::SlotKind::Fn(0), regions: &[] } |
| 1244 | ], |
| 1245 | regions: 0, |
| 1246 | }, |
| 1247 | ], &[]); |
| 1248 | // A slot is nullable in memory. |
| 1249 | try rejectedLayouts(&[ |
| 1250 | il::Layout { |
| 1251 | size: 8, |
| 1252 | alignment: 8, |
| 1253 | slots: &[ |
| 1254 | il::Slot { |
| 1255 | offset: 0, |
| 1256 | kind: il::SlotKind::Ptr(il::PtrType { |
| 1257 | layout: 0, own: true, mutable: false, nullable: false |
| 1258 | }), |
| 1259 | regions: &[], |
| 1260 | }, |
| 1261 | ], |
| 1262 | regions: 0, |
| 1263 | }, |
| 1264 | ], &[]); |
| 1265 | // Signatures refer to existing layouts and signatures. |
| 1266 | try rejectedLayouts(&[PLAIN], &[ |
| 1267 | il::Signature { |
| 1268 | params: &[ |
| 1269 | il::ValType::Ptr(il::PtrType { |
| 1270 | layout: 1, own: true, mutable: false, nullable: false |
| 1271 | }), |
| 1272 | ], |
| 1273 | ret: il::ValType::Void, |
| 1274 | borrows: &[], |
| 1275 | }, |
| 1276 | ]); |
| 1277 | try rejectedLayouts(&[PLAIN], &[ |
| 1278 | il::Signature { params: &[], ret: il::ValType::Fn(1), borrows: &[] } |
| 1279 | ]); |
| 1280 | // The wire forms of a valid table are accepted. |
| 1281 | let ok = binary::Package { |
| 1282 | version: nil, |
| 1283 | symbols: &["p"], |
| 1284 | name: "p", |
| 1285 | dependencies: &[], |
| 1286 | exports: &[], |
| 1287 | entry: nil, |
| 1288 | program: il::Program { |
| 1289 | data: &[], |
| 1290 | fns: &[], |
| 1291 | layouts: &[PLAIN, PTR_LAYOUT], |
| 1292 | signatures: &[ |
| 1293 | il::Signature { |
| 1294 | params: &[], ret: il::ValType::Fn(0), borrows: &[] |
| 1295 | }, |
| 1296 | ], |
| 1297 | traits: &[], |
| 1298 | }, |
| 1299 | links: binary::EMPTY_LINKS, |
| 1300 | }; |
| 1301 | try roundTrip(&ok); |
| 1302 | } |
| 1303 | |
| 1304 | /// Arguments of a branch that passes none. |
| 1305 | constant NO_ARGS: il::ArgRange = il::ArgRange { start: 0, len: 0 }; |
| 1306 | |
| 1307 | /// Build a block that ends in `last`, with the instruction stored in `arena`. |
| 1308 | unsafe fn endBlock(last: il::Instr, arena: &heap::Heap) -> il::Block { |
| 1309 | let mut instrs: *mut [il::Instr] = &mut []; |
| 1310 | try! instrs.append(last, arena); |
| 1311 | return il::Block { |
| 1312 | label: il::BlockLabel { base: "b", suffix: nil }, |
| 1313 | params: &[], |
| 1314 | instrs: &instrs[..], |
| 1315 | locs: &[], |
| 1316 | preds: &[], |
| 1317 | loopDepth: 0, |
| 1318 | }; |
| 1319 | } |
| 1320 | |
| 1321 | /// Instruction that jumps to `target` without arguments. |
| 1322 | fn jump(target: u32) -> il::Instr { |
| 1323 | return il::Instr::Jmp { target, args: NO_ARGS }; |
| 1324 | } |
| 1325 | |
| 1326 | /// Instruction that branches to `thenTarget` or `elseTarget` without arguments. |
| 1327 | fn branch(thenTarget: u32, elseTarget: u32) -> il::Instr { |
| 1328 | return il::Instr::Br { |
| 1329 | op: il::CmpOp::Eq, |
| 1330 | typ: il::Type::W32, |
| 1331 | a: il::Val::Imm(0), |
| 1332 | b: il::Val::Imm(0), |
| 1333 | thenTarget, |
| 1334 | thenArgs: NO_ARGS, |
| 1335 | elseTarget, |
| 1336 | elseArgs: NO_ARGS, |
| 1337 | }; |
| 1338 | } |
| 1339 | |
| 1340 | /// Check that the decoder derives predecessors and loop depths from the terminators. |
| 1341 | @test unsafe fn controlFlow() throws (testing::TestError) { |
| 1342 | // Blocks 1 and 2 head an outer and an inner loop. Block 5 switches to the same |
| 1343 | // target twice, and blocks 6 and 7 return. |
| 1344 | static FIXTURE: [u8; 4096] = [0; 4096]; |
| 1345 | let fixture = heap::over(&mut FIXTURE[..]); |
| 1346 | let switchInstr = il::Instr::Switch { |
| 1347 | val: il::Val::Imm(0), |
| 1348 | defaultTarget: 6, |
| 1349 | defaultArgs: NO_ARGS, |
| 1350 | cases: il::CaseRange { start: 0, len: 2 }, |
| 1351 | }; |
| 1352 | let terminators = [ |
| 1353 | jump(1), branch(2, 5), branch(3, 4), jump(2), jump(1), switchInstr, |
| 1354 | il::Instr::Ret { val: nil }, il::Instr::Ret { val: nil }, |
| 1355 | ]; |
| 1356 | let mut list: *mut [il::Block] = &mut []; |
| 1357 | for terminator in &terminators[..] { |
| 1358 | try! list.append(endBlock(terminator, fixture), fixture); |
| 1359 | } |
| 1360 | let blocks: *[il::Block] = &list[..]; |
| 1361 | let cases: *[il::SwitchCase] = &[ |
| 1362 | il::SwitchCase { value: 0, target: 6, args: NO_ARGS }, |
| 1363 | il::SwitchCase { |
| 1364 | value: 1, target: 7, args: NO_ARGS |
| 1365 | }, |
| 1366 | ]; |
| 1367 | let func = retainFn( |
| 1368 | il::Fn { |
| 1369 | name: "p::f", |
| 1370 | params: &[], |
| 1371 | returnType: il::ValType::Void, |
| 1372 | isExtern: false, |
| 1373 | isLeaf: true, |
| 1374 | blocks, |
| 1375 | vals: &[], |
| 1376 | cases, |
| 1377 | borrows: &[], |
| 1378 | }, |
| 1379 | fixture |
| 1380 | ); |
| 1381 | let mut fns: *mut [*il::Fn] = &mut []; |
| 1382 | try! fns.append(func, fixture); |
| 1383 | let package = binary::Package { |
| 1384 | version: nil, |
| 1385 | symbols: &["p", "p::f"], |
| 1386 | name: "p", |
| 1387 | dependencies: &[], |
| 1388 | exports: &[], |
| 1389 | entry: nil, |
| 1390 | program: il::Program { |
| 1391 | data: &[], fns: &fns[..], layouts: &[], signatures: &[], traits: &[] |
| 1392 | }, |
| 1393 | links: binary::EMPTY_LINKS, |
| 1394 | }; |
| 1395 | static buffer: [u8; 512] = [0; 512]; |
| 1396 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
| 1397 | throw testing::TestError::Failed; |
| 1398 | }; |
| 1399 | static DECODE: [u8; 4096] = [0; 4096]; |
| 1400 | let arena = heap::over(&mut DECODE[..]); |
| 1401 | let decoded = try program::decode(&buffer[.. length], arena, binary::Limits { |
| 1402 | registers: 16, blocks: 8 |
| 1403 | }) catch { |
| 1404 | throw testing::TestError::Failed; |
| 1405 | }; |
| 1406 | let expectedPreds: [*[u32]; 8] = [&[], &[0, 4], &[1, 3], &[2], &[2], &[1], &[5], &[5]]; |
| 1407 | let expectedDepth: [u32; 8] = [0, 1, 2, 2, 1, 0, 0, 0]; |
| 1408 | let decodedBlocks = decoded.program.fns[0].blocks; |
| 1409 | try testing::expect(decodedBlocks.len == 8); |
| 1410 | for block, i in decodedBlocks { |
| 1411 | try testing::expect(block.preds.len == expectedPreds[i].len); |
| 1412 | for pred, j in block.preds { |
| 1413 | try testing::expect(pred == expectedPreds[i][j]); |
| 1414 | } |
| 1415 | try testing::expect(block.loopDepth == expectedDepth[i]); |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | /// Storage for the layouts of the union tests. |
| 1420 | static UNION_STORAGE: [u8; 4096] = [0; 4096]; |
| 1421 | |
| 1422 | /// A layout of `size` bytes with `alignment` that has a union slot at offset 0, with its payload |
| 1423 | /// at `payload`, of `width` bytes, and the given variants. Its slices live in `a`. |
| 1424 | unsafe fn unionLayout( |
| 1425 | a: &heap::Heap, |
| 1426 | size: u32, |
| 1427 | alignment: u32, |
| 1428 | payload: u32, |
| 1429 | width: u32, |
| 1430 | variants: &[il::UnionVariant] |
| 1431 | ) -> il::Layout { |
| 1432 | let mut own: *mut [il::UnionVariant] = &mut []; |
| 1433 | try! own.extend(variants, a); |
| 1434 | let mut slots: *mut [il::Slot] = &mut []; |
| 1435 | try! slots.append( |
| 1436 | il::Slot { |
| 1437 | offset: 0, |
| 1438 | kind: il::SlotKind::Union { offset: payload, size: width, variants: &own[..] }, |
| 1439 | regions: &[], |
| 1440 | }, |
| 1441 | a |
| 1442 | ); |
| 1443 | return il::Layout { size, alignment, slots: &slots[..], regions: 0 }; |
| 1444 | } |
| 1445 | |
| 1446 | /// The layouts of `items` as a table. Its slice lives in `a`. |
| 1447 | unsafe fn table(a: &heap::Heap, items: &[il::Layout]) -> *[il::Layout] { |
| 1448 | let mut own: *mut [il::Layout] = &mut []; |
| 1449 | try! own.extend(items, a); |
| 1450 | return &own[..]; |
| 1451 | } |
| 1452 | |
| 1453 | /// Check that union slots survive encoding, and that the decoder rejects variant layouts |
| 1454 | /// that do not precede the layout, and slots that do not fit or are not aligned. |
| 1455 | @test unsafe fn unionPackage() throws (testing::TestError) { |
| 1456 | let a = heap::over(&mut UNION_STORAGE[..]); |
| 1457 | let layouts = table( |
| 1458 | a, |
| 1459 | &[ |
| 1460 | PLAIN, |
| 1461 | PTR_LAYOUT, |
| 1462 | unionLayout( |
| 1463 | a, |
| 1464 | 16, |
| 1465 | 8, |
| 1466 | 8, |
| 1467 | 16, |
| 1468 | &[ |
| 1469 | il::UnionVariant { tag: 0, layout: 1, regions: &[] }, |
| 1470 | il::UnionVariant { tag: 1, layout: 0, regions: &[] }, |
| 1471 | il::UnionVariant { tag: 9, layout: nil, regions: &[] }, |
| 1472 | ] |
| 1473 | ), |
| 1474 | ] |
| 1475 | ); |
| 1476 | let ok = binary::Package { |
| 1477 | version: nil, |
| 1478 | symbols: &["p"], |
| 1479 | name: "p", |
| 1480 | dependencies: &[], |
| 1481 | exports: &[], |
| 1482 | entry: nil, |
| 1483 | program: il::Program { |
| 1484 | data: &[], fns: &[], layouts, signatures: &[], traits: &[] |
| 1485 | }, |
| 1486 | links: binary::EMPTY_LINKS, |
| 1487 | }; |
| 1488 | try roundTrip(&ok); |
| 1489 | let arena = heap::over(&mut MEMORY[..]); |
| 1490 | let decoded = try reencoded(&ok, arena); |
| 1491 | let case il::SlotKind::Union { offset, size, variants } = |
| 1492 | decoded.program.layouts[2].slots[0].kind else throw testing::TestError::Failed; |
| 1493 | try testing::expect(offset == 8 and size == 16 and variants.len == 3); |
| 1494 | try testing::expect(variants[0] == il::UnionVariant { tag: 0, layout: 1, regions: &[] }); |
| 1495 | try testing::expect(variants[1] == il::UnionVariant { tag: 1, layout: 0, regions: &[] }); |
| 1496 | try testing::expect(variants[2] == il::UnionVariant { tag: 9, layout: nil, regions: &[] }); |
| 1497 | // A variant layout precedes the layout of the slot, and exists. |
| 1498 | let own = &[il::UnionVariant { tag: 0, layout: 1, regions: &[] }]; |
| 1499 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 16, 8, 8, 16, own)]), &[]); |
| 1500 | let missing = &[il::UnionVariant { tag: 0, layout: 2, regions: &[] }]; |
| 1501 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 16, 8, 8, 16, missing)]), &[]); |
| 1502 | // The slot lies within the layout, and is as aligned as its payloads. |
| 1503 | let first = &[il::UnionVariant { tag: 0, layout: 0, regions: &[] }]; |
| 1504 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 8, 8, 4, 16, first)]), &[]); |
| 1505 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 16, 4, 8, 16, first)]), &[]); |
| 1506 | } |
| 1507 | |
| 1508 | /// Exact dependency pins and typed imports survive the package envelope. |
| 1509 | @test unsafe fn linkedEnvelope() throws (testing::TestError) { |
| 1510 | let package = binary::Package { |
| 1511 | version: nil, |
| 1512 | symbols: &["p", "dep", "remote"], |
| 1513 | name: "p", |
| 1514 | dependencies: &["dep"], |
| 1515 | exports: &[], |
| 1516 | entry: nil, |
| 1517 | program: il::Program { |
| 1518 | data: &[], |
| 1519 | fns: &[], |
| 1520 | layouts: &[], |
| 1521 | signatures: &[ |
| 1522 | il::Signature { params: &[], ret: il::ValType::Void, borrows: &[] } |
| 1523 | ], |
| 1524 | traits: &[], |
| 1525 | }, |
| 1526 | links: binary::Links { |
| 1527 | metadata: binary::Id { bytes: [9; linkage::ID_BYTES] }, |
| 1528 | pins: &[ |
| 1529 | binary::Id { bytes: [1; 32] } |
| 1530 | ], |
| 1531 | imports: &[ |
| 1532 | binary::Import { |
| 1533 | dependency: 0, |
| 1534 | exported: "remote", |
| 1535 | kind: binary::ImportKind::Function(0), |
| 1536 | }, |
| 1537 | ], |
| 1538 | image: nil, |
| 1539 | verified: false, |
| 1540 | }, |
| 1541 | }; |
| 1542 | try roundTrip(&package); |
| 1543 | } |
| 1544 | |
| 1545 | /// Provider declarations retain exact signatures without retaining function bodies. |
| 1546 | @test unsafe fn providerDeclarations() throws (testing::TestError) { |
| 1547 | /// Function with a complete body in the encoded provider. |
| 1548 | unsafe static func: il::Fn = il::Fn { |
| 1549 | name: "p::f", |
| 1550 | params: &[], |
| 1551 | returnType: il::ValType::Void, |
| 1552 | isExtern: false, |
| 1553 | isLeaf: true, |
| 1554 | blocks: &[ |
| 1555 | il::Block { |
| 1556 | label: il::BlockLabel { base: "entry", suffix: nil }, |
| 1557 | params: &[], |
| 1558 | instrs: &[il::Instr::Ret { val: nil }], |
| 1559 | locs: &[], |
| 1560 | preds: &[], |
| 1561 | loopDepth: 0, |
| 1562 | }, |
| 1563 | ], |
| 1564 | vals: &[], |
| 1565 | cases: &[], |
| 1566 | borrows: &[], |
| 1567 | }; |
| 1568 | let storage = heap::over(&mut MEMORY[..]); |
| 1569 | let mut functions: *mut [*il::Fn] = &mut []; |
| 1570 | try! functions.append(&func, storage); |
| 1571 | let package = binary::Package { |
| 1572 | version: nil, |
| 1573 | symbols: &["p", "p::f"], |
| 1574 | name: "p", |
| 1575 | dependencies: &[], |
| 1576 | exports: &[ |
| 1577 | binary::Export { |
| 1578 | name: "p::f", kind: binary::ExportKind::Function(0), startable: false |
| 1579 | }, |
| 1580 | ], |
| 1581 | entry: nil, |
| 1582 | program: il::Program { |
| 1583 | data: &[], fns: &functions[..], layouts: &[], signatures: &[], traits: &[] |
| 1584 | }, |
| 1585 | links: binary::EMPTY_LINKS, |
| 1586 | }; |
| 1587 | /// Complete provider payload used for header and extent checks. |
| 1588 | static bytes: [u8; 256] = [0; 256]; |
| 1589 | let length = try! encodeInto(&mut bytes[..] as *mut [u8], &package); |
| 1590 | let arena = heap::over(&mut MEMORY[..]); |
| 1591 | let headers = try! program::declarations(&bytes[.. length], arena); |
| 1592 | assert headers.program.fns.len == 1; |
| 1593 | assert headers.program.fns[0].returnType == func.returnType; |
| 1594 | assert headers.program.fns[0].blocks.len == 0; |
| 1595 | assert headers.exports.len == 1; |
| 1596 | let mut head = package; |
| 1597 | set head.program.fns = &[]; |
| 1598 | /// Package head without function records, to locate the first extent. |
| 1599 | static headBytes: [u8; 256] = [0; 256]; |
| 1600 | let start = try! encodeInto(&mut headBytes[..] as *mut [u8], &head); |
| 1601 | let extent = mem::load32Le(&bytes[.. length], start); |
| 1602 | for size in &[0 as u32, 1, extent - 1, extent + 1, 0xffffffff] { |
| 1603 | mem::store32Le(&mut bytes[.. length], start, size); |
| 1604 | let arena = heap::over(&mut MEMORY[..]); |
| 1605 | let mut failed = false; |
| 1606 | try program::parse(&bytes[.. length], arena, LIMITS) catch error { |
| 1607 | assert error == binary::Error::Invalid or error == binary::Error::Truncated; |
| 1608 | set failed = true; |
| 1609 | }; |
| 1610 | assert failed and heap::used(arena) == 0; |
| 1611 | set failed = false; |
| 1612 | try program::declarations(&bytes[.. length], arena) catch error { |
| 1613 | assert error == binary::Error::Invalid or error == binary::Error::Truncated; |
| 1614 | set failed = true; |
| 1615 | }; |
| 1616 | assert failed and heap::used(arena) == 0; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /// Image identity uses an ASCII version digit and four zero padding bytes. |
| 1621 | @test fn imagePrefix() throws (testing::TestError) { |
| 1622 | let expected = blake3::digest(&[82, 73, 76, 48, 0, 0, 0, 0, 97, 98, 99]); |
| 1623 | let image = linkage::imageId("abc"); |
| 1624 | assert mem::eq(&image.bytes[..], &expected.bytes[..]); |
| 1625 | let payload = "abc"; |
| 1626 | for split in 0 .. payload.len + 1 { |
| 1627 | assert linkage::imageParts(&payload[.. split], &payload[split ..]) == image; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | /// Name allocation failures keep the index and first-use table in agreement. |
| 1632 | @test unsafe fn collectionRollback() throws (testing::TestError) { |
| 1633 | static storage: [u8; 512] = [0; 512]; |
| 1634 | let mut failures: u32 = 0; |
| 1635 | for capacity in 192 as u32 .. 384 { |
| 1636 | let arena = heap::withCapacity(&mut storage[..], capacity); |
| 1637 | let mut names = try collect::new(8, arena) catch { |
| 1638 | continue; |
| 1639 | }; |
| 1640 | try collect::start(&mut names, "p", arena) catch error { |
| 1641 | assert error == binary::Error::Storage; |
| 1642 | assert names.index.count == names.symbols.len; |
| 1643 | set failures += 1; |
| 1644 | }; |
| 1645 | } |
| 1646 | assert failures > 0; |
| 1647 | let arena = heap::over(&mut storage[..]); |
| 1648 | let mut names = try! collect::new(2, arena); |
| 1649 | try! collect::start(&mut names, "p", arena); |
| 1650 | let mut full = false; |
| 1651 | try collect::reference(&mut names, "p::f", arena) catch error { |
| 1652 | assert error == binary::Error::Storage; |
| 1653 | set full = true; |
| 1654 | }; |
| 1655 | assert full and names.symbols.len == 1 and names.index.count == 1; |
| 1656 | assert dict::get(&names.index, "p") == 0; |
| 1657 | assert dict::get(&names.index, "p::f") == nil; |
| 1658 | } |