.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
rv64/
asm/
image/
shared/
tests/
catalog.rad
11.6 KiB
tests.rad
22.7 KiB
tests/
asm.rad
32.4 KiB
atomics.rad
5.1 KiB
decode.rad
27.1 KiB
emit.rad
34.6 KiB
encode.rad
30.9 KiB
image.rad
6.1 KiB
isel.rad
138.3 KiB
printer.rad
19.2 KiB
shared.rad
21.1 KiB
tests.rad
73.3 KiB
vector.rad
59.4 KiB
rv64.rad
22.4 KiB
bits/
char/
collections/
fs/
hash/
lang/
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/arch/rv64/shared/tests.rad
raw
| 1 | //! Private template relocation and bounded linking checks. |
| 2 | |
| 3 | @test mod run; |
| 4 | |
| 5 | use std::arch::rv64; |
| 6 | use std::arch::rv64::asm; |
| 7 | use std::arch::rv64::shared::catalog; |
| 8 | use std::lang::gen::data; |
| 9 | use std::lang::il; |
| 10 | use std::lang::il::binary; |
| 11 | use std::lang::il::binary::linkage; |
| 12 | use std::mem; |
| 13 | use std::mem::heap; |
| 14 | use std::testing; |
| 15 | |
| 16 | /// Code generation workspace reused between checks. |
| 17 | export static ARENA: [u8; 16777216] = [0; 16777216]; |
| 18 | /// Function allocation workspace. |
| 19 | export static SCRATCH: [u8; 16777216] = [0; 16777216]; |
| 20 | /// Persistent catalog symbols. |
| 21 | unsafe static SYMBOLS: [super::Symbol; 8] = undefined; |
| 22 | /// Persistent exported symbols. |
| 23 | unsafe static EXPORTS: [super::Symbol; 8] = undefined; |
| 24 | /// Data layout workspace. |
| 25 | unsafe static DATA: [data::DataSym; 8] = undefined; |
| 26 | /// Persistent initialized bytes. |
| 27 | static TEMPLATE: [u8; 64] = [0; 64]; |
| 28 | /// Persistent private data relocations. |
| 29 | unsafe static RELOCS: [super::Relocation; 8] = undefined; |
| 30 | /// Aligned storage for a native boot catalog and its retained payloads. |
| 31 | static CATALOG: [u64; 512] = [0; 512]; |
| 32 | |
| 33 | /// Exported assembly boundaries occupy the same code extent as their package. |
| 34 | @test unsafe fn assemblyPrefix() throws (testing::TestError) { |
| 35 | let input = binary::Package { |
| 36 | version: nil, |
| 37 | symbols: &[], |
| 38 | name: "p", |
| 39 | dependencies: &[], |
| 40 | exports: &[ |
| 41 | binary::Export { |
| 42 | name: "p::entry", |
| 43 | kind: binary::ExportKind::Function(0), |
| 44 | startable: false, |
| 45 | }, |
| 46 | ], |
| 47 | entry: "p::entry", |
| 48 | program: il::Program { |
| 49 | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
| 50 | }, |
| 51 | links: binary::EMPTY_LINKS, |
| 52 | }; |
| 53 | let assembly = asm::Program { |
| 54 | text: &[0x02a00513 as u32, 0x00008067], |
| 55 | data: &[], |
| 56 | symbols: &[ |
| 57 | asm::Symbol { |
| 58 | name: "p::entry", section: asm::Section::Text, offset: 0, isExported: true |
| 59 | }, |
| 60 | ], |
| 61 | externalFixups: &[], |
| 62 | }; |
| 63 | let arena = heap::over(&mut ARENA[..]); |
| 64 | let scratch = heap::over(&mut SCRATCH[..]); |
| 65 | let package = try super::compileAssembly( |
| 66 | &input, |
| 67 | &assembly, |
| 68 | 0, |
| 69 | 0x180000000, |
| 70 | &[], |
| 71 | super::Storage { |
| 72 | data: &mut DATA[..], |
| 73 | symbols: &mut SYMBOLS[..], |
| 74 | exports: &mut EXPORTS[..], |
| 75 | template: &mut TEMPLATE[..], |
| 76 | relocations: &mut RELOCS[..], |
| 77 | stack: rv64::StackCheck::Off, |
| 78 | }, |
| 79 | arena, |
| 80 | scratch |
| 81 | ) catch { |
| 82 | throw testing::TestError::Failed; |
| 83 | }; |
| 84 | let entry = package.entry else { |
| 85 | throw testing::TestError::Failed; |
| 86 | }; |
| 87 | try testing::expect(entry == 0x180000000 and package.code.len == 2); |
| 88 | try testing::expect( |
| 89 | package.code[0] == 0x02a00513 |
| 90 | and package.exports[0].target == super::Target::Function(entry) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /// Packed catalog pointers resolve to retained metadata at the supplied native base. |
| 95 | @test unsafe fn nativeCatalog() throws (testing::TestError) { |
| 96 | let mut package = try compile(imports(), 64) catch { |
| 97 | throw testing::TestError::Failed; |
| 98 | }; |
| 99 | set package.contract = super::Contract { |
| 100 | pins: &[ |
| 101 | binary::Id { bytes: [1; 32] } |
| 102 | ], |
| 103 | image: binary::Id { bytes: [3; 32] }, |
| 104 | verification: super::Verification::Verified, |
| 105 | metadata: binary::Id { bytes: [7; linkage::ID_BYTES] }, |
| 106 | }; |
| 107 | let output = @sliceOf(&mut CATALOG[0] as *mut u8, @sizeOf([u64; 512])); |
| 108 | let base = output.ptr as u64; |
| 109 | let used = try catalog::pack( |
| 110 | &[catalog::Entry { source: "binary package", package }], base, output |
| 111 | ) catch { |
| 112 | throw testing::TestError::Failed; |
| 113 | }; |
| 114 | let storage = &CATALOG[0] as *opaque; |
| 115 | let entry = storage as *catalog::Entry; |
| 116 | try testing::expect(used > @sizeOf(catalog::Entry) and used <= output.len); |
| 117 | try testing::expect( |
| 118 | mem::eq(entry.source, "binary package") and mem::eq( |
| 119 | entry.package.name, "p" |
| 120 | ) |
| 121 | ); |
| 122 | try testing::expect(mem::eq(entry.package.dependencies[0], "dep")); |
| 123 | try testing::expect( |
| 124 | entry.package.code.ptr as u64 == package.codeAddress |
| 125 | and entry.package.code.len == package.code.len |
| 126 | ); |
| 127 | try testing::expect( |
| 128 | entry.source.ptr as u64 >= base |
| 129 | and entry.source.ptr as u64 < base + used as u64 |
| 130 | ); |
| 131 | try testing::expect( |
| 132 | entry.package.slot == package.slot |
| 133 | and entry.package.codeAddress == package.codeAddress |
| 134 | ); |
| 135 | try testing::expect( |
| 136 | entry.package.entry == package.entry |
| 137 | and entry.package.memory == package.memory |
| 138 | ); |
| 139 | try testing::expect(entry.package.alignment == package.alignment); |
| 140 | try testing::expect( |
| 141 | entry.package.exports.len == package.exports.len |
| 142 | and package.exports.len > 0 |
| 143 | ); |
| 144 | for symbol, i in package.exports { |
| 145 | try testing::expect(entry.package.exports[i].target == symbol.target); |
| 146 | try testing::expect(mem::eq(entry.package.exports[i].name, symbol.name)); |
| 147 | } |
| 148 | try testing::expect( |
| 149 | entry.package.relocations.len == package.relocations.len |
| 150 | and package.relocations.len > 0 |
| 151 | ); |
| 152 | for fixup, i in package.relocations { |
| 153 | try testing::expect(entry.package.relocations[i] == fixup); |
| 154 | } |
| 155 | assert entry.package.contract.pins[0] == package.contract.pins[0]; |
| 156 | assert entry.package.contract.metadata == package.contract.metadata; |
| 157 | assert entry.package.contract.image == package.contract.image; |
| 158 | assert entry.package.contract.verification == super::Verification::Verified; |
| 159 | try testing::expect(mem::eq(entry.package.template, package.template)); |
| 160 | set TEMPLATE[0] = 91; |
| 161 | set EXPORTS[0].name = "changed"; |
| 162 | try testing::expect( |
| 163 | entry.package.template[0] == 0 and mem::eq( |
| 164 | entry.package.exports[0].name, "p::refs" |
| 165 | ) |
| 166 | ); |
| 167 | let mut state: [u8; 48] = [255; 48]; |
| 168 | try super::instantiate(&entry.package, &[0, 0x180002000, 0x180008000], &mut state[..]) catch { |
| 169 | throw testing::TestError::Failed; |
| 170 | }; |
| 171 | try testing::expect( |
| 172 | pointer(&state[..], 0) == 0x180004000 and pointer( |
| 173 | &state[..], 16 |
| 174 | ) == 0x180001234 |
| 175 | ); |
| 176 | let relocated = try catalog::pack( |
| 177 | &[catalog::Entry { source: "binary package", package }], 0x180000000, output |
| 178 | ) catch { |
| 179 | throw testing::TestError::Failed; |
| 180 | }; |
| 181 | try testing::expect( |
| 182 | entry.source.ptr as u64 >= 0x180000000 |
| 183 | and entry.source.ptr as u64 < 0x180000000 + relocated as u64 |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /// Byte offset of `value` in the native layout of `container`. |
| 188 | unsafe fn offsetOf(container: &opaque, value: &opaque) -> u32 { |
| 189 | return (value as u64 - container as u64) as u32; |
| 190 | } |
| 191 | |
| 192 | /// Little-endian integer of `width` bytes at `offset` of a native record. |
| 193 | unsafe fn word(container: &opaque, offset: u32, width: u32) -> u64 { |
| 194 | let bytes = @sliceOf(container as &u8 as *unsafe u8, offset + width); |
| 195 | let mut value: u64 = 0; |
| 196 | for i in 0 .. width { |
| 197 | set value |= bytes[offset + i] as u64 << (i as u64 * 8); |
| 198 | } |
| 199 | return value; |
| 200 | } |
| 201 | |
| 202 | /// The offsets that the catalog writes are the compiler's native record layout. |
| 203 | @test unsafe fn nativeCatalogLayout() throws (testing::TestError) { |
| 204 | let package = super::Package { |
| 205 | name: "n", |
| 206 | dependencies: &[], |
| 207 | slot: 7, |
| 208 | codeAddress: 0x1122334455, |
| 209 | code: &[], |
| 210 | exports: &[], |
| 211 | entry: 0x99, |
| 212 | template: &[], |
| 213 | memory: 9, |
| 214 | alignment: 16, |
| 215 | relocations: &[], |
| 216 | contract: super::UNVERIFIED, |
| 217 | }; |
| 218 | let entry = catalog::Entry { source: "abc", package }; |
| 219 | try testing::expect( |
| 220 | @sizeOf(*[u8]) == catalog::SLICE_SIZE |
| 221 | and @alignOf(*[u8]) == catalog::SLICE_ALIGN |
| 222 | ); |
| 223 | try testing::expect( |
| 224 | @sizeOf(catalog::Entry) == catalog::ENTRY_SIZE |
| 225 | and @alignOf(catalog::Entry) == catalog::ENTRY_ALIGN |
| 226 | ); |
| 227 | try testing::expect(offsetOf(&entry, &entry.source) == catalog::ENTRY_SOURCE); |
| 228 | try testing::expect(offsetOf(&entry, &entry.package.name) == catalog::ENTRY_NAME); |
| 229 | try testing::expect( |
| 230 | offsetOf( |
| 231 | &entry, &entry.package.dependencies |
| 232 | ) == catalog::ENTRY_DEPENDENCIES |
| 233 | ); |
| 234 | try testing::expect(offsetOf(&entry, &entry.package.slot) == catalog::ENTRY_SLOT); |
| 235 | try testing::expect( |
| 236 | offsetOf( |
| 237 | &entry, &entry.package.codeAddress |
| 238 | ) == catalog::ENTRY_CODE_ADDRESS |
| 239 | ); |
| 240 | try testing::expect(offsetOf(&entry, &entry.package.code) == catalog::ENTRY_CODE); |
| 241 | try testing::expect(offsetOf(&entry, &entry.package.exports) == catalog::ENTRY_EXPORTS); |
| 242 | try testing::expect(offsetOf(&entry, &entry.package.entry) == catalog::ENTRY_ENTRY_TAG); |
| 243 | try testing::expect(offsetOf(&entry, &entry.package.template) == catalog::ENTRY_TEMPLATE); |
| 244 | try testing::expect(offsetOf(&entry, &entry.package.memory) == catalog::ENTRY_MEMORY); |
| 245 | try testing::expect(offsetOf(&entry, &entry.package.alignment) == catalog::ENTRY_ALIGNMENT); |
| 246 | try testing::expect(offsetOf(&entry, &entry.package.relocations) == catalog::ENTRY_RELOCATIONS); |
| 247 | assert offsetOf(&entry, &entry.package.contract.pins) == catalog::ENTRY_PINS; |
| 248 | assert offsetOf(&entry, &entry.package.contract.image) == catalog::ENTRY_IMAGE_TAG; |
| 249 | assert offsetOf(&entry, &entry.package.contract.verification) == catalog::ENTRY_VERIFICATION; |
| 250 | assert offsetOf(&entry, &entry.package.contract.metadata) == catalog::ENTRY_METADATA; |
| 251 | assert @sizeOf(binary::Id) == catalog::PIN_SIZE; |
| 252 | // A slice is an address, a length, and a capacity. |
| 253 | let slice = catalog::ENTRY_SOURCE; |
| 254 | try testing::expect(word(&entry, slice, 8) == entry.source.ptr as u64); |
| 255 | try testing::expect( |
| 256 | word(&entry, slice + catalog::SLICE_LEN, 4) == 3 and word( |
| 257 | &entry, slice + catalog::SLICE_CAP, 4 |
| 258 | ) == 3 |
| 259 | ); |
| 260 | // An optional integer is a one-byte presence tag followed by the value at the next |
| 261 | // alignment. A union variant tag is one byte as well. |
| 262 | try testing::expect( |
| 263 | word(&entry, catalog::ENTRY_ENTRY_TAG, 1) == 1 and word( |
| 264 | &entry, catalog::ENTRY_ENTRY_VALUE, 8 |
| 265 | ) == 0x99 |
| 266 | ); |
| 267 | try testing::expect( |
| 268 | word(&entry, catalog::ENTRY_SLOT, 4) == 7 and word( |
| 269 | &entry, catalog::ENTRY_CODE_ADDRESS, 8 |
| 270 | ) == 0x1122334455 |
| 271 | ); |
| 272 | try testing::expect( |
| 273 | word(&entry, catalog::ENTRY_MEMORY, 4) == 9 and word( |
| 274 | &entry, catalog::ENTRY_ALIGNMENT, 4 |
| 275 | ) == 16 |
| 276 | ); |
| 277 | let absent = catalog::Entry { |
| 278 | source: "abc", |
| 279 | package: super::Package { |
| 280 | name: "n", |
| 281 | dependencies: &[], |
| 282 | slot: 7, |
| 283 | codeAddress: 0, |
| 284 | code: &[], |
| 285 | exports: &[], |
| 286 | entry: nil, |
| 287 | template: &[], |
| 288 | memory: 9, |
| 289 | alignment: 16, |
| 290 | relocations: &[], |
| 291 | contract: super::UNVERIFIED, |
| 292 | }, |
| 293 | }; |
| 294 | try testing::expect(word(&absent, catalog::ENTRY_ENTRY_TAG, 1) == 0); |
| 295 | |
| 296 | let function = super::Symbol { |
| 297 | name: "f", |
| 298 | target: super::Target::Function(0xaabbccdd11), |
| 299 | startable: true, |
| 300 | }; |
| 301 | let data = super::Symbol { |
| 302 | name: "d", |
| 303 | target: super::Target::Data(super::DataRef { |
| 304 | slot: 3, offset: 4 |
| 305 | }), |
| 306 | startable: true, |
| 307 | }; |
| 308 | try testing::expect( |
| 309 | @sizeOf(super::Symbol) == catalog::SYMBOL_SIZE |
| 310 | and @alignOf(super::Symbol) == catalog::SYMBOL_ALIGN |
| 311 | ); |
| 312 | try testing::expect(offsetOf(&function, &function.name) == catalog::SYMBOL_NAME); |
| 313 | try testing::expect(offsetOf(&function, &function.target) == catalog::SYMBOL_TAG); |
| 314 | assert offsetOf(&function, &function.startable) == catalog::SYMBOL_STARTABLE; |
| 315 | try testing::expect( |
| 316 | word(&function, catalog::SYMBOL_TAG, 1) == 0 and word( |
| 317 | &function, catalog::SYMBOL_VALUE, 8 |
| 318 | ) == 0xaabbccdd11 |
| 319 | ); |
| 320 | try testing::expect( |
| 321 | word(&data, catalog::SYMBOL_TAG, 1) == 1 and word( |
| 322 | &data, catalog::SYMBOL_VALUE, 4 |
| 323 | ) == 3 |
| 324 | ); |
| 325 | try testing::expect(word(&data, catalog::SYMBOL_VALUE + 4, 4) == 4); |
| 326 | |
| 327 | let fixup = super::Relocation { |
| 328 | offset: 1, |
| 329 | count: 2, |
| 330 | target: super::DataRef { |
| 331 | slot: 3, offset: 4 |
| 332 | }, |
| 333 | }; |
| 334 | try testing::expect(@sizeOf(super::Relocation) == catalog::RELOCATION_SIZE); |
| 335 | try testing::expect(@alignOf(super::Relocation) == catalog::RELOCATION_ALIGN); |
| 336 | try testing::expect(offsetOf(&fixup, &fixup.offset) == catalog::RELOCATION_OFFSET); |
| 337 | try testing::expect(offsetOf(&fixup, &fixup.count) == catalog::RELOCATION_COUNT); |
| 338 | try testing::expect(offsetOf(&fixup, &fixup.target.slot) == catalog::RELOCATION_SLOT); |
| 339 | try testing::expect( |
| 340 | offsetOf( |
| 341 | &fixup, &fixup.target.offset |
| 342 | ) == catalog::RELOCATION_TARGET_OFFSET |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | /// Catalog bounds and native alignment failures are recoverable. |
| 347 | @test unsafe fn nativeCatalogBounds() throws (testing::TestError) { |
| 348 | let package = try compile(imports(), 64) catch { |
| 349 | throw testing::TestError::Failed; |
| 350 | }; |
| 351 | let entries = [catalog::Entry { source: "binary", package }]; |
| 352 | let output = @sliceOf(&mut CATALOG[0] as *mut u8, @sizeOf([u64; 512])); |
| 353 | let mut failures: u32 = 0; |
| 354 | try catalog::pack(&entries[..], 0x80000000, &mut output[.. @sizeOf(catalog::Entry)]) catch err { |
| 355 | try testing::expect(err == super::Error::Capacity); |
| 356 | set failures += 1; |
| 357 | }; |
| 358 | try catalog::pack(&entries[..], 0x80000001, output) catch err { |
| 359 | try testing::expect(err == super::Error::Alignment); |
| 360 | set failures += 1; |
| 361 | }; |
| 362 | try catalog::pack(&entries[..], 0xfffffffffffffff8, output) catch err { |
| 363 | try testing::expect(err == super::Error::Range); |
| 364 | set failures += 1; |
| 365 | }; |
| 366 | try testing::expect(failures == 3); |
| 367 | let used = try catalog::pack(&entries[..], 0x80000000, output) catch { |
| 368 | throw testing::TestError::Failed; |
| 369 | }; |
| 370 | try testing::expect(used > @sizeOf(catalog::Entry)); |
| 371 | } |
| 372 | |
| 373 | /// Build a package with repeated dependency pointers and a high code pointer. |
| 374 | unsafe fn input() -> binary::Package { |
| 375 | return binary::Package { |
| 376 | version: nil, |
| 377 | symbols: &[], |
| 378 | name: "p", |
| 379 | dependencies: &["dep"], |
| 380 | exports: &[ |
| 381 | binary::Export { |
| 382 | name: "p::refs", |
| 383 | kind: binary::ExportKind::Data(0), |
| 384 | startable: false, |
| 385 | }, |
| 386 | ], |
| 387 | entry: nil, |
| 388 | program: il::Program { |
| 389 | data: &[ |
| 390 | il::Data { |
| 391 | name: "p::refs", |
| 392 | size: 32, |
| 393 | alignment: 8, |
| 394 | layout: 0, |
| 395 | readOnly: true, |
| 396 | heap: nil, |
| 397 | isZeroInit: false, |
| 398 | values: &[ |
| 399 | il::DataValue { item: il::DataItem::Sym("dep::value"), count: 2 }, |
| 400 | il::DataValue { item: il::DataItem::Fn("dep::call"), count: 1 }, |
| 401 | ], |
| 402 | }, |
| 403 | il::Data { |
| 404 | name: "p::zero", |
| 405 | size: 16, |
| 406 | alignment: 16, |
| 407 | layout: 1, |
| 408 | readOnly: false, |
| 409 | heap: nil, |
| 410 | isZeroInit: true, |
| 411 | values: &[], |
| 412 | }, |
| 413 | ], |
| 414 | fns: &[], |
| 415 | layouts: &[ |
| 416 | il::Layout { size: 32, alignment: 8, slots: &[], regions: 0 }, |
| 417 | il::Layout { size: 16, alignment: 16, slots: &[], regions: 0 }, |
| 418 | ], |
| 419 | signatures: &[], |
| 420 | traits: &[], |
| 421 | }, |
| 422 | links: binary::EMPTY_LINKS, |
| 423 | }; |
| 424 | } |
| 425 | |
| 426 | /// Compile into small persistent tables. |
| 427 | unsafe fn compile(imports: &[super::Symbol], capacity: u32) -> super::Package |
| 428 | throws (super::Error) |
| 429 | { |
| 430 | let arena = heap::over(&mut ARENA[..]); |
| 431 | let scratch = heap::over(&mut SCRATCH[..]); |
| 432 | let package = input(); |
| 433 | return try super::compile( |
| 434 | &package, |
| 435 | 2, |
| 436 | 0x180000000, |
| 437 | imports, |
| 438 | super::Storage { |
| 439 | data: &mut DATA[..], |
| 440 | symbols: &mut SYMBOLS[..], |
| 441 | exports: &mut EXPORTS[..], |
| 442 | template: &mut TEMPLATE[.. capacity], |
| 443 | relocations: &mut RELOCS[..], |
| 444 | stack: rv64::StackCheck::Off, |
| 445 | }, |
| 446 | arena, |
| 447 | scratch |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | /// Dependency exports with a full-width function pointer and a large data offset. |
| 452 | fn imports() -> *[super::Symbol] { |
| 453 | return &[ |
| 454 | super::Symbol { |
| 455 | name: "dep::value", |
| 456 | target: super::Target::Data(super::DataRef { |
| 457 | slot: 1, offset: 8192 |
| 458 | }), |
| 459 | startable: true, |
| 460 | }, |
| 461 | super::Symbol { |
| 462 | name: "dep::call", |
| 463 | target: super::Target::Function(0x180001234), |
| 464 | startable: true, |
| 465 | }, |
| 466 | ]; |
| 467 | } |
| 468 | |
| 469 | /// Read a little-endian pointer without requiring buffer alignment. |
| 470 | fn pointer(bytes: &[u8], offset: u32) -> u64 { |
| 471 | let mut value: u64 = 0; |
| 472 | for i in 0 .. 8 { |
| 473 | set value |= bytes[offset + i] as u64 << (i as u64 * 8); |
| 474 | } |
| 475 | return value; |
| 476 | } |
| 477 | |
| 478 | /// Check repeated pointers, full-width code addresses, padding, and zero-filled tails. |
| 479 | @test unsafe fn privateRelocations() throws (testing::TestError) { |
| 480 | let package = try compile(imports(), 64) catch { |
| 481 | throw testing::TestError::Failed; |
| 482 | }; |
| 483 | try testing::expect( |
| 484 | package.template.len == 32 |
| 485 | and package.memory == 48 |
| 486 | and package.alignment == 16 |
| 487 | ); |
| 488 | try testing::expect(package.relocations.len == 1 and package.relocations[0].count == 2); |
| 489 | try testing::expect(pointer(package.template, 16) == 0x180001234); |
| 490 | let mut first: [u8; 48] = [255; 48]; |
| 491 | let mut second: [u8; 48] = [255; 48]; |
| 492 | try super::instantiate(&package, &[0, 0x180002000, 0x180008000], &mut first[..]) catch { |
| 493 | throw testing::TestError::Failed; |
| 494 | }; |
| 495 | try super::instantiate(&package, &[0, 0x280002000, 0x280008000], &mut second[..]) catch { |
| 496 | throw testing::TestError::Failed; |
| 497 | }; |
| 498 | try testing::expect( |
| 499 | pointer(&first[..], 0) == 0x180004000 and pointer( |
| 500 | &first[..], 8 |
| 501 | ) == 0x180004000 |
| 502 | ); |
| 503 | try testing::expect( |
| 504 | pointer(&second[..], 0) == 0x280004000 and pointer( |
| 505 | &second[..], 8 |
| 506 | ) == 0x280004000 |
| 507 | ); |
| 508 | try testing::expect(pointer(&first[..], 16) == pointer(&second[..], 16)); |
| 509 | for i in 24 .. 48 { |
| 510 | try testing::expect(first[i] == 0 and second[i] == 0); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /// A package with a heap region in its data. |
| 515 | unsafe fn heapInput() -> binary::Package { |
| 516 | return binary::Package { |
| 517 | version: nil, |
| 518 | symbols: &[], |
| 519 | name: "p", |
| 520 | dependencies: &[], |
| 521 | exports: &[], |
| 522 | entry: nil, |
| 523 | program: il::Program { |
| 524 | data: &[ |
| 525 | il::Data { |
| 526 | name: "p::pool", |
| 527 | size: 64, |
| 528 | alignment: 16, |
| 529 | layout: 0, |
| 530 | readOnly: false, |
| 531 | heap: il::HeapType::Typed, |
| 532 | isZeroInit: true, |
| 533 | values: &[], |
| 534 | }, |
| 535 | ], |
| 536 | fns: &[], |
| 537 | layouts: &[il::Layout { size: 0, alignment: 1, slots: &[], regions: 0 }], |
| 538 | signatures: &[], |
| 539 | traits: &[], |
| 540 | }, |
| 541 | links: binary::EMPTY_LINKS, |
| 542 | }; |
| 543 | } |
| 544 | |
| 545 | /// A heap region becomes a header and a pool in the private memory of an instance. The header |
| 546 | /// holds no used bytes, the size of the pool, and the address of the pool in this instance. |
| 547 | @test unsafe fn privateHeap() throws (testing::TestError) { |
| 548 | let arena = heap::over(&mut ARENA[..]); |
| 549 | let scratch = heap::over(&mut SCRATCH[..]); |
| 550 | let package = heapInput(); |
| 551 | let compiled = try super::compile( |
| 552 | &package, |
| 553 | 2, |
| 554 | 0x180000000, |
| 555 | &[], |
| 556 | super::Storage { |
| 557 | data: &mut DATA[..], |
| 558 | symbols: &mut SYMBOLS[..], |
| 559 | exports: &mut EXPORTS[..], |
| 560 | template: &mut TEMPLATE[..], |
| 561 | relocations: &mut RELOCS[..], |
| 562 | stack: rv64::StackCheck::Off, |
| 563 | }, |
| 564 | arena, |
| 565 | scratch |
| 566 | ) catch { |
| 567 | throw testing::TestError::Failed; |
| 568 | }; |
| 569 | try testing::expect(compiled.relocations.len == 1 and compiled.relocations[0].count == 1); |
| 570 | let mut memory: [u8; 128] = [255; 128]; |
| 571 | try super::instantiate(&compiled, &[0, 0x180002000, 0x180008000], &mut memory[..]) catch { |
| 572 | throw testing::TestError::Failed; |
| 573 | }; |
| 574 | let header = compiled.relocations[0].offset - 16; |
| 575 | try testing::expect(pointer(&memory[..], header) == 0); |
| 576 | try testing::expect(pointer(&memory[..], header + 8) == 64); |
| 577 | let pool = pointer(&memory[..], header + 16); |
| 578 | try testing::expect(pool == 0x180008000 + compiled.relocations[0].target.offset as u64); |
| 579 | try testing::expect(pool % 16 == 0); |
| 580 | } |
| 581 | |
| 582 | /// A missing instance, bad base, or short buffer leaves instance bytes untouched. |
| 583 | @test unsafe fn instanceFailures() throws (testing::TestError) { |
| 584 | let package = try compile(imports(), 64) catch { |
| 585 | throw testing::TestError::Failed; |
| 586 | }; |
| 587 | let mut bytes: [u8; 48] = [255; 48]; |
| 588 | let mut failed: u32 = 0; |
| 589 | try super::instantiate(&package, &[0, 0, 0x8000], &mut bytes[..]) catch err { |
| 590 | try testing::expect(err == super::Error::Instance); |
| 591 | set failed += 1; |
| 592 | }; |
| 593 | try super::instantiate(&package, &[0, 0x2000, 0x8008], &mut bytes[..]) catch err { |
| 594 | try testing::expect(err == super::Error::Alignment); |
| 595 | set failed += 1; |
| 596 | }; |
| 597 | try super::instantiate(&package, &[0, 0x2000, 0x8000], &mut bytes[.. 47]) catch err { |
| 598 | try testing::expect(err == super::Error::Capacity); |
| 599 | set failed += 1; |
| 600 | }; |
| 601 | try super::instantiate(&package, &[0, 0xffffffffffffffff, 0x8000], &mut bytes[..]) catch err { |
| 602 | try testing::expect(err == super::Error::Range); |
| 603 | set failed += 1; |
| 604 | }; |
| 605 | try testing::expect(failed == 4); |
| 606 | for byte in &bytes[..] { |
| 607 | try testing::expect(byte == 255); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /// Missing symbols, duplicate imports, kind mismatches, and short output fail explicitly. |
| 612 | @test unsafe fn linkFailures() throws (testing::TestError) { |
| 613 | let mut failed: u32 = 0; |
| 614 | try compile(&[], 64) catch err { |
| 615 | try testing::expect(err == super::Error::Symbol); |
| 616 | set failed += 1; |
| 617 | }; |
| 618 | let names = imports(); |
| 619 | try compile(&[names[0], names[0]], 64) catch err { |
| 620 | try testing::expect(err == super::Error::Symbol); |
| 621 | set failed += 1; |
| 622 | }; |
| 623 | try compile( |
| 624 | &[ |
| 625 | super::Symbol { |
| 626 | name: "dep::value", |
| 627 | target: super::Target::Function(0x8000), |
| 628 | startable: true, |
| 629 | }, |
| 630 | names[1], |
| 631 | ], |
| 632 | 64 |
| 633 | ) catch err { |
| 634 | try testing::expect(err == super::Error::Symbol); |
| 635 | set failed += 1; |
| 636 | }; |
| 637 | try compile(names, 31) catch err { |
| 638 | try testing::expect(err == super::Error::Capacity); |
| 639 | set failed += 1; |
| 640 | }; |
| 641 | try testing::expect(failed == 4); |
| 642 | let package = try compile(names, 64) catch { |
| 643 | throw testing::TestError::Failed; |
| 644 | }; |
| 645 | try testing::expect(package.exports.len == 1); |
| 646 | } |