.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/program.rad
raw
| 1 | //! Binary package envelopes and IL program reconstruction. |
| 2 | //! Tables occur in this order: symbols, package name and metadata identity, |
| 3 | //! dependencies with their pins, exports, entry, imports, layouts, signatures, |
| 4 | //! traits, data, functions. Block records carry parameters and instructions. The |
| 5 | //! decoder derives predecessors and loop depth from the terminators, and names |
| 6 | //! each decoded block by its index. |
| 7 | |
| 8 | use std::collections::dict; |
| 9 | use std::lang::il; |
| 10 | use std::lang::il::binary; |
| 11 | use std::lang::il::binary::cfg; |
| 12 | use std::lang::il::binary::collect; |
| 13 | use std::lang::il::binary::linkage; |
| 14 | use std::lang::il::binary::reader; |
| 15 | use std::lang::il::binary::writer; |
| 16 | use std::mem; |
| 17 | use std::mem::heap; |
| 18 | |
| 19 | /// Largest symbol table checked by direct comparison without an auxiliary index. |
| 20 | constant SMALL_SYMBOLS: u32 = 32; |
| 21 | /// Largest symbol count whose doubled hash-table size is representable. |
| 22 | constant MAX_SYMBOLS: u32 = 0x3fffffff; |
| 23 | |
| 24 | /// Function content retained while decoding a package. |
| 25 | union Contents: Copy { |
| 26 | /// Reconstruct complete function bodies. |
| 27 | Bodies, |
| 28 | /// Retain callable headers from an already verified payload. |
| 29 | Declarations, |
| 30 | } |
| 31 | |
| 32 | /// Write a counted sequence of typed SSA parameters. Function parameters are |
| 33 | /// registers zero through the parameter count, so only block parameters store |
| 34 | /// their register numbers. |
| 35 | fn writeParams(out: &mut writer::Writer, params: &[il::Param], numbered: bool) |
| 36 | throws (binary::Error) |
| 37 | { |
| 38 | try writer::word(out, params.len as u64); |
| 39 | for param, i in params { |
| 40 | if numbered { |
| 41 | try writer::word(out, param.value.n as u64); |
| 42 | } else if param.value.n <> i { |
| 43 | throw binary::Error::Invalid; |
| 44 | } |
| 45 | try writer::valType(out, param.type); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Write the package tables through `out`, up to and including the function count. |
| 50 | fn writeHead( |
| 51 | out: &mut writer::Writer, |
| 52 | package: &binary::Package, |
| 53 | fnCount: u32, |
| 54 | symbols: &mut opaque writer::SymbolTable |
| 55 | ) throws (binary::Error) { |
| 56 | try writer::word(out, binary::MAGIC as u64); |
| 57 | try writer::word(out, binary::VERSION as u64); |
| 58 | try writer::word(out, package.symbols.len as u64); |
| 59 | for name in package.symbols { |
| 60 | try writer::bytes(out, name); |
| 61 | } |
| 62 | try writer::symbol(out, package.name, symbols); |
| 63 | if let version = package.version { |
| 64 | if binary::versionParts(version) == nil { |
| 65 | throw binary::Error::Invalid; |
| 66 | } |
| 67 | try writer::byte(out, 1); |
| 68 | try writer::bytes(out, version); |
| 69 | } else { |
| 70 | try writer::byte(out, 0); |
| 71 | } |
| 72 | try linkage::writeId(out, &package.links.metadata); |
| 73 | if package.links.pins.len <> package.dependencies.len { |
| 74 | throw binary::Error::Invalid; |
| 75 | } |
| 76 | try writer::word(out, package.dependencies.len as u64); |
| 77 | for name, i in package.dependencies { |
| 78 | try writer::symbol(out, name, symbols); |
| 79 | try linkage::writeId(out, &package.links.pins[i]); |
| 80 | } |
| 81 | try writer::word(out, package.exports.len as u64); |
| 82 | for item in package.exports { |
| 83 | try writer::symbol(out, item.name, symbols); |
| 84 | match item.kind { |
| 85 | case binary::ExportKind::Function(index) => { |
| 86 | try writer::byte(out, binary::SYMBOL_FUNCTION); |
| 87 | try writer::word(out, index as u64); |
| 88 | } |
| 89 | case binary::ExportKind::Data(index) => { |
| 90 | try writer::byte(out, binary::SYMBOL_DATA); |
| 91 | try writer::word(out, index as u64); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | if let entry = package.entry { |
| 96 | try writer::byte(out, 1); |
| 97 | try writer::symbol(out, entry, symbols); |
| 98 | } else { |
| 99 | try writer::byte(out, 0); |
| 100 | } |
| 101 | try linkage::write(out, package.links.imports, symbols); |
| 102 | try writer::word(out, package.program.layouts.len as u64); |
| 103 | try writer::word(out, package.program.signatures.len as u64); |
| 104 | try writer::word(out, package.program.traits.len as u64); |
| 105 | for item in package.program.layouts { |
| 106 | try writer::layout(out, item); |
| 107 | } |
| 108 | for item in package.program.signatures { |
| 109 | try writer::signature(out, item); |
| 110 | } |
| 111 | for item in package.program.traits { |
| 112 | try writer::traitRecord(out, item); |
| 113 | } |
| 114 | try writer::word(out, package.program.data.len as u64); |
| 115 | for item in package.program.data { |
| 116 | try writer::symbol(out, item.name, symbols); |
| 117 | try writer::word(out, item.layout as u64); |
| 118 | try writer::byte(out, 1 if item.readOnly else 0); |
| 119 | try writer::byte(out, 1 if item.isZeroInit else 0); |
| 120 | if let heap = item.heap { |
| 121 | try writer::byte(out, 1 + writer::heapFlags(heap) as u64); |
| 122 | } else { |
| 123 | try writer::byte(out, 0); |
| 124 | } |
| 125 | try writer::word(out, item.values.len as u64); |
| 126 | for value in item.values { |
| 127 | try writer::dataValue(out, value, symbols); |
| 128 | } |
| 129 | } |
| 130 | try writer::word(out, fnCount as u64); |
| 131 | } |
| 132 | |
| 133 | /// Write one length-delimited function: name, signature, and blocks. A function |
| 134 | /// without blocks is external, and every other function has an entry block. |
| 135 | fn writeFunction(out: &mut writer::Writer, func: *il::Fn, symbols: &mut opaque writer::SymbolTable) |
| 136 | throws (binary::Error) |
| 137 | { |
| 138 | if func.isExtern <> (func.blocks.len == 0) { |
| 139 | throw binary::Error::Invalid; |
| 140 | } |
| 141 | let lengthOffset = out.offset; |
| 142 | try writer::word(out, 0); |
| 143 | let start = out.offset; |
| 144 | try writer::symbol(out, func.name, symbols); |
| 145 | try writer::valType(out, func.returnType); |
| 146 | try writer::borrows(out, func.borrows); |
| 147 | try writeParams(out, func.params, false); |
| 148 | try writer::word(out, func.blocks.len as u64); |
| 149 | for block in func.blocks { |
| 150 | try writeParams(out, block.params, true); |
| 151 | try writer::word(out, block.instrs.len as u64); |
| 152 | for instr in block.instrs { |
| 153 | try writer::instr(out, func, instr, symbols); |
| 154 | } |
| 155 | } |
| 156 | mem::store32Le(&mut out.bytes[..], lengthOffset, out.offset - start); |
| 157 | } |
| 158 | |
| 159 | /// Write a complete package at the current cursor. Return the final cursor offset. |
| 160 | export fn write(out: &mut writer::Writer, package: &binary::Package) -> u32 throws (binary::Error) { |
| 161 | let mut table = writer::Listed { names: package.symbols }; |
| 162 | let symbols: &mut opaque writer::SymbolTable = &mut table; |
| 163 | try writeHead(out, package, package.program.fns.len, symbols); |
| 164 | for func in package.program.fns { |
| 165 | try writeFunction(out, func, symbols); |
| 166 | } |
| 167 | return out.offset; |
| 168 | } |
| 169 | |
| 170 | /// Write a package into `bytes`, returning the buffer on success or failure. |
| 171 | export fn encode(bytes: &mut *mut [u8], package: &binary::Package) -> u32 throws (binary::Error) { |
| 172 | let mut out = writer::new(bytes.take()); |
| 173 | let length = try write(&mut out, package) catch error { |
| 174 | set *bytes = out.bytes.take(); |
| 175 | throw error; |
| 176 | }; |
| 177 | set *bytes = out.bytes.take(); |
| 178 | return length; |
| 179 | } |
| 180 | |
| 181 | /// Write package tables through the function count using a complete name index. |
| 182 | /// The buffer and index return to the caller on success or failure. |
| 183 | export fn encodeHead( |
| 184 | bytes: &mut *mut [u8], package: &binary::Package, index: &mut dict::Dict, fnCount: u32 |
| 185 | ) -> u32 throws (binary::Error) { |
| 186 | let mut out = writer::new(bytes.take()); |
| 187 | let mut table = writer::Indexed { table: dict::take(index) }; |
| 188 | try writeHead(&mut out, package, fnCount, &mut table) catch error { |
| 189 | set *bytes = out.bytes.take(); |
| 190 | set *index = dict::take(&mut table.table); |
| 191 | throw error; |
| 192 | }; |
| 193 | let end = out.offset; |
| 194 | set *bytes = out.bytes.take(); |
| 195 | set *index = dict::take(&mut table.table); |
| 196 | return end; |
| 197 | } |
| 198 | |
| 199 | /// Encode one function and intern its qualified names in first-use order. |
| 200 | /// Buffer and name storage return to their owners on success and failure. |
| 201 | export fn appendCollected( |
| 202 | bytes: &mut *mut [u8], used: u32, names: &mut collect::Names, func: *il::Fn, arena: *heap::Heap |
| 203 | ) -> u32 throws (binary::Error) { |
| 204 | assert used <= bytes.len; |
| 205 | let mut out = writer::Writer { bytes: bytes.take(), offset: used }; |
| 206 | let mut table = writer::Collected { |
| 207 | names: collect::Names { |
| 208 | symbols: names.symbols.take(), index: dict::take(&mut names.index) |
| 209 | }, |
| 210 | arena, |
| 211 | }; |
| 212 | try writeFunction(&mut out, func, &mut table) catch error { |
| 213 | set *bytes = out.bytes.take(); |
| 214 | set names.symbols = table.names.symbols.take(); |
| 215 | set names.index = dict::take(&mut table.names.index); |
| 216 | throw error; |
| 217 | }; |
| 218 | let end = out.offset; |
| 219 | set *bytes = out.bytes.take(); |
| 220 | set names.symbols = table.names.symbols.take(); |
| 221 | set names.index = dict::take(&mut table.names.index); |
| 222 | return end; |
| 223 | } |
| 224 | |
| 225 | /// Read typed SSA parameters with checked register indices. Function parameters |
| 226 | /// are registers zero through the parameter count. |
| 227 | fn readParams( |
| 228 | input: &mut reader::Reader['wire], a: &heap::Heap, numbered: bool |
| 229 | ) -> *[il::Param] throws (binary::Error) { |
| 230 | let n = try input.count(binary::MIN_PARAM_BYTES if numbered else 1); |
| 231 | if not numbered and n > input.registers { |
| 232 | throw binary::Error::Invalid; |
| 233 | } |
| 234 | let mut params: *mut [il::Param] = &mut []; |
| 235 | try params.reserve(n, a) catch { |
| 236 | throw binary::Error::Storage; |
| 237 | }; |
| 238 | for i in 0 .. n { |
| 239 | let mut value = il::Reg { n: i }; |
| 240 | if numbered { |
| 241 | set value = try input.reg(); |
| 242 | } |
| 243 | let t = try input.valType(); |
| 244 | try params.append(il::Param { value, type: t }, a) catch { |
| 245 | throw binary::Error::Storage; |
| 246 | }; |
| 247 | } |
| 248 | return ¶ms[..]; |
| 249 | } |
| 250 | |
| 251 | /// Read the layout table. Each layout is checked for structure: a power-of-two |
| 252 | /// alignment, a size that is a multiple of it, slots that are sorted, aligned, disjoint and |
| 253 | /// inside the size, and nested layouts that precede the layout that contains them. |
| 254 | fn readLayouts( |
| 255 | input: &mut reader::Reader['wire], a: &heap::Heap |
| 256 | ) -> *[il::Layout] throws (binary::Error) { |
| 257 | let mut items: *mut [il::Layout] = &mut []; |
| 258 | try items.reserve(input.layouts, a) catch { |
| 259 | throw binary::Error::Storage; |
| 260 | }; |
| 261 | for i in 0 .. input.layouts { |
| 262 | let size = try input.word() as u32; |
| 263 | let alignment = try input.word() as u32; |
| 264 | if alignment == 0 or (alignment & (alignment - 1)) <> 0 or size % alignment <> 0 { |
| 265 | throw binary::Error::Invalid; |
| 266 | } |
| 267 | let regions = try input.word() as u32; |
| 268 | let slotCount = try input.count(binary::MIN_SLOT_BYTES); |
| 269 | let mut slots: *mut [il::Slot] = &mut []; |
| 270 | try slots.reserve(slotCount, a) catch { |
| 271 | throw binary::Error::Storage; |
| 272 | }; |
| 273 | // End of the previous slot. |
| 274 | let mut end: u64 = 0; |
| 275 | for _ in 0 .. slotCount { |
| 276 | let offset = try input.word() as u32; |
| 277 | let tag = try input.byte() as u8; |
| 278 | let mut kind = il::SlotKind::Fn(0); |
| 279 | let mut width: u64 = 8; |
| 280 | let mut slotAlignment: u32 = 8; |
| 281 | match tag { |
| 282 | case binary::SLOT_PTR => { |
| 283 | let ptr = try input.ptrType(); |
| 284 | if not ptr.nullable { |
| 285 | throw binary::Error::Invalid; |
| 286 | } |
| 287 | set kind = il::SlotKind::Ptr(ptr); |
| 288 | } |
| 289 | case binary::SLOT_SLICE => { |
| 290 | let ptr = try input.ptrType(); |
| 291 | if not ptr.nullable { |
| 292 | throw binary::Error::Invalid; |
| 293 | } |
| 294 | set kind = il::SlotKind::Slice(ptr); |
| 295 | set width = 16; |
| 296 | } |
| 297 | case binary::SLOT_FN => { |
| 298 | set kind = il::SlotKind::Fn(try input.signatureIndex()); |
| 299 | } |
| 300 | case binary::SLOT_HEAP => { |
| 301 | set kind = il::SlotKind::Heap(try input.heapType()); |
| 302 | } |
| 303 | case binary::SLOT_TRAIT => { |
| 304 | let iface = try input.traitIndex(); |
| 305 | let rights = try input.rights(); |
| 306 | set kind = il::SlotKind::Trait(il::ObjectType { |
| 307 | iface, own: rights.own, mutable: rights.mutable |
| 308 | }); |
| 309 | set width = 16; |
| 310 | } |
| 311 | case binary::SLOT_UNION => { |
| 312 | let payload = try input.word() as u32; |
| 313 | let unionSize = try input.word() as u32; |
| 314 | let variantCount = try input.count(binary::MIN_VARIANT_BYTES); |
| 315 | let mut variants: *mut [il::UnionVariant] = &mut []; |
| 316 | try variants.reserve(variantCount, a) catch { |
| 317 | throw binary::Error::Storage; |
| 318 | }; |
| 319 | // The slot is as aligned as its payloads. |
| 320 | set slotAlignment = 1; |
| 321 | for _ in 0 .. variantCount { |
| 322 | let variantTag = try input.word() as u32; |
| 323 | let variantLayout = try input.optionalLayoutIndex(); |
| 324 | let variantRegions = try input.regions(a); |
| 325 | if let payloadLayout = variantLayout { |
| 326 | if payloadLayout >= i { |
| 327 | throw binary::Error::Invalid; |
| 328 | } |
| 329 | if items[payloadLayout].alignment > slotAlignment { |
| 330 | set slotAlignment = items[payloadLayout].alignment; |
| 331 | } |
| 332 | } |
| 333 | try variants.append( |
| 334 | il::UnionVariant { |
| 335 | tag: variantTag, layout: variantLayout, regions: variantRegions |
| 336 | }, |
| 337 | a |
| 338 | ) catch { |
| 339 | throw binary::Error::Storage; |
| 340 | }; |
| 341 | } |
| 342 | set kind = il::SlotKind::Union { |
| 343 | offset: payload, size: unionSize, variants: &variants[..] |
| 344 | }; |
| 345 | set width = unionSize as u64; |
| 346 | } |
| 347 | case binary::SLOT_SUB => { |
| 348 | let sub = try input.word() as u32; |
| 349 | if sub >= i { |
| 350 | throw binary::Error::Invalid; |
| 351 | } |
| 352 | set kind = il::SlotKind::Sub(sub); |
| 353 | set width = items[sub].size as u64; |
| 354 | set slotAlignment = items[sub].alignment; |
| 355 | } |
| 356 | case binary::SLOT_ARRAY => { |
| 357 | let element = try input.word() as u32; |
| 358 | let count = try input.word() as u32; |
| 359 | if element >= i { |
| 360 | throw binary::Error::Invalid; |
| 361 | } |
| 362 | set kind = il::SlotKind::Array { layout: element, count }; |
| 363 | set width = items[element].size as u64 * count as u64; |
| 364 | set slotAlignment = items[element].alignment; |
| 365 | } |
| 366 | else => throw binary::Error::Invalid, |
| 367 | } |
| 368 | if slotAlignment > alignment |
| 369 | or offset % slotAlignment <> 0 |
| 370 | or offset as u64 < end |
| 371 | or offset as u64 + width > size as u64 |
| 372 | { |
| 373 | throw binary::Error::Invalid; |
| 374 | } |
| 375 | set end = offset as u64 + width; |
| 376 | let slotRegions = try input.regions(a); |
| 377 | try slots.append(il::Slot { offset, kind, regions: slotRegions }, a) catch { |
| 378 | throw binary::Error::Storage; |
| 379 | }; |
| 380 | } |
| 381 | try items.append(il::Layout { size, alignment, slots: &slots[..], regions }, a) catch { |
| 382 | throw binary::Error::Storage; |
| 383 | }; |
| 384 | } |
| 385 | return &items[..]; |
| 386 | } |
| 387 | |
| 388 | /// Read the signature table. Value types are checked against the table sizes. |
| 389 | fn readSignatures( |
| 390 | input: &mut reader::Reader['wire], a: &heap::Heap |
| 391 | ) -> *[il::Signature] throws (binary::Error) { |
| 392 | let mut items: *mut [il::Signature] = &mut []; |
| 393 | try items.reserve(input.signatures, a) catch { |
| 394 | throw binary::Error::Storage; |
| 395 | }; |
| 396 | for _ in 0 .. input.signatures { |
| 397 | let paramCount = try input.count(1); |
| 398 | let mut params: *mut [il::ValType] = &mut []; |
| 399 | try params.reserve(paramCount, a) catch { |
| 400 | throw binary::Error::Storage; |
| 401 | }; |
| 402 | for _ in 0 .. paramCount { |
| 403 | let param = try input.valType(); |
| 404 | try params.append(param, a) catch { |
| 405 | throw binary::Error::Storage; |
| 406 | }; |
| 407 | } |
| 408 | let ret = try input.valType(); |
| 409 | let borrows = try input.borrows(a); |
| 410 | try items.append( |
| 411 | il::Signature { params: ¶ms[..], ret, borrows }, a |
| 412 | ) catch { |
| 413 | throw binary::Error::Storage; |
| 414 | }; |
| 415 | } |
| 416 | return &items[..]; |
| 417 | } |
| 418 | |
| 419 | /// Read the trait table. Signature indices are checked against the table size. |
| 420 | fn readTraits( |
| 421 | input: &mut reader::Reader['wire], a: &heap::Heap |
| 422 | ) -> *[il::Trait] throws (binary::Error) { |
| 423 | let mut items: *mut [il::Trait] = &mut []; |
| 424 | try items.reserve(input.traits, a) catch { |
| 425 | throw binary::Error::Storage; |
| 426 | }; |
| 427 | for _ in 0 .. input.traits { |
| 428 | let methodCount = try input.count(binary::MIN_METHOD_BYTES); |
| 429 | let mut methods: *mut [il::Method] = &mut []; |
| 430 | try methods.reserve(methodCount, a) catch { |
| 431 | throw binary::Error::Storage; |
| 432 | }; |
| 433 | for _ in 0 .. methodCount { |
| 434 | let sig = try input.signatureIndex(); |
| 435 | let receiver = try input.word() as u32; |
| 436 | let rights = try input.rights(); |
| 437 | try methods.append( |
| 438 | il::Method { sig, receiver, own: rights.own, mutable: rights.mutable }, a |
| 439 | ) catch { |
| 440 | throw binary::Error::Storage; |
| 441 | }; |
| 442 | } |
| 443 | try items.append(il::Trait { methods: &methods[..] }, a) catch { |
| 444 | throw binary::Error::Storage; |
| 445 | }; |
| 446 | } |
| 447 | return &items[..]; |
| 448 | } |
| 449 | |
| 450 | /// Read global data and check initializer extents against the layout storage. |
| 451 | fn readData( |
| 452 | input: &mut reader::Reader['wire], a: &heap::Heap, layouts: &[il::Layout] |
| 453 | ) -> *[il::Data] throws (binary::Error) { |
| 454 | let n = try input.count(binary::MIN_DATA_BYTES); |
| 455 | let mut items: *mut [il::Data] = &mut []; |
| 456 | try items.reserve(n, a) catch { |
| 457 | throw binary::Error::Storage; |
| 458 | }; |
| 459 | for _ in 0 .. n { |
| 460 | let name = try input.symbol(); |
| 461 | let layout = try input.layoutIndex(); |
| 462 | let size = layouts[layout].size; |
| 463 | let alignment = layouts[layout].alignment; |
| 464 | let readOnly = try input.flag(); |
| 465 | let isZeroInit = try input.flag(); |
| 466 | let heapKind = try input.byte() as u8; |
| 467 | let mut heap: ?il::HeapType = nil; |
| 468 | if heapKind <> 0 { |
| 469 | set heap = try reader::heapRights(heapKind - 1); |
| 470 | } |
| 471 | let count = try input.count( |
| 472 | binary::MIN_DATA_VALUE_BYTES |
| 473 | ); |
| 474 | let mut values: *mut [il::DataValue] = &mut []; |
| 475 | try values.reserve(count, a) catch { |
| 476 | throw binary::Error::Storage; |
| 477 | }; |
| 478 | let mut extent: u64 = 0; |
| 479 | for _ in 0 .. count { |
| 480 | let value = try input.dataValue(a); |
| 481 | let mut width: u32 = 0; |
| 482 | match value.item { |
| 483 | case il::DataItem::Val { typ, .. } => { |
| 484 | set width = il::typeSize(typ); |
| 485 | } |
| 486 | case il::DataItem::Sym(_), il::DataItem::Fn(_) => { |
| 487 | set width = 8; |
| 488 | } |
| 489 | case il::DataItem::Str(text) => { |
| 490 | set width = text.len; |
| 491 | } |
| 492 | case il::DataItem::Undef => { |
| 493 | set width = 1; |
| 494 | } |
| 495 | } |
| 496 | set extent += width as u64 * value.count as u64; |
| 497 | if extent > size as u64 { |
| 498 | throw binary::Error::Invalid; |
| 499 | } |
| 500 | try values.append(value, a) catch { |
| 501 | throw binary::Error::Storage; |
| 502 | }; |
| 503 | } |
| 504 | try items.append( |
| 505 | il::Data { |
| 506 | name, size, alignment, layout, readOnly, heap, isZeroInit, values: &values[..] |
| 507 | }, |
| 508 | a |
| 509 | ) catch { |
| 510 | throw binary::Error::Storage; |
| 511 | }; |
| 512 | } |
| 513 | return &items[..]; |
| 514 | } |
| 515 | |
| 516 | /// Read one block with its parameters and instructions, and name it by its index. |
| 517 | /// Return whether the block contains a call. |
| 518 | fn readBlock( |
| 519 | input: &mut reader::Reader['wire], |
| 520 | a: &heap::Heap, |
| 521 | blocks: &mut *mut [il::Block] |
| 522 | ) -> bool throws (binary::Error) { |
| 523 | let label = il::BlockLabel { base: "b", suffix: blocks.len }; |
| 524 | let params = try readParams(input, a, true); |
| 525 | let instrCount = try input.count(1); |
| 526 | let mut instrs: *mut [il::Instr] = &mut []; |
| 527 | try instrs.reserve(instrCount, a) catch { |
| 528 | throw binary::Error::Storage; |
| 529 | }; |
| 530 | let mut hasCall = false; |
| 531 | for _ in 0 .. instrCount { |
| 532 | let instr = try input.instr(a); |
| 533 | if il::isCall(&instr) { |
| 534 | set hasCall = true; |
| 535 | } |
| 536 | try instrs.append(instr, a) catch { |
| 537 | throw binary::Error::Storage; |
| 538 | }; |
| 539 | } |
| 540 | try blocks.append( |
| 541 | il::Block { |
| 542 | label, |
| 543 | params, |
| 544 | instrs: &instrs[..], |
| 545 | locs: &[], |
| 546 | preds: &[], |
| 547 | loopDepth: 0, |
| 548 | }, |
| 549 | a |
| 550 | ) catch { |
| 551 | throw binary::Error::Storage; |
| 552 | }; |
| 553 | return hasCall; |
| 554 | } |
| 555 | |
| 556 | /// Read callable headers and bound each function body to its encoded extent. |
| 557 | fn readFunctions( |
| 558 | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits, contents: Contents |
| 559 | ) -> *[*il::Fn] throws (binary::Error) { |
| 560 | let n = try input.count(binary::MIN_FUNCTION_BYTES); |
| 561 | let mut fns: *mut [*il::Fn] = &mut []; |
| 562 | try fns.reserve(n, a) catch { |
| 563 | throw binary::Error::Storage; |
| 564 | }; |
| 565 | for _ in 0 .. n { |
| 566 | let func = try readFunction(input, a, limits, contents); |
| 567 | try fns.append(func, a) catch { |
| 568 | throw binary::Error::Storage; |
| 569 | }; |
| 570 | } |
| 571 | return &fns[..]; |
| 572 | } |
| 573 | |
| 574 | /// Read one function from its bounded wire extent. |
| 575 | fn readFunction( |
| 576 | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits, contents: Contents |
| 577 | ) -> *il::Fn throws (binary::Error) { |
| 578 | set input.registers = limits.registers; |
| 579 | let length = try input.count(1); |
| 580 | let end = input.offset + length; |
| 581 | let wire = input.wire; |
| 582 | set input.wire = &wire[.. end]; |
| 583 | let name = try input.symbol(); |
| 584 | let returnType = try input.valType(); |
| 585 | let borrows = try input.borrows(a); |
| 586 | let params = try readParams(input, a, false); |
| 587 | let count = try input.count(binary::MIN_BLOCK_BYTES); |
| 588 | let mut blocks: *mut [il::Block] = &mut []; |
| 589 | let mut isLeaf = true; |
| 590 | if contents == Contents::Declarations { |
| 591 | set input.offset = end; |
| 592 | } else { |
| 593 | if count > limits.blocks { |
| 594 | throw binary::Error::Invalid; |
| 595 | } |
| 596 | set input.blocks = count; |
| 597 | try blocks.reserve(count, a) catch { |
| 598 | throw binary::Error::Storage; |
| 599 | }; |
| 600 | for _ in 0 .. count { |
| 601 | if try readBlock(input, a, &mut blocks) { |
| 602 | set isLeaf = false; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | if input.offset <> end { |
| 607 | throw binary::Error::Invalid; |
| 608 | } |
| 609 | set input.wire = wire; |
| 610 | try cfg::link(&mut blocks[..], &input.cases[..], a); |
| 611 | // The function takes the pools. The next function starts with empty ones. |
| 612 | let vals = input.vals.take(); |
| 613 | let cases = input.cases.take(); |
| 614 | let mut funcs: *mut [il::Fn] = &mut []; |
| 615 | try funcs.append( |
| 616 | il::Fn { |
| 617 | name, |
| 618 | params, |
| 619 | returnType, |
| 620 | isExtern: count == 0, |
| 621 | isLeaf, |
| 622 | blocks: &blocks[..], |
| 623 | vals: &vals[..], |
| 624 | cases: &cases[..], |
| 625 | borrows, |
| 626 | }, |
| 627 | a |
| 628 | ) catch { |
| 629 | throw binary::Error::Storage; |
| 630 | }; |
| 631 | return &funcs[0]; |
| 632 | } |
| 633 | |
| 634 | /// Read one complete function after the package header. The caller counts functions |
| 635 | /// and checks the final cursor. Function storage can be reset between calls. |
| 636 | export fn nextFunction( |
| 637 | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits |
| 638 | ) -> *il::Fn throws (binary::Error) { |
| 639 | return try readFunction(input, a, limits, Contents::Bodies); |
| 640 | } |
| 641 | |
| 642 | /// Read the package tables. The input must contain exactly one package. |
| 643 | fn readHeader( |
| 644 | input: &mut reader::Reader['wire], a: &heap::Heap |
| 645 | ) -> binary::Package throws (binary::Error) { |
| 646 | let magic = try input.word() as u32; |
| 647 | if magic <> binary::MAGIC { |
| 648 | throw binary::Error::Invalid; |
| 649 | } |
| 650 | if try input.word() <> binary::VERSION as u64 { |
| 651 | throw binary::Error::Invalid; |
| 652 | } |
| 653 | let symbolCount = try input.count(4); |
| 654 | let mut symbols: *mut [*[u8]] = &mut []; |
| 655 | try symbols.reserve(symbolCount, a) catch { |
| 656 | throw binary::Error::Storage; |
| 657 | }; |
| 658 | let mut slots: *mut [dict::Entry] = &mut []; |
| 659 | if symbolCount > SMALL_SYMBOLS { |
| 660 | if symbolCount > MAX_SYMBOLS { |
| 661 | throw binary::Error::Capacity; |
| 662 | } |
| 663 | let mut size: u32 = 2; |
| 664 | while size < symbolCount * 2 { |
| 665 | set size *= 2; |
| 666 | } |
| 667 | try slots.resize(size, dict::Entry { key: &[], value: 0 }, a) catch { |
| 668 | throw binary::Error::Storage; |
| 669 | }; |
| 670 | } |
| 671 | let mut seen = dict::Dict { entries: slots, count: 0 }; |
| 672 | for i in 0 .. symbolCount { |
| 673 | let name = try input.bytes(a); |
| 674 | if name.len == 0 { |
| 675 | throw binary::Error::Invalid; |
| 676 | } |
| 677 | if seen.entries.len > 0 { |
| 678 | let count = seen.count; |
| 679 | try dict::insert(&mut seen, name, 0) catch { |
| 680 | throw binary::Error::Storage; |
| 681 | }; |
| 682 | if seen.count == count { |
| 683 | throw binary::Error::Invalid; |
| 684 | } |
| 685 | } else { |
| 686 | for j in 0 .. i { |
| 687 | if mem::eq(name, symbols[j]) { |
| 688 | throw binary::Error::Invalid; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | try symbols.append(name, a) catch { |
| 693 | throw binary::Error::Storage; |
| 694 | }; |
| 695 | } |
| 696 | set input.symbols = &symbols[..]; |
| 697 | let name = try input.symbol(); |
| 698 | let mut version: ?*[u8] = nil; |
| 699 | if try input.flag() { |
| 700 | let text = try input.bytes(a); |
| 701 | if binary::versionParts(text) == nil { |
| 702 | throw binary::Error::Invalid; |
| 703 | } |
| 704 | set version = text; |
| 705 | } |
| 706 | let metadata = try linkage::readId(input); |
| 707 | let depCount = try input.count(4 + linkage::ID_BYTES); |
| 708 | let mut dependencies: *mut [*[u8]] = &mut []; |
| 709 | let mut pins: *mut [binary::Id] = &mut []; |
| 710 | try dependencies.reserve(depCount, a) catch { |
| 711 | throw binary::Error::Storage; |
| 712 | }; |
| 713 | try pins.reserve(depCount, a) catch { |
| 714 | throw binary::Error::Storage; |
| 715 | }; |
| 716 | for _ in 0 .. depCount { |
| 717 | let dependency = try input.symbol(); |
| 718 | try dependencies.append(dependency, a) catch { |
| 719 | throw binary::Error::Storage; |
| 720 | }; |
| 721 | try pins.append(try linkage::readId(input), a) catch { |
| 722 | throw binary::Error::Storage; |
| 723 | }; |
| 724 | } |
| 725 | let exportCount = try input.count(binary::MIN_EXPORT_BYTES); |
| 726 | let mut exports: *mut [binary::Export] = &mut []; |
| 727 | try exports.reserve(exportCount, a) catch { |
| 728 | throw binary::Error::Storage; |
| 729 | }; |
| 730 | for _ in 0 .. exportCount { |
| 731 | let symbol = try input.symbol(); |
| 732 | let isData = try input.flag(); |
| 733 | let index = try input.word() as u32; |
| 734 | let kind = binary::ExportKind::Data(index) |
| 735 | if isData |
| 736 | else binary::ExportKind::Function(index); |
| 737 | try exports.append( |
| 738 | binary::Export { |
| 739 | name: symbol, |
| 740 | kind, |
| 741 | startable: false, |
| 742 | }, |
| 743 | a |
| 744 | ) catch { |
| 745 | throw binary::Error::Storage; |
| 746 | }; |
| 747 | } |
| 748 | let mut entry: ?*[u8] = nil; |
| 749 | if try input.flag() { |
| 750 | set entry = try input.symbol(); |
| 751 | } |
| 752 | let links = binary::Links { |
| 753 | metadata, |
| 754 | pins: &pins[..], |
| 755 | imports: try linkage::read(input, a), |
| 756 | image: nil, |
| 757 | verified: false, |
| 758 | }; |
| 759 | set input.layouts = try input.count( |
| 760 | binary::MIN_LAYOUT_BYTES |
| 761 | ); |
| 762 | set input.signatures = try input.count( |
| 763 | binary::MIN_SIGNATURE_BYTES |
| 764 | ); |
| 765 | set input.traits = try input.count(binary::MIN_TRAIT_BYTES); |
| 766 | let layouts = try readLayouts(input, a); |
| 767 | let signatures = try readSignatures(input, a); |
| 768 | let traits = try readTraits(input, a); |
| 769 | try linkage::check(&links, depCount, input.layouts, input.signatures); |
| 770 | let data = try readData(input, a, layouts); |
| 771 | return binary::Package { |
| 772 | version, |
| 773 | symbols: input.symbols, |
| 774 | name, |
| 775 | dependencies: &dependencies[..], |
| 776 | exports: &exports[..], |
| 777 | entry, |
| 778 | program: il::Program { data, fns: &[], layouts, signatures, traits }, |
| 779 | links, |
| 780 | }; |
| 781 | } |
| 782 | |
| 783 | /// Read the package tables and leave the cursor at the function count. |
| 784 | /// Function records can then be consumed in bounded temporary storage. |
| 785 | export fn header(input: &mut reader::Reader['wire], a: &heap::Heap) -> binary::Package |
| 786 | throws (binary::Error) |
| 787 | { |
| 788 | return try readHeader(input, a); |
| 789 | } |
| 790 | |
| 791 | /// Read all functions after their shared descriptor tables. |
| 792 | fn readPackage( |
| 793 | input: &mut reader::Reader['wire], a: &heap::Heap, limits: binary::Limits, contents: Contents |
| 794 | ) -> binary::Package throws (binary::Error) { |
| 795 | let mut package = try readHeader(input, a); |
| 796 | set package.program.fns = try readFunctions(input, a, limits, contents); |
| 797 | if not input.atEnd() { |
| 798 | throw binary::Error::Invalid; |
| 799 | } |
| 800 | return package; |
| 801 | } |
| 802 | |
| 803 | /// Decode package fields without computing a content identity. Restore the arena |
| 804 | /// offset on every failure. The caller can hash the same immutable bytes separately. |
| 805 | export fn parse( |
| 806 | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits |
| 807 | ) -> binary::Package throws (binary::Error) { |
| 808 | return try read(bytes, arena, limits, Contents::Bodies); |
| 809 | } |
| 810 | |
| 811 | /// Decode a package and restore the arena offset after failure. |
| 812 | fn read( |
| 813 | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits, contents: Contents |
| 814 | ) -> binary::Package throws (binary::Error) { |
| 815 | let saved = heap::mark(arena); |
| 816 | let mut input = reader::new(bytes); |
| 817 | let package = try readPackage(&mut input, arena, limits, contents) catch err { |
| 818 | // A failed decode returns no pointer into the storage that it allocated, and the caller |
| 819 | // uses the heap only after the call, so nothing uses that range. |
| 820 | unsafe { |
| 821 | heap::rewind(arena, saved); |
| 822 | } |
| 823 | throw err; |
| 824 | }; |
| 825 | return package; |
| 826 | } |
| 827 | |
| 828 | /// Decode and identify one complete immutable package payload. |
| 829 | export fn decode( |
| 830 | bytes: &'wire [u8], arena: &heap::Heap, limits: binary::Limits |
| 831 | ) -> binary::Package throws (binary::Error) { |
| 832 | let mut package = try parse(bytes, arena, limits); |
| 833 | set package.links.image = linkage::imageId(bytes); |
| 834 | return package; |
| 835 | } |
| 836 | |
| 837 | /// Read type tables, data and callable headers from an immutable package payload. |
| 838 | /// The caller must supply verified source. Function extents delimit skipped bodies. |
| 839 | export fn declarations(bytes: &'wire [u8], arena: &heap::Heap) -> binary::Package |
| 840 | throws (binary::Error) |
| 841 | { |
| 842 | return try read( |
| 843 | bytes, |
| 844 | arena, |
| 845 | binary::Limits { |
| 846 | registers: 0xffffffff, blocks: 0 |
| 847 | }, |
| 848 | Contents::Declarations |
| 849 | ); |
| 850 | } |