rv64: Emit shared native images

28a0943a366d0c860ec6026a50beb1c1d84541e8896e393299a4544238c3f9f5
Packages could only be linked into one program, so two programs could
not run the same compiled package with private state.

Add `std::arch::rv64::shared`, which compiles a decoded and verified
binary RIL package into position-independent code with a private data
template, and links packages through their exports and imports. Data
address loads add the package-state offset of the running domain.
Add the package catalog, the linker and shared-image tests, and
`make shared-test`, which builds two packages once and runs them with
two private state graphs, including its error cases.

Assisted-by: Codex:gpt-6
Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent fb5beff8
lib/std/arch/rv64.rad +1 -0
16 16
export mod decode;
17 17
export mod emit;
18 18
export mod isel;
19 19
export mod printer;
20 20
export mod asm;
21 +
export mod shared;
21 22
22 23
@test mod tests;
23 24
24 25
use std::mem;
25 26
use std::collections::dict;
lib/std/arch/rv64/emit.rad +7 -0
105 105
/// Emission context. Tracks state during code generation.
106 106
export record Emitter {
107 107
    /// Storage of the append-backed emitter lists. It is the only source of
108 108
    /// their growth, so the emitter limits its own memory use.
109 109
    arena: alloc::Arena,
110 +
    /// Data address loads use the current domain package-state table.
111 +
    sharedData: bool,
110 112
    /// Frames and outgoing argument areas are checked against the stack limit in `tp`.
111 113
    stackCheck: bool,
112 114
    /// Emitted instructions storage.
113 115
    code: *mut [u32],
114 116
    /// Local branches needing offset patching.
213 215
    if debug {
214 216
        try debugEntries.reserve(MAX_DEBUG_ENTRIES, &mut arena);
215 217
    }
216 218
    return Emitter {
217 219
        arena,
220 +
        sharedData: false,
218 221
        stackCheck: false,
219 222
        code,
220 223
        pendingBranches,
221 224
        pendingCalls,
222 225
        pendingJumps,
385 388
    }, &mut e.arena) catch {
386 389
        throw super::Error::Allocation;
387 390
    };
388 391
    try emit(e, encode::nop()); // Address-load upper instruction.
389 392
    try emit(e, encode::nop()); // Address-load lower instruction.
393 +
    if e.sharedData {
394 +
        try emit(e, encode::nop()); // Package offset addition.
395 +
        try emit(e, encode::nop()); // Package offset low bits.
396 +
    }
390 397
}
391 398
392 399
/// Patch local branches and clear the pending list.
393 400
///
394 401
/// Called after each function.
lib/std/arch/rv64/shared.rad added +523 -0
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::mem;
7 +
use std::lang::alloc;
8 +
use std::lang::il;
9 +
use std::lang::il::binary;
10 +
use std::lang::il::verify;
11 +
use std::lang::gen::regalloc::liveness;
12 +
use std::lang::gen;
13 +
use std::lang::gen::data;
14 +
use std::lang::gen::labels;
15 +
use std::collections::dict;
16 +
use super::emit;
17 +
use super::encode;
18 +
use super::image;
19 +
use super::asm;
20 +
21 +
/// Largest layout in bytes accepted for an untrusted package.
22 +
constant MAX_LAYOUT_SIZE: u32 = 1048576;
23 +
/// Maximum nested layout depth accepted for an untrusted package.
24 +
constant MAX_LAYOUT_DEPTH: u32 = 16;
25 +
/// Maximum parameters accepted for an untrusted package.
26 +
constant MAX_PARAMS: u32 = 16;
27 +
28 +
/// Maximum number of resident package slots in a domain state table.
29 +
export constant MAX_PACKAGES: u32 = 256;
30 +
31 +
/// Package linking or instance storage failure.
32 +
export union Error: Copy {
33 +
    /// Reusable backend generation failed.
34 +
    Codegen(super::Error),
35 +
    /// A caller-supplied output buffer is full.
36 +
    Capacity,
37 +
    /// A symbol is absent, duplicated, or has the wrong kind.
38 +
    Symbol,
39 +
    /// An address or data extent is outside the supported range.
40 +
    Range,
41 +
    /// A data or code address has invalid alignment.
42 +
    Alignment,
43 +
    /// A referenced package has no private instance.
44 +
    Instance,
45 +
}
46 +
47 +
/// Address of data within a domain's package graph.
48 +
export record DataRef: Copy {
49 +
    /// Stable global package slot.
50 +
    slot: u32,
51 +
    /// Byte offset in the package's private state.
52 +
    offset: u32,
53 +
}
54 +
55 +
/// Resolved function or domain-relative data address.
56 +
export union Target: Copy {
57 +
    /// Absolute address of shared executable code.
58 +
    Function(u64),
59 +
    /// Private data location selected through gp.
60 +
    Data(DataRef),
61 +
}
62 +
63 +
/// Resolved qualified symbol.
64 +
export record Symbol: Copy {
65 +
    /// Fully qualified symbol name.
66 +
    name: *[u8],
67 +
    /// Code address or private data location.
68 +
    target: Target,
69 +
}
70 +
71 +
/// Private pointer fixup applied when a domain is instantiated.
72 +
export record Relocation: Copy {
73 +
    /// Byte offset of the first pointer in the private template.
74 +
    offset: u32,
75 +
    /// Number of consecutive 64-bit pointers.
76 +
    count: u32,
77 +
    /// Target data location in the domain graph.
78 +
    target: DataRef,
79 +
}
80 +
81 +
/// Caller-owned storage for one package's persistent link results.
82 +
export record Storage {
83 +
    /// Local data layout workspace.
84 +
    data: *mut [data::DataSym],
85 +
    /// Local function and data definitions.
86 +
    symbols: *mut [Symbol],
87 +
    /// Public symbols for dependent packages.
88 +
    exports: *mut [Symbol],
89 +
    /// Private data initializer bytes.
90 +
    template: *mut [u8],
91 +
    /// Private pointer fixups.
92 +
    relocations: *mut [Relocation],
93 +
    /// Stack growth check of the generated code.
94 +
    stack: super::StackCheck,
95 +
}
96 +
97 +
/// A resident package entry used by boot catalogs and runtime loading.
98 +
export record Package: Copy {
99 +
    /// Immutable package identity.
100 +
    name: *[u8],
101 +
    /// Required package names, in binary package order.
102 +
    dependencies: *[*[u8]],
103 +
    /// Stable slot in each domain's package-state table.
104 +
    slot: u32,
105 +
    /// Address at which the shared instructions execute.
106 +
    codeAddress: u64,
107 +
    /// Generated shared instructions.
108 +
    code: *[u32],
109 +
    /// Public function and data definitions.
110 +
    exports: *[Symbol],
111 +
    /// Exported default entry, if present.
112 +
    entry: ?u64,
113 +
    /// Initialized private bytes. The rest of memory must be zero-filled.
114 +
    template: *[u8],
115 +
    /// Total private state extent.
116 +
    memory: u32,
117 +
    /// Required private state base alignment.
118 +
    alignment: u32,
119 +
    /// Private pointers to resolve after all graph instances have storage.
120 +
    relocations: *[Relocation],
121 +
}
122 +
123 +
/// Find exactly one qualified symbol in a bounded table.
124 +
export fn lookup(symbols: &[Symbol], name: &[u8]) -> ?Target {
125 +
    for symbol in symbols {
126 +
        if mem::eq(symbol.name, name) {
127 +
            return symbol.target;
128 +
        }
129 +
    }
130 +
    return nil;
131 +
}
132 +
133 +
/// Resolve a local or imported symbol.
134 +
fn resolve(local: &[Symbol], imports: &[Symbol], name: *[u8]) -> Target throws (Error) {
135 +
    if let target = lookup(local, name) {
136 +
        return target;
137 +
    }
138 +
    let target = lookup(imports, name) else {
139 +
        throw Error::Symbol;
140 +
    };
141 +
    return target;
142 +
}
143 +
144 +
/// Append a unique local definition to bounded symbol storage.
145 +
fn define(symbols: &mut [Symbol], count: &mut u32, name: *[u8], target: Target) throws (Error) {
146 +
    if lookup(&symbols[..*count], name) <> nil {
147 +
        throw Error::Symbol;
148 +
    }
149 +
    if *count == symbols.len {
150 +
        throw Error::Capacity;
151 +
    }
152 +
    set symbols[*count] = Symbol { name, target };
153 +
    set *count += 1;
154 +
}
155 +
156 +
/// Resolve a shared function address with its required kind.
157 +
fn function(local: &[Symbol], imports: &[Symbol], name: *[u8]) -> u64 throws (Error) {
158 +
    match try resolve(local, imports, name) {
159 +
        case Target::Function(address) => return address,
160 +
        else => throw Error::Symbol,
161 +
    }
162 +
}
163 +
164 +
/// Require a private data target.
165 +
fn dataRef(target: Target) -> DataRef throws (Error) {
166 +
    match target {
167 +
        case Target::Data(location) => return location,
168 +
        else => throw Error::Symbol,
169 +
    }
170 +
}
171 +
172 +
/// Patch a PC-relative call or function address load. A `link` register selects a call that
173 +
/// links through it; without one the second word adds the low part into `rd`.
174 +
fn relative(e: &mut emit::Emitter, index: u32, base: u64, target: u64, rd: gen::Reg,
175 +
    link: ?gen::Reg)
176 +
    throws (Error)
177 +
{
178 +
    let delta = image::displacement(base + index as u64 * 4, target) else {
179 +
        throw Error::Range;
180 +
    };
181 +
    let parts = emit::splitImm(delta);
182 +
    emit::patch(e, index, encode::auipc(rd, parts.hi));
183 +
    if let reg = link {
184 +
        emit::patch(e, index + 1, encode::jalr(reg, rd, parts.lo));
185 +
    } else {
186 +
        emit::patch(e, index + 1, encode::addi(rd, rd, parts.lo));
187 +
    }
188 +
}
189 +
190 +
/// Link shared code against local definitions and dependency exports.
191 +
fn link(e: &mut emit::Emitter, base: u64, local: &[Symbol], imports: &[Symbol]) throws (Error) {
192 +
    for i in 0..e.pendingCalls.len {
193 +
        let pending = e.pendingCalls[i];
194 +
        let address = try function(local, imports, pending.target);
195 +
        try relative(e, pending.index, base, address, pending.scratch, pending.link);
196 +
    }
197 +
    for i in 0..e.pendingJumps.len {
198 +
        let pending = e.pendingJumps[i];
199 +
        let address = try function(local, imports, pending.target);
200 +
        let delta = image::displacement(base + pending.index as u64 * 4, address) else {
201 +
            throw Error::Range;
202 +
        };
203 +
        if not encode::isJumpImm(delta) {
204 +
            throw Error::Range;
205 +
        }
206 +
        emit::patch(e, pending.index, encode::jal(pending.rd, delta));
207 +
    }
208 +
    for i in 0..e.pendingAddrLoads.len {
209 +
        let pending = e.pendingAddrLoads[i];
210 +
        if not pending.isData {
211 +
            try relative(e, pending.index, base, try function(local, imports, pending.target),
212 +
                pending.rd, nil);
213 +
            continue;
214 +
        }
215 +
        let target = try resolve(local, imports, pending.target);
216 +
        let location = try dataRef(target);
217 +
        if location.slot >= MAX_PACKAGES or
218 +
            location.offset > image::DISPLACEMENT_MAX {
219 +
            throw Error::Range;
220 +
        }
221 +
        if pending.rd == super::ADDR_SCRATCH or pending.rd == super::GP
222 +
            or pending.rd == super::ZERO {
223 +
            throw Error::Symbol;
224 +
        }
225 +
        let parts = emit::splitImm(location.offset as i32);
226 +
        emit::patch(e, pending.index, encode::ld(pending.rd, super::GP, location.slot as i32 * 8));
227 +
        emit::patch(e, pending.index + 1, encode::lui(super::ADDR_SCRATCH, parts.hi));
228 +
        emit::patch(e, pending.index + 2, encode::add(pending.rd, pending.rd, super::ADDR_SCRATCH));
229 +
        emit::patch(e, pending.index + 3, encode::addi(pending.rd, pending.rd, parts.lo));
230 +
    }
231 +
}
232 +
233 +
/// Write a little-endian integer into a validated byte extent.
234 +
fn integer(bytes: &mut [u8], offset: u32, value: u64, width: u32) {
235 +
    for i in 0..width {
236 +
        set bytes[offset + i] = (value >> (i as u64 * 8)) as u8;
237 +
    }
238 +
}
239 +
240 +
/// Initialized output extents.
241 +
record TemplateSize: Copy {
242 +
    /// Initialized byte count.
243 +
    bytes: u32,
244 +
    /// Private pointer fixup count.
245 +
    relocations: u32,
246 +
}
247 +
248 +
/// Build initialized bytes and compact private pointer fixups.
249 +
fn template(items: *[il::Data], local: &[Symbol], imports: &[Symbol], bytes: &mut [u8],
250 +
    relocs: &mut [Relocation])
251 +
    -> TemplateSize throws (Error)
252 +
{
253 +
    let mut initialized: u32 = 0;
254 +
    let mut count: u32 = 0;
255 +
    for item in items {
256 +
        if item.isZeroInit {
257 +
            continue;
258 +
        }
259 +
        let location = try dataRef(try resolve(local, &[], item.name));
260 +
        if location.offset > bytes.len or item.size > bytes.len - location.offset {
261 +
            throw Error::Capacity;
262 +
        }
263 +
        let end = location.offset + item.size;
264 +
        if end > initialized {
265 +
            set initialized = end;
266 +
        }
267 +
    }
268 +
    for i in 0..initialized {
269 +
        set bytes[i] = 0;
270 +
    }
271 +
    for item in items {
272 +
        if item.isZeroInit {
273 +
            continue;
274 +
        }
275 +
        let location = try dataRef(try resolve(local, &[], item.name));
276 +
        let mut offset = location.offset;
277 +
        let end = offset + item.size;
278 +
        for value in item.values {
279 +
            let mut width: u32 = 1;
280 +
            let mut number: u64 = 0;
281 +
            match value.item {
282 +
                case il::DataItem::Val { typ, val } => {
283 +
                    set width = il::typeSize(typ);
284 +
                    set number = val as u64;
285 +
                },
286 +
                case il::DataItem::Fn(name) => {
287 +
                    set width = 8;
288 +
                    set number = try function(local, imports, name);
289 +
                },
290 +
                case il::DataItem::Sym(name) => {
291 +
                    set width = 8;
292 +
                    let target = try dataRef(try resolve(local, imports, name));
293 +
                    if value.count > 0 {
294 +
                        if count == relocs.len {
295 +
                            throw Error::Capacity;
296 +
                        }
297 +
                        set relocs[count] = Relocation { offset, count: value.count, target };
298 +
                        set count += 1;
299 +
                    }
300 +
                },
301 +
                case il::DataItem::Str(s) => {
302 +
                    set width = s.len;
303 +
                },
304 +
                case il::DataItem::Undef => {},
305 +
            }
306 +
            if width == 0 {
307 +
                continue;
308 +
            }
309 +
            if value.count > (end - offset) / width {
310 +
                throw Error::Range;
311 +
            }
312 +
            for _ in 0..value.count {
313 +
                match value.item {
314 +
                    case il::DataItem::Str(s) => {
315 +
                        try! mem::copy(&mut bytes[offset..offset + width], s);
316 +
                    },
317 +
                    else => {
318 +
                        integer(bytes, offset, number, width);
319 +
                    },
320 +
                }
321 +
                set offset += width;
322 +
            }
323 +
        }
324 +
    }
325 +
    return TemplateSize { bytes: initialized, relocations: count };
326 +
}
327 +
328 +
/// Allocate the block and function label tables of a generator in `scratch`.
329 +
fn labelTables(scratch: &mut alloc::Arena) -> labels::Labels throws (Error) {
330 +
    let mut blockOffsets: *mut [i32] = &mut [];
331 +
    try blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, scratch) catch {
332 +
        throw Error::Capacity;
333 +
    };
