il: Link verified packages by exact image
1e2ce264ceeb6e83477a12f79679d362f475bf68ab35f0dba0c66f3c2795599a
Pin dependencies by ImageId and check imported signatures, layouts, rights and borrow contracts against verified provider declarations. Read those declarations from length-delimited RIL function records. Carry resolved provider IDs through lowering. Use RIL0 followed by four zero bytes as the image identity domain. The ASCII digit versions the hash scheme. The eight-byte prefix keeps word alignment for a contiguous aligned payload. Share verified native images between domains, with private instance state and separate context-entry permissions. Resolve resident providers once per load and retain package edges during RIL emission. Supply the resolved dependencies directly to symbol collection and retain provider export tables during boot import binding. Check boot image reuse once before reservation. Keep publication setup in kernel test support. Use checked word access, rotates and trailing-zero counts in BLAKE3. Size compiler storage for complete standard library and kernel test builds. Cover typed dependencies, borrowed results, image versions, exact prefix bytes, resident reuse and cancellation. Assisted-by: Codex:gpt-6 Change-Id: I29413504c47e431f9ea3f333849d74a27706e25b
1 parent
16903da6
Makefile
+1 -1
| 24 | 24 | ||
| 25 | 25 | # Compiler build |
|
| 26 | 26 | ||
| 27 | 27 | SEED := seed/radiance.rv64 |
|
| 28 | 28 | COMPILER_SRC := compiler/radiance.rad compiler/radiance/codegen.rad \ |
|
| 29 | - | compiler/radiance/testgen.rad |
|
| 29 | + | compiler/radiance/testgen.rad compiler/radiance/link.rad |
|
| 30 | 30 | SEED_OPTS := $(STD) -pkg radiance $(patsubst %,-mod %,$(COMPILER_SRC)) \ |
|
| 31 | 31 | -entry radiance |
|
| 32 | 32 | ||
| 33 | 33 | $(RAD_BIN): Makefile $(SEED) std.lib $(STD_LIB) $(COMPILER_SRC) | $(BIN_DIR) |
|
| 34 | 34 | @echo "radiance $(SEED) => $@" |
compiler/radiance.rad
+89 -82
| 1 | 1 | //! Radiance compiler front-end. |
|
| 2 | 2 | mod codegen; |
|
| 3 | 3 | mod testgen; |
|
| 4 | + | mod link; |
|
| 4 | 5 | ||
| 5 | - | use std::mem; |
|
| 6 | - | use std::lang::alloc::AllocError; |
|
| 7 | - | use std::fmt; |
|
| 8 | - | use std::io; |
|
| 9 | - | use std::mem::heap; |
|
| 10 | - | use std::lang::ast; |
|
| 11 | - | use std::lang::layout; |
|
| 12 | - | use std::lang::parser; |
|
| 13 | - | use std::lang::pretty; |
|
| 14 | - | use std::lang::scanner; |
|
| 15 | - | use std::lang::resolver; |
|
| 16 | - | use std::lang::module; |
|
| 17 | - | use std::lang::strings; |
|
| 18 | - | use std::lang::package; |
|
| 19 | - | use std::lang::il; |
|
| 20 | - | use std::lang::lower; |
|
| 21 | 6 | use std::arch::rv64; |
|
| 22 | 7 | use std::arch::rv64::asm; |
|
| 23 | 8 | use std::arch::rv64::image; |
|
| 24 | 9 | use std::arch::rv64::printer; |
|
| 25 | - | use std::lang::sexpr; |
|
| 10 | + | use std::collections::dict; |
|
| 11 | + | use std::fmt; |
|
| 12 | + | use std::fs; |
|
| 13 | + | use std::io; |
|
| 14 | + | use std::lang::alloc::AllocError; |
|
| 15 | + | use std::lang::ast; |
|
| 26 | 16 | use std::lang::gen::data; |
|
| 27 | 17 | use std::lang::gen::labels; |
|
| 18 | + | use std::lang::gen::types; |
|
| 19 | + | use std::lang::il; |
|
| 28 | 20 | use std::lang::il::binary; |
|
| 29 | 21 | use std::lang::il::binary::collect; |
|
| 22 | + | use std::lang::il::binary::linkage; |
|
| 30 | 23 | use std::lang::il::binary::program; |
|
| 31 | - | use std::lang::gen::types; |
|
| 24 | + | use std::lang::layout; |
|
| 25 | + | use std::lang::lower; |
|
| 26 | + | use std::lang::module; |
|
| 27 | + | use std::lang::package; |
|
| 28 | + | use std::lang::parser; |
|
| 29 | + | use std::lang::pretty; |
|
| 30 | + | use std::lang::resolver; |
|
| 31 | + | use std::lang::scanner; |
|
| 32 | + | use std::lang::sexpr; |
|
| 33 | + | use std::lang::strings; |
|
| 34 | + | use std::mem; |
|
| 35 | + | use std::mem::heap; |
|
| 32 | 36 | use std::sys; |
|
| 33 | - | use std::fs; |
|
| 34 | - | use std::collections::dict; |
|
| 35 | 37 | ||
| 36 | 38 | /// Maximum number of modules we can load per package. |
|
| 37 | 39 | constant MAX_LOADED_MODULES: u32 = module::MAX_MODULES; |
|
| 38 | 40 | /// Maximum number of packages we can compile. |
|
| 39 | 41 | constant MAX_PACKAGES: u32 = 4; |
| 44 | 46 | /// room for it to grow. |
|
| 45 | 47 | constant MAX_SOURCES_SIZE: u32 = 6291456; |
|
| 46 | 48 | /// Maximum number of assembly source paths we can load per package. |
|
| 47 | 49 | constant MAX_ASM_MODULES: u32 = 64; |
|
| 48 | 50 | ||
| 49 | - | /// AST arena size (84 MB). It retains parsed nodes throughout compilation. |
|
| 51 | + | /// AST arena size (92 MB). It retains parsed nodes throughout compilation. |
|
| 50 | 52 | /// The kernel test build includes all standard library tests and its generated entry. |
|
| 51 | - | constant TEMP_ARENA_SIZE: u32 = 88080384; |
|
| 53 | + | constant TEMP_ARENA_SIZE: u32 = 96468992; |
|
| 52 | 54 | /// Function IL arena size (16 MB). Every lowering pass resets it after each |
|
| 53 | 55 | /// function. It must fit the largest function of the standard library, |
|
| 54 | 56 | /// compiler and kernel test builds. |
|
| 55 | 57 | constant FN_ARENA_SIZE: u32 = 16777216; |
|
| 56 | - | /// IL dump text buffer size (32 MB). It holds the printed functions of the |
|
| 57 | - | /// whole program until the data section is printed. The IL text of the test |
|
| 58 | - | /// build of the standard library takes about 6 MB. |
|
| 59 | - | constant IL_TEXT_SIZE: u32 = 33554432; |
|
| 58 | + | /// IL dump text buffer size (24 MB). It holds the printed functions of the |
|
| 59 | + | /// whole program until the data section is printed. The complete kernel test |
|
| 60 | + | /// build uses about 13 MB of IL text. |
|
| 61 | + | constant IL_TEXT_SIZE: u32 = 25165824; |
|
| 60 | 62 | /// Code generator storage size (32 MB). The code generator reserves its code, |
|
| 61 | 63 | /// relocation, function, and debug entry lists up front, 15.0 MB without debug |
|
| 62 | 64 | /// entries and 21.3 MB with them. The compiler and the test build of the |
|
| 63 | 65 | /// standard library fill at most 26% of the code list and 59% of the function |
|
| 64 | 66 | /// list, so no list grows past its reservation. The rest of the region absorbs |
|
| 65 | 67 | /// the doubling of any one relocation list of a larger program. |
|
| 66 | 68 | constant EMITTER_SIZE: u32 = 33554432; |
|
| 67 | - | /// Main arena size (166 MB) - lives throughout compilation. |
|
| 68 | - | /// Used for: resolver data, types, symbols, global IL data, and assembled modules. |
|
| 69 | - | constant MAIN_ARENA_SIZE: u32 = 174063616; |
|
| 69 | + | /// Main arena size (184 MB). It holds resolver data, types, symbols, global |
|
| 70 | + | /// IL data, and assembled modules throughout compilation. The kernel test |
|
| 71 | + | /// build uses about 175 MB. |
|
| 72 | + | constant MAIN_ARENA_SIZE: u32 = 192937984; |
|
| 70 | 73 | /// Driver table arena size (2 MB). It holds the resolver error buffer, the |
|
| 71 | 74 | /// resolver package list, the module path copies, and the test descriptors for |
|
| 72 | 75 | /// the whole compilation. |
|
| 73 | 76 | constant DRIVER_ARENA_SIZE: u32 = 2097152; |
|
| 74 | 77 |
| 1327 | 1330 | }; |
|
| 1328 | 1331 | let mut low = newLowerer(ctx, modules, res, fnArena); |
|
| 1329 | 1332 | try lowerAllPackagesInto(ctx, modules, &mut low, &mut spool); |
|
| 1330 | 1333 | let dataOrigins = low.dataOrigins.take(); |
|
| 1331 | 1334 | let tableStarts = low.tables.tableStarts.take(); |
|
| 1335 | + | let references = low.tables.references.take(); |
|
| 1332 | 1336 | let whole = lower::finalize(low, lower::FnCollector { fns: &mut [] }); |
|
| 1333 | 1337 | ||
| 1334 | 1338 | let mut head: *mut [u8] = &mut []; |
|
| 1335 | 1339 | try head.resize(RIL_HEAD_SIZE, 0, spool.arena) catch { |
|
| 1336 | 1340 | throw error(&["out of memory for binary RIL tables"]); |
|
| 1337 | 1341 | }; |
|
| 1338 | - | for i in 0 .. ctx.packageCount { |
|
| 1339 | - | let pkg = &ctx.packages[i]; |
|
| 1340 | - | let tables = lower::packageTables(&whole, &tableStarts[..], pkg.id); |
|
| 1341 | - | let mut dataItems: *mut [il::Data] = &mut []; |
|
| 1342 | - | for item, j in whole.data { |
|
| 1343 | - | let origin = dataOrigins[j]; |
|
| 1344 | - | if origin.package <> pkg.id { |
|
| 1342 | + | let mut plan = try link::prepare( |
|
| 1343 | + | &mut spool, |
|
| 1344 | + | &whole, |
|
| 1345 | + | &dataOrigins[..], |
|
| 1346 | + | &tableStarts[..], |
|
| 1347 | + | &references[..] |
|
| 1348 | + | ) catch { |
|
| 1349 | + | throw error(&["cannot resolve typed package imports"]); |
|
| 1350 | + | }; |
|
| 1351 | + | let mut emitted: [bool; MAX_PACKAGES] = [false; MAX_PACKAGES]; |
|
| 1352 | + | let mut identities: [?binary::Id; MAX_PACKAGES] = [nil; MAX_PACKAGES]; |
|
| 1353 | + | let mut remaining = ctx.packageCount; |
|
| 1354 | + | while remaining > 0 { |
|
| 1355 | + | let mut progress = false; |
|
| 1356 | + | for i in 0 .. ctx.packageCount { |
|
| 1357 | + | if emitted[i] { |
|
| 1345 | 1358 | continue; |
|
| 1346 | 1359 | } |
|
| 1347 | - | try dataItems.append(item, spool.arena) catch { |
|
| 1348 | - | throw error(&["out of memory selecting package data", pkg.name]); |
|
| 1360 | + | let selected = try link::pins( |
|
| 1361 | + | plan.dependencies[i], &identities[..], spool.arena |
|
| 1362 | + | ) catch { |
|
| 1363 | + | throw error(&["cannot select package dependency identities"]); |
|
| 1364 | + | }; |
|
| 1365 | + | let pins = selected else continue; |
|
| 1366 | + | set plan.images[i].links.pins = pins; |
|
| 1367 | + | let pkg = &ctx.packages[i]; |
|
| 1368 | + | let image = plan.images[i]; |
|
| 1369 | + | let fnCount = spool.packages[i].count; |
|
| 1370 | + | let length = try program::encodeHead( |
|
| 1371 | + | &mut head, &image, &mut spool.packages[i].names.index, fnCount |
|
| 1372 | + | ) catch { |
|
| 1373 | + | throw error(&["binary RIL tables exceed their buffer", pkg.name]); |
|
| 1349 | 1374 | }; |
|
| 1350 | - | if let case lower::Linkage::Exported = origin.linkage { |
|
| 1351 | - | let definition = binary::Export { name: item.name, kind: binary::ExportKind::Data }; |
|
| 1352 | - | try spool.packages[i].exports.append(definition, spool.arena) catch { |
|
| 1353 | - | throw error(&["out of memory collecting package exports", pkg.name]); |
|
| 1375 | + | let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN]; |
|
| 1376 | + | let mut pos: u32 = 0; |
|
| 1377 | + | for part in &[directory, "/", pkg.name, ".ril"] { |
|
| 1378 | + | set pos += try mem::copy(&mut path[pos ..], part) catch { |
|
| 1379 | + | throw error(&["binary RIL output path is too long"]); |
|
| 1354 | 1380 | }; |
|
| 1355 | 1381 | } |
|
| 1356 | - | } |
|
| 1357 | - | let exports = spool.packages[i].exports.take(); |
|
| 1358 | - | let entry = spool.packages[i].entry; |
|
| 1359 | - | let image = try collect::finish( |
|
| 1360 | - | &mut spool.packages[i].names, |
|
| 1361 | - | pkg.name, |
|
| 1362 | - | il::Program { |
|
| 1363 | - | data: &dataItems[..], |
|
| 1364 | - | fns: &[], |
|
| 1365 | - | layouts: tables.layouts, |
|
| 1366 | - | signatures: tables.signatures, |
|
| 1367 | - | traits: tables.traits, |
|
| 1368 | - | }, |
|
| 1369 | - | &exports[..], |
|
| 1370 | - | entry, |
|
| 1371 | - | spool.arena |
|
| 1372 | - | ) catch { |
|
| 1373 | - | throw error(&["cannot collect binary RIL package", pkg.name]); |
|
| 1374 | - | }; |
|
| 1375 | - | let fnCount = spool.packages[i].count; |
|
| 1376 | - | let length = try program::encodeHead( |
|
| 1377 | - | &mut head, &image, &mut spool.packages[i].names.index, fnCount |
|
| 1378 | - | ) catch { |
|
| 1379 | - | throw error(&["binary RIL tables exceed their buffer", pkg.name]); |
|
| 1380 | - | }; |
|
| 1381 | - | let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN]; |
|
| 1382 | - | let mut pos: u32 = 0; |
|
| 1383 | - | for part in &[directory, "/", pkg.name, ".ril"] { |
|
| 1384 | - | set pos += try mem::copy(&mut path[pos ..], part) catch { |
|
| 1385 | - | throw error(&["binary RIL output path is too long"]); |
|
| 1382 | + | let start = spool.packages[i].start; |
|
| 1383 | + | let end = spool.packages[i].end; |
|
| 1384 | + | // Only dependents use the package's image identity. |
|
| 1385 | + | if plan.needed[i] { |
|
| 1386 | + | set identities[i] = linkage::imageParts( |
|
| 1387 | + | &head[.. length], &spool.bytes[start .. end] |
|
| 1388 | + | ); |
|
| 1389 | + | } |
|
| 1390 | + | let mut out = try fs::create(&path[.. pos]) catch { |
|
| 1391 | + | throw error(&["cannot write binary RIL package", pkg.name]); |
|
| 1392 | + | }; |
|
| 1393 | + | out.write(&head[.. length]); |
|
| 1394 | + | out.write(&spool.bytes[start .. end]); |
|
| 1395 | + | try out.close() catch { |
|
| 1396 | + | throw error(&["cannot write binary RIL package", pkg.name]); |
|
| 1386 | 1397 | }; |
|
| 1398 | + | set emitted[i] = true; |
|
| 1399 | + | set remaining -= 1; |
|
| 1400 | + | set progress = true; |
|
| 1401 | + | } |
|
| 1402 | + | if not progress { |
|
| 1403 | + | throw error(&["binary RIL packages have cyclic dependencies"]); |
|
| 1387 | 1404 | } |
|
| 1388 | - | let start = spool.packages[i].start; |
|
| 1389 | - | let end = spool.packages[i].end; |
|
| 1390 | - | let mut out = try fs::create(&path[.. pos]) catch { |
|
| 1391 | - | throw error(&["cannot write binary RIL package", pkg.name]); |
|
| 1392 | - | }; |
|
| 1393 | - | out.write(&head[.. length]); |
|
| 1394 | - | out.write(&spool.bytes[start .. end]); |
|
| 1395 | - | try out.close() catch { |
|
| 1396 | - | throw error(&["cannot write binary RIL package", pkg.name]); |
|
| 1397 | - | }; |
|
| 1398 | 1405 | } |
|
| 1399 | 1406 | } |
|
| 1400 | 1407 | ||
| 1401 | 1408 | /// Lower, optionally dump, and optionally generate binary output. |
|
| 1402 | 1409 | /// The lowerer takes ownership of `res` and of `fnArena`, and the passes take |
compiler/radiance/codegen.rad
+16 -6
| 2 | 2 | //! Each sink reclaims the function arena after it consumes a function. |
|
| 3 | 3 | ||
| 4 | 4 | use std::arch::rv64; |
|
| 5 | 5 | use std::collections::dict; |
|
| 6 | 6 | use std::io; |
|
| 7 | - | use std::mem::heap; |
|
| 8 | - | use std::lang::il; |
|
| 9 | - | use std::lang::lower; |
|
| 10 | - | use std::lang::sexpr; |
|
| 11 | 7 | use std::lang::gen::data; |
|
| 8 | + | use std::lang::il; |
|
| 12 | 9 | use std::lang::il::binary; |
|
| 13 | 10 | use std::lang::il::binary::collect; |
|
| 14 | 11 | use std::lang::il::binary::program; |
|
| 12 | + | use std::lang::lower; |
|
| 13 | + | use std::lang::sexpr; |
|
| 15 | 14 | use std::mem; |
|
| 15 | + | use std::mem::heap; |
|
| 16 | 16 | ||
| 17 | 17 | /// Code generator that receives the functions of a streaming lowering pass. |
|
| 18 | 18 | export record Context { |
|
| 19 | 19 | /// Generator owned by the context for the whole lowering pass. |
|
| 20 | 20 | generator: rv64::Generator, |
| 145 | 145 | for name in names { |
|
| 146 | 146 | let mut tables = try collect::new(collect::INDEX_SLOTS, arena); |
|
| 147 | 147 | try collect::start(&mut tables, name, arena); |
|
| 148 | 148 | try packages.append( |
|
| 149 | 149 | RilPackage { |
|
| 150 | - | name, names: tables, start: 0, end: 0, count: 0, exports: &mut [], entry: nil |
|
| 150 | + | name, |
|
| 151 | + | names: tables, |
|
| 152 | + | start: 0, |
|
| 153 | + | end: 0, |
|
| 154 | + | count: 0, |
|
| 155 | + | exports: &mut [], |
|
| 156 | + | entry: nil, |
|
| 151 | 157 | }, |
|
| 152 | 158 | arena |
|
| 153 | 159 | ) catch { |
|
| 154 | 160 | throw binary::Error::Storage; |
|
| 155 | 161 | }; |
| 199 | 205 | throw binary::Error::Invalid; |
|
| 200 | 206 | } |
|
| 201 | 207 | let name = spool.packages[i].name; |
|
| 202 | 208 | let stored = try collect::function(&mut spool.packages[i].names, name, func, spool.arena); |
|
| 203 | 209 | if let case lower::Linkage::Exported = origin.linkage { |
|
| 204 | - | let definition = binary::Export { name: stored, kind: binary::ExportKind::Function }; |
|
| 210 | + | let definition = binary::Export { |
|
| 211 | + | name: stored, |
|
| 212 | + | kind: binary::ExportKind::Function, |
|
| 213 | + | startable: false, |
|
| 214 | + | }; |
|
| 205 | 215 | try spool.packages[i].exports.append(definition, spool.arena) catch { |
|
| 206 | 216 | throw binary::Error::Storage; |
|
| 207 | 217 | }; |
|
| 208 | 218 | } |
|
| 209 | 219 | if let case lower::FnRole::Default = role { |
compiler/radiance/link.rad
added
+176 -0
| 1 | + | //! Package heads and explicit import selection from resolved definition ownership. |
|
| 2 | + | ||
| 3 | + | use std::lang::il; |
|
| 4 | + | use std::lang::il::binary; |
|
| 5 | + | use std::lang::il::binary::collect; |
|
| 6 | + | use std::lang::lower; |
|
| 7 | + | use std::mem; |
|
| 8 | + | use std::mem::heap; |
|
| 9 | + | use super::codegen; |
|
| 10 | + | ||
| 11 | + | /// Test whether a resolved foreign reference names a public provider definition. |
|
| 12 | + | fn exported(exports: &[*[binary::Export]], owner: u32, name: *[u8]) -> bool { |
|
| 13 | + | for item in exports[owner] { |
|
| 14 | + | if mem::eq(item.name, name) { |
|
| 15 | + | return true; |
|
| 16 | + | } |
|
| 17 | + | } |
|
| 18 | + | return false; |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | /// Package heads and the resolved graph used to emit them. |
|
| 22 | + | export record Plan { |
|
| 23 | + | /// Package heads whose function records remain in the spool. |
|
| 24 | + | images: *mut [binary::Package], |
|
| 25 | + | /// Provider indices in each package's dependency order. |
|
| 26 | + | dependencies: *[*[u32]], |
|
| 27 | + | /// Whether a dependent package needs the image identity. |
|
| 28 | + | needed: *[bool], |
|
| 29 | + | } |
|
| 30 | + | ||
| 31 | + | /// Construct package heads with typed aliases and exact dependency labels. |
|
| 32 | + | /// Function records remain in the spool. |
|
| 33 | + | export fn prepare( |
|
| 34 | + | spool: &mut codegen::RilSpool, |
|
| 35 | + | whole: &il::Program, |
|
| 36 | + | origins: &[lower::Origin], |
|
| 37 | + | starts: &[lower::TableStart], |
|
| 38 | + | references: &[lower::Reference] |
|
| 39 | + | ) -> Plan throws (binary::Error) { |
|
| 40 | + | let arena = spool.arena; |
|
| 41 | + | for item, i in whole.data { |
|
| 42 | + | if let case lower::Linkage::Exported = origins[i].linkage { |
|
| 43 | + | try spool.packages[origins[i].package as u32].exports.append( |
|
| 44 | + | binary::Export { |
|
| 45 | + | name: item.name, kind: binary::ExportKind::Data, startable: false |
|
| 46 | + | }, |
|
| 47 | + | arena |
|
| 48 | + | ) catch { |
|
| 49 | + | throw binary::Error::Storage; |
|
| 50 | + | }; |
|
| 51 | + | } |
|
| 52 | + | } |
|
| 53 | + | let mut exportTables: *mut [*[binary::Export]] = &mut []; |
|
| 54 | + | for i in 0 .. spool.packages.len { |
|
| 55 | + | let entries = spool.packages[i].exports.take(); |
|
| 56 | + | let frozen: *[binary::Export] = &entries[..]; |
|
| 57 | + | try exportTables.append(frozen, arena) catch { |
|
| 58 | + | throw binary::Error::Storage; |
|
| 59 | + | }; |
|
| 60 | + | } |
|
| 61 | + | let published: *[*[binary::Export]] = &exportTables[..]; |
|
| 62 | + | let mut images: *mut [binary::Package] = &mut []; |
|
| 63 | + | let mut providers: *mut [*[u32]] = &mut []; |
|
| 64 | + | let mut needed: *mut [bool] = &mut []; |
|
| 65 | + | try needed.resize(spool.packages.len, false, arena) catch { |
|
| 66 | + | throw binary::Error::Storage; |
|
| 67 | + | }; |
|
| 68 | + | for i in 0 .. spool.packages.len { |
|
| 69 | + | let tables = lower::packageTables(whole, starts, i as u16); |
|
| 70 | + | let mut dependencies: *mut [*[u8]] = &mut []; |
|
| 71 | + | let mut indices: *mut [u32] = &mut []; |
|
| 72 | + | let mut imports: *mut [binary::Import] = &mut []; |
|
| 73 | + | for reference in references { |
|
| 74 | + | if reference.package as u32 <> i { |
|
| 75 | + | continue; |
|
| 76 | + | } |
|
| 77 | + | let owner = reference.provider as u32; |
|
| 78 | + | if owner >= spool.packages.len { |
|
| 79 | + | throw binary::Error::Symbol; |
|
| 80 | + | } |
|
| 81 | + | if owner == i { |
|
| 82 | + | continue; |
|
| 83 | + | } |
|
| 84 | + | if not exported(&published[..], owner, reference.name) { |
|
| 85 | + | throw binary::Error::Symbol; |
|
| 86 | + | } |
|
| 87 | + | let label = spool.packages[owner].name; |
|
| 88 | + | let mut dependency: ?u32 = nil; |
|
| 89 | + | for k in 0 .. indices.len { |
|
| 90 | + | if indices[k] == owner { |
|
| 91 | + | set dependency = k; |
|
| 92 | + | break; |
|
| 93 | + | } |
|
| 94 | + | } |
|
| 95 | + | if dependency == nil { |
|
| 96 | + | set dependency = dependencies.len; |
|
| 97 | + | try dependencies.append(label, arena) catch { |
|
| 98 | + | throw binary::Error::Storage; |
|
| 99 | + | }; |
|
| 100 | + | try indices.append(owner, arena) catch { |
|
| 101 | + | throw binary::Error::Storage; |
|
| 102 | + | }; |
|
| 103 | + | set needed[owner] = true; |
|
| 104 | + | } |
|
| 105 | + | let selected = dependency else panic "prepare: dependency index"; |
|
| 106 | + | try imports.append( |
|
| 107 | + | binary::Import { |
|
| 108 | + | name: reference.name, |
|
| 109 | + | dependency: selected, |
|
| 110 | + | exported: reference.name, |
|
| 111 | + | kind: reference.kind, |
|
| 112 | + | }, |
|
| 113 | + | arena |
|
| 114 | + | ) catch { |
|
| 115 | + | throw binary::Error::Storage; |
|
| 116 | + | }; |
|
| 117 | + | } |
|
| 118 | + | let mut data: *mut [il::Data] = &mut []; |
|
| 119 | + | for item, k in whole.data { |
|
| 120 | + | if origins[k].package as u32 == i { |
|
| 121 | + | try data.append(item, arena) catch { |
|
| 122 | + | throw binary::Error::Storage; |
|
| 123 | + | }; |
|
| 124 | + | } |
|
| 125 | + | } |
|
| 126 | + | let exports = published[i]; |
|
| 127 | + | let name = spool.packages[i].name; |
|
| 128 | + | let entry = spool.packages[i].entry; |
|
| 129 | + | let mut image = try collect::finish( |
|
| 130 | + | &mut spool.packages[i].names, |
|
| 131 | + | name, |
|
| 132 | + | &dependencies[..], |
|
| 133 | + | il::Program { |
|
| 134 | + | data: &data[..], |
|
| 135 | + | fns: &[], |
|
| 136 | + | layouts: tables.layouts, |
|
| 137 | + | signatures: tables.signatures, |
|
| 138 | + | traits: tables.traits, |
|
| 139 | + | }, |
|
| 140 | + | exports, |
|
| 141 | + | entry, |
|
| 142 | + | arena |
|
| 143 | + | ); |
|
| 144 | + | set image.links.imports = &imports[..]; |
|
| 145 | + | try images.append(image, arena) catch { |
|
| 146 | + | throw binary::Error::Storage; |
|
| 147 | + | }; |
|
| 148 | + | let frozen: *[u32] = &indices[..]; |
|
| 149 | + | try providers.append(frozen, arena) catch { |
|
| 150 | + | throw binary::Error::Storage; |
|
| 151 | + | }; |
|
| 152 | + | } |
|
| 153 | + | return Plan { |
|
| 154 | + | images, dependencies: &providers[..], needed: &needed[..] |
|
| 155 | + | }; |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | /// Resolve a package's exact dependency identities when all providers have been emitted. |
|
| 159 | + | /// An absent result means that a provider has not been emitted yet. |
|
| 160 | + | export fn pins( |
|
| 161 | + | providers: &[u32], identities: &[?binary::Id], arena: &heap::Heap |
|
| 162 | + | ) -> ?*[binary::Id] throws (binary::Error) { |
|
| 163 | + | for index in providers { |
|
| 164 | + | if identities[index] == nil { |
|
| 165 | + | return nil; |
|
| 166 | + | } |
|
| 167 | + | } |
|
| 168 | + | let mut result: *mut [binary::Id] = &mut []; |
|
| 169 | + | for index in providers { |
|
| 170 | + | let pin = identities[index] else panic "pins: provider identity"; |
|
| 171 | + | try result.append(pin, arena) catch { |
|
| 172 | + | throw binary::Error::Storage; |
|
| 173 | + | }; |
|
| 174 | + | } |
|
| 175 | + | return &result[..]; |
|
| 176 | + | } |
kernel/kernel/calls.rad
+33 -32
| 4 | 4 | //! and plain values. The entry points in the last section are the only code |
|
| 5 | 5 | //! here that reads hart-local state, takes the metadata lock, reaches the |
|
| 6 | 6 | //! shared stores, or locates a domain's table. Each locked region has one |
|
| 7 | 7 | //! such access, one call into safe logic, and one release of the lock. |
|
| 8 | 8 | ||
| 9 | + | use std::arch::rv64; |
|
| 10 | + | use std::lang::il; |
|
| 9 | 11 | use std::lang::il::binary; |
|
| 12 | + | use std::mem; |
|
| 10 | 13 | use super::abi; |
|
| 11 | - | use super::slots; |
|
| 12 | - | use super::limits; |
|
| 13 | - | use super::capability; |
|
| 14 | - | use super::pages; |
|
| 15 | - | use super::range; |
|
| 16 | 14 | use super::access; |
|
| 17 | - | use super::physical; |
|
| 18 | - | use super::domains; |
|
| 19 | - | use super::instances; |
|
| 20 | - | use super::registry; |
|
| 21 | 15 | use super::budgets; |
|
| 22 | - | use super::trap; |
|
| 23 | - | use super::events; |
|
| 24 | - | use super::transactions; |
|
| 25 | - | use super::loader; |
|
| 16 | + | use super::capability; |
|
| 17 | + | use super::devices; |
|
| 26 | 18 | use super::dispatch; |
|
| 27 | - | use super::timers; |
|
| 28 | - | use super::sync; |
|
| 29 | - | use super::remote; |
|
| 19 | + | use super::domains; |
|
| 20 | + | use super::events; |
|
| 21 | + | use super::instances; |
|
| 30 | 22 | use super::interrupts; |
|
| 31 | - | use super::devices; |
|
| 32 | - | use super::plic; |
|
| 33 | 23 | use super::lifecycle; |
|
| 34 | - | use super::state; |
|
| 24 | + | use super::limits; |
|
| 25 | + | use super::loader; |
|
| 35 | 26 | use super::locked; |
|
| 36 | - | use std::mem; |
|
| 37 | - | use std::arch::rv64; |
|
| 38 | - | use std::lang::il; |
|
| 27 | + | use super::pages; |
|
| 28 | + | use super::physical; |
|
| 29 | + | use super::plic; |
|
| 30 | + | use super::range; |
|
| 31 | + | use super::registry; |
|
| 32 | + | use super::remote; |
|
| 33 | + | use super::slots; |
|
| 34 | + | use super::state; |
|
| 35 | + | use super::sync; |
|
| 36 | + | use super::timers; |
|
| 37 | + | use super::transactions; |
|
| 38 | + | use super::trap; |
|
| 39 | 39 | ||
| 40 | 40 | // Types // |
|
| 41 | 41 | ||
| 42 | 42 | /// Page reservation operation. |
|
| 43 | 43 | export union PageOperation: Copy { |
| 1286 | 1286 | sync::leave(guard); |
|
| 1287 | 1287 | match begun { |
|
| 1288 | 1288 | case locked::LoadBegun::Held(lease, start, access) => { |
|
| 1289 | 1289 | // The lease keeps this call's workspace allocated and exclusive, and this |
|
| 1290 | 1290 | // continuation is the only holder of its tokens. |
|
| 1291 | - | let input = try lifecycle::decodeLoad(&start.loading, &access, &lease) catch error { |
|
| 1292 | - | cancelLoad(&domain, lease, access); |
|
| 1293 | - | throw error; |
|
| 1294 | - | }; |
|
| 1295 | - | let existing = try lifecycle::identifyLoad( |
|
| 1296 | - | &start.resident, &start.loading, &access, &lease, &input |
|
| 1291 | + | let input = try lifecycle::decodeLoad( |
|
| 1292 | + | &start.resident, &start.loading, &access, &lease |
|
| 1297 | 1293 | ) catch error { |
|
| 1298 | 1294 | cancelLoad(&domain, lease, access); |
|
| 1299 | 1295 | throw error; |
|
| 1300 | 1296 | }; |
|
| 1301 | - | return try finishLoad(&domain, lease, access, &input, existing); |
|
| 1297 | + | return try finishLoad(&domain, lease, access, &input); |
|
| 1302 | 1298 | } |
|
| 1303 | 1299 | } |
|
| 1304 | 1300 | } |
|
| 1305 | 1301 | ||
| 1306 | 1302 | /// Admit the decoded load under the lock, then compile its output and publish it. |
|
| 1307 | 1303 | fn finishLoad( |
|
| 1308 | 1304 | domain: &domains::Domain, |
|
| 1309 | 1305 | lease: lifecycle::Lease, |
|
| 1310 | 1306 | access: lifecycle::LoadAccess, |
|
| 1311 | - | input: &binary::Package, |
|
| 1312 | - | existing: ?abi::Ref |
|
| 1307 | + | input: &loader::Prepared |
|
| 1313 | 1308 | ) -> u64 throws (abi::Error) { |
|
| 1309 | + | let mut existing: ?abi::Ref = nil; |
|
| 1310 | + | if let case loader::Prepared::Resident(object) = *input { |
|
| 1311 | + | set existing = object; |
|
| 1312 | + | } |
|
| 1314 | 1313 | let allocationGuard = sync::enter(); |
|
| 1315 | 1314 | let admitted = try locked::continueLoad( |
|
| 1316 | 1315 | &allocationGuard, domain, lease, access, existing |
|
| 1317 | 1316 | ) catch error { |
|
| 1318 | 1317 | sync::leave(allocationGuard); |
| 1325 | 1324 | } |
|
| 1326 | 1325 | case Admitted::Compile(lease, access) => { |
|
| 1327 | 1326 | // The output was reserved above, so the capture includes its package slot. |
|
| 1328 | 1327 | let start = locked::captureCompile(&allocationGuard, &lease); |
|
| 1329 | 1328 | sync::leave(allocationGuard); |
|
| 1329 | + | let case loader::Prepared::New(package) = *input |
|
| 1330 | + | else panic "finishLoad: compilation requires checked input"; |
|
| 1330 | 1331 | let compiled = try lifecycle::compileLoad( |
|
| 1331 | - | &start.resident, &start.loading, &access, &lease, input |
|
| 1332 | + | &start.resident, &start.loading, &access, &lease, &package |
|
| 1332 | 1333 | ) catch error { |
|
| 1333 | 1334 | cancelLoad(domain, lease, access); |
|
| 1334 | 1335 | throw error; |
|
| 1335 | 1336 | }; |
|
| 1336 | 1337 | let publicationGuard = sync::enter(); |
kernel/kernel/domains.rad
+11 -11
| 1 | 1 | //! Domain creation and ownership of initial contexts and private memory. |
|
| 2 | 2 | ||
| 3 | + | use std::arch::rv64; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 3 | 5 | use super::abi; |
|
| 4 | - | use super::limits; |
|
| 5 | - | use super::slots; |
|
| 6 | - | use super::frames; |
|
| 7 | - | use super::physical; |
|
| 8 | 6 | use super::backing; |
|
| 9 | 7 | use super::capability; |
|
| 10 | - | use super::registry; |
|
| 11 | - | use super::instances; |
|
| 12 | 8 | use super::events; |
|
| 13 | - | use super::trap; |
|
| 14 | - | use super::range; |
|
| 9 | + | use super::frames; |
|
| 10 | + | use super::instances; |
|
| 11 | + | use super::limits; |
|
| 15 | 12 | use super::pages; |
|
| 16 | - | use std::arch::rv64::shared; |
|
| 17 | - | use std::arch::rv64; |
|
| 13 | + | use super::physical; |
|
| 14 | + | use super::range; |
|
| 15 | + | use super::registry; |
|
| 16 | + | use super::slots; |
|
| 17 | + | use super::trap; |
|
| 18 | 18 | ||
| 19 | 19 | /// Retained kernel stack capacity for one suspended execution context. |
|
| 20 | 20 | export constant KERNEL_STACK_PAGES: u32 = 64; |
|
| 21 | 21 | ||
| 22 | 22 | /// Management rights returned to a domain's creator. |
| 667 | 667 | return false; |
|
| 668 | 668 | } |
|
| 669 | 669 | for symbol in package.exports { |
|
| 670 | 670 | match symbol.target { |
|
| 671 | 671 | case shared::Target::Function(entry) => { |
|
| 672 | - | if entry == address { |
|
| 672 | + | if entry == address and symbol.startable { |
|
| 673 | 673 | set found = true; |
|
| 674 | 674 | } |
|
| 675 | 675 | } |
|
| 676 | 676 | else => {} |
|
| 677 | 677 | } |
kernel/kernel/lifecycle.rad
+17 -33
| 1 | 1 | //! Terminal notification delivery and execution eligibility. |
|
| 2 | 2 | use std::lang::il::binary; |
|
| 3 | - | use super::limits; |
|
| 4 | - | use super::slots; |
|
| 5 | - | use super::events; |
|
| 6 | - | use super::domains; |
|
| 7 | - | use super::budgets; |
|
| 8 | 3 | use super::abi; |
|
| 9 | - | use super::remote; |
|
| 10 | - | use super::pages; |
|
| 11 | - | use super::registry; |
|
| 12 | - | use super::loader; |
|
| 4 | + | use super::backing; |
|
| 5 | + | use super::budgets; |
|
| 13 | 6 | use super::capability; |
|
| 7 | + | use super::domains; |
|
| 8 | + | use super::events; |
|
| 14 | 9 | use super::frames; |
|
| 15 | - | use super::physical; |
|
| 16 | - | use super::backing; |
|
| 17 | 10 | use super::interrupts; |
|
| 11 | + | use super::limits; |
|
| 12 | + | use super::loader; |
|
| 13 | + | use super::pages; |
|
| 14 | + | use super::physical; |
|
| 18 | 15 | use super::plic; |
|
| 16 | + | use super::registry; |
|
| 17 | + | use super::remote; |
|
| 18 | + | use super::slots; |
|
| 19 | 19 | ||
| 20 | 20 | /// Maximum backing records released in one domain cleanup step. |
|
| 21 | 21 | constant CLEANUP_BATCH: u32 = 64; |
|
| 22 | 22 | ||
| 23 | 23 | /// Words needed to track request ownership for every context slot. |
| 286 | 286 | ||
| 287 | 287 | /// Decode the private loader input held by a lease. |
|
| 288 | 288 | /// `loading` is the lease's allocation, copied under the lock with `load`, and |
|
| 289 | 289 | /// `access` its tokens. The lease keeps the workspace allocated and exclusive |
|
| 290 | 290 | /// until `cancelLoad` or `finishLoad` takes it. |
|
| 291 | - | export fn decodeLoad(loading: &Loading, access: &LoadAccess, lease: &Lease) -> binary::Package |
|
| 291 | + | export fn decodeLoad( |
|
| 292 | + | packages: ®istry::Published, loading: &Loading, access: &LoadAccess, lease: &Lease |
|
| 293 | + | ) -> loader::Prepared |
|
| 292 | 294 | throws (abi::Error) |
|
| 293 | 295 | { |
|
| 294 | 296 | match access { |
|
| 295 | - | case LoadAccess::Input(input) => return try loader::decodeInput(&loading.input, input), |
|
| 297 | + | case LoadAccess::Input(input) => |
|
| 298 | + | return try loader::decodeInput(packages, &loading.input, input), |
|
| 296 | 299 | case LoadAccess::Output(input, output) => |
|
| 297 | 300 | return try loader::decodeInput( |
|
| 298 | - | &loading.input, input |
|
| 301 | + | packages, &loading.input, input |
|
| 299 | 302 | ), |
|
| 300 | 303 | } |
|
| 301 | 304 | } |
|
| 302 | 305 | ||
| 303 | - | /// Find the resident package that matches decoded loader input, if any. |
|
| 304 | - | /// `packages` was captured under the lock; see `decodeLoad` for `loading` and `lease`. |
|
| 305 | - | export fn identifyLoad( |
|
| 306 | - | packages: ®istry::Published, |
|
| 307 | - | loading: &Loading, |
|
| 308 | - | access: &LoadAccess, |
|
| 309 | - | lease: &Lease, |
|
| 310 | - | package: &binary::Package |
|
| 311 | - | ) -> ?abi::Ref throws (abi::Error) { |
|
| 312 | - | match access { |
|
| 313 | - | case LoadAccess::Input(input) => |
|
| 314 | - | return try loader::identifyInput( |
|
| 315 | - | packages, &loading.input, input, package |
|
| 316 | - | ), |
|
| 317 | - | case LoadAccess::Output(input, output) => |
|
| 318 | - | return try loader::identifyInput(packages, &loading.input, input, package), |
|
| 319 | - | } |
|
| 320 | - | } |
|
| 321 | - | ||
| 322 | 306 | /// Generate native code for decoded loader input into the lease's reserved output. |
|
| 323 | 307 | /// `loading` must be copied under the lock after `reserveOutput`, and `packages` |
|
| 324 | 308 | /// captured after it; see `decodeLoad` for `lease`. |
|
| 325 | 309 | export fn compileLoad( |
|
| 326 | 310 | packages: ®istry::Published, |
kernel/kernel/loader.rad
+58 -56
| 1 | 1 | //! Bounded verified-RIL loading and unpublished physical-memory ownership. |
|
| 2 | 2 | ||
| 3 | - | use std::mem::heap; |
|
| 3 | + | use std::arch::rv64; |
|
| 4 | + | use std::arch::rv64::shared; |
|
| 5 | + | use std::arch::rv64::shared::catalog; |
|
| 4 | 6 | use std::lang::gen::data; |
|
| 7 | + | use std::lang::gen::labels; |
|
| 8 | + | use std::lang::gen::regalloc::liveness; |
|
| 5 | 9 | use std::lang::il::binary; |
|
| 10 | + | use std::lang::il::binary::linkage; |
|
| 6 | 11 | use std::lang::il::binary::program; |
|
| 7 | 12 | use std::lang::il::verify; |
|
| 8 | - | use std::arch::rv64; |
|
| 9 | - | use std::arch::rv64::shared; |
|
| 10 | - | use std::arch::rv64::shared::catalog; |
|
| 13 | + | use std::mem::heap; |
|
| 11 | 14 | use super::abi; |
|
| 12 | - | use super::limits; |
|
| 13 | - | use super::slots; |
|
| 14 | - | use super::frames; |
|
| 15 | - | use super::physical; |
|
| 16 | - | use super::pages; |
|
| 17 | 15 | use super::backing; |
|
| 18 | 16 | use super::capability; |
|
| 19 | - | use super::registry; |
|
| 17 | + | use super::frames; |
|
| 18 | + | use super::limits; |
|
| 19 | + | use super::pages; |
|
| 20 | + | use super::physical; |
|
| 20 | 21 | use super::range; |
|
| 22 | + | use super::registry; |
|
| 23 | + | use super::slots; |
|
| 21 | 24 | use super::sync; |
|
| 22 | - | use std::lang::gen::labels; |
|
| 23 | - | use std::lang::gen::regalloc::liveness; |
|
| 24 | 25 | ||
| 25 | 26 | /// Number of elements reserved for decoded storage. |
|
| 26 | 27 | constant DECODED_CAPACITY: u32 = 67108864; |
|
| 27 | 28 | /// Number of elements reserved for code storage. |
|
| 28 | 29 | constant CODE_CAPACITY: u32 = 16777216; |
| 52 | 53 | export record Workspace: Copy { |
|
| 53 | 54 | /// Stable input bytes used throughout decoding and compilation. |
|
| 54 | 55 | input: [u8; INPUT_BYTES], |
|
| 55 | 56 | /// Decoded binary program and names. |
|
| 56 | 57 | decoded: [u8; DECODED_CAPACITY], |
|
| 58 | + | /// Stable provider references retained through verification and publication. |
|
| 59 | + | dependencies: registry::Dependencies, |
|
| 57 | 60 | /// Native emitter state and generated instructions. |
|
| 58 | 61 | code: [u8; CODE_CAPACITY], |
|
| 59 | 62 | /// Reusable per-function storage, and the packed catalog once compilation ends. |
|
| 60 | 63 | scratch: [u8; SCRATCH_CAPACITY], |
|
| 61 | 64 | /// Native data layout workspace. |
| 272 | 275 | access: &OutputAccess, |
|
| 273 | 276 | length: u32 |
|
| 274 | 277 | ) -> Compiled throws (abi::Error) { |
|
| 275 | 278 | match access { |
|
| 276 | 279 | case OutputAccess::Held(code, metadata) => { |
|
| 277 | - | let count = try registry::imports( |
|
| 278 | - | packages, &input.dependencies[..], &mut work.imports[..] |
|
| 280 | + | let count = try registry::bindings( |
|
| 281 | + | packages, input, &work.dependencies, &mut work.imports[..] |
|
| 279 | 282 | ); |
|
| 280 | 283 | let arena = heap::over(&mut work.code[..]); |
|
| 281 | 284 | let scratch = heap::over(&mut work.scratch[..]); |
|
| 282 | 285 | let package = try shared::compile( |
|
| 283 | 286 | &*input, |
| 327 | 330 | try physical::zeroAt(code, codeBytes as u64, tail); |
|
| 328 | 331 | // Successful packing wrote one entry at metadataBase, in frames private to the output |
|
| 329 | 332 | // reservation. |
|
| 330 | 333 | let native = descriptor(target.metadataBase); |
|
| 331 | 334 | let admission = try registry::admit( |
|
| 332 | - | packages, target.object, native.source, native.package |
|
| 335 | + | packages, target.object, native.source, native.package, &work.dependencies |
|
| 333 | 336 | ); |
|
| 334 | 337 | return Compiled { |
|
| 335 | 338 | code: codePages, |
|
| 336 | 339 | metadata: (used |
|
| 337 | 340 | + (limits::FRAME_SIZE as u32 - 1)) / limits::FRAME_SIZE as u32, |
| 360 | 363 | return physical::start(work); |
|
| 361 | 364 | } |
|
| 362 | 365 | } |
|
| 363 | 366 | } |
|
| 364 | 367 | ||
| 368 | + | /// Resident reuse or a newly verified package held in the loader workspace. |
|
| 369 | + | export union Prepared: Copy { |
|
| 370 | + | /// An immutable image already compiled under the user verification policy. |
|
| 371 | + | Resident(abi::Ref), |
|
| 372 | + | /// Checked local definitions and imports ready for native compilation. |
|
| 373 | + | New(binary::Package), |
|
| 374 | + | } |
|
| 375 | + | ||
| 365 | 376 | /// Snapshot, decode and verify input in the held private workspace. |
|
| 366 | 377 | /// The decoded package stays valid until this reservation is completed or cancelled. |
|
| 367 | - | export fn decode(reservation: &Reservation) -> binary::Package throws (abi::Error) { |
|
| 378 | + | export fn decode(packages: ®istry::Published, reservation: &Reservation) -> Prepared |
|
| 379 | + | throws (abi::Error) |
|
| 380 | + | { |
|
| 368 | 381 | match reservation { |
|
| 369 | 382 | case Reservation::Held(input, access) => |
|
| 370 | 383 | return try decodeInput( |
|
| 371 | - | input, access |
|
| 384 | + | packages, input, access |
|
| 372 | 385 | ), |
|
| 373 | 386 | } |
|
| 374 | 387 | } |
|
| 375 | 388 | ||
| 376 | - | /// Snapshot retained input, decode it, and verify it in its private workspace. Input that |
|
| 377 | - | /// the verifier rejects fails with `VerifyFailed`, and storage exhaustion with `Exhausted`. |
|
| 378 | - | export fn decodeInput(input: &Input, access: &InputAccess) -> binary::Package throws (abi::Error) { |
|
| 389 | + | /// Snapshot and hash input, then reuse verified resident code or decode and verify |
|
| 390 | + | /// a new image. Rejected input fails with VerifyFailed, storage with Exhausted. |
|
| 391 | + | export fn decodeInput( |
|
| 392 | + | packages: ®istry::Published, input: &Input, access: &InputAccess |
|
| 393 | + | ) -> Prepared throws (abi::Error) { |
|
| 379 | 394 | let address = workspaceAddress(access); |
|
| 380 | 395 | match access { |
|
| 381 | 396 | case InputAccess::Held(held, source) => { |
|
| 382 | 397 | // SAFETY: see `Input`. |
|
| 383 | 398 | unsafe { |
|
| 384 | 399 | let work = view(address); |
|
| 385 | 400 | try physical::readAt(source, 0, &mut work.input[.. input.length]); |
|
| 401 | + | let image = linkage::imageId(&work.input[.. input.length]); |
|
| 402 | + | if let resident = registry::findImage( |
|
| 403 | + | packages, image, shared::Verification::Verified |
|
| 404 | + | ) { |
|
| 405 | + | return Prepared::Resident(resident); |
|
| 406 | + | } |
|
| 386 | 407 | let decoder = heap::over(&mut work.decoded[..]); |
|
| 387 | - | let mut package = try program::decode( |
|
| 408 | + | let mut package = try program::parse( |
|
| 388 | 409 | &work.input[.. input.length], |
|
| 389 | 410 | decoder, |
|
| 390 | 411 | binary::Limits { |
|
| 391 | 412 | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 392 | 413 | } |
| 394 | 415 | if err == binary::Error::Storage { |
|
| 395 | 416 | throw abi::Error::Exhausted; |
|
| 396 | 417 | } |
|
| 397 | 418 | throw abi::Error::VerifyFailed; |
|
| 398 | 419 | }; |
|
| 420 | + | set package.links.image = image; |
|
| 399 | 421 | let scratch = heap::over(&mut work.scratch[..]); |
|
| 400 | - | try verify::verifyPackage( |
|
| 422 | + | set work.dependencies = try registry::resolve( |
|
| 423 | + | packages, |
|
| 424 | + | package.dependencies, |
|
| 425 | + | package.links.pins, |
|
| 426 | + | shared::Verification::Verified |
|
| 427 | + | ); |
|
| 428 | + | let providers = try registry::providers(packages, &work.dependencies, decoder); |
|
| 429 | + | try verify::verifyLinkedPackage( |
|
| 401 | 430 | &mut package, |
|
| 431 | + | &providers[..], |
|
| 402 | 432 | shared::verifierConfig(@sizeOf(abi::Env) as u32), |
|
| 403 | 433 | scratch, |
|
| 404 | 434 | decoder |
|
| 405 | 435 | ) catch err { |
|
| 406 | 436 | if err.rule == verify::Rule::Storage { |
|
| 407 | 437 | throw abi::Error::Exhausted; |
|
| 408 | 438 | } |
|
| 409 | 439 | throw abi::Error::VerifyFailed; |
|
| 410 | 440 | }; |
|
| 411 | - | return package; |
|
| 441 | + | return Prepared::New(package); |
|
| 412 | 442 | } |
|
| 413 | 443 | } |
|
| 414 | 444 | } |
|
| 415 | 445 | } |
|
| 416 | 446 |
| 576 | 606 | return result; |
|
| 577 | 607 | } |
|
| 578 | 608 | } |
|
| 579 | 609 | } |
|
| 580 | 610 | ||
| 581 | - | /// Compare the snapshot with immutable resident content while the workspace is held. |
|
| 582 | - | export fn identify( |
|
| 583 | - | packages: ®istry::Published, reservation: &Reservation, input: &binary::Package |
|
| 584 | - | ) -> ?abi::Ref throws (abi::Error) { |
|
| 585 | - | match reservation { |
|
| 586 | - | case Reservation::Held(source, access) => |
|
| 587 | - | return try identifyInput( |
|
| 588 | - | packages, source, access, input |
|
| 589 | - | ), |
|
| 590 | - | } |
|
| 591 | - | } |
|
| 592 | - | ||
| 593 | - | /// Compare retained source bytes with immutable resident package content. |
|
| 594 | - | export fn identifyInput( |
|
| 595 | - | packages: ®istry::Published, source: &Input, access: &InputAccess, input: &binary::Package |
|
| 596 | - | ) -> ?abi::Ref throws (abi::Error) { |
|
| 597 | - | let address = workspaceAddress(access); |
|
| 598 | - | // SAFETY: see `Input`. |
|
| 599 | - | unsafe { |
|
| 600 | - | return try registry::identify( |
|
| 601 | - | packages, &input.name[..], &(*view(address)).input[.. source.length] |
|
| 602 | - | ); |
|
| 603 | - | } |
|
| 604 | - | } |
|
| 605 | - | ||
| 606 | 611 | /// Reserve an Image handle and exclusive workspace under metadata serialization. |
|
| 607 | 612 | export fn reserve( |
|
| 608 | 613 | state: &mut State, store: &mut pages::Store, table: &mut capability::Table, request: Request |
|
| 609 | 614 | ) -> Reservation throws (abi::Error) { |
|
| 610 | 615 | let address = try source(store, table, request); |
| 726 | 731 | request: Request |
|
| 727 | 732 | ) -> abi::Handle throws (abi::Error) { |
|
| 728 | 733 | let pending = try reserve(state, store, table, request); |
|
| 729 | 734 | // `pending` holds the workspace lease until `cancel` or `finish` consumes it below, so the |
|
| 730 | 735 | // workspace stays allocated and exclusive through every decode, identify, and compile step. |
|
| 731 | - | let input = try decode(&pending) catch err { |
|
| 736 | + | let input = try decode(®istry::capture(&*packages), &pending) catch err { |
|
| 732 | 737 | cancel(state, store, table, pending); |
|
| 733 | 738 | throw err; |
|
| 734 | 739 | }; |
|
| 735 | - | let existing = try identify(®istry::capture(&*packages), &pending, &input) catch err { |
|
| 736 | - | cancel(state, store, table, pending); |
|
| 737 | - | throw err; |
|
| 738 | - | }; |
|
| 739 | - | if let object = existing { |
|
| 740 | + | let case Prepared::New(package) = input else { |
|
| 741 | + | let case Prepared::Resident(object) = input else panic "load: invalid prepared input"; |
|
| 740 | 742 | return try finish(state, store, table, pending, object); |
|
| 741 | - | } |
|
| 743 | + | }; |
|
| 742 | 744 | let output = try reserveOutput(&mut store.backings.pool, packages) catch err { |
|
| 743 | 745 | cancel(state, store, table, pending); |
|
| 744 | 746 | throw err; |
|
| 745 | 747 | }; |
|
| 746 | - | let sizes = try compile(®istry::capture(&*packages), &pending, &input, &output) catch err { |
|
| 748 | + | let sizes = try compile(®istry::capture(&*packages), &pending, &package, &output) catch err { |
|
| 747 | 749 | cancelOutput(&mut store.backings.pool, packages, output); |
|
| 748 | 750 | cancel(state, store, table, pending); |
|
| 749 | 751 | throw err; |
|
| 750 | 752 | }; |
|
| 751 | 753 | let object = publishOutput(state, &mut store.backings.pool, packages, output, sizes); |
kernel/kernel/registry.rad
+157 -79
| 7 | 7 | //! cancels, or publishes slots, and boot does so before other harts start. |
|
| 8 | 8 | //! Code that reads packages without the metadata lock does so through a |
|
| 9 | 9 | //! `Published` view captured under the lock: the lock's release and acquire |
|
| 10 | 10 | //! order a package's publication before any later capture. |
|
| 11 | 11 | ||
| 12 | - | use std::mem; |
|
| 13 | 12 | use std::arch::rv64::shared; |
|
| 14 | 13 | use std::arch::rv64::shared::catalog; |
|
| 14 | + | use std::lang::il::binary; |
|
| 15 | + | use std::lang::il::binary::linkage; |
|
| 16 | + | use std::lang::il::binary::program; |
|
| 17 | + | use std::lang::il::verify::linking; |
|
| 18 | + | use std::mem; |
|
| 19 | + | use std::mem::heap; |
|
| 15 | 20 | use super::abi; |
|
| 21 | + | use super::capability; |
|
| 16 | 22 | use super::limits; |
|
| 17 | 23 | use super::slots; |
|
| 18 | - | use super::capability; |
|
| 19 | 24 | ||
| 20 | 25 | /// Rights installed on a new Image capability. |
|
| 21 | 26 | export constant IMAGE_RIGHTS: u16 = abi::READ | abi::EXECUTE | abi::GRANT | abi::TRANSFER; |
|
| 22 | 27 | ||
| 23 | 28 | /// Fixed package registry. Callers serialize admission and handle publication. |
| 76 | 81 | /// Read resident metadata of a package that was live at capture. |
|
| 77 | 82 | export fn lookup(view: &Published, object: abi::Ref) -> shared::Package throws (abi::Error) { |
|
| 78 | 83 | return (try describe(view, object)).package; |
|
| 79 | 84 | } |
|
| 80 | 85 | ||
| 81 | - | /// Find a resident package by its immutable name. |
|
| 86 | + | /// Find a boot role or trusted native dependency by its diagnostic label. |
|
| 82 | 87 | export fn find(view: &Published, name: &[u8]) -> ?abi::Ref { |
|
| 83 | 88 | for i in 0 .. limits::PACKAGES { |
|
| 84 | 89 | if view.slots[i].state <> slots::State::Live { |
|
| 85 | 90 | continue; |
|
| 86 | 91 | } |
| 90 | 95 | } |
|
| 91 | 96 | } |
|
| 92 | 97 | return nil; |
|
| 93 | 98 | } |
|
| 94 | 99 | ||
| 95 | - | /// Reuse identical content and reject conflicting content under a resident name. |
|
| 96 | - | export fn identify(view: &Published, name: &[u8], source: &[u8]) -> ?abi::Ref throws (abi::Error) { |
|
| 97 | - | let object = find(view, name) else { |
|
| 98 | - | return nil; |
|
| 99 | - | }; |
|
| 100 | - | if not mem::eq((try! describe(view, object)).source, &source[..]) { |
|
| 101 | - | throw abi::Error::VerifyFailed; |
|
| 100 | + | /// Find an exact immutable artifact compiled under the requested verification policy. |
|
| 101 | + | export fn findImage( |
|
| 102 | + | view: &Published, image: binary::Id, verification: shared::Verification |
|
| 103 | + | ) -> ?abi::Ref { |
|
| 104 | + | for slot, i in &view.slots[..] { |
|
| 105 | + | if slot.state <> slots::State::Live { |
|
| 106 | + | continue; |
|
| 107 | + | } |
|
| 108 | + | let object = abi::Ref { index: i, generation: slot.generation }; |
|
| 109 | + | let package = try! lookup(view, object); |
|
| 110 | + | if package.contract.verification <> verification { |
|
| 111 | + | continue; |
|
| 112 | + | } |
|
| 113 | + | let identity = package.contract.image else continue; |
|
| 114 | + | if mem::eq(&identity.bytes[..], &image.bytes[..]) { |
|
| 115 | + | return object; |
|
| 116 | + | } |
|
| 102 | 117 | } |
|
| 103 | - | return object; |
|
| 118 | + | return nil; |
|
| 104 | 119 | } |
|
| 105 | 120 | ||
| 106 | 121 | /// Reserve the stable package-state slot before compiling its native code. |
|
| 107 | 122 | export fn reserve(store: &mut Store) -> slots::Reservation throws (abi::Error) { |
|
| 108 | 123 | return try slots::reserve(&mut store.slots[..]); |
| 123 | 138 | dependencies: [abi::Ref; limits::PACKAGES], |
|
| 124 | 139 | /// Number of initialized dependency references. |
|
| 125 | 140 | count: u32, |
|
| 126 | 141 | } |
|
| 127 | 142 | ||
| 128 | - | /// Resolve admission against immutable packages while holding sole publisher ownership. |
|
| 129 | - | /// The caller keeps that ownership and the descriptor storage through commit or cancellation. |
|
| 130 | - | export fn admit( |
|
| 131 | - | view: &Published, object: abi::Ref, source: *[u8], package: shared::Package |
|
| 132 | - | ) -> Admission throws (abi::Error) { |
|
| 133 | - | if not slots::matches(&view.slots[..], object, slots::State::Reserved) { |
|
| 134 | - | throw abi::Error::BadHandle; |
|
| 143 | + | /// Read a decoded image identity or compute it from retained native source. |
|
| 144 | + | fn identity(source: &[u8], package: &shared::Package) -> binary::Id { |
|
| 145 | + | if let image = package.contract.image { |
|
| 146 | + | return image; |
|
| 135 | 147 | } |
|
| 136 | - | if try identify(view, &package.name[..], &source[..]) <> nil { |
|
| 137 | - | throw abi::Error::Busy; |
|
| 138 | - | } |
|
| 139 | - | if package.name.len == 0 |
|
| 140 | - | or package.slot <> object.index |
|
| 141 | - | or package.dependencies.len > limits::PACKAGES |
|
| 148 | + | return linkage::imageId(source); |
|
| 149 | + | } |
|
| 150 | + | ||
| 151 | + | /// Resident providers resolved in a package's dependency order. |
|
| 152 | + | export opaque record Dependencies: Copy { |
|
| 153 | + | /// Stable references to immutable resident packages. |
|
| 154 | + | objects: [abi::Ref; limits::PACKAGES], |
|
| 155 | + | /// Number of initialized references. |
|
| 156 | + | count: u32, |
|
| 157 | + | } |
|
| 158 | + | ||
| 159 | + | /// Resolve exact pins, or diagnostic labels for trusted native packages. |
|
| 160 | + | export fn resolve( |
|
| 161 | + | view: &Published, names: &[*[u8]], pins: &[binary::Id], verification: shared::Verification |
|
| 162 | + | ) -> Dependencies throws (abi::Error) { |
|
| 163 | + | if names.len > limits::PACKAGES |
|
| 164 | + | or (verification == shared::Verification::Verified and pins.len <> names.len) |
|
| 142 | 165 | { |
|
| 143 | 166 | throw abi::Error::VerifyFailed; |
|
| 144 | 167 | } |
|
| 145 | - | let mut result = Admission { |
|
| 146 | - | object, |
|
| 147 | - | entry: catalog::Entry { source, package }, |
|
| 148 | - | dependencies: undefined, |
|
| 149 | - | count: package.dependencies.len, |
|
| 150 | - | }; |
|
| 151 | - | for dependency, i in package.dependencies { |
|
| 152 | - | let target = find(view, &dependency[..]) else { |
|
| 153 | - | throw abi::Error::VerifyFailed; |
|
| 154 | - | }; |
|
| 155 | - | for prior in &result.dependencies[.. i] { |
|
| 168 | + | let mut result = Dependencies { objects: undefined, count: names.len }; |
|
| 169 | + | for name, i in names { |
|
| 170 | + | let mut selected: ?abi::Ref = nil; |
|
| 171 | + | if pins.len == names.len { |
|
| 172 | + | set selected = findImage(view, pins[i], verification); |
|
| 173 | + | } else { |
|
| 174 | + | set selected = find(view, name); |
|
| 175 | + | } |
|
| 176 | + | let target = selected else throw abi::Error::VerifyFailed; |
|
| 177 | + | for prior in &result.objects[.. i] { |
|
| 156 | 178 | if prior == target { |
|
| 157 | 179 | throw abi::Error::VerifyFailed; |
|
| 158 | 180 | } |
|
| 159 | 181 | } |
|
| 160 | - | set result.dependencies[i] = target; |
|
| 182 | + | set result.objects[i] = target; |
|
| 161 | 183 | } |
|
| 162 | 184 | return result; |
|
| 163 | 185 | } |
|
| 164 | 186 | ||
| 187 | + | /// Admit a reserved image with providers resolved under sole publisher ownership. |
|
| 188 | + | /// The caller checks image reuse before admission and keeps publisher ownership |
|
| 189 | + | /// and descriptor storage through commit or cancellation. |
|
| 190 | + | export fn admit( |
|
| 191 | + | view: &Published, |
|
| 192 | + | object: abi::Ref, |
|
| 193 | + | source: *[u8], |
|
| 194 | + | compiled: shared::Package, |
|
| 195 | + | dependencies: &Dependencies |
|
| 196 | + | ) -> Admission throws (abi::Error) { |
|
| 197 | + | if not slots::matches(&view.slots[..], object, slots::State::Reserved) { |
|
| 198 | + | throw abi::Error::BadHandle; |
|
| 199 | + | } |
|
| 200 | + | if compiled.name.len == 0 or compiled.slot <> object.index { |
|
| 201 | + | throw abi::Error::VerifyFailed; |
|
| 202 | + | } |
|
| 203 | + | assert dependencies.count == compiled.dependencies.len; |
|
| 204 | + | let mut package = compiled; |
|
| 205 | + | set package.contract.image = identity(source, &package); |
|
| 206 | + | return Admission { |
|
| 207 | + | object, |
|
| 208 | + | entry: catalog::Entry { source, package }, |
|
| 209 | + | dependencies: dependencies.objects, |
|
| 210 | + | count: dependencies.count, |
|
| 211 | + | }; |
|
| 212 | + | } |
|
| 213 | + | ||
| 165 | 214 | /// Publish admitted metadata under serialization using only bounded reference copies. |
|
| 166 | 215 | export fn commit( |
|
| 167 | 216 | store: &mut Store, reservation: slots::Reservation, admission: Admission |
|
| 168 | 217 | ) -> abi::Ref { |
|
| 169 | 218 | let object = slots::reference(&reservation); |
| 179 | 228 | set store.counts[object.index] = admission.count; |
|
| 180 | 229 | set store.entries[object.index] = admission.entry; |
|
| 181 | 230 | return try! slots::commit(&mut store.slots[..], reservation); |
|
| 182 | 231 | } |
|
| 183 | 232 | ||
| 184 | - | /// Publish persistent compiler output after successful admission checks. |
|
| 185 | - | /// All descriptor pointers and source bytes must remain immutable and resident. |
|
| 186 | - | /// Failure cancels the reservation; duplicate content returns its existing slot. |
|
| 187 | - | export fn publish( |
|
| 188 | - | store: &mut Store, reservation: slots::Reservation, source: *[u8], package: shared::Package |
|
| 189 | - | ) -> abi::Ref throws (abi::Error) { |
|
| 190 | - | let view = capture(&*store); |
|
| 191 | - | let existing = try identify(&view, &package.name[..], &source[..]) catch err { |
|
| 192 | - | cancel(store, reservation); |
|
| 193 | - | throw err; |
|
| 194 | - | }; |
|
| 195 | - | if let object = existing { |
|
| 196 | - | cancel(store, reservation); |
|
| 197 | - | return object; |
|
| 198 | - | } |
|
| 199 | - | let object = slots::reference(&reservation); |
|
| 200 | - | let admission = try admit(&view, object, source, package) catch err { |
|
| 201 | - | cancel(store, reservation); |
|
| 202 | - | throw err; |
|
| 203 | - | }; |
|
| 204 | - | return commit(store, reservation, admission); |
|
| 205 | - | } |
|
| 206 | - | ||
| 207 | 233 | /// Read resident metadata through a generation-bearing package reference. |
|
| 208 | 234 | export fn get(store: &Store, object: abi::Ref) -> shared::Package throws (abi::Error) { |
|
| 209 | 235 | if not slots::matches(&store.slots[..], object, slots::State::Live) { |
|
| 210 | 236 | throw abi::Error::BadHandle; |
|
| 211 | 237 | } |
| 221 | 247 | throw abi::Error::VerifyFailed; |
|
| 222 | 248 | }; |
|
| 223 | 249 | return target; |
|
| 224 | 250 | } |
|
| 225 | 251 | ||
| 226 | - | /// Collect exports from declared dependencies into caller-owned compiler storage. |
|
| 227 | - | export fn imports(view: &Published, dependencies: &[*[u8]], output: &mut [shared::Symbol]) -> u32 |
|
| 228 | - | throws (abi::Error) |
|
| 229 | - | { |
|
| 230 | - | let mut count: u32 = 0; |
|
| 231 | - | for name in dependencies { |
|
| 232 | - | let object = find(view, &name[..]) else { |
|
| 252 | + | /// Decode immutable verified provider declarations into the load's retained workspace. |
|
| 253 | + | export fn providers( |
|
| 254 | + | view: &Published, dependencies: &Dependencies, arena: &heap::Heap |
|
| 255 | + | ) -> *[linking::Provider] throws (abi::Error) { |
|
| 256 | + | let mut result: *mut [linking::Provider] = &mut []; |
|
| 257 | + | for object in &dependencies.objects[.. dependencies.count] { |
|
| 258 | + | let native = try describe(view, object); |
|
| 259 | + | assert native.package.contract.verification == shared::Verification::Verified; |
|
| 260 | + | let pin = native.package.contract.image else panic "providers: image identity"; |
|
| 261 | + | let declarations = try program::declarations(native.source, arena) catch error { |
|
| 262 | + | if error == binary::Error::Storage { |
|
| 263 | + | throw abi::Error::Exhausted; |
|
| 264 | + | } |
|
| 233 | 265 | throw abi::Error::VerifyFailed; |
|
| 234 | 266 | }; |
|
| 235 | - | let package = try! lookup(view, object); |
|
| 236 | - | if package.exports.len > output.len - count { |
|
| 267 | + | try result.append( |
|
| 268 | + | linking::Provider { |
|
| 269 | + | image: pin, contract: declarations |
|
| 270 | + | }, |
|
| 271 | + | arena |
|
| 272 | + | ) catch { |
|
| 237 | 273 | throw abi::Error::Exhausted; |
|
| 238 | - | } |
|
| 239 | - | set count += package.exports.len; |
|
| 274 | + | }; |
|
| 240 | 275 | } |
|
| 241 | - | set count = 0; |
|
| 242 | - | for name in dependencies { |
|
| 243 | - | let object = find(view, &name[..]) else panic "imports: resident dependency"; |
|
| 244 | - | let package = try! lookup(view, object); |
|
| 245 | - | for symbol in package.exports { |
|
| 246 | - | set output[count] = symbol; |
|
| 247 | - | set count += 1; |
|
| 276 | + | return &result[..]; |
|
| 277 | + | } |
|
| 278 | + | ||
| 279 | + | /// Resolve only the checked aliases requested by one verified importing package. |
|
| 280 | + | export fn bindings( |
|
| 281 | + | view: &Published, |
|
| 282 | + | input: &binary::Package, |
|
| 283 | + | dependencies: &Dependencies, |
|
| 284 | + | output: &mut [shared::Symbol] |
|
| 285 | + | ) -> u32 throws (abi::Error) { |
|
| 286 | + | if not input.links.verified or input.links.pins.len <> input.dependencies.len { |
|
| 287 | + | throw abi::Error::VerifyFailed; |
|
| 288 | + | } |
|
| 289 | + | if input.links.imports.len > output.len { |
|
| 290 | + | throw abi::Error::Exhausted; |
|
| 291 | + | } |
|
| 292 | + | assert dependencies.count == input.links.pins.len; |
|
| 293 | + | let mut count: u32 = 0; |
|
| 294 | + | for imported in input.links.imports { |
|
| 295 | + | if imported.dependency >= input.links.pins.len { |
|
| 296 | + | throw abi::Error::VerifyFailed; |
|
| 248 | 297 | } |
|
| 298 | + | let package = try lookup(view, dependencies.objects[imported.dependency]); |
|
| 299 | + | let target = shared::lookup(package.exports, imported.exported) |
|
| 300 | + | else throw abi::Error::VerifyFailed; |
|
| 301 | + | match imported.kind { |
|
| 302 | + | case binary::ImportKind::Function(_) => { |
|
| 303 | + | let case shared::Target::Function(_) = target else throw abi::Error::VerifyFailed; |
|
| 304 | + | } |
|
| 305 | + | case binary::ImportKind::Data { .. } => { |
|
| 306 | + | let case shared::Target::Data(_) = target else throw abi::Error::VerifyFailed; |
|
| 307 | + | } |
|
| 308 | + | } |
|
| 309 | + | set output[count] = shared::Symbol { name: imported.name, target, startable: false }; |
|
| 310 | + | set count += 1; |
|
| 249 | 311 | } |
|
| 250 | 312 | return count; |
|
| 251 | 313 | } |
|
| 252 | 314 | ||
| 253 | 315 | /// Register a persistent boot catalog in dependency order through normal admission. |
|
| 254 | 316 | export fn boot(store: &mut Store, catalog: &[catalog::Entry]) throws (abi::Error) { |
|
| 255 | 317 | for entry in catalog { |
|
| 256 | - | let existing = try identify(&capture(&*store), &entry.package.name[..], &entry.source[..]); |
|
| 318 | + | let image = identity(entry.source, &entry.package); |
|
| 319 | + | let view = capture(&*store); |
|
| 320 | + | let existing = findImage(&view, image, entry.package.contract.verification); |
|
| 257 | 321 | if existing <> nil { |
|
| 258 | 322 | continue; |
|
| 259 | 323 | } |
|
| 324 | + | let dependencies = try resolve( |
|
| 325 | + | &view, |
|
| 326 | + | entry.package.dependencies, |
|
| 327 | + | entry.package.contract.pins, |
|
| 328 | + | entry.package.contract.verification |
|
| 329 | + | ); |
|
| 260 | 330 | let reservation = try reserve(store); |
|
| 261 | - | let object = try publish(store, reservation, entry.source, entry.package); |
|
| 331 | + | let mut package = entry.package; |
|
| 332 | + | set package.contract.image = image; |
|
| 333 | + | let admission = try admit( |
|
| 334 | + | &capture(&*store), slots::reference(&reservation), entry.source, package, &dependencies |
|
| 335 | + | ) catch err { |
|
| 336 | + | cancel(store, reservation); |
|
| 337 | + | throw err; |
|
| 338 | + | }; |
|
| 339 | + | let object = commit(store, reservation, admission); |
|
| 262 | 340 | } |
|
| 263 | 341 | } |
|
| 264 | 342 | ||
| 265 | 343 | /// Install an Image handle for a resident package without duplicating its code. |
|
| 266 | 344 | export fn install(store: &Store, table: &mut capability::Table, object: abi::Ref) -> abi::Handle |
kernel/kernel/tests.rad
+1 -0
| 1 | 1 | //! Kernel unit tests. Machine execution tests have separate entry points. |
|
| 2 | 2 | ||
| 3 | + | export mod support; |
|
| 3 | 4 | export mod range; |
|
| 4 | 5 | export mod abi; |
|
| 5 | 6 | export mod slots; |
|
| 6 | 7 | export mod fdt; |
|
| 7 | 8 | export mod platform; |
kernel/kernel/tests/domains.rad
+26 -15
| 1 | 1 | //! Pending domain creation, initial context state, and creation authority. |
|
| 2 | 2 | ||
| 3 | - | use std::testing; |
|
| 4 | 3 | use std::arch::rv64::shared; |
|
| 5 | - | use kernel::state; |
|
| 4 | + | use std::testing; |
|
| 5 | + | use super::support; |
|
| 6 | 6 | use kernel::abi; |
|
| 7 | - | use kernel::slots; |
|
| 8 | 7 | use kernel::backing; |
|
| 8 | + | use kernel::budgets; |
|
| 9 | 9 | use kernel::capability; |
|
| 10 | - | use kernel::registry; |
|
| 11 | 10 | use kernel::domains; |
|
| 11 | + | use kernel::events; |
|
| 12 | + | use kernel::frames; |
|
| 13 | + | use kernel::instances; |
|
| 14 | + | use kernel::interrupts; |
|
| 12 | 15 | use kernel::lifecycle; |
|
| 16 | + | use kernel::limits; |
|
| 13 | 17 | use kernel::loader; |
|
| 14 | - | use kernel::instances; |
|
| 18 | + | use kernel::pages; |
|
| 15 | 19 | use kernel::physical; |
|
| 16 | - | use kernel::limits; |
|
| 17 | - | use kernel::frames; |
|
| 18 | - | use kernel::budgets; |
|
| 19 | - | use kernel::interrupts; |
|
| 20 | + | use kernel::registry; |
|
| 21 | + | use kernel::slots; |
|
| 22 | + | use kernel::state; |
|
| 20 | 23 | use kernel::transactions; |
|
| 21 | - | use kernel::pages; |
|
| 22 | - | use kernel::events; |
|
| 23 | 24 | ||
| 24 | 25 | /// Exclusive resources released by the domain reaper. |
|
| 25 | 26 | unsafe static WINDOWS: budgets::Store = undefined; |
|
| 26 | 27 | /// Interrupt ownership released after hardware masking. |
|
| 27 | 28 | unsafe static IRQS: interrupts::Store = undefined; |
| 69 | 70 | let authority = try! capability::install(&mut TABLE, capability::Entry { |
|
| 70 | 71 | kind: abi::Kind::Domain, object: owner, rights: abi::Rights(rights) |
|
| 71 | 72 | }); |
|
| 72 | 73 | let imageSlot = try! registry::reserve(&mut PACKAGES); |
|
| 73 | 74 | let image = slots::reference(&imageSlot); |
|
| 74 | - | let object = try! registry::publish( |
|
| 75 | + | let object = try! support::publish( |
|
| 75 | 76 | &mut PACKAGES, |
|
| 76 | 77 | imageSlot, |
|
| 77 | 78 | "program", |
|
| 78 | 79 | shared::Package { |
|
| 79 | 80 | name: "program", |
|
| 80 | 81 | dependencies: &[], |
|
| 81 | 82 | slot: image.index, |
|
| 82 | 83 | codeAddress: 0x80000000, |
|
| 83 | - | code: &[0x00008067 as u32, 0x00008067], |
|
| 84 | + | code: &[0x00008067 as u32, 0x00008067, 0x00008067], |
|
| 84 | 85 | exports: &[ |
|
| 85 | 86 | shared::Symbol { |
|
| 86 | - | name: "program::work", target: shared::Target::Function(0x80000004) |
|
| 87 | + | name: "program::work", |
|
| 88 | + | target: shared::Target::Function(0x80000004), |
|
| 89 | + | startable: true, |
|
| 90 | + | }, |
|
| 91 | + | shared::Symbol { |
|
| 92 | + | name: "program::borrow", |
|
| 93 | + | target: shared::Target::Function(0x80000008), |
|
| 94 | + | startable: false, |
|
| 87 | 95 | }, |
|
| 88 | 96 | shared::Symbol { |
|
| 89 | - | name: "program::entry", target: shared::Target::Function(0x80000000) |
|
| 97 | + | name: "program::entry", |
|
| 98 | + | target: shared::Target::Function(0x80000000), |
|
| 99 | + | startable: true, |
|
| 90 | 100 | }, |
|
| 91 | 101 | ], |
|
| 92 | 102 | entry: 0x80000000, |
|
| 93 | 103 | template: &[7], |
|
| 94 | 104 | memory: 8, |
|
| 95 | 105 | alignment: 8, |
|
| 96 | 106 | relocations: &[], |
|
| 107 | + | contract: shared::UNVERIFIED, |
|
| 97 | 108 | } |
|
| 98 | 109 | ); |
|
| 99 | 110 | set IMAGE = try! registry::install(&PACKAGES, &mut TABLE, object); |
|
| 100 | 111 | } |
|
| 101 | 112 |
kernel/kernel/tests/instances.rad
+6 -4
| 1 | 1 | //! Private package-state graphs over shared resident code. |
|
| 2 | 2 | ||
| 3 | - | use std::testing; |
|
| 4 | 3 | use std::arch::rv64::shared; |
|
| 4 | + | use std::testing; |
|
| 5 | + | use super::support; |
|
| 5 | 6 | use kernel::abi; |
|
| 6 | 7 | use kernel::frames; |
|
| 7 | - | use kernel::slots; |
|
| 8 | - | use kernel::registry; |
|
| 9 | 8 | use kernel::instances; |
|
| 10 | 9 | use kernel::physical; |
|
| 10 | + | use kernel::registry; |
|
| 11 | + | use kernel::slots; |
|
| 11 | 12 | ||
| 12 | 13 | /// Mapped storage for independent private graphs. |
|
| 13 | 14 | static RAM: [u64; 131072] = [0; 131072]; |
|
| 14 | 15 | /// Physical allocation metadata. |
|
| 15 | 16 | unsafe static POOL: frames::Pool = undefined; |
| 72 | 73 | size: u32, |
|
| 73 | 74 | alignment: u32 |
|
| 74 | 75 | ) -> abi::Ref { |
|
| 75 | 76 | let pending = try! registry::reserve(&mut PACKAGES); |
|
| 76 | 77 | let reference = slots::reference(&pending); |
|
| 77 | - | return try! registry::publish(&mut PACKAGES, pending, name, shared::Package { |
|
| 78 | + | return try! support::publish(&mut PACKAGES, pending, name, shared::Package { |
|
| 78 | 79 | name, |
|
| 79 | 80 | dependencies, |
|
| 80 | 81 | slot: reference.index, |
|
| 81 | 82 | codeAddress: 0x80000000, |
|
| 82 | 83 | code: &[0x00008067 as u32], |
| 84 | 85 | entry: 0x80000000, |
|
| 85 | 86 | template: &[7, 0, 0, 0, 0, 0, 0, 0], |
|
| 86 | 87 | memory: size, |
|
| 87 | 88 | alignment, |
|
| 88 | 89 | relocations, |
|
| 90 | + | contract: shared::UNVERIFIED, |
|
| 89 | 91 | }); |
|
| 90 | 92 | } |
|
| 91 | 93 | ||
| 92 | 94 | /// Map an instance's private word through the original test allocation. |
|
| 93 | 95 | unsafe fn word(address: u64) -> *unsafe mut u64 { |
kernel/kernel/tests/loader.rad
+390 -24
| 1 | 1 | //! Verified binary loading, resident snapshots, and resource rollback. |
|
| 2 | 2 | ||
| 3 | - | use std::testing; |
|
| 4 | 3 | use std::arch::rv64::shared; |
|
| 5 | - | use std::mem::heap; |
|
| 6 | 4 | use std::lang::il; |
|
| 7 | 5 | use std::lang::il::binary; |
|
| 6 | + | use std::lang::il::binary::linkage; |
|
| 8 | 7 | use std::lang::il::binary::program; |
|
| 8 | + | use std::mem; |
|
| 9 | + | use std::mem::heap; |
|
| 10 | + | use std::testing; |
|
| 11 | + | use super::support; |
|
| 9 | 12 | use kernel::abi; |
|
| 10 | - | use kernel::limits; |
|
| 11 | - | use kernel::slots; |
|
| 12 | - | use kernel::pages; |
|
| 13 | - | use kernel::frames; |
|
| 14 | 13 | use kernel::backing; |
|
| 15 | 14 | use kernel::capability; |
|
| 16 | - | use kernel::registry; |
|
| 17 | - | use kernel::loader; |
|
| 18 | 15 | use kernel::domains; |
|
| 16 | + | use kernel::frames; |
|
| 17 | + | use kernel::instances; |
|
| 19 | 18 | use kernel::lifecycle; |
|
| 19 | + | use kernel::limits; |
|
| 20 | + | use kernel::loader; |
|
| 21 | + | use kernel::pages; |
|
| 22 | + | use kernel::physical; |
|
| 23 | + | use kernel::registry; |
|
| 24 | + | use kernel::slots; |
|
| 20 | 25 | ||
| 21 | 26 | /// Caller metadata used for interrupted loader calls. |
|
| 22 | 27 | unsafe static DOMAINS: domains::Store = undefined; |
|
| 23 | 28 | /// Caller-owned private reservations. |
|
| 24 | 29 | unsafe static CALLS: lifecycle::Calls = undefined; |
|
| 25 | 30 | /// Capability storage retained until the stopped caller is reclaimed. |
|
| 26 | 31 | unsafe static MEMORY: domains::Memory = undefined; |
|
| 27 | 32 | ||
| 28 | 33 | /// Mapped frames for temporary compiler storage and persistent native output. |
|
| 29 | - | static RAM: [u64; 18874368] = [0; 18874368]; |
|
| 34 | + | static RAM: [u64; 19398656] = [0; 19398656]; |
|
| 30 | 35 | /// Physical frame and page metadata. |
|
| 31 | 36 | unsafe static PAGES: pages::Store = undefined; |
|
| 32 | 37 | /// Resident package registry. |
|
| 33 | 38 | unsafe static PACKAGES: registry::Store = undefined; |
|
| 34 | 39 | /// Loader ownership and resident-frame accounting. |
| 49 | 54 | loader::initialize(&mut LOADER); |
|
| 50 | 55 | let pointer = &RAM[0]; |
|
| 51 | 56 | let base = pointer as u64; |
|
| 52 | 57 | let aligned = (base + 4095) & ~4095; |
|
| 53 | 58 | set OFFSET = (aligned - base) as u32; |
|
| 54 | - | set PAGES.backings.pool.count = (@sizeOf([u64; 18874368]) - OFFSET) / 4096; |
|
| 59 | + | set PAGES.backings.pool.count = (@sizeOf([u64; 19398656]) - OFFSET) / 4096; |
|
| 55 | 60 | for i in 0 .. PAGES.backings.pool.count { |
|
| 56 | 61 | set PAGES.backings.pool.addresses[i] = aligned + i as u64 * 4096; |
|
| 57 | 62 | set PAGES.backings.pool.free[i / 64] |= 1 << (i % 64) as u64; |
|
| 58 | 63 | } |
|
| 59 | 64 | let owner = abi::Ref { index: 0, generation: 1 }; |
| 112 | 117 | let mut name: *[u8] = "loaded::main"; |
|
| 113 | 118 | if badExport { |
|
| 114 | 119 | set name = "missing"; |
|
| 115 | 120 | } |
|
| 116 | 121 | let mut exports: *mut [binary::Export] = &mut []; |
|
| 117 | - | try! exports.append(binary::Export { name, kind: binary::ExportKind::Function }, arena); |
|
| 122 | + | try! exports.append( |
|
| 123 | + | binary::Export { |
|
| 124 | + | name, |
|
| 125 | + | kind: binary::ExportKind::Function, |
|
| 126 | + | startable: false, |
|
| 127 | + | }, |
|
| 128 | + | arena |
|
| 129 | + | ); |
|
| 118 | 130 | let package = binary::Package { |
|
| 119 | 131 | symbols: &["loaded", "loaded::main", "entry", "missing"], |
|
| 120 | 132 | name: "loaded", |
|
| 121 | 133 | dependencies, |
|
| 122 | 134 | exports, |
|
| 123 | 135 | entry: "loaded::main", |
|
| 124 | 136 | program: il::Program { data: &[], fns: list, layouts: &[], signatures: &[], traits: &[] }, |
|
| 137 | + | links: binary::EMPTY_LINKS, |
|
| 125 | 138 | }; |
|
| 126 | - | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 139 | + | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 19398656])); |
|
| 127 | 140 | let mut output = &mut bytes[OFFSET .. OFFSET + 4096] as *mut [u8]; |
|
| 128 | 141 | let length = try! program::encode(&mut output, &package); |
|
| 129 | 142 | return loader::Request { |
|
| 130 | 143 | authority: abi::Handle(0), source: SOURCE, offset: 0, length: length as u64 |
|
| 131 | 144 | }; |
| 169 | 182 | ); |
|
| 170 | 183 | set tokens = lifecycle::withOutput(tokens, output); |
|
| 171 | 184 | } |
|
| 172 | 185 | if stage == 2 { |
|
| 173 | 186 | let retained = lifecycle::load(&CALLS, &lease); |
|
| 174 | - | let decoded = try! lifecycle::decodeLoad(&retained, &tokens, &lease); |
|
| 187 | + | let case loader::Prepared::New(decoded) = try! lifecycle::decodeLoad( |
|
| 188 | + | ®istry::capture(&PACKAGES), &retained, &tokens, &lease |
|
| 189 | + | ) else panic "test: expected a new image"; |
|
| 175 | 190 | let compiled = try! lifecycle::compileLoad( |
|
| 176 | 191 | ®istry::capture(&PACKAGES), &retained, &tokens, &lease, &decoded |
|
| 177 | 192 | ); |
|
| 178 | 193 | assert compiled.code > 0; |
|
| 179 | 194 | } |
| 210 | 225 | let retryOutput = try! lifecycle::reserveOutput( |
|
| 211 | 226 | &mut CALLS, &active, &mut PAGES, &mut PACKAGES |
|
| 212 | 227 | ); |
|
| 213 | 228 | let joined = lifecycle::withOutput(retryAccess, retryOutput); |
|
| 214 | 229 | let retained = lifecycle::load(&CALLS, &active); |
|
| 215 | - | let decoded = try! lifecycle::decodeLoad(&retained, &joined, &active); |
|
| 230 | + | let case loader::Prepared::New(decoded) = try! lifecycle::decodeLoad( |
|
| 231 | + | ®istry::capture(&PACKAGES), &retained, &joined, &active |
|
| 232 | + | ) else panic "test: expected a new image"; |
|
| 216 | 233 | let compiled = try! lifecycle::compileLoad( |
|
| 217 | 234 | ®istry::capture(&PACKAGES), &retained, &joined, &active, &decoded |
|
| 218 | 235 | ); |
|
| 219 | 236 | let handle = try! lifecycle::finishLoad( |
|
| 220 | 237 | &mut CALLS, |
| 233 | 250 | &mut LOADER, &mut PAGES, &mut MEMORY.table, request |
|
| 234 | 251 | ); |
|
| 235 | 252 | match lifecycle::holdLoad(&mut CALLS, &mut DOMAINS, owner, duplicate) { |
|
| 236 | 253 | case lifecycle::LoadHeld::Held(reuse, reuseAccess) => { |
|
| 237 | 254 | let source = lifecycle::load(&CALLS, &reuse); |
|
| 238 | - | let program = try! lifecycle::decodeLoad(&source, &reuseAccess, &reuse); |
|
| 239 | - | assert try! lifecycle::identifyLoad( |
|
| 240 | - | ®istry::capture(&PACKAGES), &source, &reuseAccess, &reuse, &program |
|
| 241 | - | ) == object; |
|
| 255 | + | let program = try! lifecycle::decodeLoad( |
|
| 256 | + | ®istry::capture(&PACKAGES), &source, &reuseAccess, &reuse |
|
| 257 | + | ); |
|
| 258 | + | let case loader::Prepared::Resident(found) = program |
|
| 259 | + | else panic "test: expected resident reuse"; |
|
| 260 | + | assert found == object; |
|
| 242 | 261 | let same = try! lifecycle::finishLoad( |
|
| 243 | 262 | &mut CALLS, |
|
| 244 | 263 | &mut DOMAINS, |
|
| 245 | 264 | &mut PAGES, |
|
| 246 | 265 | &mut LOADER, |
| 266 | 285 | let request = input(43, false); |
|
| 267 | 286 | let before = free(); |
|
| 268 | 287 | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 269 | 288 | assert LOADER.owner <> nil and TABLE.slots[2].state == slots::State::Reserved; |
|
| 270 | 289 | let other = try! pages::allocate(&mut PAGES, &mut TABLE, abi::Handle(0), 1); |
|
| 271 | - | let decoded = try! loader::decode(&pending); |
|
| 290 | + | let case loader::Prepared::New(decoded) = try! loader::decode( |
|
| 291 | + | ®istry::capture(&PACKAGES), &pending |
|
| 292 | + | ) else panic "test: expected a new image"; |
|
| 272 | 293 | assert decoded.program.fns.len == 1 and decoded.name.len == 6; |
|
| 273 | 294 | let replacement = input(99, false); |
|
| 274 | 295 | assert decoded.program.fns[0].blocks[0].instrs[0] == il::Instr::Ret { val: il::Val::Imm(43) }; |
|
| 275 | 296 | let mut busy = false; |
|
| 276 | 297 | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request) catch error { |
| 313 | 334 | @test unsafe fn stagedOutput() throws (testing::TestError) { |
|
| 314 | 335 | initialize(); |
|
| 315 | 336 | let request = input(43, false); |
|
| 316 | 337 | let before = free(); |
|
| 317 | 338 | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 318 | - | let decoded = try! loader::decode(&pending); |
|
| 339 | + | let case loader::Prepared::New(decoded) = try! loader::decode( |
|
| 340 | + | ®istry::capture(&PACKAGES), &pending |
|
| 341 | + | ) else panic "test: expected a new image"; |
|
| 319 | 342 | let output = try! loader::reserveOutput(&mut PAGES.backings.pool, &mut PACKAGES); |
|
| 320 | 343 | assert PACKAGES.slots[0].state == slots::State::Reserved and not LOADER.live[0]; |
|
| 321 | 344 | let other = try! pages::allocate(&mut PAGES, &mut TABLE, abi::Handle(0), 1); |
|
| 322 | 345 | let used = try! loader::compile(®istry::capture(&PACKAGES), &pending, &decoded, &output); |
|
| 323 | 346 | assert used.code > 0 and used.metadata > 0; |
| 359 | 382 | let package = try! registry::get(&PACKAGES, object); |
|
| 360 | 383 | try testing::expect(package.code.len > 0 and package.entry <> nil); |
|
| 361 | 384 | let retained = LOADER.resident[object.index]; |
|
| 362 | 385 | try testing::expect(free() + retained.code.count + retained.metadata.count == before); |
|
| 363 | 386 | try testing::expect(LOADER.owner == nil); |
|
| 364 | - | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 387 | + | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 19398656])); |
|
| 365 | 388 | let magic = bytes[OFFSET]; |
|
| 366 | 389 | set bytes[OFFSET] = 0; |
|
| 367 | 390 | try testing::expect(PACKAGES.entries[object.index].source[0] == magic); |
|
| 368 | 391 | set bytes[OFFSET] = magic; |
|
| 369 | 392 | let duplicate = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
| 490 | 513 | entry: nil, |
|
| 491 | 514 | template: &[], |
|
| 492 | 515 | memory: 0, |
|
| 493 | 516 | alignment: 8, |
|
| 494 | 517 | relocations: &[], |
|
| 518 | + | contract: shared::UNVERIFIED, |
|
| 495 | 519 | }; |
|
| 496 | - | let resident = try! registry::publish(&mut PACKAGES, pending, "dependency", dependency); |
|
| 520 | + | let resident = try! support::publish(&mut PACKAGES, pending, "dependency", dependency); |
|
| 497 | 521 | let before = free(); |
|
| 498 | 522 | try rejectDependencies(&["missing"], before, resident); |
|
| 499 | 523 | try rejectDependencies(&["missing", "missing"], before, resident); |
|
| 500 | 524 | let valid = input(43, false); |
|
| 501 | 525 | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, valid); |
| 506 | 530 | /// Generated code leaves no stale bytes behind it in its final executable frame. |
|
| 507 | 531 | @test unsafe fn codeTailCleared() throws (testing::TestError) { |
|
| 508 | 532 | initialize(); |
|
| 509 | 533 | let request = input(43, false); |
|
| 510 | 534 | let pending = try! loader::reserve(&mut LOADER, &mut PAGES, &mut TABLE, request); |
|
| 511 | - | let decoded = try! loader::decode(&pending); |
|
| 535 | + | let case loader::Prepared::New(decoded) = try! loader::decode( |
|
| 536 | + | ®istry::capture(&PACKAGES), &pending |
|
| 537 | + | ) else panic "test: expected a new image"; |
|
| 512 | 538 | let output = try! loader::reserveOutput(&mut PAGES.backings.pool, &mut PACKAGES); |
|
| 513 | 539 | match &output { |
|
| 514 | 540 | case loader::OutputReservation::Held(target, access) => { |
|
| 515 | - | let ram = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 18874368])); |
|
| 541 | + | let ram = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 19398656])); |
|
| 516 | 542 | let start = (target.codeBase - &RAM[0] as u64) as u32; |
|
| 517 | 543 | let stale = &mut ram[start .. start + 4096]; |
|
| 518 | 544 | for i in 0 .. stale.len { |
|
| 519 | 545 | set stale[i] = 0xa5; |
|
| 520 | 546 | } |
| 529 | 555 | } |
|
| 530 | 556 | } |
|
| 531 | 557 | loader::cancelOutput(&mut PAGES.backings.pool, &mut PACKAGES, output); |
|
| 532 | 558 | loader::cancel(&mut LOADER, &mut PAGES, &mut TABLE, pending); |
|
| 533 | 559 | } |
|
| 560 | + | ||
| 561 | + | /// Contract changes exercised by runtime admission tests. |
|
| 562 | + | union LinkCase: Copy { |
|
| 563 | + | Valid, |
|
| 564 | + | Image, |
|
| 565 | + | Signature, |
|
| 566 | + | Rights, |
|
| 567 | + | Private, |
|
| 568 | + | Alias, |
|
| 569 | + | Heap, |
|
| 570 | + | Kind, |
|
| 571 | + | Unbound, |
|
| 572 | + | Version, |
|
| 573 | + | } |
|
| 574 | + | ||
| 575 | + | /// Encode a verified library or a client with explicit local function and data aliases. |
|
| 576 | + | unsafe fn linkedInput(provider: ?shared::Package, change: LinkCase) -> loader::Request { |
|
| 577 | + | let arena = heap::over(&mut CODE[..]); |
|
| 578 | + | let mut layouts: *mut [il::Layout] = &mut []; |
|
| 579 | + | try! layouts.append(il::Layout { size: 4, alignment: 4, slots: &[], regions: 0 }, arena); |
|
| 580 | + | let mut data: *mut [il::Data] = &mut []; |
|
| 581 | + | let mut instructions: *mut [il::Instr] = &mut []; |
|
| 582 | + | let mut signatures: *mut [il::Signature] = &mut []; |
|
| 583 | + | let mut exports: *mut [binary::Export] = &mut []; |
|
| 584 | + | let mut pins: *mut [binary::Id] = &mut []; |
|
| 585 | + | let mut imports: *mut [binary::Import] = &mut []; |
|
| 586 | + | let mut dependencies: *[*[u8]] = &[]; |
|
| 587 | + | let mut entry: ?*[u8] = nil; |
|
| 588 | + | let mut name: *[u8] = "unit::get"; |
|
| 589 | + | let mut leaf = true; |
|
| 590 | + | if let supplied = provider { |
|
| 591 | + | set name = "unit::main"; |
|
| 592 | + | set entry = name; |
|
| 593 | + | set leaf = false; |
|
| 594 | + | set dependencies = &["unit"]; |
|
| 595 | + | let image = supplied.contract.image else panic "linkedInput: provider identity"; |
|
| 596 | + | let mut pin = image; |
|
| 597 | + | if change == LinkCase::Image { |
|
| 598 | + | set pin.bytes[0] ^= 1; |
|
| 599 | + | } |
|
| 600 | + | try! pins.append(pin, arena); |
|
| 601 | + | let ret = il::ValType::Data( |
|
| 602 | + | il::Type::W64 if change == LinkCase::Signature else il::Type::W32 |
|
| 603 | + | ); |
|
| 604 | + | try! signatures.append(il::Signature { params: &[], ret, borrows: &[] }, arena); |
|
| 605 | + | try! imports.append( |
|
| 606 | + | binary::Import { |
|
| 607 | + | name: "aliasFn", |
|
| 608 | + | dependency: 0, |
|
| 609 | + | exported: "unit::counter" if change == LinkCase::Kind else "unit::get", |
|
| 610 | + | kind: binary::ImportKind::Function(0), |
|
| 611 | + | }, |
|
| 612 | + | arena |
|
| 613 | + | ); |
|
| 614 | + | try! imports.append( |
|
| 615 | + | binary::Import { |
|
| 616 | + | name: "aliasFn" if change == LinkCase::Alias else "aliasData", |
|
| 617 | + | dependency: 0, |
|
| 618 | + | exported: "unit::private" if change == LinkCase::Private else "unit::counter", |
|
| 619 | + | kind: binary::ImportKind::Data { |
|
| 620 | + | layout: 0, |
|
| 621 | + | readOnly: change == LinkCase::Rights, |
|
| 622 | + | heap: il::HeapType::Typed if change == LinkCase::Heap else nil, |
|
| 623 | + | }, |
|
| 624 | + | }, |
|
| 625 | + | arena |
|
| 626 | + | ); |
|
| 627 | + | let mut slots: *mut [il::Slot] = &mut []; |
|
| 628 | + | try! slots.append( |
|
| 629 | + | il::Slot { |
|
| 630 | + | offset: 0, |
|
| 631 | + | kind: il::SlotKind::Ptr(il::PtrType { |
|
| 632 | + | layout: 0, own: true, mutable: change <> LinkCase::Rights, nullable: true |
|
| 633 | + | }), |
|
| 634 | + | regions: &[], |
|
| 635 | + | }, |
|
| 636 | + | arena |
|
| 637 | + | ); |
|
| 638 | + | try! layouts.append( |
|
| 639 | + | il::Layout { size: 8, alignment: 8, slots: &slots[..], regions: 0 }, arena |
|
| 640 | + | ); |
|
| 641 | + | try! data.append( |
|
| 642 | + | il::Data { |
|
| 643 | + | name: "unit::pointer", |
|
| 644 | + | size: 8, |
|
| 645 | + | alignment: 8, |
|
| 646 | + | layout: 1, |
|
| 647 | + | readOnly: true, |
|
| 648 | + | heap: nil, |
|
| 649 | + | isZeroInit: false, |
|
| 650 | + | values: &[il::DataValue { item: il::DataItem::Sym("aliasData"), count: 1 }], |
|
| 651 | + | }, |
|
| 652 | + | arena |
|
| 653 | + | ); |
|
| 654 | + | try! instructions.append( |
|
| 655 | + | il::Instr::Call { |
|
| 656 | + | retTy: ret, |
|
| 657 | + | dst: il::Reg { n: 0 }, |
|
| 658 | + | func: il::Val::FnAddr("aliasFn"), |
|
| 659 | + | args: il::ArgRange { start: 0, len: 0 }, |
|
| 660 | + | }, |
|
| 661 | + | arena |
|
| 662 | + | ); |
|
| 663 | + | try! instructions.append( |
|
| 664 | + | il::Instr::Copy { dst: il::Reg { n: 1 }, val: il::Val::DataSym("aliasData") }, arena |
|
| 665 | + | ); |
|
| 666 | + | try! instructions.append( |
|
| 667 | + | il::Instr::Load { |
|
| 668 | + | typ: il::Type::W32, dst: il::Reg { n: 2 }, src: il::Reg { n: 1 }, offset: 0 |
|
| 669 | + | }, |
|
| 670 | + | arena |
|
| 671 | + | ); |
|
| 672 | + | try! instructions.append( |
|
| 673 | + | il::Instr::BinOp { |
|
| 674 | + | op: il::BinOp::Add, |
|
| 675 | + | typ: il::Type::W32, |
|
| 676 | + | dst: il::Reg { n: 3 }, |
|
| 677 | + | a: il::Val::Reg(il::Reg { n: 0 }), |
|
| 678 | + | b: il::Val::Reg(il::Reg { n: 2 }), |
|
| 679 | + | }, |
|
| 680 | + | arena |
|
| 681 | + | ); |
|
| 682 | + | try! instructions.append(il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 3 }) }, arena); |
|
| 683 | + | } else { |
|
| 684 | + | try! data.append( |
|
| 685 | + | il::Data { |
|
| 686 | + | name: "unit::counter", |
|
| 687 | + | size: 4, |
|
| 688 | + | alignment: 4, |
|
| 689 | + | layout: 0, |
|
| 690 | + | readOnly: false, |
|
| 691 | + | heap: nil, |
|
| 692 | + | isZeroInit: false, |
|
| 693 | + | values: &[ |
|
| 694 | + | il::DataValue { |
|
| 695 | + | item: il::DataItem::Val { typ: il::Type::W32, val: 7 }, count: 1 |
|
| 696 | + | }, |
|
| 697 | + | ], |
|
| 698 | + | }, |
|
| 699 | + | arena |
|
| 700 | + | ); |
|
| 701 | + | try! exports.append( |
|
| 702 | + | binary::Export { |
|
| 703 | + | name: "unit::counter", kind: binary::ExportKind::Data, startable: false |
|
| 704 | + | }, |
|
| 705 | + | arena |
|
| 706 | + | ); |
|
| 707 | + | try! data.append( |
|
| 708 | + | il::Data { |
|
| 709 | + | name: "unit::private", |
|
| 710 | + | size: 4, |
|
| 711 | + | alignment: 4, |
|
| 712 | + | layout: 0, |
|
| 713 | + | readOnly: false, |
|
| 714 | + | heap: nil, |
|
| 715 | + | isZeroInit: true, |
|
| 716 | + | values: &[], |
|
| 717 | + | }, |
|
| 718 | + | arena |
|
| 719 | + | ); |
|
| 720 | + | try! instructions.append( |
|
| 721 | + | il::Instr::Ret { |
|
| 722 | + | val: il::Val::Imm(42 if change == LinkCase::Version else 41) |
|
| 723 | + | }, |
|
| 724 | + | arena |
|
| 725 | + | ); |
|
| 726 | + | } |
|
| 727 | + | try! exports.append( |
|
| 728 | + | binary::Export { name, kind: binary::ExportKind::Function, startable: false }, arena |
|
| 729 | + | ); |
|
| 730 | + | let mut blocks: *mut [il::Block] = &mut []; |
|
| 731 | + | try! blocks.append( |
|
| 732 | + | il::Block { |
|
| 733 | + | label: il::BlockLabel { base: "entry", suffix: nil }, |
|
| 734 | + | params: &[], |
|
| 735 | + | instrs: &instructions[..], |
|
| 736 | + | locs: &[], |
|
| 737 | + | preds: &[], |
|
| 738 | + | loopDepth: 0, |
|
| 739 | + | }, |
|
| 740 | + | arena |
|
| 741 | + | ); |
|
| 742 | + | let mut definitions: *mut [il::Fn] = &mut []; |
|
| 743 | + | try! definitions.append( |
|
| 744 | + | il::Fn { |
|
| 745 | + | name, |
|
| 746 | + | params: &[], |
|
| 747 | + | returnType: il::ValType::Data(il::Type::W32), |
|
| 748 | + | isExtern: false, |
|
| 749 | + | isLeaf: leaf, |
|
| 750 | + | blocks: &blocks[..], |
|
| 751 | + | vals: &[], |
|
| 752 | + | cases: &[], |
|
| 753 | + | borrows: &[], |
|
| 754 | + | }, |
|
| 755 | + | arena |
|
| 756 | + | ); |
|
| 757 | + | let frozen: *[il::Fn] = &definitions[..]; |
|
| 758 | + | let mut functions: *mut [*il::Fn] = &mut []; |
|
| 759 | + | try! functions.append(&frozen[0], arena); |
|
| 760 | + | let mut package = binary::Package { |
|
| 761 | + | symbols: &[ |
|
| 762 | + | "unit", |
|
| 763 | + | "unit::get", |
|
| 764 | + | "unit::counter", |
|
| 765 | + | "unit::main", |
|
| 766 | + | "unit::pointer", |
|
| 767 | + | "aliasFn", |
|
| 768 | + | "aliasData", |
|
| 769 | + | "entry", |
|
| 770 | + | "unit::private", |
|
| 771 | + | ], |
|
| 772 | + | name: "unit", |
|
| 773 | + | dependencies, |
|
| 774 | + | exports: &exports[..], |
|
| 775 | + | entry, |
|
| 776 | + | program: il::Program { |
|
| 777 | + | data: &data[..], |
|
| 778 | + | fns: &functions[..], |
|
| 779 | + | layouts: &layouts[..], |
|
| 780 | + | signatures: &signatures[..], |
|
| 781 | + | traits: &[], |
|
| 782 | + | }, |
|
| 783 | + | links: binary::Links { |
|
| 784 | + | pins: &pins[..], imports: &imports[..], image: nil, verified: false |
|
| 785 | + | }, |
|
| 786 | + | }; |
|
| 787 | + | if change == LinkCase::Unbound { |
|
| 788 | + | set package.links.imports = &[]; |
|
| 789 | + | } |
|
| 790 | + | let bytes = @sliceOf(&mut RAM[0] as *mut u8, @sizeOf([u64; 19398656])); |
|
| 791 | + | let mut output = &mut bytes[OFFSET .. OFFSET + 4096] as *mut [u8]; |
|
| 792 | + | let length = try! program::encode(&mut output, &package); |
|
| 793 | + | return loader::Request { |
|
| 794 | + | authority: abi::Handle(0), source: SOURCE, offset: 0, length: length as u64 |
|
| 795 | + | }; |
|
| 796 | + | } |
|
| 797 | + | ||
| 798 | + | /// A library and client with one display name link by hash and instantiate private state. |
|
| 799 | + | @test unsafe fn linkedPackages() throws (testing::TestError) { |
|
| 800 | + | initialize(); |
|
| 801 | + | let libraryRequest = linkedInput(nil, LinkCase::Valid); |
|
| 802 | + | let libraryHandle = try! loader::load( |
|
| 803 | + | &mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, libraryRequest |
|
| 804 | + | ); |
|
| 805 | + | let libraryRef = (try! capability::get(&TABLE, libraryHandle)).object; |
|
| 806 | + | let library = try! registry::get(&PACKAGES, libraryRef); |
|
| 807 | + | assert library.contract.verification == shared::Verification::Verified; |
|
| 808 | + | let request = linkedInput(library, LinkCase::Valid); |
|
| 809 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 810 | + | let object = (try! capability::get(&TABLE, handle)).object; |
|
| 811 | + | let client = try! registry::get(&PACKAGES, object); |
|
| 812 | + | assert object <> libraryRef and PACKAGES.dependencies[object.index][0] == libraryRef; |
|
| 813 | + | assert client.relocations.len == 1 and client.relocations[0].target.slot == libraryRef.index; |
|
| 814 | + | let first = try! instances::create(&PACKAGES, &mut PAGES.backings.pool, object); |
|
| 815 | + | let second = try! instances::create(&PACKAGES, &mut PAGES.backings.pool, object); |
|
| 816 | + | let mut firstLibrary: u64 = 0; |
|
| 817 | + | let mut secondLibrary: u64 = 0; |
|
| 818 | + | let mut firstClient: u64 = 0; |
|
| 819 | + | match first { |
|
| 820 | + | case instances::Instance::Held(run, table) => { |
|
| 821 | + | set firstLibrary = instances::base(&table, libraryRef.index); |
|
| 822 | + | set firstClient = instances::base(&table, object.index); |
|
| 823 | + | } |
|
| 824 | + | } |
|
| 825 | + | match second { |
|
| 826 | + | case instances::Instance::Held(run, table) => |
|
| 827 | + | set secondLibrary = instances::base(&table, libraryRef.index), |
|
| 828 | + | } |
|
| 829 | + | assert firstLibrary <> secondLibrary; |
|
| 830 | + | let case shared::Target::Data(counter) = shared::lookup(library.exports, "unit::counter") |
|
| 831 | + | else panic "linkedPackages: counter"; |
|
| 832 | + | let a = firstLibrary + counter.offset as u64; |
|
| 833 | + | let b = secondLibrary + counter.offset as u64; |
|
| 834 | + | let mut value: [u8; 4] = [0; 4]; |
|
| 835 | + | try! physical::read(&PAGES.backings.pool, a, &mut value[..]); |
|
| 836 | + | assert mem::readLe(&value[..], 0, 4) == 7; |
|
| 837 | + | try! physical::write32(&PAGES.backings.pool, a, 19); |
|
| 838 | + | try! physical::read(&PAGES.backings.pool, b, &mut value[..]); |
|
| 839 | + | assert mem::readLe(&value[..], 0, 4) == 7; |
|
| 840 | + | let mut pointer: [u64; 1] = [0]; |
|
| 841 | + | try! physical::readWords( |
|
| 842 | + | &PAGES.backings.pool, firstClient + client.relocations[0].offset as u64, &mut pointer[..] |
|
| 843 | + | ); |
|
| 844 | + | assert pointer[0] == a; |
|
| 845 | + | } |
|
| 846 | + | ||
| 847 | + | /// Mismatched image, signature, and data rights fail without publication or leaks. |
|
| 848 | + | @test unsafe fn linkedRejections() throws (testing::TestError) { |
|
| 849 | + | initialize(); |
|
| 850 | + | let request = linkedInput(nil, LinkCase::Valid); |
|
| 851 | + | let handle = try! loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request); |
|
| 852 | + | let object = (try! capability::get(&TABLE, handle)).object; |
|
| 853 | + | let library = try! registry::get(&PACKAGES, object); |
|
| 854 | + | for change in &[ |
|
| 855 | + | LinkCase::Image, LinkCase::Signature, LinkCase::Rights, |
|
| 856 | + | LinkCase::Private, LinkCase::Alias, LinkCase::Heap, LinkCase::Kind, LinkCase::Unbound, |
|
| 857 | + | ] { |
|
| 858 | + | let bad = linkedInput(library, change); |
|
| 859 | + | let before = free(); |
|
| 860 | + | let mut rejected = false; |
|
| 861 | + | try loader::load(&mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, bad) catch error { |
|
| 862 | + | assert error == abi::Error::VerifyFailed; |
|
| 863 | + | set rejected = true; |
|
| 864 | + | }; |
|
| 865 | + | assert rejected and free() == before and LOADER.owner == nil; |
|
| 866 | + | assert PACKAGES.slots[1].state == slots::State::Free; |
|
| 867 | + | } |
|
| 868 | + | } |
|
| 869 | + | ||
| 870 | + | /// Body versions and clients with different exact pins coexist under the same names. |
|
| 871 | + | @test unsafe fn linkedVersions() throws (testing::TestError) { |
|
| 872 | + | initialize(); |
|
| 873 | + | let firstRequest = linkedInput(nil, LinkCase::Valid); |
|
| 874 | + | let firstHandle = try! loader::load( |
|
| 875 | + | &mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, firstRequest |
|
| 876 | + | ); |
|
| 877 | + | let firstRef = (try! capability::get(&TABLE, firstHandle)).object; |
|
| 878 | + | let first = try! registry::get(&PACKAGES, firstRef); |
|
| 879 | + | let secondRequest = linkedInput(nil, LinkCase::Version); |
|
| 880 | + | let secondHandle = try! loader::load( |
|
| 881 | + | &mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, secondRequest |
|
| 882 | + | ); |
|
| 883 | + | let secondRef = (try! capability::get(&TABLE, secondHandle)).object; |
|
| 884 | + | let second = try! registry::get(&PACKAGES, secondRef); |
|
| 885 | + | assert firstRef <> secondRef; |
|
| 886 | + | assert first.contract.image <> second.contract.image; |
|
| 887 | + | let oldRequest = linkedInput(first, LinkCase::Valid); |
|
| 888 | + | let bytes = @sliceOf(&RAM[0] as *u8, @sizeOf([u64; 19398656])); |
|
| 889 | + | let oldImage = linkage::imageId(&bytes[OFFSET .. OFFSET + oldRequest.length as u32]); |
|
| 890 | + | let request = linkedInput(second, LinkCase::Valid); |
|
| 891 | + | let newImage = linkage::imageId(&bytes[OFFSET .. OFFSET + request.length as u32]); |
|
| 892 | + | assert oldImage <> newImage; |
|
| 893 | + | let clientHandle = try! loader::load( |
|
| 894 | + | &mut LOADER, &mut PAGES, &mut PACKAGES, &mut TABLE, request |
|
| 895 | + | ); |
|
| 896 | + | let client = (try! capability::get(&TABLE, clientHandle)).object; |
|
| 897 | + | assert client.index == 2; |
|
| 898 | + | assert PACKAGES.dependencies[client.index][0] == secondRef; |
|
| 899 | + | } |
kernel/kernel/tests/registry.rad
+69 -45
| 1 | 1 | //! Resident package identity, dependency admission, and image capabilities. |
|
| 2 | 2 | ||
| 3 | - | use std::testing; |
|
| 4 | 3 | use std::arch::rv64::shared; |
|
| 5 | 4 | use std::arch::rv64::shared::catalog; |
|
| 5 | + | use std::lang::il::binary::linkage; |
|
| 6 | + | use std::testing; |
|
| 7 | + | use super::support; |
|
| 6 | 8 | use kernel::abi; |
|
| 7 | - | use kernel::slots; |
|
| 8 | 9 | use kernel::capability; |
|
| 9 | 10 | use kernel::registry; |
|
| 11 | + | use kernel::slots; |
|
| 10 | 12 | ||
| 11 | 13 | /// Resident registry metadata workspace. |
|
| 12 | 14 | unsafe static STORE: registry::Store = undefined; |
|
| 13 | 15 | /// Persistent distinct names for complete registry exhaustion. |
|
| 14 | 16 | unsafe static NAMES: [[u8; 4]; 256] = undefined; |
| 25 | 27 | entry: nil, |
|
| 26 | 28 | template: &[], |
|
| 27 | 29 | memory: 0, |
|
| 28 | 30 | alignment: 8, |
|
| 29 | 31 | relocations: &[], |
|
| 32 | + | contract: shared::UNVERIFIED, |
|
| 30 | 33 | }; |
|
| 31 | 34 | } |
|
| 32 | 35 | ||
| 33 | 36 | /// Admit persistent package bytes and metadata through a reserved registry slot. |
|
| 34 | 37 | unsafe fn register(name: *[u8], dependencies: *[*[u8]], bytes: *[u8]) -> abi::Ref |
|
| 35 | 38 | throws (abi::Error) |
|
| 36 | 39 | { |
|
| 37 | 40 | let pending = try registry::reserve(&mut STORE); |
|
| 38 | 41 | let slot = slots::reference(&pending); |
|
| 39 | - | return try registry::publish(&mut STORE, pending, bytes, package( |
|
| 42 | + | return try support::publish(&mut STORE, pending, bytes, package( |
|
| 40 | 43 | name, dependencies, slot.index |
|
| 41 | 44 | )); |
|
| 42 | 45 | } |
|
| 43 | 46 | ||
| 44 | 47 | /// Admission resolves dependencies before its reserved package becomes visible. |
|
| 45 | 48 | @test unsafe fn stagedAdmission() throws (testing::TestError) { |
|
| 46 | 49 | registry::initialize(&mut STORE); |
|
| 47 | 50 | let base = try! register("base", &[], "base bytes"); |
|
| 48 | 51 | let pending = try! registry::reserve(&mut STORE); |
|
| 49 | 52 | let object = slots::reference(&pending); |
|
| 50 | - | let admitted = try! registry::admit(®istry::capture(&STORE), object, "child bytes", package( |
|
| 51 | - | "child", &["base"], object.index |
|
| 52 | - | )); |
|
| 53 | + | let dependencies = try! registry::resolve( |
|
| 54 | + | ®istry::capture(&STORE), &["base"], &[], shared::Verification::Unverified |
|
| 55 | + | ); |
|
| 56 | + | let admitted = try! registry::admit( |
|
| 57 | + | ®istry::capture(&STORE), |
|
| 58 | + | object, |
|
| 59 | + | "child bytes", |
|
| 60 | + | package( |
|
| 61 | + | "child", &["base"], object.index |
|
| 62 | + | ), |
|
| 63 | + | &dependencies |
|
| 64 | + | ); |
|
| 53 | 65 | assert STORE.slots[object.index].state == slots::State::Reserved; |
|
| 54 | 66 | assert registry::find(®istry::capture(&STORE), &"child"[..]) == nil; |
|
| 55 | 67 | assert admitted.dependencies[0] == base; |
|
| 56 | 68 | let published = registry::commit(&mut STORE, pending, admitted); |
|
| 57 | 69 | assert published == object and STORE.counts[object.index] == 1; |
|
| 58 | 70 | assert STORE.dependencies[object.index][0] == base; |
|
| 59 | 71 | let rejected = try! registry::reserve(&mut STORE); |
|
| 60 | 72 | let slot = slots::reference(&rejected); |
|
| 61 | 73 | let mut duplicate = false; |
|
| 62 | - | try registry::admit(®istry::capture(&STORE), slot, "bad bytes", package( |
|
| 63 | - | "bad", |
|
| 64 | - | &[ |
|
| 65 | - | "base", "base" |
|
| 66 | - | ], |
|
| 67 | - | slot.index |
|
| 68 | - | )) catch error { |
|
| 74 | + | try registry::resolve( |
|
| 75 | + | ®istry::capture(&STORE), &["base", "base"], &[], shared::Verification::Unverified |
|
| 76 | + | ) catch error { |
|
| 69 | 77 | assert error == abi::Error::VerifyFailed; |
|
| 70 | 78 | set duplicate = true; |
|
| 71 | 79 | }; |
|
| 72 | 80 | assert duplicate and STORE.slots[slot.index].state == slots::State::Reserved; |
|
| 73 | 81 | registry::cancel(&mut STORE, rejected); |
|
| 74 | 82 | assert STORE.slots[slot.index].state == slots::State::Free; |
|
| 75 | 83 | } |
|
| 76 | 84 | ||
| 77 | - | /// Identical bytes reuse one package; a name cannot identify different content. |
|
| 85 | + | /// Identical content reuses one image; distinct versions can share a display name. |
|
| 78 | 86 | @test unsafe fn identity() throws (testing::TestError) { |
|
| 79 | 87 | registry::initialize(&mut STORE); |
|
| 80 | 88 | let first = try! register("a", &[], "binary a"); |
|
| 81 | 89 | let second = try! register("a", &[], "binary a"); |
|
| 82 | 90 | try testing::expect(first == second and STORE.slots[1].state == slots::State::Free); |
|
| 83 | - | let found = try! registry::identify(®istry::capture(&STORE), &"a"[..], &"binary a"[..]); |
|
| 91 | + | let found = registry::findImage( |
|
| 92 | + | ®istry::capture(&STORE), |
|
| 93 | + | linkage::imageId("binary a"), |
|
| 94 | + | shared::Verification::Unverified |
|
| 95 | + | ); |
|
| 84 | 96 | try testing::expect(found == first); |
|
| 85 | - | let mut conflict = false; |
|
| 86 | - | try register("a", &[], "different") catch err { |
|
| 87 | - | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 88 | - | set conflict = true; |
|
| 89 | - | }; |
|
| 90 | - | try testing::expect(conflict and STORE.slots[1].state == slots::State::Free); |
|
| 97 | + | let other = try! register("a", &[], "different"); |
|
| 98 | + | assert other <> first and STORE.slots[1].state == slots::State::Live; |
|
| 91 | 99 | } |
|
| 92 | 100 | ||
| 93 | 101 | /// Dependencies must already be resident and retain shared identity in a diamond. |
|
| 94 | 102 | @test unsafe fn dependencyGraph() throws (testing::TestError) { |
|
| 95 | 103 | registry::initialize(&mut STORE); |
| 144 | 152 | try! registry::boot(&mut STORE, &catalog[..]); |
|
| 145 | 153 | try testing::expect(STORE.counts[1] == 1 and STORE.dependencies[1][0].index == 0); |
|
| 146 | 154 | try testing::expect(STORE.slots[2].state == slots::State::Free); |
|
| 147 | 155 | } |
|
| 148 | 156 | ||
| 149 | - | /// Export lookup and dependency import collection preserve code and private-data targets. |
|
| 157 | + | /// Export lookup preserves code and private-data targets and rejects private names. |
|
| 150 | 158 | @test unsafe fn exports() throws (testing::TestError) { |
|
| 151 | 159 | registry::initialize(&mut STORE); |
|
| 152 | 160 | let pending = try! registry::reserve(&mut STORE); |
|
| 153 | 161 | let mut native = package("base", &[], 0); |
|
| 154 | 162 | set native.exports = &[ |
|
| 155 | - | shared::Symbol { name: "base::run", target: shared::Target::Function(0x80000000) }, |
|
| 163 | + | shared::Symbol { |
|
| 164 | + | name: "base::run", |
|
| 165 | + | target: shared::Target::Function(0x80000000), |
|
| 166 | + | startable: true, |
|
| 167 | + | }, |
|
| 156 | 168 | shared::Symbol { |
|
| 157 | 169 | name: "base::state", |
|
| 158 | 170 | target: shared::Target::Data(shared::DataRef { slot: 0, offset: 8 }), |
|
| 171 | + | startable: true, |
|
| 159 | 172 | }, |
|
| 160 | 173 | ]; |
|
| 161 | - | let object = try! registry::publish(&mut STORE, pending, "base", native); |
|
| 174 | + | let object = try! support::publish(&mut STORE, pending, "base", native); |
|
| 162 | 175 | let target = try! registry::exported(&STORE, object, &"base::run"[..]); |
|
| 163 | 176 | try testing::expect(target == shared::Target::Function(0x80000000)); |
|
| 164 | - | let mut output: [shared::Symbol; 2] = undefined; |
|
| 165 | - | let count = try! registry::imports(®istry::capture(&STORE), &["base"], &mut output[..]); |
|
| 166 | - | try testing::expect( |
|
| 167 | - | count == 2 |
|
| 168 | - | and output[1].target == shared::Target::Data(shared::DataRef { slot: 0, offset: 8 }) |
|
| 169 | - | ); |
|
| 170 | - | let saved = output[0]; |
|
| 171 | - | let mut failures: u32 = 0; |
|
| 172 | - | try registry::imports(®istry::capture(&STORE), &["base"], &mut output[.. 1]) catch err { |
|
| 173 | - | try testing::expect(err == abi::Error::Exhausted); |
|
| 174 | - | set failures += 1; |
|
| 175 | - | }; |
|
| 177 | + | let state = try! registry::exported(&STORE, object, &"base::state"[..]); |
|
| 178 | + | assert state == shared::Target::Data(shared::DataRef { slot: 0, offset: 8 }); |
|
| 179 | + | let mut rejected = false; |
|
| 176 | 180 | try registry::exported(&STORE, object, &"base::private"[..]) catch err { |
|
| 177 | - | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 178 | - | set failures += 1; |
|
| 179 | - | }; |
|
| 180 | - | try registry::imports(®istry::capture(&STORE), &["absent"], &mut output[..]) catch err { |
|
| 181 | - | try testing::expect(err == abi::Error::VerifyFailed); |
|
| 182 | - | set failures += 1; |
|
| 181 | + | assert err == abi::Error::VerifyFailed; |
|
| 182 | + | set rejected = true; |
|
| 183 | 183 | }; |
|
| 184 | - | try testing::expect(failures == 3 and output[0] == saved); |
|
| 184 | + | assert rejected; |
|
| 185 | 185 | } |
|
| 186 | 186 | ||
| 187 | 187 | /// Full registry exhaustion leaves existing packages available for duplicate lookup. |
|
| 188 | 188 | @test unsafe fn capacity() throws (testing::TestError) { |
|
| 189 | 189 | registry::initialize(&mut STORE); |
| 198 | 198 | try register("extra", &[], "extra") catch err { |
|
| 199 | 199 | try testing::expect(err == abi::Error::Exhausted); |
|
| 200 | 200 | set full = true; |
|
| 201 | 201 | }; |
|
| 202 | 202 | try testing::expect(full); |
|
| 203 | - | let found = try! registry::identify( |
|
| 204 | - | ®istry::capture(&STORE), &NAMES[255][..], &NAMES[255][..] |
|
| 203 | + | let found = registry::findImage( |
|
| 204 | + | ®istry::capture(&STORE), |
|
| 205 | + | linkage::imageId(&NAMES[255][..]), |
|
| 206 | + | shared::Verification::Unverified |
|
| 205 | 207 | ) else panic "capacity: resident package"; |
|
| 206 | 208 | try testing::expect(found.index == 255); |
|
| 207 | 209 | } |
|
| 208 | 210 | ||
| 209 | 211 | /// A captured view exposes only the packages published at capture, and its reads |
| 230 | 232 | assert rejected == 2; |
|
| 231 | 233 | let fresh = registry::capture(&STORE); |
|
| 232 | 234 | assert registry::find(&fresh, &"later"[..]) == later; |
|
| 233 | 235 | registry::cancel(&mut STORE, pending); |
|
| 234 | 236 | } |
|
| 237 | + | ||
| 238 | + | /// A privileged native cache entry cannot satisfy verified user-code admission. |
|
| 239 | + | @test unsafe fn verificationPolicy() throws (testing::TestError) { |
|
| 240 | + | registry::initialize(&mut STORE); |
|
| 241 | + | let privileged = try! register("unit", &[], "same bytes"); |
|
| 242 | + | let image = linkage::imageId("same bytes"); |
|
| 243 | + | assert registry::findImage( |
|
| 244 | + | ®istry::capture(&STORE), image, shared::Verification::Verified |
|
| 245 | + | ) == nil; |
|
| 246 | + | let pending = try! registry::reserve(&mut STORE); |
|
| 247 | + | let object = slots::reference(&pending); |
|
| 248 | + | let mut checked = package("unit", &[], object.index); |
|
| 249 | + | set checked.contract.verification = shared::Verification::Verified; |
|
| 250 | + | let verified = try! support::publish(&mut STORE, pending, "same bytes", checked); |
|
| 251 | + | assert privileged <> verified; |
|
| 252 | + | assert registry::findImage( |
|
| 253 | + | ®istry::capture(&STORE), image, shared::Verification::Unverified |
|
| 254 | + | ) == privileged; |
|
| 255 | + | assert registry::findImage( |
|
| 256 | + | ®istry::capture(&STORE), image, shared::Verification::Verified |
|
| 257 | + | ) == verified; |
|
| 258 | + | } |
kernel/kernel/tests/support.rad
added
+41 -0
| 1 | + | //! Shared setup for kernel unit tests. |
|
| 2 | + | ||
| 3 | + | use std::arch::rv64::shared; |
|
| 4 | + | use std::lang::il::binary::linkage; |
|
| 5 | + | use kernel::abi; |
|
| 6 | + | use kernel::registry; |
|
| 7 | + | use kernel::slots; |
|
| 8 | + | ||
| 9 | + | /// Publish persistent compiler output after successful admission checks. |
|
| 10 | + | /// All descriptor pointers and source bytes must remain immutable and resident. |
|
| 11 | + | /// Failure cancels the reservation; duplicate content returns its existing slot. |
|
| 12 | + | export fn publish( |
|
| 13 | + | store: &mut registry::Store, |
|
| 14 | + | reservation: slots::Reservation, |
|
| 15 | + | source: *[u8], |
|
| 16 | + | compiled: shared::Package |
|
| 17 | + | ) -> abi::Ref throws (abi::Error) { |
|
| 18 | + | let view = registry::capture(&*store); |
|
| 19 | + | let mut package = compiled; |
|
| 20 | + | if package.contract.image == nil { |
|
| 21 | + | set package.contract.image = linkage::imageId(source); |
|
| 22 | + | } |
|
| 23 | + | let image = package.contract.image else panic "publish: image identity"; |
|
| 24 | + | let existing = registry::findImage(&view, image, package.contract.verification); |
|
| 25 | + | if let object = existing { |
|
| 26 | + | registry::cancel(store, reservation); |
|
| 27 | + | return object; |
|
| 28 | + | } |
|
| 29 | + | let dependencies = try registry::resolve( |
|
| 30 | + | &view, package.dependencies, package.contract.pins, package.contract.verification |
|
| 31 | + | ) catch err { |
|
| 32 | + | registry::cancel(store, reservation); |
|
| 33 | + | throw err; |
|
| 34 | + | }; |
|
| 35 | + | let object = slots::reference(&reservation); |
|
| 36 | + | let admission = try registry::admit(&view, object, source, package, &dependencies) catch err { |
|
| 37 | + | registry::cancel(store, reservation); |
|
| 38 | + | throw err; |
|
| 39 | + | }; |
|
| 40 | + | return registry::commit(store, reservation, admission); |
|
| 41 | + | } |
kernel/tools/build.rad
+86 -22
| 1 | 1 | //! Build a native kernel image with shared packages and a persistent boot catalog. |
|
| 2 | 2 | ||
| 3 | - | use std::sys; |
|
| 4 | - | use std::io; |
|
| 5 | - | use std::mem; |
|
| 6 | - | use std::fs; |
|
| 7 | - | use std::mem::heap; |
|
| 8 | - | use std::lang::strings; |
|
| 9 | - | use std::lang::il::binary; |
|
| 10 | - | use std::lang::il::binary::program; |
|
| 11 | - | use std::lang::il::verify; |
|
| 12 | - | use std::lang::gen::data; |
|
| 13 | - | use std::lang::gen::labels; |
|
| 14 | - | use std::collections::dict; |
|
| 15 | 3 | use std::arch::rv64; |
|
| 16 | 4 | use std::arch::rv64::asm; |
|
| 17 | 5 | use std::arch::rv64::emit; |
|
| 18 | 6 | use std::arch::rv64::encode; |
|
| 19 | 7 | use std::arch::rv64::image; |
|
| 20 | 8 | use std::arch::rv64::shared; |
|
| 21 | 9 | use std::arch::rv64::shared::catalog; |
|
| 10 | + | use std::collections::dict; |
|
| 11 | + | use std::fs; |
|
| 12 | + | use std::io; |
|
| 13 | + | use std::lang::gen::data; |
|
| 14 | + | use std::lang::gen::labels; |
|
| 22 | 15 | use std::lang::gen::regalloc::liveness; |
|
| 16 | + | use std::lang::il::binary; |
|
| 17 | + | use std::lang::il::binary::program; |
|
| 18 | + | use std::lang::il::verify; |
|
| 19 | + | use std::lang::il::verify::linking; |
|
| 20 | + | use std::lang::strings; |
|
| 21 | + | use std::mem; |
|
| 22 | + | use std::mem::heap; |
|
| 23 | + | use std::sys; |
|
| 23 | 24 | ||
| 24 | 25 | /// Byte alignment of boot package code and data segments. |
|
| 25 | 26 | constant CODE_ALIGNMENT: u32 = 4096; |
|
| 26 | 27 | /// Bytes reserved for the boot entry trampoline. |
|
| 27 | 28 | constant TRAMPOLINE_BYTES: u32 = 4096; |
| 173 | 174 | for word, i in &e.code[..] { |
|
| 174 | 175 | set NATIVE[i] = word; |
|
| 175 | 176 | } |
|
| 176 | 177 | } |
|
| 177 | 178 | ||
| 179 | + | /// Select one exact verified provider from earlier immutable boot entries. |
|
| 180 | + | fn dependency(entries: &[catalog::Entry], pin: binary::Id) -> catalog::Entry |
|
| 181 | + | throws (shared::Error) |
|
| 182 | + | { |
|
| 183 | + | for entry in entries { |
|
| 184 | + | let package = entry.package; |
|
| 185 | + | if package.contract.verification <> shared::Verification::Verified { |
|
| 186 | + | continue; |
|
| 187 | + | } |
|
| 188 | + | let image = package.contract.image else continue; |
|
| 189 | + | if not mem::eq(&image.bytes[..], &pin.bytes[..]) { |
|
| 190 | + | continue; |
|
| 191 | + | } |
|
| 192 | + | return entry; |
|
| 193 | + | } |
|
| 194 | + | throw shared::Error::Symbol; |
|
| 195 | + | } |
|
| 196 | + | ||
| 197 | + | /// Build typed provider declarations and the same native aliases for a user boot package. |
|
| 198 | + | fn userImports( |
|
| 199 | + | input: &binary::Package, |
|
| 200 | + | entries: &[catalog::Entry], |
|
| 201 | + | output: &mut [shared::Symbol], |
|
| 202 | + | arena: &heap::Heap |
|
| 203 | + | ) -> *[linking::Provider] throws (shared::Error) { |
|
| 204 | + | if input.links.pins.len <> input.dependencies.len { |
|
| 205 | + | throw shared::Error::Symbol; |
|
| 206 | + | } |
|
| 207 | + | if input.links.imports.len > output.len { |
|
| 208 | + | throw shared::Error::Capacity; |
|
| 209 | + | } |
|
| 210 | + | let mut providers: *mut [linking::Provider] = &mut []; |
|
| 211 | + | let mut exports: *mut [*[shared::Symbol]] = &mut []; |
|
| 212 | + | for pin in input.links.pins { |
|
| 213 | + | let selected = try dependency(entries, pin); |
|
| 214 | + | let declarations = try program::declarations(selected.source, arena) catch error { |
|
| 215 | + | if error == binary::Error::Storage { |
|
| 216 | + | throw shared::Error::Capacity; |
|
| 217 | + | } |
|
| 218 | + | throw shared::Error::Symbol; |
|
| 219 | + | }; |
|
| 220 | + | try exports.append(selected.package.exports, arena) catch { |
|
| 221 | + | throw shared::Error::Capacity; |
|
| 222 | + | }; |
|
| 223 | + | try providers.append( |
|
| 224 | + | linking::Provider { |
|
| 225 | + | image: pin, contract: declarations |
|
| 226 | + | }, |
|
| 227 | + | arena |
|
| 228 | + | ) catch { |
|
| 229 | + | throw shared::Error::Capacity; |
|
| 230 | + | }; |
|
| 231 | + | } |
|
| 232 | + | for imported, i in input.links.imports { |
|
| 233 | + | if imported.dependency >= input.links.pins.len { |
|
| 234 | + | throw shared::Error::Symbol; |
|
| 235 | + | } |
|
| 236 | + | let target = shared::lookup(exports[imported.dependency], imported.exported) |
|
| 237 | + | else throw shared::Error::Symbol; |
|
| 238 | + | set output[i] = shared::Symbol { name: imported.name, target, startable: false }; |
|
| 239 | + | } |
|
| 240 | + | return &providers[..]; |
|
| 241 | + | } |
|
| 242 | + | ||
| 178 | 243 | /// Link shared boot packages, retain their catalog, and instantiate the kernel graph. |
|
| 179 | 244 | @default unsafe fn main(env: *sys::Env) -> i32 { |
|
| 180 | 245 | assert env.args.len >= 4 and env.args.len <= PACKAGES + 2; |
|
| 181 | 246 | let count = env.args.len - 2; |
|
| 182 | 247 | let decoder = heap::over(&mut DECODE[..]); |
| 256 | 321 | let mut input = try! program::decode(bytes, decoder, binary::Limits { |
|
| 257 | 322 | registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN |
|
| 258 | 323 | }); |
|
| 259 | 324 | // User packages run in U-mode, so every one must verify; there is no way around this. |
|
| 260 | 325 | let verifier = heap::over(&mut SCRATCH[..]); |
|
| 261 | - | try verify::verifyPackage( |
|
| 262 | - | &mut input, shared::verifierConfig(ENV_SIZE), verifier, decoder |
|
| 326 | + | let providers = try userImports( |
|
| 327 | + | &input, &catalogEntries[.. slot], &mut IMPORTS[..], decoder |
|
| 328 | + | ) catch error { |
|
| 329 | + | report(error); |
|
| 330 | + | return 1; |
|
| 331 | + | }; |
|
| 332 | + | try verify::verifyLinkedPackage( |
|
| 333 | + | &mut input, &providers[..], shared::verifierConfig(ENV_SIZE), verifier, decoder |
|
| 263 | 334 | ) catch error { |
|
| 264 | 335 | io::print("native build: boot package fails verification: "); |
|
| 265 | 336 | io::print(verify::ruleName(error.rule)); |
|
| 266 | 337 | io::print(" "); |
|
| 267 | 338 | match error.place { |
| 273 | 344 | } |
|
| 274 | 345 | io::print(" in "); |
|
| 275 | 346 | io::printLn(input.name); |
|
| 276 | 347 | return 1; |
|
| 277 | 348 | }; |
|
| 278 | - | let mut imported: u32 = 0; |
|
| 279 | - | for previous in 0 .. slot { |
|
| 280 | - | for symbol in catalogEntries[previous].package.exports { |
|
| 281 | - | assert imported < IMPORTS.len; |
|
| 282 | - | set IMPORTS[imported] = symbol; |
|
| 283 | - | set imported += 1; |
|
| 284 | - | } |
|
| 285 | - | } |
|
| 349 | + | let imported = input.links.imports.len; |
|
| 286 | 350 | let package = try compile( |
|
| 287 | 351 | &input, |
|
| 288 | 352 | slot, |
|
| 289 | 353 | aligned( |
|
| 290 | 354 | codeEnd, CODE_ALIGNMENT |
lib/std.rad
+1 -0
| 1 | 1 | //! The Radiance Standard Library. |
|
| 2 | 2 | ||
| 3 | 3 | export mod io; |
|
| 4 | + | export mod hash; |
|
| 4 | 5 | export mod collections; |
|
| 5 | 6 | export mod char; |
|
| 6 | 7 | export mod lang; |
|
| 7 | 8 | export mod sys; |
|
| 8 | 9 | export mod fs; |
lib/std/arch/rv64/shared.rad
+58 -12
| 1 | 1 | //! Shared package code, private data templates, and qualified symbol linking. |
|
| 2 | 2 | ||
| 3 | 3 | @test export mod tests; |
|
| 4 | 4 | export mod catalog; |
|
| 5 | 5 | ||
| 6 | - | use std::mem; |
|
| 7 | - | use std::mem::heap; |
|
| 8 | - | use std::lang::il; |
|
| 9 | - | use std::lang::il::binary; |
|
| 10 | - | use std::lang::il::verify; |
|
| 11 | - | use std::lang::gen::regalloc::liveness; |
|
| 6 | + | use std::collections::dict; |
|
| 12 | 7 | use std::lang::gen; |
|
| 13 | 8 | use std::lang::gen::data; |
|
| 14 | 9 | use std::lang::gen::labels; |
|
| 15 | - | use std::collections::dict; |
|
| 10 | + | use std::lang::gen::regalloc::liveness; |
|
| 11 | + | use std::lang::il; |
|
| 12 | + | use std::lang::il::binary; |
|
| 13 | + | use std::lang::il::verify; |
|
| 14 | + | use std::mem; |
|
| 15 | + | use std::mem::heap; |
|
| 16 | + | use super::asm; |
|
| 16 | 17 | use super::emit; |
|
| 17 | 18 | use super::encode; |
|
| 18 | 19 | use super::image; |
|
| 19 | - | use super::asm; |
|
| 20 | 20 | ||
| 21 | 21 | /// Largest layout in bytes accepted for an untrusted package. |
|
| 22 | 22 | constant MAX_LAYOUT_SIZE: u32 = 1048576; |
|
| 23 | 23 | /// Maximum nested layout depth accepted for an untrusted package. |
|
| 24 | 24 | constant MAX_LAYOUT_DEPTH: u32 = 16; |
| 64 | 64 | export record Symbol: Copy { |
|
| 65 | 65 | /// Fully qualified symbol name. |
|
| 66 | 66 | name: *[u8], |
|
| 67 | 67 | /// Code address or private data location. |
|
| 68 | 68 | target: Target, |
|
| 69 | + | /// Whether a fresh context may start at this function. |
|
| 70 | + | startable: bool, |
|
| 69 | 71 | } |
|
| 70 | 72 | ||
| 71 | 73 | /// Private pointer fixup applied when a domain is instantiated. |
|
| 72 | 74 | export record Relocation: Copy { |
|
| 73 | 75 | /// Byte offset of the first pointer in the private template. |
| 92 | 94 | relocations: *mut [Relocation], |
|
| 93 | 95 | /// Stack growth check of the generated code. |
|
| 94 | 96 | stack: super::StackCheck, |
|
| 95 | 97 | } |
|
| 96 | 98 | ||
| 99 | + | /// Verification policy under which native code was generated. |
|
| 100 | + | export union Verification: Copy { |
|
| 101 | + | /// Native code without the user-package safety proof and checked stack ABI. |
|
| 102 | + | Unverified, |
|
| 103 | + | /// Checked RIL with a protected stack and no assembly prefix. |
|
| 104 | + | Verified, |
|
| 105 | + | } |
|
| 106 | + | ||
| 107 | + | /// Persistent evidence and exact dependencies of a native package. |
|
| 108 | + | export record Contract: Copy { |
|
| 109 | + | /// Exact dependency identities in the declared order. |
|
| 110 | + | pins: *[binary::Id], |
|
| 111 | + | /// Exact source identity, present for decoded binary packages. |
|
| 112 | + | image: ?binary::Id, |
|
| 113 | + | /// Eligibility for user-package imports and native-cache reuse. |
|
| 114 | + | verification: Verification, |
|
| 115 | + | } |
|
| 116 | + | ||
| 117 | + | /// Native metadata without a user-package verification result. |
|
| 118 | + | export constant UNVERIFIED: Contract = Contract { |
|
| 119 | + | pins: &[], |
|
| 120 | + | image: nil, |
|
| 121 | + | verification: Verification::Unverified, |
|
| 122 | + | }; |
|
| 123 | + | ||
| 97 | 124 | /// A resident package entry used by boot catalogs and runtime loading. |
|
| 98 | 125 | export record Package: Copy { |
|
| 99 | - | /// Immutable package identity. |
|
| 126 | + | /// Diagnostic label of the package. |
|
| 100 | 127 | name: *[u8], |
|
| 101 | - | /// Required package names, in binary package order. |
|
| 128 | + | /// Diagnostic dependency labels, in exact pin order. |
|
| 102 | 129 | dependencies: *[*[u8]], |
|
| 103 | 130 | /// Stable slot in each domain's package-state table. |
|
| 104 | 131 | slot: u32, |
|
| 105 | 132 | /// Address at which the shared instructions execute. |
|
| 106 | 133 | codeAddress: u64, |
| 116 | 143 | memory: u32, |
|
| 117 | 144 | /// Required private state base alignment. |
|
| 118 | 145 | alignment: u32, |
|
| 119 | 146 | /// Private pointers to resolve after all graph instances have storage. |
|
| 120 | 147 | relocations: *[Relocation], |
|
| 148 | + | /// Exact dependencies, source identity and native verification policy. |
|
| 149 | + | contract: Contract, |
|
| 121 | 150 | } |
|
| 122 | 151 | ||
| 123 | 152 | /// Find exactly one qualified symbol in a bounded table. |
|
| 124 | 153 | export fn lookup(symbols: &[Symbol], name: &[u8]) -> ?Target { |
|
| 125 | 154 | for symbol in symbols { |
| 147 | 176 | throw Error::Symbol; |
|
| 148 | 177 | } |
|
| 149 | 178 | if *count == symbols.len { |
|
| 150 | 179 | throw Error::Capacity; |
|
| 151 | 180 | } |
|
| 152 | - | set symbols[*count] = Symbol { name, target }; |
|
| 181 | + | set symbols[*count] = Symbol { |
|
| 182 | + | name, |
|
| 183 | + | target, |
|
| 184 | + | startable: false, |
|
| 185 | + | }; |
|
| 153 | 186 | set *count += 1; |
|
| 154 | 187 | } |
|
| 155 | 188 | ||
| 156 | 189 | /// Resolve a shared function address with its required kind. |
|
| 157 | 190 | fn function(local: &[Symbol], imports: &[Symbol], name: *[u8]) -> u64 throws (Error) { |
| 499 | 532 | } |
|
| 500 | 533 | } |
|
| 501 | 534 | if lookup(&exportStorage[.. i], exported.name) <> nil { |
|
| 502 | 535 | throw Error::Symbol; |
|
| 503 | 536 | } |
|
| 504 | - | set exportStorage[i] = Symbol { name: exported.name, target }; |
|
| 537 | + | set exportStorage[i] = Symbol { |
|
| 538 | + | name: exported.name, |
|
| 539 | + | target, |
|
| 540 | + | startable: exported.startable, |
|
| 541 | + | }; |
|
| 505 | 542 | } |
|
| 506 | 543 | let mut entry: ?u64 = nil; |
|
| 507 | 544 | if let name = input.entry { |
|
| 508 | 545 | set entry = try function(&exportStorage[.. input.exports.len], &[], name); |
|
| 509 | 546 | } |
| 524 | 561 | entry, |
|
| 525 | 562 | template: &templateStorage[.. size.bytes], |
|
| 526 | 563 | memory, |
|
| 527 | 564 | alignment, |
|
| 528 | 565 | relocations: &relocationStorage[.. size.relocations], |
|
| 566 | + | contract: Contract { |
|
| 567 | + | pins: input.links.pins, |
|
| 568 | + | image: input.links.image, |
|
| 569 | + | verification: Verification::Verified |
|
| 570 | + | if input.links.verified |
|
| 571 | + | and stack == super::StackCheck::TpLimit |
|
| 572 | + | and assembly.text.len == 0 |
|
| 573 | + | else Verification::Unverified, |
|
| 574 | + | }, |
|
| 529 | 575 | }; |
|
| 530 | 576 | } |
|
| 531 | 577 | ||
| 532 | 578 | /// Validate a package's private-state bases and fixups without touching memory. |
|
| 533 | 579 | /// Every base must be assigned and aligned, and every fixup must stay in range. |
lib/std/arch/rv64/shared/catalog.rad
+49 -3
| 4 | 4 | //! written at the byte offsets of the compiler's native layout. The constants |
|
| 5 | 5 | //! below name those offsets, and `shared/tests.rad` compares every one of them |
|
| 6 | 6 | //! with the layout that the compiler gives the records, so that a change of |
|
| 7 | 7 | //! the layout fails a test. All integers are little-endian. |
|
| 8 | 8 | ||
| 9 | - | use std::mem; |
|
| 10 | 9 | use std::arch::rv64::shared; |
|
| 10 | + | use std::lang::il::binary; |
|
| 11 | + | use std::mem; |
|
| 11 | 12 | ||
| 12 | 13 | /// Binary package identity and its resident native descriptor. |
|
| 13 | 14 | export record Entry: Copy { |
|
| 14 | 15 | /// Complete immutable binary RIL bytes. |
|
| 15 | 16 | source: *[u8], |
| 25 | 26 | export constant SLICE_LEN: u32 = 8; |
|
| 26 | 27 | /// Offset of the capacity in a native slice. |
|
| 27 | 28 | export constant SLICE_CAP: u32 = 12; |
|
| 28 | 29 | ||
| 29 | 30 | /// Size of a native [`Entry`]. |
|
| 30 | - | export constant ENTRY_SIZE: u32 = 152; |
|
| 31 | + | export constant ENTRY_SIZE: u32 = 208; |
|
| 31 | 32 | /// Alignment of a native [`Entry`]. |
|
| 32 | 33 | export constant ENTRY_ALIGN: u32 = 8; |
|
| 33 | 34 | /// Offset of `Entry.source`. |
|
| 34 | 35 | export constant ENTRY_SOURCE: u32 = 0; |
|
| 35 | 36 | /// Offset of `Entry.package.name`. |
| 57 | 58 | export constant ENTRY_ALIGNMENT: u32 = 132; |
|
| 58 | 59 | /// Offset of `Entry.package.relocations`. |
|
| 59 | 60 | export constant ENTRY_RELOCATIONS: u32 = 136; |
|
| 60 | 61 | ||
| 61 | 62 | /// Size of a native [`shared::Symbol`]. |
|
| 62 | - | export constant SYMBOL_SIZE: u32 = 32; |
|
| 63 | + | export constant SYMBOL_SIZE: u32 = 40; |
|
| 63 | 64 | /// Alignment of a native [`shared::Symbol`]. |
|
| 64 | 65 | export constant SYMBOL_ALIGN: u32 = 8; |
|
| 65 | 66 | /// Offset of `Symbol.name`. |
|
| 66 | 67 | export constant SYMBOL_NAME: u32 = 0; |
|
| 67 | 68 | /// Offset of the one-byte variant tag of `Symbol.target`: zero for a function, |
| 69 | 70 | export constant SYMBOL_TAG: u32 = 16; |
|
| 70 | 71 | /// Offset of the payload of `Symbol.target`: the address of a function, or the |
|
| 71 | 72 | /// slot and the offset of a data reference as two 32-bit integers. |
|
| 72 | 73 | export constant SYMBOL_VALUE: u32 = 24; |
|
| 73 | 74 | ||
| 75 | + | /// Offset of the exact dependency pin slice in the native package contract. |
|
| 76 | + | export constant ENTRY_PINS: u32 = 152; |
|
| 77 | + | /// Offset of the verification policy tag of the native package contract. |
|
| 78 | + | export constant ENTRY_VERIFICATION: u32 = 201; |
|
| 79 | + | /// Offset of the optional exact image identity tag. |
|
| 80 | + | export constant ENTRY_IMAGE_TAG: u32 = 168; |
|
| 81 | + | /// Offset of the 32 identity bytes when the identity is present. |
|
| 82 | + | export constant ENTRY_IMAGE_VALUE: u32 = 169; |
|
| 83 | + | /// Size in bytes of one 256-bit dependency identity. |
|
| 84 | + | export constant PIN_SIZE: u32 = 32; |
|
| 85 | + | ||
| 74 | 86 | /// Size of a native [`shared::Relocation`]. |
|
| 75 | 87 | export constant RELOCATION_SIZE: u32 = 16; |
|
| 76 | 88 | /// Alignment of a native [`shared::Relocation`]. |
|
| 77 | 89 | export constant RELOCATION_ALIGN: u32 = 4; |
|
| 78 | 90 | /// Offset of `Relocation.offset`. |
| 82 | 94 | /// Offset of `Relocation.target.slot`. |
|
| 83 | 95 | export constant RELOCATION_SLOT: u32 = 8; |
|
| 84 | 96 | /// Offset of `Relocation.target.offset`. |
|
| 85 | 97 | export constant RELOCATION_TARGET_OFFSET: u32 = 12; |
|
| 86 | 98 | ||
| 99 | + | /// Offset of the verified context-entry classification of a public symbol. |
|
| 100 | + | export constant SYMBOL_STARTABLE: u32 = 32; |
|
| 101 | + | ||
| 87 | 102 | /// Reserve aligned bytes in the native catalog output. |
|
| 88 | 103 | fn reserve(output: &mut [u8], used: &mut u32, size: u32, alignment: u32) -> u32 |
|
| 89 | 104 | throws (shared::Error) |
|
| 90 | 105 | { |
|
| 91 | 106 | let padding = (alignment - (*used & (alignment - 1))) & (alignment - 1); |
| 137 | 152 | slice(output, offset + i * SLICE_SIZE, base + at as u64, name.len); |
|
| 138 | 153 | } |
|
| 139 | 154 | return offset; |
|
| 140 | 155 | } |
|
| 141 | 156 | ||
| 157 | + | /// Copy dependency identities into immutable native metadata. |
|
| 158 | + | fn pins(input: *[binary::Id], output: &mut [u8], used: &mut u32) -> u32 |
|
| 159 | + | throws (shared::Error) |
|
| 160 | + | { |
|
| 161 | + | if input.len > output.len / PIN_SIZE { |
|
| 162 | + | throw shared::Error::Capacity; |
|
| 163 | + | } |
|
| 164 | + | let offset = try reserve(output, used, input.len * PIN_SIZE, 1); |
|
| 165 | + | for pin, i in input { |
|
| 166 | + | let at = offset + i * PIN_SIZE; |
|
| 167 | + | try! mem::copy(&mut output[at .. at + PIN_SIZE], &pin.bytes[..]); |
|
| 168 | + | } |
|
| 169 | + | return offset; |
|
| 170 | + | } |
|
| 171 | + | ||
| 142 | 172 | /// Copy public symbols and their names into native catalog storage. |
|
| 143 | 173 | fn symbols(input: *[shared::Symbol], base: u64, output: &mut [u8], used: &mut u32) -> u32 |
|
| 144 | 174 | throws (shared::Error) |
|
| 145 | 175 | { |
|
| 146 | 176 | if input.len > output.len / SYMBOL_SIZE { |
| 149 | 179 | let offset = try reserve(output, used, input.len * SYMBOL_SIZE, SYMBOL_ALIGN); |
|
| 150 | 180 | for symbol, i in input { |
|
| 151 | 181 | let at = offset + i * SYMBOL_SIZE; |
|
| 152 | 182 | let name = try bytes(symbol.name, output, used); |
|
| 153 | 183 | slice(output, at + SYMBOL_NAME, base + name as u64, symbol.name.len); |
|
| 184 | + | integer(output, at + SYMBOL_STARTABLE, 1 if symbol.startable else 0, 1); |
|
| 154 | 185 | match symbol.target { |
|
| 155 | 186 | case shared::Target::Function(address) => { |
|
| 156 | 187 | integer(output, at + SYMBOL_TAG, 0, 8); |
|
| 157 | 188 | integer(output, at + SYMBOL_VALUE, address, 8); |
|
| 158 | 189 | } |
| 209 | 240 | let name = try bytes(package.name, output, &mut used); |
|
| 210 | 241 | let required = try dependencies(package.dependencies, base, output, &mut used); |
|
| 211 | 242 | let exports = try symbols(package.exports, base, output, &mut used); |
|
| 212 | 243 | let template = try bytes(package.template, output, &mut used); |
|
| 213 | 244 | let fixups = try relocations(package.relocations, output, &mut used); |
|
| 245 | + | let exact = try pins(package.contract.pins, output, &mut used); |
|
| 214 | 246 | slice(output, at + ENTRY_SOURCE, base + source as u64, entry.source.len); |
|
| 215 | 247 | slice(output, at + ENTRY_NAME, base + name as u64, package.name.len); |
|
| 216 | 248 | slice(output, at + ENTRY_DEPENDENCIES, base + required as u64, package.dependencies.len); |
|
| 217 | 249 | integer(output, at + ENTRY_SLOT, package.slot as u64, 4); |
|
| 218 | 250 | integer(output, at + ENTRY_CODE_ADDRESS, package.codeAddress, 8); |
| 224 | 256 | } |
|
| 225 | 257 | slice(output, at + ENTRY_TEMPLATE, base + template as u64, package.template.len); |
|
| 226 | 258 | integer(output, at + ENTRY_MEMORY, package.memory as u64, 4); |
|
| 227 | 259 | integer(output, at + ENTRY_ALIGNMENT, package.alignment as u64, 4); |
|
| 228 | 260 | slice(output, at + ENTRY_RELOCATIONS, base + fixups as u64, package.relocations.len); |
|
| 261 | + | slice(output, at + ENTRY_PINS, base + exact as u64, package.contract.pins.len); |
|
| 262 | + | if let identity = package.contract.image { |
|
| 263 | + | integer(output, at + ENTRY_IMAGE_TAG, 1, 1); |
|
| 264 | + | try! mem::copy( |
|
| 265 | + | &mut output[at + ENTRY_IMAGE_VALUE .. at + ENTRY_IMAGE_VALUE + 32], |
|
| 266 | + | &identity.bytes[..] |
|
| 267 | + | ); |
|
| 268 | + | } |
|
| 269 | + | integer( |
|
| 270 | + | output, |
|
| 271 | + | at + ENTRY_VERIFICATION, |
|
| 272 | + | 1 if package.contract.verification == shared::Verification::Verified else 0, |
|
| 273 | + | 1 |
|
| 274 | + | ); |
|
| 229 | 275 | } |
|
| 230 | 276 | return used; |
|
| 231 | 277 | } |
lib/std/arch/rv64/shared/tests.rad
+59 -11
| 1 | 1 | //! Private template relocation and bounded linking checks. |
|
| 2 | 2 | ||
| 3 | 3 | @test mod run; |
|
| 4 | 4 | ||
| 5 | - | use std::testing; |
|
| 6 | 5 | use std::arch::rv64; |
|
| 7 | - | use std::mem; |
|
| 8 | - | use std::arch::rv64::shared::catalog; |
|
| 9 | 6 | use std::arch::rv64::asm; |
|
| 10 | - | use std::mem::heap; |
|
| 7 | + | use std::arch::rv64::shared::catalog; |
|
| 8 | + | use std::lang::gen::data; |
|
| 11 | 9 | use std::lang::il; |
|
| 12 | 10 | use std::lang::il::binary; |
|
| 13 | - | use std::lang::gen::data; |
|
| 11 | + | use std::lang::il::binary::linkage; |
|
| 12 | + | use std::mem; |
|
| 13 | + | use std::mem::heap; |
|
| 14 | + | use std::testing; |
|
| 14 | 15 | ||
| 15 | 16 | /// Code generation workspace reused between checks. |
|
| 16 | 17 | export static ARENA: [u8; 16777216] = [0; 16777216]; |
|
| 17 | 18 | /// Function allocation workspace. |
|
| 18 | 19 | export static SCRATCH: [u8; 16777216] = [0; 16777216]; |
| 33 | 34 | @test unsafe fn assemblyPrefix() throws (testing::TestError) { |
|
| 34 | 35 | let input = binary::Package { |
|
| 35 | 36 | symbols: &[], |
|
| 36 | 37 | name: "p", |
|
| 37 | 38 | dependencies: &[], |
|
| 38 | - | exports: &[binary::Export { name: "p::entry", kind: binary::ExportKind::Function }], |
|
| 39 | + | exports: &[ |
|
| 40 | + | binary::Export { |
|
| 41 | + | name: "p::entry", |
|
| 42 | + | kind: binary::ExportKind::Function, |
|
| 43 | + | startable: false, |
|
| 44 | + | }, |
|
| 45 | + | ], |
|
| 39 | 46 | entry: "p::entry", |
|
| 40 | 47 | program: il::Program { |
|
| 41 | 48 | data: &[], fns: &[], layouts: &[], signatures: &[], traits: &[] |
|
| 42 | 49 | }, |
|
| 50 | + | links: binary::EMPTY_LINKS, |
|
| 43 | 51 | }; |
|
| 44 | 52 | let assembly = asm::Program { |
|
| 45 | 53 | text: &[0x02a00513 as u32, 0x00008067], |
|
| 46 | 54 | data: &[], |
|
| 47 | 55 | symbols: &[ |
| 82 | 90 | ); |
|
| 83 | 91 | } |
|
| 84 | 92 | ||
| 85 | 93 | /// Packed catalog pointers resolve to retained metadata at the supplied native base. |
|
| 86 | 94 | @test unsafe fn nativeCatalog() throws (testing::TestError) { |
|
| 87 | - | let package = try compile(imports(), 64) catch { |
|
| 95 | + | let mut package = try compile(imports(), 64) catch { |
|
| 88 | 96 | throw testing::TestError::Failed; |
|
| 89 | 97 | }; |
|
| 98 | + | set package.contract = super::Contract { |
|
| 99 | + | pins: &[ |
|
| 100 | + | binary::Id { bytes: [1; 32] } |
|
| 101 | + | ], |
|
| 102 | + | image: binary::Id { bytes: [3; 32] }, |
|
| 103 | + | verification: super::Verification::Verified, |
|
| 104 | + | }; |
|
| 90 | 105 | let output = @sliceOf(&mut CATALOG[0] as *mut u8, @sizeOf([u64; 512])); |
|
| 91 | 106 | let base = output.ptr as u64; |
|
| 92 | 107 | let used = try catalog::pack( |
|
| 93 | 108 | &[catalog::Entry { source: "binary package", package }], base, output |
|
| 94 | 109 | ) catch { |
| 133 | 148 | and package.relocations.len > 0 |
|
| 134 | 149 | ); |
|
| 135 | 150 | for fixup, i in package.relocations { |
|
| 136 | 151 | try testing::expect(entry.package.relocations[i] == fixup); |
|
| 137 | 152 | } |
|
| 153 | + | assert entry.package.contract.pins[0] == package.contract.pins[0]; |
|
| 154 | + | assert entry.package.contract.image == package.contract.image; |
|
| 155 | + | assert entry.package.contract.verification == super::Verification::Verified; |
|
| 138 | 156 | try testing::expect(mem::eq(entry.package.template, package.template)); |
|
| 139 | 157 | set TEMPLATE[0] = 91; |
|
| 140 | 158 | set EXPORTS[0].name = "changed"; |
|
| 141 | 159 | try testing::expect( |
|
| 142 | 160 | entry.package.template[0] == 0 and mem::eq( |
| 190 | 208 | entry: 0x99, |
|
| 191 | 209 | template: &[], |
|
| 192 | 210 | memory: 9, |
|
| 193 | 211 | alignment: 16, |
|
| 194 | 212 | relocations: &[], |
|
| 213 | + | contract: super::UNVERIFIED, |
|
| 195 | 214 | }; |
|
| 196 | 215 | let entry = catalog::Entry { source: "abc", package }; |
|
| 197 | 216 | try testing::expect( |
|
| 198 | 217 | @sizeOf(*[u8]) == catalog::SLICE_SIZE |
|
| 199 | 218 | and @alignOf(*[u8]) == catalog::SLICE_ALIGN |
| 220 | 239 | try testing::expect(offsetOf(&entry, &entry.package.entry) == catalog::ENTRY_ENTRY_TAG); |
|
| 221 | 240 | try testing::expect(offsetOf(&entry, &entry.package.template) == catalog::ENTRY_TEMPLATE); |
|
| 222 | 241 | try testing::expect(offsetOf(&entry, &entry.package.memory) == catalog::ENTRY_MEMORY); |
|
| 223 | 242 | try testing::expect(offsetOf(&entry, &entry.package.alignment) == catalog::ENTRY_ALIGNMENT); |
|
| 224 | 243 | try testing::expect(offsetOf(&entry, &entry.package.relocations) == catalog::ENTRY_RELOCATIONS); |
|
| 244 | + | assert offsetOf(&entry, &entry.package.contract.pins) == catalog::ENTRY_PINS; |
|
| 245 | + | assert offsetOf(&entry, &entry.package.contract.image) == catalog::ENTRY_IMAGE_TAG; |
|
| 246 | + | assert offsetOf(&entry, &entry.package.contract.verification) == catalog::ENTRY_VERIFICATION; |
|
| 247 | + | assert @sizeOf(binary::Id) == catalog::PIN_SIZE; |
|
| 225 | 248 | // A slice is an address, a length, and a capacity. |
|
| 226 | 249 | let slice = catalog::ENTRY_SOURCE; |
|
| 227 | 250 | try testing::expect(word(&entry, slice, 8) == entry.source.ptr as u64); |
|
| 228 | 251 | try testing::expect( |
|
| 229 | 252 | word(&entry, slice + catalog::SLICE_LEN, 4) == 3 and word( |
| 259 | 282 | entry: nil, |
|
| 260 | 283 | template: &[], |
|
| 261 | 284 | memory: 9, |
|
| 262 | 285 | alignment: 16, |
|
| 263 | 286 | relocations: &[], |
|
| 287 | + | contract: super::UNVERIFIED, |
|
| 264 | 288 | }, |
|
| 265 | 289 | }; |
|
| 266 | 290 | try testing::expect(word(&absent, catalog::ENTRY_ENTRY_TAG, 1) == 0); |
|
| 267 | 291 | ||
| 268 | - | let function = super::Symbol { name: "f", target: super::Target::Function(0xaabbccdd11) }; |
|
| 292 | + | let function = super::Symbol { |
|
| 293 | + | name: "f", |
|
| 294 | + | target: super::Target::Function(0xaabbccdd11), |
|
| 295 | + | startable: true, |
|
| 296 | + | }; |
|
| 269 | 297 | let data = super::Symbol { |
|
| 270 | 298 | name: "d", |
|
| 271 | 299 | target: super::Target::Data(super::DataRef { |
|
| 272 | 300 | slot: 3, offset: 4 |
|
| 273 | 301 | }), |
|
| 302 | + | startable: true, |
|
| 274 | 303 | }; |
|
| 275 | 304 | try testing::expect( |
|
| 276 | 305 | @sizeOf(super::Symbol) == catalog::SYMBOL_SIZE |
|
| 277 | 306 | and @alignOf(super::Symbol) == catalog::SYMBOL_ALIGN |
|
| 278 | 307 | ); |
|
| 279 | 308 | try testing::expect(offsetOf(&function, &function.name) == catalog::SYMBOL_NAME); |
|
| 280 | 309 | try testing::expect(offsetOf(&function, &function.target) == catalog::SYMBOL_TAG); |
|
| 310 | + | assert offsetOf(&function, &function.startable) == catalog::SYMBOL_STARTABLE; |
|
| 281 | 311 | try testing::expect( |
|
| 282 | 312 | word(&function, catalog::SYMBOL_TAG, 1) == 0 and word( |
|
| 283 | 313 | &function, catalog::SYMBOL_VALUE, 8 |
|
| 284 | 314 | ) == 0xaabbccdd11 |
|
| 285 | 315 | ); |
| 340 | 370 | unsafe fn input() -> binary::Package { |
|
| 341 | 371 | return binary::Package { |
|
| 342 | 372 | symbols: &[], |
|
| 343 | 373 | name: "p", |
|
| 344 | 374 | dependencies: &["dep"], |
|
| 345 | - | exports: &[binary::Export { name: "p::refs", kind: binary::ExportKind::Data }], |
|
| 375 | + | exports: &[ |
|
| 376 | + | binary::Export { |
|
| 377 | + | name: "p::refs", |
|
| 378 | + | kind: binary::ExportKind::Data, |
|
| 379 | + | startable: false, |
|
| 380 | + | }, |
|
| 381 | + | ], |
|
| 346 | 382 | entry: nil, |
|
| 347 | 383 | program: il::Program { |
|
| 348 | 384 | data: &[ |
|
| 349 | 385 | il::Data { |
|
| 350 | 386 | name: "p::refs", |
| 376 | 412 | il::Layout { size: 16, alignment: 16, slots: &[], regions: 0 }, |
|
| 377 | 413 | ], |
|
| 378 | 414 | signatures: &[], |
|
| 379 | 415 | traits: &[], |
|
| 380 | 416 | }, |
|
| 417 | + | links: binary::EMPTY_LINKS, |
|
| 381 | 418 | }; |
|
| 382 | 419 | } |
|
| 383 | 420 | ||
| 384 | 421 | /// Compile into small persistent tables. |
|
| 385 | 422 | unsafe fn compile(imports: &[super::Symbol], capacity: u32) -> super::Package |
| 412 | 449 | super::Symbol { |
|
| 413 | 450 | name: "dep::value", |
|
| 414 | 451 | target: super::Target::Data(super::DataRef { |
|
| 415 | 452 | slot: 1, offset: 8192 |
|
| 416 | 453 | }), |
|
| 454 | + | startable: true, |
|
| 455 | + | }, |
|
| 456 | + | super::Symbol { |
|
| 457 | + | name: "dep::call", |
|
| 458 | + | target: super::Target::Function(0x180001234), |
|
| 459 | + | startable: true, |
|
| 417 | 460 | }, |
|
| 418 | - | super::Symbol { name: "dep::call", target: super::Target::Function(0x180001234) }, |
|
| 419 | 461 | ]; |
|
| 420 | 462 | } |
|
| 421 | 463 | ||
| 422 | 464 | /// Read a little-endian pointer without requiring buffer alignment. |
|
| 423 | 465 | fn pointer(bytes: &[u8], offset: u32) -> u64 { |
| 488 | 530 | fns: &[], |
|
| 489 | 531 | layouts: &[il::Layout { size: 0, alignment: 1, slots: &[], regions: 0 }], |
|
| 490 | 532 | signatures: &[], |
|
| 491 | 533 | traits: &[], |
|
| 492 | 534 | }, |
|
| 535 | + | links: binary::EMPTY_LINKS, |
|
| 493 | 536 | }; |
|
| 494 | 537 | } |
|
| 495 | 538 | ||
| 496 | 539 | /// A heap region becomes a header and a pool in the private memory of an instance. The header |
|
| 497 | 540 | /// holds no used bytes, the size of the pool, and the address of the pool in this instance. |
| 571 | 614 | try testing::expect(err == super::Error::Symbol); |
|
| 572 | 615 | set failed += 1; |
|
| 573 | 616 | }; |
|
| 574 | 617 | try compile( |
|
| 575 | 618 | &[ |
|
| 576 | - | super::Symbol { name: "dep::value", target: super::Target::Function(0x8000) }, names[1] |
|
| 619 | + | super::Symbol { |
|
| 620 | + | name: "dep::value", |
|
| 621 | + | target: super::Target::Function(0x8000), |
|
| 622 | + | startable: true, |
|
| 623 | + | }, |
|
| 624 | + | names[1], |
|
| 577 | 625 | ], |
|
| 578 | 626 | 64 |
|
| 579 | 627 | ) catch err { |
|
| 580 | 628 | try testing::expect(err == super::Error::Symbol); |
|
| 581 | 629 | set failed += 1; |
lib/std/arch/rv64/shared/tests/run.rad
+21 -8
| 2 | 2 | //! |
|
| 3 | 3 | //! A check compiles one function with the shared code generator, runs its machine code on |
|
| 4 | 4 | //! a model of the few instructions it uses, and compares the registers and memory that the |
|
| 5 | 5 | //! function leaves with the semantics of the IL instruction. |
|
| 6 | 6 | ||
| 7 | - | use std::testing; |
|
| 8 | 7 | use std::arch::rv64; |
|
| 9 | - | use std::arch::rv64::shared; |
|
| 10 | 8 | use std::arch::rv64::atomics; |
|
| 11 | 9 | use std::arch::rv64::decode; |
|
| 12 | - | use std::mem::heap; |
|
| 10 | + | use std::arch::rv64::shared; |
|
| 11 | + | use std::arch::rv64::shared::tests; |
|
| 13 | 12 | use std::lang::gen; |
|
| 13 | + | use std::lang::gen::data; |
|
| 14 | 14 | use std::lang::il; |
|
| 15 | 15 | use std::lang::il::binary; |
|
| 16 | - | use std::lang::gen::data; |
|
| 17 | - | use std::arch::rv64::shared::tests; |
|
| 16 | + | use std::mem::heap; |
|
| 17 | + | use std::testing; |
|
| 18 | 18 | ||
| 19 | 19 | /// Storage for the IL of a check. |
|
| 20 | 20 | static IL: [u8; 65536] = [0; 65536]; |
|
| 21 | 21 | /// Layout table of a blit check, which has the copied layout only. |
|
| 22 | 22 | unsafe static COPIED: [il::Layout; 1] = undefined; |
| 409 | 409 | let input = binary::Package { |
|
| 410 | 410 | symbols: &[], |
|
| 411 | 411 | name: "p", |
|
| 412 | 412 | dependencies: &[], |
|
| 413 | 413 | exports: &[ |
|
| 414 | - | binary::Export { name: "p::f", kind: binary::ExportKind::Function }, |
|
| 415 | - | binary::Export { name: FIRST, kind: binary::ExportKind::Data }, |
|
| 416 | - | binary::Export { name: SECOND, kind: binary::ExportKind::Data }, |
|
| 414 | + | binary::Export { |
|
| 415 | + | name: "p::f", |
|
| 416 | + | kind: binary::ExportKind::Function, |
|
| 417 | + | startable: false, |
|
| 418 | + | }, |
|
| 419 | + | binary::Export { |
|
| 420 | + | name: FIRST, |
|
| 421 | + | kind: binary::ExportKind::Data, |
|
| 422 | + | startable: false, |
|
| 423 | + | }, |
|
| 424 | + | binary::Export { |
|
| 425 | + | name: SECOND, |
|
| 426 | + | kind: binary::ExportKind::Data, |
|
| 427 | + | startable: false, |
|
| 428 | + | }, |
|
| 417 | 429 | ], |
|
| 418 | 430 | entry: nil, |
|
| 419 | 431 | program: il::Program { |
|
| 420 | 432 | data: &[ |
|
| 421 | 433 | il::Data { |
| 442 | 454 | fns: &fns[..], |
|
| 443 | 455 | layouts, |
|
| 444 | 456 | signatures: &[], |
|
| 445 | 457 | traits: &[], |
|
| 446 | 458 | }, |
|
| 459 | + | links: binary::EMPTY_LINKS, |
|
| 447 | 460 | }; |
|
| 448 | 461 | let arena = heap::over(&mut tests::ARENA[..]); |
|
| 449 | 462 | let scratch = heap::over(&mut tests::SCRATCH[..]); |
|
| 450 | 463 | return try shared::compile( |
|
| 451 | 464 | &input, |
lib/std/hash.rad
added
+3 -0
| 1 | + | //! Content digests. |
|
| 2 | + | ||
| 3 | + | export mod blake3; |
lib/std/hash/blake3.rad
added
+368 -0
| 1 | + | //! Streaming unkeyed BLAKE3 with 32-byte digests. |
|
| 2 | + | ||
| 3 | + | @test export mod tests; |
|
| 4 | + | ||
| 5 | + | use std::bits; |
|
| 6 | + | use std::mem; |
|
| 7 | + | ||
| 8 | + | /// Initial chaining words from the BLAKE3 specification. |
|
| 9 | + | constant IV: [u32; 8] = [ |
|
| 10 | + | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
|
| 11 | + | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, |
|
| 12 | + | ]; |
|
| 13 | + | /// Flag for the first block of a chunk. |
|
| 14 | + | constant CHUNK_START: u32 = 1; |
|
| 15 | + | /// Flag for the last block of a chunk. |
|
| 16 | + | constant CHUNK_END: u32 = 2; |
|
| 17 | + | /// Flag for a parent node of the hash tree. |
|
| 18 | + | constant PARENT: u32 = 4; |
|
| 19 | + | /// Flag for the root output of the hash tree. |
|
| 20 | + | constant ROOT: u32 = 8; |
|
| 21 | + | ||
| 22 | + | /// A 32-byte BLAKE3 digest. |
|
| 23 | + | export record Digest: Copy { |
|
| 24 | + | /// Digest bytes in output order. |
|
| 25 | + | bytes: [u8; 32], |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | /// Compression input for a chunk block or a parent node. |
|
| 29 | + | record Output: Copy { |
|
| 30 | + | /// Input chaining words. |
|
| 31 | + | words: [u32; 8], |
|
| 32 | + | /// Little-endian message words with zero padding. |
|
| 33 | + | block: [u32; 16], |
|
| 34 | + | /// Chunk number, or zero for a parent or root output. |
|
| 35 | + | counter: u64, |
|
| 36 | + | /// Number of message bytes in the block. |
|
| 37 | + | length: u32, |
|
| 38 | + | /// Node and block flags. |
|
| 39 | + | flags: u32, |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Incremental state for a message shorter than 2^64 bytes. |
|
| 43 | + | export opaque record State { |
|
| 44 | + | /// Current chunk chaining words. |
|
| 45 | + | words: [u32; 8], |
|
| 46 | + | /// Partial block, retained until more input or finalization. |
|
| 47 | + | block: [u8; 64], |
|
| 48 | + | /// Number of initialized bytes in the block. |
|
| 49 | + | used: u32, |
|
| 50 | + | /// Number of compressed blocks in the current chunk, from zero to 15. |
|
| 51 | + | blocks: u32, |
|
| 52 | + | /// Current chunk number. |
|
| 53 | + | counter: u64, |
|
| 54 | + | /// Number of input bytes. |
|
| 55 | + | length: u64, |
|
| 56 | + | /// Left subtree values, eight words each. 54 levels cover 2^64 bytes. |
|
| 57 | + | stack: [u32; 432], |
|
| 58 | + | /// Number of subtree values in the stack. |
|
| 59 | + | depth: u32, |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Initialize an unkeyed BLAKE3 message. |
|
| 63 | + | export fn new() -> State { |
|
| 64 | + | return State { |
|
| 65 | + | words: IV, |
|
| 66 | + | block: [0; 64], |
|
| 67 | + | used: 0, |
|
| 68 | + | blocks: 0, |
|
| 69 | + | counter: 0, |
|
| 70 | + | length: 0, |
|
| 71 | + | stack: [0; 432], |
|
| 72 | + | depth: 0, |
|
| 73 | + | }; |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Apply one BLAKE3 round to the columns and diagonals. Word sums wrap. |
|
| 77 | + | fn round(state: &mut [u32; 16], m: &[u32; 16]) { |
|
| 78 | + | let mut v0 = state[0]; |
|
| 79 | + | let mut v1 = state[1]; |
|
| 80 | + | let mut v2 = state[2]; |
|
| 81 | + | let mut v3 = state[3]; |
|
| 82 | + | let mut v4 = state[4]; |
|
| 83 | + | let mut v5 = state[5]; |
|
| 84 | + | let mut v6 = state[6]; |
|
| 85 | + | let mut v7 = state[7]; |
|
| 86 | + | let mut v8 = state[8]; |
|
| 87 | + | let mut v9 = state[9]; |
|
| 88 | + | let mut v10 = state[10]; |
|
| 89 | + | let mut v11 = state[11]; |
|
| 90 | + | let mut v12 = state[12]; |
|
| 91 | + | let mut v13 = state[13]; |
|
| 92 | + | let mut v14 = state[14]; |
|
| 93 | + | let mut v15 = state[15]; |
|
| 94 | + | set v0 += v4 + m[0]; |
|
| 95 | + | set v12 ^= v0; |
|
| 96 | + | set v12 = bits::rotateRight32(v12, 16); |
|
| 97 | + | set v8 += v12; |
|
| 98 | + | set v4 ^= v8; |
|
| 99 | + | set v4 = bits::rotateRight32(v4, 12); |
|
| 100 | + | set v0 += v4 + m[1]; |
|
| 101 | + | set v12 ^= v0; |
|
| 102 | + | set v12 = bits::rotateRight32(v12, 8); |
|
| 103 | + | set v8 += v12; |
|
| 104 | + | set v4 ^= v8; |
|
| 105 | + | set v4 = bits::rotateRight32(v4, 7); |
|
| 106 | + | set v1 += v5 + m[2]; |
|
| 107 | + | set v13 ^= v1; |
|
| 108 | + | set v13 = bits::rotateRight32(v13, 16); |
|
| 109 | + | set v9 += v13; |
|
| 110 | + | set v5 ^= v9; |
|
| 111 | + | set v5 = bits::rotateRight32(v5, 12); |
|
| 112 | + | set v1 += v5 + m[3]; |
|
| 113 | + | set v13 ^= v1; |
|
| 114 | + | set v13 = bits::rotateRight32(v13, 8); |
|
| 115 | + | set v9 += v13; |
|
| 116 | + | set v5 ^= v9; |
|
| 117 | + | set v5 = bits::rotateRight32(v5, 7); |
|
| 118 | + | set v2 += v6 + m[4]; |
|
| 119 | + | set v14 ^= v2; |
|
| 120 | + | set v14 = bits::rotateRight32(v14, 16); |
|
| 121 | + | set v10 += v14; |
|
| 122 | + | set v6 ^= v10; |
|
| 123 | + | set v6 = bits::rotateRight32(v6, 12); |
|
| 124 | + | set v2 += v6 + m[5]; |
|
| 125 | + | set v14 ^= v2; |
|
| 126 | + | set v14 = bits::rotateRight32(v14, 8); |
|
| 127 | + | set v10 += v14; |
|
| 128 | + | set v6 ^= v10; |
|
| 129 | + | set v6 = bits::rotateRight32(v6, 7); |
|
| 130 | + | set v3 += v7 + m[6]; |
|
| 131 | + | set v15 ^= v3; |
|
| 132 | + | set v15 = bits::rotateRight32(v15, 16); |
|
| 133 | + | set v11 += v15; |
|
| 134 | + | set v7 ^= v11; |
|
| 135 | + | set v7 = bits::rotateRight32(v7, 12); |
|
| 136 | + | set v3 += v7 + m[7]; |
|
| 137 | + | set v15 ^= v3; |
|
| 138 | + | set v15 = bits::rotateRight32(v15, 8); |
|
| 139 | + | set v11 += v15; |
|
| 140 | + | set v7 ^= v11; |
|
| 141 | + | set v7 = bits::rotateRight32(v7, 7); |
|
| 142 | + | set v0 += v5 + m[8]; |
|
| 143 | + | set v15 ^= v0; |
|
| 144 | + | set v15 = bits::rotateRight32(v15, 16); |
|
| 145 | + | set v10 += v15; |
|
| 146 | + | set v5 ^= v10; |
|
| 147 | + | set v5 = bits::rotateRight32(v5, 12); |
|
| 148 | + | set v0 += v5 + m[9]; |
|
| 149 | + | set v15 ^= v0; |
|
| 150 | + | set v15 = bits::rotateRight32(v15, 8); |
|
| 151 | + | set v10 += v15; |
|
| 152 | + | set v5 ^= v10; |
|
| 153 | + | set v5 = bits::rotateRight32(v5, 7); |
|
| 154 | + | set v1 += v6 + m[10]; |
|
| 155 | + | set v12 ^= v1; |
|
| 156 | + | set v12 = bits::rotateRight32(v12, 16); |
|
| 157 | + | set v11 += v12; |
|
| 158 | + | set v6 ^= v11; |
|
| 159 | + | set v6 = bits::rotateRight32(v6, 12); |
|
| 160 | + | set v1 += v6 + m[11]; |
|
| 161 | + | set v12 ^= v1; |
|
| 162 | + | set v12 = bits::rotateRight32(v12, 8); |
|
| 163 | + | set v11 += v12; |
|
| 164 | + | set v6 ^= v11; |
|
| 165 | + | set v6 = bits::rotateRight32(v6, 7); |
|
| 166 | + | set v2 += v7 + m[12]; |
|
| 167 | + | set v13 ^= v2; |
|
| 168 | + | set v13 = bits::rotateRight32(v13, 16); |
|
| 169 | + | set v8 += v13; |
|
| 170 | + | set v7 ^= v8; |
|
| 171 | + | set v7 = bits::rotateRight32(v7, 12); |
|
| 172 | + | set v2 += v7 + m[13]; |
|
| 173 | + | set v13 ^= v2; |
|
| 174 | + | set v13 = bits::rotateRight32(v13, 8); |
|
| 175 | + | set v8 += v13; |
|
| 176 | + | set v7 ^= v8; |
|
| 177 | + | set v7 = bits::rotateRight32(v7, 7); |
|
| 178 | + | set v3 += v4 + m[14]; |
|
| 179 | + | set v14 ^= v3; |
|
| 180 | + | set v14 = bits::rotateRight32(v14, 16); |
|
| 181 | + | set v9 += v14; |
|
| 182 | + | set v4 ^= v9; |
|
| 183 | + | set v4 = bits::rotateRight32(v4, 12); |
|
| 184 | + | set v3 += v4 + m[15]; |
|
| 185 | + | set v14 ^= v3; |
|
| 186 | + | set v14 = bits::rotateRight32(v14, 8); |
|
| 187 | + | set v9 += v14; |
|
| 188 | + | set v4 ^= v9; |
|
| 189 | + | set v4 = bits::rotateRight32(v4, 7); |
|
| 190 | + | set state[0] = v0; |
|
| 191 | + | set state[1] = v1; |
|
| 192 | + | set state[2] = v2; |
|
| 193 | + | set state[3] = v3; |
|
| 194 | + | set state[4] = v4; |
|
| 195 | + | set state[5] = v5; |
|
| 196 | + | set state[6] = v6; |
|
| 197 | + | set state[7] = v7; |
|
| 198 | + | set state[8] = v8; |
|
| 199 | + | set state[9] = v9; |
|
| 200 | + | set state[10] = v10; |
|
| 201 | + | set state[11] = v11; |
|
| 202 | + | set state[12] = v12; |
|
| 203 | + | set state[13] = v13; |
|
| 204 | + | set state[14] = v14; |
|
| 205 | + | set state[15] = v15; |
|
| 206 | + | } |
|
| 207 | + | ||
| 208 | + | /// Compress a node and return its first eight output words. |
|
| 209 | + | fn compress(input: &Output) -> [u32; 8] { |
|
| 210 | + | let mut state: [u32; 16] = [ |
|
| 211 | + | input.words[0], input.words[1], input.words[2], input.words[3], |
|
| 212 | + | input.words[4], input.words[5], input.words[6], input.words[7], |
|
| 213 | + | IV[0], IV[1], IV[2], IV[3], input.counter as u32, |
|
| 214 | + | (input.counter >> 32) as u32, input.length, input.flags, |
|
| 215 | + | ]; |
|
| 216 | + | let mut m = input.block; |
|
| 217 | + | for i in 0 as u32 .. 7 { |
|
| 218 | + | round(&mut state, &m); |
|
| 219 | + | if i < 6 { |
|
| 220 | + | // Permute the message words for the next round. The permutation |
|
| 221 | + | // has two cycles of eight words. |
|
| 222 | + | let first = m[0]; |
|
| 223 | + | set m[0] = m[2]; |
|
| 224 | + | set m[2] = m[3]; |
|
| 225 | + | set m[3] = m[10]; |
|
| 226 | + | set m[10] = m[12]; |
|
| 227 | + | set m[12] = m[9]; |
|
| 228 | + | set m[9] = m[11]; |
|
| 229 | + | set m[11] = m[5]; |
|
| 230 | + | set m[5] = first; |
|
| 231 | + | let second = m[1]; |
|
| 232 | + | set m[1] = m[6]; |
|
| 233 | + | set m[6] = m[4]; |
|
| 234 | + | set m[4] = m[7]; |
|
| 235 | + | set m[7] = m[13]; |
|
| 236 | + | set m[13] = m[14]; |
|
| 237 | + | set m[14] = m[15]; |
|
| 238 | + | set m[15] = m[8]; |
|
| 239 | + | set m[8] = second; |
|
| 240 | + | } |
|
| 241 | + | } |
|
| 242 | + | return [ |
|
| 243 | + | state[0] ^ state[8], state[1] ^ state[9], |
|
| 244 | + | state[2] ^ state[10], state[3] ^ state[11], |
|
| 245 | + | state[4] ^ state[12], state[5] ^ state[13], |
|
| 246 | + | state[6] ^ state[14], state[7] ^ state[15], |
|
| 247 | + | ]; |
|
| 248 | + | } |
|
| 249 | + | ||
| 250 | + | /// Read one complete block as little-endian words. |
|
| 251 | + | fn readBlock(bytes: &[u8]) -> [u32; 16] { |
|
| 252 | + | let mut words: [u32; 16] = [0; 16]; |
|
| 253 | + | for i in 0 as u32 .. 16 { |
|
| 254 | + | set words[i] = mem::load32Le(bytes, i * 4); |
|
| 255 | + | } |
|
| 256 | + | return words; |
|
| 257 | + | } |
|
| 258 | + | ||
| 259 | + | /// Join two subtree values into a parent compression input. |
|
| 260 | + | fn parent(left: &[u32], right: &[u32]) -> Output { |
|
| 261 | + | let mut block: [u32; 16] = [0; 16]; |
|
| 262 | + | for i in 0 as u32 .. 8 { |
|
| 263 | + | set block[i] = left[i]; |
|
| 264 | + | set block[i + 8] = right[i]; |
|
| 265 | + | } |
|
| 266 | + | return Output { words: IV, block, counter: 0, length: 64, flags: PARENT }; |
|
| 267 | + | } |
|
| 268 | + | ||
| 269 | + | /// Add message bytes. The complete message must be shorter than 2^64 bytes. |
|
| 270 | + | export fn update(state: &mut State, bytes: &[u8]) { |
|
| 271 | + | assert bytes.len as u64 <= 0xffffffffffffffff - state.length; |
|
| 272 | + | set state.length += bytes.len as u64; |
|
| 273 | + | let mut offset: u32 = 0; |
|
| 274 | + | while offset < bytes.len { |
|
| 275 | + | let flags = CHUNK_START if state.blocks == 0 else 0; |
|
| 276 | + | if state.used == 64 { |
|
| 277 | + | let mut output = Output { |
|
| 278 | + | words: state.words, |
|
| 279 | + | block: readBlock(&state.block[..]), |
|
| 280 | + | counter: state.counter, |
|
| 281 | + | length: 64, |
|
| 282 | + | flags, |
|
| 283 | + | }; |
|
| 284 | + | if state.blocks == 15 { |
|
| 285 | + | set output.flags |= CHUNK_END; |
|
| 286 | + | let mut value = compress(&output); |
|
| 287 | + | set state.counter += 1; |
|
| 288 | + | // Each trailing zero completes one left subtree. |
|
| 289 | + | let merges = bits::trailingZeros64(state.counter); |
|
| 290 | + | for _ in 0 .. merges { |
|
| 291 | + | set state.depth -= 1; |
|
| 292 | + | let start = state.depth * 8; |
|
| 293 | + | set value = compress(&parent(&state.stack[start .. start + 8], &value[..])); |
|
| 294 | + | } |
|
| 295 | + | let start = state.depth * 8; |
|
| 296 | + | for i in 0 as u32 .. 8 { |
|
| 297 | + | set state.stack[start + i] = value[i]; |
|
| 298 | + | } |
|
| 299 | + | set state.depth += 1; |
|
| 300 | + | set state.words = IV; |
|
| 301 | + | set state.blocks = 0; |
|
| 302 | + | } else { |
|
| 303 | + | set state.words = compress(&output); |
|
| 304 | + | set state.blocks += 1; |
|
| 305 | + | } |
|
| 306 | + | set state.used = 0; |
|
| 307 | + | continue; |
|
| 308 | + | } |
|
| 309 | + | // Full nonfinal blocks can be read from the input slice. |
|
| 310 | + | if state.used == 0 and bytes.len - offset > 64 and state.blocks < 15 { |
|
| 311 | + | set state.words = compress(&Output { |
|
| 312 | + | words: state.words, |
|
| 313 | + | block: readBlock(&bytes[offset .. offset + 64]), |
|
| 314 | + | counter: state.counter, |
|
| 315 | + | length: 64, |
|
| 316 | + | flags, |
|
| 317 | + | }); |
|
| 318 | + | set state.blocks += 1; |
|
| 319 | + | set offset += 64; |
|
| 320 | + | continue; |
|
| 321 | + | } |
|
| 322 | + | let space = 64 - state.used; |
|
| 323 | + | let remaining = bytes.len - offset; |
|
| 324 | + | let count = remaining if remaining < space else space; |
|
| 325 | + | try! mem::copy( |
|
| 326 | + | &mut state.block[state.used .. state.used + count], &bytes[offset .. offset + count] |
|
| 327 | + | ); |
|
| 328 | + | set state.used += count; |
|
| 329 | + | set offset += count; |
|
| 330 | + | } |
|
| 331 | + | } |
|
| 332 | + | ||
| 333 | + | /// Complete the final chunk and hash tree, then return the root digest. |
|
| 334 | + | export fn finish(input: State) -> Digest { |
|
| 335 | + | let mut state = input; |
|
| 336 | + | for i in state.used .. 64 { |
|
| 337 | + | set state.block[i] = 0; |
|
| 338 | + | } |
|
| 339 | + | let flags = CHUNK_END | (CHUNK_START if state.blocks == 0 else 0); |
|
| 340 | + | let mut output = Output { |
|
| 341 | + | words: state.words, |
|
| 342 | + | block: readBlock(&state.block[..]), |
|
| 343 | + | counter: state.counter, |
|
| 344 | + | length: state.used, |
|
| 345 | + | flags, |
|
| 346 | + | }; |
|
| 347 | + | while state.depth > 0 { |
|
| 348 | + | set state.depth -= 1; |
|
| 349 | + | let start = state.depth * 8; |
|
| 350 | + | let right = compress(&output); |
|
| 351 | + | set output = parent(&state.stack[start .. start + 8], &right[..]); |
|
| 352 | + | } |
|
| 353 | + | set output.counter = 0; |
|
| 354 | + | set output.flags |= ROOT; |
|
| 355 | + | let words = compress(&output); |
|
| 356 | + | let mut result = Digest { bytes: [0; 32] }; |
|
| 357 | + | for word, i in &words[..] { |
|
| 358 | + | mem::store32Le(&mut result.bytes[..], i * 4, word); |
|
| 359 | + | } |
|
| 360 | + | return result; |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | /// Compute the unkeyed digest of one byte sequence. |
|
| 364 | + | export fn digest(bytes: &[u8]) -> Digest { |
|
| 365 | + | let mut state = new(); |
|
| 366 | + | update(&mut state, bytes); |
|
| 367 | + | return finish(state); |
|
| 368 | + | } |
lib/std/hash/blake3/tests.rad
added
+206 -0
| 1 | + | //! BLAKE3 official unkeyed vectors and streaming boundaries. |
|
| 2 | + | ||
| 3 | + | use std::fmt; |
|
| 4 | + | use std::hash::blake3; |
|
| 5 | + | use std::mem; |
|
| 6 | + | use std::testing; |
|
| 7 | + | ||
| 8 | + | /// Compare the 32 digest bytes with a hexadecimal test vector. |
|
| 9 | + | fn expect(value: &blake3::Digest, expected: &[u8]) { |
|
| 10 | + | let mut text: [u8; 64] = [0; 64]; |
|
| 11 | + | assert mem::eq(fmt::formatHex(&value.bytes[..], &mut text[..]), expected); |
|
| 12 | + | } |
|
| 13 | + | ||
| 14 | + | /// Check official vectors through chunk boundaries and unbalanced trees. |
|
| 15 | + | @test fn vectors() throws (testing::TestError) { |
|
| 16 | + | let mut input: [u8; 102400] = [0; 102400]; |
|
| 17 | + | for i in 0 .. input.len { |
|
| 18 | + | set input[i] = (i % 251) as u8; |
|
| 19 | + | } |
|
| 20 | + | expect( |
|
| 21 | + | &blake3::digest(&input[.. 0]), |
|
| 22 | + | "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262" |
|
| 23 | + | ); |
|
| 24 | + | expect( |
|
| 25 | + | &blake3::digest(&input[.. 1]), |
|
| 26 | + | "2d3adedff11b61f14c886e35afa036736dcd87a74d27b5c1510225d0f592e213" |
|
| 27 | + | ); |
|
| 28 | + | expect( |
|
| 29 | + | &blake3::digest(&input[.. 2]), |
|
| 30 | + | "7b7015bb92cf0b318037702a6cdd81dee41224f734684c2c122cd6359cb1ee63" |
|
| 31 | + | ); |
|
| 32 | + | expect( |
|
| 33 | + | &blake3::digest(&input[.. 3]), |
|
| 34 | + | "e1be4d7a8ab5560aa4199eea339849ba8e293d55ca0a81006726d184519e647f" |
|
| 35 | + | ); |
|
| 36 | + | expect( |
|
| 37 | + | &blake3::digest(&input[.. 4]), |
|
| 38 | + | "f30f5ab28fe047904037f77b6da4fea1e27241c5d132638d8bedce9d40494f32" |
|
| 39 | + | ); |
|
| 40 | + | expect( |
|
| 41 | + | &blake3::digest(&input[.. 5]), |
|
| 42 | + | "b40b44dfd97e7a84a996a91af8b85188c66c126940ba7aad2e7ae6b385402aa2" |
|
| 43 | + | ); |
|
| 44 | + | expect( |
|
| 45 | + | &blake3::digest(&input[.. 6]), |
|
| 46 | + | "06c4e8ffb6872fad96f9aaca5eee1553eb62aed0ad7198cef42e87f6a616c844" |
|
| 47 | + | ); |
|
| 48 | + | expect( |
|
| 49 | + | &blake3::digest(&input[.. 7]), |
|
| 50 | + | "3f8770f387faad08faa9d8414e9f449ac68e6ff0417f673f602a646a891419fe" |
|
| 51 | + | ); |
|
| 52 | + | expect( |
|
| 53 | + | &blake3::digest(&input[.. 8]), |
|
| 54 | + | "2351207d04fc16ade43ccab08600939c7c1fa70a5c0aaca76063d04c3228eaeb" |
|
| 55 | + | ); |
|
| 56 | + | expect( |
|
| 57 | + | &blake3::digest(&input[.. 63]), |
|
| 58 | + | "e9bc37a594daad83be9470df7f7b3798297c3d834ce80ba85d6e207627b7db7b" |
|
| 59 | + | ); |
|
| 60 | + | expect( |
|
| 61 | + | &blake3::digest(&input[.. 64]), |
|
| 62 | + | "4eed7141ea4a5cd4b788606bd23f46e212af9cacebacdc7d1f4c6dc7f2511b98" |
|
| 63 | + | ); |
|
| 64 | + | expect( |
|
| 65 | + | &blake3::digest(&input[.. 65]), |
|
| 66 | + | "de1e5fa0be70df6d2be8fffd0e99ceaa8eb6e8c93a63f2d8d1c30ecb6b263dee" |
|
| 67 | + | ); |
|
| 68 | + | expect( |
|
| 69 | + | &blake3::digest(&input[.. 127]), |
|
| 70 | + | "d81293fda863f008c09e92fc382a81f5a0b4a1251cba1634016a0f86a6bd640d" |
|
| 71 | + | ); |
|
| 72 | + | expect( |
|
| 73 | + | &blake3::digest(&input[.. 128]), |
|
| 74 | + | "f17e570564b26578c33bb7f44643f539624b05df1a76c81f30acd548c44b45ef" |
|
| 75 | + | ); |
|
| 76 | + | expect( |
|
| 77 | + | &blake3::digest(&input[.. 129]), |
|
| 78 | + | "683aaae9f3c5ba37eaaf072aed0f9e30bac0865137bae68b1fde4ca2aebdcb12" |
|
| 79 | + | ); |
|
| 80 | + | expect( |
|
| 81 | + | &blake3::digest(&input[.. 1023]), |
|
| 82 | + | "10108970eeda3eb932baac1428c7a2163b0e924c9a9e25b35bba72b28f70bd11" |
|
| 83 | + | ); |
|
| 84 | + | expect( |
|
| 85 | + | &blake3::digest(&input[.. 1024]), |
|
| 86 | + | "42214739f095a406f3fc83deb889744ac00df831c10daa55189b5d121c855af7" |
|
| 87 | + | ); |
|
| 88 | + | expect( |
|
| 89 | + | &blake3::digest(&input[.. 1025]), |
|
| 90 | + | "d00278ae47eb27b34faecf67b4fe263f82d5412916c1ffd97c8cb7fb814b8444" |
|
| 91 | + | ); |
|
| 92 | + | expect( |
|
| 93 | + | &blake3::digest(&input[.. 2048]), |
|
| 94 | + | "e776b6028c7cd22a4d0ba182a8bf62205d2ef576467e838ed6f2529b85fba24a" |
|
| 95 | + | ); |
|
| 96 | + | expect( |
|
| 97 | + | &blake3::digest(&input[.. 2049]), |
|
| 98 | + | "5f4d72f40d7a5f82b15ca2b2e44b1de3c2ef86c426c95c1af0b6879522563030" |
|
| 99 | + | ); |
|
| 100 | + | expect( |
|
| 101 | + | &blake3::digest(&input[.. 3072]), |
|
| 102 | + | "b98cb0ff3623be03326b373de6b9095218513e64f1ee2edd2525c7ad1e5cffd2" |
|
| 103 | + | ); |
|
| 104 | + | expect( |
|
| 105 | + | &blake3::digest(&input[.. 3073]), |
|
| 106 | + | "7124b49501012f81cc7f11ca069ec9226cecb8a2c850cfe644e327d22d3e1cd3" |
|
| 107 | + | ); |
|
| 108 | + | expect( |
|
| 109 | + | &blake3::digest(&input[.. 4096]), |
|
| 110 | + | "015094013f57a5277b59d8475c0501042c0b642e531b0a1c8f58d2163229e969" |
|
| 111 | + | ); |
|
| 112 | + | expect( |
|
| 113 | + | &blake3::digest(&input[.. 4097]), |
|
| 114 | + | "9b4052b38f1c5fc8b1f9ff7ac7b27cd242487b3d890d15c96a1c25b8aa0fb995" |
|
| 115 | + | ); |
|
| 116 | + | expect( |
|
| 117 | + | &blake3::digest(&input[.. 5120]), |
|
| 118 | + | "9cadc15fed8b5d854562b26a9536d9707cadeda9b143978f319ab34230535833" |
|
| 119 | + | ); |
|
| 120 | + | expect( |
|
| 121 | + | &blake3::digest(&input[.. 5121]), |
|
| 122 | + | "628bd2cb2004694adaab7bbd778a25df25c47b9d4155a55f8fbd79f2fe154cff" |
|
| 123 | + | ); |
|
| 124 | + | expect( |
|
| 125 | + | &blake3::digest(&input[.. 6144]), |
|
| 126 | + | "3e2e5b74e048f3add6d21faab3f83aa44d3b2278afb83b80b3c35164ebeca205" |
|
| 127 | + | ); |
|
| 128 | + | expect( |
|
| 129 | + | &blake3::digest(&input[.. 6145]), |
|
| 130 | + | "f1323a8631446cc50536a9f705ee5cb619424d46887f3c376c695b70e0f0507f" |
|
| 131 | + | ); |
|
| 132 | + | expect( |
|
| 133 | + | &blake3::digest(&input[.. 7168]), |
|
| 134 | + | "61da957ec2499a95d6b8023e2b0e604ec7f6b50e80a9678b89d2628e99ada77a" |
|
| 135 | + | ); |
|
| 136 | + | expect( |
|
| 137 | + | &blake3::digest(&input[.. 7169]), |
|
| 138 | + | "a003fc7a51754a9b3c7fae0367ab3d782dccf28855a03d435f8cfe74605e7817" |
|
| 139 | + | ); |
|
| 140 | + | expect( |
|
| 141 | + | &blake3::digest(&input[.. 8192]), |
|
| 142 | + | "aae792484c8efe4f19e2ca7d371d8c467ffb10748d8a5a1ae579948f718a2a63" |
|
| 143 | + | ); |
|
| 144 | + | expect( |
|
| 145 | + | &blake3::digest(&input[.. 8193]), |
|
| 146 | + | "bab6c09cb8ce8cf459261398d2e7aef35700bf488116ceb94a36d0f5f1b7bc3b" |
|
| 147 | + | ); |
|
| 148 | + | expect( |
|
| 149 | + | &blake3::digest(&input[.. 16384]), |
|
| 150 | + | "f875d6646de28985646f34ee13be9a576fd515f76b5b0a26bb324735041ddde4" |
|
| 151 | + | ); |
|
| 152 | + | expect( |
|
| 153 | + | &blake3::digest(&input[.. 31744]), |
|
| 154 | + | "62b6960e1a44bcc1eb1a611a8d6235b6b4b78f32e7abc4fb4c6cdcce94895c47" |
|
| 155 | + | ); |
|
| 156 | + | expect( |
|
| 157 | + | &blake3::digest(&input[.. 102400]), |
|
| 158 | + | "bc3e3d41a1146b069abffad3c0d44860cf664390afce4d9661f7902e7943e085" |
|
| 159 | + | ); |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Updates preserve partial blocks, final chunks, and subtree order. |
|
| 163 | + | @test fn streaming() throws (testing::TestError) { |
|
| 164 | + | let mut input: [u8; 8193] = [0; 8193]; |
|
| 165 | + | for i in 0 .. input.len { |
|
| 166 | + | set input[i] = (i % 251) as u8; |
|
| 167 | + | } |
|
| 168 | + | let lengths: [u32; 16] = [ |
|
| 169 | + | 0, 1, 63, 64, 65, 1023, 1024, 1025, 2047, 2048, 2049, 3072, 4096, 4097, 8192, 8193 |
|
| 170 | + | ]; |
|
| 171 | + | let steps: [u32; 9] = [1, 63, 64, 65, 1023, 1024, 1025, 2048, 4096]; |
|
| 172 | + | for length in &lengths[..] { |
|
| 173 | + | let expected = blake3::digest(&input[.. length]); |
|
| 174 | + | for step in &steps[..] { |
|
| 175 | + | let mut state = blake3::new(); |
|
| 176 | + | let mut offset: u32 = 0; |
|
| 177 | + | while offset < length { |
|
| 178 | + | let end = offset + step if offset + step < length else length; |
|
| 179 | + | blake3::update(&mut state, &input[offset .. end]); |
|
| 180 | + | blake3::update(&mut state, &[]); |
|
| 181 | + | set offset = end; |
|
| 182 | + | } |
|
| 183 | + | let actual = blake3::finish(state); |
|
| 184 | + | assert mem::eq(&actual.bytes[..], &expected.bytes[..]); |
|
| 185 | + | } |
|
| 186 | + | } |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Input alignment preserves full blocks, partial blocks and chunk boundaries. |
|
| 190 | + | @test fn inputAlignment() throws (testing::TestError) { |
|
| 191 | + | let mut input: [u8; 4097] = [0; 4097]; |
|
| 192 | + | let mut shifted: [u8; 4104] = [0; 4104]; |
|
| 193 | + | for i in 0 .. input.len { |
|
| 194 | + | set input[i] = (i % 251) as u8; |
|
| 195 | + | } |
|
| 196 | + | for length in &[63 as u32, 64, 65, 1023, 1024, 1025, 4097] { |
|
| 197 | + | let expected = blake3::digest(&input[.. length]); |
|
| 198 | + | for offset in 0 .. 8 { |
|
| 199 | + | try! mem::copy( |
|
| 200 | + | &mut shifted[offset .. offset + length], &input[.. length] |
|
| 201 | + | ); |
|
| 202 | + | let actual = blake3::digest(&shifted[offset .. offset + length]); |
|
| 203 | + | assert mem::eq(&actual.bytes[..], &expected.bytes[..]); |
|
| 204 | + | } |
|
| 205 | + | } |
|
| 206 | + | } |
lib/std/lang/il.rad
+2 -1
| 74 | 74 | // TODO: Blocks should have an instruction in `Instr`. |
|
| 75 | 75 | ||
| 76 | 76 | export mod printer; |
|
| 77 | 77 | export mod binary; |
|
| 78 | 78 | export mod verify; |
|
| 79 | + | export mod link; |
|
| 79 | 80 | ||
| 80 | - | use std::mem::heap; |
|
| 81 | 81 | use std::lang::alloc::AllocError; |
|
| 82 | + | use std::mem::heap; |
|
| 82 | 83 | ||
| 83 | 84 | /// DeviceAccess flag bits that contain the byte width. |
|
| 84 | 85 | export constant DEVICE_WIDTH_MASK: u64 = 0xff; |
|
| 85 | 86 | /// DeviceAccess flag that selects a register write. |
|
| 86 | 87 | export constant DEVICE_WRITE: u64 = 0x100; |
lib/std/lang/il/binary.rad
+63 -1
| 5 | 5 | export mod writer; |
|
| 6 | 6 | export mod reader; |
|
| 7 | 7 | export mod program; |
|
| 8 | 8 | export mod collect; |
|
| 9 | 9 | export mod cfg; |
|
| 10 | + | export mod linkage; |
|
| 10 | 11 | ||
| 11 | 12 | use std::lang::il; |
|
| 12 | 13 | @test export mod tests; |
|
| 13 | 14 | ||
| 14 | 15 | /// Minimum wire bytes in a register and value-type parameter. |
| 22 | 23 | /// Minimum wire bytes in a global data record. |
|
| 23 | 24 | export constant MIN_DATA_BYTES: u32 = 15; |
|
| 24 | 25 | /// Minimum wire bytes in a repeated data value. |
|
| 25 | 26 | export constant MIN_DATA_VALUE_BYTES: u32 = 5; |
|
| 26 | 27 | /// Minimum wire bytes in a function record. |
|
| 27 | - | export constant MIN_FUNCTION_BYTES: u32 = 18; |
|
| 28 | + | export constant MIN_FUNCTION_BYTES: u32 = 22; |
|
| 28 | 29 | /// Minimum wire bytes in a block record. |
|
| 29 | 30 | export constant MIN_BLOCK_BYTES: u32 = 13; |
|
| 30 | 31 | /// Minimum wire bytes in an exported symbol. |
|
| 31 | 32 | export constant MIN_EXPORT_BYTES: u32 = 5; |
|
| 32 | 33 | /// Minimum wire bytes in a layout record. |
| 285 | 286 | export record Export: Copy { |
|
| 286 | 287 | /// Qualified symbol name. |
|
| 287 | 288 | name: *[u8], |
|
| 288 | 289 | /// Definition kind. |
|
| 289 | 290 | kind: ExportKind, |
|
| 291 | + | /// Context-entry eligibility, derived by package verification. |
|
| 292 | + | startable: bool, |
|
| 290 | 293 | } |
|
| 291 | 294 | ||
| 295 | + | /// A 256-bit content identity. |
|
| 296 | + | export record Id: Copy { |
|
| 297 | + | /// Digest bytes in network order. |
|
| 298 | + | bytes: [u8; 32], |
|
| 299 | + | } |
|
| 300 | + | ||
| 301 | + | /// Type expected at an imported symbol. |
|
| 302 | + | export union ImportKind: Copy { |
|
| 303 | + | /// Index of the expected signature in the importing program. |
|
| 304 | + | Function(u32), |
|
| 305 | + | /// Expected data layout and access rights. |
|
| 306 | + | Data { |
|
| 307 | + | /// Index in the importing program's layout table. |
|
| 308 | + | layout: u32, |
|
| 309 | + | /// Whether the data can only be read. |
|
| 310 | + | readOnly: bool, |
|
| 311 | + | /// Heap handle rights, when this names a heap. |
|
| 312 | + | heap: ?il::HeapType, |
|
| 313 | + | }, |
|
| 314 | + | } |
|
| 315 | + | ||
| 316 | + | /// An explicit symbol binding in one importing package. |
|
| 317 | + | export record Import: Copy { |
|
| 318 | + | /// Unique local symbol alias used by the package's instructions. |
|
| 319 | + | name: *[u8], |
|
| 320 | + | /// Index in the dependency and pin tables. |
|
| 321 | + | dependency: u32, |
|
| 322 | + | /// Public export key in the selected dependency. |
|
| 323 | + | exported: *[u8], |
|
| 324 | + | /// Expected call or data contract. |
|
| 325 | + | kind: ImportKind, |
|
| 326 | + | } |
|
| 327 | + | ||
| 328 | + | /// Link declarations and a derived verification classification. |
|
| 329 | + | export record Links: Copy { |
|
| 330 | + | /// Exact identities in dependency order. Loadable dependencies require pins. |
|
| 331 | + | pins: *[Id], |
|
| 332 | + | /// Explicit local aliases for imported definitions. |
|
| 333 | + | imports: *[Import], |
|
| 334 | + | /// Exact payload identity, computed after complete binary decoding. |
|
| 335 | + | /// Constructed IL has no identity until it has an encoded payload. |
|
| 336 | + | image: ?Id, |
|
| 337 | + | /// Whether package verification checked all local bodies and exact import bindings. |
|
| 338 | + | /// This field is derived and is never decoded from the wire. |
|
| 339 | + | verified: bool, |
|
| 340 | + | } |
|
| 341 | + | ||
| 342 | + | /// Link metadata of a package with no dependencies. |
|
| 343 | + | export constant EMPTY_LINKS: Links = Links { |
|
| 344 | + | pins: &[], imports: &[], image: nil, verified: false |
|
| 345 | + | }; |
|
| 346 | + | ||
| 347 | + | /// Supported RIL package semantics and envelope encoding. |
|
| 348 | + | export constant FORMAT_VERSION: u32 = 1; |
|
| 349 | + | /// RV64 call, layout, and domain-state ABI of loadable packages. |
|
| 350 | + | export constant ABI_VERSION: u32 = 1; |
|
| 351 | + | ||
| 292 | 352 | /// A binary package and its IL definitions. |
|
| 293 | 353 | /// Referenced storage must remain valid and immutable while the package is read. |
|
| 294 | 354 | export record Package: Copy { |
|
| 295 | 355 | /// Unique symbol names in wire-index order. |
|
| 296 | 356 | symbols: *[*[u8]], |
| 302 | 362 | exports: *[Export], |
|
| 303 | 363 | /// Default exported entry, if the package has one. |
|
| 304 | 364 | entry: ?*[u8], |
|
| 305 | 365 | /// Package-local functions and global data. |
|
| 306 | 366 | program: il::Program, |
|
| 367 | + | /// Exact dependency identities and typed import bindings. |
|
| 368 | + | links: Links, |
|
| 307 | 369 | } |
|
| 308 | 370 | ||
| 309 | 371 | /// Structural limits supplied by the consumer of decoded IL. |
|
| 310 | 372 | export record Limits: Copy { |
|
| 311 | 373 | /// Exclusive upper bound for SSA register numbers. |
lib/std/lang/il/binary/collect.rad
+84 -93
| 1 | - | //! Deterministic symbol and dependency collection for package-local IL. |
|
| 1 | + | //! Deterministic symbol collection for package-local IL. |
|
| 2 | 2 | ||
| 3 | - | use std::mem; |
|
| 3 | + | use std::collections::dict; |
|
| 4 | + | use std::lang::alloc; |
|
| 4 | 5 | use std::lang::il; |
|
| 5 | 6 | use std::lang::il::binary; |
|
| 6 | - | use std::lang::alloc; |
|
| 7 | + | use std::mem; |
|
| 7 | 8 | use std::mem::heap; |
|
| 8 | - | use std::collections::dict; |
|
| 9 | 9 | ||
| 10 | 10 | /// Number of slots of the name index. It must be a power of two. The index |
|
| 11 | 11 | /// takes at most half of its slots, which bounds the names of one package. |
|
| 12 | 12 | export constant INDEX_SLOTS: u32 = 32768; |
|
| 13 | 13 |
| 16 | 16 | /// Names are copied into that allocator, so callers may release their own |
|
| 17 | 17 | /// storage after each call. |
|
| 18 | 18 | export record Names { |
|
| 19 | 19 | /// Unique names in first-use order. |
|
| 20 | 20 | symbols: *mut [*[u8]], |
|
| 21 | - | /// Unique dependency names in first-use order. |
|
| 22 | - | dependencies: *mut [*[u8]], |
|
| 23 | 21 | /// Maps each name to its position in `symbols`. |
|
| 24 | 22 | index: dict::Dict, |
|
| 25 | 23 | } |
|
| 26 | 24 | ||
| 27 | 25 | /// Start collection with empty tables. |
| 29 | 27 | export fn new(slots: u32, a: &heap::Heap) -> Names throws (binary::Error) { |
|
| 30 | 28 | let mut table: *mut [dict::Entry] = &mut []; |
|
| 31 | 29 | try table.resize(slots, dict::Entry { key: &[], value: 0 }, a) catch { |
|
| 32 | 30 | throw binary::Error::Storage; |
|
| 33 | 31 | }; |
|
| 34 | - | return Names { symbols: &mut [], dependencies: &mut [], index: dict::fresh(table) }; |
|
| 32 | + | return Names { symbols: &mut [], index: dict::fresh(table) }; |
|
| 35 | 33 | } |
|
| 36 | 34 | ||
| 37 | 35 | /// Add a unique name and return the stored copy. |
|
| 38 | - | fn add(names: &mut Names, name: *[u8], a: &heap::Heap) -> *[u8] |
|
| 36 | + | export fn intern(names: &mut Names, name: *[u8], a: &heap::Heap) -> *[u8] |
|
| 39 | 37 | throws (binary::Error) |
|
| 40 | 38 | { |
|
| 41 | 39 | if name.len == 0 { |
|
| 42 | 40 | throw binary::Error::Invalid; |
|
| 43 | 41 | } |
| 55 | 53 | throw binary::Error::Storage; |
|
| 56 | 54 | }; |
|
| 57 | 55 | return stored; |
|
| 58 | 56 | } |
|
| 59 | 57 | ||
| 60 | - | /// Add a qualified reference and its owning package dependency. |
|
| 61 | - | fn reference( |
|
| 62 | - | names: &mut Names, |
|
| 63 | - | owner: *[u8], |
|
| 64 | - | name: *[u8], |
|
| 65 | - | a: &heap::Heap |
|
| 66 | - | ) throws (binary::Error) { |
|
| 67 | - | let stored = try add(names, name, a); |
|
| 58 | + | /// Add a qualified reference with a nonempty package and local name. |
|
| 59 | + | fn reference(names: &mut Names, name: *[u8], a: &heap::Heap) |
|
| 60 | + | throws (binary::Error) |
|
| 61 | + | { |
|
| 62 | + | let stored = try intern(names, name, a); |
|
| 68 | 63 | for i in 0 .. stored.len { |
|
| 69 | 64 | if stored[i] == ':' and i + 1 < stored.len and stored[i + 1] == ':' { |
|
| 70 | 65 | if i == 0 or i + 2 == stored.len { |
|
| 71 | 66 | throw binary::Error::Invalid; |
|
| 72 | 67 | } |
|
| 73 | - | let dependency = &stored[.. i]; |
|
| 74 | - | if mem::eq(dependency, owner) { |
|
| 75 | - | return; |
|
| 76 | - | } |
|
| 77 | - | let known = try add(names, dependency, a); |
|
| 78 | - | for other in &names.dependencies[..] { |
|
| 79 | - | if mem::eq(other, known) { |
|
| 80 | - | return; |
|
| 81 | - | } |
|
| 82 | - | } |
|
| 83 | - | try names.dependencies.append(known, a) catch { |
|
| 84 | - | throw binary::Error::Storage; |
|
| 85 | - | }; |
|
| 86 | 68 | return; |
|
| 87 | 69 | } |
|
| 88 | 70 | } |
|
| 89 | 71 | throw binary::Error::Invalid; |
|
| 90 | 72 | } |
|
| 91 | 73 | ||
| 92 | 74 | /// Collect names referenced by one IL value. |
|
| 93 | - | fn value(names: &mut Names, owner: *[u8], item: il::Val, a: &heap::Heap) |
|
| 75 | + | fn value(names: &mut Names, item: il::Val, a: &heap::Heap) |
|
| 94 | 76 | throws (binary::Error) |
|
| 95 | 77 | { |
|
| 96 | 78 | match item { |
|
| 97 | - | case il::Val::DataSym(name) => try reference(names, owner, name, a), |
|
| 98 | - | case il::Val::FnAddr(name) => try reference(names, owner, name, a), |
|
| 79 | + | case il::Val::DataSym(name) => try reference(names, name, a), |
|
| 80 | + | case il::Val::FnAddr(name) => try reference(names, name, a), |
|
| 99 | 81 | else => {} |
|
| 100 | 82 | } |
|
| 101 | 83 | } |
|
| 102 | 84 | ||
| 103 | 85 | /// Collect names from an instruction's variable-length operand sequence. |
|
| 104 | - | fn values(names: &mut Names, owner: *[u8], items: &[il::Val], a: &heap::Heap) |
|
| 86 | + | fn values(names: &mut Names, items: &[il::Val], a: &heap::Heap) |
|
| 105 | 87 | throws (binary::Error) |
|
| 106 | 88 | { |
|
| 107 | 89 | for item in items { |
|
| 108 | - | try value(names, owner, item, a); |
|
| 90 | + | try value(names, item, a); |
|
| 109 | 91 | } |
|
| 110 | 92 | } |
|
| 111 | 93 | ||
| 112 | 94 | /// Collect all symbolic instruction operands of `func`. |
|
| 113 | 95 | fn instruction( |
|
| 114 | 96 | names: &mut Names, |
|
| 115 | - | owner: *[u8], |
|
| 116 | 97 | func: &il::Fn, |
|
| 117 | 98 | instr: il::Instr, |
|
| 118 | 99 | a: &heap::Heap |
|
| 119 | 100 | ) throws (binary::Error) { |
|
| 120 | 101 | match instr { |
|
| 121 | - | case il::Instr::Store { src, .. } => try value(names, owner, src, a), |
|
| 122 | - | case il::Instr::Copy { val, .. } => try value(names, owner, val, a), |
|
| 123 | - | case il::Instr::Zext { val, .. } => try value(names, owner, val, a), |
|
| 124 | - | case il::Instr::Sext { val, .. } => try value(names, owner, val, a), |
|
| 102 | + | case il::Instr::Store { src, .. } => try value(names, src, a), |
|
| 103 | + | case il::Instr::Copy { val, .. } => try value(names, val, a), |
|
| 104 | + | case il::Instr::Zext { val, .. } => try value(names, val, a), |
|
| 105 | + | case il::Instr::Sext { val, .. } => try value(names, val, a), |
|
| 125 | 106 | case il::Instr::BinOp { a: left, b: right, .. } => { |
|
| 126 | - | try value(names, owner, left, a); |
|
| 127 | - | try value(names, owner, right, a); |
|
| 107 | + | try value(names, left, a); |
|
| 108 | + | try value(names, right, a); |
|
| 128 | 109 | } |
|
| 129 | - | case il::Instr::UnOp { a: operand, .. } => try value(names, owner, operand, a), |
|
| 110 | + | case il::Instr::UnOp { a: operand, .. } => try value(names, operand, a), |
|
| 130 | 111 | case il::Instr::Call { func: callee, args, .. } => { |
|
| 131 | - | try value(names, owner, callee, a); |
|
| 132 | - | try values(names, owner, il::argVals(func, args), a); |
|
| 112 | + | try value(names, callee, a); |
|
| 113 | + | try values(names, il::argVals(func, args), a); |
|
| 133 | 114 | } |
|
| 134 | 115 | case il::Instr::Ret { val } => { |
|
| 135 | 116 | if let item = val { |
|
| 136 | - | try value(names, owner, item, a); |
|
| 117 | + | try value(names, item, a); |
|
| 137 | 118 | } |
|
| 138 | 119 | } |
|
| 139 | - | case il::Instr::Jmp { args, .. } => try values(names, owner, il::argVals(func, args), a), |
|
| 120 | + | case il::Instr::Jmp { args, .. } => try values(names, il::argVals(func, args), a), |
|
| 140 | 121 | case il::Instr::Br { a: left, b: right, thenArgs, elseArgs, .. } => { |
|
| 141 | - | try value(names, owner, left, a); |
|
| 142 | - | try value(names, owner, right, a); |
|
| 143 | - | try values(names, owner, il::argVals(func, thenArgs), a); |
|
| 144 | - | try values(names, owner, il::argVals(func, elseArgs), a); |
|
| 122 | + | try value(names, left, a); |
|
| 123 | + | try value(names, right, a); |
|
| 124 | + | try values(names, il::argVals(func, thenArgs), a); |
|
| 125 | + | try values(names, il::argVals(func, elseArgs), a); |
|
| 145 | 126 | } |
|
| 146 | 127 | case il::Instr::Switch { val, defaultArgs, cases, .. } => { |
|
| 147 | - | try value(names, owner, val, a); |
|
| 148 | - | try values(names, owner, il::argVals(func, defaultArgs), a); |
|
| 128 | + | try value(names, val, a); |
|
| 129 | + | try values(names, il::argVals(func, defaultArgs), a); |
|
| 149 | 130 | for branch in il::switchCases(func, cases) { |
|
| 150 | - | try values(names, owner, il::argVals(func, branch.args), a); |
|
| 131 | + | try values(names, il::argVals(func, branch.args), a); |
|
| 151 | 132 | } |
|
| 152 | 133 | } |
|
| 153 | 134 | case il::Instr::Ecall { num, a0, a1, a2, a3, .. } => { |
|
| 154 | - | try value(names, owner, num, a); |
|
| 155 | - | try value(names, owner, a0, a); |
|
| 156 | - | try value(names, owner, a1, a); |
|
| 157 | - | try value(names, owner, a2, a); |
|
| 158 | - | try value(names, owner, a3, a); |
|
| 135 | + | try value(names, num, a); |
|
| 136 | + | try value(names, a0, a); |
|
| 137 | + | try value(names, a1, a); |
|
| 138 | + | try value(names, a2, a); |
|
| 139 | + | try value(names, a3, a); |
|
| 159 | 140 | } |
|
| 160 | - | case il::Instr::PageSlice { handle, .. } => try value(names, owner, handle, a), |
|
| 161 | - | case il::Instr::PageHeap { handle, .. } => try value(names, owner, handle, a), |
|
| 162 | - | case il::Instr::HeapReset { heap } => try value(names, owner, heap, a), |
|
| 141 | + | case il::Instr::PageSlice { handle, .. } => try value(names, handle, a), |
|
| 142 | + | case il::Instr::PageHeap { handle, .. } => try value(names, handle, a), |
|
| 143 | + | case il::Instr::HeapReset { heap } => try value(names, heap, a), |
|
| 163 | 144 | case il::Instr::DeviceRead { handle, offset, .. } => { |
|
| 164 | - | try value(names, owner, handle, a); |
|
| 165 | - | try value(names, owner, offset, a); |
|
| 145 | + | try value(names, handle, a); |
|
| 146 | + | try value(names, offset, a); |
|
| 166 | 147 | } |
|
| 167 | 148 | case il::Instr::DeviceWrite { handle, offset, value: source, .. } => { |
|
| 168 | - | try value(names, owner, handle, a); |
|
| 169 | - | try value(names, owner, offset, a); |
|
| 170 | - | try value(names, owner, source, a); |
|
| 149 | + | try value(names, handle, a); |
|
| 150 | + | try value(names, offset, a); |
|
| 151 | + | try value(names, source, a); |
|
| 171 | 152 | } |
|
| 172 | 153 | case il::Instr::SliceNew { base, start, end, .. } => { |
|
| 173 | - | try value(names, owner, base, a); |
|
| 174 | - | try value(names, owner, start, a); |
|
| 175 | - | try value(names, owner, end, a); |
|
| 154 | + | try value(names, base, a); |
|
| 155 | + | try value(names, start, a); |
|
| 156 | + | try value(names, end, a); |
|
| 176 | 157 | } |
|
| 177 | 158 | case il::Instr::SliceSub { start, end, .. } => { |
|
| 178 | - | try value(names, owner, start, a); |
|
| 179 | - | try value(names, owner, end, a); |
|
| 159 | + | try value(names, start, a); |
|
| 160 | + | try value(names, end, a); |
|
| 180 | 161 | } |
|
| 181 | 162 | case il::Instr::SliceGrow { heap, capacity, .. } => { |
|
| 182 | - | try value(names, owner, heap, a); |
|
| 183 | - | try value(names, owner, capacity, a); |
|
| 163 | + | try value(names, heap, a); |
|
| 164 | + | try value(names, capacity, a); |
|
| 184 | 165 | } |
|
| 185 | - | case il::Instr::SliceLen { length, .. } => try value(names, owner, length, a), |
|
| 186 | - | case il::Instr::Elem { index, .. } => try value(names, owner, index, a), |
|
| 187 | - | case il::Instr::BytesLoad { offset, .. } => try value(names, owner, offset, a), |
|
| 166 | + | case il::Instr::SliceLen { length, .. } => try value(names, length, a), |
|
| 167 | + | case il::Instr::Elem { index, .. } => try value(names, index, a), |
|
| 168 | + | case il::Instr::BytesLoad { offset, .. } => try value(names, offset, a), |
|
| 188 | 169 | case il::Instr::BytesStore { offset, value: val, .. } => { |
|
| 189 | - | try value(names, owner, offset, a); |
|
| 190 | - | try value(names, owner, val, a); |
|
| 170 | + | try value(names, offset, a); |
|
| 171 | + | try value(names, val, a); |
|
| 191 | 172 | } |
|
| 192 | - | case il::Instr::SliceElem { index, .. } => try value(names, owner, index, a), |
|
| 193 | - | case il::Instr::StorePtr { src, .. } => try value(names, owner, src, a), |
|
| 194 | - | case il::Instr::TraitNew { table, .. } => try value(names, owner, table, a), |
|
| 195 | - | case il::Instr::UnionSet { src, .. } => try value(names, owner, src, a), |
|
| 173 | + | case il::Instr::SliceElem { index, .. } => try value(names, index, a), |
|
| 174 | + | case il::Instr::StorePtr { src, .. } => try value(names, src, a), |
|
| 175 | + | case il::Instr::TraitNew { table, .. } => try value(names, table, a), |
|
| 176 | + | case il::Instr::UnionSet { src, .. } => try value(names, src, a), |
|
| 196 | 177 | case il::Instr::TraitCall { args, .. } => |
|
| 197 | - | try values(names, owner, il::argVals(func, args), a), |
|
| 178 | + | try values(names, il::argVals(func, args), a), |
|
| 198 | 179 | case |
|
| 199 | 180 | il::Instr::Field { .. }, |
|
| 200 | 181 | il::Instr::TraitCopy { .. }, |
|
| 201 | 182 | il::Instr::UnionGet { .. }, |
|
| 202 | 183 | il::Instr::SliceCopy { .. }, |
| 220 | 201 | throw binary::Error::Invalid; |
|
| 221 | 202 | }; |
|
| 222 | 203 | if rest.len < 3 or rest[0] <> ':' or rest[1] <> ':' { |
|
| 223 | 204 | throw binary::Error::Invalid; |
|
| 224 | 205 | } |
|
| 225 | - | return try add(names, name, a); |
|
| 206 | + | return try intern(names, name, a); |
|
| 226 | 207 | } |
|
| 227 | 208 | ||
| 228 | 209 | /// Add the package name to the tables. Call it before the first [`function`]. |
|
| 229 | 210 | export fn start(names: &mut Names, owner: *[u8], a: &heap::Heap) |
|
| 230 | 211 | throws (binary::Error) |
|
| 231 | 212 | { |
|
| 232 | - | try add(names, owner, a); |
|
| 213 | + | try intern(names, owner, a); |
|
| 233 | 214 | } |
|
| 234 | 215 | ||
| 235 | 216 | /// Collect the definition and the references of one function. Return the |
|
| 236 | 217 | /// stored copy of its name. Nothing in `func` is retained, so the caller may |
|
| 237 | 218 | /// release the function afterward. |
| 242 | 223 | a: &heap::Heap |
|
| 243 | 224 | ) -> *[u8] throws (binary::Error) { |
|
| 244 | 225 | let stored = try definition(names, owner, func.name, a); |
|
| 245 | 226 | for block in func.blocks { |
|
| 246 | 227 | for instr in block.instrs { |
|
| 247 | - | try instruction(names, owner, func, instr, a); |
|
| 228 | + | try instruction(names, func, instr, a); |
|
| 248 | 229 | } |
|
| 249 | 230 | } |
|
| 250 | 231 | return stored; |
|
| 251 | 232 | } |
|
| 252 | 233 | ||
| 253 | - | /// Build package tables from local definitions and their qualified references. |
|
| 234 | + | /// Build package tables from explicit dependencies and local definitions. |
|
| 254 | 235 | /// The tables move out of `names`, but its name index stays valid. |
|
| 255 | 236 | /// The functions of `program` must have been collected with [`function`]. |
|
| 256 | 237 | export fn finish( |
|
| 257 | 238 | names: &mut Names, |
|
| 258 | 239 | owner: *[u8], |
|
| 240 | + | dependencies: *[*[u8]], |
|
| 259 | 241 | program: il::Program, |
|
| 260 | 242 | exports: *[binary::Export], |
|
| 261 | 243 | entry: ?*[u8], |
|
| 262 | 244 | a: &heap::Heap |
|
| 263 | 245 | ) -> binary::Package throws (binary::Error) { |
|
| 246 | + | for dependency in dependencies { |
|
| 247 | + | try intern(names, dependency, a); |
|
| 248 | + | } |
|
| 264 | 249 | for item in exports { |
|
| 265 | 250 | try definition(names, owner, item.name, a); |
|
| 266 | 251 | } |
|
| 267 | 252 | if let name = entry { |
|
| 268 | 253 | try definition(names, owner, name, a); |
|
| 269 | 254 | } |
|
| 270 | 255 | for item in program.data { |
|
| 271 | 256 | try definition(names, owner, item.name, a); |
|
| 272 | 257 | for data in item.values { |
|
| 273 | 258 | match data.item { |
|
| 274 | - | case il::DataItem::Sym(name) => try reference(names, owner, name, a), |
|
| 275 | - | case il::DataItem::Fn(name) => try reference(names, owner, name, a), |
|
| 259 | + | case il::DataItem::Sym(name) => try reference(names, name, a), |
|
| 260 | + | case il::DataItem::Fn(name) => try reference(names, name, a), |
|
| 276 | 261 | else => {} |
|
| 277 | 262 | } |
|
| 278 | 263 | } |
|
| 279 | 264 | } |
|
| 280 | 265 | let symbols = names.symbols.take(); |
|
| 281 | - | let dependencies = names.dependencies.take(); |
|
| 282 | 266 | return binary::Package { |
|
| 283 | - | symbols: &symbols[..], name: owner, dependencies: &dependencies[..], exports, entry, program |
|
| 267 | + | symbols: &symbols[..], |
|
| 268 | + | name: owner, |
|
| 269 | + | dependencies, |
|
| 270 | + | exports, |
|
| 271 | + | entry, |
|
| 272 | + | program, |
|
| 273 | + | links: binary::EMPTY_LINKS, |
|
| 284 | 274 | }; |
|
| 285 | 275 | } |
|
| 286 | 276 | ||
| 287 | - | /// Build package tables for a complete program. |
|
| 277 | + | /// Build package tables for a complete program with explicit dependencies. |
|
| 288 | 278 | export fn package( |
|
| 289 | 279 | names: &mut Names, |
|
| 290 | 280 | owner: *[u8], |
|
| 281 | + | dependencies: *[*[u8]], |
|
| 291 | 282 | program: il::Program, |
|
| 292 | 283 | exports: *[binary::Export], |
|
| 293 | 284 | entry: ?*[u8], |
|
| 294 | 285 | a: &heap::Heap |
|
| 295 | 286 | ) -> binary::Package throws (binary::Error) { |
|
| 296 | 287 | try start(names, owner, a); |
|
| 297 | 288 | for func in program.fns { |
|
| 298 | 289 | try function(names, owner, func, a); |
|
| 299 | 290 | } |
|
| 300 | - | return try finish(names, owner, program, exports, entry, a); |
|
| 291 | + | return try finish(names, owner, dependencies, program, exports, entry, a); |
|
| 301 | 292 | } |
lib/std/lang/il/binary/linkage.rad
added
+151 -0
| 1 | + | //! Exact dependency pins and typed import bindings in binary RIL envelopes. |
|
| 2 | + | ||
| 3 | + | use std::hash::blake3; |
|
| 4 | + | use std::lang::il; |
|
| 5 | + | use std::lang::il::binary; |
|
| 6 | + | use std::lang::il::binary::reader; |
|
| 7 | + | use std::lang::il::binary::writer; |
|
| 8 | + | use std::mem::heap; |
|
| 9 | + | ||
| 10 | + | /// Image identity domain: RIL, an ASCII version digit, and four zero padding bytes. |
|
| 11 | + | /// The eight-byte length preserves word alignment for a contiguous payload. |
|
| 12 | + | constant IMAGE_PREFIX: *[u8] = &['R', 'I', 'L', '0', 0, 0, 0, 0]; |
|
| 13 | + | ||
| 14 | + | /// Hash a complete versioned package payload with the image identity prefix. |
|
| 15 | + | export fn imageId(bytes: &[u8]) -> binary::Id { |
|
| 16 | + | return imageParts(bytes, &[]); |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Hash a package header followed by its streamed function bodies. |
|
| 20 | + | export fn imageParts(head: &[u8], functions: &[u8]) -> binary::Id { |
|
| 21 | + | let mut state = blake3::new(); |
|
| 22 | + | blake3::update(&mut state, IMAGE_PREFIX); |
|
| 23 | + | blake3::update(&mut state, head); |
|
| 24 | + | blake3::update(&mut state, functions); |
|
| 25 | + | let digest = blake3::finish(state); |
|
| 26 | + | return binary::Id { bytes: digest.bytes }; |
|
| 27 | + | } |
|
| 28 | + | ||
| 29 | + | /// Write the exact bytes of a content identity. |
|
| 30 | + | fn writeId(out: &mut writer::Writer, id: &binary::Id) throws (binary::Error) { |
|
| 31 | + | for byte in &id.bytes[..] { |
|
| 32 | + | try writer::integer(out, byte as u64, 1); |
|
| 33 | + | } |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Read a complete content identity. |
|
| 37 | + | fn readId(input: &mut reader::Reader['wire]) -> binary::Id throws (binary::Error) { |
|
| 38 | + | let mut result = binary::Id { bytes: [0; 32] }; |
|
| 39 | + | for i in 0 .. result.bytes.len { |
|
| 40 | + | set result.bytes[i] = try input.integer(1) as u8; |
|
| 41 | + | } |
|
| 42 | + | return result; |
|
| 43 | + | } |
|
| 44 | + | ||
| 45 | + | /// Encode persistent declarations. Verification state has no wire representation. |
|
| 46 | + | export fn write(out: &mut writer::Writer, links: &binary::Links) throws (binary::Error) { |
|
| 47 | + | try writer::integer(out, links.pins.len as u64, 4); |
|
| 48 | + | for pin in links.pins { |
|
| 49 | + | try writeId(out, &pin); |
|
| 50 | + | } |
|
| 51 | + | try writer::integer(out, links.imports.len as u64, 4); |
|
| 52 | + | for item in links.imports { |
|
| 53 | + | try writer::symbol(out, item.name); |
|
| 54 | + | try writer::integer(out, item.dependency as u64, 4); |
|
| 55 | + | try writer::symbol(out, item.exported); |
|
| 56 | + | match item.kind { |
|
| 57 | + | case binary::ImportKind::Function(signature) => { |
|
| 58 | + | try writer::integer(out, 0, 1); |
|
| 59 | + | try writer::integer(out, signature as u64, 4); |
|
| 60 | + | } |
|
| 61 | + | case binary::ImportKind::Data { layout, readOnly, heap } => { |
|
| 62 | + | try writer::integer(out, 1, 1); |
|
| 63 | + | try writer::integer(out, layout as u64, 4); |
|
| 64 | + | try writer::integer(out, 1 if readOnly else 0, 1); |
|
| 65 | + | if let rights = heap { |
|
| 66 | + | try writer::integer(out, 1 + writer::heapFlags(rights) as u64, 1); |
|
| 67 | + | } else { |
|
| 68 | + | try writer::integer(out, 0, 1); |
|
| 69 | + | } |
|
| 70 | + | } |
|
| 71 | + | } |
|
| 72 | + | } |
|
| 73 | + | } |
|
| 74 | + | ||
| 75 | + | /// Decode link declarations before reading the program's descriptor tables. |
|
| 76 | + | export fn read(input: &mut reader::Reader['wire], arena: &heap::Heap) -> binary::Links |
|
| 77 | + | throws (binary::Error) |
|
| 78 | + | { |
|
| 79 | + | let pinCount = try input.count(32); |
|
| 80 | + | let mut pins: *mut [binary::Id] = &mut []; |
|
| 81 | + | try pins.reserve(pinCount, arena) catch { |
|
| 82 | + | throw binary::Error::Storage; |
|
| 83 | + | }; |
|
| 84 | + | for _ in 0 .. pinCount { |
|
| 85 | + | let image = try readId(input); |
|
| 86 | + | try pins.append(image, arena) catch { |
|
| 87 | + | throw binary::Error::Storage; |
|
| 88 | + | }; |
|
| 89 | + | } |
|
| 90 | + | let importCount = try input.count(17); |
|
| 91 | + | let mut imports: *mut [binary::Import] = &mut []; |
|
| 92 | + | try imports.reserve(importCount, arena) catch { |
|
| 93 | + | throw binary::Error::Storage; |
|
| 94 | + | }; |
|
| 95 | + | for _ in 0 .. importCount { |
|
| 96 | + | let name = try input.symbol(); |
|
| 97 | + | let dependency = try input.integer(4) as u32; |
|
| 98 | + | let exported = try input.symbol(); |
|
| 99 | + | let tag = try input.integer(1); |
|
| 100 | + | let mut kind = binary::ImportKind::Function(0); |
|
| 101 | + | if tag == 0 { |
|
| 102 | + | set kind = binary::ImportKind::Function(try input.integer(4) as u32); |
|
| 103 | + | } else if tag == 1 { |
|
| 104 | + | let layout = try input.integer(4) as u32; |
|
| 105 | + | let readOnly = try input.flag(); |
|
| 106 | + | let rights = try input.integer(1) as u8; |
|
| 107 | + | let mut heap: ?il::HeapType = nil; |
|
| 108 | + | if rights > 0 { |
|
| 109 | + | set heap = try reader::heapRights(rights - 1); |
|
| 110 | + | } |
|
| 111 | + | set kind = binary::ImportKind::Data { layout, readOnly, heap }; |
|
| 112 | + | } else { |
|
| 113 | + | throw binary::Error::Invalid; |
|
| 114 | + | } |
|
| 115 | + | try imports.append(binary::Import { name, dependency, exported, kind }, arena) catch { |
|
| 116 | + | throw binary::Error::Storage; |
|
| 117 | + | }; |
|
| 118 | + | } |
|
| 119 | + | return binary::Links { |
|
| 120 | + | pins: &pins[..], |
|
| 121 | + | imports: &imports[..], |
|
| 122 | + | image: nil, |
|
| 123 | + | verified: false, |
|
| 124 | + | }; |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Check import indices against the decoded package table sizes. |
|
| 128 | + | export fn check(links: &binary::Links, dependencies: u32, layouts: u32, signatures: u32) |
|
| 129 | + | throws (binary::Error) |
|
| 130 | + | { |
|
| 131 | + | if links.pins.len <> 0 and links.pins.len <> dependencies { |
|
| 132 | + | throw binary::Error::Invalid; |
|
| 133 | + | } |
|
| 134 | + | for item in links.imports { |
|
| 135 | + | if item.dependency >= dependencies { |
|
| 136 | + | throw binary::Error::Invalid; |
|
| 137 | + | } |
|
| 138 | + | match item.kind { |
|
| 139 | + | case binary::ImportKind::Function(index) => { |
|
| 140 | + | if index >= signatures { |
|
| 141 | + | throw binary::Error::Invalid; |
|
| 142 | + | } |
|
| 143 | + | } |
|
| 144 | + | case binary::ImportKind::Data { layout, .. } => { |
|
| 145 | + | if layout >= layouts { |
|
| 146 | + | throw binary::Error::Invalid; |
|
| 147 | + | } |
|
| 148 | + | } |
|
| 149 | + | } |
|
| 150 | + | } |
|
| 151 | + | } |
lib/std/lang/il/binary/program.rad
+93 -17
| 2 | 2 | //! Tables occur in this order: symbols, dependencies, exports, layouts, signatures, |
|
| 3 | 3 | //! traits, data, functions. Block records carry labels, parameters, and instructions. The |
|
| 4 | 4 | //! decoder derives predecessors and loop depth from the terminators. |
|
| 5 | 5 | //! A label is its base text followed by an optional decimal suffix. |
|
| 6 | 6 | ||
| 7 | + | use std::collections::dict; |
|
| 7 | 8 | use std::lang::il; |
|
| 8 | 9 | use std::lang::il::binary; |
|
| 10 | + | use std::lang::il::binary::cfg; |
|
| 11 | + | use std::lang::il::binary::linkage; |
|
| 9 | 12 | use std::lang::il::binary::reader; |
|
| 10 | 13 | use std::lang::il::binary::writer; |
|
| 11 | - | use std::lang::il::binary::cfg; |
|
| 12 | - | use std::mem::heap; |
|
| 13 | 14 | use std::mem; |
|
| 14 | - | use std::collections::dict; |
|
| 15 | + | use std::mem::heap; |
|
| 16 | + | ||
| 17 | + | /// Function content retained while decoding a package. |
|
| 18 | + | union Contents: Copy { |
|
| 19 | + | /// Reconstruct complete function bodies. |
|
| 20 | + | Bodies, |
|
| 21 | + | /// Retain callable headers from an already verified payload. |
|
| 22 | + | Declarations, |
|
| 23 | + | } |
|
| 15 | 24 | ||
| 16 | 25 | /// Write a counted sequence of typed SSA parameters. |
|
| 17 | 26 | fn writeParams(out: &mut writer::Writer, params: &[il::Param]) throws (binary::Error) { |
|
| 18 | 27 | try writer::integer(out, params.len as u64, 4); |
|
| 19 | 28 | for param in params { |
| 25 | 34 | /// Write the package tables through `out`, up to and including the function count. |
|
| 26 | 35 | fn writeHead(out: &mut writer::Writer, package: &binary::Package, fnCount: u32) |
|
| 27 | 36 | throws (binary::Error) |
|
| 28 | 37 | { |
|
| 29 | 38 | try writer::integer(out, binary::MAGIC as u64, 4); |
|
| 39 | + | try writer::integer(out, binary::FORMAT_VERSION as u64, 4); |
|
| 40 | + | try writer::integer(out, binary::ABI_VERSION as u64, 4); |
|
| 30 | 41 | try writer::integer(out, package.symbols.len as u64, 4); |
|
| 31 | 42 | for name in package.symbols { |
|
| 32 | 43 | try writer::bytes(out, name); |
|
| 33 | 44 | } |
|
| 34 | 45 | try writer::symbol(out, package.name); |
| 48 | 59 | try writer::integer(out, 1, 1); |
|
| 49 | 60 | try writer::symbol(out, entry); |
|
| 50 | 61 | } else { |
|
| 51 | 62 | try writer::integer(out, 0, 1); |
|
| 52 | 63 | } |
|
| 64 | + | try linkage::write(out, &package.links); |
|
| 53 | 65 | try writer::integer(out, package.program.layouts.len as u64, 4); |
|
| 54 | 66 | try writer::integer(out, package.program.signatures.len as u64, 4); |
|
| 55 | 67 | try writer::integer(out, package.program.traits.len as u64, 4); |
|
| 56 | 68 | for item in package.program.layouts { |
|
| 57 | 69 | try writer::layout(out, item); |
| 79 | 91 | } |
|
| 80 | 92 | } |
|
| 81 | 93 | try writer::integer(out, fnCount as u64, 4); |
|
| 82 | 94 | } |
|
| 83 | 95 | ||
| 84 | - | /// Write one function: name, signature, and blocks. |
|
| 96 | + | /// Write one length-delimited function: name, signature, and blocks. |
|
| 85 | 97 | fn writeFunction(out: &mut writer::Writer, func: *il::Fn) throws (binary::Error) { |
|
| 98 | + | let lengthOffset = out.offset; |
|
| 99 | + | try writer::integer(out, 0, 4); |
|
| 100 | + | let start = out.offset; |
|
| 86 | 101 | try writer::symbol(out, func.name); |
|
| 87 | 102 | try writer::valType(out, func.returnType); |
|
| 88 | 103 | try writer::borrows(out, func.borrows); |
|
| 89 | 104 | try writer::integer(out, 1 if func.isExtern else 0, 1); |
|
| 90 | 105 | try writeParams(out, func.params); |
| 101 | 116 | try writer::integer(out, block.instrs.len as u64, 4); |
|
| 102 | 117 | for instr in block.instrs { |
|
| 103 | 118 | try writer::instr(out, func, instr); |
|
| 104 | 119 | } |
|
| 105 | 120 | } |
|
| 121 | + | mem::store32Le(&mut out.bytes[..], lengthOffset, out.offset - start); |
|
| 106 | 122 | } |
|
| 107 | 123 | ||
| 108 | - | /// Write the package tables and functions through `out`. Return the number of encoded bytes. |
|
| 109 | - | fn writePackage(out: &mut writer::Writer, package: &binary::Package) -> u32 throws (binary::Error) { |
|
| 124 | + | /// Write a complete package at the current cursor. Return the final cursor offset. |
|
| 125 | + | export fn write(out: &mut writer::Writer, package: &binary::Package) -> u32 throws (binary::Error) { |
|
| 110 | 126 | try writeHead(out, package, package.program.fns.len); |
|
| 111 | 127 | for func in package.program.fns { |
|
| 112 | 128 | try writeFunction(out, func); |
|
| 113 | 129 | } |
|
| 114 | 130 | return out.offset; |
| 123 | 139 | /// Write a package into `bytes`. Return the number of encoded bytes. The |
|
| 124 | 140 | /// buffer is lent to the writer for the call and is back in `bytes` after it, |
|
| 125 | 141 | /// also on failure. |
|
| 126 | 142 | export fn encode(bytes: &mut *mut [u8], package: &binary::Package) -> u32 throws (binary::Error) { |
|
| 127 | 143 | let mut out = writer::new(bytes.take(), package.symbols); |
|
| 128 | - | let length = try writePackage(&mut out, package) catch err { |
|
| 144 | + | let length = try write(&mut out, package) catch err { |
|
| 129 | 145 | set *bytes = out.bytes.take(); |
|
| 130 | 146 | throw err; |
|
| 131 | 147 | }; |
|
| 132 | 148 | set *bytes = out.bytes.take(); |
|
| 133 | 149 | return length; |
| 494 | 510 | throw binary::Error::Storage; |
|
| 495 | 511 | }; |
|
| 496 | 512 | return hasCall; |
|
| 497 | 513 | } |
|
| 498 | 514 | ||
| 499 | - | /// Read functions with checked block and register indices. |
|
| 515 | + | /// Read callable headers and bound each function body to its encoded extent. |
|
| 500 | 516 | fn readFunctions( |
|
| 501 | - | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits |
|
| 517 | + | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits, contents: Contents |
|
| 502 | 518 | ) -> *[*il::Fn] throws (binary::Error) { |
|
| 503 | 519 | let n = try input.count(binary::MIN_FUNCTION_BYTES); |
|
| 504 | 520 | let mut fns: *mut [*il::Fn] = &mut []; |
|
| 505 | 521 | try fns.reserve(n, a) catch { |
|
| 506 | 522 | throw binary::Error::Storage; |
|
| 507 | 523 | }; |
|
| 508 | 524 | set input.registers = limits.registers; |
|
| 509 | 525 | for _ in 0 .. n { |
|
| 526 | + | let length = try input.count(1); |
|
| 527 | + | let end = input.offset + length; |
|
| 528 | + | let wire = input.wire; |
|
| 529 | + | set input.wire = &wire[.. end]; |
|
| 510 | 530 | let name = try input.symbol(); |
|
| 511 | 531 | let returnType = try input.valType(); |
|
| 512 | 532 | let borrows = try input.borrows(a); |
|
| 513 | 533 | let isExtern = try input.flag(); |
|
| 514 | 534 | let params = try readParams(input, a); |
|
| 515 | - | let count = try input.count(binary::MIN_BLOCK_BYTES); |
|
| 535 | + | let mut count: u32 = 0; |
|
| 536 | + | if contents == Contents::Bodies { |
|
| 537 | + | set count = try input.count(binary::MIN_BLOCK_BYTES); |
|
| 538 | + | } else { |
|
| 539 | + | set input.offset = end; |
|
| 540 | + | } |
|
| 516 | 541 | if count > limits.blocks or (isExtern and count <> 0) { |
|
| 517 | 542 | throw binary::Error::Invalid; |
|
| 518 | 543 | } |
|
| 519 | 544 | set input.blocks = count; |
|
| 520 | 545 | let mut blocks: *mut [il::Block] = &mut []; |
| 525 | 550 | for _ in 0 .. count { |
|
| 526 | 551 | if try readBlock(input, a, &mut blocks) { |
|
| 527 | 552 | set isLeaf = false; |
|
| 528 | 553 | } |
|
| 529 | 554 | } |
|
| 555 | + | if input.offset <> end { |
|
| 556 | + | throw binary::Error::Invalid; |
|
| 557 | + | } |
|
| 558 | + | set input.wire = wire; |
|
| 530 | 559 | try cfg::link(&mut blocks[..], &input.cases[..], a); |
|
| 531 | 560 | // The function takes the pools. The next function starts with empty ones. |
|
| 532 | 561 | let vals = input.vals.take(); |
|
| 533 | 562 | let cases = input.cases.take(); |
|
| 534 | 563 | let mut funcs: *mut [il::Fn] = &mut []; |
| 555 | 584 | return &fns[..]; |
|
| 556 | 585 | } |
|
| 557 | 586 | ||
| 558 | 587 | /// Read the package tables. The input must contain exactly one package. |
|
| 559 | 588 | fn readPackage( |
|
| 560 | - | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits |
|
| 589 | + | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits, contents: Contents |
|
| 561 | 590 | ) -> binary::Package throws (binary::Error) { |
|
| 562 | 591 | let magic = try input.integer(4) as u32; |
|
| 563 | 592 | if magic <> binary::MAGIC { |
|
| 564 | 593 | throw binary::Error::Invalid; |
|
| 565 | 594 | } |
|
| 595 | + | if try input.integer(4) <> binary::FORMAT_VERSION as u64 |
|
| 596 | + | or try input.integer(4) <> binary::ABI_VERSION as u64 |
|
| 597 | + | { |
|
| 598 | + | throw binary::Error::Invalid; |
|
| 599 | + | } |
|
| 566 | 600 | let symbolCount = try input.count(4); |
|
| 567 | 601 | try input.symbols.reserve(symbolCount, a) catch { |
|
| 568 | 602 | throw binary::Error::Storage; |
|
| 569 | 603 | }; |
|
| 570 | 604 | for i in 0 .. symbolCount { |
| 601 | 635 | for _ in 0 .. exportCount { |
|
| 602 | 636 | let symbol = try input.symbol(); |
|
| 603 | 637 | let kind = binary::ExportKind::Data |
|
| 604 | 638 | if try input.flag() |
|
| 605 | 639 | else binary::ExportKind::Function; |
|
| 606 | - | try exports.append(binary::Export { name: symbol, kind }, a) catch { |
|
| 640 | + | try exports.append( |
|
| 641 | + | binary::Export { |
|
| 642 | + | name: symbol, |
|
| 643 | + | kind, |
|
| 644 | + | startable: false, |
|
| 645 | + | }, |
|
| 646 | + | a |
|
| 647 | + | ) catch { |
|
| 607 | 648 | throw binary::Error::Storage; |
|
| 608 | 649 | }; |
|
| 609 | 650 | } |
|
| 610 | 651 | let mut entry: ?*[u8] = nil; |
|
| 611 | 652 | if try input.flag() { |
|
| 612 | 653 | set entry = try input.symbol(); |
|
| 613 | 654 | } |
|
| 655 | + | let links = try linkage::read(input, a); |
|
| 614 | 656 | set input.layouts = try input.count( |
|
| 615 | 657 | binary::MIN_LAYOUT_BYTES |
|
| 616 | 658 | ); |
|
| 617 | 659 | set input.signatures = try input.count( |
|
| 618 | 660 | binary::MIN_SIGNATURE_BYTES |
|
| 619 | 661 | ); |
|
| 620 | 662 | set input.traits = try input.count(binary::MIN_TRAIT_BYTES); |
|
| 621 | 663 | let layouts = try readLayouts(input, a); |
|
| 622 | 664 | let signatures = try readSignatures(input, a); |
|
| 623 | 665 | let traits = try readTraits(input, a); |
|
| 666 | + | try linkage::check(&links, depCount, input.layouts, input.signatures); |
|
| 624 | 667 | let data = try readData(input, a, layouts); |
|
| 625 | - | let fns = try readFunctions(input, a, limits); |
|
| 668 | + | let fns = try readFunctions(input, a, limits, contents); |
|
| 626 | 669 | if not input.atEnd() { |
|
| 627 | 670 | throw binary::Error::Invalid; |
|
| 628 | 671 | } |
|
| 629 | 672 | let symbols = input.symbols.take(); |
|
| 630 | 673 | return binary::Package { |
| 632 | 675 | name, |
|
| 633 | 676 | dependencies: &dependencies[..], |
|
| 634 | 677 | exports: &exports[..], |
|
| 635 | 678 | entry, |
|
| 636 | 679 | program: il::Program { data, fns, layouts, signatures, traits }, |
|
| 680 | + | links, |
|
| 637 | 681 | }; |
|
| 638 | 682 | } |
|
| 639 | 683 | ||
| 640 | - | /// Decode one package into storage from `arena`. Restore the arena offset on |
|
| 641 | - | /// every failure. |
|
| 642 | - | export fn decode( |
|
| 684 | + | /// Decode package fields without computing a content identity. Restore the arena |
|
| 685 | + | /// offset on every failure. The caller can hash the same immutable bytes separately. |
|
| 686 | + | export fn parse( |
|
| 643 | 687 | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits |
|
| 688 | + | ) -> binary::Package throws (binary::Error) { |
|
| 689 | + | return try read(bytes, arena, limits, Contents::Bodies); |
|
| 690 | + | } |
|
| 691 | + | ||
| 692 | + | /// Decode a package and restore the arena offset after failure. |
|
| 693 | + | fn read( |
|
| 694 | + | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits, contents: Contents |
|
| 644 | 695 | ) -> binary::Package throws (binary::Error) { |
|
| 645 | 696 | let saved = heap::mark(arena); |
|
| 646 | 697 | let mut input = reader::new(bytes); |
|
| 647 | - | return try readPackage(&mut input, arena, limits) catch err { |
|
| 698 | + | let package = try readPackage(&mut input, arena, limits, contents) catch err { |
|
| 648 | 699 | // A failed decode returns no pointer into the storage that it allocated, and the caller |
|
| 649 | 700 | // uses the heap only after the call, so nothing uses that range. |
|
| 650 | 701 | unsafe { |
|
| 651 | 702 | heap::rewind(arena, saved); |
|
| 652 | 703 | } |
|
| 653 | 704 | throw err; |
|
| 654 | 705 | }; |
|
| 706 | + | return package; |
|
| 707 | + | } |
|
| 708 | + | ||
| 709 | + | /// Decode and identify one complete immutable package payload. |
|
| 710 | + | export fn decode( |
|
| 711 | + | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits |
|
| 712 | + | ) -> binary::Package throws (binary::Error) { |
|
| 713 | + | let mut package = try parse(bytes, arena, limits); |
|
| 714 | + | set package.links.image = linkage::imageId(bytes); |
|
| 715 | + | return package; |
|
| 716 | + | } |
|
| 717 | + | ||
| 718 | + | /// Read type tables, data and callable headers from an immutable package payload. |
|
| 719 | + | /// The caller must supply verified source. Function extents delimit skipped bodies. |
|
| 720 | + | export fn declarations(bytes: &'wire [u8], arena: &heap::Heap) -> binary::Package |
|
| 721 | + | throws (binary::Error) |
|
| 722 | + | { |
|
| 723 | + | return try read( |
|
| 724 | + | bytes, |
|
| 725 | + | arena, |
|
| 726 | + | binary::Limits { |
|
| 727 | + | registers: 0xffffffff, blocks: 0 |
|
| 728 | + | }, |
|
| 729 | + | Contents::Declarations |
|
| 730 | + | ); |
|
| 655 | 731 | } |
lib/std/lang/il/binary/tests/decode.rad
+180 -24
| 1 | 1 | //! Package reconstruction, malformed input, and arena rollback tests. |
|
| 2 | 2 | ||
| 3 | - | use std::testing; |
|
| 4 | - | use std::mem::heap; |
|
| 3 | + | use std::collections::dict; |
|
| 4 | + | use std::hash::blake3; |
|
| 5 | 5 | use std::lang::il; |
|
| 6 | 6 | use std::lang::il::binary; |
|
| 7 | - | use std::lang::il::binary::reader; |
|
| 8 | - | use std::lang::il::binary::program; |
|
| 9 | 7 | use std::lang::il::binary::collect; |
|
| 10 | - | use std::collections::dict; |
|
| 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; |
|
| 11 | 14 | ||
| 12 | 15 | /// Decode arena backing storage. Tests reset it before each use. |
|
| 13 | 16 | static MEMORY: [u8; 2048] = [0; 2048]; |
|
| 14 | 17 | ||
| 15 | 18 | /// Program without declarations. |
| 83 | 86 | } |
|
| 84 | 87 | ||
| 85 | 88 | /// Check an empty program with a package identity. |
|
| 86 | 89 | @test unsafe fn emptyPackage() throws (testing::TestError) { |
|
| 87 | 90 | let package = binary::Package { |
|
| 88 | - | symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil, program: EMPTY |
|
| 91 | + | symbols: &["p"], |
|
| 92 | + | name: "p", |
|
| 93 | + | dependencies: &[], |
|
| 94 | + | exports: &[], |
|
| 95 | + | entry: nil, |
|
| 96 | + | program: EMPTY, |
|
| 97 | + | links: binary::EMPTY_LINKS, |
|
| 89 | 98 | }; |
|
| 90 | 99 | try roundTrip(&package); |
|
| 91 | - | static buffer: [u8; 64] = [0; 64]; |
|
| 100 | + | static buffer: [u8; 128] = [0; 128]; |
|
| 92 | 101 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
|
| 93 | 102 | throw testing::TestError::Failed; |
|
| 94 | 103 | }; |
|
| 95 | 104 | try testing::expectBytesEq( |
|
| 96 | 105 | &buffer[.. length], |
|
| 97 | 106 | &[ |
|
| 98 | - | 82, 73, 76, 0, 1, 0, 0, 0, |
|
| 99 | - | 1, 0, 0, 0, 112, 0, 0, 0, 0, |
|
| 100 | - | 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 101 | - | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 102 | - | 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 107 | + | 82, 73, 76, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, |
|
| 108 | + | 1, 0, 0, 0, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 109 | + | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 110 | + | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
| 103 | 111 | ] |
|
| 104 | 112 | ); |
|
| 105 | 113 | } |
|
| 106 | 114 | ||
| 107 | 115 | /// Check functions, dependencies, exports, initializers, and block metadata. |
| 194 | 202 | let package = binary::Package { |
|
| 195 | 203 | symbols: &["p", "dep", "p::main", "dep::fn", "p::data", "p::zero"], |
|
| 196 | 204 | name: "p", |
|
| 197 | 205 | dependencies: &["dep"], |
|
| 198 | 206 | exports: &[ |
|
| 199 | - | binary::Export { name: "p::main", kind: binary::ExportKind::Function }, |
|
| 200 | - | binary::Export { name: "p::data", kind: binary::ExportKind::Data }, |
|
| 207 | + | binary::Export { |
|
| 208 | + | name: "p::main", |
|
| 209 | + | kind: binary::ExportKind::Function, |
|
| 210 | + | startable: false, |
|
| 211 | + | }, |
|
| 212 | + | binary::Export { |
|
| 213 | + | name: "p::data", |
|
| 214 | + | kind: binary::ExportKind::Data, |
|
| 215 | + | startable: false, |
|
| 216 | + | }, |
|
| 201 | 217 | ], |
|
| 202 | 218 | entry: "p::main", |
|
| 203 | 219 | program: il::Program { |
|
| 204 | 220 | data: retainData(&[data, zero]), |
|
| 205 | 221 | fns: functions, |
| 208 | 224 | il::Layout { size: 4096, alignment: 4096, slots: &[], regions: 0 }, |
|
| 209 | 225 | ], |
|
| 210 | 226 | signatures: &[], |
|
| 211 | 227 | traits: &[], |
|
| 212 | 228 | }, |
|
| 229 | + | links: binary::EMPTY_LINKS, |
|
| 213 | 230 | }; |
|
| 214 | 231 | try roundTrip(&package); |
|
| 215 | 232 | } |
|
| 216 | 233 | ||
| 217 | 234 | /// Reject an invalid encoded instruction without a panic. |
| 275 | 292 | } |
|
| 276 | 293 | ||
| 277 | 294 | /// Check header validation and package symbol indices with arena rollback. |
|
| 278 | 295 | @test unsafe fn malformedPackages() throws (testing::TestError) { |
|
| 279 | 296 | let package = binary::Package { |
|
| 280 | - | symbols: &["p"], name: "p", dependencies: &[], exports: &[], entry: nil, program: EMPTY |
|
| 297 | + | symbols: &["p"], |
|
| 298 | + | name: "p", |
|
| 299 | + | dependencies: &[], |
|
| 300 | + | exports: &[], |
|
| 301 | + | entry: nil, |
|
| 302 | + | program: EMPTY, |
|
| 303 | + | links: binary::EMPTY_LINKS, |
|
| 281 | 304 | }; |
|
| 282 | - | static buffer: [u8; 64] = [0; 64]; |
|
| 305 | + | static buffer: [u8; 128] = [0; 128]; |
|
| 283 | 306 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
|
| 284 | 307 | throw testing::TestError::Failed; |
|
| 285 | 308 | }; |
|
| 286 | 309 | let arena = heap::withCapacity(&mut MEMORY[..], 64); |
|
| 287 | 310 | for offset in &[0, 4, 17, 29] { |
| 349 | 372 | fns: &[], |
|
| 350 | 373 | layouts: &[il::Layout { size: 0xffffffff, alignment: 8, slots: &[], regions: 0 }], |
|
| 351 | 374 | signatures: &[], |
|
| 352 | 375 | traits: &[], |
|
| 353 | 376 | }, |
|
| 377 | + | links: binary::EMPTY_LINKS, |
|
| 354 | 378 | }; |
|
| 355 | 379 | try rejected(&package, LIMITS); |
|
| 356 | 380 | set item.values = &[ |
|
| 357 | 381 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
|
| 358 | 382 | il::DataValue { item: il::DataItem::Undef, count: 1 }, |
| 403 | 427 | exports: &[], |
|
| 404 | 428 | entry: nil, |
|
| 405 | 429 | program: il::Program { |
|
| 406 | 430 | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
|
| 407 | 431 | }, |
|
| 432 | + | links: binary::EMPTY_LINKS, |
|
| 408 | 433 | }; |
|
| 409 | 434 | try rejected(&package, limits); |
|
| 410 | 435 | } |
|
| 411 | 436 | ||
| 412 | 437 | /// Check function block limits, parameter indices, and branch targets. |
| 534 | 559 | exports: &[], |
|
| 535 | 560 | entry: nil, |
|
| 536 | 561 | program: il::Program { |
|
| 537 | 562 | data: &[], fns: &list[..], layouts: &[], signatures: &[], traits: &[] |
|
| 538 | 563 | }, |
|
| 564 | + | links: binary::EMPTY_LINKS, |
|
| 539 | 565 | }; |
|
| 540 | 566 | let length = try encodeInto(&mut whole[..] as *mut [u8], &package) catch { |
|
| 541 | 567 | throw testing::TestError::Failed; |
|
| 542 | 568 | }; |
|
| 543 | 569 | let mut index = dict::init(&mut slots[..] as *mut [dict::Entry]); |
| 594 | 620 | } |
|
| 595 | 621 | } |
|
| 596 | 622 | } |
|
| 597 | 623 | ||
| 598 | 624 | /// Collect the names of `local` for package `owner` in `arena`. |
|
| 599 | - | unsafe fn collected(owner: *[u8], local: il::Program, arena: &heap::Heap) -> binary::Package |
|
| 625 | + | unsafe fn collected( |
|
| 626 | + | owner: *[u8], dependencies: *[*[u8]], local: il::Program, arena: &heap::Heap |
|
| 627 | + | ) -> binary::Package |
|
| 600 | 628 | throws (binary::Error) |
|
| 601 | 629 | { |
|
| 602 | 630 | let mut names = try collect::new(64, arena); |
|
| 603 | - | return try collect::package(&mut names, owner, local, &[], nil, arena); |
|
| 631 | + | return try collect::package(&mut names, owner, dependencies, local, &[], nil, arena); |
|
| 604 | 632 | } |
|
| 605 | 633 | ||
| 606 | - | /// Check symbol collection storage exhaustion and package ownership failures. |
|
| 634 | + | /// Check explicit dependencies, collection storage and package ownership. |
|
| 607 | 635 | @test unsafe fn collectionBounds() throws (testing::TestError) { |
|
| 608 | 636 | let local = il::Program { |
|
| 609 | 637 | data: &[ |
|
| 610 | 638 | il::Data { |
|
| 611 | 639 | name: "p::data", |
| 623 | 651 | signatures: &[], |
|
| 624 | 652 | traits: &[], |
|
| 625 | 653 | }; |
|
| 626 | 654 | let arena = heap::over(&mut MEMORY[..]); |
|
| 627 | 655 | let mut failed = false; |
|
| 628 | - | try collected("other", local, arena) catch err { |
|
| 656 | + | try collected("other", &["provider"], local, arena) catch err { |
|
| 629 | 657 | try testing::expect(err == binary::Error::Invalid); |
|
| 630 | 658 | set failed = true; |
|
| 631 | 659 | }; |
|
| 632 | 660 | try testing::expect(failed); |
|
| 633 | 661 | heap::rewind(arena, 0); |
|
| 634 | - | let package = try collected("p", local, arena) catch { |
|
| 662 | + | let package = try collected("p", &["provider"], local, arena) catch { |
|
| 635 | 663 | throw testing::TestError::Failed; |
|
| 636 | 664 | }; |
|
| 637 | - | try testing::expect(package.dependencies.len == 1); |
|
| 638 | - | try testing::expectBytesEq(package.dependencies[0], "dep"); |
|
| 665 | + | assert package.dependencies.len == 1; |
|
| 666 | + | assert mem::eq(package.dependencies[0], "provider"); |
|
| 639 | 667 | try testing::expect(package.symbols.len == 4); |
|
| 640 | 668 | let required = heap::used(arena); |
|
| 641 | 669 | for capacity in 0 .. required { |
|
| 642 | 670 | let short = heap::withCapacity(&mut MEMORY[..], capacity); |
|
| 643 | 671 | set failed = false; |
|
| 644 | - | try collected("p", local, short) catch err { |
|
| 672 | + | try collected("p", &["provider"], local, short) catch err { |
|
| 645 | 673 | try testing::expect(err == binary::Error::Storage); |
|
| 646 | 674 | set failed = true; |
|
| 647 | 675 | }; |
|
| 648 | 676 | try testing::expect(failed); |
|
| 649 | 677 | } |
| 790 | 818 | name: "p", |
|
| 791 | 819 | dependencies: &[], |
|
| 792 | 820 | exports: &[], |
|
| 793 | 821 | entry: nil, |
|
| 794 | 822 | program: il::Program { data: &[], fns: &list[..], layouts, signatures, traits: &[] }, |
|
| 823 | + | links: binary::EMPTY_LINKS, |
|
| 795 | 824 | }; |
|
| 796 | 825 | try roundTrip(&package); |
|
| 797 | 826 | ||
| 798 | 827 | let arena = heap::over(&mut MEMORY[..]); |
|
| 799 | 828 | let decoded = try reencoded(&package, arena); |
| 873 | 902 | name: "p", |
|
| 874 | 903 | dependencies: &[], |
|
| 875 | 904 | exports: &[], |
|
| 876 | 905 | entry: nil, |
|
| 877 | 906 | program: il::Program { data: &[], fns: &[], layouts, signatures, traits }, |
|
| 907 | + | links: binary::EMPTY_LINKS, |
|
| 878 | 908 | }; |
|
| 879 | 909 | try roundTrip(&package); |
|
| 880 | 910 | ||
| 881 | 911 | let arena = heap::over(&mut MEMORY[..]); |
|
| 882 | 912 | let decoded = try reencoded(&package, arena); |
| 906 | 936 | il::Trait { |
|
| 907 | 937 | methods: &[il::Method { sig: 1, receiver: 0, own: false, mutable: false }] |
|
| 908 | 938 | }, |
|
| 909 | 939 | ], |
|
| 910 | 940 | }, |
|
| 941 | + | links: binary::EMPTY_LINKS, |
|
| 911 | 942 | }; |
|
| 912 | 943 | try rejected(&unsigned, LIMITS); |
|
| 913 | 944 | } |
|
| 914 | 945 | ||
| 915 | 946 | /// Encode a package with the given tables and check that decoding rejects it. |
| 921 | 952 | name: "p", |
|
| 922 | 953 | dependencies: &[], |
|
| 923 | 954 | exports: &[], |
|
| 924 | 955 | entry: nil, |
|
| 925 | 956 | program: il::Program { data: &[], fns: &[], layouts, signatures, traits: &[] }, |
|
| 957 | + | links: binary::EMPTY_LINKS, |
|
| 926 | 958 | }; |
|
| 927 | 959 | try rejected(&package, LIMITS); |
|
| 928 | 960 | } |
|
| 929 | 961 | ||
| 930 | 962 | /// Owned nullable pointer to layout 1, as stored in a slot. |
| 1200 | 1232 | params: &[], ret: il::ValType::Fn(0), borrows: &[] |
|
| 1201 | 1233 | }, |
|
| 1202 | 1234 | ], |
|
| 1203 | 1235 | traits: &[], |
|
| 1204 | 1236 | }, |
|
| 1237 | + | links: binary::EMPTY_LINKS, |
|
| 1205 | 1238 | }; |
|
| 1206 | 1239 | try roundTrip(&ok); |
|
| 1207 | 1240 | } |
|
| 1208 | 1241 | ||
| 1209 | 1242 | /// Arguments of a branch that passes none. |
| 1292 | 1325 | exports: &[], |
|
| 1293 | 1326 | entry: nil, |
|
| 1294 | 1327 | program: il::Program { |
|
| 1295 | 1328 | data: &[], fns: &fns[..], layouts: &[], signatures: &[], traits: &[] |
|
| 1296 | 1329 | }, |
|
| 1330 | + | links: binary::EMPTY_LINKS, |
|
| 1297 | 1331 | }; |
|
| 1298 | 1332 | static buffer: [u8; 512] = [0; 512]; |
|
| 1299 | 1333 | let length = try encodeInto(&mut buffer[..] as *mut [u8], &package) catch { |
|
| 1300 | 1334 | throw testing::TestError::Failed; |
|
| 1301 | 1335 | }; |
| 1383 | 1417 | exports: &[], |
|
| 1384 | 1418 | entry: nil, |
|
| 1385 | 1419 | program: il::Program { |
|
| 1386 | 1420 | data: &[], fns: &[], layouts, signatures: &[], traits: &[] |
|
| 1387 | 1421 | }, |
|
| 1422 | + | links: binary::EMPTY_LINKS, |
|
| 1388 | 1423 | }; |
|
| 1389 | 1424 | try roundTrip(&ok); |
|
| 1390 | 1425 | let arena = heap::over(&mut MEMORY[..]); |
|
| 1391 | 1426 | let decoded = try reencoded(&ok, arena); |
|
| 1392 | 1427 | let case il::SlotKind::Union { offset, size, variants } = |
| 1403 | 1438 | // The slot lies within the layout, and is as aligned as its payloads. |
|
| 1404 | 1439 | let first = &[il::UnionVariant { tag: 0, layout: 0, regions: &[] }]; |
|
| 1405 | 1440 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 8, 8, 4, 16, first)]), &[]); |
|
| 1406 | 1441 | try rejectedLayouts(table(a, &[PLAIN, unionLayout(a, 16, 4, 8, 16, first)]), &[]); |
|
| 1407 | 1442 | } |
|
| 1443 | + | ||
| 1444 | + | /// Exact dependency pins and importer-local aliases survive the package envelope. |
|
| 1445 | + | @test unsafe fn linkedEnvelope() throws (testing::TestError) { |
|
| 1446 | + | let package = binary::Package { |
|
| 1447 | + | symbols: &["p", "dep", "local", "remote"], |
|
| 1448 | + | name: "p", |
|
| 1449 | + | dependencies: &["dep"], |
|
| 1450 | + | exports: &[], |
|
| 1451 | + | entry: nil, |
|
| 1452 | + | program: il::Program { |
|
| 1453 | + | data: &[], |
|
| 1454 | + | fns: &[], |
|
| 1455 | + | layouts: &[], |
|
| 1456 | + | signatures: &[ |
|
| 1457 | + | il::Signature { params: &[], ret: il::ValType::Void, borrows: &[] } |
|
| 1458 | + | ], |
|
| 1459 | + | traits: &[], |
|
| 1460 | + | }, |
|
| 1461 | + | links: binary::Links { |
|
| 1462 | + | pins: &[ |
|
| 1463 | + | binary::Id { bytes: [1; 32] } |
|
| 1464 | + | ], |
|
| 1465 | + | imports: &[ |
|
| 1466 | + | binary::Import { |
|
| 1467 | + | name: "local", |
|
| 1468 | + | dependency: 0, |
|
| 1469 | + | exported: "remote", |
|
| 1470 | + | kind: binary::ImportKind::Function(0), |
|
| 1471 | + | }, |
|
| 1472 | + | ], |
|
| 1473 | + | image: nil, |
|
| 1474 | + | verified: false, |
|
| 1475 | + | }, |
|
| 1476 | + | }; |
|
| 1477 | + | try roundTrip(&package); |
|
| 1478 | + | } |
|
| 1479 | + | ||
| 1480 | + | /// Provider declarations retain exact signatures without retaining function bodies. |
|
| 1481 | + | @test unsafe fn providerDeclarations() throws (testing::TestError) { |
|
| 1482 | + | /// Function with a complete body in the encoded provider. |
|
| 1483 | + | unsafe static func: il::Fn = il::Fn { |
|
| 1484 | + | name: "p::f", |
|
| 1485 | + | params: &[], |
|
| 1486 | + | returnType: il::ValType::Void, |
|
| 1487 | + | isExtern: false, |
|
| 1488 | + | isLeaf: true, |
|
| 1489 | + | blocks: &[ |
|
| 1490 | + | il::Block { |
|
| 1491 | + | label: il::BlockLabel { base: "entry", suffix: nil }, |
|
| 1492 | + | params: &[], |
|
| 1493 | + | instrs: &[il::Instr::Ret { val: nil }], |
|
| 1494 | + | locs: &[], |
|
| 1495 | + | preds: &[], |
|
| 1496 | + | loopDepth: 0, |
|
| 1497 | + | }, |
|
| 1498 | + | ], |
|
| 1499 | + | vals: &[], |
|
| 1500 | + | cases: &[], |
|
| 1501 | + | borrows: &[], |
|
| 1502 | + | }; |
|
| 1503 | + | let storage = heap::over(&mut MEMORY[..]); |
|
| 1504 | + | let mut functions: *mut [*il::Fn] = &mut []; |
|
| 1505 | + | try! functions.append(&func, storage); |
|
| 1506 | + | let package = binary::Package { |
|
| 1507 | + | symbols: &["p", "p::f"], |
|
| 1508 | + | name: "p", |
|
| 1509 | + | dependencies: &[], |
|
| 1510 | + | exports: &[ |
|
| 1511 | + | binary::Export { |
|
| 1512 | + | name: "p::f", kind: binary::ExportKind::Function, startable: false |
|
| 1513 | + | }, |
|
| 1514 | + | ], |
|
| 1515 | + | entry: nil, |
|
| 1516 | + | program: il::Program { |
|
| 1517 | + | data: &[], fns: &functions[..], layouts: &[], signatures: &[], traits: &[] |
|
| 1518 | + | }, |
|
| 1519 | + | links: binary::EMPTY_LINKS, |
|
| 1520 | + | }; |
|
| 1521 | + | /// Complete provider payload used for header and extent checks. |
|
| 1522 | + | static bytes: [u8; 256] = [0; 256]; |
|
| 1523 | + | let length = try! encodeInto(&mut bytes[..] as *mut [u8], &package); |
|
| 1524 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 1525 | + | let headers = try! program::declarations(&bytes[.. length], arena); |
|
| 1526 | + | assert headers.program.fns.len == 1; |
|
| 1527 | + | assert headers.program.fns[0].returnType == func.returnType; |
|
| 1528 | + | assert headers.program.fns[0].blocks.len == 0; |
|
| 1529 | + | assert headers.exports.len == 1; |
|
| 1530 | + | let mut head = package; |
|
| 1531 | + | set head.program.fns = &[]; |
|
| 1532 | + | /// Package head without function records, to locate the first extent. |
|
| 1533 | + | static headBytes: [u8; 256] = [0; 256]; |
|
| 1534 | + | let start = try! encodeInto(&mut headBytes[..] as *mut [u8], &head); |
|
| 1535 | + | let extent = mem::load32Le(&bytes[.. length], start); |
|
| 1536 | + | for size in &[0 as u32, 1, extent - 1, extent + 1, 0xffffffff] { |
|
| 1537 | + | mem::store32Le(&mut bytes[.. length], start, size); |
|
| 1538 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 1539 | + | let mut failed = false; |
|
| 1540 | + | try program::parse(&bytes[.. length], arena, LIMITS) catch error { |
|
| 1541 | + | assert error == binary::Error::Invalid or error == binary::Error::Truncated; |
|
| 1542 | + | set failed = true; |
|
| 1543 | + | }; |
|
| 1544 | + | assert failed and heap::used(arena) == 0; |
|
| 1545 | + | set failed = false; |
|
| 1546 | + | try program::declarations(&bytes[.. length], arena) catch error { |
|
| 1547 | + | assert error == binary::Error::Invalid or error == binary::Error::Truncated; |
|
| 1548 | + | set failed = true; |
|
| 1549 | + | }; |
|
| 1550 | + | assert failed and heap::used(arena) == 0; |
|
| 1551 | + | } |
|
| 1552 | + | } |
|
| 1553 | + | ||
| 1554 | + | /// Image identity uses an ASCII version digit and four zero padding bytes. |
|
| 1555 | + | @test fn imagePrefix() throws (testing::TestError) { |
|
| 1556 | + | let expected = blake3::digest(&[82, 73, 76, 48, 0, 0, 0, 0, 97, 98, 99]); |
|
| 1557 | + | let image = linkage::imageId("abc"); |
|
| 1558 | + | assert mem::eq(&image.bytes[..], &expected.bytes[..]); |
|
| 1559 | + | let payload = "abc"; |
|
| 1560 | + | for split in 0 .. payload.len + 1 { |
|
| 1561 | + | assert linkage::imageParts(&payload[.. split], &payload[split ..]) == image; |
|
| 1562 | + | } |
|
| 1563 | + | } |
lib/std/lang/il/link.rad
added
+357 -0
| 1 | + | //! Bounded comparison of type contracts from separate RIL programs. |
|
| 2 | + | ||
| 3 | + | @test export mod tests; |
|
| 4 | + | ||
| 5 | + | use std::lang::il; |
|
| 6 | + | use std::mem::heap; |
|
| 7 | + | ||
| 8 | + | /// Largest comparison workspace accepted by the graph matcher. |
|
| 9 | + | export constant MAX_PAIRS: u32 = 65536; |
|
| 10 | + | ||
| 11 | + | /// Multiplier for the left descriptor index in a pair hash. |
|
| 12 | + | constant HASH_LEFT: u64 = 0x9e3779b1; |
|
| 13 | + | /// Multiplier for the right descriptor index in a pair hash. |
|
| 14 | + | constant HASH_RIGHT: u64 = 0x85ebca77; |
|
| 15 | + | ||
| 16 | + | /// A graph comparison exceeded its capacity or could not allocate storage. |
|
| 17 | + | export union Error: Copy { |
|
| 18 | + | /// The number of distinct descriptor pairs exceeds the configured limit. |
|
| 19 | + | Limit, |
|
| 20 | + | /// The scratch heap has insufficient storage. |
|
| 21 | + | Storage, |
|
| 22 | + | } |
|
| 23 | + | ||
| 24 | + | /// Descriptor table selected by a graph edge. |
|
| 25 | + | union Kind: Copy { |
|
| 26 | + | /// Object representation and its protected slots. |
|
| 27 | + | Layout, |
|
| 28 | + | /// Function parameters, result, and borrow relations. |
|
| 29 | + | Signature, |
|
| 30 | + | /// Ordered method contracts and receiver rights. |
|
| 31 | + | Trait, |
|
| 32 | + | } |
|
| 33 | + | ||
| 34 | + | /// One distinct pair of descriptors and its occupied hash-table slot. |
|
| 35 | + | record Pair: Copy { |
|
| 36 | + | /// Descriptor table. |
|
| 37 | + | kind: Kind, |
|
| 38 | + | /// Index in the left program. |
|
| 39 | + | left: u32, |
|
| 40 | + | /// Index in the right program. |
|
| 41 | + | right: u32, |
|
| 42 | + | /// Slot which indexes this pair in Workspace.slots. |
|
| 43 | + | bucket: u32, |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Reusable storage for comparison of cyclic descriptor graphs. |
|
| 47 | + | export opaque record Workspace { |
|
| 48 | + | /// Distinct pairs, in discovery order. Its length is the pair limit. |
|
| 49 | + | pairs: *mut [Pair], |
|
| 50 | + | /// Open-addressed pair indices plus one; zero denotes an empty slot. |
|
| 51 | + | slots: *mut [u32], |
|
| 52 | + | /// Initialized prefix of pairs. |
|
| 53 | + | count: u32, |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// Allocate a bounded workspace. The heap must outlive all comparisons with it. |
|
| 57 | + | export fn workspace(limit: u32, arena: &heap::Heap) -> Workspace throws (Error) { |
|
| 58 | + | if limit == 0 or limit > MAX_PAIRS { |
|
| 59 | + | throw Error::Limit; |
|
| 60 | + | } |
|
| 61 | + | let mut size: u32 = 2; |
|
| 62 | + | while size < limit * 2 { |
|
| 63 | + | set size *= 2; |
|
| 64 | + | } |
|
| 65 | + | let mut slots: *mut [u32] = &mut []; |
|
| 66 | + | let mut pairs: *mut [Pair] = &mut []; |
|
| 67 | + | try slots.resize(size, 0, arena) catch { |
|
| 68 | + | throw Error::Storage; |
|
| 69 | + | }; |
|
| 70 | + | try pairs.resize( |
|
| 71 | + | limit, Pair { kind: Kind::Layout, left: 0, right: 0, bucket: 0 }, arena |
|
| 72 | + | ) catch { |
|
| 73 | + | throw Error::Storage; |
|
| 74 | + | }; |
|
| 75 | + | return Workspace { pairs, slots, count: 0 }; |
|
| 76 | + | } |
|
| 77 | + | ||
| 78 | + | /// Clear the slots used by the previous comparison. |
|
| 79 | + | fn reset(work: &mut Workspace) { |
|
| 80 | + | for pair in &work.pairs[.. work.count] { |
|
| 81 | + | set work.slots[pair.bucket] = 0; |
|
| 82 | + | } |
|
| 83 | + | set work.count = 0; |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | /// Add an unseen edge. Existing pairs terminate traversal of a cycle. |
|
| 87 | + | fn edge(work: &mut Workspace, kind: Kind, left: u32, right: u32) throws (Error) { |
|
| 88 | + | let mut tag: u64 = 0; |
|
| 89 | + | match kind { |
|
| 90 | + | case Kind::Layout => set tag = 1, |
|
| 91 | + | case Kind::Signature => set tag = 2, |
|
| 92 | + | case Kind::Trait => set tag = 3, |
|
| 93 | + | } |
|
| 94 | + | let hash = ((left as u64 * HASH_LEFT) ^ (right as u64 * HASH_RIGHT) ^ tag) as u32; |
|
| 95 | + | let mask = work.slots.len - 1; |
|
| 96 | + | let mut bucket = hash & mask; |
|
| 97 | + | loop { |
|
| 98 | + | let index = work.slots[bucket]; |
|
| 99 | + | if index == 0 { |
|
| 100 | + | if work.count == work.pairs.len { |
|
| 101 | + | throw Error::Limit; |
|
| 102 | + | } |
|
| 103 | + | set work.pairs[work.count] = Pair { kind, left, right, bucket }; |
|
| 104 | + | set work.count += 1; |
|
| 105 | + | set work.slots[bucket] = work.count; |
|
| 106 | + | return; |
|
| 107 | + | } |
|
| 108 | + | let pair = work.pairs[index - 1]; |
|
| 109 | + | if pair.kind == kind and pair.left == left and pair.right == right { |
|
| 110 | + | return; |
|
| 111 | + | } |
|
| 112 | + | set bucket = (bucket + 1) & mask; |
|
| 113 | + | } |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Match pointer rights and enqueue the pointee layouts. |
|
| 117 | + | fn pointer(work: &mut Workspace, left: il::PtrType, right: il::PtrType) -> bool throws (Error) { |
|
| 118 | + | if left.own <> right.own or left.mutable <> right.mutable or left.nullable <> right.nullable { |
|
| 119 | + | return false; |
|
| 120 | + | } |
|
| 121 | + | try edge(work, Kind::Layout, left.layout, right.layout); |
|
| 122 | + | return true; |
|
| 123 | + | } |
|
| 124 | + | ||
| 125 | + | /// Match a value type and enqueue its descriptor edges. |
|
| 126 | + | fn value(work: &mut Workspace, left: il::ValType, right: il::ValType) -> bool throws (Error) { |
|
| 127 | + | match left { |
|
| 128 | + | case il::ValType::Void => { |
|
| 129 | + | if let case il::ValType::Void = right { |
|
| 130 | + | return true; |
|
| 131 | + | } |
|
| 132 | + | } |
|
| 133 | + | case il::ValType::Data(a) => { |
|
| 134 | + | if let case il::ValType::Data(b) = right { |
|
| 135 | + | return a == b; |
|
| 136 | + | } |
|
| 137 | + | } |
|
| 138 | + | case il::ValType::Ptr(a) => { |
|
| 139 | + | if let case il::ValType::Ptr(b) = right { |
|
| 140 | + | return try pointer(work, a, b); |
|
| 141 | + | } |
|
| 142 | + | } |
|
| 143 | + | case il::ValType::Fn(a) => { |
|
| 144 | + | if let case il::ValType::Fn(b) = right { |
|
| 145 | + | try edge(work, Kind::Signature, a, b); |
|
| 146 | + | return true; |
|
| 147 | + | } |
|
| 148 | + | } |
|
| 149 | + | case il::ValType::Heap(a) => { |
|
| 150 | + | if let case il::ValType::Heap(b) = right { |
|
| 151 | + | return a == b; |
|
| 152 | + | } |
|
| 153 | + | } |
|
| 154 | + | } |
|
| 155 | + | return false; |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | /// Match region positions, which determine the meaning of borrow contracts. |
|
| 159 | + | fn regions(left: &[u32], right: &[u32]) -> bool { |
|
| 160 | + | if left.len <> right.len { |
|
| 161 | + | return false; |
|
| 162 | + | } |
|
| 163 | + | for item, i in left { |
|
| 164 | + | if item <> right[i] { |
|
| 165 | + | return false; |
|
| 166 | + | } |
|
| 167 | + | } |
|
| 168 | + | return true; |
|
| 169 | + | } |
|
| 170 | + | ||
| 171 | + | /// Match a protected slot and enqueue every referenced descriptor. |
|
| 172 | + | fn slot(work: &mut Workspace, left: il::SlotKind, right: il::SlotKind) -> bool throws (Error) { |
|
| 173 | + | match left { |
|
| 174 | + | case il::SlotKind::Ptr(a) => { |
|
| 175 | + | if let case il::SlotKind::Ptr(b) = right { |
|
| 176 | + | return try pointer(work, a, b); |
|
| 177 | + | } |
|
| 178 | + | } |
|
| 179 | + | case il::SlotKind::Slice(a) => { |
|
| 180 | + | if let case il::SlotKind::Slice(b) = right { |
|
| 181 | + | return try pointer(work, a, b); |
|
| 182 | + | } |
|
| 183 | + | } |
|
| 184 | + | case il::SlotKind::Fn(a) => { |
|
| 185 | + | if let case il::SlotKind::Fn(b) = right { |
|
| 186 | + | try edge(work, Kind::Signature, a, b); |
|
| 187 | + | return true; |
|
| 188 | + | } |
|
| 189 | + | } |
|
| 190 | + | case il::SlotKind::Heap(a) => { |
|
| 191 | + | if let case il::SlotKind::Heap(b) = right { |
|
| 192 | + | return a == b; |
|
| 193 | + | } |
|
| 194 | + | } |
|
| 195 | + | case il::SlotKind::Trait(a) => { |
|
| 196 | + | if let case il::SlotKind::Trait(b) = right { |
|
| 197 | + | if a.own <> b.own or a.mutable <> b.mutable { |
|
| 198 | + | return false; |
|
| 199 | + | } |
|
| 200 | + | try edge(work, Kind::Trait, a.iface, b.iface); |
|
| 201 | + | return true; |
|
| 202 | + | } |
|
| 203 | + | } |
|
| 204 | + | case il::SlotKind::Sub(a) => { |
|
| 205 | + | if let case il::SlotKind::Sub(b) = right { |
|
| 206 | + | try edge(work, Kind::Layout, a, b); |
|
| 207 | + | return true; |
|
| 208 | + | } |
|
| 209 | + | } |
|
| 210 | + | case il::SlotKind::Array { layout: a, count: countA } => { |
|
| 211 | + | if let case il::SlotKind::Array { layout: b, count: countB } = right { |
|
| 212 | + | if countA <> countB { |
|
| 213 | + | return false; |
|
| 214 | + | } |
|
| 215 | + | try edge(work, Kind::Layout, a, b); |
|
| 216 | + | return true; |
|
| 217 | + | } |
|
| 218 | + | } |
|
| 219 | + | case il::SlotKind::Union { offset: offsetA, size: sizeA, variants: a } => { |
|
| 220 | + | if let case il::SlotKind::Union { offset: offsetB, size: sizeB, variants: b } = right { |
|
| 221 | + | if offsetA <> offsetB or sizeA <> sizeB or a.len <> b.len { |
|
| 222 | + | return false; |
|
| 223 | + | } |
|
| 224 | + | for variant, i in a { |
|
| 225 | + | if variant.tag <> b[i].tag or not regions(variant.regions, b[i].regions) { |
|
| 226 | + | return false; |
|
| 227 | + | } |
|
| 228 | + | if let child = variant.layout { |
|
| 229 | + | let other = b[i].layout else { |
|
| 230 | + | return false; |
|
| 231 | + | }; |
|
| 232 | + | try edge(work, Kind::Layout, child, other); |
|
| 233 | + | } else if b[i].layout <> nil { |
|
| 234 | + | return false; |
|
| 235 | + | } |
|
| 236 | + | } |
|
| 237 | + | return true; |
|
| 238 | + | } |
|
| 239 | + | } |
|
| 240 | + | } |
|
| 241 | + | return false; |
|
| 242 | + | } |
|
| 243 | + | ||
| 244 | + | /// Check every reachable pair. Invalid table indices are mismatches. |
|
| 245 | + | fn finish(work: &mut Workspace, left: &il::Program, right: &il::Program) -> bool throws (Error) { |
|
| 246 | + | let mut cursor: u32 = 0; |
|
| 247 | + | while cursor < work.count { |
|
| 248 | + | let pair = work.pairs[cursor]; |
|
| 249 | + | set cursor += 1; |
|
| 250 | + | match pair.kind { |
|
| 251 | + | case Kind::Layout => { |
|
| 252 | + | if pair.left >= left.layouts.len or pair.right >= right.layouts.len { |
|
| 253 | + | return false; |
|
| 254 | + | } |
|
| 255 | + | let a = left.layouts[pair.left]; |
|
| 256 | + | let b = right.layouts[pair.right]; |
|
| 257 | + | if a.size <> b.size |
|
| 258 | + | or a.alignment <> b.alignment |
|
| 259 | + | or a.regions <> b.regions |
|
| 260 | + | or a.slots.len <> b.slots.len |
|
| 261 | + | { |
|
| 262 | + | return false; |
|
| 263 | + | } |
|
| 264 | + | for item, i in a.slots { |
|
| 265 | + | let other = b.slots[i]; |
|
| 266 | + | if item.offset <> other.offset |
|
| 267 | + | or not regions(item.regions, other.regions) |
|
| 268 | + | or not try slot(work, item.kind, other.kind) |
|
| 269 | + | { |
|
| 270 | + | return false; |
|
| 271 | + | } |
|
| 272 | + | } |
|
| 273 | + | } |
|
| 274 | + | case Kind::Signature => { |
|
| 275 | + | if pair.left >= left.signatures.len or pair.right >= right.signatures.len { |
|
| 276 | + | return false; |
|
| 277 | + | } |
|
| 278 | + | let a = left.signatures[pair.left]; |
|
| 279 | + | let b = right.signatures[pair.right]; |
|
| 280 | + | if a.params.len <> b.params.len |
|
| 281 | + | or not il::sameBorrows(a.borrows, b.borrows) |
|
| 282 | + | or not try value(work, a.ret, b.ret) |
|
| 283 | + | { |
|
| 284 | + | return false; |
|
| 285 | + | } |
|
| 286 | + | for param, i in a.params { |
|
| 287 | + | if not try value(work, param, b.params[i]) { |
|
| 288 | + | return false; |
|
| 289 | + | } |
|
| 290 | + | } |
|
| 291 | + | } |
|
| 292 | + | case Kind::Trait => { |
|
| 293 | + | if pair.left >= left.traits.len or pair.right >= right.traits.len { |
|
| 294 | + | return false; |
|
| 295 | + | } |
|
| 296 | + | let a = left.traits[pair.left]; |
|
| 297 | + | let b = right.traits[pair.right]; |
|
| 298 | + | if a.methods.len <> b.methods.len { |
|
| 299 | + | return false; |
|
| 300 | + | } |
|
| 301 | + | for method, i in a.methods { |
|
| 302 | + | let other = b.methods[i]; |
|
| 303 | + | if method.receiver <> other.receiver |
|
| 304 | + | or method.own <> other.own |
|
| 305 | + | or method.mutable <> other.mutable |
|
| 306 | + | { |
|
| 307 | + | return false; |
|
| 308 | + | } |
|
| 309 | + | try edge(work, Kind::Signature, method.sig, other.sig); |
|
| 310 | + | } |
|
| 311 | + | } |
|
| 312 | + | } |
|
| 313 | + | } |
|
| 314 | + | return true; |
|
| 315 | + | } |
|
| 316 | + | ||
| 317 | + | /// Compare complete layout graphs across program-local indices. |
|
| 318 | + | export fn sameLayout( |
|
| 319 | + | work: &mut Workspace, left: &il::Program, a: u32, right: &il::Program, b: u32 |
|
| 320 | + | ) -> bool throws (Error) { |
|
| 321 | + | reset(work); |
|
| 322 | + | try edge(work, Kind::Layout, a, b); |
|
| 323 | + | return try finish(work, left, right); |
|
| 324 | + | } |
|
| 325 | + | ||
| 326 | + | /// Compare data declarations and their reachable layout graphs. |
|
| 327 | + | export fn sameData( |
|
| 328 | + | work: &mut Workspace, left: &il::Program, a: &il::Data, right: &il::Program, b: &il::Data |
|
| 329 | + | ) -> bool throws (Error) { |
|
| 330 | + | if a.size <> b.size |
|
| 331 | + | or a.alignment <> b.alignment |
|
| 332 | + | or a.readOnly <> b.readOnly |
|
| 333 | + | or a.heap <> b.heap |
|
| 334 | + | { |
|
| 335 | + | return false; |
|
| 336 | + | } |
|
| 337 | + | return try sameLayout(work, left, a.layout, right, b.layout); |
|
| 338 | + | } |
|
| 339 | + | ||
| 340 | + | /// Compare an importing signature with a provider's declared function. |
|
| 341 | + | export fn sameCallable( |
|
| 342 | + | work: &mut Workspace, left: &il::Program, a: &il::Signature, right: &il::Program, b: &il::Fn |
|
| 343 | + | ) -> bool throws (Error) { |
|
| 344 | + | reset(work); |
|
| 345 | + | if a.params.len <> b.params.len |
|
| 346 | + | or not il::sameBorrows(a.borrows, b.borrows) |
|
| 347 | + | or not try value(work, a.ret, b.returnType) |
|
| 348 | + | { |
|
| 349 | + | return false; |
|
| 350 | + | } |
|
| 351 | + | for param, i in a.params { |
|
| 352 | + | if not try value(work, param, b.params[i].type) { |
|
| 353 | + | return false; |
|
| 354 | + | } |
|
| 355 | + | } |
|
| 356 | + | return try finish(work, left, right); |
|
| 357 | + | } |
lib/std/lang/il/link/tests.rad
added
+228 -0
| 1 | + | //! Cross-package descriptor graph matching. |
|
| 2 | + | ||
| 3 | + | use std::lang::il; |
|
| 4 | + | use std::lang::il::link; |
|
| 5 | + | use std::mem::heap; |
|
| 6 | + | use std::testing; |
|
| 7 | + | ||
| 8 | + | /// Storage for bounded graph comparison. |
|
| 9 | + | static MEMORY: [u8; 32768] = [0; 32768]; |
|
| 10 | + | ||
| 11 | + | /// Plain scalar layout used by pointer targets. |
|
| 12 | + | constant PLAIN: il::Layout = il::Layout { size: 4, alignment: 4, slots: &[], regions: 0 }; |
|
| 13 | + | ||
| 14 | + | /// Construct a program with only a layout graph. |
|
| 15 | + | fn program(layouts: *[il::Layout]) -> il::Program { |
|
| 16 | + | return il::Program { data: &[], fns: &[], layouts, signatures: &[], traits: &[] }; |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | /// Construct a provider declaration from a test signature. |
|
| 20 | + | fn declaration(signature: il::Signature, arena: &heap::Heap) -> il::Fn { |
|
| 21 | + | let mut params: *mut [il::Param] = &mut []; |
|
| 22 | + | for typ, i in signature.params { |
|
| 23 | + | try! params.append(il::Param { value: il::Reg { n: i }, type: typ }, arena); |
|
| 24 | + | } |
|
| 25 | + | return il::Fn { |
|
| 26 | + | name: "provider::call", |
|
| 27 | + | params: ¶ms[..], |
|
| 28 | + | returnType: signature.ret, |
|
| 29 | + | isExtern: true, |
|
| 30 | + | isLeaf: true, |
|
| 31 | + | blocks: &[], |
|
| 32 | + | vals: &[], |
|
| 33 | + | cases: &[], |
|
| 34 | + | borrows: signature.borrows, |
|
| 35 | + | }; |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | /// A cyclic graph matches with different package-local layout indices. |
|
| 39 | + | @test unsafe fn recursiveLayouts() throws (testing::TestError) { |
|
| 40 | + | let left = program(&[ |
|
| 41 | + | il::Layout { |
|
| 42 | + | size: 8, |
|
| 43 | + | alignment: 8, |
|
| 44 | + | slots: &[ |
|
| 45 | + | il::Slot { |
|
| 46 | + | offset: 0, |
|
| 47 | + | kind: il::SlotKind::Ptr(il::PtrType { |
|
| 48 | + | layout: 0, own: true, mutable: false, nullable: true |
|
| 49 | + | }), |
|
| 50 | + | regions: &[], |
|
| 51 | + | }, |
|
| 52 | + | ], |
|
| 53 | + | regions: 0, |
|
| 54 | + | }, |
|
| 55 | + | ]); |
|
| 56 | + | let right = program( |
|
| 57 | + | &[ |
|
| 58 | + | il::Layout { size: 1, alignment: 1, slots: &[], regions: 0 }, |
|
| 59 | + | il::Layout { |
|
| 60 | + | size: 8, |
|
| 61 | + | alignment: 8, |
|
| 62 | + | slots: &[ |
|
| 63 | + | il::Slot { |
|
| 64 | + | offset: 0, |
|
| 65 | + | kind: il::SlotKind::Ptr(il::PtrType { |
|
| 66 | + | layout: 1, own: true, mutable: false, nullable: true |
|
| 67 | + | }), |
|
| 68 | + | regions: &[], |
|
| 69 | + | }, |
|
| 70 | + | ], |
|
| 71 | + | regions: 0, |
|
| 72 | + | }, |
|
| 73 | + | ] |
|
| 74 | + | ); |
|
| 75 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 76 | + | let mut work = try! link::workspace(32, arena); |
|
| 77 | + | assert try! link::sameLayout(&mut work, &left, 0, &right, 1); |
|
| 78 | + | assert not try! link::sameLayout(&mut work, &left, 0, &right, 0); |
|
| 79 | + | assert try! link::sameLayout(&mut work, &left, 0, &right, 1); |
|
| 80 | + | } |
|
| 81 | + | ||
| 82 | + | /// Pointer rights are checked after following an otherwise equal outer layout. |
|
| 83 | + | @test unsafe fn nestedRights() throws (testing::TestError) { |
|
| 84 | + | let left = program( |
|
| 85 | + | &[ |
|
| 86 | + | PLAIN, |
|
| 87 | + | il::Layout { |
|
| 88 | + | size: 8, |
|
| 89 | + | alignment: 8, |
|
| 90 | + | slots: &[ |
|
| 91 | + | il::Slot { |
|
| 92 | + | offset: 0, |
|
| 93 | + | kind: il::SlotKind::Ptr(il::PtrType { |
|
| 94 | + | layout: 0, own: true, mutable: true, nullable: true |
|
| 95 | + | }), |
|
| 96 | + | regions: &[], |
|
| 97 | + | }, |
|
| 98 | + | ], |
|
| 99 | + | regions: 0, |
|
| 100 | + | }, |
|
| 101 | + | ] |
|
| 102 | + | ); |
|
| 103 | + | let right = program( |
|
| 104 | + | &[ |
|
| 105 | + | PLAIN, |
|
| 106 | + | il::Layout { |
|
| 107 | + | size: 8, |
|
| 108 | + | alignment: 8, |
|
| 109 | + | slots: &[ |
|
| 110 | + | il::Slot { |
|
| 111 | + | offset: 0, |
|
| 112 | + | kind: il::SlotKind::Ptr(il::PtrType { |
|
| 113 | + | layout: 0, own: true, mutable: false, nullable: true |
|
| 114 | + | }), |
|
| 115 | + | regions: &[], |
|
| 116 | + | }, |
|
| 117 | + | ], |
|
| 118 | + | regions: 0, |
|
| 119 | + | }, |
|
| 120 | + | ] |
|
| 121 | + | ); |
|
| 122 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 123 | + | let mut work = try! link::workspace(32, arena); |
|
| 124 | + | assert not try! link::sameLayout(&mut work, &left, 1, &right, 1); |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Borrow source positions must match even when all physical types match. |
|
| 128 | + | @test unsafe fn borrowSources() throws (testing::TestError) { |
|
| 129 | + | let pointer = il::ValType::Ptr(il::PtrType { |
|
| 130 | + | layout: 0, own: false, mutable: false, nullable: false |
|
| 131 | + | }); |
|
| 132 | + | let mut params: *mut [il::ValType] = &mut []; |
|
| 133 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 134 | + | try! params.append(pointer, arena); |
|
| 135 | + | try! params.append(pointer, arena); |
|
| 136 | + | let mut signatures: *mut [il::Signature] = &mut []; |
|
| 137 | + | try! signatures.append( |
|
| 138 | + | il::Signature { |
|
| 139 | + | params: ¶ms[..], |
|
| 140 | + | ret: pointer, |
|
| 141 | + | borrows: &[ |
|
| 142 | + | il::Borrow { target: nil, result: 0, param: 0, source: nil } |
|
| 143 | + | ], |
|
| 144 | + | }, |
|
| 145 | + | arena |
|
| 146 | + | ); |
|
| 147 | + | let left = il::Program { |
|
| 148 | + | data: &[], fns: &[], layouts: &[PLAIN], signatures: &signatures[..], traits: &[] |
|
| 149 | + | }; |
|
| 150 | + | let mut right = left; |
|
| 151 | + | let mut changed: *mut [il::Signature] = &mut []; |
|
| 152 | + | try! changed.append( |
|
| 153 | + | il::Signature { |
|
| 154 | + | params: left.signatures[0].params, |
|
| 155 | + | ret: pointer, |
|
| 156 | + | borrows: &[ |
|
| 157 | + | il::Borrow { target: nil, result: 0, param: 1, source: nil } |
|
| 158 | + | ], |
|
| 159 | + | }, |
|
| 160 | + | arena |
|
| 161 | + | ); |
|
| 162 | + | set right.signatures = &changed[..]; |
|
| 163 | + | let mut work = try! link::workspace(8, arena); |
|
| 164 | + | let mut callable = declaration(left.signatures[0], arena); |
|
| 165 | + | assert try! link::sameCallable(&mut work, &left, &left.signatures[0], &left, &callable); |
|
| 166 | + | set callable.borrows = right.signatures[0].borrows; |
|
| 167 | + | assert not try! link::sameCallable(&mut work, &left, &left.signatures[0], &right, &callable); |
|
| 168 | + | } |
|
| 169 | + | ||
| 170 | + | /// Method receivers and nested scalar widths are part of trait contracts. |
|
| 171 | + | @test unsafe fn traitContracts() throws (testing::TestError) { |
|
| 172 | + | let left = il::Program { |
|
| 173 | + | data: &[], |
|
| 174 | + | fns: &[], |
|
| 175 | + | layouts: &[ |
|
| 176 | + | il::Layout { |
|
| 177 | + | size: 16, |
|
| 178 | + | alignment: 8, |
|
| 179 | + | slots: &[ |
|
| 180 | + | il::Slot { |
|
| 181 | + | offset: 0, |
|
| 182 | + | kind: il::SlotKind::Trait(il::ObjectType { |
|
| 183 | + | iface: 0, own: false, mutable: false |
|
| 184 | + | }), |
|
| 185 | + | regions: &[0], |
|
| 186 | + | }, |
|
| 187 | + | ], |
|
| 188 | + | regions: 1, |
|
| 189 | + | }, |
|
| 190 | + | ], |
|
| 191 | + | signatures: &[ |
|
| 192 | + | il::Signature { params: &[], ret: il::ValType::Data(il::Type::W32), borrows: &[] } |
|
| 193 | + | ], |
|
| 194 | + | traits: &[ |
|
| 195 | + | il::Trait { |
|
| 196 | + | methods: &[ |
|
| 197 | + | il::Method { sig: 0, receiver: 0, own: false, mutable: false } |
|
| 198 | + | ], |
|
| 199 | + | }, |
|
| 200 | + | ], |
|
| 201 | + | }; |
|
| 202 | + | let arena = heap::over(&mut MEMORY[..]); |
|
| 203 | + | let mut work = try! link::workspace(8, arena); |
|
| 204 | + | assert try! link::sameLayout(&mut work, &left, 0, &left, 0); |
|
| 205 | + | let mut right = left; |
|
| 206 | + | set right.traits = &[ |
|
| 207 | + | il::Trait { |
|
| 208 | + | methods: &[ |
|
| 209 | + | il::Method { sig: 0, receiver: 0, own: false, mutable: true } |
|
| 210 | + | ], |
|
| 211 | + | }, |
|
| 212 | + | ]; |
|
| 213 | + | assert not try! link::sameLayout(&mut work, &left, 0, &right, 0); |
|
| 214 | + | set right.traits = left.traits; |
|
| 215 | + | set right.signatures = &[ |
|
| 216 | + | il::Signature { params: &[], ret: il::ValType::Data(il::Type::W64), borrows: &[] } |
|
| 217 | + | ]; |
|
| 218 | + | assert not try! link::sameLayout(&mut work, &left, 0, &right, 0); |
|
| 219 | + | let mut small = try! link::workspace(1, arena); |
|
| 220 | + | let mut limited = false; |
|
| 221 | + | try link::sameLayout(&mut small, &left, 0, &left, 0) catch error { |
|
| 222 | + | assert error == link::Error::Limit; |
|
| 223 | + | set limited = true; |
|
| 224 | + | }; |
|
| 225 | + | assert limited; |
|
| 226 | + | let callable = declaration(left.signatures[0], arena); |
|
| 227 | + | assert try! link::sameCallable(&mut small, &left, &left.signatures[0], &left, &callable); |
|
| 228 | + | } |
lib/std/lang/il/verify.rad
+63 -29
| 24 | 24 | //! checks everything else, and [`verifyPackage`] adds the rules of a loadable package. |
|
| 25 | 25 | ||
| 26 | 26 | export mod layout; |
|
| 27 | 27 | export mod flow; |
|
| 28 | 28 | export mod data; |
|
| 29 | + | export mod linking; |
|
| 29 | 30 | ||
| 30 | 31 | @test export mod tests; |
|
| 31 | 32 | ||
| 32 | - | use std::mem; |
|
| 33 | 33 | use std::collections::dict; |
|
| 34 | - | use std::mem::heap; |
|
| 35 | 34 | use std::lang::il; |
|
| 36 | 35 | use std::lang::il::binary; |
|
| 36 | + | use std::mem; |
|
| 37 | + | use std::mem::heap; |
|
| 37 | 38 | ||
| 38 | 39 | /////////////// |
|
| 39 | 40 | // Constants // |
|
| 40 | 41 | /////////////// |
|
| 41 | 42 |
| 243 | 244 | table: layout::Table, |
|
| 244 | 245 | /// Index of each function by name. |
|
| 245 | 246 | fnNames: dict::Dict, |
|
| 246 | 247 | /// Index of each data definition by name. |
|
| 247 | 248 | dataNames: dict::Dict, |
|
| 249 | + | /// Number of function definitions supplied by this package. |
|
| 250 | + | localFunctions: u32, |
|
| 251 | + | /// Number of data initializers supplied by this package. |
|
| 252 | + | localData: u32, |
|
| 248 | 253 | } |
|
| 249 | 254 | ||
| 250 | 255 | /// State of the verification of one function. |
|
| 251 | 256 | record Verifier { |
|
| 252 | 257 | /// Facts about the program. |
| 675 | 680 | } |
|
| 676 | 681 | case il::Val::FnAddr(name) => { |
|
| 677 | 682 | let index = dict::get(&v.ctx.fnNames, name) else { |
|
| 678 | 683 | throw fail(v, Rule::Symbol); |
|
| 679 | 684 | }; |
|
| 680 | - | if v.ctx.prog.fns[index as u32].isExtern { |
|
| 685 | + | if v.ctx.prog.fns[index as u32].isExtern and index as u32 < v.ctx.localFunctions { |
|
| 681 | 686 | throw fail(v, Rule::Symbol); |
|
| 682 | 687 | } |
|
| 683 | 688 | return RegState::Fn(FnState { sig: SigRef::Func(index as u32), nullable: false }); |
|
| 684 | 689 | } |
|
| 685 | 690 | } |
| 2484 | 2489 | } |
|
| 2485 | 2490 | try dict::insert(&mut dataNames, name, i as i32) catch { |
|
| 2486 | 2491 | throw STORAGE; |
|
| 2487 | 2492 | }; |
|
| 2488 | 2493 | } |
|
| 2489 | - | let c = Context { prog: *program, config, table, fnNames, dataNames }; |
|
| 2494 | + | let c = Context { |
|
| 2495 | + | prog: *program, |
|
| 2496 | + | config, |
|
| 2497 | + | table, |
|
| 2498 | + | fnNames, |
|
| 2499 | + | dataNames, |
|
| 2500 | + | localFunctions: program.fns.len, |
|
| 2501 | + | localData: program.data.len, |
|
| 2502 | + | }; |
|
| 2490 | 2503 | try checkSignatures(&c); |
|
| 2491 | 2504 | try checkTraits(&c); |
|
| 2492 | 2505 | return c; |
|
| 2493 | 2506 | } |
|
| 2494 | 2507 |
| 2573 | 2586 | else => return false, |
|
| 2574 | 2587 | } |
|
| 2575 | 2588 | } |
|
| 2576 | 2589 | ||
| 2577 | 2590 | /// Check the rules of a loadable package that [`verifyProgram`] does not cover. |
|
| 2578 | - | /// A package stands alone, so it has no dependencies and no extern functions. Its entry |
|
| 2579 | - | /// takes no parameter or one pointer to the plain Env record. |
|
| 2591 | + | /// Local definitions have bodies, and the domain entry takes no parameter or one |
|
| 2592 | + | /// pointer to the plain Env record. Imports are checked against their providers. |
|
| 2580 | 2593 | fn checkPackage(package: &binary::Package, c: &Context) throws (Error) { |
|
| 2581 | 2594 | let bad = Error { rule: Rule::Package, place: Place::Package }; |
|
| 2582 | - | if package.dependencies.len > 0 { |
|
| 2583 | - | throw bad; |
|
| 2584 | - | } |
|
| 2585 | 2595 | for i in 0 .. package.program.fns.len { |
|
| 2586 | 2596 | if package.program.fns[i].isExtern { |
|
| 2587 | 2597 | throw failFn(i, Rule::Package); |
|
| 2588 | 2598 | } |
|
| 2589 | 2599 | } |
|
| 2590 | 2600 | if let name = package.entry { |
|
| 2591 | 2601 | let index = dict::get(&c.fnNames, name) else { |
|
| 2592 | 2602 | throw bad; |
|
| 2593 | 2603 | }; |
|
| 2604 | + | if index as u32 >= c.localFunctions { |
|
| 2605 | + | throw bad; |
|
| 2606 | + | } |
|
| 2594 | 2607 | let func = package.program.fns[index as u32]; |
|
| 2595 | 2608 | if func.params.len > 1 { |
|
| 2596 | 2609 | throw failFn(index as u32, Rule::Package); |
|
| 2597 | 2610 | } |
|
| 2598 | 2611 | for param in func.params { |
| 2612 | 2625 | } |
|
| 2613 | 2626 | } |
|
| 2614 | 2627 | for item in package.exports { |
|
| 2615 | 2628 | match item.kind { |
|
| 2616 | 2629 | case binary::ExportKind::Function => { |
|
| 2617 | - | if dict::get(&c.fnNames, item.name) == nil { |
|
| 2630 | + | let index = dict::get(&c.fnNames, item.name) else throw bad; |
|
| 2631 | + | if index as u32 >= c.localFunctions { |
|
| 2618 | 2632 | throw bad; |
|
| 2619 | 2633 | } |
|
| 2620 | 2634 | } |
|
| 2621 | 2635 | case binary::ExportKind::Data => { |
|
| 2622 | - | if dict::get(&c.dataNames, item.name) == nil { |
|
| 2636 | + | let index = dict::get(&c.dataNames, item.name) else throw bad; |
|
| 2637 | + | if index as u32 >= c.localData { |
|
| 2623 | 2638 | throw bad; |
|
| 2624 | 2639 | } |
|
| 2625 | 2640 | } |
|
| 2626 | 2641 | } |
|
| 2627 | 2642 | } |
| 2630 | 2645 | /// Number of arguments that the kernel passes to a new context, in registers a0 and a1. |
|
| 2631 | 2646 | /// The kernel defines no other argument, and a parameter past the argument registers would |
|
| 2632 | 2647 | /// be read from above the fresh stack. |
|
| 2633 | 2648 | constant START_ARGS: u32 = 2; |
|
| 2634 | 2649 | ||
| 2635 | - | /// Whether a context may start at the exported function `name`: it is the entry, which |
|
| 2636 | - | /// receives the Env pointer, or takes at most `START_ARGS` data arguments and returns data, |
|
| 2637 | - | /// so that a context starts with plain arguments and no forged pointer. |
|
| 2650 | + | /// Whether an exported function accepts the plain arguments of a fresh context. |
|
| 2651 | + | /// The domain entry uses its separate Env contract. |
|
| 2638 | 2652 | fn startable(package: &binary::Package, c: &Context, name: *[u8]) -> bool { |
|
| 2639 | 2653 | if let entry = package.entry { |
|
| 2640 | 2654 | if mem::eq(entry, name) { |
|
| 2641 | - | return true; |
|
| 2655 | + | return false; |
|
| 2642 | 2656 | } |
|
| 2643 | 2657 | } |
|
| 2644 | 2658 | let index = dict::get(&c.fnNames, name) else { |
|
| 2645 | 2659 | return false; |
|
| 2646 | 2660 | }; |
| 2654 | 2668 | } |
|
| 2655 | 2669 | } |
|
| 2656 | 2670 | return true; |
|
| 2657 | 2671 | } |
|
| 2658 | 2672 | ||
| 2659 | - | /// Narrow the exports of the package to its data and its startable functions, allocated |
|
| 2660 | - | /// from `keep`. A package that passes [`checkPackage`] has no dependents, so the other |
|
| 2661 | - | /// exports serve only to share code between its modules. |
|
| 2662 | - | fn narrowExports(package: &mut binary::Package, c: &Context, keep: &heap::Heap) |
|
| 2673 | + | /// Classify public functions for checked library calls and fresh context creation. |
|
| 2674 | + | /// The retained export metadata is allocated from keep and must outlive the package. |
|
| 2675 | + | fn classifyExports(package: &mut binary::Package, c: &Context, keep: &heap::Heap) |
|
| 2663 | 2676 | throws (Error) |
|
| 2664 | 2677 | { |
|
| 2665 | 2678 | let mut kept: *mut [binary::Export] = &mut []; |
|
| 2666 | 2679 | try kept.reserve(package.exports.len, keep) catch { |
|
| 2667 | 2680 | throw STORAGE; |
|
| 2668 | 2681 | }; |
|
| 2669 | 2682 | for item in package.exports { |
|
| 2670 | - | if item.kind == binary::ExportKind::Function and not startable(package, c, item.name) { |
|
| 2671 | - | continue; |
|
| 2672 | - | } |
|
| 2673 | - | try kept.append(item, keep) catch { |
|
| 2683 | + | let eligible = |
|
| 2684 | + | item.kind == binary::ExportKind::Function and startable(package, c, item.name); |
|
| 2685 | + | try kept.append( |
|
| 2686 | + | binary::Export { |
|
| 2687 | + | name: item.name, kind: item.kind, startable: eligible |
|
| 2688 | + | }, |
|
| 2689 | + | keep |
|
| 2690 | + | ) catch { |
|
| 2674 | 2691 | throw STORAGE; |
|
| 2675 | 2692 | }; |
|
| 2676 | 2693 | } |
|
| 2677 | 2694 | set package.exports = &kept[..]; |
|
| 2678 | 2695 | } |
|
| 2679 | 2696 | ||
| 2680 | - | /// Verify a package that the kernel loads: the rules of [`verifyProgram`] and those of a |
|
| 2681 | - | /// self-contained package with a safe entry. On success, the exports of the package are |
|
| 2682 | - | /// narrowed to the startable ones, in storage allocated from `keep`, which must outlive |
|
| 2683 | - | /// the package. Other exported functions stay callable within the package. |
|
| 2697 | + | /// Verify a loadable package with a safe domain entry. Public functions retain their |
|
| 2698 | + | /// checked call contracts and receive separate fresh-context entry classifications. |
|
| 2699 | + | /// Derived export metadata in keep must outlive the package. |
|
| 2684 | 2700 | export fn verifyPackage( |
|
| 2685 | 2701 | package: &mut binary::Package, |
|
| 2686 | 2702 | config: Config, |
|
| 2687 | 2703 | scratch: &heap::Heap, |
|
| 2688 | 2704 | keep: &heap::Heap |
|
| 2689 | 2705 | ) throws (Error) { |
|
| 2690 | - | let c = try prepare(&package.program, config, scratch); |
|
| 2706 | + | try verifyLinkedPackage(package, &[], config, scratch, keep); |
|
| 2707 | + | } |
|
| 2708 | + | ||
| 2709 | + | /// Verify local bodies and every import against already verified dependency images. |
|
| 2710 | + | /// Providers are in exact dependency order and must describe immutable resident code. |
|
| 2711 | + | /// Derived export classifications are retained in keep; scratch holds only temporary data. |
|
| 2712 | + | export fn verifyLinkedPackage( |
|
| 2713 | + | package: &mut binary::Package, |
|
| 2714 | + | providers: &[linking::Provider], |
|
| 2715 | + | config: Config, |
|
| 2716 | + | scratch: &heap::Heap, |
|
| 2717 | + | keep: &heap::Heap |
|
| 2718 | + | ) throws (Error) { |
|
| 2719 | + | set package.links.verified = false; |
|
| 2720 | + | let resolved = try linking::bind(package, providers, scratch); |
|
| 2721 | + | let mut c = try prepare(&resolved, config, scratch); |
|
| 2722 | + | set c.localFunctions = package.program.fns.len; |
|
| 2723 | + | set c.localData = package.program.data.len; |
|
| 2691 | 2724 | try checkPackage(package, &c); |
|
| 2692 | - | try narrowExports(package, &c, keep); |
|
| 2725 | + | try classifyExports(package, &c, keep); |
|
| 2693 | 2726 | try finish(c, scratch); |
|
| 2727 | + | set package.links.verified = true; |
|
| 2694 | 2728 | } |
lib/std/lang/il/verify/data.rad
+3 -3
| 261 | 261 | for i in 0 .. live.tags.len { |
|
| 262 | 262 | set live.tags[i] = nil; |
|
| 263 | 263 | } |
|
| 264 | 264 | set live.pending = nil; |
|
| 265 | 265 | for value in data.values { |
|
| 266 | - | // The index of the symbol that the item names. A function must have a body. |
|
| 266 | + | // The symbol names local code or a checked import from resident code. |
|
| 267 | 267 | let mut symbol: u32 = 0; |
|
| 268 | 268 | match value.item { |
|
| 269 | 269 | case il::DataItem::Sym(name) => { |
|
| 270 | 270 | let target = dict::get(&c.dataNames, name) else { |
|
| 271 | 271 | throw fail(index, verify::Rule::Symbol); |
| 274 | 274 | } |
|
| 275 | 275 | case il::DataItem::Fn(name) => { |
|
| 276 | 276 | let target = dict::get(&c.fnNames, name) else { |
|
| 277 | 277 | throw fail(index, verify::Rule::Symbol); |
|
| 278 | 278 | }; |
|
| 279 | - | if c.prog.fns[target as u32].isExtern { |
|
| 279 | + | if c.prog.fns[target as u32].isExtern and target as u32 < c.localFunctions { |
|
| 280 | 280 | throw fail(index, verify::Rule::Symbol); |
|
| 281 | 281 | } |
|
| 282 | 282 | set symbol = target as u32; |
|
| 283 | 283 | } |
|
| 284 | 284 | else => {} |
| 315 | 315 | let mut tags: *mut [?UnionTag] = &mut []; |
|
| 316 | 316 | try tags.resize(depth, nil, a) catch { |
|
| 317 | 317 | throw verify::STORAGE; |
|
| 318 | 318 | }; |
|
| 319 | 319 | let mut live = Live { pending: nil, tags }; |
|
| 320 | - | for i in 0 .. c.prog.data.len { |
|
| 320 | + | for i in 0 .. c.localData { |
|
| 321 | 321 | try checkData(c, i, &mut live); |
|
| 322 | 322 | } |
|
| 323 | 323 | } |
lib/std/lang/il/verify/linking.rad
added
+203 -0
| 1 | + | //! Typed import environments for exact verified provider images. |
|
| 2 | + | ||
| 3 | + | use std::lang::il; |
|
| 4 | + | use std::lang::il::binary; |
|
| 5 | + | use std::lang::il::link; |
|
| 6 | + | use std::lang::il::verify; |
|
| 7 | + | use std::mem; |
|
| 8 | + | use std::mem::heap; |
|
| 9 | + | ||
| 10 | + | /// Maximum distinct descriptor pairs in one import comparison. |
|
| 11 | + | constant PAIRS: u32 = 4096; |
|
| 12 | + | ||
| 13 | + | /// Declarations of code which the caller has already verified. |
|
| 14 | + | /// Kernel callers obtain these only from verified resident image descriptors. |
|
| 15 | + | export record Provider: Copy { |
|
| 16 | + | /// Identity of the resident image's complete immutable source payload. |
|
| 17 | + | image: binary::Id, |
|
| 18 | + | /// Decoded declarations from that verified image, retained for this check. |
|
| 19 | + | contract: binary::Package, |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | /// A loadable-package contract violation. |
|
| 23 | + | constant BAD: verify::Error = verify::Error { |
|
| 24 | + | rule: verify::Rule::Package, place: verify::Place::Package |
|
| 25 | + | }; |
|
| 26 | + | ||
| 27 | + | /// Convert a graph workspace failure into the verifier's resource errors. |
|
| 28 | + | fn graphError(error: link::Error) -> verify::Error { |
|
| 29 | + | match error { |
|
| 30 | + | case link::Error::Limit => |
|
| 31 | + | return verify::Error { |
|
| 32 | + | rule: verify::Rule::Limit, place: verify::Place::Package |
|
| 33 | + | }, |
|
| 34 | + | case link::Error::Storage => return verify::STORAGE, |
|
| 35 | + | } |
|
| 36 | + | } |
|
| 37 | + | ||
| 38 | + | /// Append a local signature as a checked external declaration. |
|
| 39 | + | fn function( |
|
| 40 | + | fns: &mut *mut [*il::Fn], name: *[u8], sig: il::Signature, arena: &heap::Heap |
|
| 41 | + | ) throws (verify::Error) { |
|
| 42 | + | let mut params: *mut [il::Param] = &mut []; |
|
| 43 | + | for typ, i in sig.params { |
|
| 44 | + | try params.append(il::Param { value: il::Reg { n: i }, type: typ }, arena) catch { |
|
| 45 | + | throw verify::STORAGE; |
|
| 46 | + | }; |
|
| 47 | + | } |
|
| 48 | + | let mut declaration: *mut [il::Fn] = &mut []; |
|
| 49 | + | try declaration.append( |
|
| 50 | + | il::Fn { |
|
| 51 | + | name, |
|
| 52 | + | params: ¶ms[..], |
|
| 53 | + | returnType: sig.ret, |
|
| 54 | + | isExtern: true, |
|
| 55 | + | isLeaf: true, |
|
| 56 | + | blocks: &[], |
|
| 57 | + | vals: &[], |
|
| 58 | + | cases: &[], |
|
| 59 | + | borrows: sig.borrows, |
|
| 60 | + | }, |
|
| 61 | + | arena |
|
| 62 | + | ) catch { |
|
| 63 | + | throw verify::STORAGE; |
|
| 64 | + | }; |
|
| 65 | + | let frozen: *[il::Fn] = &declaration[..]; |
|
| 66 | + | try fns.append(&frozen[0], arena) catch { |
|
| 67 | + | throw verify::STORAGE; |
|
| 68 | + | }; |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | /// Match explicit imports to verified provider exports and extend only the verifier's |
|
| 72 | + | /// symbol environment. Imported metadata has no local native allocation. |
|
| 73 | + | export fn bind( |
|
| 74 | + | package: &binary::Package, providers: &[Provider], arena: &heap::Heap |
|
| 75 | + | ) -> il::Program throws (verify::Error) { |
|
| 76 | + | if package.dependencies.len <> providers.len or package.links.pins.len <> providers.len { |
|
| 77 | + | throw BAD; |
|
| 78 | + | } |
|
| 79 | + | for provider, i in providers { |
|
| 80 | + | let pin = package.links.pins[i]; |
|
| 81 | + | if not mem::eq(&pin.bytes[..], &provider.image.bytes[..]) { |
|
| 82 | + | throw BAD; |
|
| 83 | + | } |
|
| 84 | + | for earlier in &providers[.. i] { |
|
| 85 | + | if mem::eq(&earlier.image.bytes[..], &provider.image.bytes[..]) { |
|
| 86 | + | throw BAD; |
|
| 87 | + | } |
|
| 88 | + | } |
|
| 89 | + | } |
|
| 90 | + | if package.links.imports.len == 0 { |
|
| 91 | + | return package.program; |
|
| 92 | + | } |
|
| 93 | + | let leftCount = package.program.layouts.len as u64 |
|
| 94 | + | + package.program.signatures.len as u64 |
|
| 95 | + | + package.program.traits.len as u64; |
|
| 96 | + | let mut rightCount: u64 = 0; |
|
| 97 | + | for provider in providers { |
|
| 98 | + | let p = provider.contract.program; |
|
| 99 | + | let count = p.layouts.len as u64 + p.signatures.len as u64 + p.traits.len as u64; |
|
| 100 | + | if count > rightCount { |
|
| 101 | + | set rightCount = count; |
|
| 102 | + | } |
|
| 103 | + | } |
|
| 104 | + | let product = leftCount * rightCount; |
|
| 105 | + | let mut capacity: u32 = 1; |
|
| 106 | + | if product > 0 { |
|
| 107 | + | set capacity = PAIRS if product > PAIRS as u64 else product as u32; |
|
| 108 | + | } |
|
| 109 | + | let mut work = try link::workspace(capacity, arena) catch error { |
|
| 110 | + | throw graphError(error); |
|
| 111 | + | }; |
|
| 112 | + | let mut fns: *mut [*il::Fn] = &mut []; |
|
| 113 | + | let mut data: *mut [il::Data] = &mut []; |
|
| 114 | + | try fns.extend(package.program.fns, arena) catch { |
|
| 115 | + | throw verify::STORAGE; |
|
| 116 | + | }; |
|
| 117 | + | try data.extend(package.program.data, arena) catch { |
|
| 118 | + | throw verify::STORAGE; |
|
| 119 | + | }; |
|
| 120 | + | for imported in package.links.imports { |
|
| 121 | + | if imported.dependency >= providers.len { |
|
| 122 | + | throw BAD; |
|
| 123 | + | } |
|
| 124 | + | let provider = providers[imported.dependency].contract; |
|
| 125 | + | let mut exported: ?binary::ExportKind = nil; |
|
| 126 | + | for entry in provider.exports { |
|
| 127 | + | if mem::eq(entry.name, imported.exported) { |
|
| 128 | + | if exported <> nil { |
|
| 129 | + | throw BAD; |
|
| 130 | + | } |
|
| 131 | + | set exported = entry.kind; |
|
| 132 | + | } |
|
| 133 | + | } |
|
| 134 | + | let exportKind = exported else throw BAD; |
|
| 135 | + | match imported.kind { |
|
| 136 | + | case binary::ImportKind::Function(index) => { |
|
| 137 | + | if exportKind <> binary::ExportKind::Function |
|
| 138 | + | or index >= package.program.signatures.len |
|
| 139 | + | { |
|
| 140 | + | throw BAD; |
|
| 141 | + | } |
|
| 142 | + | let mut target: ?*il::Fn = nil; |
|
| 143 | + | for func in provider.program.fns { |
|
| 144 | + | if mem::eq(func.name, imported.exported) { |
|
| 145 | + | set target = func; |
|
| 146 | + | } |
|
| 147 | + | } |
|
| 148 | + | let definition = target else throw BAD; |
|
| 149 | + | let sig = package.program.signatures[index]; |
|
| 150 | + | let matches = try link::sameCallable( |
|
| 151 | + | &mut work, &package.program, &sig, &provider.program, definition |
|
| 152 | + | ) catch error { |
|
| 153 | + | throw graphError(error); |
|
| 154 | + | }; |
|
| 155 | + | if not matches { |
|
| 156 | + | throw BAD; |
|
| 157 | + | } |
|
| 158 | + | try function(&mut fns, imported.name, sig, arena); |
|
| 159 | + | } |
|
| 160 | + | case binary::ImportKind::Data { layout, readOnly, heap } => { |
|
| 161 | + | if exportKind <> binary::ExportKind::Data or layout >= package.program.layouts.len { |
|
| 162 | + | throw BAD; |
|
| 163 | + | } |
|
| 164 | + | let mut target: ?il::Data = nil; |
|
| 165 | + | for item in provider.program.data { |
|
| 166 | + | if mem::eq(item.name, imported.exported) { |
|
| 167 | + | set target = item; |
|
| 168 | + | } |
|
| 169 | + | } |
|
| 170 | + | let definition = target else throw BAD; |
|
| 171 | + | let info = package.program.layouts[layout]; |
|
| 172 | + | let declaration = il::Data { |
|
| 173 | + | name: imported.name, |
|
| 174 | + | size: info.size, |
|
| 175 | + | alignment: info.alignment, |
|
| 176 | + | layout, |
|
| 177 | + | readOnly, |
|
| 178 | + | heap, |
|
| 179 | + | isZeroInit: definition.isZeroInit, |
|
| 180 | + | values: definition.values, |
|
| 181 | + | }; |
|
| 182 | + | let matches = try link::sameData( |
|
| 183 | + | &mut work, &package.program, &declaration, &provider.program, &definition |
|
| 184 | + | ) catch error { |
|
| 185 | + | throw graphError(error); |
|
| 186 | + | }; |
|
| 187 | + | if not matches { |
|
| 188 | + | throw BAD; |
|
| 189 | + | } |
|
| 190 | + | try data.append(declaration, arena) catch { |
|
| 191 | + | throw verify::STORAGE; |
|
| 192 | + | }; |
|
| 193 | + | } |
|
| 194 | + | } |
|
| 195 | + | } |
|
| 196 | + | return il::Program { |
|
| 197 | + | data: &data[..], |
|
| 198 | + | fns: &fns[..], |
|
| 199 | + | layouts: package.program.layouts, |
|
| 200 | + | signatures: package.program.signatures, |
|
| 201 | + | traits: package.program.traits, |
|
| 202 | + | }; |
|
| 203 | + | } |
lib/std/lang/il/verify/tests.rad
+137 -16
| 2 | 2 | //! |
|
| 3 | 3 | //! A test builds a small program with [`Builder`], and checks that the verifier accepts it |
|
| 4 | 4 | //! or rejects it with a given rule. Every program starts with the layouts and signatures |
|
| 5 | 5 | //! named by the constants below. |
|
| 6 | 6 | ||
| 7 | - | use std::testing; |
|
| 8 | - | use std::mem::heap; |
|
| 9 | 7 | use std::lang::il; |
|
| 10 | 8 | use std::lang::il::binary; |
|
| 9 | + | use std::lang::il::binary::linkage; |
|
| 11 | 10 | use std::lang::il::verify; |
|
| 12 | 11 | use std::lang::il::verify::layout; |
|
| 12 | + | use std::lang::il::verify::linking; |
|
| 13 | + | use std::mem::heap; |
|
| 14 | + | use std::testing; |
|
| 13 | 15 | ||
| 14 | 16 | /////////////// |
|
| 15 | 17 | // Constants // |
|
| 16 | 18 | /////////////// |
|
| 17 | 19 |
| 82 | 84 | ||
| 83 | 85 | /// Storage for the program under test. |
|
| 84 | 86 | static BUILD: [u8; 65536] = [0; 65536]; |
|
| 85 | 87 | /// Working storage of the verifier. |
|
| 86 | 88 | static SCRATCH: [u8; 65536] = [0; 65536]; |
|
| 87 | - | /// Storage for the narrowed exports. |
|
| 89 | + | /// Storage for derived export classifications. |
|
| 88 | 90 | static KEEP: [u8; 4096] = [0; 4096]; |
|
| 89 | 91 | ||
| 90 | 92 | /////////////////// |
|
| 91 | 93 | // Program Build // |
|
| 92 | 94 | /////////////////// |
| 3143 | 3145 | b: &mut Builder, dependencies: *[*[u8]], entry: ?*[u8], exports: &[binary::Export] |
|
| 3144 | 3146 | ) -> ?verify::Error { |
|
| 3145 | 3147 | let mut owned: *mut [binary::Export] = &mut []; |
|
| 3146 | 3148 | try! owned.extend(exports, b.arena); |
|
| 3147 | 3149 | let mut package = binary::Package { |
|
| 3148 | - | symbols: &[], name: "p", dependencies, exports: &owned[..], entry, program: program(b) |
|
| 3150 | + | symbols: &[], |
|
| 3151 | + | name: "p", |
|
| 3152 | + | dependencies, |
|
| 3153 | + | exports: &owned[..], |
|
| 3154 | + | entry, |
|
| 3155 | + | program: program(b), |
|
| 3156 | + | links: binary::EMPTY_LINKS, |
|
| 3149 | 3157 | }; |
|
| 3150 | 3158 | let scratch = heap::over(&mut SCRATCH[..]); |
|
| 3151 | 3159 | let keep = heap::over(&mut KEEP[..]); |
|
| 3152 | 3160 | try verify::verifyPackage(&mut package, CONFIG, scratch, keep) catch err { |
|
| 3153 | 3161 | return err; |
|
| 3154 | 3162 | }; |
|
| 3155 | 3163 | set EXPORTED = package.exports.len; |
|
| 3164 | + | set STARTABLE = 0; |
|
| 3165 | + | for item in package.exports { |
|
| 3166 | + | if item.startable { |
|
| 3167 | + | set STARTABLE += 1; |
|
| 3168 | + | } |
|
| 3169 | + | } |
|
| 3156 | 3170 | return nil; |
|
| 3157 | 3171 | } |
|
| 3158 | 3172 | ||
| 3159 | 3173 | /// Number of exports that the last accepted package kept. |
|
| 3160 | 3174 | static EXPORTED: u32 = 0; |
|
| 3161 | 3175 | ||
| 3176 | + | /// Number of functions that the last accepted package permits as context entries. |
|
| 3177 | + | static STARTABLE: u32 = 0; |
|
| 3178 | + | ||
| 3162 | 3179 | /// Check that the package is rejected for violating the package rules. |
|
| 3163 | 3180 | unsafe fn packageRejected( |
|
| 3164 | 3181 | b: &mut Builder, dependencies: *[*[u8]], entry: ?*[u8], exports: &[binary::Export] |
|
| 3165 | 3182 | ) throws (testing::TestError) { |
|
| 3166 | 3183 | let err = packageVerdict(b, dependencies, entry, exports) else { |
| 3224 | 3241 | let mut exact = newBuilder(); |
|
| 3225 | 3242 | let fits = addLayout(&mut exact, 32, 1, &[]); |
|
| 3226 | 3243 | try entryCase(&mut exact, &[param(0, ptrType(fits, true, false, false))], nil); |
|
| 3227 | 3244 | } |
|
| 3228 | 3245 | ||
| 3229 | - | /// Only a function that takes at most two data arguments and returns data stays exported, so |
|
| 3230 | - | /// that a context starts at it with plain arguments. Other exported functions are accepted, and |
|
| 3231 | - | /// dropped from the exports. |
|
| 3246 | + | /// Public functions retain their call contracts; only data-argument entries can start contexts. |
|
| 3232 | 3247 | @test unsafe fn packageExports() throws (testing::TestError) { |
|
| 3233 | - | let function = binary::Export { name: "f", kind: binary::ExportKind::Function }; |
|
| 3248 | + | let function = binary::Export { |
|
| 3249 | + | name: "f", |
|
| 3250 | + | kind: binary::ExportKind::Function, |
|
| 3251 | + | startable: false, |
|
| 3252 | + | }; |
|
| 3234 | 3253 | let mut ok = newBuilder(); |
|
| 3235 | 3254 | addFn(&mut ok, "f", &[oneData()], dataType(), &[ret(reg(0))]); |
|
| 3236 | 3255 | try testing::expect(packageVerdict(&mut ok, &[], nil, &[function]) == nil); |
|
| 3237 | - | try testing::expect(EXPORTED == 1); |
|
| 3256 | + | assert EXPORTED == 1; |
|
| 3257 | + | assert STARTABLE == 1; |
|
| 3238 | 3258 | let mut two = newBuilder(); |
|
| 3239 | 3259 | addFn(&mut two, "f", &[oneData(), param(1, dataType())], dataType(), &[ret(reg(0))]); |
|
| 3240 | 3260 | try testing::expect(packageVerdict(&mut two, &[], nil, &[function]) == nil); |
|
| 3241 | - | try testing::expect(EXPORTED == 1); |
|
| 3261 | + | assert EXPORTED == 1; |
|
| 3262 | + | assert STARTABLE == 1; |
|
| 3242 | 3263 | let mut three = newBuilder(); |
|
| 3243 | 3264 | addFn(&mut three, "f", &[oneData(), param(1, dataType()), param(2, dataType())], dataType(), &[ |
|
| 3244 | 3265 | ret(reg(0)) |
|
| 3245 | 3266 | ]); |
|
| 3246 | 3267 | try testing::expect(packageVerdict(&mut three, &[], nil, &[function]) == nil); |
|
| 3247 | - | try testing::expect(EXPORTED == 0); |
|
| 3268 | + | assert EXPORTED == 1; |
|
| 3269 | + | assert STARTABLE == 0; |
|
| 3248 | 3270 | let mut pointer = newBuilder(); |
|
| 3249 | 3271 | addFn(&mut pointer, "f", &[param(0, ptrType(P4, true, true, false))], il::ValType::Void, &[ |
|
| 3250 | 3272 | retVoid() |
|
| 3251 | 3273 | ]); |
|
| 3252 | 3274 | try testing::expect(packageVerdict(&mut pointer, &[], nil, &[function]) == nil); |
|
| 3253 | - | try testing::expect(EXPORTED == 0); |
|
| 3275 | + | assert EXPORTED == 1; |
|
| 3276 | + | assert STARTABLE == 0; |
|
| 3254 | 3277 | let mut result = newBuilder(); |
|
| 3255 | 3278 | addFn( |
|
| 3256 | 3279 | &mut result, |
|
| 3257 | 3280 | "f", |
|
| 3258 | 3281 | &[param(0, ptrType(P4, true, true, false))], |
|
| 3259 | 3282 | ptrType(P4, true, true, false), |
|
| 3260 | 3283 | &[ret(reg(0))] |
|
| 3261 | 3284 | ); |
|
| 3262 | 3285 | try testing::expect(packageVerdict(&mut result, &[], nil, &[function]) == nil); |
|
| 3263 | - | try testing::expect(EXPORTED == 0); |
|
| 3286 | + | assert EXPORTED == 1; |
|
| 3287 | + | assert STARTABLE == 0; |
|
| 3264 | 3288 | let mut callback = newBuilder(); |
|
| 3265 | 3289 | addFn(&mut callback, "f", &[param(0, il::ValType::Fn(SIG_DATA))], il::ValType::Void, &[ |
|
| 3266 | 3290 | retVoid() |
|
| 3267 | 3291 | ]); |
|
| 3268 | 3292 | try testing::expect(packageVerdict(&mut callback, &[], nil, &[function]) == nil); |
|
| 3269 | - | try testing::expect(EXPORTED == 0); |
|
| 3293 | + | assert EXPORTED == 1; |
|
| 3294 | + | assert STARTABLE == 0; |
|
| 3270 | 3295 | let mut missing = newBuilder(); |
|
| 3271 | 3296 | try packageRejected(&mut missing, &[], nil, &[function]); |
|
| 3272 | 3297 | let env = param(0, ptrType(P16, true, false, false)); |
|
| 3273 | - | let entry = binary::Export { name: "entry", kind: binary::ExportKind::Function }; |
|
| 3298 | + | let entry = binary::Export { |
|
| 3299 | + | name: "entry", |
|
| 3300 | + | kind: binary::ExportKind::Function, |
|
| 3301 | + | startable: false, |
|
| 3302 | + | }; |
|
| 3274 | 3303 | let mut exportedEntry = newBuilder(); |
|
| 3275 | 3304 | addFn(&mut exportedEntry, "entry", &[env], il::ValType::Void, &[retVoid()]); |
|
| 3276 | 3305 | try testing::expect(packageVerdict(&mut exportedEntry, &[], "entry", &[entry]) == nil); |
|
| 3277 | 3306 | try testing::expect(EXPORTED == 1); |
|
| 3278 | 3307 | let mut data = newBuilder(); |
|
| 3279 | 3308 | addData(&mut data, "d", P4, true, &[]); |
|
| 3280 | 3309 | ||
| 3281 | - | let item = binary::Export { name: "d", kind: binary::ExportKind::Data }; |
|
| 3310 | + | let item = binary::Export { |
|
| 3311 | + | name: "d", |
|
| 3312 | + | kind: binary::ExportKind::Data, |
|
| 3313 | + | startable: false, |
|
| 3314 | + | }; |
|
| 3282 | 3315 | try testing::expect(packageVerdict(&mut data, &[], nil, &[item]) == nil); |
|
| 3283 | 3316 | let mut absent = newBuilder(); |
|
| 3284 | 3317 | try packageRejected(&mut absent, &[], nil, &[item]); |
|
| 3285 | 3318 | } |
|
| 3286 | 3319 |
| 5481 | 5514 | let nullable = param(0, ptrType(SLICE, false, false, true)); |
|
| 5482 | 5515 | try check(&[nullable], &[read], verify::Rule::Null); |
|
| 5483 | 5516 | try check(&[nullable], &[write], verify::Rule::Null); |
|
| 5484 | 5517 | } |
|
| 5485 | 5518 | } |
|
| 5519 | + | ||
| 5520 | + | /// Verified data-only dependency function used by the import environment tests. |
|
| 5521 | + | constant IMPORT_TARGET: il::Fn = il::Fn { |
|
| 5522 | + | name: "dep::id", |
|
| 5523 | + | params: &[il::Param { value: il::Reg { n: 0 }, type: il::ValType::Data(il::Type::W64) }], |
|
| 5524 | + | returnType: il::ValType::Data(il::Type::W64), |
|
| 5525 | + | isExtern: false, |
|
| 5526 | + | isLeaf: true, |
|
| 5527 | + | blocks: &[ |
|
| 5528 | + | il::Block { |
|
| 5529 | + | label: il::BlockLabel { base: "entry", suffix: nil }, |
|
| 5530 | + | params: &[], |
|
| 5531 | + | instrs: &[il::Instr::Ret { val: il::Val::Reg(il::Reg { n: 0 }) }], |
|
| 5532 | + | locs: &[], |
|
| 5533 | + | preds: &[], |
|
| 5534 | + | loopDepth: 0, |
|
| 5535 | + | }, |
|
| 5536 | + | ], |
|
| 5537 | + | vals: &[], |
|
| 5538 | + | cases: &[], |
|
| 5539 | + | borrows: &[], |
|
| 5540 | + | }; |
|
| 5541 | + | ||
| 5542 | + | /// Independent storage for the verified dependency declaration. |
|
| 5543 | + | static IMPORT_MEMORY: [u8; 4096] = [0; 4096]; |
|
| 5544 | + | ||
| 5545 | + | /// An import alias gets its checked signature from an exact dependency binding. |
|
| 5546 | + | @test unsafe fn typedPackageImport() throws (testing::TestError) { |
|
| 5547 | + | let providerArena = heap::over(&mut IMPORT_MEMORY[..]); |
|
| 5548 | + | let mut declarations: *mut [il::Fn] = &mut []; |
|
| 5549 | + | try! declarations.append(IMPORT_TARGET, providerArena); |
|
| 5550 | + | let frozen: *[il::Fn] = &declarations[..]; |
|
| 5551 | + | let mut functions: *mut [*il::Fn] = &mut []; |
|
| 5552 | + | try! functions.append(&frozen[0], providerArena); |
|
| 5553 | + | let provider = binary::Package { |
|
| 5554 | + | symbols: &["dep", "dep::id"], |
|
| 5555 | + | name: "dep", |
|
| 5556 | + | dependencies: &[], |
|
| 5557 | + | exports: &[ |
|
| 5558 | + | binary::Export { name: "dep::id", kind: binary::ExportKind::Function, startable: false } |
|
| 5559 | + | ], |
|
| 5560 | + | entry: nil, |
|
| 5561 | + | program: il::Program { |
|
| 5562 | + | data: &[], fns: &functions[..], layouts: &[], signatures: &[], traits: &[] |
|
| 5563 | + | }, |
|
| 5564 | + | links: binary::EMPTY_LINKS, |
|
| 5565 | + | }; |
|
| 5566 | + | let scratch = heap::over(&mut SCRATCH[..]); |
|
| 5567 | + | try! verify::verifyProgram(&provider.program, CONFIG, scratch); |
|
| 5568 | + | let mut b = newBuilder(); |
|
| 5569 | + | let arguments = args(&mut b, &[imm(42)]); |
|
| 5570 | + | addFn(&mut b, "caller", &[], dataType(), &[ |
|
| 5571 | + | call(0, dataType(), il::Val::FnAddr("alias"), arguments), ret(reg(0)) |
|
| 5572 | + | ]); |
|
| 5573 | + | let identity = linkage::imageId("provider image"); |
|
| 5574 | + | let mut pins: *mut [binary::Id] = &mut []; |
|
| 5575 | + | try! pins.append( |
|
| 5576 | + | identity, b.arena |
|
| 5577 | + | ); |
|
| 5578 | + | let mut package = binary::Package { |
|
| 5579 | + | symbols: &[], |
|
| 5580 | + | name: "p", |
|
| 5581 | + | dependencies: &["dep"], |
|
| 5582 | + | exports: &[], |
|
| 5583 | + | entry: nil, |
|
| 5584 | + | program: program(&mut b), |
|
| 5585 | + | links: binary::Links { |
|
| 5586 | + | pins: &pins[..], |
|
| 5587 | + | imports: &[ |
|
| 5588 | + | binary::Import { |
|
| 5589 | + | name: "alias", |
|
| 5590 | + | dependency: 0, |
|
| 5591 | + | exported: "dep::id", |
|
| 5592 | + | kind: binary::ImportKind::Function(SIG_DATA), |
|
| 5593 | + | }, |
|
| 5594 | + | ], |
|
| 5595 | + | image: nil, |
|
| 5596 | + | verified: false, |
|
| 5597 | + | }, |
|
| 5598 | + | }; |
|
| 5599 | + | let mut supplied: *mut [linking::Provider] = &mut []; |
|
| 5600 | + | try! supplied.append( |
|
| 5601 | + | linking::Provider { image: identity, contract: provider }, b.arena |
|
| 5602 | + | ); |
|
| 5603 | + | let work = heap::over(&mut SCRATCH[..]); |
|
| 5604 | + | let keep = heap::over(&mut KEEP[..]); |
|
| 5605 | + | try! verify::verifyLinkedPackage(&mut package, &supplied[..], CONFIG, work, keep); |
|
| 5606 | + | } |
lib/std/lang/lower.rad
+150 -16
| 91 | 91 | //! |
|
| 92 | 92 | @test mod tests; |
|
| 93 | 93 | ||
| 94 | 94 | mod regions; |
|
| 95 | 95 | ||
| 96 | + | use std::collections::dict; |
|
| 96 | 97 | use std::fmt; |
|
| 97 | 98 | use std::io; |
|
| 98 | 99 | use std::lang::alloc; |
|
| 99 | - | use std::mem::heap; |
|
| 100 | - | use std::lang::types; |
|
| 101 | - | use std::mem; |
|
| 102 | 100 | use std::lang::ast; |
|
| 103 | 101 | use std::lang::il; |
|
| 102 | + | use std::lang::il::binary; |
|
| 104 | 103 | use std::lang::module; |
|
| 105 | 104 | use std::lang::parser; |
|
| 106 | 105 | use std::lang::resolver; |
|
| 106 | + | use std::lang::types; |
|
| 107 | + | use std::mem; |
|
| 108 | + | use std::mem::heap; |
|
| 107 | 109 | ||
| 108 | 110 | // TODO: Search for all `_ as i32` to ensure that casts from u32 to i32 don't |
|
| 109 | 111 | // happen, since they are potentially truncating values. |
|
| 110 | 112 | ||
| 111 | 113 | // TODO: Support constant union lowering. |
| 367 | 369 | id: resolver::TraitId, |
|
| 368 | 370 | /// Package-relative index in the trait table. |
|
| 369 | 371 | index: u32, |
|
| 370 | 372 | } |
|
| 371 | 373 | ||
| 374 | + | /// A typed symbol reference in one package's RIL namespace. |
|
| 375 | + | export record Reference: Copy { |
|
| 376 | + | /// Package whose instructions use the reference. |
|
| 377 | + | package: u16, |
|
| 378 | + | /// Package which defines the referenced symbol. |
|
| 379 | + | provider: u16, |
|
| 380 | + | /// Local symbol alias. |
|
| 381 | + | name: *[u8], |
|
| 382 | + | /// Expected signature or data description in that package's tables. |
|
| 383 | + | kind: binary::ImportKind, |
|
| 384 | + | } |
|
| 385 | + | ||
| 372 | 386 | /// The layout, signature and trait tables of a lowering pass. |
|
| 373 | 387 | /// |
|
| 374 | 388 | /// Indices in IL are relative to the tables of the current package. The tables of all |
|
| 375 | 389 | /// packages are stored one after the other, and `layoutBase` and `signatureBase` locate |
|
| 376 | 390 | /// the current ones. With a program-wide scope the bases stay zero. |
| 398 | 412 | typeLayouts: *mut [TypeLayout], |
|
| 399 | 413 | /// Layout cache of nominal types, indexed by nominal id. |
|
| 400 | 414 | nominalLayouts: *mut [NominalLayout], |
|
| 401 | 415 | /// Layouts that were numbered and whose slots are not computed yet. |
|
| 402 | 416 | fillQueue: *mut [PendingLayout], |
|
| 417 | + | /// Typed symbol references, with their importing package. |
|
| 418 | + | references: *mut [Reference], |
|
| 419 | + | /// Names already registered in the current package's reference table. |
|
| 420 | + | referenceIndex: dict::Dict, |
|
| 403 | 421 | } |
|
| 404 | 422 | ||
| 405 | 423 | /// Layout, signature and trait tables of one package. |
|
| 406 | 424 | export record PackageTables: Copy { |
|
| 407 | 425 | /// Layouts that the functions and data of the package refer to. |
| 1224 | 1242 | tableStarts: &mut [], |
|
| 1225 | 1243 | sharedLayouts: &mut [], |
|
| 1226 | 1244 | typeLayouts: &mut [], |
|
| 1227 | 1245 | nominalLayouts: &mut [], |
|
| 1228 | 1246 | fillQueue: &mut [], |
|
| 1247 | + | references: &mut [], |
|
| 1248 | + | referenceIndex: dict::Dict { entries: &mut [], count: 0 }, |
|
| 1229 | 1249 | }, |
|
| 1230 | 1250 | fnType: nil, |
|
| 1231 | 1251 | localCount: 0, |
|
| 1232 | 1252 | fnName: &[], |
|
| 1233 | 1253 | argPool: &mut [], |
| 1366 | 1386 | return try il::formatQualifiedName(self.resolver.arena, path, name) catch { |
|
| 1367 | 1387 | throw LowerError::AllocationFailed; |
|
| 1368 | 1388 | }; |
|
| 1369 | 1389 | } |
|
| 1370 | 1390 | ||
| 1391 | + | /// Provider package of a foreign symbol. A nil module belongs to the current package. |
|
| 1392 | + | fn foreignPackage(self: &Lowerer, modId: ?u16) -> ?u16 throws (LowerError) { |
|
| 1393 | + | if self.options.tableScope <> TableScope::Package { |
|
| 1394 | + | return nil; |
|
| 1395 | + | } |
|
| 1396 | + | let id = modId else return nil; |
|
| 1397 | + | let entry = module::get(self.moduleTable, id) else throw LowerError::MissingMetadata; |
|
| 1398 | + | if entry.packageId == self.package { |
|
| 1399 | + | return nil; |
|
| 1400 | + | } |
|
| 1401 | + | return entry.packageId; |
|
| 1402 | + | } |
|
| 1403 | + | ||
| 1404 | + | /// Retain one typed reference for package linking. |
|
| 1405 | + | fn recordReference( |
|
| 1406 | + | self: &mut Lowerer, provider: u16, name: *[u8], kind: binary::ImportKind |
|
| 1407 | + | ) |
|
| 1408 | + | throws (LowerError) |
|
| 1409 | + | { |
|
| 1410 | + | if dict::get(&self.tables.referenceIndex, name) <> nil { |
|
| 1411 | + | return; |
|
| 1412 | + | } |
|
| 1413 | + | let index = self.tables.references.len; |
|
| 1414 | + | try self.tables.references.append( |
|
| 1415 | + | Reference { package: self.package, provider, name, kind }, self.resolver.arena |
|
| 1416 | + | ) catch { |
|
| 1417 | + | throw LowerError::AllocationFailed; |
|
| 1418 | + | }; |
|
| 1419 | + | try dict::insert(&mut self.tables.referenceIndex, name, index as i32) catch { |
|
| 1420 | + | throw LowerError::AllocationFailed; |
|
| 1421 | + | }; |
|
| 1422 | + | } |
|
| 1423 | + | ||
| 1424 | + | /// Retain the signature of a named function used by package-local RIL. |
|
| 1425 | + | fn recordFunctionReference( |
|
| 1426 | + | self: &mut Lowerer, provider: u16, name: *[u8], typ: *resolver::FnType |
|
| 1427 | + | ) |
|
| 1428 | + | throws (LowerError) |
|
| 1429 | + | { |
|
| 1430 | + | if dict::get(&self.tables.referenceIndex, name) <> nil { |
|
| 1431 | + | return; |
|
| 1432 | + | } |
|
| 1433 | + | let signature = try signatureIndex(self, typ); |
|
| 1434 | + | try recordReference(self, provider, name, binary::ImportKind::Function(signature)); |
|
| 1435 | + | } |
|
| 1436 | + | ||
| 1371 | 1437 | /// Register a function symbol with its qualified name. |
|
| 1372 | 1438 | /// Called when lowering function declarations, so cross-package calls can find |
|
| 1373 | 1439 | /// the function by name. |
|
| 1374 | 1440 | fn registerFnSym(self: &mut Lowerer, sym: resolver::SymbolId, qualName: *[u8]) throws (LowerError) { |
|
| 1375 | 1441 | try self.fnSyms.append(FnSymEntry { sym, qualName }, self.resolver.arena) catch { |
| 1401 | 1467 | set self.package = package; |
|
| 1402 | 1468 | set self.pkgName = pkgName; |
|
| 1403 | 1469 | set self.currentMod = nil; |
|
| 1404 | 1470 | set self.packageDataStart = self.data.len; |
|
| 1405 | 1471 | if self.options.tableScope == TableScope::Package { |
|
| 1472 | + | let mut entries: *mut [dict::Entry] = &mut []; |
|
| 1473 | + | try entries.resize( |
|
| 1474 | + | binary::collect::INDEX_SLOTS, dict::Entry { key: &[], value: 0 }, self.resolver.arena |
|
| 1475 | + | ) catch { |
|
| 1476 | + | throw LowerError::AllocationFailed; |
|
| 1477 | + | }; |
|
| 1478 | + | set self.tables.referenceIndex = dict::fresh(entries); |
|
| 1406 | 1479 | try self.tables.tableStarts.append( |
|
| 1407 | 1480 | TableStart { |
|
| 1408 | 1481 | package, |
|
| 1409 | 1482 | layouts: self.tables.layouts.len, |
|
| 1410 | 1483 | signatures: self.tables.signatures.len, |
| 2087 | 2160 | { |
|
| 2088 | 2161 | set found = entry; |
|
| 2089 | 2162 | } |
|
| 2090 | 2163 | } |
|
| 2091 | 2164 | let inst = found else throw LowerError::MissingMetadata; |
|
| 2165 | + | return try instanceLayout(self, traitInfo, inst); |
|
| 2166 | + | } |
|
| 2167 | + | ||
| 2168 | + | /// Layout of the concrete function entries of one resolved trait instance. |
|
| 2169 | + | fn instanceLayout( |
|
| 2170 | + | self: &mut Lowerer, traitInfo: resolver::TraitId, inst: resolver::InstanceEntry |
|
| 2171 | + | ) -> u32 throws (LowerError) { |
|
| 2092 | 2172 | let methodCount = self.resolver.traits[*traitInfo].methods.len; |
|
| 2093 | 2173 | let mut slots: *mut [il::Slot] = &mut []; |
|
| 2094 | 2174 | try slots.reserve(methodCount, self.resolver.arena) catch { |
|
| 2095 | 2175 | throw LowerError::AllocationFailed; |
|
| 2096 | 2176 | }; |
| 2476 | 2556 | return b.values.len - start; |
|
| 2477 | 2557 | } |
|
| 2478 | 2558 | } |
|
| 2479 | 2559 | } |
|
| 2480 | 2560 | // Function pointer references in constant data. |
|
| 2481 | - | if let case resolver::Type::Fn(_) = ty { |
|
| 2561 | + | if let case resolver::Type::Fn(fnType) = ty { |
|
| 2482 | 2562 | let sym = resolver::symbolFor(&self.resolver, node) |
|
| 2483 | 2563 | else throw LowerError::MissingSymbol(node); |
|
| 2484 | 2564 | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 2485 | 2565 | let symName = self.resolver.symbols[*sym].name; |
|
| 2486 | 2566 | let qualName = try qualifyName(self, modId, symName); |
|
| 2567 | + | if let provider = try foreignPackage(self, modId) { |
|
| 2568 | + | try recordFunctionReference(self, provider, qualName, fnType); |
|
| 2569 | + | } |
|
| 2487 | 2570 | try dataBuilderPush( |
|
| 2488 | 2571 | b, |
|
| 2489 | 2572 | il::DataValue { |
|
| 2490 | 2573 | item: il::DataItem::Fn(qualName), count: 1 |
|
| 2491 | 2574 | }, |
| 3630 | 3713 | let dst = nextReg(self); |
|
| 3631 | 3714 | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 3632 | 3715 | let symName = self.resolver.symbols[*sym].name; |
|
| 3633 | 3716 | let qualName = try qualifyName(self, modId, symName); |
|
| 3634 | 3717 | ||
| 3718 | + | if let provider = try foreignPackage(self, modId) { |
|
| 3719 | + | let symbol = self.resolver.symbols[*sym]; |
|
| 3720 | + | let mut typ = resolver::Type::Void; |
|
| 3721 | + | match symbol.data { |
|
| 3722 | + | case resolver::SymbolData::Value { type: value, .. } => set typ = value, |
|
| 3723 | + | case resolver::SymbolData::Constant { type: value, .. } => set typ = value, |
|
| 3724 | + | else => throw LowerError::MissingMetadata, |
|
| 3725 | + | } |
|
| 3726 | + | let mut layout = try layoutOf(self, typ); |
|
| 3727 | + | let mut readOnly = false; |
|
| 3728 | + | let mut heap: ?il::HeapType = nil; |
|
| 3729 | + | match symbol.node.value { |
|
| 3730 | + | case ast::NodeValue::ConstDecl(_) => set readOnly = true, |
|
| 3731 | + | case ast::NodeValue::StaticDecl(decl) => { |
|
| 3732 | + | if let case resolver::NodeExtra::StaticHeap(size) = |
|
| 3733 | + | resolver::nodeData(&self.resolver, decl.value).extra |
|
| 3734 | + | { |
|
| 3735 | + | let kind = resolver::heapKindOfType(&self.resolver, typ) |
|
| 3736 | + | else throw LowerError::MissingMetadata; |
|
| 3737 | + | set layout = try plainLayoutOf(self, resolver::Layout { size, alignment: 1 }); |
|
| 3738 | + | set heap = heapKindRights(kind); |
|
| 3739 | + | } |
|
| 3740 | + | } |
|
| 3741 | + | else => {} |
|
| 3742 | + | } |
|
| 3743 | + | try recordReference(self, provider, qualName, binary::ImportKind::Data { |
|
| 3744 | + | layout, readOnly, heap |
|
| 3745 | + | }); |
|
| 3746 | + | } |
|
| 3635 | 3747 | try emit(self, il::Instr::Copy { dst, val: il::Val::DataSym(qualName) }); |
|
| 3636 | 3748 | ||
| 3637 | 3749 | return dst; |
|
| 3638 | 3750 | } |
|
| 3639 | 3751 |
| 3642 | 3754 | let dst = nextReg(self); |
|
| 3643 | 3755 | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 3644 | 3756 | let symName = self.resolver.symbols[*sym].name; |
|
| 3645 | 3757 | let qualName = try qualifyName(self, modId, symName); |
|
| 3646 | 3758 | ||
| 3759 | + | if let provider = try foreignPackage(self, modId) { |
|
| 3760 | + | if let case resolver::SymbolData::Value { type: resolver::Type::Fn(typ), .. } = |
|
| 3761 | + | self.resolver.symbols[*sym].data |
|
| 3762 | + | { |
|
| 3763 | + | try recordFunctionReference(self, provider, qualName, typ); |
|
| 3764 | + | } |
|
| 3765 | + | } |
|
| 3647 | 3766 | try emit(self, il::Instr::Copy { dst, val: il::Val::FnAddr(qualName) }); |
|
| 3648 | 3767 | ||
| 3649 | 3768 | return dst; |
|
| 3650 | 3769 | } |
|
| 3651 | 3770 |
| 6562 | 6681 | instId: resolver::InstanceId |
|
| 6563 | 6682 | ) -> il::Val throws (LowerError) { |
|
| 6564 | 6683 | let inst = self.resolver.instances[*instId]; |
|
| 6565 | 6684 | let traitName = self.resolver.traits[*traitInfo].name; |
|
| 6566 | 6685 | let vName = try vtableName(self, inst.moduleId, inst.concreteTypeName, traitName); |
|
| 6686 | + | if let provider = try foreignPackage(self, inst.moduleId) { |
|
| 6687 | + | let layout = try instanceLayout(self, traitInfo, inst); |
|
| 6688 | + | try recordReference(self, provider, vName, binary::ImportKind::Data { |
|
| 6689 | + | layout, readOnly: true, heap: nil |
|
| 6690 | + | }); |
|
| 6691 | + | } |
|
| 6567 | 6692 | if resolver::isTraitSlot(objType) { |
|
| 6568 | 6693 | let slot = try emitReserve(self, objType); |
|
| 6569 | 6694 | let recv = try emitValToReg(self, dataVal); |
|
| 6570 | 6695 | try emit(self, il::Instr::TraitNew { dst: slot, recv, table: il::Val::DataSym(vName) }); |
|
| 6571 | 6696 | return il::Val::Reg(slot); |
| 10672 | 10797 | set self.argPool[args.start + argOffset] = receiverVal; |
|
| 10673 | 10798 | for arg, i in call.args { |
|
| 10674 | 10799 | let val = try lowerCallArg(self, arg, i + 1 < call.args.len); |
|
| 10675 | 10800 | set self.argPool[args.start + i + 1 + argOffset] = val; |
|
| 10676 | 10801 | } |
|
| 10802 | + | if let provider = try foreignPackage(self, method.moduleId) { |
|
| 10803 | + | try recordFunctionReference(self, provider, qualName, fnInfo); |
|
| 10804 | + | } |
|
| 10677 | 10805 | return try emitCallValue(self, CallTarget::Fn(il::Val::FnAddr(qualName)), fnInfo, args); |
|
| 10678 | 10806 | } |
|
| 10679 | 10807 | ||
| 10680 | 10808 | /// Check if a call is to a compiler intrinsic and lower it directly. |
|
| 10681 | 10809 | fn lowerIntrinsicCall(self: &mut Lowerer, call: &ast::Call) -> ?il::Val throws (LowerError) { |
| 10871 | 10999 | return il::Val::Undef; |
|
| 10872 | 11000 | } |
|
| 10873 | 11001 | ||
| 10874 | 11002 | /// Resolve callee to an IL value. For direct function calls, use the symbol name. |
|
| 10875 | 11003 | /// For variables holding function pointers or complex expressions (eg. `array[i]()`), |
|
| 10876 | - | /// lower the callee expression. |
|
| 10877 | - | fn lowerCallee(self: &mut Lowerer, callee: *ast::Node) -> il::Val throws (LowerError) { |
|
| 11004 | + | /// lower the callee expression. `fnInfo` is the function type of the callee. |
|
| 11005 | + | fn lowerCallee(self: &mut Lowerer, callee: *ast::Node, fnInfo: *resolver::FnType) -> il::Val |
|
| 11006 | + | throws (LowerError) |
|
| 11007 | + | { |
|
| 10878 | 11008 | if let sym = resolver::symbolFor(&self.resolver, callee) { |
|
| 10879 | 11009 | if let case ast::NodeValue::FnDecl(_) = self.resolver.symbols[*sym].node.value { |
|
| 11010 | + | let modId = resolver::moduleIdForSymbol(&self.resolver, sym); |
|
| 10880 | 11011 | // First try to look up the symbol in our registered functions. |
|
| 10881 | 11012 | // This handles cross-package calls correctly, since packages are |
|
| 10882 | 11013 | // lowered in dependency order. |
|
| 10883 | - | if let qualName = lookupFnSym(self, sym) { |
|
| 10884 | - | return il::Val::FnAddr(qualName); |
|
| 11014 | + | let mut qualName = lookupFnSym(self, sym); |
|
| 11015 | + | if qualName == nil { |
|
| 11016 | + | // Fall back to computing the qualified name from the module graph. |
|
| 11017 | + | // This works for functions in the current package. |
|
| 11018 | + | let id = modId else throw LowerError::MissingMetadata; |
|
| 11019 | + | let symName = self.resolver.symbols[*sym].name; |
|
| 11020 | + | set qualName = try qualifyName(self, id, symName); |
|
| 10885 | 11021 | } |
|
| 10886 | - | // Fall back to computing the qualified name from the module graph. |
|
| 10887 | - | // This works for functions in the current package. |
|
| 10888 | - | let modId = resolver::moduleIdForSymbol(&self.resolver, sym) else { |
|
| 10889 | - | throw LowerError::MissingMetadata; |
|
| 10890 | - | }; |
|
| 10891 | - | let symName = self.resolver.symbols[*sym].name; |
|
| 10892 | - | return il::Val::FnAddr(try qualifyName(self, modId, symName)); |
|
| 11022 | + | let name = qualName else panic "lowerCallee: missing function name"; |
|
| 11023 | + | if let provider = try foreignPackage(self, modId) { |
|
| 11024 | + | try recordFunctionReference(self, provider, name, fnInfo); |
|
| 11025 | + | } |
|
| 11026 | + | return il::Val::FnAddr(name); |
|
| 10893 | 11027 | } |
|
| 10894 | 11028 | } |
|
| 10895 | 11029 | return try lowerExpr(self, callee); |
|
| 10896 | 11030 | } |
|
| 10897 | 11031 |
| 10905 | 11039 | } |
|
| 10906 | 11040 | let calleeTy = try typeOf(self, call.callee); |
|
| 10907 | 11041 | let case resolver::Type::Fn(fnInfo) = calleeTy else { |
|
| 10908 | 11042 | throw LowerError::ExpectedFunction; |
|
| 10909 | 11043 | }; |
|
| 10910 | - | let callee = try lowerCallee(self, call.callee); |
|
| 11044 | + | let callee = try lowerCallee(self, call.callee, fnInfo); |
|
| 10911 | 11045 | let offset: u32 = 1 if requiresReturnParam(&self.resolver, fnInfo) else 0; |
|
| 10912 | 11046 | let args = try allocVals(self, call.args.len + offset); |
|
| 10913 | 11047 | for arg, i in call.args { |
|
| 10914 | 11048 | let val = try lowerCallArg(self, arg, i + 1 < call.args.len); |
|
| 10915 | 11049 | set self.argPool[args.start + i + offset] = val; |
seed/emulator-args
+1 -1
| 1 | - | -memory-size=393216 -data-size=356608 -stack-size=1024 |
|
| 1 | + | -memory-size=393216 -data-size=375040 -stack-size=1024 |
seed/update
+2 -2
| 12 | 12 | # --from-s0 Run the seed as a native binary (stage 0) instead of |
|
| 13 | 13 | # through the emulator |
|
| 14 | 14 | # |
|
| 15 | 15 | # Environment variables: |
|
| 16 | 16 | # EMULATOR_MEMORY_SIZE Emulator memory in KB (default: 393216) |
|
| 17 | - | # EMULATOR_DATA_SIZE Emulator data segment in KB (default: 356608) |
|
| 17 | + | # EMULATOR_DATA_SIZE Emulator data segment in KB (default: 375040) |
|
| 18 | 18 | # |
|
| 19 | 19 | set -eu |
|
| 20 | 20 | ||
| 21 | 21 | SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
|
| 22 | 22 | ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" |
| 39 | 39 | # --------------------------------------------------------------------------- |
|
| 40 | 40 | # Emulator settings |
|
| 41 | 41 | # --------------------------------------------------------------------------- |
|
| 42 | 42 | ||
| 43 | 43 | MEMORY_SIZE="${EMULATOR_MEMORY_SIZE:-393216}" |
|
| 44 | - | DATA_SIZE="${EMULATOR_DATA_SIZE:-356608}" |
|
| 44 | + | DATA_SIZE="${EMULATOR_DATA_SIZE:-375040}" |
|
| 45 | 45 | EMULATOR="${RAD_EMULATOR:-emulator} -debug -no-validate \ |
|
| 46 | 46 | -memory-size=${MEMORY_SIZE} -data-size=${DATA_SIZE} \ |
|
| 47 | 47 | -run" |
|
| 48 | 48 | ||
| 49 | 49 | # --------------------------------------------------------------------------- |
std.lib
+5 -0
| 67 | 67 | lib/std/lang/gen/regalloc/spill.rad |
|
| 68 | 68 | lib/std/lang/gen/regalloc/assign.rad |
|
| 69 | 69 | lib/std/arch/rv64/shared.rad |
|
| 70 | 70 | lib/std/arch/rv64/shared/catalog.rad |
|
| 71 | 71 | lib/std/arch/rv64/atomics.rad |
|
| 72 | + | lib/std/hash.rad |
|
| 73 | + | lib/std/hash/blake3.rad |
|
| 74 | + | lib/std/lang/il/link.rad |
|
| 75 | + | lib/std/lang/il/binary/linkage.rad |
|
| 76 | + | lib/std/lang/il/verify/linking.rad |
std.lib.test
+2 -0
| 29 | 29 | lib/std/arch/rv64/shared/tests/run.rad |
|
| 30 | 30 | lib/std/arch/rv64/tests/bounds.rad |
|
| 31 | 31 | lib/std/arch/rv64/tests/atomics.rad |
|
| 32 | 32 | lib/std/bits/tests.rad |
|
| 33 | 33 | lib/std/mem/tests.rad |
|
| 34 | + | lib/std/hash/blake3/tests.rad |
|
| 35 | + | lib/std/lang/il/link/tests.rad |
test/integration.rad
+1 -0
| 32 | 32 | mod fixtures; |
|
| 33 | 33 | mod format; |
|
| 34 | 34 | mod host; |
|
| 35 | 35 | mod kernel; |
|
| 36 | 36 | mod machine; |
|
| 37 | + | mod linking; |
|
| 37 | 38 | mod boot; |
|
| 38 | 39 | mod verify; |
|
| 39 | 40 | ||
| 40 | 41 | use std::io; |
|
| 41 | 42 | use std::mem; |
test/integration/linking.rad
added
+45 -0
| 1 | + | //! Deterministic exact package identities and implementation updates. |
|
| 2 | + | ||
| 3 | + | use std::fs; |
|
| 4 | + | use std::io; |
|
| 5 | + | use std::lang::il::binary; |
|
| 6 | + | use std::lang::il::binary::program; |
|
| 7 | + | use std::sys; |
|
| 8 | + | use super::harness; |
|
| 9 | + | use super::harness::Harness; |
|
| 10 | + | ||
| 11 | + | /// Compile repeated and body-changed package images. |
|
| 12 | + | export fn check(h: &mut Harness) throws (sys::Error) { |
|
| 13 | + | try h.begin("linking"); |
|
| 14 | + | let mut packages: *mut [binary::Package] = &mut []; |
|
| 15 | + | for name, i in &["first", "body", "repeat"] { |
|
| 16 | + | let directory = h.path(&[name]); |
|
| 17 | + | try harness::makeDirs(directory); |
|
| 18 | + | let source = h.join(&[directory, "/support.rad"]); |
|
| 19 | + | let body = "1" if i == 1 else "0"; |
|
| 20 | + | try fs::writeFileParts( |
|
| 21 | + | source, |
|
| 22 | + | &[ |
|
| 23 | + | "/// Public nominal value.\nexport record Value: Copy {\n", |
|
| 24 | + | " /// Payload.\n value: u32,\n}\n", |
|
| 25 | + | "/// Read the payload.\nexport fn read(input: &Value) -> u32 {\n", |
|
| 26 | + | " return input.value + ", body, ";\n}\n", |
|
| 27 | + | ] |
|
| 28 | + | ); |
|
| 29 | + | h.add(&["-pkg", "support", "-mod", source, "-ril", directory]); |
|
| 30 | + | if not h.compile(h.join(&["compile package ", name]), h.out(&[name, ".log"])) { |
|
| 31 | + | return; |
|
| 32 | + | } |
|
| 33 | + | let wire = try h.load(h.join(&[directory, "/support.ril"])); |
|
| 34 | + | let decoded = try! program::decode(wire, h.arena, binary::Limits { |
|
| 35 | + | registers: 1024, blocks: 1024 |
|
| 36 | + | }); |
|
| 37 | + | try! packages.append(decoded, h.arena); |
|
| 38 | + | } |
|
| 39 | + | assert packages[0].links.image <> packages[1].links.image; |
|
| 40 | + | assert packages[0].links.image == packages[2].links.image; |
|
| 41 | + | h.pass(); |
|
| 42 | + | io::printLn( |
|
| 43 | + | "package identities: deterministic images and body updates passed" |
|
| 44 | + | ); |
|
| 45 | + | } |
test/integration/machine.rad
+7 -5
| 1 | 1 | //! Fixtures that run on a bare machine: synchronization, traps, shared |
|
| 2 | 2 | //! packages and the kernel dispatch fixtures. Also the rejection of slot |
|
| 3 | 3 | //! reservation misuse and the check of the checked-in seed image. |
|
| 4 | 4 | ||
| 5 | + | use std::arch::rv64::image; |
|
| 5 | 6 | use std::fs; |
|
| 6 | 7 | use std::io; |
|
| 7 | 8 | use std::mem; |
|
| 8 | 9 | use std::process; |
|
| 10 | + | use std::process::Outcome; |
|
| 11 | + | use std::process::Target; |
|
| 9 | 12 | use std::sys; |
|
| 10 | - | use std::arch::rv64::image; |
|
| 11 | 13 | use super::harness; |
|
| 12 | - | use super::kernel; |
|
| 13 | - | use super::harness::Harness; |
|
| 14 | 14 | use super::harness::Expect; |
|
| 15 | - | use std::process::Outcome; |
|
| 16 | - | use std::process::Target; |
|
| 15 | + | use super::harness::Harness; |
|
| 16 | + | use super::kernel; |
|
| 17 | 17 | use super::kernel::Embedded; |
|
| 18 | + | use super::linking; |
|
| 18 | 19 | ||
| 19 | 20 | /// The checked-in seed compiler image. |
|
| 20 | 21 | constant SEED: *[u8] = "seed/radiance.rv64"; |
|
| 21 | 22 | ||
| 22 | 23 | /// Run `tool` on the arguments to build `image`, then run the image on |
| 121 | 122 | buildAndRun( |
|
| 122 | 123 | h, "typed catch across compilations", "test/shared/build.rv64", &packages[..], image, 0 |
|
| 123 | 124 | ); |
|
| 124 | 125 | } |
|
| 125 | 126 | h.done(mark, "shared packages: typed catch agrees across separate compilations"); |
|
| 127 | + | try linking::check(h); |
|
| 126 | 128 | } |
|
| 127 | 129 | ||
| 128 | 130 | /// Check that misuse of reservations fails during compilation: one consumed |
|
| 129 | 131 | /// twice and one that is never consumed. |
|
| 130 | 132 | export fn slots(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) { |
test/loader/hostile.rad
+16 -9
| 3 | 3 | //! The language cannot express these programs, so each one is built as typed IL and |
|
| 4 | 4 | //! encoded. Every package is small and well formed for the decoder, and breaks exactly one |
|
| 5 | 5 | //! rule of the verifier. The tool writes `<name>.ril` for each package into the directory |
|
| 6 | 6 | //! given as the first argument. |
|
| 7 | 7 | ||
| 8 | - | use std::sys; |
|
| 9 | 8 | use std::fs; |
|
| 10 | - | use std::mem::heap; |
|
| 11 | 9 | use std::lang::il; |
|
| 12 | 10 | use std::lang::il::binary; |
|
| 13 | 11 | use std::lang::il::binary::program; |
|
| 12 | + | use std::mem::heap; |
|
| 13 | + | use std::sys; |
|
| 14 | 14 | ||
| 15 | 15 | /// Layout of eight plain bytes. |
|
| 16 | 16 | constant PLAIN: u32 = 0; |
|
| 17 | 17 | /// Layout of a nullable owned pointer slot. |
|
| 18 | 18 | constant CELL: u32 = 1; |
| 62 | 62 | try! table.append(il::Layout { size: 8, alignment: 8, slots: pointer, regions: 0 }, arena); |
|
| 63 | 63 | let words = il::PtrType { layout: PLAIN, own: true, mutable: true, nullable: true }; |
|
| 64 | 64 | let slice = only(arena, il::Slot { offset: 0, kind: il::SlotKind::Slice(words), regions: &[] }); |
|
| 65 | 65 | try! table.append(il::Layout { size: 16, alignment: 8, slots: slice, regions: 0 }, arena); |
|
| 66 | 66 | let cells = il::PtrType { layout: CELL, own: true, mutable: true, nullable: true }; |
|
| 67 | - | let sliceCells = only( |
|
| 68 | - | arena, il::Slot { offset: 0, kind: il::SlotKind::Slice(cells), regions: &[] } |
|
| 69 | - | ); |
|
| 67 | + | let sliceCells = only(arena, il::Slot { |
|
| 68 | + | offset: 0, kind: il::SlotKind::Slice(cells), regions: &[] |
|
| 69 | + | }); |
|
| 70 | 70 | try! table.append(il::Layout { size: 16, alignment: 8, slots: sliceCells, regions: 0 }, arena); |
|
| 71 | 71 | let handle = only(arena, il::Slot { |
|
| 72 | - | offset: 0, kind: il::SlotKind::Heap(il::HeapType::Typed), |
|
| 73 | - | regions: &[], |
|
| 72 | + | offset: 0, kind: il::SlotKind::Heap(il::HeapType::Typed), regions: &[] |
|
| 74 | 73 | }); |
|
| 75 | 74 | try! table.append(il::Layout { size: 8, alignment: 8, slots: handle, regions: 0 }, arena); |
|
| 76 | 75 | let mut variants: *mut [il::UnionVariant] = &mut []; |
|
| 77 | 76 | try! variants.append(il::UnionVariant { tag: 0, layout: CELL, regions: &[] }, arena); |
|
| 78 | 77 | try! variants.append(il::UnionVariant { tag: 1, layout: PLAIN, regions: &[] }, arena); |
|
| 79 | 78 | let mixed = only(arena, il::Slot { |
|
| 80 | - | offset: 0, kind: il::SlotKind::Union { offset: 8, size: 16, variants: &variants[..] }, |
|
| 79 | + | offset: 0, |
|
| 80 | + | kind: il::SlotKind::Union { offset: 8, size: 16, variants: &variants[..] }, |
|
| 81 | 81 | regions: &[], |
|
| 82 | 82 | }); |
|
| 83 | 83 | try! table.append(il::Layout { size: 16, alignment: 8, slots: mixed, regions: 0 }, arena); |
|
| 84 | 84 | return table; |
|
| 85 | 85 | } |
| 162 | 162 | let table = layouts(arena); |
|
| 163 | 163 | let package = binary::Package { |
|
| 164 | 164 | symbols, |
|
| 165 | 165 | name: "hostile", |
|
| 166 | 166 | dependencies, |
|
| 167 | - | exports: &[binary::Export { name: ENTRY, kind: binary::ExportKind::Function }], |
|
| 167 | + | exports: &[ |
|
| 168 | + | binary::Export { |
|
| 169 | + | name: ENTRY, |
|
| 170 | + | kind: binary::ExportKind::Function, |
|
| 171 | + | startable: false, |
|
| 172 | + | }, |
|
| 173 | + | ], |
|
| 168 | 174 | entry: ENTRY, |
|
| 169 | 175 | program: il::Program { |
|
| 170 | 176 | data: &tables[..], fns: &list[..], layouts: &table[..], signatures: &[], traits: &[] |
|
| 171 | 177 | }, |
|
| 178 | + | links: binary::EMPTY_LINKS, |
|
| 172 | 179 | }; |
|
| 173 | 180 | let mut output: *mut [u8] = &mut OUT[..]; |
|
| 174 | 181 | let length = try! program::encode(&mut output, &package); |
|
| 175 | 182 | let mut path: *mut [u8] = &mut []; |
|
| 176 | 183 | try! path.extend(directory, arena); |
test/runtime/kernel/dispatchcheck.rad
+2 -3
| 23 | 23 | ||
| 24 | 24 | /// Delay before the first user window, in timer ticks. |
|
| 25 | 25 | constant START_DELAY: u64 = 1000000; |
|
| 26 | 26 | /// End of the first interrupted load, relative to the first window start. It |
|
| 27 | 27 | /// must fall after the loader reserves its output and before its compilation |
|
| 28 | - | /// ends. Both depend on the speed of the generated compiler: the load reserves |
|
| 29 | - | /// its output after about 4 million ticks and ends after about 12 million. |
|
| 30 | - | constant FIRST_WINDOW_END: u64 = 8000000; |
|
| 28 | + | /// ends, so cancellation covers unpublished native code and metadata. |
|
| 29 | + | constant FIRST_WINDOW_END: u64 = 10000000; |
|
| 31 | 30 | /// End of the controller's cancellation window. |
|
| 32 | 31 | constant CANCEL_WINDOW_END: u64 = 50000000; |
|
| 33 | 32 | /// End of the retry loader's first window. |
|
| 34 | 33 | constant RETRY_WINDOW_END: u64 = 60000000; |
|
| 35 | 34 | /// End of the witness window. |
test/scheduling/scheduler.rad
+6 -4
| 22 | 22 | // Boot authority has no end, so the scheduler can run for the life of the system. |
|
| 23 | 23 | let initial = try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT); |
|
| 24 | 24 | let boot = try! sys::queryBudget(abi::Handle(initial.budget)); |
|
| 25 | 25 | assert boot.end == 0xffffffffffffffff; |
|
| 26 | 26 | for turn in 0 .. 4 { |
|
| 27 | - | let current = (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)); |
|
| 27 | + | let current = try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT); |
|
| 28 | 28 | let budget = abi::Handle(current.budget); |
|
| 29 | 29 | let window = try! sys::queryBudget(budget); |
|
| 30 | 30 | let later = try! sys::budgetSplit(budget, window.start + info.timebase); |
|
| 31 | 31 | let worker = try! sys::domainCreate(self, info.schedulerImage); |
|
| 32 | 32 | let stack = try! sys::pageAllocate(self, 16); |
| 37 | 37 | assert (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget == *later; |
|
| 38 | 38 | } |
|
| 39 | 39 | // The kernel retires each window when it ends and leaves its handle stale, so |
|
| 40 | 40 | // more successive windows than its 512 budget objects run to completion. |
|
| 41 | 41 | for turn in 0 .. 513 { |
|
| 42 | - | let ending = abi::Handle((try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget); |
|
| 43 | - | // One millisecond outlasts the split call, so the split point is still ahead. |
|
| 44 | - | let end = mmio::read64(info.clock, 0) + info.timebase / 1000; |
|
| 42 | + | let ending = abi::Handle( |
|
| 43 | + | (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget |
|
| 44 | + | ); |
|
| 45 | + | // Leave 100 milliseconds for syscall admission and concurrent timer work. |
|
| 46 | + | let end = mmio::read64(info.clock, 0) + info.timebase / 10; |
|
| 45 | 47 | let next = try! sys::budgetSplit(ending, end); |
|
| 46 | 48 | while (try! sys::queryContext(abi::Handle(0), abi::CURRENT_CONTEXT)).budget <> *next {} |
|
| 47 | 49 | let mut stale = false; |
|
| 48 | 50 | try sys::queryBudget(ending) catch err { |
|
| 49 | 51 | assert err == abi::Error::BadHandle; |
test/shared/app.rad
+9 -1
| 3 | 3 | use support; |
|
| 4 | 4 | ||
| 5 | 5 | /// Callback table contains a linked dependency function address. |
|
| 6 | 6 | static CALLBACKS: [fn() -> i32; 1] = [support::read]; |
|
| 7 | 7 | /// The slice contains a pointer into this domain's string storage. |
|
| 8 | - | unsafe static TEXT: *[u8] = "xy"; |
|
| 8 | + | constant TEXT: *[u8] = "xy"; |
|
| 9 | 9 | /// Number of calls in the current domain. |
|
| 10 | 10 | unsafe static calls: i32 = 0; |
|
| 11 | 11 | ||
| 12 | 12 | /// Read dependency state through calls and data, then advance private state. |
|
| 13 | 13 | @default export fn main() -> i32 { |
|
| 14 | + | let number = support::Number { value: 23, extra: 47 }; |
|
| 15 | + | let borrowed = support::borrow(&number); |
|
| 16 | + | let copied = support::copied(borrowed); |
|
| 17 | + | assert copied.value == 23; |
|
| 18 | + | assert copied.extra == 47; |
|
| 19 | + | let referenced = support::referenced(&number); |
|
| 20 | + | assert referenced.value.value == 23; |
|
| 21 | + | assert support::reading(&copied) == 23; |
|
| 14 | 22 | unsafe { |
|
| 15 | 23 | set calls += 1; |
|
| 16 | 24 | return support::read() + CALLBACKS[0]() + support::value + TEXT[0] as i32 + calls; |
|
| 17 | 25 | } |
|
| 18 | 26 | } |
test/shared/build.rad
+74 -16
| 1 | 1 | //! Build a machine fixture from separately decoded and linked packages. |
|
| 2 | 2 | ||
| 3 | - | use std::sys; |
|
| 4 | - | use std::fs; |
|
| 5 | - | use std::mem; |
|
| 6 | - | use std::mem::heap; |
|
| 7 | - | use std::lang::il::binary; |
|
| 8 | - | use std::lang::il::binary::program; |
|
| 9 | - | use std::lang::gen::data; |
|
| 10 | - | use std::lang::gen::labels; |
|
| 11 | - | use std::collections::dict; |
|
| 12 | 3 | use std::arch::rv64; |
|
| 13 | - | use std::arch::rv64::shared; |
|
| 14 | 4 | use std::arch::rv64::emit; |
|
| 15 | 5 | use std::arch::rv64::encode; |
|
| 16 | 6 | use std::arch::rv64::image; |
|
| 7 | + | use std::arch::rv64::shared; |
|
| 8 | + | use std::collections::dict; |
|
| 9 | + | use std::fs; |
|
| 10 | + | use std::io; |
|
| 11 | + | use std::lang::gen::data; |
|
| 12 | + | use std::lang::gen::labels; |
|
| 17 | 13 | use std::lang::gen::regalloc::liveness; |
|
| 14 | + | use std::lang::il::binary; |
|
| 15 | + | use std::lang::il::binary::linkage; |
|
| 16 | + | use std::lang::il::binary::program; |
|
| 17 | + | use std::lang::il::verify; |
|
| 18 | + | use std::lang::il::verify::linking; |
|
| 19 | + | use std::mem; |
|
| 20 | + | use std::mem::heap; |
|
| 21 | + | use std::sys; |
|
| 18 | 22 | ||
| 19 | 23 | /// Native fixture entry trampoline address. |
|
| 20 | 24 | constant CODE_BASE: u64 = 0x80010000; |
|
| 21 | 25 | /// First linked package code address. |
|
| 22 | 26 | constant PACKAGE_CODE_BASE: u64 = 0x80011000; |
| 46 | 50 | /// Number of elements reserved for scratch storage. |
|
| 47 | 51 | constant SCRATCH_CAPACITY: u32 = 16777216; |
|
| 48 | 52 | /// Number of elements reserved for decode storage. |
|
| 49 | 53 | constant DECODE_CAPACITY: u32 = 1048576; |
|
| 50 | 54 | /// Number of elements reserved for input storage. |
|
| 51 | - | constant INPUT_CAPACITY: u32 = 65536; |
|
| 55 | + | constant INPUT_CAPACITY: u32 = 131072; |
|
| 52 | 56 | /// Number of elements reserved for templates storage. |
|
| 53 | 57 | constant TEMPLATES_CAPACITY: u32 = 16384; |
|
| 54 | 58 | /// Number of elements reserved for symbols storage. |
|
| 55 | 59 | constant SYMBOLS_CAPACITY: u32 = 128; |
|
| 56 | 60 | /// Number of elements reserved for exports storage. |
| 85 | 89 | /// Native instruction image, with fixed package placements. |
|
| 86 | 90 | static TEXT: [u32; TEXT_CAPACITY] = [0; TEXT_CAPACITY]; |
|
| 87 | 91 | /// Two private graphs followed by their package-state tables. |
|
| 88 | 92 | static STATE: [u8; STATE_CAPACITY] = [0; STATE_CAPACITY]; |
|
| 89 | 93 | ||
| 90 | - | /// Decode trusted package IL into persistent storage. |
|
| 94 | + | /// Decode package IL into persistent storage. |
|
| 91 | 95 | unsafe fn load(path: *[u8], arena: &heap::Heap) -> binary::Package { |
|
| 92 | 96 | let length = try fs::readFile(path, &mut INPUT[..]) catch { |
|
| 93 | 97 | panic "load: missing package"; |
|
| 94 | 98 | }; |
|
| 95 | 99 | return try! program::decode(&INPUT[.. length], arena, binary::Limits { |
| 113 | 117 | data: &mut DATA[index][..], |
|
| 114 | 118 | symbols: &mut SYMBOLS[index][..], |
|
| 115 | 119 | exports: &mut EXPORTS[index][..], |
|
| 116 | 120 | template: &mut TEMPLATES[index][..], |
|
| 117 | 121 | relocations: &mut RELOCS[index][..], |
|
| 118 | - | stack: rv64::StackCheck::Off, |
|
| 122 | + | stack: rv64::StackCheck::TpLimit, |
|
| 119 | 123 | }, |
|
| 120 | 124 | arena, |
|
| 121 | 125 | scratch |
|
| 122 | 126 | ); |
|
| 123 | 127 | } |
| 145 | 149 | @default unsafe fn main(env: *sys::Env) -> i32 { |
|
| 146 | 150 | // A fourth argument selects the typed-catch scenario, whose entry returns 222. |
|
| 147 | 151 | assert env.args.len == 3 or env.args.len == 4; |
|
| 148 | 152 | let catchScenario = env.args.len == 4; |
|
| 149 | 153 | let decoder = heap::over(&mut DECODE[..]); |
|
| 150 | - | let supportInput = load(env.args[0], decoder); |
|
| 151 | - | let appInput = load(env.args[1], decoder); |
|
| 154 | + | let mut supportInput = load(env.args[0], decoder); |
|
| 155 | + | let mut appInput = load(env.args[1], decoder); |
|
| 156 | + | let config = verify::Config { |
|
| 157 | + | registers: liveness::MAX_SSA_REGS, |
|
| 158 | + | blocks: labels::MAX_BLOCKS_PER_FN, |
|
| 159 | + | layoutSize: 1048576, |
|
| 160 | + | layoutDepth: 64, |
|
| 161 | + | params: 64, |
|
| 162 | + | envSize: 0, |
|
| 163 | + | }; |
|
| 164 | + | let scratch = heap::over(&mut SCRATCH[..]); |
|
| 165 | + | try verify::verifyPackage(&mut supportInput, config, scratch, decoder) catch err { |
|
| 166 | + | io::print("support verification: "); |
|
| 167 | + | io::printLn(verify::ruleName(err.rule)); |
|
| 168 | + | return 1; |
|
| 169 | + | }; |
|
| 170 | + | let identity = supportInput.links.image else panic "main: missing image identity"; |
|
| 171 | + | let provider = linking::Provider { |
|
| 172 | + | image: identity, contract: supportInput |
|
| 173 | + | }; |
|
| 174 | + | let checking = heap::over(&mut SCRATCH[..]); |
|
| 175 | + | try verify::verifyLinkedPackage( |
|
| 176 | + | &mut appInput, &[provider], config, checking, decoder |
|
| 177 | + | ) catch err { |
|
| 178 | + | io::print("app verification: "); |
|
| 179 | + | io::printLn(verify::ruleName(err.rule)); |
|
| 180 | + | if let case verify::Place::Instr { func, block, instr } = err.place { |
|
| 181 | + | io::printLn(appInput.program.fns[func].name); |
|
| 182 | + | io::printU32(block); |
|
| 183 | + | io::print(":"); |
|
| 184 | + | io::printU32(instr); |
|
| 185 | + | io::print("\n"); |
|
| 186 | + | } |
|
| 187 | + | if let case verify::Place::Data(i) = err.place { |
|
| 188 | + | io::printLn(appInput.program.data[i].name); |
|
| 189 | + | } |
|
| 190 | + | return 1; |
|
| 191 | + | }; |
|
| 192 | + | assert appInput.links.pins[0] == identity; |
|
| 152 | 193 | let support = try! compile(&supportInput, 0, &[]); |
|
| 153 | 194 | let mut far: [shared::Symbol; EXPORTS_CAPACITY] = undefined; |
|
| 154 | 195 | for symbol, i in support.exports { |
|
| 155 | 196 | set far[i] = symbol; |
|
| 156 | 197 | match symbol.target { |
| 164 | 205 | try compile(&appInput, 1, &far[.. support.exports.len]) catch err { |
|
| 165 | 206 | assert err == shared::Error::Range; |
|
| 166 | 207 | set rejected = true; |
|
| 167 | 208 | }; |
|
| 168 | 209 | assert rejected; |
|
| 169 | - | let app = try! compile(&appInput, 1, support.exports); |
|
| 210 | + | let mut imports: [shared::Symbol; EXPORTS_CAPACITY] = undefined; |
|
| 211 | + | assert appInput.links.imports.len <= imports.len; |
|
| 212 | + | for imported, i in appInput.links.imports { |
|
| 213 | + | let mut target: ?shared::Target = nil; |
|
| 214 | + | for symbol in support.exports { |
|
| 215 | + | if mem::eq(symbol.name, imported.exported) { |
|
| 216 | + | set target = symbol.target; |
|
| 217 | + | } |
|
| 218 | + | } |
|
| 219 | + | let resolved = target else panic "main: missing export"; |
|
| 220 | + | set imports[i] = shared::Symbol { |
|
| 221 | + | name: imported.name, target: resolved, startable: false |
|
| 222 | + | }; |
|
| 223 | + | } |
|
| 224 | + | let app = try! compile(&appInput, 1, &imports[.. appInput.links.imports.len]); |
|
| 225 | + | assert support.contract.verification == shared::Verification::Verified; |
|
| 226 | + | assert app.contract.verification == shared::Verification::Verified; |
|
| 170 | 227 | let catalog = [support, app]; |
|
| 171 | 228 | assert catchScenario or app.relocations.len > 0; |
|
| 172 | 229 | let entry = app.entry else panic "main: missing entry"; |
|
| 173 | 230 | for i in 0 .. STATE.len { |
|
| 174 | 231 | set STATE[i] = 0; |
| 203 | 260 | &mut e, |
|
| 204 | 261 | rv64::GP, |
|
| 205 | 262 | (RW_BASE |
|
| 206 | 263 | + TABLES_OFFSET as u64) as i64 |
|
| 207 | 264 | ); |
|
| 265 | + | try! emit::loadImm(&mut e, rv64::TP, 0); |
|
| 208 | 266 | let first = check(&mut e, entry, 222 if catchScenario else 142); |
|
| 209 | 267 | let second = check(&mut e, entry, 222 if catchScenario else 143); |
|
| 210 | 268 | try! emit::loadImm( |
|
| 211 | 269 | &mut e, |
|
| 212 | 270 | rv64::GP, |
test/shared/support.rad
+48 -0
| 13 | 13 | unsafe { |
|
| 14 | 14 | set padding[8191] = 0; |
|
| 15 | 15 | return value + tail; |
|
| 16 | 16 | } |
|
| 17 | 17 | } |
|
| 18 | + | ||
| 19 | + | /// Value with a public nominal identity and one trait implementation. |
|
| 20 | + | export record Number: Copy { |
|
| 21 | + | /// Value read through the trait. |
|
| 22 | + | value: i32, |
|
| 23 | + | /// Second word that requires a caller-provided return buffer. |
|
| 24 | + | extra: u64, |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Read access to a value. |
|
| 28 | + | export trait Reading { |
|
| 29 | + | /// Read the receiver's value. |
|
| 30 | + | fn (&Reading) get() -> i32; |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | /// Reading access to a Number. |
|
| 34 | + | instance Reading for Number { |
|
| 35 | + | /// Read the receiver's value. |
|
| 36 | + | fn (self: &Number) get() -> i32 { |
|
| 37 | + | return self.value; |
|
| 38 | + | } |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Preserve the input storage region in the result. |
|
| 42 | + | export fn borrow(value: &'a Number) -> &'a Number { |
|
| 43 | + | return value; |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Return a record through the caller's output buffer. |
|
| 47 | + | export fn copied(value: &Number) -> Number { |
|
| 48 | + | return *value; |
|
| 49 | + | } |
|
| 50 | + | ||
| 51 | + | /// Dispatch through a borrowed trait supplied by the caller. |
|
| 52 | + | export fn reading(value: &opaque Reading) -> i32 { |
|
| 53 | + | return value.get(); |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// A borrowed value returned through the caller's storage. |
|
| 57 | + | export record Reference['r]: Copy { |
|
| 58 | + | /// Storage borrowed from the input value. |
|
| 59 | + | value: &'r Number, |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | /// Transfer the input borrow into the caller's result buffer. |
|
| 63 | + | export fn referenced(value: &'r Number) -> Reference['r] { |
|
| 64 | + | return Reference { value }; |
|
| 65 | + | } |