.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
binary/
tests/
cfg.rad
8.2 KiB
collect.rad
4.3 KiB
linkage.rad
4.8 KiB
program.rad
30.5 KiB
reader.rad
28.5 KiB
tests.rad
30.4 KiB
writer.rad
28.4 KiB
link/
printer/
verify/
binary.rad
14.7 KiB
link.rad
11.8 KiB
printer.rad
31.7 KiB
verify.rad
99.5 KiB
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/lang/il/binary/linkage.rad
raw
| 1 | //! Content identities, 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 | /// Bytes in a content identity. |
| 11 | export constant ID_BYTES: u32 = blake3::DIGEST_BYTES; |
| 12 | |
| 13 | /// Image identity domain: RIL, an ASCII version digit, and four zero padding bytes. |
| 14 | /// The eight-byte length preserves word alignment for a contiguous payload. |
| 15 | constant IMAGE_PREFIX: *[u8] = &['R', 'I', 'L', '0', 0, 0, 0, 0]; |
| 16 | |
| 17 | /// Hash a complete versioned package payload with the image identity prefix. |
| 18 | export fn imageId(bytes: &[u8]) -> binary::Id { |
| 19 | return imageParts(bytes, &[]); |
| 20 | } |
| 21 | |
| 22 | /// Hash a package header followed by its streamed function bodies. |
| 23 | export fn imageParts(head: &[u8], functions: &[u8]) -> binary::Id { |
| 24 | let mut state = blake3::new(); |
| 25 | blake3::update(&mut state, IMAGE_PREFIX); |
| 26 | blake3::update(&mut state, head); |
| 27 | blake3::update(&mut state, functions); |
| 28 | let digest = blake3::finish(state); |
| 29 | return binary::Id { bytes: digest.bytes }; |
| 30 | } |
| 31 | |
| 32 | /// Write the exact bytes of a content identity. |
| 33 | export fn writeId(out: &mut writer::Writer, id: &binary::Id) throws (binary::Error) { |
| 34 | for byte in &id.bytes[..] { |
| 35 | try writer::byte(out, byte as u64); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Read a complete content identity. |
| 40 | export fn readId(input: &mut reader::Reader['wire]) -> binary::Id throws (binary::Error) { |
| 41 | let mut result = binary::Id { bytes: [0; ID_BYTES] }; |
| 42 | for i in 0 .. result.bytes.len { |
| 43 | set result.bytes[i] = try input.byte() as u8; |
| 44 | } |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | /// Encode typed import bindings. |
| 49 | export fn write( |
| 50 | out: &mut writer::Writer, imports: &[binary::Import], symbols: &mut opaque writer::SymbolTable |
| 51 | ) throws (binary::Error) { |
| 52 | try writer::word(out, imports.len as u64); |
| 53 | for item in imports { |
| 54 | try writer::word(out, item.dependency as u64); |
| 55 | try writer::symbol(out, item.exported, symbols); |
| 56 | match item.kind { |
| 57 | case binary::ImportKind::Function(signature) => { |
| 58 | try writer::byte(out, binary::SYMBOL_FUNCTION); |
| 59 | try writer::word(out, signature as u64); |
| 60 | } |
| 61 | case binary::ImportKind::Data { layout, readOnly, heap } => { |
| 62 | try writer::byte(out, binary::SYMBOL_DATA); |
| 63 | try writer::word(out, layout as u64); |
| 64 | try writer::byte(out, 1 if readOnly else 0); |
| 65 | if let rights = heap { |
| 66 | try writer::byte(out, 1 + writer::heapFlags(rights) as u64); |
| 67 | } else { |
| 68 | try writer::byte(out, 0); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Decode typed import bindings before reading the program's descriptor tables. |
| 76 | export fn read(input: &mut reader::Reader['wire], arena: &heap::Heap) -> *[binary::Import] |
| 77 | throws (binary::Error) |
| 78 | { |
| 79 | let importCount = try input.count(binary::MIN_IMPORT_BYTES); |
| 80 | let mut imports: *mut [binary::Import] = &mut []; |
| 81 | try imports.reserve(importCount, arena) catch { |
| 82 | throw binary::Error::Storage; |
| 83 | }; |
| 84 | for _ in 0 .. importCount { |
| 85 | let dependency = try input.word() as u32; |
| 86 | let exported = try input.symbol(); |
| 87 | let tag = try input.byte(); |
| 88 | let mut kind = binary::ImportKind::Function(0); |
| 89 | if tag == binary::SYMBOL_FUNCTION { |
| 90 | set kind = binary::ImportKind::Function(try input.word() as u32); |
| 91 | } else if tag == binary::SYMBOL_DATA { |
| 92 | let layout = try input.word() as u32; |
| 93 | let readOnly = try input.flag(); |
| 94 | let rights = try input.byte() as u8; |
| 95 | let mut heap: ?il::HeapType = nil; |
| 96 | if rights > 0 { |
| 97 | set heap = try reader::heapRights(rights - 1); |
| 98 | } |
| 99 | set kind = binary::ImportKind::Data { layout, readOnly, heap }; |
| 100 | } else { |
| 101 | throw binary::Error::Invalid; |
| 102 | } |
| 103 | try imports.append(binary::Import { dependency, exported, kind }, arena) catch { |
| 104 | throw binary::Error::Storage; |
| 105 | }; |
| 106 | } |
| 107 | return &imports[..]; |
| 108 | } |
| 109 | |
| 110 | /// Check import indices against the decoded package table sizes. |
| 111 | export fn check(links: &binary::Links, dependencies: u32, layouts: u32, signatures: u32) |
| 112 | throws (binary::Error) |
| 113 | { |
| 114 | if links.pins.len <> dependencies { |
| 115 | throw binary::Error::Invalid; |
| 116 | } |
| 117 | for item in links.imports { |
| 118 | if item.dependency >= dependencies { |
| 119 | throw binary::Error::Invalid; |
| 120 | } |
| 121 | match item.kind { |
| 122 | case binary::ImportKind::Function(index) => { |
| 123 | if index >= signatures { |
| 124 | throw binary::Error::Invalid; |
| 125 | } |
| 126 | } |
| 127 | case binary::ImportKind::Data { layout, .. } => { |
| 128 | if layout >= layouts { |
| 129 | throw binary::Error::Invalid; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |