.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
rv64/
asm/
image/
shared/
tests/
asm.rad
32.4 KiB
atomics.rad
5.1 KiB
decode.rad
27.1 KiB
emit.rad
34.6 KiB
encode.rad
30.9 KiB
image.rad
6.1 KiB
isel.rad
138.3 KiB
printer.rad
19.2 KiB
shared.rad
21.1 KiB
tests.rad
73.3 KiB
vector.rad
59.4 KiB
rv64.rad
22.4 KiB
bits/
char/
collections/
fs/
hash/
lang/
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/arch/rv64/shared.rad
raw
| 1 | //! Shared package code, private data templates, and qualified symbol linking. |
| 2 | |
| 3 | @test export mod tests; |
| 4 | export mod catalog; |
| 5 | |
| 6 | use std::collections::dict; |
| 7 | use std::lang::gen; |
| 8 | use std::lang::gen::data; |
| 9 | use std::lang::gen::labels; |
| 10 | use std::lang::gen::regalloc::liveness; |
| 11 | use std::lang::il; |
| 12 | use std::lang::il::binary; |
| 13 | use std::lang::il::binary::linkage; |
| 14 | use std::lang::il::verify; |
| 15 | use std::mem; |
| 16 | use std::mem::heap; |
| 17 | use super::asm; |
| 18 | use super::emit; |
| 19 | use super::encode; |
| 20 | use super::image; |
| 21 | |
| 22 | /// Largest layout in bytes accepted for an untrusted package. |
| 23 | constant MAX_LAYOUT_SIZE: u32 = 1048576; |
| 24 | /// Maximum nested layout depth accepted for an untrusted package. |
| 25 | constant MAX_LAYOUT_DEPTH: u32 = 16; |
| 26 | /// Maximum parameters accepted for an untrusted package. |
| 27 | constant MAX_PARAMS: u32 = 16; |
| 28 | |
| 29 | /// Maximum number of resident package slots in a domain state table. |
| 30 | export constant MAX_PACKAGES: u32 = 256; |
| 31 | |
| 32 | /// Package linking or instance storage failure. |
| 33 | export union Error: Copy { |
| 34 | /// Reusable backend generation failed. |
| 35 | Codegen(super::Error), |
| 36 | /// A caller-supplied output buffer is full. |
| 37 | Capacity, |
| 38 | /// A symbol is absent, duplicated, or has the wrong kind. |
| 39 | Symbol, |
| 40 | /// An address or data extent is outside the supported range. |
| 41 | Range, |
| 42 | /// A data or code address has invalid alignment. |
| 43 | Alignment, |
| 44 | /// A referenced package has no private instance. |
| 45 | Instance, |
| 46 | } |
| 47 | |
| 48 | /// Address of data within a domain's package graph. |
| 49 | export record DataRef: Copy { |
| 50 | /// Stable global package slot. |
| 51 | slot: u32, |
| 52 | /// Byte offset in the package's private state. |
| 53 | offset: u32, |
| 54 | } |
| 55 | |
| 56 | /// Resolved function or domain-relative data address. |
| 57 | export union Target: Copy { |
| 58 | /// Absolute address of shared executable code. |
| 59 | Function(u64), |
| 60 | /// Private data location selected through gp. |
| 61 | Data(DataRef), |
| 62 | } |
| 63 | |
| 64 | /// Resolved qualified symbol. |
| 65 | export record Symbol: Copy { |
| 66 | /// Fully qualified symbol name. |
| 67 | name: *[u8], |
| 68 | /// Code address or private data location. |
| 69 | target: Target, |
| 70 | /// Whether a fresh context may start at this function. |
| 71 | startable: bool, |
| 72 | } |
| 73 | |
| 74 | /// Private pointer fixup applied when a domain is instantiated. |
| 75 | export record Relocation: Copy { |
| 76 | /// Byte offset of the first pointer in the private template. |
| 77 | offset: u32, |
| 78 | /// Number of consecutive 64-bit pointers. |
| 79 | count: u32, |
| 80 | /// Target data location in the domain graph. |
| 81 | target: DataRef, |
| 82 | } |
| 83 | |
| 84 | /// Caller-owned storage for one package's persistent link results. |
| 85 | export record Storage { |
| 86 | /// Local data layout workspace. |
| 87 | data: *mut [data::DataSym], |
| 88 | /// Local function and data definitions. |
| 89 | symbols: *mut [Symbol], |
| 90 | /// Public symbols for dependent packages. |
| 91 | exports: *mut [Symbol], |
| 92 | /// Private data initializer bytes. |
| 93 | template: *mut [u8], |
| 94 | /// Private pointer fixups. |
| 95 | relocations: *mut [Relocation], |
| 96 | /// Stack growth check of the generated code. |
| 97 | stack: super::StackCheck, |
| 98 | } |
| 99 | |
| 100 | /// Verification policy under which native code was generated. |
| 101 | export union Verification: Copy { |
| 102 | /// Native code without the user-package safety proof and checked stack ABI. |
| 103 | Unverified, |
| 104 | /// Checked RIL with a protected stack and no assembly prefix. |
| 105 | Verified, |
| 106 | } |
| 107 | |
| 108 | /// Persistent source identity and dependency metadata of a native package. |
| 109 | export record Contract: Copy { |
| 110 | /// Dependency metadata hashes in the declared order. |
| 111 | pins: *[binary::Id], |
| 112 | /// Exact source identity, present for decoded binary packages. |
| 113 | image: ?binary::Id, |
| 114 | /// Eligibility for user-package imports and native-cache reuse. |
| 115 | verification: Verification, |
| 116 | /// Compiler metadata hash of the retained package. |
| 117 | metadata: binary::Id, |
| 118 | } |
| 119 | |
| 120 | /// Native metadata without a user-package verification result. |
| 121 | export constant UNVERIFIED: Contract = Contract { |
| 122 | pins: &[], |
| 123 | image: nil, |
| 124 | verification: Verification::Unverified, |
| 125 | metadata: binary::Id { bytes: [0; linkage::ID_BYTES] }, |
| 126 | }; |
| 127 | |
| 128 | /// A resident package entry used by boot catalogs and runtime loading. |
| 129 | export record Package: Copy { |
| 130 | /// Diagnostic label of the package. |
| 131 | name: *[u8], |
| 132 | /// Dependency labels in metadata pin order. |
| 133 | dependencies: *[*[u8]], |
| 134 | /// Stable slot in each domain's package-state table. |
| 135 | slot: u32, |
| 136 | /// Address at which the shared instructions execute. |
| 137 | codeAddress: u64, |
| 138 | /// Generated shared instructions. |
| 139 | code: *[u32], |
| 140 | /// Public function and data definitions. |
| 141 | exports: *[Symbol], |
| 142 | /// Exported default entry, if present. |
| 143 | entry: ?u64, |
| 144 | /// Initialized private bytes. The rest of memory must be zero-filled. |
| 145 | template: *[u8], |
| 146 | /// Total private state extent. |
| 147 | memory: u32, |
| 148 | /// Required private state base alignment. |
| 149 | alignment: u32, |
| 150 | /// Private pointers to resolve after all graph instances have storage. |
| 151 | relocations: *[Relocation], |
| 152 | /// Exact dependencies, source identity and native verification policy. |
| 153 | contract: Contract, |
| 154 | } |
| 155 | |
| 156 | /// Find exactly one qualified symbol in a bounded table. |
| 157 | export fn lookup(symbols: &[Symbol], name: &[u8]) -> ?Target { |
| 158 | for symbol in symbols { |
| 159 | if mem::eq(symbol.name, name) { |
| 160 | return symbol.target; |
| 161 | } |
| 162 | } |
| 163 | return nil; |
| 164 | } |
| 165 | |
| 166 | /// Resolve a local or imported symbol. |
| 167 | fn resolve(local: &[Symbol], imports: &[Symbol], name: *[u8]) -> Target throws (Error) { |
| 168 | if let target = lookup(local, name) { |
| 169 | return target; |
| 170 | } |
| 171 | let target = lookup(imports, name) else { |
| 172 | throw Error::Symbol; |
| 173 | }; |
| 174 | return target; |
| 175 | } |
| 176 | |
| 177 | /// Append a unique local definition to bounded symbol storage. |
| 178 | fn define(symbols: &mut [Symbol], count: &mut u32, name: *[u8], target: Target) throws (Error) { |
| 179 | if lookup(&symbols[.. *count], name) <> nil { |
| 180 | throw Error::Symbol; |
| 181 | } |
| 182 | if *count == symbols.len { |
| 183 | throw Error::Capacity; |
| 184 | } |
| 185 | set symbols[*count] = Symbol { |
| 186 | name, |
| 187 | target, |
| 188 | startable: false, |
| 189 | }; |
| 190 | set *count += 1; |
| 191 | } |
| 192 | |
| 193 | /// Resolve a shared function address with its required kind. |
| 194 | fn function(local: &[Symbol], imports: &[Symbol], name: *[u8]) -> u64 throws (Error) { |
| 195 | match try resolve(local, imports, name) { |
| 196 | case Target::Function(address) => return address, |
| 197 | else => throw Error::Symbol, |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /// Require a private data target. |
| 202 | fn dataRef(target: Target) -> DataRef throws (Error) { |
| 203 | match target { |
| 204 | case Target::Data(location) => return location, |
| 205 | else => throw Error::Symbol, |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /// Patch a PC-relative call or function address load. A `link` register selects a call that |
| 210 | /// links through it; without one the second word adds the low part into `rd`. |
| 211 | fn relative( |
| 212 | e: &mut emit::Emitter, index: u32, base: u64, target: u64, rd: gen::Reg, link: ?gen::Reg |
| 213 | ) |
| 214 | throws (Error) |
| 215 | { |
| 216 | let delta = image::displacement(base + index as u64 * 4, target) else { |
| 217 | throw Error::Range; |
| 218 | }; |
| 219 | let parts = emit::splitImm(delta); |
| 220 | emit::patch(e, index, encode::auipc(rd, parts.hi)); |
| 221 | if let reg = link { |
| 222 | emit::patch(e, index + 1, encode::jalr(reg, rd, parts.lo)); |
| 223 | } else { |
| 224 | emit::patch(e, index + 1, encode::addi(rd, rd, parts.lo)); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /// Link shared code against local definitions and dependency exports. |
| 229 | fn link(e: &mut emit::Emitter, base: u64, local: &[Symbol], imports: &[Symbol]) throws (Error) { |
| 230 | for i in 0 .. e.pendingCalls.len { |
| 231 | let pending = e.pendingCalls[i]; |
| 232 | let address = try function(local, imports, pending.target); |
| 233 | try relative(e, pending.index, base, address, pending.scratch, pending.link); |
| 234 | } |
| 235 | for i in 0 .. e.pendingJumps.len { |
| 236 | let pending = e.pendingJumps[i]; |
| 237 | let address = try function(local, imports, pending.target); |
| 238 | let delta = image::displacement(base + pending.index as u64 * 4, address) else { |
| 239 | throw Error::Range; |
| 240 | }; |
| 241 | if not encode::isJumpImm(delta) { |
| 242 | throw Error::Range; |
| 243 | } |
| 244 | emit::patch(e, pending.index, encode::jal(pending.rd, delta)); |
| 245 | } |
| 246 | for i in 0 .. e.pendingAddrLoads.len { |
| 247 | let pending = e.pendingAddrLoads[i]; |
| 248 | if not pending.isData { |
| 249 | try relative( |
| 250 | e, |
| 251 | pending.index, |
| 252 | base, |
| 253 | try function(local, imports, pending.target), |
| 254 | pending.rd, |
| 255 | nil |
| 256 | ); |
| 257 | continue; |
| 258 | } |
| 259 | let target = try resolve(local, imports, pending.target); |
| 260 | let location = try dataRef(target); |
| 261 | if location.slot >= MAX_PACKAGES |
| 262 | or location.offset > image::DISPLACEMENT_MAX |
| 263 | { |
| 264 | throw Error::Range; |
| 265 | } |
| 266 | if pending.rd == super::ADDR_SCRATCH |
| 267 | or pending.rd == super::GP |
| 268 | or pending.rd == super::ZERO |
| 269 | { |
| 270 | throw Error::Symbol; |
| 271 | } |
| 272 | let parts = emit::splitImm(location.offset as i32); |
| 273 | emit::patch(e, pending.index, encode::ld(pending.rd, super::GP, location.slot as i32 * 8)); |
| 274 | emit::patch(e, pending.index + 1, encode::lui(super::ADDR_SCRATCH, parts.hi)); |
| 275 | emit::patch(e, pending.index + 2, encode::add(pending.rd, pending.rd, super::ADDR_SCRATCH)); |
| 276 | emit::patch(e, pending.index + 3, encode::addi(pending.rd, pending.rd, parts.lo)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /// Write a little-endian integer into a validated byte extent. |
| 281 | fn integer(bytes: &mut [u8], offset: u32, value: u64, width: u32) { |
| 282 | for i in 0 .. width { |
| 283 | set bytes[offset + i] = (value >> (i as u64 * 8)) as u8; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /// Initialized output extents. |
| 288 | record TemplateSize: Copy { |
| 289 | /// Initialized byte count. |
| 290 | bytes: u32, |
| 291 | /// Private pointer fixup count. |
| 292 | relocations: u32, |
| 293 | } |
| 294 | |
| 295 | /// Build initialized bytes and compact private pointer fixups. |
| 296 | fn template( |
| 297 | items: *[data::Data], |
| 298 | local: &[Symbol], |
| 299 | imports: &[Symbol], |
| 300 | bytes: &mut [u8], |
| 301 | relocs: &mut [Relocation] |
| 302 | ) -> TemplateSize throws (Error) { |
| 303 | let mut initialized: u32 = 0; |
| 304 | let mut count: u32 = 0; |
| 305 | for item in items { |
| 306 | if item.isZeroInit { |
| 307 | continue; |
| 308 | } |
| 309 | let location = try dataRef(try resolve(local, &[], item.name)); |
| 310 | if location.offset > bytes.len or item.size > bytes.len - location.offset { |
| 311 | throw Error::Capacity; |
| 312 | } |
| 313 | let end = location.offset + item.size; |
| 314 | if end > initialized { |
| 315 | set initialized = end; |
| 316 | } |
| 317 | } |
| 318 | mem::fill(&mut bytes[.. initialized], 0); |
| 319 | for item in items { |
| 320 | if item.isZeroInit { |
| 321 | continue; |
| 322 | } |
| 323 | let location = try dataRef(try resolve(local, &[], item.name)); |
| 324 | let mut offset = location.offset; |
| 325 | let end = offset + item.size; |
| 326 | for value in item.values { |
| 327 | let mut width: u32 = 1; |
| 328 | let mut number: u64 = 0; |
| 329 | match value.item { |
| 330 | case il::DataItem::Val { typ, val } => { |
| 331 | set width = il::typeSize(typ); |
| 332 | set number = val as u64; |
| 333 | } |
| 334 | case il::DataItem::Fn(name) => { |
| 335 | set width = 8; |
| 336 | set number = try function(local, imports, name); |
| 337 | } |
| 338 | case il::DataItem::Sym(name) => { |
| 339 | set width = 8; |
| 340 | let target = try dataRef(try resolve(local, imports, name)); |
| 341 | if value.count > 0 { |
| 342 | if count == relocs.len { |
| 343 | throw Error::Capacity; |
| 344 | } |
| 345 | set relocs[count] = Relocation { offset, count: value.count, target }; |
| 346 | set count += 1; |
| 347 | } |
| 348 | } |
| 349 | case il::DataItem::Str(s) => { |
| 350 | set width = s.len; |
| 351 | } |
| 352 | case il::DataItem::Undef => {} |
| 353 | } |
| 354 | if width == 0 { |
| 355 | continue; |
| 356 | } |
| 357 | if value.count > (end - offset) / width { |
| 358 | throw Error::Range; |
| 359 | } |
| 360 | let length = value.count * width; |
| 361 | match value.item { |
| 362 | case il::DataItem::Str(s) => { |
| 363 | mem::fillPattern(&mut bytes[offset .. offset + length], s); |
| 364 | } |
| 365 | else => { |
| 366 | if value.count == 1 { |
| 367 | integer(bytes, offset, number, width); |
| 368 | set offset += width; |
| 369 | continue; |
| 370 | } |
| 371 | let mut pattern: [u8; 8] = undefined; |
| 372 | integer(&mut pattern[..], 0, number, width); |
| 373 | mem::fillPattern( |
| 374 | &mut bytes[offset .. offset + length], &pattern[.. width] |
| 375 | ); |
| 376 | } |
| 377 | } |
| 378 | set offset += length; |
| 379 | } |
| 380 | } |
| 381 | return TemplateSize { bytes: initialized, relocations: count }; |
| 382 | } |
| 383 | |
| 384 | /// Allocate the block and function label tables of a generator in `scratch`. |
| 385 | fn labelTables(scratch: &heap::Heap) -> labels::Labels throws (Error) { |
| 386 | let mut blockOffsets: *mut [i32] = &mut []; |
| 387 | try blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, scratch) catch { |
| 388 | throw Error::Capacity; |
| 389 | }; |
| 390 | let mut funcEntries: *mut [dict::Entry] = &mut []; |
| 391 | try funcEntries.resize( |
| 392 | labels::FUNC_TABLE_SIZE, dict::Entry { key: &[], value: 0 }, scratch |
| 393 | ) catch { |
| 394 | throw Error::Capacity; |
| 395 | }; |
| 396 | return labels::fresh(blockOffsets, funcEntries); |
| 397 | } |
| 398 | |
| 399 | /// Verifier configuration for packages that this backend compiles for untrusted use: its |
| 400 | /// register, block and layout capacities, with an Env record of `envSize` bytes. |
| 401 | export fn verifierConfig(envSize: u32) -> verify::Config { |
| 402 | return verify::Config { |
| 403 | registers: liveness::MAX_SSA_REGS, |
| 404 | blocks: labels::MAX_BLOCKS_PER_FN, |
| 405 | layoutSize: MAX_LAYOUT_SIZE, |
| 406 | layoutDepth: MAX_LAYOUT_DEPTH, |
| 407 | params: MAX_PARAMS, |
| 408 | envSize, |
| 409 | }; |
| 410 | } |
| 411 | |
| 412 | /// Compile one package into caller-owned code, symbol, and private template storage. |
| 413 | /// Imports must contain unique exports from the package's admitted dependencies. |
| 414 | /// The generator owns `arena` and places the code in it. `scratch` holds the label |
| 415 | /// tables and per-function workspace, and is free for reuse after the call. |
| 416 | /// Arena storage and binary package names must outlive the returned catalog entry. |
| 417 | export fn compile( |
| 418 | input: &binary::Package, |
| 419 | slot: u32, |
| 420 | codeAddress: u64, |
| 421 | imports: &[Symbol], |
| 422 | storage: Storage, |
| 423 | arena: *heap::Heap, |
| 424 | scratch: &heap::Heap |
| 425 | ) -> Package throws (Error) { |
| 426 | let none = asm::Program { text: &[], data: &[], symbols: &[], externalFixups: &[] }; |
| 427 | return try compileAssembly(input, &none, slot, codeAddress, imports, storage, arena, scratch); |
| 428 | } |
| 429 | |
| 430 | /// Compile a package with a text-only assembly prefix and its exported boundaries. |
| 431 | /// Assembly and RIL definitions share one native code extent and package-state slot. |
| 432 | export fn compileAssembly( |
| 433 | input: &binary::Package, |
| 434 | assembly: &asm::Program, |
| 435 | slot: u32, |
| 436 | codeAddress: u64, |
| 437 | imports: &[Symbol], |
| 438 | storage: Storage, |
| 439 | arena: *heap::Heap, |
| 440 | scratch: &heap::Heap |
| 441 | ) -> Package throws (Error) { |
| 442 | let case Storage { |
| 443 | data: dataStorage, |
| 444 | symbols: symbolStorage, |
| 445 | exports: exportStorage, |
| 446 | template: templateStorage, |
| 447 | relocations: relocationStorage, |
| 448 | stack, |
| 449 | } = |
| 450 | storage else panic "compileAssembly: expected shared linker storage"; |
| 451 | if assembly.data.len <> 0 { |
| 452 | throw Error::Range; |
| 453 | } |
| 454 | if slot >= MAX_PACKAGES { |
| 455 | throw Error::Range; |
| 456 | } |
| 457 | if (codeAddress & 3) <> 0 { |
| 458 | throw Error::Alignment; |
| 459 | } |
| 460 | for imported, i in imports { |
| 461 | if lookup(&imports[.. i], imported.name) <> nil { |
| 462 | throw Error::Symbol; |
| 463 | } |
| 464 | } |
| 465 | let tables = try labelTables(scratch); |
| 466 | let mut generator = try super::beginProgram( |
| 467 | super::ProgramOptions { |
| 468 | stack, |
| 469 | entryPatch: super::EntryPatch::None, |
| 470 | debug: false, |
| 471 | placement: image::Placement::Hosted, |
| 472 | }, |
| 473 | arena, |
| 474 | tables |
| 475 | ) catch err { |
| 476 | throw Error::Codegen(err); |
| 477 | }; |
| 478 | set generator.e.sharedData = true; |
| 479 | try super::addAssembly(&mut generator, *assembly) catch err { |
| 480 | throw Error::Codegen(err); |
| 481 | }; |
| 482 | for func in input.program.fns { |
| 483 | try super::generateFunction( |
| 484 | &mut generator, func, input.program.layouts, scratch |
| 485 | ) catch err { |
| 486 | throw Error::Codegen(err); |
| 487 | }; |
| 488 | } |
| 489 | if codeAddress > mem::U64_MAX - generator.e.code.len as u64 * 4 { |
| 490 | throw Error::Range; |
| 491 | } |
| 492 | let mut symbols: u32 = 0; |
| 493 | for i in 0 .. generator.e.funcs.len { |
| 494 | let func = generator.e.funcs[i]; |
| 495 | try define(&mut symbolStorage[..], &mut symbols, func.name, Target::Function( |
| 496 | codeAddress + func.index as u64 * 4 |
| 497 | )); |
| 498 | } |
| 499 | let items = try data::prepare(input.program.data, generator.e.arena) catch { |
| 500 | throw Error::Range; |
| 501 | }; |
| 502 | let mut dataCount: u32 = 0; |
| 503 | let roSize = try data::layoutSection( |
| 504 | items, &mut dataStorage[..], &mut dataCount, 0, true |
| 505 | ) catch { |
| 506 | throw Error::Range; |
| 507 | }; |
| 508 | let memory = try data::layoutSectionAtOffset( |
| 509 | items, &mut dataStorage[..], &mut dataCount, 0, roSize, false |
| 510 | ) catch { |
| 511 | throw Error::Range; |
| 512 | }; |
| 513 | if memory > image::DISPLACEMENT_MAX { |
| 514 | throw Error::Range; |
| 515 | } |
| 516 | for item in &dataStorage[.. dataCount] { |
| 517 | try define(&mut symbolStorage[..], &mut symbols, item.name, Target::Data(DataRef { |
| 518 | slot, offset: item.addr as u32 |
| 519 | })); |
| 520 | } |
| 521 | let local = &symbolStorage[.. symbols]; |
| 522 | for symbol in local { |
| 523 | if lookup(imports, symbol.name) <> nil { |
| 524 | throw Error::Symbol; |
| 525 | } |
| 526 | } |
| 527 | try link(&mut generator.e, codeAddress, local, imports); |
| 528 | let size = try template( |
| 529 | items, local, imports, &mut templateStorage[..], &mut relocationStorage[..] |
| 530 | ); |
| 531 | if input.exports.len > exportStorage.len { |
| 532 | throw Error::Capacity; |
| 533 | } |
| 534 | for exported, i in input.exports { |
| 535 | let target = try resolve(local, &[], exported.name); |
| 536 | match exported.kind { |
| 537 | case binary::ExportKind::Function(_) => { |
| 538 | try function(local, &[], exported.name); |
| 539 | } |
| 540 | case binary::ExportKind::Data(_) => { |
| 541 | try dataRef(target); |
| 542 | } |
| 543 | } |
| 544 | if lookup(&exportStorage[.. i], exported.name) <> nil { |
| 545 | throw Error::Symbol; |
| 546 | } |
| 547 | set exportStorage[i] = Symbol { |
| 548 | name: exported.name, |
| 549 | target, |
| 550 | startable: exported.startable, |
| 551 | }; |
| 552 | } |
| 553 | let mut entry: ?u64 = nil; |
| 554 | if let name = input.entry { |
| 555 | set entry = try function(&exportStorage[.. input.exports.len], &[], name); |
| 556 | } |
| 557 | let mut alignment: u32 = 8; |
| 558 | for item in items { |
| 559 | if item.alignment > alignment { |
| 560 | set alignment = item.alignment; |
| 561 | } |
| 562 | } |
| 563 | let code = generator.e.code.take(); |
| 564 | return Package { |
| 565 | name: input.name, |
| 566 | dependencies: input.dependencies, |
| 567 | slot, |
| 568 | codeAddress, |
| 569 | code: &code[..], |
| 570 | exports: &exportStorage[.. input.exports.len], |
| 571 | entry, |
| 572 | template: &templateStorage[.. size.bytes], |
| 573 | memory, |
| 574 | alignment, |
| 575 | relocations: &relocationStorage[.. size.relocations], |
| 576 | contract: Contract { |
| 577 | pins: input.links.pins, |
| 578 | image: input.links.image, |
| 579 | verification: Verification::Verified |
| 580 | if input.links.verified |
| 581 | and stack == super::StackCheck::TpLimit |
| 582 | and assembly.text.len == 0 |
| 583 | else Verification::Unverified, |
| 584 | metadata: input.links.metadata, |
| 585 | }, |
| 586 | }; |
| 587 | } |
| 588 | |
| 589 | /// Validate a package's private-state bases and fixups without touching memory. |
| 590 | /// Every base must be assigned and aligned, and every fixup must stay in range. |
| 591 | export fn checkInstance(package: &Package, bases: &[u64]) throws (Error) { |
| 592 | if package.slot >= bases.len or bases[package.slot] == 0 { |
| 593 | throw Error::Instance; |
| 594 | } |
| 595 | if package.template.len > package.memory { |
| 596 | throw Error::Capacity; |
| 597 | } |
| 598 | if (bases[package.slot] & (package.alignment as u64 - 1)) <> 0 { |
| 599 | throw Error::Alignment; |
| 600 | } |
| 601 | if bases[package.slot] > mem::U64_MAX - package.memory as u64 { |
| 602 | throw Error::Range; |
| 603 | } |
| 604 | for fixup in package.relocations { |
| 605 | if fixup.target.slot >= bases.len or bases[fixup.target.slot] == 0 { |
| 606 | throw Error::Instance; |
| 607 | } |
| 608 | if bases[fixup.target.slot] > mem::U64_MAX - fixup.target.offset as u64 { |
| 609 | throw Error::Range; |
| 610 | } |
| 611 | if fixup.offset > package.memory or fixup.count > (package.memory - fixup.offset) / 8 { |
| 612 | throw Error::Range; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /// Initialize private package memory after all graph bases have been assigned. |
| 618 | /// Validate every fixup before writing bytes so a rejected instance stays intact. |
| 619 | export fn instantiate(package: &Package, bases: &[u64], memory: &mut [u8]) throws (Error) { |
| 620 | if memory.len < package.memory { |
| 621 | throw Error::Capacity; |
| 622 | } |
| 623 | try checkInstance(package, bases); |
| 624 | for i in 0 .. package.memory { |
| 625 | set memory[i] = 0; |
| 626 | } |
| 627 | try! mem::copy(memory, package.template); |
| 628 | for fixup in package.relocations { |
| 629 | let address = bases[fixup.target.slot] + fixup.target.offset as u64; |
| 630 | for i in 0 .. fixup.count { |
| 631 | integer(memory, fixup.offset + i * 8, address, 8); |
| 632 | } |
| 633 | } |
| 634 | } |