334 +
    let mut funcEntries: *mut [dict::Entry] = &mut [];
335 +
    try funcEntries.resize(labels::FUNC_TABLE_SIZE, dict::Entry { key: &[], value: 0 },
336 +
        scratch) catch {
337 +
        throw Error::Capacity;
338 +
    };
339 +
    return labels::fresh(blockOffsets, funcEntries);
340 +
}
341 +
342 +
/// Verifier configuration for packages that this backend compiles for untrusted use: its
343 +
/// register, block and layout capacities, with an Env record of `envSize` bytes.
344 +
export fn verifierConfig(envSize: u32) -> verify::Config {
345 +
    return verify::Config {
346 +
        registers: liveness::MAX_SSA_REGS,
347 +
        blocks: labels::MAX_BLOCKS_PER_FN,
348 +
        layoutSize: MAX_LAYOUT_SIZE,
349 +
        layoutDepth: MAX_LAYOUT_DEPTH,
350 +
        params: MAX_PARAMS,
351 +
        envSize,
352 +
    };
353 +
}
354 +
355 +
/// Compile one package into caller-owned code, symbol, and private template storage.
356 +
/// Imports must contain unique exports from the package's admitted dependencies.
357 +
/// The generator owns `arena` and places the code in it. `scratch` holds the label
358 +
/// tables and per-function workspace, and is free for reuse after the call.
359 +
/// Arena storage and binary package names must outlive the returned catalog entry.
360 +
export fn compile(input: &binary::Package, slot: u32, codeAddress: u64, imports: &[Symbol],
361 +
    storage: Storage, arena: alloc::Arena, scratch: &mut alloc::Arena) -> Package throws (Error)
362 +
{
363 +
    let none = asm::Program { text: &[], data: &[], symbols: &[], externalFixups: &[] };
364 +
    return try compileAssembly(input, &none, slot, codeAddress, imports, storage, arena, scratch);
365 +
}
366 +
367 +
/// Compile a package with a text-only assembly prefix and its exported boundaries.
368 +
/// Assembly and RIL definitions share one native code extent and package-state slot.
369 +
export fn compileAssembly(input: &binary::Package, assembly: &asm::Program, slot: u32,
370 +
    codeAddress: u64,
371 +
    imports: &[Symbol], storage: Storage, arena: alloc::Arena, scratch: &mut alloc::Arena)
372 +
        -> Package throws (Error)
