.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
binary/
link/
printer/
verify/
data.rad
12.4 KiB
flow.rad
6.9 KiB
layout.rad
30.9 KiB
linking.rad
7.2 KiB
tests.rad
216.6 KiB
binary.rad
14.7 KiB
link.rad
11.8 KiB
printer.rad
31.7 KiB
verify.rad
99.5 KiB
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/il/verify/linking.rad
raw
| 1 | //! Typed import environments for verified provider metadata. |
| 2 | //! |
| 3 | //! Imports match their providers by structure: layouts, signatures and traits |
| 4 | //! carry no owner. Verification guarantees memory safety, not encapsulation. |
| 5 | //! A verified client can build any value whose layout fits, including a value of |
| 6 | //! a provider's opaque record, so `opaque` hides a representation only from source |
| 7 | //! code. Provider code must treat every value it receives as untrusted, and an |
| 8 | //! opaque record cannot serve as authority across a verified boundary; kernel |
| 9 | //! handles, or keys that the provider checks against state it holds, can. |
| 10 | //! Memory safety holds because every provider is itself verified against any |
| 11 | //! input of its types. A verified client therefore never links against an |
| 12 | //! unverified provider, whose unsafe code might rely on opaque invariants. |
| 13 | |
| 14 | use std::lang::il; |
| 15 | use std::lang::il::binary; |
| 16 | use std::lang::il::link; |
| 17 | use std::lang::il::verify; |
| 18 | use std::mem; |
| 19 | use std::mem::heap; |
| 20 | |
| 21 | /// Declarations of code which the caller has already verified. |
| 22 | /// Kernel callers obtain these only from verified resident image descriptors. |
| 23 | /// An unverified provider must never be supplied: see the module documentation. |
| 24 | export record Provider: Copy { |
| 25 | /// Identity of the resident image's complete immutable source payload. |
| 26 | image: binary::Id, |
| 27 | /// Decoded declarations from that verified image, retained for this check. |
| 28 | contract: binary::Package, |
| 29 | } |
| 30 | |
| 31 | /// A loadable-package contract violation. |
| 32 | constant BAD: verify::Error = verify::Error { |
| 33 | rule: verify::Rule::Package, place: verify::Place::Package |
| 34 | }; |
| 35 | |
| 36 | /// Convert a graph workspace failure into the verifier's resource errors. |
| 37 | fn graphError(error: link::Error) -> verify::Error { |
| 38 | match error { |
| 39 | case link::Error::Limit => |
| 40 | return verify::Error { |
| 41 | rule: verify::Rule::Limit, place: verify::Place::Package |
| 42 | }, |
| 43 | case link::Error::Storage => return verify::STORAGE, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Append a local signature as a checked external declaration. |
| 48 | fn function( |
| 49 | fns: &mut *mut [*il::Fn], name: *[u8], sig: il::Signature, arena: &heap::Heap |
| 50 | ) throws (verify::Error) { |
| 51 | let mut params: *mut [il::Param] = &mut []; |
| 52 | for typ, i in sig.params { |
| 53 | try params.append(il::Param { value: il::Reg { n: i }, type: typ }, arena) catch { |
| 54 | throw verify::STORAGE; |
| 55 | }; |
| 56 | } |
| 57 | let mut declaration: *mut [il::Fn] = &mut []; |
| 58 | try declaration.append( |
| 59 | il::Fn { |
| 60 | name, |
| 61 | params: ¶ms[..], |
| 62 | returnType: sig.ret, |
| 63 | isExtern: true, |
| 64 | isLeaf: true, |
| 65 | blocks: &[], |
| 66 | vals: &[], |
| 67 | cases: &[], |
| 68 | borrows: sig.borrows, |
| 69 | }, |
| 70 | arena |
| 71 | ) catch { |
| 72 | throw verify::STORAGE; |
| 73 | }; |
| 74 | let frozen: *[il::Fn] = &declaration[..]; |
| 75 | try fns.append(&frozen[0], arena) catch { |
| 76 | throw verify::STORAGE; |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | /// Match explicit imports to verified provider exports and extend only the verifier's |
| 81 | /// symbol environment. Imported metadata has no local native allocation. |
| 82 | export fn bind( |
| 83 | package: &binary::Package, providers: &[Provider], arena: &heap::Heap |
| 84 | ) -> il::Program throws (verify::Error) { |
| 85 | if package.dependencies.len <> providers.len or package.links.pins.len <> providers.len { |
| 86 | throw BAD; |
| 87 | } |
| 88 | for provider, i in providers { |
| 89 | let pin = package.links.pins[i]; |
| 90 | if not mem::eq(&pin.bytes[..], &provider.contract.links.metadata.bytes[..]) { |
| 91 | throw BAD; |
| 92 | } |
| 93 | for earlier in &providers[.. i] { |
| 94 | if mem::eq(&earlier.image.bytes[..], &provider.image.bytes[..]) { |
| 95 | throw BAD; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | if package.links.imports.len == 0 { |
| 100 | return package.program; |
| 101 | } |
| 102 | let leftCount = package.program.layouts.len as u64 |
| 103 | + package.program.signatures.len as u64 |
| 104 | + package.program.traits.len as u64; |
| 105 | let mut rightCount: u64 = 0; |
| 106 | for provider in providers { |
| 107 | let p = provider.contract.program; |
| 108 | let count = p.layouts.len as u64 + p.signatures.len as u64 + p.traits.len as u64; |
| 109 | if count > rightCount { |
| 110 | set rightCount = count; |
| 111 | } |
| 112 | } |
| 113 | let product = leftCount * rightCount; |
| 114 | let mut capacity: u32 = 1; |
| 115 | if product > 0 { |
| 116 | set capacity = link::MAX_PAIRS if product > link::MAX_PAIRS as u64 else product as u32; |
| 117 | } |
| 118 | let mut work = try link::workspace(capacity, arena) catch error { |
| 119 | throw graphError(error); |
| 120 | }; |
| 121 | let mut fns: *mut [*il::Fn] = &mut []; |
| 122 | let mut data: *mut [il::Data] = &mut []; |
| 123 | try fns.extend(package.program.fns, arena) catch { |
| 124 | throw verify::STORAGE; |
| 125 | }; |
| 126 | try data.extend(package.program.data, arena) catch { |
| 127 | throw verify::STORAGE; |
| 128 | }; |
| 129 | for imported in package.links.imports { |
| 130 | if imported.dependency >= providers.len { |
| 131 | throw BAD; |
| 132 | } |
| 133 | let provider = providers[imported.dependency].contract; |
| 134 | let exported = binary::find(provider.exports, imported.exported) else throw BAD; |
| 135 | match imported.kind { |
| 136 | case binary::ImportKind::Function(index) => { |
| 137 | let case binary::ExportKind::Function(target) = exported.kind else throw BAD; |
| 138 | if index >= package.program.signatures.len { |
| 139 | throw BAD; |
| 140 | } |
| 141 | let definition = provider.program.fns[target]; |
| 142 | let sig = package.program.signatures[index]; |
| 143 | let matches = try link::sameCallable( |
| 144 | &mut work, &package.program, &sig, &provider.program, definition |
| 145 | ) catch error { |
| 146 | throw graphError(error); |
| 147 | }; |
| 148 | if not matches { |
| 149 | throw BAD; |
| 150 | } |
| 151 | try function(&mut fns, imported.exported, sig, arena); |
| 152 | } |
| 153 | case binary::ImportKind::Data { layout, readOnly, heap } => { |
| 154 | let case binary::ExportKind::Data(target) = exported.kind else throw BAD; |
| 155 | if layout >= package.program.layouts.len { |
| 156 | throw BAD; |
| 157 | } |
| 158 | let definition = provider.program.data[target]; |
| 159 | let info = package.program.layouts[layout]; |
| 160 | let declaration = il::Data { |
| 161 | name: imported.exported, |
| 162 | size: info.size, |
| 163 | alignment: info.alignment, |
| 164 | layout, |
| 165 | readOnly, |
| 166 | heap, |
| 167 | isZeroInit: definition.isZeroInit, |
| 168 | values: definition.values, |
| 169 | }; |
| 170 | let matches = try link::sameData( |
| 171 | &mut work, &package.program, &declaration, &provider.program, &definition |
| 172 | ) catch error { |
| 173 | throw graphError(error); |
| 174 | }; |
| 175 | if not matches { |
| 176 | throw BAD; |
| 177 | } |
| 178 | try data.append(declaration, arena) catch { |
| 179 | throw verify::STORAGE; |
| 180 | }; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | return il::Program { |
| 185 | data: &data[..], |
| 186 | fns: &fns[..], |
| 187 | layouts: package.program.layouts, |
| 188 | signatures: package.program.signatures, |
| 189 | traits: package.program.traits, |
| 190 | }; |
| 191 | } |