lib/std/lang/il/binary/linkage.rad 4.8 KiB 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
}