373 +
{
374 +
    let case Storage { data: dataStorage, symbols: symbolStorage, exports: exportStorage,
375 +
        template: templateStorage, relocations: relocationStorage, stack } = storage
376 +
        else panic "compileAssembly: expected shared linker storage";
377 +
    if assembly.data.len <> 0 {
378 +
        throw Error::Range;
379 +
    }
380 +
    if slot >= MAX_PACKAGES {
381 +
        throw Error::Range;
382 +
    }
383 +
    if (codeAddress & 3) <> 0 {
384 +
        throw Error::Alignment;
385 +
    }
386 +
    for imported, i in imports {
387 +
        if lookup(&imports[..i], imported.name) <> nil {
388 +
            throw Error::Symbol;
389 +
        }
390 +
    }
391 +
    let tables = try labelTables(scratch);
392 +
    let mut generator = try super::beginProgram(super::ProgramOptions {
393 +
        stack, entryPatch: super::EntryPatch::None, debug: false,
394 +
        placement: image::Placement::Hosted,
395 +
    }, arena, tables) catch err {
396 +
        throw Error::Codegen(err);
397 +
    };
398 +
    set generator.e.sharedData = true;
399 +
    try super::addAssembly(&mut generator, *assembly) catch err {
400 +
        throw Error::Codegen(err);
401 +
    };
402 +
    for func in input.program.fns {
403 +
        try super::generateFunction(&mut generator, func, input.program.layouts, scratch)
404 +
            catch err {
405 +
                throw Error::Codegen(err);
406 +
            };
407 +
    }
408 +
    if codeAddress > mem::U64_MAX - generator.e.code.len as u64 * 4 {
409 +
        throw Error::Range;
410 +
    }
411 +
    let mut symbols: u32 = 0;
412 +
    for i in 0..generator.e.funcs.len {
413 +
        let func = generator.e.funcs[i];
414 +
        try define(&mut symbolStorage[..], &mut symbols, func.name,
415 +
            Target::Function(codeAddress + func.index as u64 * 4));
416 +
    }
417 +
    let mut dataCount: u32 = 0;
418 +
    let roSize = try data::layoutSection(input.program.data, &mut dataStorage[..], &mut dataCount,
419 +
        0, true)
420 +
        catch {
421 +
            throw Error::Range;
422 +
        };
423 +
    let memory = try data::layoutSectionAtOffset(input.program.data, &mut dataStorage[..],
424 +
        &mut dataCount, 0, roSize, false)
425 +
        catch {
426 +
            throw Error::Range;
427 +
        };
428 +
    if memory > image::DISPLACEMENT_MAX {
429 +
        throw Error::Range;
430 +
    }
431 +
    for item in &dataStorage[..dataCount] {
432 +
        try define(&mut symbolStorage[..], &mut symbols, item.name, Target::Data(DataRef { slot,
433 +
            offset: item.addr as u32 }));
434 +
    }
435 +
    let local = &symbolStorage[..symbols];
436 +
    for symbol in local { if lookup(imports, symbol.name) <> nil {
437 +
        throw Error::Symbol;
438 +
    } }
439 +
    try link(&mut generator.e, codeAddress, local, imports);
440 +
    let size = try template(input.program.data, local, imports, &mut templateStorage[..],
441 +
        &mut relocationStorage[..]);
442 +
    if input.exports.len > exportStorage.len {
443 +
        throw Error::Capacity;
444 +
    }
445 +
    for exported, i in input.exports {
446 +
        let target = try resolve(local, &[], exported.name);
447 +
        match exported.kind {
448 +
            case binary::ExportKind::Function => {
449 +
                try function(local, &[], exported.name);
450 +
            },
451 +
            case binary::ExportKind::Data => {
452 +
                try dataRef(target);
453 +
            },
454 +
        }
455 +
        if lookup(&exportStorage[..i], exported.name) <> nil {
456 +
            throw Error::Symbol;
457 +
        }
458 +
        set exportStorage[i] = Symbol { name: exported.name, target };
459 +
    }
460 +
    let mut entry: ?u64 = nil;
461 +
    if let name = input.entry {
462 +
        set entry = try function(&exportStorage[..input.exports.len], &[], name);
463 +
    }
464 +
    let mut alignment: u32 = 8;
465 +
    for item in input.program.data { if item.alignment > alignment {
466 +
        set alignment = item.alignment;
467 +
    } }
468 +
    let code = generator.e.code.take();
469 +
    return Package {
470 +
        name: input.name, dependencies: input.dependencies, slot, codeAddress,
471 +
        code: &code[..], exports: &exportStorage[..input.exports.len], entry,
472 +
        template: &templateStorage[..size.bytes], memory, alignment,
473 +
        relocations: &relocationStorage[..size.relocations],
474 +
    };
475 +
}
476 +
477 +
/// Validate a package's private-state bases and fixups without touching memory.
478 +
/// Every base must be assigned and aligned, and every fixup must stay in range.
479 +
export fn checkInstance(package: &Package, bases: &[u64]) throws (Error) {
480 +
    if package.slot >= bases.len or bases[package.slot] == 0 {
481 +
        throw Error::Instance;
482 +
    }
483 +
    if package.template.len > package.memory {
484 +
        throw Error::Capacity;
485 +
    }
486 +
    if (bases[package.slot] & (package.alignment as u64 - 1)) <> 0 {
487 +
        throw Error::Alignment;
488 +
    }
489 +
    if bases[package.slot] > mem::U64_MAX - package.memory as u64 {
490 +
        throw Error::Range;
491 +
    }
492 +
    for fixup in package.relocations {
493 +
        if fixup.target.slot >= bases.len or bases[fixup.target.slot] == 0 {
494 +
            throw Error::Instance;
495 +
        }
496 +
        if bases[fixup.target.slot] > mem::U64_MAX - fixup.target.offset as u64
497 +
            {
498 +
            throw Error::Range;
499 +
        }
500 +
        if fixup.offset > package.memory or fixup.count > (package.memory - fixup.offset) / 8 {
501 +
            throw Error::Range;
502 +
        }
503 +
    }
504 +
}
505 +
506 +
/// Initialize private package memory after all graph bases have been assigned.
507 +
/// Validate every fixup before writing bytes so a rejected instance stays intact.
508 +
export fn instantiate(package: &Package, bases: &[u64], memory: &mut [u8]) throws (Error) {
509 +
    if memory.len < package.memory {
510 +
        throw Error::Capacity;
511 +
    }
512 +
    try checkInstance(package, bases);
513 +
    for i in 0..package.memory {
514 +
        set memory[i] = 0;
515 +
    }
516 +
    try! mem::copy(memory, package.template);
517 +
    for fixup in package.relocations {
518 +
        let address = bases[fixup.target.slot] + fixup.target.offset as u64;
519 +
        for i in 0..fixup.count {
520 +
            integer(memory, fixup.offset + i * 8, address, 8);
521 +
        }
522 +
    }
523 +
}
lib/std/arch/rv64/shared/catalog.rad added +227 -0
1 +
//! Persistent native boot catalogs for the RV64 record ABI.
2 +
//!
3 +
//! The kernel reads a catalog in place as [`Entry`] records, so each record is
4 +
//! written at the byte offsets of the compiler's native layout. The constants
5 +
//! below name those offsets, and `shared/tests.rad` compares every one of them
6 +
//! with the layout that the compiler gives the records, so that a change of
7 +
//! the layout fails a test. All integers are little-endian.
8 +
9 +
use std::mem;
10 +
use std::arch::rv64::shared;
11 +
12 +
/// Binary package identity and its resident native descriptor.
13 +
export record Entry: Copy {
14 +
    /// Complete immutable binary RIL bytes.
15 +
    source: *[u8],
16 +
    /// Shared code, exports, and private-state template.
17 +
    package: shared::Package,
18 +
}
19 +
20 +
/// Size of a native slice: data address, length, and capacity.
21 +
export constant SLICE_SIZE: u32 = 16;
22 +
/// Alignment of a native slice.
23 +
export constant SLICE_ALIGN: u32 = 8;
24 +
/// Offset of the length in a native slice. The capacity follows it.
25 +
export constant SLICE_LEN: u32 = 8;
26 +
/// Offset of the capacity in a native slice.
27 +
export constant SLICE_CAP: u32 = 12;
28 +
29 +
/// Size of a native [`Entry`].
30 +
export constant ENTRY_SIZE: u32 = 152;
31 +
/// Alignment of a native [`Entry`].
32 +
export constant ENTRY_ALIGN: u32 = 8;
33 +
/// Offset of `Entry.source`.
34 +
export constant ENTRY_SOURCE: u32 = 0;
35 +
/// Offset of `Entry.package.name`.
36 +
export constant ENTRY_NAME: u32 = 16;
37 +
/// Offset of `Entry.package.dependencies`.
38 +
export constant ENTRY_DEPENDENCIES: u32 = 32;
39 +
/// Offset of `Entry.package.slot`.
40 +
export constant ENTRY_SLOT: u32 = 48;
41 +
/// Offset of `Entry.package.codeAddress`.
42 +
export constant ENTRY_CODE_ADDRESS: u32 = 56;
43 +
/// Offset of `Entry.package.code`.
44 +
export constant ENTRY_CODE: u32 = 64;
45 +
/// Offset of `Entry.package.exports`.
46 +
export constant ENTRY_EXPORTS: u32 = 80;
47 +
/// Offset of the one-byte presence tag of `Entry.package.entry`: one when
48 +
/// present. The padding up to the value is zero.
49 +
export constant ENTRY_ENTRY_TAG: u32 = 96;
50 +
/// Offset of the value of `Entry.package.entry`.
51 +
export constant ENTRY_ENTRY_VALUE: u32 = 104;
52 +
/// Offset of `Entry.package.template`.
53 +
export constant ENTRY_TEMPLATE: u32 = 112;
54 +
/// Offset of `Entry.package.memory`.
55 +
export constant ENTRY_MEMORY: u32 = 128;
56 +
/// Offset of `Entry.package.alignment`.
57 +
export constant ENTRY_ALIGNMENT: u32 = 132;
58 +
/// Offset of `Entry.package.relocations`.
59 +
export constant ENTRY_RELOCATIONS: u32 = 136;
60 +
61 +
/// Size of a native [`shared::Symbol`].
62 +
export constant SYMBOL_SIZE: u32 = 32;
63 +
/// Alignment of a native [`shared::Symbol`].
64 +
export constant SYMBOL_ALIGN: u32 = 8;
65 +
/// Offset of `Symbol.name`.
66 +
export constant SYMBOL_NAME: u32 = 0;
67 +
/// Offset of the one-byte variant tag of `Symbol.target`: zero for a function,
68 +
/// one for data. The padding up to the payload is zero.
69 +
export constant SYMBOL_TAG: u32 = 16;
70 +
/// Offset of the payload of `Symbol.target`: the address of a function, or the
71 +
/// slot and the offset of a data reference as two 32-bit integers.
72 +
export constant SYMBOL_VALUE: u32 = 24;
73 +
74 +
/// Size of a native [`shared::Relocation`].
75 +
export constant RELOCATION_SIZE: u32 = 16;
76 +
/// Alignment of a native [`shared::Relocation`].
77 +
export constant RELOCATION_ALIGN: u32 = 4;
78 +
/// Offset of `Relocation.offset`.
79 +
export constant RELOCATION_OFFSET: u32 = 0;
80 +
/// Offset of `Relocation.count`.
81 +
export constant RELOCATION_COUNT: u32 = 4;
82 +
/// Offset of `Relocation.target.slot`.
83 +
export constant RELOCATION_SLOT: u32 = 8;
84 +
/// Offset of `Relocation.target.offset`.
85 +
export constant RELOCATION_TARGET_OFFSET: u32 = 12;
86 +
87 +
/// Reserve aligned bytes in the native catalog output.
88 +
fn reserve(output: &mut [u8], used: &mut u32, size: u32, alignment: u32) -> u32
89 +
    throws (shared::Error) {
90 +
    let padding = (alignment - (*used & (alignment - 1))) & (alignment - 1);
91 +
    if padding > output.len - *used {
92 +
        throw shared::Error::Capacity;
93 +
    }
94 +
    let offset = *used + padding;
95 +
    if size > output.len - offset {
96 +
        throw shared::Error::Capacity;
97 +
    }
98 +
    for i in *used..offset {
99 +
        set output[i] = 0;
100 +
    }
101 +
    set *used = offset + size;
102 +
    return offset;
103 +
}
104 +
105 +
/// Write one little-endian integer into a reserved native field.
106 +
fn integer(output: &mut [u8], offset: u32, value: u64, width: u32) {
107 +
    for i in 0..width {
108 +
        set output[offset + i] = (value >> (i as u64 * 8)) as u8;
109 +
    }
110 +
}
111 +
112 +
/// Write the RV64 pointer, length, and capacity of a native immutable slice.
113 +
fn slice(output: &mut [u8], offset: u32, address: u64, length: u32) {
114 +
    integer(output, offset, address, 8);
115 +
    integer(output, offset + SLICE_LEN, length as u64, 4);
116 +
    integer(output, offset + SLICE_CAP, length as u64, 4);
117 +
}
118 +
119 +
/// Copy bytes into the catalog and return their target-relative offset.
120 +
fn bytes(input: *[u8], output: &mut [u8], used: &mut u32) -> u32 throws (shared::Error) {
121 +
    let offset = try reserve(output, used, input.len, 1);
122 +
    try! mem::copy(&mut output[offset..offset + input.len], input);
123 +
    return offset;
124 +
}
125 +
126 +
/// Copy a dependency list and each name into native catalog storage.
127 +
fn dependencies(input: *[*[u8]], base: u64, output: &mut [u8], used: &mut u32) -> u32
128 +
    throws (shared::Error) {
129 +
    if input.len > output.len / SLICE_SIZE {
130 +
        throw shared::Error::Capacity;
131 +
    }
132 +
    let offset = try reserve(output, used, input.len * SLICE_SIZE, SLICE_ALIGN);
133 +
    for name, i in input {
134 +
        let at = try bytes(name, output, used);
135 +
        slice(output, offset + i * SLICE_SIZE, base + at as u64, name.len);
136 +
    }
137 +
    return offset;
138 +
}
139 +
140 +
/// Copy public symbols and their names into native catalog storage.
141 +
fn symbols(input: *[shared::Symbol], base: u64, output: &mut [u8], used: &mut u32) -> u32
142 +
    throws (shared::Error) {
143 +
    if input.len > output.len / SYMBOL_SIZE {
144 +
        throw shared::Error::Capacity;
145 +
    }
146 +
    let offset = try reserve(output, used, input.len * SYMBOL_SIZE, SYMBOL_ALIGN);
147 +
    for symbol, i in input {
148 +
        let at = offset + i * SYMBOL_SIZE;
149 +
        let name = try bytes(symbol.name, output, used);
150 +
        slice(output, at + SYMBOL_NAME, base + name as u64, symbol.name.len);
151 +
        match symbol.target {
152 +
            case shared::Target::Function(address) => {
153 +
                integer(output, at + SYMBOL_TAG, 0, 8);
154 +
                integer(output, at + SYMBOL_VALUE, address, 8);
155 +
            },
156 +
            case shared::Target::Data(location) => {
157 +
                integer(output, at + SYMBOL_TAG, 1, 8);
158 +
                integer(output, at + SYMBOL_VALUE, location.slot as u64, 4);
159 +
                integer(output, at + SYMBOL_VALUE + 4, location.offset as u64, 4);
160 +
            },
161 +
        }
162 +
    }
163 +
    return offset;
164 +
}
165 +
166 +
/// Copy private-state relocation records into native catalog storage.
167 +
fn relocations(input: *[shared::Relocation], output: &mut [u8], used: &mut u32) -> u32
168 +
    throws (shared::Error) {
169 +
    if input.len > output.len / RELOCATION_SIZE {
170 +
        throw shared::Error::Capacity;
171 +
    }
172 +
    let offset = try reserve(output, used, input.len * RELOCATION_SIZE, RELOCATION_ALIGN);
173 +
    for fixup, i in input {
174 +
        let at = offset + i * RELOCATION_SIZE;
175 +
        integer(output, at + RELOCATION_OFFSET, fixup.offset as u64, 4);
176 +
        integer(output, at + RELOCATION_COUNT, fixup.count as u64, 4);
177 +
        integer(output, at + RELOCATION_SLOT, fixup.target.slot as u64, 4);
178 +
        integer(output, at + RELOCATION_TARGET_OFFSET, fixup.target.offset as u64, 4);
179 +
    }
180 +
    return offset;
181 +
}
182 +
183 +
/// Pack native Entry records followed by all referenced metadata and source bytes.
184 +
/// Code is placed separately at each package's codeAddress. The target base must
185 +
/// have Entry alignment. Return the used extent. Failure leaves partial output.
186 +
export fn pack(entries: &[Entry], base: u64, output: &mut [u8]) -> u32 throws (shared::Error) {
187 +
    if (base & (ENTRY_ALIGN as u64 - 1)) <> 0 {
188 +
        throw shared::Error::Alignment;
189 +
    }
190 +
    if base > mem::U64_MAX - output.len as u64 {
191 +
        throw shared::Error::Range;
192 +
    }
193 +
    if entries.len > output.len / ENTRY_SIZE {
194 +
        throw shared::Error::Capacity;
195 +
    }
196 +
    let mut used: u32 = 0;
197 +
    try reserve(output, &mut used, entries.len * ENTRY_SIZE, ENTRY_ALIGN);
198 +
    for entry, i in entries {
199 +
        let at = i * ENTRY_SIZE;
200 +
        for k in 0..ENTRY_SIZE {
201 +
            set output[at + k] = 0;
202 +
        }
203 +
        let package = entry.package;
204 +
        let source = try bytes(entry.source, output, &mut used);
205 +
        let name = try bytes(package.name, output, &mut used);
206 +
        let required = try dependencies(package.dependencies, base, output, &mut used);
207 +
        let exports = try symbols(package.exports, base, output, &mut used);
208 +
        let template = try bytes(package.template, output, &mut used);
209 +
        let fixups = try relocations(package.relocations, output, &mut used);
210 +
        slice(output, at + ENTRY_SOURCE, base + source as u64, entry.source.len);
211 +
        slice(output, at + ENTRY_NAME, base + name as u64, package.name.len);
212 +
        slice(output, at + ENTRY_DEPENDENCIES, base + required as u64, package.dependencies.len);
213 +
        integer(output, at + ENTRY_SLOT, package.slot as u64, 4);
214 +
        integer(output, at + ENTRY_CODE_ADDRESS, package.codeAddress, 8);
215 +
        slice(output, at + ENTRY_CODE, package.codeAddress, package.code.len);
216 +
        slice(output, at + ENTRY_EXPORTS, base + exports as u64, package.exports.len);
217 +
        if let address = package.entry {
218 +
            integer(output, at + ENTRY_ENTRY_TAG, 1, 8);
219 +
            integer(output, at + ENTRY_ENTRY_VALUE, address, 8);
220 +
        }
221 +
        slice(output, at + ENTRY_TEMPLATE, base + template as u64, package.template.len);
222 +
        integer(output, at + ENTRY_MEMORY, package.memory as u64, 4);
223 +
        integer(output, at + ENTRY_ALIGNMENT, package.alignment as u64, 4);
224 +
        slice(output, at + ENTRY_RELOCATIONS, base + fixups as u64, package.relocations.len);
225 +
    }
226 +
    return used;
227 +
}
lib/std/arch/rv64/shared/tests.rad added +381 -0
1 +
//! Private template relocation and bounded linking checks.
2 +
3 +
@test mod run;
4 +
5 +
use std::testing;
6 +
use std::arch::rv64;
7 +
use std::mem;
8 +
use std::arch::rv64::shared::catalog;
9 +
use std::arch::rv64::asm;
10 +
use std::lang::alloc;
11 +
use std::lang::il;
12 +
use std::lang::il::binary;
13 +
use std::lang::gen::data;
14 +
15 +
/// Code generation workspace reused between checks.
16 +
export static ARENA: [u8; 16777216] = [0; 16777216];
17 +
/// Function allocation workspace.
18 +
export static SCRATCH: [u8; 16777216] = [0; 16777216];
19 +
/// Persistent catalog symbols.
20 +
unsafe static SYMBOLS: [super::Symbol; 8] = undefined;
21 +
/// Persistent exported symbols.
22 +
unsafe static EXPORTS: [super::Symbol; 8] = undefined;
23 +
/// Data layout workspace.
24 +
unsafe static DATA: [data::DataSym; 8] = undefined;
25 +
/// Persistent initialized bytes.
26 +
static TEMPLATE: [u8; 64] = [0; 64];
27 +
/// Persistent private data relocations.
28 +
unsafe static RELOCS: [super::Relocation; 8] = undefined;
29 +
/// Aligned storage for a native boot catalog and its retained payloads.
30 +
static CATALOG: [u64; 512] = [0; 512];
31 +
32 +
/// Exported assembly boundaries occupy the same code extent as their package.
33 +
@test unsafe fn assemblyPrefix() throws (testing::TestError) {
34 +
    let input = binary::Package {
35 +
        symbols: &[], name: "p", dependencies: &[],
36 +
        exports: &[binary::Export { name: "p::entry", kind: binary::ExportKind::Function }],
37 +
        entry: "p::entry", program: il::Program { data: &[], fns: &[], layouts: &[],
38 +
            signatures: &[] },
39 +
    };
40 +
    let assembly = asm::Program {
41 +
        text: &[0x02a00513 as u32, 0x00008067], data: &[],
42 +
        symbols: &[asm::Symbol { name: "p::entry", section: asm::Section::Text, offset: 0,
43 +
            isExported: true }],
44 +
        externalFixups: &[],
45 +
    };
46 +
    let arena = alloc::new(&mut ARENA[..]);
47 +
    let mut scratch = alloc::new(&mut SCRATCH[..]);
48 +
    let package = try super::compileAssembly(&input, &assembly, 0, 0x180000000, &[],
49 +
        super::Storage {
50 +
            data: &mut DATA[..], symbols: &mut SYMBOLS[..], exports: &mut EXPORTS[..],
51 +
            template: &mut TEMPLATE[..], relocations: &mut RELOCS[..],
52 +
            stack: rv64::StackCheck::Off,
53 +
        }, arena, &mut scratch) catch {
54 +
            throw testing::TestError::Failed;
55 +
        };
56 +
    let entry = package.entry else {
57 +
        throw testing::TestError::Failed;
58 +
    };
59 +
    try testing::expect(entry == 0x180000000 and package.code.len == 2);
60 +
    try testing::expect(package.code[0] == 0x02a00513
61 +
        and package.exports[0].target == super::Target::Function(entry));
62 +
}
63 +
64 +
/// Packed catalog pointers resolve to retained metadata at the supplied native base.
65 +
@test unsafe fn nativeCatalog() throws (testing::TestError) {
66 +
    let package = try compile(imports(), 64) catch {
67 +
        throw testing::TestError::Failed;
68 +
    };
69 +
    let output = @sliceOf(&mut CATALOG[0] as *mut u8, @sizeOf([u64; 512]));
70 +
    let base = output.ptr as u64;
71 +
    let used = try catalog::pack(&[catalog::Entry { source: "binary package", package }], base,
72 +
        output)
73 +
        catch {
74 +
            throw testing::TestError::Failed;
75 +
        };
76 +
    let storage = &CATALOG[0] as *opaque;
77 +
    let entry = storage as *catalog::Entry;
78 +
    try testing::expect(used > @sizeOf(catalog::Entry) and used <= output.len);
79 +
    try testing::expect(mem::eq(entry.source, "binary package") and mem::eq(entry.package.name,
80 +
        "p"));
81 +
    try testing::expect(mem::eq(entry.package.dependencies[0], "dep"));
82 +
    try testing::expect(entry.package.code.ptr as u64 == package.codeAddress
83 +
        and entry.package.code.len == package.code.len);
84 +
    try testing::expect(entry.source.ptr as u64 >= base
85 +
        and entry.source.ptr as u64 < base + used as u64);
86 +
    try testing::expect(entry.package.slot == package.slot
87 +
        and entry.package.codeAddress == package.codeAddress);
88 +
    try testing::expect(entry.package.entry == package.entry
89 +
        and entry.package.memory == package.memory);
90 +
    try testing::expect(entry.package.alignment == package.alignment);
91 +
    try testing::expect(entry.package.exports.len == package.exports.len
92 +
        and package.exports.len > 0);
93 +
    for symbol, i in package.exports {
94 +
        try testing::expect(entry.package.exports[i].target == symbol.target);
95 +
        try testing::expect(mem::eq(entry.package.exports[i].name, symbol.name));
96 +
    }
97 +
    try testing::expect(entry.package.relocations.len == package.relocations.len
98 +
        and package.relocations.len > 0);
99 +
    for fixup, i in package.relocations {
100 +
        try testing::expect(entry.package.relocations[i] == fixup);
101 +
    }
102 +
    try testing::expect(mem::eq(entry.package.template, package.template));
103 +
    set TEMPLATE[0] = 91;
104 +
    set EXPORTS[0].name = "changed";
105 +
    try testing::expect(entry.package.template[0] == 0 and mem::eq(entry.package.exports[0].name,
106 +
        "p::refs"));
107 +
    let mut state: [u8; 48] = [255; 48];
108 +
    try super::instantiate(&entry.package, &[0, 0x180002000, 0x180008000], &mut state[..])
109 +
        catch {
110 +
            throw testing::TestError::Failed;
111 +
        };
112 +
    try testing::expect(pointer(&state[..], 0) == 0x180004000 and pointer(&state[..],
113 +
        16) == 0x180001234);
114 +
    let relocated = try catalog::pack(&[catalog::Entry { source: "binary package", package }],
115 +
        0x180000000, output)
116 +
        catch {
117 +
            throw testing::TestError::Failed;
118 +
        };
119 +
    try testing::expect(entry.source.ptr as u64 >= 0x180000000
120 +
        and entry.source.ptr as u64 < 0x180000000 + relocated as u64);
121 +
}
122 +
123 +
/// Byte offset of `value` in the native layout of `container`.
124 +
unsafe fn offsetOf(container: &opaque, value: &opaque) -> u32 {
125 +
    return (value as u64 - container as u64) as u32;
126 +
}
127 +
128 +
/// Little-endian integer of `width` bytes at `offset` of a native record.
129 +
unsafe fn word(container: &opaque, offset: u32, width: u32) -> u64 {
130 +
    let bytes = @sliceOf((container as &u8) as *unsafe u8, offset + width);
131 +
    let mut value: u64 = 0;
132 +
    for i in 0..width {
133 +
        set value |= bytes[offset + i] as u64 << (i as u64 * 8);
134 +
    }
135 +
    return value;
136 +
}
137 +
138 +
/// The offsets that the catalog writes are the compiler's native record layout.
139 +
@test unsafe fn nativeCatalogLayout() throws (testing::TestError) {
140 +
    let package = super::Package {
141 +
        name: "n", dependencies: &[], slot: 7, codeAddress: 0x1122334455, code: &[], exports: &[],
142 +
        entry: 0x99, template: &[], memory: 9, alignment: 16, relocations: &[],
143 +
    };
144 +
    let entry = catalog::Entry { source: "abc", package };
145 +
    try testing::expect(@sizeOf(*[u8]) == catalog::SLICE_SIZE
146 +
        and @alignOf(*[u8]) == catalog::SLICE_ALIGN);
147 +
    try testing::expect(@sizeOf(catalog::Entry) == catalog::ENTRY_SIZE
148 +
        and @alignOf(catalog::Entry) == catalog::ENTRY_ALIGN);
149 +
    try testing::expect(offsetOf(&entry, &entry.source) == catalog::ENTRY_SOURCE);
150 +
    try testing::expect(offsetOf(&entry, &entry.package.name) == catalog::ENTRY_NAME);
151 +
    try testing::expect(offsetOf(&entry,
152 +
        &entry.package.dependencies) == catalog::ENTRY_DEPENDENCIES);
153 +
    try testing::expect(offsetOf(&entry, &entry.package.slot) == catalog::ENTRY_SLOT);
154 +
    try testing::expect(offsetOf(&entry,
155 +
        &entry.package.codeAddress) == catalog::ENTRY_CODE_ADDRESS);
156 +
    try testing::expect(offsetOf(&entry, &entry.package.code) == catalog::ENTRY_CODE);
157 +
    try testing::expect(offsetOf(&entry, &entry.package.exports) == catalog::ENTRY_EXPORTS);
158 +
    try testing::expect(offsetOf(&entry, &entry.package.entry) == catalog::ENTRY_ENTRY_TAG);
159 +
    try testing::expect(offsetOf(&entry, &entry.package.template) == catalog::ENTRY_TEMPLATE);
160 +
    try testing::expect(offsetOf(&entry, &entry.package.memory) == catalog::ENTRY_MEMORY);
161 +
    try testing::expect(offsetOf(&entry, &entry.package.alignment) == catalog::ENTRY_ALIGNMENT);
162 +
    try testing::expect(offsetOf(&entry, &entry.package.relocations) == catalog::ENTRY_RELOCATIONS);
163 +
    // A slice is an address, a length, and a capacity.
164 +
    let slice = catalog::ENTRY_SOURCE;
165 +
    try testing::expect(word(&entry, slice, 8) == entry.source.ptr as u64);
166 +
    try testing::expect(word(&entry, slice + catalog::SLICE_LEN, 4) == 3 and word(&entry,
167 +
        slice + catalog::SLICE_CAP, 4) == 3);
168 +
    // An optional integer is a one-byte presence tag followed by the value at the next
169 +
    // alignment. A union variant tag is one byte as well.
170 +
    try testing::expect(word(&entry, catalog::ENTRY_ENTRY_TAG, 1) == 1 and word(&entry,
171 +
        catalog::ENTRY_ENTRY_VALUE, 8) == 0x99);
172 +
    try testing::expect(word(&entry, catalog::ENTRY_SLOT, 4) == 7 and word(&entry,
173 +
        catalog::ENTRY_CODE_ADDRESS, 8) == 0x1122334455);
174 +
    try testing::expect(word(&entry, catalog::ENTRY_MEMORY, 4) == 9 and word(&entry,
175 +
        catalog::ENTRY_ALIGNMENT, 4) == 16);
176 +
    let absent = catalog::Entry { source: "abc", package: super::Package {
177 +
        name: "n", dependencies: &[], slot: 7, codeAddress: 0, code: &[], exports: &[], entry: nil,
178 +
        template: &[], memory: 9, alignment: 16, relocations: &[],
179 +
    } };
180 +
    try testing::expect(word(&absent, catalog::ENTRY_ENTRY_TAG, 1) == 0);
181 +
182 +
    let function = super::Symbol { name: "f", target: super::Target::Function(0xaabbccdd11) };
183 +
    let data = super::Symbol { name: "d", target: super::Target::Data(super::DataRef { slot: 3,
184 +
        offset: 4 }) };
185 +
    try testing::expect(@sizeOf(super::Symbol) == catalog::SYMBOL_SIZE
186 +
        and @alignOf(super::Symbol) == catalog::SYMBOL_ALIGN);
187 +
    try testing::expect(offsetOf(&function, &function.name) == catalog::SYMBOL_NAME);
188 +
    try testing::expect(offsetOf(&function, &function.target) == catalog::SYMBOL_TAG);
189 +
    try testing::expect(word(&function, catalog::SYMBOL_TAG, 1) == 0 and word(&function,
190 +
        catalog::SYMBOL_VALUE, 8) == 0xaabbccdd11);
191 +
    try testing::expect(word(&data, catalog::SYMBOL_TAG, 1) == 1 and word(&data,
192 +
        catalog::SYMBOL_VALUE, 4) == 3);
193 +
    try testing::expect(word(&data, catalog::SYMBOL_VALUE + 4, 4) == 4);
194 +
195 +
    let fixup = super::Relocation { offset: 1, count: 2, target: super::DataRef { slot: 3,
196 +
        offset: 4 } };
197 +
    try testing::expect(@sizeOf(super::Relocation) == catalog::RELOCATION_SIZE);
198 +
    try testing::expect(@alignOf(super::Relocation) == catalog::RELOCATION_ALIGN);
199 +
    try testing::expect(offsetOf(&fixup, &fixup.offset) == catalog::RELOCATION_OFFSET);
200 +
    try testing::expect(offsetOf(&fixup, &fixup.count) == catalog::RELOCATION_COUNT);
201 +
    try testing::expect(offsetOf(&fixup, &fixup.target.slot) == catalog::RELOCATION_SLOT);
202 +
    try testing::expect(offsetOf(&fixup,
203 +
        &fixup.target.offset) == catalog::RELOCATION_TARGET_OFFSET);
204 +
}
205 +
206 +
/// Catalog bounds and native alignment failures are recoverable.
207 +
@test unsafe fn nativeCatalogBounds() throws (testing::TestError) {
208 +
    let package = try compile(imports(), 64) catch {
209 +
        throw testing::TestError::Failed;
210 +
    };
211 +
    let entries = [catalog::Entry { source: "binary", package }];
212 +
    let output = @sliceOf(&mut CATALOG[0] as *mut u8, @sizeOf([u64; 512]));
213 +
    let mut failures: u32 = 0;
214 +
    try catalog::pack(&entries[..], 0x80000000, &mut output[..@sizeOf(catalog::Entry)]) catch err {
215 +
        try testing::expect(err == super::Error::Capacity);
216 +
        set failures += 1;
217 +
    };
218 +
    try catalog::pack(&entries[..], 0x80000001, output) catch err {
219 +
        try testing::expect(err == super::Error::Alignment);
220 +
        set failures += 1;
221 +
    };
222 +
    try catalog::pack(&entries[..], 0xfffffffffffffff8, output) catch err {
223 +
        try testing::expect(err == super::Error::Range);
224 +
        set failures += 1;
225 +
    };
226 +
    try testing::expect(failures == 3);
227 +
    let used = try catalog::pack(&entries[..], 0x80000000, output) catch {
228 +
        throw testing::TestError::Failed;
229 +
    };
230 +
    try testing::expect(used > @sizeOf(catalog::Entry));
231 +
}
232 +
233 +
/// Build a package with repeated dependency pointers and a high code pointer.
234 +
unsafe fn input() -> binary::Package {
235 +
    return binary::Package {
236 +
        symbols: &[], name: "p", dependencies: &["dep"],
237 +
        exports: &[binary::Export { name: "p::refs", kind: binary::ExportKind::Data }],
238 +
        entry: nil,
239 +
        program: il::Program {
240 +
            data: &[
241 +
                il::Data {
242 +
                    name: "p::refs", size: 32, alignment: 8, layout: 0, readOnly: true,
243 +
                        isZeroInit: false,
244 +
                    values: &[
245 +
                        il::DataValue { item: il::DataItem::Sym("dep::value"), count: 2 },
246 +
                        il::DataValue { item: il::DataItem::Fn("dep::call"), count: 1 },
247 +
                    ],
248 +
                },
249 +
                il::Data {
250 +
                    name: "p::zero", size: 16, alignment: 16, layout: 1, readOnly: false,
251 +
                        isZeroInit: true, values: &[],
252 +
                },
253 +
            ],
254 +
            fns: &[],
255 +
            layouts: &[
256 +
                il::Layout { size: 32, alignment: 8, slots: &[] },
257 +
                il::Layout { size: 16, alignment: 16, slots: &[] },
258 +
            ],
259 +
            signatures: &[],
260 +
        },
261 +
    };
262 +
}
263 +
264 +
/// Compile into small persistent tables.
265 +
unsafe fn compile(imports: &[super::Symbol], capacity: u32) -> super::Package
266 +
    throws (super::Error) {
267 +
    let arena = alloc::new(&mut ARENA[..]);
268 +
    let mut scratch = alloc::new(&mut SCRATCH[..]);
269 +
    let package = input();
270 +
    return try super::compile(&package, 2, 0x180000000, imports,
271 +
        super::Storage {
272 +
            data: &mut DATA[..], symbols: &mut SYMBOLS[..], exports: &mut EXPORTS[..],
273 +
            template: &mut TEMPLATE[..capacity], relocations: &mut RELOCS[..],
274 +
            stack: rv64::StackCheck::Off,
275 +
        }, arena, &mut scratch);
276 +
}
277 +
278 +
/// Dependency exports with a full-width function pointer and a large data offset.
279 +
fn imports() -> *[super::Symbol] {
280 +
    return &[
281 +
        super::Symbol { name: "dep::value", target: super::Target::Data(super::DataRef { slot: 1,
282 +
            offset: 8192 }) },
283 +
        super::Symbol { name: "dep::call", target: super::Target::Function(0x180001234) },
284 +
    ];
285 +
}
286 +
287 +
/// Read a little-endian pointer without requiring buffer alignment.
288 +
fn pointer(bytes: &[u8], offset: u32) -> u64 {
289 +
    let mut value: u64 = 0;
290 +
    for i in 0..8 {
291 +
        set value |= bytes[offset + i] as u64 << (i as u64 * 8);
292 +
    }
293 +
    return value;
294 +
}
295 +
296 +
/// Check repeated pointers, full-width code addresses, padding, and zero-filled tails.
297 +
@test unsafe fn privateRelocations() throws (testing::TestError) {
298 +
    let package = try compile(imports(), 64) catch {
299 +
        throw testing::TestError::Failed;
300 +
    };
301 +
    try testing::expect(package.template.len == 32 and package.memory == 48
302 +
        and package.alignment == 16);
303 +
    try testing::expect(package.relocations.len == 1 and package.relocations[0].count == 2);
304 +
    try testing::expect(pointer(package.template, 16) == 0x180001234);
305 +
    let mut first: [u8; 48] = [255; 48];
306 +
    let mut second: [u8; 48] = [255; 48];
307 +
    try super::instantiate(&package, &[0, 0x180002000, 0x180008000], &mut first[..])
308 +
        catch {
309 +
            throw testing::TestError::Failed;
310 +
        };
311 +
    try super::instantiate(&package, &[0, 0x280002000, 0x280008000], &mut second[..])
312 +
        catch {
313 +
            throw testing::TestError::Failed;
314 +
        };
315 +
    try testing::expect(pointer(&first[..], 0) == 0x180004000 and pointer(&first[..],
316 +
        8) == 0x180004000);
317 +
    try testing::expect(pointer(&second[..], 0) == 0x280004000 and pointer(&second[..],
318 +
        8) == 0x280004000);
319 +
    try testing::expect(pointer(&first[..], 16) == pointer(&second[..], 16));
320 +
    for i in 24..48 {
321 +
        try testing::expect(first[i] == 0 and second[i] == 0);
322 +
    }
323 +
}
324 +
325 +
/// A missing instance, bad base, or short buffer leaves instance bytes untouched.
326 +
@test unsafe fn instanceFailures() throws (testing::TestError) {
327 +
    let package = try compile(imports(), 64) catch {
328 +
        throw testing::TestError::Failed;
329 +
    };
330 +
    let mut bytes: [u8; 48] = [255; 48];
331 +
    let mut failed: u32 = 0;
332 +
    try super::instantiate(&package, &[0, 0, 0x8000], &mut bytes[..]) catch err {
333 +
        try testing::expect(err == super::Error::Instance);
334 +
        set failed += 1;
335 +
    };
336 +
    try super::instantiate(&package, &[0, 0x2000, 0x8008], &mut bytes[..]) catch err {
337 +
        try testing::expect(err == super::Error::Alignment);
338 +
        set failed += 1;
339 +
    };
340 +
    try super::instantiate(&package, &[0, 0x2000, 0x8000], &mut bytes[..47]) catch err {
341 +
        try testing::expect(err == super::Error::Capacity);
342 +
        set failed += 1;
343 +
    };
344 +
    try super::instantiate(&package, &[0, 0xffffffffffffffff, 0x8000], &mut bytes[..]) catch err {
345 +
        try testing::expect(err == super::Error::Range);
346 +
        set failed += 1;
347 +
    };
348 +
    try testing::expect(failed == 4);
349 +
    for byte in &bytes[..] {
350 +
        try testing::expect(byte == 255);
351 +
    }
352 +
}
353 +
354 +
/// Missing symbols, duplicate imports, kind mismatches, and short output fail explicitly.
355 +
@test unsafe fn linkFailures() throws (testing::TestError) {
356 +
    let mut failed: u32 = 0;
357 +
    try compile(&[], 64) catch err {
358 +
        try testing::expect(err == super::Error::Symbol);
359 +
        set failed += 1;
360 +
    };
361 +
    let names = imports();
362 +
    try compile(&[names[0], names[0]], 64) catch err {
363 +
        try testing::expect(err == super::Error::Symbol);
364 +
        set failed += 1;
365 +
    };
366 +
    try compile(&[
367 +
        super::Symbol { name: "dep::value", target: super::Target::Function(0x8000) }, names[1],
368 +
    ], 64) catch err {
369 +
        try testing::expect(err == super::Error::Symbol);
370 +
        set failed += 1;
371 +
    };
372 +
    try compile(names, 31) catch err {
373 +
        try testing::expect(err == super::Error::Capacity);
374 +
        set failed += 1;
375 +
    };
376 +
    try testing::expect(failed == 4);
377 +
    let package = try compile(names, 64) catch {
378 +
        throw testing::TestError::Failed;
379 +
    };
380 +
    try testing::expect(package.exports.len == 1);
381 +
}
lib/std/arch/rv64/shared/tests/run.rad added +735 -0
1 +
//! Execution of generated shared code by a small interpreter.
2 +
//!
3 +
//! A check compiles one function with the shared code generator, runs its machine code on
4 +
//! a model of the few instructions it uses, and compares the registers and memory that the
5 +
//! function leaves with the semantics of the IL instruction.
6 +
7 +
use std::testing;
8 +
use std::arch::rv64;
9 +
use std::arch::rv64::shared;
10 +
use std::arch::rv64::decode;
11 +
use std::lang::alloc;
12 +
use std::lang::gen;
13 +
use std::lang::il;
14 +
use std::lang::il::binary;
15 +
use std::lang::gen::data;
16 +
use std::arch::rv64::shared::tests;
17 +
18 +
/// Storage for the IL of a check.
19 +
static IL: [u8; 65536] = [0; 65536];
20 +
/// Layout table of a blit check, which has the copied layout only.
21 +
unsafe static COPIED: [il::Layout; 1] = undefined;
22 +
/// Storage for the instructions of a blit check.
23 +
static BODY: [u8; 131072] = [0; 131072];
24 +
/// Persistent catalog symbols.
25 +
unsafe static SYMBOLS: [shared::Symbol; 8] = undefined;
26 +
/// Persistent exported symbols.
27 +
unsafe static EXPORTS: [shared::Symbol; 8] = undefined;
28 +
/// Data layout workspace.
29 +
unsafe static DATA: [data::DataSym; 8] = undefined;
30 +
/// Persistent initialized bytes.
31 +
static TEMPLATE: [u8; 64] = [0; 64];
32 +
/// Persistent private data relocations.
33 +
unsafe static RELOCS: [shared::Relocation; 8] = undefined;
34 +
35 +
/// Address of the modeled memory.
36 +
constant MEMORY_BASE: u64 = 0x1000;
37 +
/// Size of the modeled memory in bytes.
38 +
constant MEMORY_SIZE: u32 = 8192;
39 +
/// Modeled memory.
40 +
static MEMORY: [u8; 8192] = [0; 8192];
41 +
/// Address of the table of package bases, which `gp` holds.
42 +
constant TABLE: u64 = 0x1000;
43 +
/// Package slot of the checked function.
44 +
constant SLOT: u32 = 1;
45 +
/// Base address of the private data of the package. It is small, so that the low 32 bits of
46 +
/// a data address are an index that the slice instructions accept.
47 +
constant PACKAGE_BASE: u64 = 0x10;
48 +
/// Address of the slice object that a check writes.
49 +
constant RESULT: u64 = 0x1040;
50 +
/// Address of the slice object that a check reads.
51 +
constant SOURCE: u64 = 0x1080;
52 +
/// Value that `ra` holds on entry. Jumping to it ends the run.
53 +
constant RETURN: u64 = 0xdead0000;
54 +
/// Initial stack pointer.
55 +
constant STACK: u64 = 0x3000;
56 +
/// Data pointer of the source slice and the array base of a check.
57 +
constant POINTER: u64 = 0x5000;
58 +
/// Instructions that one run may execute.
59 +
constant MAX_STEPS: u32 = 20000;
60 +
/// Size in bytes of the element layout of the checks.
61 +
constant ELEMENT: u64 = 8;
62 +
/// Name of the data symbol at the lower address.
63 +
constant FIRST: *[u8] = "p::first";
64 +
/// Name of the data symbol at the higher address.
65 +
constant SECOND: *[u8] = "p::second";
66 +
/// Address of the first byte of the area that a blit check copies from.
67 +
constant SOURCE_AREA: u64 = 0x1100;
68 +
/// Address of the first byte of the area that a blit check copies to.
69 +
constant TARGET_AREA: u64 = 0x1a00;
70 +
/// Bytes of guard before and after the copied object in an area. The object is aligned to
71 +
/// the largest unit of a copy, so the guard has that size.
72 +
constant GUARD: u64 = 8;
73 +
/// Value of the bytes that a blit check must leave alone.
74 +
constant UNTOUCHED: u8 = 0xaa;
75 +
/// Number of values that stay live across the blit of a check with spilled operands.
76 +
constant LIVE: u32 = 30;
77 +
78 +
/// How a run ended.
79 +
union Outcome: Copy {
80 +
    /// The function returned `value` in `a0`.
81 +
    Returned { value: u64 },
82 +
    /// The function executed `ebreak`.
83 +
    Trapped,
84 +
    /// The function did something that the model does not support.
85 +
    Faulted,
86 +
}
87 +
88 +
/// Registers of the modeled machine.
89 +
record Machine {
90 +
    /// General-purpose registers.
91 +
    regs: [u64; 32],
92 +
    /// Number of instructions executed.
93 +
    steps: u32,
94 +
}
95 +
96 +
/// Read a register.
97 +
fn read(m: &Machine, r: gen::Reg) -> u64 {
98 +
    return m.regs[*r as u32];
99 +
}
100 +
101 +
/// Write a register. `zero` ignores writes.
102 +
fn write(m: &mut Machine, r: gen::Reg, value: u64) {
103 +
    if *r <> 0 {
104 +
        set m.regs[*r as u32] = value;
105 +
    }
106 +
}
107 +
108 +
/// Whether an access of `width` bytes at `address` is inside the modeled memory and aligned to
109 +
/// its width.
110 +
fn accessible(address: u64, width: u32) -> bool {
111 +
    return address >= MEMORY_BASE and address - MEMORY_BASE + width as u64 <= MEMORY_SIZE as u64
112 +
        and address % width as u64 == 0;
113 +
}
114 +
115 +
/// Load `width` bytes at `address`, or `nil` outside the modeled memory or when misaligned.
116 +
unsafe fn load(address: u64, width: u32) -> ?u64 {
117 +
    if not accessible(address, width) {
118 +
        return nil;
119 +
    }
120 +
    let offset = (address - MEMORY_BASE) as u32;
121 +
    let mut value: u64 = 0;
122 +
    for i in 0..width {
123 +
        set value |= MEMORY[offset + i] as u64 << (i as u64 * 8);
124 +
    }
125 +
    return value;
126 +
}
127 +
128 +
/// Store the low `width` bytes of `value` at `address`. Returns false outside the memory or
129 +
/// when misaligned.
130 +
unsafe fn store(address: u64, width: u32, value: u64) -> bool {
131 +
    if not accessible(address, width) {
132 +
        return false;
133 +
    }
134 +
    let offset = (address - MEMORY_BASE) as u32;
135 +
    for i in 0..width {
136 +
        set MEMORY[offset + i] = (value >> (i as u64 * 8)) as u8;
137 +
    }
138 +
    return true;
139 +
}
140 +
141 +
/// Sign-extend a 32-bit value to 64 bits.
142 +
fn sext32(value: u64) -> u64 {
143 +
    return (value as u32 as i32) as i64 as u64;
144 +
}
145 +
146 +
/// Sign-extend a 16-bit value to 64 bits.
147 +
fn sext16(value: u64) -> u64 {
148 +
    return (value as u16 as i16) as i64 as u64;
149 +
}
150 +
151 +
/// Sign-extend an 8-bit value to 64 bits.
152 +
fn sext8(value: u64) -> u64 {
153 +
    return (value as u8 as i8) as i64 as u64;
154 +
}
155 +
156 +
/// Run `code` from its first instruction until it returns, traps or leaves the model.
157 +
unsafe fn run(code: *[u32], m: &mut Machine) -> Outcome {
158 +
    let mut pc: u32 = 0;
159 +
    for _ in 0..MAX_STEPS {
160 +
        if pc >= code.len {
161 +
            return Outcome::Faulted;
162 +
        }
163 +
        set m.steps += 1;
164 +
        let mut next = pc + 1;
165 +
        match decode::decode(code[pc]) {
166 +
            case decode::Instr::Addi { rd, rs1, imm } => {
167 +
                write(m, rd, read(m, rs1) + imm as i64 as u64);
168 +
            },
169 +
            case decode::Instr::Addiw { rd, rs1, imm } => {
170 +
                write(m, rd, sext32(read(m, rs1) + imm as i64 as u64));
171 +
            },
172 +
            case decode::Instr::Add { rd, rs1, rs2 } => {
173 +
                write(m, rd, read(m, rs1) + read(m, rs2));
174 +
            },
175 +
            case decode::Instr::Sub { rd, rs1, rs2 } => {
176 +
                write(m, rd, read(m, rs1) - read(m, rs2));
177 +
            },
178 +
            case decode::Instr::Slli { rd, rs1, shamt } => {
179 +
                write(m, rd, read(m, rs1) << shamt as u64);
180 +
            },
181 +
            case decode::Instr::Srli { rd, rs1, shamt } => {
182 +
                write(m, rd, read(m, rs1) >> shamt as u64);
183 +
            },
184 +
            case decode::Instr::Lui { rd, imm } => {
185 +
                write(m, rd, sext32((imm as i64 as u64) << 12));
186 +
            },
187 +
            case decode::Instr::Ld { rd, rs1, imm } => {
188 +
                let value = load(read(m, rs1) + imm as i64 as u64, 8) else {
189 +
                    return Outcome::Faulted;
190 +
                };
191 +
                write(m, rd, value);
192 +
            },
193 +
            case decode::Instr::Lwu { rd, rs1, imm } => {
194 +
                let value = load(read(m, rs1) + imm as i64 as u64, 4) else {
195 +
                    return Outcome::Faulted;
196 +
                };
197 +
                write(m, rd, value);
198 +
            },
199 +
            case decode::Instr::Lw { rd, rs1, imm } => {
200 +
                let value = load(read(m, rs1) + imm as i64 as u64, 4) else {
201 +
                    return Outcome::Faulted;
202 +
                };
203 +
                write(m, rd, sext32(value));
204 +
            },
205 +
            case decode::Instr::Lhu { rd, rs1, imm } => {
206 +
                let value = load(read(m, rs1) + imm as i64 as u64, 2) else {
207 +
                    return Outcome::Faulted;
208 +
                };
209 +
                write(m, rd, value);
210 +
            },
211 +
            case decode::Instr::Lh { rd, rs1, imm } => {
212 +
                let value = load(read(m, rs1) + imm as i64 as u64, 2) else {
213 +
                    return Outcome::Faulted;
214 +
                };
215 +
                write(m, rd, sext16(value));
216 +
            },
217 +
            case decode::Instr::Lbu { rd, rs1, imm } => {
218 +
                let value = load(read(m, rs1) + imm as i64 as u64, 1) else {
219 +
                    return Outcome::Faulted;
220 +
                };
221 +
                write(m, rd, value);
222 +
            },
223 +
            case decode::Instr::Lb { rd, rs1, imm } => {
224 +
                let value = load(read(m, rs1) + imm as i64 as u64, 1) else {
225 +
                    return Outcome::Faulted;
226 +
                };
227 +
                write(m, rd, sext8(value));
228 +
            },
229 +
            case decode::Instr::Sh { rs2, rs1, imm } => {
230 +
                if not store(read(m, rs1) + imm as i64 as u64, 2, read(m, rs2)) {
231 +
                    return Outcome::Faulted;
232 +
                }
233 +
            },
234 +
            case decode::Instr::Sb { rs2, rs1, imm } => {
235 +
                if not store(read(m, rs1) + imm as i64 as u64, 1, read(m, rs2)) {
236 +
                    return Outcome::Faulted;
237 +
                }
238 +
            },
239 +
            case decode::Instr::Sd { rs2, rs1, imm } => {
240 +
                if not store(read(m, rs1) + imm as i64 as u64, 8, read(m, rs2)) {
241 +
                    return Outcome::Faulted;
242 +
                }
243 +
            },
244 +
            case decode::Instr::Sw { rs2, rs1, imm } => {
245 +
                if not store(read(m, rs1) + imm as i64 as u64, 4, read(m, rs2)) {
246 +
                    return Outcome::Faulted;
247 +
                }
248 +
            },
249 +
            case decode::Instr::Beq { rs1, rs2, imm } => {
250 +
                if read(m, rs1) == read(m, rs2) {
251 +
                    set next = (pc as i32 + imm / 4) as u32;
252 +
                }
253 +
            },
254 +
            case decode::Instr::Bne { rs1, rs2, imm } => {
255 +
                if read(m, rs1) <> read(m, rs2) {
256 +
                    set next = (pc as i32 + imm / 4) as u32;
257 +
                }
258 +
            },
259 +
            case decode::Instr::Bltu { rs1, rs2, imm } => {
260 +
                if read(m, rs1) < read(m, rs2) {
261 +
                    set next = (pc as i32 + imm / 4) as u32;
262 +
                }
263 +
            },
264 +
            case decode::Instr::Bgeu { rs1, rs2, imm } => {
265 +
                if read(m, rs1) >= read(m, rs2) {
266 +
                    set next = (pc as i32 + imm / 4) as u32;
267 +
                }
268 +
            },
269 +
            case decode::Instr::Jalr { rd, rs1, imm } => {
270 +
                if read(m, rs1) + imm as i64 as u64 <> RETURN {
271 +
                    return Outcome::Faulted;
272 +
                }
273 +
                return Outcome::Returned { value: read(m, rv64::A0) };
274 +
            },
275 +
            case decode::Instr::Ebreak => {
276 +
                return Outcome::Trapped;
277 +
            },
278 +
            else => {
279 +
                return Outcome::Faulted;
280 +
            },
281 +
        }
282 +
        set pc = next;
283 +
    }
284 +
    return Outcome::Faulted;
285 +
}
286 +
287 +
/// Compile a function of `paramCount` data parameters, one instruction, and a return of
288 +
/// `result`, into a package with two data symbols.
289 +
unsafe fn compile(instr: il::Instr, paramCount: u32, result: ?il::Val)
290 +
    -> shared::Package throws (shared::Error) {
291 +
    return try compileBody(&[instr, il::Instr::Ret { val: result }], paramCount,
292 +
        &[il::Layout { size: ELEMENT as u32, alignment: 8, slots: &[] }]);
293 +
}
294 +
295 +
/// Compile a function of `paramCount` data parameters with the instructions `body`, which end
296 +
/// with a return, into a package with two data symbols and the layout table `layouts`.
297 +
unsafe fn compileBody(body: &[il::Instr], paramCount: u32, layouts: *[il::Layout])
298 +
    -> shared::Package throws (shared::Error) {
299 +
    let mut ilArena = alloc::new(&mut IL[..]);
300 +
    let mut params: *mut [il::Param] = &mut [];
301 +
    for n in 0..paramCount {
302 +
        try! params.append(dataParam(n), &mut ilArena);
303 +
    }
304 +
    let mut instrs: *mut [il::Instr] = &mut [];
305 +
    try! instrs.extend(body, &mut ilArena);
306 +
    let mut blocks: *mut [il::Block] = &mut [];
307 +
    try! blocks.append(il::Block {
308 +
        label: il::BlockLabel { base: "b", suffix: nil }, params: &[], instrs: &instrs[..],
309 +
        locs: &[], preds: &[], loopDepth: 0,
310 +
    }, &mut ilArena);
311 +
    let mut funcs: *mut [il::Fn] = &mut [];
312 +
    try! funcs.append(il::Fn {
313 +
        name: "p::f", params: &params[..], returnType: il::ValType::Data(il::Type::W64),
314 +
        isExtern: false, isLeaf: false, blocks: &blocks[..], vals: &[], cases: &[],
315 +
    }, &mut ilArena);
316 +
    let mut fns: *mut [*il::Fn] = &mut [];
317 +
    try! fns.append(&funcs[0], &mut ilArena);
318 +
    let input = binary::Package {
319 +
        symbols: &[], name: "p", dependencies: &[],
320 +
        exports: &[
321 +
            binary::Export { name: "p::f", kind: binary::ExportKind::Function },
322 +
            binary::Export { name: FIRST, kind: binary::ExportKind::Data },
323 +
            binary::Export { name: SECOND, kind: binary::ExportKind::Data },
324 +
        ],
325 +
        entry: nil,
326 +
        program: il::Program {
327 +
            data: &[
328 +
                il::Data { name: FIRST, size: 8, alignment: 8, layout: 0, readOnly: false,
329 +
                    isZeroInit: true, values: &[] },
330 +
                il::Data { name: SECOND, size: 8, alignment: 8, layout: 0, readOnly: false,
331 +
                    isZeroInit: true, values: &[] },
332 +
            ],
333 +
            fns: &fns[..],
334 +
            layouts,
335 +
            signatures: &[],
336 +
        },
337 +
    };
338 +
    let arena = alloc::new(&mut tests::ARENA[..]);
339 +
    let mut scratch = alloc::new(&mut tests::SCRATCH[..]);
340 +
    return try shared::compile(&input, SLOT, 0x180000000, &[],
341 +
        shared::Storage {
342 +
            data: &mut DATA[..], symbols: &mut SYMBOLS[..], exports: &mut EXPORTS[..],
343 +
            template: &mut TEMPLATE[..], relocations: &mut RELOCS[..],
344 +
            stack: rv64::StackCheck::Off,
345 +
        }, arena, &mut scratch);
346 +
}
347 +
348 +
/// Address of a data symbol of the package once the package is instantiated at the base.
349 +
fn address(package: &shared::Package, name: *[u8]) -> u64 {
350 +
    match shared::lookup(package.exports, name) {
351 +
        case shared::Target::Data(location) => return PACKAGE_BASE + location.offset as u64,
352 +
        else => panic "address: not a data symbol",
353 +
    }
354 +
}
355 +
356 +
/// The result of running a function that writes a slice object.
357 +
record Built {
358 +
    /// How the run ended.
359 +
    outcome: Outcome,
360 +
    /// Data pointer of the slice object.
361 +
    ptr: u64,
362 +
    /// Length of the slice object.
363 +
    len: u64,
364 +
    /// Capacity of the slice object.
365 +
    cap: u64,
366 +
}
367 +
368 +
/// A data parameter of a function.
369 +
fn dataParam(n: u32) -> il::Param {
370 +
    return il::Param { value: il::Reg { n }, type: il::ValType::Data(il::Type::W64) };
371 +
}
372 +
373 +
/// Reset the modeled memory, and set up the registers for a call with the arguments.
374 +
unsafe fn boot(args: &[u64]) -> Machine {
375 +
    for i in 0..MEMORY_SIZE {
376 +
        set MEMORY[i] = 0;
377 +
    }
378 +
    assert store(TABLE + SLOT as u64 * 8, 8, PACKAGE_BASE);
379 +
    let mut m = Machine { regs: [0; 32], steps: 0 };
380 +
    write(&mut m, rv64::GP, TABLE);
381 +
    write(&mut m, rv64::SP, STACK);
382 +
    write(&mut m, rv64::RA, RETURN);
383 +
    for arg, i in args {
384 +
        write(&mut m, rv64::ARG_REGS[i], arg);
385 +
    }
386 +
    return m;
387 +
}
388 +
389 +
/// The slice object at [`RESULT`] after a run.
390 +
unsafe fn built(outcome: Outcome) -> Built {
391 +
    let ptr = load(RESULT, 8) else panic "built: result outside memory";
392 +
    let len = load(RESULT + 8, 4) else panic "built: result outside memory";
393 +
    let cap = load(RESULT + 12, 4) else panic "built: result outside memory";
394 +
    return Built { outcome, ptr, len, cap };
395 +
}
396 +
397 +
/// Run `slice.new` with the bounds on an array of `count` elements at [`POINTER`].
398 +
unsafe fn sliceNew(first: il::Val, end: il::Val, count: u32) -> Built
399 +
    throws (testing::TestError) {
400 +
    let package = try compile(il::Instr::SliceNew {
401 +
        dst: il::Reg { n: 0 }, base: il::Val::Reg(il::Reg { n: 1 }), layout: 0, count,
402 +
        start: first, end,
403 +
    }, 2, nil) catch {
404 +
        throw testing::TestError::Failed;
405 +
    };
406 +
    let mut m = boot(&[RESULT, POINTER]);
407 +
    return built(run(package.code, &mut m));
408 +
}
409 +
410 +
/// Run `slice.sub` with the bounds on a source slice at [`POINTER`] of `len` elements and
411 +
/// ten more of capacity.
412 +
unsafe fn sliceSub(first: il::Val, end: il::Val, len: u32) -> Built
413 +
    throws (testing::TestError) {
414 +
    let package = try compile(il::Instr::SliceSub {
415 +
        dst: il::Reg { n: 0 }, src: il::Reg { n: 1 }, layout: 0, start: first, end,
416 +
    }, 2, nil) catch {
417 +
        throw testing::TestError::Failed;
418 +
    };
419 +
    let mut m = boot(&[RESULT, SOURCE]);
420 +
    assert store(SOURCE, 8, POINTER);
421 +
    assert store(SOURCE + 8, 4, len as u64);
422 +
    assert store(SOURCE + 12, 4, len as u64 + 10);
423 +
    return built(run(package.code, &mut m));
424 +
}
425 +
426 +
/// Run `elem` of an array of `count` elements at [`POINTER`]. With `inRegister`, the index is
427 +
/// passed in a register instead of as an immediate.
428 +
unsafe fn elem(index: i64, count: u32, inRegister: bool) -> Outcome
429 +
    throws (testing::TestError) {
430 +
    let mut operand = il::Val::Imm(index);
431 +
    if inRegister {
432 +
        set operand = il::Val::Reg(il::Reg { n: 1 });
433 +
    }
434 +
    let package = try compile(il::Instr::Elem {
435 +
        dst: il::Reg { n: 2 }, base: il::Reg { n: 0 }, index: operand, layout: 0, count,
436 +
    }, 2, il::Val::Reg(il::Reg { n: 2 })) catch {
437 +
        throw testing::TestError::Failed;
438 +
    };
439 +
    let mut m = boot(&[POINTER, index as u64]);
440 +
    return run(package.code, &mut m);
441 +
}
442 +
443 +
/// Whether a run trapped.
444 +
fn trapped(outcome: Outcome) -> bool {
445 +
    match outcome {
446 +
        case Outcome::Trapped => return true,
447 +
        else => return false,
448 +
    }
449 +
}
450 +
451 +
/// Whether a run returned `value`.
452 +
fn returned(outcome: Outcome, value: u64) -> bool {
453 +
    match outcome {
454 +
        case Outcome::Returned { value: v } => return v == value,
455 +
        else => return false,
456 +
    }
457 +
}
458 +
459 +
/// Whether a run returned, so that its slice object is complete.
460 +
fn finished(outcome: Outcome) -> bool {
461 +
    match outcome {
462 +
        case Outcome::Returned { .. } => return true,
463 +
        else => return false,
464 +
    }
465 +
}
466 +
467 +
/// Check that a run built a slice that starts at element `first` of the array at [`POINTER`].
468 +
fn expectBuilt(b: Built, first: u64, len: u64, cap: u64) throws (testing::TestError) {
469 +
    try testing::expect(finished(b.outcome));
470 +
    try testing::expect(b.ptr == POINTER + first * ELEMENT and b.len == len and b.cap == cap);
471 +
}
472 +
473 +
/// Check that a run of an instruction with a violated bound trapped.
474 +
fn expectTrap(b: Built) throws (testing::TestError) {
475 +
    try testing::expect(trapped(b.outcome));
476 +
}
477 +
478 +
/// Addresses of the two data symbols, which are indexes that the slice instructions accept.
479 +
record Addresses {
480 +
    /// Address of [`FIRST`].
481 +
    first: u64,
482 +
    /// Address of [`SECOND`].
483 +
    second: u64,
484 +
}
485 +
486 +
/// Compile a function without code to find the addresses of the data symbols.
487 +
unsafe fn addresses() -> Addresses throws (testing::TestError) {
488 +
    let package = try compile(il::Instr::Ret { val: nil }, 0, nil) catch {
489 +
        throw testing::TestError::Failed;
490 +
    };
491 +
    return Addresses { first: address(&package, FIRST), second: address(&package, SECOND) };
492 +
}
493 +
494 +
/// The bounds of a slice are 32-bit values, and a data symbol is accepted as one: its low 32
495 +
/// bits count. Every pair of operands builds the slice, or traps when a bound is violated.
496 +
@test unsafe fn sliceNewBounds() throws (testing::TestError) {
497 +
    let at = try addresses();
498 +
    let one = il::Val::DataSym(FIRST);
499 +
    let two = il::Val::DataSym(SECOND);
500 +
    try expectBuilt(try sliceNew(one, two, 64), at.first, at.second - at.first, 64 - at.first);
501 +
    try expectBuilt(try sliceNew(il::Val::Imm(2), two, 64), 2, at.second - 2, 62);
502 +
    try expectBuilt(try sliceNew(one, il::Val::Imm(40), 64), at.first, 40 - at.first,
503 +
        64 - at.first);
504 +
    try expectBuilt(try sliceNew(il::Val::Imm(0), two, 64), 0, at.second, 64);
505 +
    try expectBuilt(try sliceNew(two, two, 64), at.second, 0, 64 - at.second);
506 +
    try expectTrap(try sliceNew(one, two, 20));
507 +
    try expectTrap(try sliceNew(two, one, 64));
508 +
    try expectTrap(try sliceNew(one, il::Val::Imm(8), 64));
509 +
}
510 +
511 +
/// A data symbol is accepted as a bound of `slice.sub`, as of `slice.new`.
512 +
@test unsafe fn sliceSubBounds() throws (testing::TestError) {
513 +
    let at = try addresses();
514 +
    let one = il::Val::DataSym(FIRST);
515 +
    let two = il::Val::DataSym(SECOND);
516 +
    try expectBuilt(try sliceSub(one, two, 40), at.first, at.second - at.first, 50 - at.first);
517 +
    try expectBuilt(try sliceSub(il::Val::Imm(2), two, 40), 2, at.second - 2, 48);
518 +
    try expectBuilt(try sliceSub(one, il::Val::Imm(40), 40), at.first, 40 - at.first,
519 +
        50 - at.first);
520 +
    try expectBuilt(try sliceSub(il::Val::Imm(0), two, 40), 0, at.second, 50);
521 +
    try expectBuilt(try sliceSub(two, two, 40), at.second, 0, 50 - at.second);
522 +
    try expectTrap(try sliceSub(one, two, 20));
523 +
    try expectTrap(try sliceSub(two, one, 40));
524 +
    try expectTrap(try sliceSub(one, il::Val::Imm(8), 40));
525 +
}
526 +
527 +
/// An immediate index is an unsigned 32-bit value, like the same value in a register: only its
528 +
/// low 32 bits select the element, and an index at or above the count traps.
529 +
@test unsafe fn elemImmediateIndex() throws (testing::TestError) {
530 +
    let indexes: [i64; 10] = [
531 +
        0, 7, 8, 0xffffffff, 0x100000000, 0x100000001, 0x100000008, -1, -8, 0x7fffffff00000003,
532 +
    ];
533 +
    let elements: [u64; 10] = [0, 7, 8, 0xffffffff, 0, 1, 8, 0xffffffff, 0xfffffff8, 3];
534 +
    for i in 0..indexes.len {
535 +
        let index = indexes[i];
536 +
        let element = elements[i];
537 +
        for form in 0..2 {
538 +
            let outcome = try elem(index, 8, form == 1);
539 +
            if element < 8 {
540 +
                try testing::expect(returned(outcome, POINTER + element * ELEMENT));
541 +
            } else {
542 +
                try testing::expect(trapped(outcome));
543 +
            }
544 +
        }
545 +
    }
546 +
    // No index is below a count of zero.
547 +
    for form in 0..2 {
548 +
        let zero = try elem(0, 0, form == 1);
549 +
        let wide = try elem(0x100000000, 0, form == 1);
550 +
        try testing::expect(trapped(zero) and trapped(wide));
551 +
    }
552 +
}
553 +
554 +
/// How a blit check passes its operands.
555 +
union BlitMode: Copy {
556 +
    /// The addresses are in registers.
557 +
    Registers,
558 +
    /// Many values are live across the copy, so the addresses are spilled to the stack.
559 +
    Spilled,
560 +
    /// The destination is the source.
561 +
    Aliased,
562 +
}
563 +
564 +
/// The byte of the copied object at `index`.
565 +
fn pattern(index: u64) -> u8 {
566 +
    return ((index * 7 + 3) & 0xff) as u8;
567 +
}
568 +
569 +
/// Compile a function that copies an object of `size` bytes and `boundary` bytes of alignment
570 +
/// from its second parameter to its first, and returns the sum of the addresses. With spilled
571 +
/// operands, it also returns the sum of the values that are live across the copy.
572 +
unsafe fn blitPackage(size: u32, boundary: u32, mode: BlitMode)
573 +
    -> shared::Package throws (shared::Error) {
574 +
    let mut arena = alloc::new(&mut BODY[..]);
575 +
    let mut body: *mut [il::Instr] = &mut [];
576 +
    let mut next: u32 = 2;
577 +
    let mut source: u32 = 1;
578 +
    if mode == BlitMode::Aliased {
579 +
        set source = 0;
580 +
    }
581 +
    if mode == BlitMode::Spilled {
582 +
        for i in 0..LIVE {
583 +
            try! body.append(il::Instr::Copy {
584 +
                dst: il::Reg { n: next + i }, val: il::Val::Imm(i as i64 + 1),
585 +
            }, &mut arena);
586 +
        }
587 +
        set next += LIVE;
588 +
    }
589 +
    try! body.append(il::Instr::Blit {
590 +
        dst: il::Reg { n: 0 }, src: il::Reg { n: source }, layout: 0,
591 +
    }, &mut arena);
592 +
    let mut sum = il::Val::Reg(il::Reg { n: 0 });
593 +
    if mode == BlitMode::Spilled {
594 +
        // Use each live value several times, so that spilling the addresses is cheaper.
595 +
        for _ in 0..3 {
596 +
            for i in 0..LIVE {
597 +
                try! body.append(il::Instr::BinOp {
598 +
                    op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: next },
599 +
                    a: il::Val::Reg(il::Reg { n: 2 + i }), b: il::Val::Reg(il::Reg { n: 2 + i }),
600 +
                }, &mut arena);
601 +
                try! body.append(il::Instr::BinOp {
602 +
                    op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: next + 1 },
603 +
                    a: sum, b: il::Val::Reg(il::Reg { n: next }),
604 +
                }, &mut arena);
605 +
                set sum = il::Val::Reg(il::Reg { n: next + 1 });
606 +
                set next += 2;
607 +
            }
608 +
        }
609 +
    }
610 +
    try! body.append(il::Instr::BinOp {
611 +
        op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: next },
612 +
        a: sum, b: il::Val::Reg(il::Reg { n: 1 }),
613 +
    }, &mut arena);
614 +
    try! body.append(il::Instr::Ret { val: il::Val::Reg(il::Reg { n: next }) }, &mut arena);
615 +
    set COPIED[0] = il::Layout { size, alignment: boundary, slots: &[] };
616 +
    return try compileBody(&body[..], 2, (&COPIED[..]) as *[il::Layout]);
617 +
}
618 +
619 +
/// The byte at `address` of the modeled memory.
620 +
unsafe fn byteAt(address: u64) -> u8 {
621 +
    let value = load(address, 1) else panic "byteAt: outside memory";
622 +
    return value as u8;
623 +
}
624 +
625 +
/// The base register of a store instruction, or nil for any other instruction.
626 +
fn storeBase(word: u32) -> ?gen::Reg {
627 +
    match decode::decode(word) {
628 +
        case decode::Instr::Sd { rs1, .. } => return rs1,
629 +
        case decode::Instr::Sw { rs1, .. } => return rs1,
630 +
        case decode::Instr::Sh { rs1, .. } => return rs1,
631 +
        case decode::Instr::Sb { rs1, .. } => return rs1,
632 +
        else => return nil,
633 +
    }
634 +
}
635 +
636 +
/// Whether the code reloads both spilled operands from the stack before its first store to
637 +
/// other memory, which is the first store of the copy.
638 +
fn reloadsOperands(code: *[u32]) -> bool {
639 +
    let mut destination = false;
640 +
    let mut source = false;
641 +
    for word in code {
642 +
        if let base = storeBase(word) {
643 +
            if *base <> *rv64::SP {
644 +
                return destination and source;
645 +
            }
646 +
        } else if let case decode::Instr::Ld { rd, rs1, .. } = decode::decode(word) {
647 +
            if *rs1 == *rv64::SP {
648 +
                set destination = destination or *rd == *rv64::SCRATCH2;
649 +
                set source = source or *rd == *rv64::SCRATCH1;
650 +
            }
651 +
        }
652 +
    }
653 +
    return false;
654 +
}
655 +
656 +
/// Copy `size` bytes between objects with `boundary` bytes of alignment in the given mode, and
657 +
/// check the result: the destination holds the source, the bytes around it and the source are
658 +
/// unchanged, the function returns the sum of the addresses that it was given, and, without
659 +
/// spilled operands, the copy is not slower than a loop over its units.
660 +
unsafe fn expectBlit(size: u32, boundary: u32, mode: BlitMode) throws (testing::TestError) {
661 +
    let package = try blitPackage(size, boundary, mode) catch {
662 +
        throw testing::TestError::Failed;
663 +
    };
664 +
    let target = TARGET_AREA + GUARD;
665 +
    let source = SOURCE_AREA + GUARD;
666 +
    let mut m = boot(&[target, source]);
667 +
    for i in 0..size as u64 + 2 * GUARD {
668 +
        set MEMORY[(SOURCE_AREA - MEMORY_BASE + i) as u32] = UNTOUCHED;
669 +
        set MEMORY[(TARGET_AREA - MEMORY_BASE + i) as u32] = UNTOUCHED;
670 +
    }
671 +
    for i in 0..size as u64 {
672 +
        set MEMORY[(source - MEMORY_BASE + i) as u32] = pattern(i);
673 +
    }
674 +
    if mode == BlitMode::Aliased {
675 +
        for i in 0..size as u64 {
676 +
            set MEMORY[(target - MEMORY_BASE + i) as u32] = pattern(i);
677 +
        }
678 +
    }
679 +
    let outcome = run(package.code, &mut m);
680 +
    // The sum of the values that are live across the copy: each of them, doubled, three times.
681 +
    let mut live: u64 = 0;
682 +
    if mode == BlitMode::Spilled {
683 +
        set live = 3 * LIVE as u64 * (LIVE as u64 + 1);
684 +
    }
685 +
    try testing::expect(returned(outcome, target + source + live));
686 +
    for i in 0..size as u64 {
687 +
        try testing::expect(byteAt(target + i) == pattern(i));
688 +
        if mode <> BlitMode::Aliased {
689 +
            try testing::expect(byteAt(source + i) == pattern(i));
690 +
        }
691 +
    }
692 +
    for i in 0..GUARD {
693 +
        try testing::expect(byteAt(TARGET_AREA + i) == UNTOUCHED);
694 +
        try testing::expect(byteAt(target + size as u64 + i) == UNTOUCHED);
695 +
        if mode <> BlitMode::Aliased {
696 +
            try testing::expect(byteAt(SOURCE_AREA + i) == UNTOUCHED);
697 +
            try testing::expect(byteAt(source + size as u64 + i) == UNTOUCHED);
698 +
        }
699 +
    }
700 +
    if mode == BlitMode::Spilled and size > 0 {
701 +
        try testing::expect(reloadsOperands(package.code));
702 +
    }
703 +
    if mode == BlitMode::Registers {
704 +
        // A copy is a load and a store per unit, or a loop of five instructions per unit.
705 +
        let unit = boundary;
706 +
        let units = (size + unit - 1) / unit;
707 +
        try testing::expect(m.steps <= 5 * units + 16);
708 +
    }
709 +
}
710 +
711 +
/// The blit checks copy every size below this one.
712 +
constant SMALL_SIZES: u32 = 41;
713 +
/// Larger sizes of the blit checks. They include copies that need a loop, with and without a
714 +
/// tail, and a copy longer than an immediate offset reaches.
715 +
constant LARGE_SIZES: [u32; 12] = [48, 56, 64, 65, 72, 77, 96, 128, 256, 520, 2051, 2056];
716 +
/// Alignments of the objects of the blit checks.
717 +
constant BOUNDARIES: [u32; 4] = [1, 2, 4, 8];
718 +
/// The modes of the blit checks.
719 +
constant MODES: [BlitMode; 3] = [BlitMode::Registers, BlitMode::Spilled, BlitMode::Aliased];
720 +
721 +
/// A copy moves exactly the bytes of the layout, with units as wide as its alignment allows,
722 +
/// for every size from zero to forty and some larger ones, also sizes that the alignment does
723 +
/// not divide. It leaves the addresses intact, also when they are spilled or equal.
724 +
@test unsafe fn blitCopies() throws (testing::TestError) {
725 +
    for boundary in BOUNDARIES {
726 +
        for mode in MODES {
727 +
            for size in 0..SMALL_SIZES {
728 +
                try expectBlit(size, boundary, mode);
729 +
            }
730 +
            for large in LARGE_SIZES {
731 +
                try expectBlit(large, boundary, mode);
732 +
            }
733 +
        }
734 +
    }
735 +
}
std.lib +2 -0
55 55
lib/std/lang/gen/types.rad
56 56
lib/std/lang/gen/regalloc.rad
57 57
lib/std/lang/gen/regalloc/liveness.rad
58 58
lib/std/lang/gen/regalloc/spill.rad
59 59
lib/std/lang/gen/regalloc/assign.rad
60 +
lib/std/arch/rv64/shared.rad
61 +
lib/std/arch/rv64/shared/catalog.rad
60 62
lib/std/arch/rv64/atomics.rad
std.lib.test +2 -0
19 19
lib/std/lang/gen/bitset/tests.rad
20 20
lib/std/lang/il/binary/tests.rad
21 21
lib/std/lang/il/binary/tests/decode.rad
22 22
lib/std/lang/il/verify/tests.rad
23 23
lib/std/arch/rv64/image/tests.rad
24 +
lib/std/arch/rv64/shared/tests.rad
25 +
lib/std/arch/rv64/shared/tests/run.rad
24 26
lib/std/arch/rv64/tests/bounds.rad
25 27
lib/std/arch/rv64/tests/atomics.rad
test/Makefile +7 -1
1 1
# Compiler and standard library test suites. Run from the repository root.
2 2
# JOBS controls workers inside test scripts.
3 3
JOBS ?= 1
4 4
export JOBS
5 5
TESTS := seed-test std-test bin-test module-test \
6 -
    native-test verify-test
6 +
    native-test shared-test verify-test
7 7
8 8
test: $(TESTS)
9 9
.PHONY: test $(TESTS) smp-test cycles-test clean-std-test clean-bin-test
10 10
11 11
# Hosted test tools share one compiler recipe.
12 12
# Runner binary: the lowering IL checker.
13 13
TEST_TOOL_SRC := test/runner.rad test/native/build.rad \
14 +
    test/shared/build.rad \
14 15
    test/verify/check.rad
15 16
TEST_TOOLS := $(TEST_TOOL_SRC:.rad=.rv64)
16 17
17 18
$(TEST_TOOLS): %.rv64: %.rad $(STD_LIB) $(RAD_BIN) test/Makefile
18 19
	@$(RADIANCE) $(STD) -pkg tool -mod $< -entry tool -o $@
42 43
	@$${RAD_EMULATOR:-emulator} -machine -max-steps=1000000 -run $<
43 44
44 45
$(BIN_DIR)/native.fixture.rv64: test/native/build.rv64 test/Makefile
45 46
	@$(HOSTED) $< -- $@
46 47
48 +
# Shared package execution
49 +
shared-test: test/shared/build.rv64
50 +
	@sh test/shared/run
51 +
	@sh test/shared/errors/run
52 +
47 53
# Binary Tests
48 54
49 55
# Only tests with `//! returns:` are compiled to binaries and executed.
50 56
BIN_TEST_SRC := $(shell grep -l '^//! returns:' \
51 57
    $(wildcard test/tests/*.rad test/tests/*.ras))
test/shared/app.rad added +18 -0
1 +
//! Shared entry with callbacks, initialized data pointers, and private counters.
2 +
3 +
use support;
4 +
5 +
/// Callback table contains a linked dependency function address.
6 +
static CALLBACKS: [fn() -> i32; 1] = [support::read];
7 +
/// The slice contains a pointer into this domain's string storage.
8 +
static TEXT: *[u8] = "xy";
9 +
/// Number of calls in the current domain.
10 +
unsafe static calls: i32 = 0;
11 +
12 +
/// Read dependency state through calls and data, then advance private state.
13 +
@default export fn main() -> i32 {
14 +
    unsafe {
15 +
        set calls += 1;
16 +
        return support::read() + CALLBACKS[0]() + support::value + TEXT[0] as i32 + calls;
17 +
    }
18 +
}
test/shared/build.rad added +233 -0
1 +
//! Build a machine fixture from separately decoded and linked packages.
2 +
3 +
use std::sys;
4 +
use std::sys::unix;
5 +
use std::mem;
6 +
use std::lang::alloc;
7 +
use std::lang::il::binary;
8 +
use std::lang::il::binary::program;
9 +
use std::lang::gen::data;
10 +
use std::lang::gen::labels;
11 +
use std::collections::dict;
12 +
use std::arch::rv64;
13 +
use std::arch::rv64::shared;
14 +
use std::arch::rv64::emit;
15 +
use std::arch::rv64::encode;
16 +
use std::arch::rv64::image;
17 +
use std::lang::gen::regalloc::liveness;
18 +
19 +
/// Native fixture entry trampoline address.
20 +
constant CODE_BASE: u64 = 0x80010000;
21 +
/// First linked package code address.
22 +
constant PACKAGE_CODE_BASE: u64 = 0x80011000;
23 +
/// Bytes reserved for each code slot.
24 +
constant CODE_SLOT_BYTES: u32 = 4096;
25 +
/// Instructions reserved for each code slot.
26 +
constant CODE_SLOT_WORDS: u32 = CODE_SLOT_BYTES / rv64::INSTR_SIZE as u32;
27 +
/// Native fixture private state segment base.
28 +
constant RW_BASE: u64 = 0x80020000;
29 +
/// Bytes reserved for one private package graph.
30 +
constant GRAPH_BYTES: u32 = 32768;
31 +
/// Bytes reserved for one package private state.
32 +
constant PACKAGE_STATE_BYTES: u32 = 16384;
33 +
/// Byte offset of package base tables in the state segment.
34 +
constant TABLES_OFFSET: u32 = 65536;
35 +
/// Bytes in one graph package base table.
36 +
constant TABLE_BYTES: u32 = shared::MAX_PACKAGES * rv64::DWORD_SIZE as u32;
37 +
/// Machine completion device address.
38 +
constant FINISH_ADDRESS: i64 = 0x10001000;
39 +
/// Completion value for a successful machine test.
40 +
constant FINISH_PASS: i64 = 0x5555;
41 +
/// Completion value for a failed machine test.
42 +
constant FINISH_FAIL: i64 = 0x13333;
43 +
44 +
/// Number of elements reserved for code storage.
45 +
constant CODE_CAPACITY: u32 = 16777216;
46 +
/// Number of elements reserved for scratch storage.
47 +
constant SCRATCH_CAPACITY: u32 = 16777216;
48 +
/// Number of elements reserved for decode storage.
49 +
constant DECODE_CAPACITY: u32 = 1048576;
50 +
/// Number of elements reserved for input storage.
51 +
constant INPUT_CAPACITY: u32 = 65536;
52 +
/// Number of elements reserved for templates storage.
53 +
constant TEMPLATES_CAPACITY: u32 = 16384;
54 +
/// Number of elements reserved for symbols storage.
55 +
constant SYMBOLS_CAPACITY: u32 = 128;
56 +
/// Number of elements reserved for exports storage.
57 +
constant EXPORTS_CAPACITY: u32 = 128;
58 +
/// Number of elements reserved for relocs storage.
59 +
constant RELOCS_CAPACITY: u32 = 128;
60 +
/// Number of elements reserved for data storage.
61 +
constant DATA_CAPACITY: u32 = 128;
62 +
/// Number of elements reserved for text storage.
63 +
constant TEXT_CAPACITY: u32 = 4096;
64 +
/// Number of elements reserved for state storage.
65 +
constant STATE_CAPACITY: u32 = 69632;
66 +
67 +
/// Separate code arenas preserve the boot catalog's package records.
68 +
unsafe static CODE: [[u8; CODE_CAPACITY]; 3] = undefined;
69 +
/// Reusable function workspace.
70 +
static SCRATCH: [u8; SCRATCH_CAPACITY] = [0; SCRATCH_CAPACITY];
71 +
/// Decoder arena shared by the two input packages.
72 +
static DECODE: [u8; DECODE_CAPACITY] = [0; DECODE_CAPACITY];
73 +
/// Input file buffer.
74 +
static INPUT: [u8; INPUT_CAPACITY] = [0; INPUT_CAPACITY];
75 +
/// Persistent private templates.
76 +
unsafe static TEMPLATES: [[u8; TEMPLATES_CAPACITY]; 2] = undefined;
77 +
/// Local symbol workspaces.
78 +
unsafe static SYMBOLS: [[shared::Symbol; SYMBOLS_CAPACITY]; 2] = undefined;
79 +
/// Package exports.
80 +
unsafe static EXPORTS: [[shared::Symbol; EXPORTS_CAPACITY]; 2] = undefined;
81 +
/// Private pointer fixups.
82 +
unsafe static RELOCS: [[shared::Relocation; RELOCS_CAPACITY]; 2] = undefined;
83 +
/// Data layout workspaces.
84 +
unsafe static DATA: [[data::DataSym; DATA_CAPACITY]; 2] = undefined;
85 +
/// Native instruction image, with fixed package placements.
86 +
static TEXT: [u32; TEXT_CAPACITY] = [0; TEXT_CAPACITY];
87 +
/// Two private graphs followed by their package-state tables.
88 +
static STATE: [u8; STATE_CAPACITY] = [0; STATE_CAPACITY];
89 +
90 +
/// Decode trusted package IL into persistent storage.
91 +
unsafe fn load(path: *[u8], arena: &mut alloc::Arena) -> binary::Package {
92 +
    let length = unix::readFile(path, &mut INPUT[..]) else panic "load: missing package";
93 +
    return try! program::decode(&INPUT[..length], arena,
94 +
        binary::Limits { registers: liveness::MAX_SSA_REGS,
95 +
        blocks: labels::MAX_BLOCKS_PER_FN });
96 +
}
97 +
98 +
/// Compile through the same entry used for boot catalogs and runtime loading.
99 +
unsafe fn compile(input: &binary::Package, index: u32, imports: &[shared::Symbol])
100 +
    -> shared::Package throws (shared::Error) {
101 +
    let arena = alloc::new(&mut CODE[index][..]);
102 +
    let mut scratch = alloc::new(&mut SCRATCH[..]);
103 +
    return try shared::compile(input, index, PACKAGE_CODE_BASE +
104 +
        index as u64 * CODE_SLOT_BYTES as u64, imports,
105 +
        shared::Storage {
106 +
            data: &mut DATA[index][..], symbols: &mut SYMBOLS[index][..],
107 +
                exports: &mut EXPORTS[index][..],
108 +
            template: &mut TEMPLATES[index][..], relocations: &mut RELOCS[index][..],
109 +
            stack: rv64::StackCheck::Off,
110 +
        }, arena, &mut scratch);
111 +
}
112 +
113 +
/// Append a call result check and return its branch patch location.
114 +
fn check(e: &mut emit::Emitter, entry: u64, expected: i64) -> u32 {
115 +
    let delta = entry - (CODE_BASE +
116 +
        e.code.len as u64 * rv64::INSTR_SIZE as u64);
117 +
    try! emit::emit(e, encode::jal(rv64::RA, delta as i32));
118 +
    try! emit::loadImm(e, rv64::T0, expected);
119 +
    let index = e.code.len;
120 +
    try! emit::emit(e, encode::nop());
121 +
    return index;
122 +
}
123 +
124 +
/// Emit an explicit platform completion status.
125 +
fn finish(e: &mut emit::Emitter, status: i64) {
126 +
    try! emit::loadImm(e, rv64::T0, FINISH_ADDRESS);
127 +
    try! emit::loadImm(e, rv64::T1, status);
128 +
    try! emit::emit(e, encode::sw(rv64::T1, rv64::T0, 0));
129 +
    try! emit::emit(e, encode::ebreak());
130 +
}
131 +
132 +
/// Verify catalog linking, then execute one code copy against two private graphs.
133 +
@default unsafe fn main(env: *sys::Env) -> i32 {
134 +
    // A fifth argument selects the typed-catch scenario, whose entry returns 222.
135 +
    assert env.args.len == 4 or env.args.len == 5;
136 +
    let catchScenario = env.args.len == 5;
137 +
    let mut decoder = alloc::new(&mut DECODE[..]);
138 +
    let supportInput = load(env.args[1], &mut decoder);
139 +
    let appInput = load(env.args[2], &mut decoder);
140 +
    let support = try! compile(&supportInput, 0, &[]);
141 +
    let mut far: [shared::Symbol; EXPORTS_CAPACITY] = undefined;
142 +
    for symbol, i in support.exports {
143 +
        set far[i] = symbol;
144 +
        match symbol.target {
145 +
            case shared::Target::Function(_) => {
146 +
                set far[i].target = shared::Target::Function(0x180000000);
147 +
            },
148 +
            else => {},
149 +
        }
150 +
    }
151 +
    let mut rejected = false;
152 +
    try compile(&appInput, 1, &far[..support.exports.len]) catch err {
153 +
        assert err == shared::Error::Range;
154 +
        set rejected = true;
155 +
    };
156 +
    assert rejected;
157 +
    let app = try! compile(&appInput, 1, support.exports);
158 +
    let catalog = [support, app];
159 +
    assert catchScenario or app.relocations.len > 0;
160 +
    let entry = app.entry else panic "main: missing entry";
161 +
    for i in 0..STATE.len {
162 +
        set STATE[i] = 0;
163 +
    }
164 +
    for domain in 0..2 {
165 +
        let mut bases: [u64; shared::MAX_PACKAGES] = [0; shared::MAX_PACKAGES];
166 +
        for package, i in &catalog[..] {
167 +
            let offset = domain * GRAPH_BYTES + i * PACKAGE_STATE_BYTES;
168 +
            set bases[i] = RW_BASE + offset as u64;
169 +
        }
170 +
        for package, i in &catalog[..] {
171 +
            let offset = domain * GRAPH_BYTES + i * PACKAGE_STATE_BYTES;
172 +
            try! shared::instantiate(&package, &bases[..],
173 +
                &mut STATE[offset..offset + PACKAGE_STATE_BYTES]);
174 +
        }
175 +
        let table = @sliceOf(&bases[0] as *unsafe u8,
176 +
            @sizeOf([u64; shared::MAX_PACKAGES]));
177 +
        let tableOffset: u32 = TABLES_OFFSET + domain * TABLE_BYTES;
178 +
        try! mem::copy(&mut STATE[tableOffset..], table);
179 +
    }
180 +
    let mut arena = alloc::new(&mut CODE[2][..]);
181 +
    let mut blockOffsets: *mut [i32] = &mut [];
182 +
    try! blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, &mut arena);
183 +
    let mut funcEntries: *mut [dict::Entry] = &mut [];
184 +
    try! funcEntries.resize(labels::FUNC_TABLE_SIZE, dict::Entry { key: &[], value: 0 },
185 +
        &mut arena);
186 +
    let mut e = try! emit::emitter(arena, false, labels::fresh(blockOffsets, funcEntries));
187 +
    try! emit::loadImm(&mut e, rv64::GP, (RW_BASE +
188 +
        TABLES_OFFSET as u64) as i64);
189 +
    let first = check(&mut e, entry, 222 if catchScenario else 142);
190 +
    let second = check(&mut e, entry, 222 if catchScenario else 143);
191 +
    try! emit::loadImm(&mut e, rv64::GP, (RW_BASE + TABLES_OFFSET as u64 +
192 +
        TABLE_BYTES as u64) as i64);
193 +
    let other = check(&mut e, entry, 222 if catchScenario else 142);
194 +
    try! emit::loadImm(&mut e, rv64::GP, (RW_BASE +
195 +
        TABLES_OFFSET as u64) as i64);
196 +
    let resumed = check(&mut e, entry, 222 if catchScenario else 144);
197 +
    finish(&mut e, FINISH_PASS);
198 +
    let failure = e.code.len;
199 +
    finish(&mut e, FINISH_FAIL);
200 +
    for index in &[first, second, other, resumed] {
201 +
        emit::patch(&mut e, index, encode::bne(rv64::A0, rv64::T0,
202 +
            (failure - index) as i32 * rv64::INSTR_SIZE));
203 +
    }
204 +
    for i in 0..TEXT.len {
205 +
        set TEXT[i] = encode::nop();
206 +
    }
207 +
    assert e.code.len <= CODE_SLOT_WORDS;
208 +
    for word, i in &e.code[..] {
209 +
        set TEXT[i] = word;
210 +
    }
211 +
    for package, i in &catalog[..] {
212 +
        assert package.code.len <= CODE_SLOT_WORDS;
213 +
        for word, j in package.code {
214 +
            set TEXT[CODE_SLOT_WORDS + i * CODE_SLOT_WORDS + j] = word;
215 +
        }
216 +
    }
217 +
    let header = try! image::header(image::Layout {
218 +
        code: image::Segment { address: CODE_BASE, size: TEXT_CAPACITY * rv64::INSTR_SIZE as u32 },
219 +
        roData: image::Segment { address: 0, size: 0 },
220 +
        rwData: image::Segment { address: RW_BASE, size: STATE.len },
221 +
        rwTail: 0,
222 +
    });
223 +
    let fd = unix::openOpts(env.args[3],
224 +
        unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC),
225 +
            unix::FILE_MODE);
226 +
    assert fd >= 0;
227 +
    let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd, @sliceOf(&TEXT[0] as *u8,
228 +
        TEXT_CAPACITY * rv64::INSTR_SIZE as u32)) and unix::writeAll(fd,
229 +
            &STATE[..]);
230 +
    let closed = unix::close(fd) == 0;
231 +
    assert written and closed;
232 +
    return 0;
233 +
}
test/shared/errors/app.rad added +13 -0
1 +
//! Entry whose typed catch must select the handler of the thrown error type.
2 +
3 +
use support;
4 +
5 +
/// Return 222 when the `Second` handler runs and 111 when the `First` handler runs.
6 +
@default export fn main() -> i32 {
7 +
    try support::second() catch err as support::First {
8 +
        return 111;
9 +
    } catch err as support::Second {
10 +
        return 222;
11 +
    };
12 +
    return 0;
13 +
}
test/shared/errors/noise.rad added +9 -0
1 +
//! Unrelated package that throws an error before `support` is lowered.
2 +
3 +
/// Error type that is unrelated to the other packages.
4 +
union Noise: Copy { Failure }
5 +
6 +
/// Throw the noise error.
7 +
fn fail() throws (Noise) {
8 +
    throw Noise::Failure;
9 +
}
test/shared/errors/run added +19 -0
1 +
#!/bin/sh
2 +
# Link a package built alone with an entry built beside an unrelated package,
3 +
# then run the result. Error tags must agree across separate compilations.
4 +
set -eu
5 +
emulator=${RAD_EMULATOR:-emulator}
6 +
work=$(mktemp -d)
7 +
trap 'rm -rf "$work"' EXIT HUP INT TERM
8 +
dir=test/shared/errors
9 +
mkdir "$work/alone" "$work/combined"
10 +
scripts/hosted bin/radiance.rv64.dev \
11 +
    -pkg support -mod $dir/support.rad -ril "$work/alone"
12 +
scripts/hosted bin/radiance.rv64.dev \
13 +
    -pkg noise -mod $dir/noise.rad \
14 +
    -pkg support -mod $dir/support.rad \
15 +
    -pkg app -mod $dir/app.rad -entry app -ril "$work/combined"
16 +
"$emulator" -run test/shared/build.rv64 -- \
17 +
    "$work/alone/support.ril" "$work/combined/app.ril" "$work/native.rv64" catch
18 +
"$emulator" -machine -run "$work/native.rv64"
19 +
printf 'shared packages: typed catch agrees across separate compilations\n'
test/shared/errors/support.rad added +16 -0
1 +
//! Dependency with two error types and functions that throw them.
2 +
3 +
/// First error type.
4 +
export union First: Copy { Failure }
5 +
/// Second error type.
6 +
export union Second: Copy { Failure }
7 +
8 +
/// Throw the first error type.
9 +
export fn first() throws (First) {
10 +
    throw First::Failure;
11 +
}
12 +
13 +
/// Throw the second error type, from a list that also names the first.
14 +
export fn second() throws (First, Second) {
15 +
    throw Second::Failure;
16 +
}
test/shared/run added +12 -0
1 +
#!/bin/sh
2 +
# Compile two packages once and execute them with two private state graphs.
3 +
set -eu
4 +
emulator=${RAD_EMULATOR:-emulator}
5 +
work=$(mktemp -d)
6 +
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 +
scripts/hosted bin/radiance.rv64.dev \
8 +
    -pkg support -mod test/shared/support.rad \
9 +
    -pkg app -mod test/shared/app.rad -entry app -ril "$work"
10 +
"$emulator" -run test/shared/build.rv64 -- "$work/support.ril" "$work/app.ril" "$work/native.rv64"
11 +
"$emulator" -machine -run "$work/native.rv64"
12 +
printf 'shared packages: calls, callbacks, private pointers, and independent state passed\n'
test/shared/support.rad added +17 -0
1 +
//! Shared dependency with private writable state.
2 +
3 +
/// Value read by dependent packages.
4 +
export unsafe static value: i32 = 7;
5 +
6 +
/// Large private allocation before the final zero-initialized scalar.
7 +
unsafe static padding: [u8; 8192] = [0; 8192];
8 +
/// Zero-initialized value at a large package offset.
9 +
unsafe static tail: i32 = 0;
10 +
11 +
/// Read this domain's value.
12 +
export fn read() -> i32 {
13 +
    unsafe {
14 +
        set padding[8191] = 0;
15 +
        return value + tail;
16 +
    }
17 +
}