sys: Add a host ABI and drive tests from Radiance

2fde7d2e88b9286d42049f99fd26b82b35f58a012f2700488fbc80a7bd0035fe
Hosted programs used Linux system call numbers and passed Linux open
flags straight to the host, and the integration tests were shell
scripts built on mktemp, grep, od, xargs and timeout. Both tied the
project to Unix hosts.

Hosted programs now use a Radiance host ABI, starting at call 0x400.
It covers files, directories and running other RV64 images through the
same emulator, so guests never name host programs. `std::sys::host`
issues the calls and maps their error codes to `std::sys::Error`.
`std::fs` and `std::process` build on it and lay out its records.
Paths need no zero terminator, `image::parse` reads image headers, and
`std::mem` gains `find`, `endsWith`, `split`, `readLe` and `writeLe`.

A single driver, `test/integration.rv64`, replaces the test scripts.
It builds fixtures, runs children with instruction budgets instead of
wall-clock timeouts, works in `bin/test/<command>`, and runs batches
over a worker pool. A new host test covers `std::process`. Tools read
their arguments from element zero, without a leading `--`. Six
assembly fixtures now state their full exit status, which the scripts
truncated to eight bits.

Assisted-by: Claude:opus-5.5
Assisted-by: Codex:gpt-6
Alexis Sellier committed ago 1 parent e208b9f6
AGENTS.md +16 -9
90 90
## Build and test entry points
91 91
92 92
Use Make to compile: its recipes supply required memory and stack settings.
93 93
Use timeouts for builds, tests, scripts, and emulator runs. These limits are
94 94
starting points; investigate timeouts before increasing them. Export
95 -
`RAD_EMULATOR` when selecting an emulator so Make and scripts use the same one.
95 +
`RAD_EMULATOR` when selecting an emulator so Make and `seed/update` use the
96 +
same one. The integration driver `test/integration.rv64` runs the tests that
97 +
start other programs; Make starts it with the compiler and job count. Its
98 +
commands are listed in `test/integration.rad`. Child programs have instruction
99 +
budgets instead of wall-clock timeouts.
96 100
97 101
```bash
98 102
timeout 300 make           # Build bin/radiance.rv64.dev from the seed
99 103
timeout 600 make test      # Seed format, std, and binary tests
100 104
timeout 300 make std-test  # Standard library tests
101 105
timeout 300 make bin-test  # All IL and executable checks
106 +
timeout 300 make host-test # std::process against the emulator
107 +
timeout 300 make fmt       # Format the std and kernel sources in place
102 108
```
103 109
104 110
For one executable fixture, replace `NAME` with its basename:
105 111
106 112
```bash
107 -
timeout 300 make test/runner.rv64 test/tests/NAME.rv64
108 -
timeout 10 test/run test/tests/NAME.rad
113 +
timeout 300 make test/runner.rv64 test/integration.rv64 test/tests/NAME.rv64
114 +
timeout 10 "$RAD_EMULATOR" -run test/integration.rv64 bin test/tests/NAME.rad
109 115
```
110 116
111 -
For an IL-only fixture, build only `test/runner.rv64` before `test/run`.
112 -
The harness runs existing binaries; it does not build them.
117 +
For an IL-only fixture, build only `test/runner.rv64` and
118 +
`test/integration.rv64` before running the driver. The driver runs existing
119 +
binaries; it does not build them.
113 120
114 121
## Task-specific references
115 122
116 123
- Compiler work: `compiler/radiance.rad` is the entry point; `lib/std/lang/`
117 124
  contains scanner, parser, resolver, lowering, and code generation modules.
118 125
  RV64 backend and assembler code is in `lib/std/arch/rv64/`.
119 -
- Tests: read nearby fixtures and `test/run` for supported checks. `.ril` files
120 -
  check IL; `//! returns: N` requests an executable check. Success fixtures use
121 -
  `@default fn main`, assertions, and a zero return. Inspect generated IL before
122 -
  accepting a new snapshot.
126 +
- Tests: read nearby fixtures and `test/integration/fixtures.rad` for supported
127 +
  checks. `.ril` files check IL; `//! returns: N` requests an executable check.
128 +
  Success fixtures use `@default fn main`, assertions, and a zero return.
129 +
  Inspect generated IL before accepting a new snapshot.
123 130
- Session fixtures: `//! session-support` uses `test/support/std.lib`. Keep
124 131
  individual golden checks below one second. Test production std behavior in std
125 132
  unit suites. Module lists are in `std.lib` and `std.lib.test`.
126 133
- Backend debugging: locate the first incorrect stage, from AST and resolution
127 134
  through IL, assembly, and execution. Use Make recipes with the existing
Makefile +11 -6
8 8
9 9
# Compiler output.
10 10
BIN_DIR := bin
11 11
RAD_BIN := $(BIN_DIR)/radiance.rv64.dev
12 12
13 -
# Command that runs the self-hosted compiler. The scripts choose the emulator.
14 -
HOSTED    := scripts/hosted
13 +
# Emulator that runs every image. The integration driver starts its children
14 +
# on the same emulator. seed/update reads it from the environment.
15 +
RAD_EMULATOR ?= emulator
16 +
export RAD_EMULATOR
17 +
18 +
# Command that runs hosted build tools with shared memory and stack limits.
19 +
HOSTED    := $(RAD_EMULATOR) -memory-size=385024 -data-size=348416 \
20 +
    -stack-size=1024 -count-instructions -run
15 21
RADIANCE  := $(HOSTED) $(RAD_BIN)
16 22
17 23
default: $(RAD_BIN)
18 24
19 25
# Compiler build
22 28
COMPILER_SRC := compiler/radiance.rad compiler/radiance/codegen.rad \
23 29
    compiler/radiance/testgen.rad
24 30
SEED_OPTS    := $(STD) -pkg radiance $(patsubst %,-mod %,$(COMPILER_SRC)) \
25 31
    -entry radiance
26 32
27 -
$(RAD_BIN): Makefile $(SEED) std.lib $(STD_LIB) $(COMPILER_SRC) \
28 -
    scripts/hosted | $(BIN_DIR)
33 +
$(RAD_BIN): Makefile $(SEED) std.lib $(STD_LIB) $(COMPILER_SRC) | $(BIN_DIR)
29 34
	@echo "radiance $(SEED) => $@"
30 35
	@$(HOSTED) $(SEED) $(SEED_OPTS) -o $@
31 36
32 37
$(BIN_DIR):
33 38
	@mkdir -p $@
37 42
38 43
# Format the standard library and kernel sources in place. Set `FMT_SRC` to
39 44
# format other files.
40 45
FMT_SRC ?= $(STD_TEST_LIB) $(shell find kernel -name '*.rad' | sort)
41 46
42 -
fmt: $(RAD_BIN) scripts/fmt
43 -
	@sh scripts/fmt $(FMT_SRC)
47 +
fmt: $(RAD_BIN) $(INTEGRATION)
48 +
	@$(call integration,format,$(FMT_SRC))
44 49
45 50
seed:
46 51
	seed/update
47 52
48 53
clean-rad:
compiler/radiance.rad +32 -42
27 27
use std::lang::il::binary;
28 28
use std::lang::il::binary::collect;
29 29
use std::lang::il::binary::program;
30 30
use std::lang::gen::types;
31 31
use std::sys;
32 -
use std::sys::unix;
32 +
use std::fs;
33 33
use std::collections::dict;
34 34
35 35
/// Maximum number of modules we can load per package.
36 36
constant MAX_LOADED_MODULES: u32 = module::MAX_MODULES;
37 37
/// Maximum number of packages we can compile.
277 277
        }
278 278
    }
279 279
    io::printError("\n");
280 280
}
281 281
282 -
/// Return `true` when `path` ends with `ext`.
283 -
fn hasExtension(path: *[u8], ext: *[u8]) -> bool {
284 -
    if path.len < ext.len {
285 -
        return false;
286 -
    }
287 -
    let start = path.len - ext.len;
288 -
    return mem::eq(&path[start..], ext);
289 -
}
290 -
291 282
/// Allocate storage for `count` node records in `arena`.
292 283
///
293 284
/// This is the trusted boundary for resolver node metadata. The elements are not
294 285
/// initialized here. [`resolver::resolver`] writes every element to sentinel values
295 286
/// before any read, so the storage must be passed only to that call. The arena
337 328
    if alloc::remaining(arena) == 0 {
338 329
        throw error(&["fatal:", "source arena exhausted"]);
339 330
    }
340 331
    unsafe {
341 332
        let free = alloc::remainingBuf(arena);
342 -
        let sourceLen = unix::readFile(path, free) else {
343 -
            throw error(&["error reading file"]);
333 +
        let sourceLen = try fs::readFile(path, free) catch err {
334 +
            if err == sys::Error::TooLarge {
335 +
                throw error(&["fatal:", "source arena too small, file truncated:", path]);
336 +
            }
337 +
            throw error(&["error reading file:", path]);
344 338
        };
345 -
        // A read that fills the free space may have stopped at its end.
346 -
        if sourceLen >= free.len {
347 -
            throw error(&["fatal:", "source arena too small, file truncated:", path]);
348 -
        }
349 339
        let source: *mut [u8] = &mut free[..sourceLen];
350 340
        alloc::commit(arena, sourceLen);
351 341
        return source;
352 342
    }
353 343
}
479 469
                if byte == '=' {
480 470
                    set mapped = true;
481 471
                    break;
482 472
                }
483 473
            }
484 -
            if not mapped and hasExtension(args[idx], ASM_SOURCE_EXT) {
474 +
            if not mapped and mem::endsWith(args[idx], ASM_SOURCE_EXT) {
485 475
                if input.asmPathCount >= MAX_ASM_MODULES {
486 476
                    throw error(&["too many assembly modules specified"]);
487 477
                }
488 478
                set input.asmPaths[input.asmPathCount] = args[idx];
489 479
                set input.asmPathCount += 1;
501 491
            };
502 492
            let input = &mut inputs[pkgIdx];
503 493
            if input.startupPath <> nil {
504 494
                throw error(&["package", input.name, "has more than one startup file"]);
505 495
            }
506 -
            if not hasExtension(args[idx], ASM_SOURCE_EXT) {
496 +
            if not mem::endsWith(args[idx], ASM_SOURCE_EXT) {
507 497
                throw error(&["`-start` requires a `.ras` assembly file"]);
508 498
            }
509 499
            set input.startupPath = args[idx];
510 500
        } else if mem::eq(arg, "-entry") {
511 501
            try nextArg(args, &mut idx, &["`-entry` requires a package name"]);
965 955
    path: *[u8]
966 956
) -> bool {
967 957
    let header = try image::header(layout) catch {
968 958
        return false;
969 959
    };
970 -
    let fd = unix::openOpts(path, unix::OpenFlags(*unix::O_WRONLY |
971 -
        *unix::O_CREAT | *unix::O_TRUNC), unix::FILE_MODE);
972 -
    if fd < 0 { return false; }
973 -
    let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd, codeBytes(code))
974 -
        and unix::writeAll(fd, roData) and unix::writeAll(fd, rwData);
975 -
    let closed = unix::close(fd) == 0;
976 -
    return written and closed;
960 +
    let mut out = try fs::create(path) catch {
961 +
        return false;
962 +
    };
963 +
    out.write(&header[..]);
964 +
    out.write(codeBytes(code));
965 +
    out.write(roData);
966 +
    out.write(rwData);
967 +
    try out.close() catch {
968 +
        return false;
969 +
    };
970 +
    return true;
977 971
}
978 972
979 973
/// Write a data section to a file at `basePath` + `ext`.
980 974
/// Empty data truncates any stale sidecar left by an earlier build.
981 975
fn writeDataWithExt(
984 978
    ext: *[u8]
985 979
) throws (Error) {
986 980
    let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN];
987 981
    let mut pos: u32 = 0;
988 982
989 -
    if basePath.len >= MAX_PATH_LEN or ext.len >= MAX_PATH_LEN - basePath.len {
983 +
    if basePath.len > MAX_PATH_LEN or ext.len > MAX_PATH_LEN - basePath.len {
990 984
        throw error(&["fatal: output path too long:", basePath]);
991 985
    }
992 986
    set pos += try! mem::copy(&mut path[pos..], basePath);
993 987
    set pos += try! mem::copy(&mut path[pos..], ext);
994 -
    set path[pos] = 0; // Null-terminate for syscall.
995 988
996 -
    if not unix::writeFile(&path[..pos], data) {
989 +
    try fs::writeFile(&path[..pos], data) catch {
997 990
        throw error(&["fatal:", "failed to write data file"]);
998 -
    }
991 +
    };
999 992
}
1000 993
1001 994
/// Serialize debug entries and write the `.debug` file.
1002 995
/// Resolves module IDs to file paths via the module graph.
1003 996
/// Format per entry is `{pc: u32,  offset: u32, filePath: [u8], NULL}`.
1031 1024
        let entry = &entries[i];
1032 1025
        let modEntry = module::get(modules, entry.moduleId) else {
1033 1026
            panic "writeDebugInfo: module not found for debug entry";
1034 1027
        };
1035 1028
        try! buf.resize(pos + 2 * WORD_BYTES + modEntry.filePath.len + 1, 0, a);
1036 -
        data::writeLe(&mut buf[pos..], 0, entry.pc as u64, WORD_BYTES);
1037 -
        data::writeLe(&mut buf[pos..], WORD_BYTES, entry.offset as u64, WORD_BYTES);
1029 +
        mem::writeLe(&mut buf[pos..], 0, entry.pc as u64, WORD_BYTES);
1030 +
        mem::writeLe(&mut buf[pos..], WORD_BYTES, entry.offset as u64, WORD_BYTES);
1038 1031
        set pos += 2 * WORD_BYTES;
1039 1032
        set pos += try! mem::copy(&mut buf[pos..], modEntry.filePath);
1040 1033
1041 1034
        set buf[pos] = 0;
1042 1035
        set pos += 1;
1329 1322
            throw error(&["binary RIL tables exceed their buffer", pkg.name]);
1330 1323
        };
1331 1324
        let mut path: [u8; MAX_PATH_LEN] = [0; MAX_PATH_LEN];
1332 1325
        let mut pos: u32 = 0;
1333 1326
        for part in &[directory, "/", pkg.name, ".ril"] {
1334 -
            set pos += try mem::copy(&mut path[pos..MAX_PATH_LEN - 1], part) catch {
1327 +
            set pos += try mem::copy(&mut path[pos..], part) catch {
1335 1328
                throw error(&["binary RIL output path is too long"]);
1336 1329
            };
1337 1330
        }
1338 -
        let fd = unix::openOpts(&path[..pos],
1339 -
            unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC),
1340 -
                unix::FILE_MODE);
1341 -
        if fd < 0 {
1342 -
            throw error(&["cannot write binary RIL package", pkg.name]);
1343 -
        }
1344 1331
        let start = spool.packages[i].start;
1345 1332
        let end = spool.packages[i].end;
1346 -
        let written = unix::writeAll(fd, &head[..length]) and unix::writeAll(fd,
1347 -
            &spool.bytes[start..end]);
1348 -
        if not (unix::close(fd) == 0 and written) {
1333 +
        let mut out = try fs::create(&path[..pos]) catch {
1349 1334
            throw error(&["cannot write binary RIL package", pkg.name]);
1350 -
        }
1335 +
        };
1336 +
        out.write(&head[..length]);
1337 +
        out.write(&spool.bytes[start..end]);
1338 +
        try out.close() catch {
1339 +
            throw error(&["cannot write binary RIL package", pkg.name]);
1340 +
        };
1351 1341
    }
1352 1342
}
1353 1343
1354 1344
/// Lower, optionally dump, and optionally generate binary output.
1355 1345
/// The lowerer takes ownership of `res` and of `fnArena`, and the passes take
kernel/Makefile +1 -1
27 27
	@$(RADIANCE) $(STD) -pkg build -mod $< -entry build -o $@
28 28
29 29
$(BIN_DIR)/kernel.rv64: $(BIN_DIR)/std.ril $(BIN_DIR)/kernel.ril \
30 30
    $(BIN_DIR)/kernel.build.rv64 $(KERNEL_ASM) kernel/Makefile
31 31
	@cat $(KERNEL_ASM) > $(BIN_DIR)/kernel.ras
32 -
	@$(HOSTED) $(BIN_DIR)/kernel.build.rv64 -- $(BIN_DIR)/std.ril \
32 +
	@$(HOSTED) $(BIN_DIR)/kernel.build.rv64 $(BIN_DIR)/std.ril \
33 33
	    $(BIN_DIR)/kernel.ril $(BIN_DIR)/kernel.ras $@
kernel/tools/build.rad +24 -24
1 1
//! Build a native kernel image with shared packages and a persistent boot catalog.
2 2
3 3
use std::sys;
4 4
use std::io;
5 5
use std::mem;
6 -
use std::sys::unix;
6 +
use std::fs;
7 7
use std::lang::alloc;
8 8
use std::lang::strings;
9 9
use std::lang::il::binary;
10 10
use std::lang::il::binary::program;
11 11
use std::lang::il::verify;
173 173
    }
174 174
}
175 175
176 176
/// Link shared boot packages, retain their catalog, and instantiate the kernel graph.
177 177
@default unsafe fn main(env: *sys::Env) -> i32 {
178 -
    assert env.args.len >= 5 and env.args.len <= PACKAGES + 3;
179 -
    let count = env.args.len - 3;
178 +
    assert env.args.len >= 4 and env.args.len <= PACKAGES + 2;
179 +
    let count = env.args.len - 2;
180 180
    let mut decoder = alloc::new(&mut DECODE[..]);
181 -
    let libraryBytesLength = unix::readFile(env.args[1], &mut INPUT[0][..]) else panic "main: std RIL";
181 +
    let libraryBytesLength = try fs::readFile(env.args[0], &mut INPUT[0][..]) catch {
182 +
        panic "main: std RIL";
183 +
    };
182 184
    let libraryBytes = &INPUT[0][..libraryBytesLength];
183 185
    let libraryInput = try! program::decode(libraryBytes, &mut decoder, binary::Limits {
184 186
        registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN
185 187
    });
186 -
    let kernelBytesLength = unix::readFile(env.args[2], &mut INPUT[1][..]) else panic "main: kernel RIL";
188 +
    let kernelBytesLength = try fs::readFile(env.args[1], &mut INPUT[1][..]) catch {
189 +
        panic "main: kernel RIL";
190 +
    };
187 191
    let kernelBytes = &INPUT[1][..kernelBytesLength];
188 192
    let kernelInput = try! program::decode(kernelBytes, &mut decoder, binary::Limits {
189 193
        registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN
190 194
    });
191 195
    assert mem::eq(libraryInput.name, "std") and mem::eq(kernelInput.name, "kernel");
192 196
    assert libraryInput.dependencies.len == 0;
193 197
    for dependency in kernelInput.dependencies {
194 198
        assert mem::eq(dependency, "std");
195 199
    }
196 -
    let sourceLength = unix::readFile(env.args[3], &mut SOURCE[..]) else panic "main: kernel assembly";
200 +
    let sourceLength = try fs::readFile(env.args[2], &mut SOURCE[..]) catch {
201 +
        panic "main: kernel assembly";
202 +
    };
197 203
    let source = &SOURCE[..sourceLength];
198 204
    let mut assemblyArena = alloc::new(&mut ASSEMBLY[..]);
199 205
    let empty: *mut [u8] = &mut [];
200 206
    let mut names = strings::pool(&mut NAMES[..]);
201 207
    let assembly = try asm::assemble(
205 211
        empty,
206 212
        &mut assemblyArena,
207 213
        &mut names,
208 214
        0
209 215
    ) catch error {
210 -
        asm::printError(error, &env.args[3][..], source);
216 +
        asm::printError(error, &env.args[2][..], source);
211 217
        return 1;
212 218
    };
213 219
    let library = try compile(
214 220
        &libraryInput,
215 221
        0,
237 243
    set catalogEntries[0] = catalog::Entry { source: libraryBytes, package: library };
238 244
    set catalogEntries[1] = catalog::Entry { source: kernelBytes, package: kernel };
239 245
    let mut codeEnd = kernel.codeAddress
240 246
        + kernel.code.len as u64 * rv64::INSTR_SIZE as u64;
241 247
    for slot in 2..count {
242 -
        let bytesLength = unix::readFile(
243 -
            env.args[slot + 3], &mut INPUT[slot][..]
244 -
        ) else panic "main: boot package RIL";
248 +
        let bytesLength = try fs::readFile(
249 +
            env.args[slot + 2], &mut INPUT[slot][..]
250 +
        ) catch {
251 +
            panic "main: boot package RIL";
252 +
        };
245 253
        let bytes = &INPUT[slot][..bytesLength];
246 254
        let mut input = try! program::decode(bytes, &mut decoder, binary::Limits {
247 255
            registers: liveness::MAX_SSA_REGS, blocks: labels::MAX_BLOCKS_PER_FN
248 256
        });
249 257
        // User packages run in U-mode, so every one must verify; there is no way around this.
333 341
        code: image::Segment { address: CODE_ADDRESS, size: codeSize },
334 342
        roData: image::Segment { address: roAddress, size: roSize },
335 343
        rwData: image::Segment { address: rwAddress, size: rwSize },
336 344
        rwTail: 0,
337 345
    });
338 -
    let fd = unix::openOpts(
339 -
        env.args[4],
340 -
        unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC),
341 -
        unix::FILE_MODE
342 -
    );
343 -
    assert fd >= 0;
344 -
    let written = unix::writeAll(fd, &header[..])
345 -
        and unix::writeAll(fd, @sliceOf(
346 -
            &NATIVE[0] as *u8, codeSize
347 -
        ))
348 -
        and unix::writeAll(fd, &RO[..roSize])
349 -
        and unix::writeAll(fd, &RW[..rwSize]);
350 -
    let closed = unix::close(fd) == 0;
351 -
    assert written and closed;
346 +
    let mut out = try! fs::create(env.args[3]);
347 +
    out.write(&header[..]);
348 +
    out.write(@sliceOf(&NATIVE[0] as *u8, codeSize));
349 +
    out.write(&RO[..roSize]);
350 +
    out.write(&RW[..rwSize]);
351 +
    try! out.close();
352 352
    return 0;
353 353
}
lib/std.rad +2 -0
3 3
export mod io;
4 4
export mod collections;
5 5
export mod char;
6 6
export mod lang;
7 7
export mod sys;
8 +
export mod fs;
9 +
export mod process;
8 10
export mod arch;
9 11
export mod fmt;
10 12
export mod mem;
11 13
export mod vec;
12 14
export mod intrinsics;
lib/std/arch/rv64/image.rad +34 -5
1 1
//! Native image segment placement and wire headers.
2 2
3 3
use std::mem;
4 -
use std::lang::gen::data;
5 4
@test export mod tests;
6 5
7 6
/// Largest positive AUIPC/ADDI displacement.
8 7
export constant DISPLACEMENT_MAX: u32 = 0x7ffff7ff;
9 8
/// Largest negative AUIPC/ADDI displacement magnitude.
35 34
    Overlap,
36 35
    /// Emitted data exceeds the extent laid out for it.
37 36
    Size,
38 37
    /// A PC-relative address load exceeds its instruction range.
39 38
    Relocation,
39 +
    /// The input is shorter than a header.
40 +
    Truncated,
41 +
    /// The input does not start with the native image signature.
42 +
    Signature,
40 43
}
41 44
42 45
/// Address and stored size of one image segment.
43 46
export record Segment: Copy {
44 47
    /// Physical start address.
102 105
103 106
/// Encode a validated native header in little-endian order.
104 107
export fn header(layout: Layout) -> [u8; HEADER_SIZE] throws (Error) {
105 108
    try validate(layout);
106 109
    let mut bytes: [u8; HEADER_SIZE] = [0; HEADER_SIZE];
107 -
    data::writeLe(&mut bytes[..], 0, MAGIC, ADDRESS_SIZE);
110 +
    mem::writeLe(&mut bytes[..], 0, MAGIC, ADDRESS_SIZE);
108 111
    let segments = [layout.code, layout.roData, layout.rwData];
109 112
    let offsets = [CODE_OFFSET, RO_DATA_OFFSET, RW_DATA_OFFSET];
110 113
    for item, i in &segments[..] {
111 -
        data::writeLe(&mut bytes[..], offsets[i], item.address, ADDRESS_SIZE);
112 -
        data::writeLe(&mut bytes[..], offsets[i] + ADDRESS_SIZE, item.size as u64, SIZE_SIZE);
114 +
        mem::writeLe(&mut bytes[..], offsets[i], item.address, ADDRESS_SIZE);
115 +
        mem::writeLe(&mut bytes[..], offsets[i] + ADDRESS_SIZE, item.size as u64, SIZE_SIZE);
113 116
    }
114 -
    data::writeLe(
117 +
    mem::writeLe(
115 118
        &mut bytes[..], RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, layout.rwTail as u64, SIZE_SIZE
116 119
    );
117 120
    return bytes;
118 121
}
119 122
123 +
/// Decode and validate the header at the start of `bytes`. It is the inverse
124 +
/// of [`header`]. Later bytes are not examined.
125 +
export fn parse(bytes: &[u8]) -> Layout throws (Error) {
126 +
    if bytes.len < HEADER_SIZE {
127 +
        throw Error::Truncated;
128 +
    }
129 +
    if mem::readLe(bytes, 0, ADDRESS_SIZE) <> MAGIC {
130 +
        throw Error::Signature;
131 +
    }
132 +
    let code = segmentAt(bytes, CODE_OFFSET);
133 +
    let roData = segmentAt(bytes, RO_DATA_OFFSET);
134 +
    let rwData = segmentAt(bytes, RW_DATA_OFFSET);
135 +
    let rwTail = mem::readLe(bytes, RW_DATA_OFFSET + ADDRESS_SIZE + SIZE_SIZE, SIZE_SIZE);
136 +
    let layout = Layout { code, roData, rwData, rwTail: rwTail as u32 };
137 +
    try validate(layout);
138 +
    return layout;
139 +
}
140 +
141 +
/// Decode the segment record at `offset` of a header.
142 +
fn segmentAt(bytes: &[u8], offset: u32) -> Segment {
143 +
    return Segment {
144 +
        address: mem::readLe(bytes, offset, ADDRESS_SIZE),
145 +
        size: mem::readLe(bytes, offset + ADDRESS_SIZE, SIZE_SIZE) as u32,
146 +
    };
147 +
}
148 +
120 149
/// Address policy for generated code and data.
121 150
export union Placement: Copy {
122 151
    /// Hosted layout with code after read-only data.
123 152
    Hosted,
124 153
    /// Fixed physical segment addresses. Execution starts at `code`.
lib/std/arch/rv64/image/tests.rad +43 -0
62 62
            0, 32, 0, 128, 0, 0, 0, 0, 4, 0, 0, 0, 252, 15, 0, 0,
63 63
        ]
64 64
    );
65 65
}
66 66
67 +
/// Parsing a header returns the layout that produced it.
68 +
@test fn parseRoundTrip() throws (testing::TestError) {
69 +
    let item = layout();
70 +
    let bytes = try image::header(item) catch {
71 +
        throw testing::TestError::Failed;
72 +
    };
73 +
    let parsed = try image::parse(&bytes[..]) catch {
74 +
        throw testing::TestError::Failed;
75 +
    };
76 +
    try testing::expect(parsed.code.address == item.code.address);
77 +
    try testing::expect(parsed.code.size == item.code.size);
78 +
    try testing::expect(parsed.roData.address == item.roData.address);
79 +
    try testing::expect(parsed.roData.size == item.roData.size);
80 +
    try testing::expect(parsed.rwData.address == item.rwData.address);
81 +
    try testing::expect(parsed.rwData.size == item.rwData.size);
82 +
    try testing::expect(parsed.rwTail == item.rwTail);
83 +
}
84 +
85 +
/// Check a rejected header.
86 +
fn parseFails(bytes: &[u8], expected: image::Error) throws (testing::TestError) {
87 +
    let mut failed = false;
88 +
    try image::parse(bytes) catch err {
89 +
        try testing::expect(err == expected);
90 +
        set failed = true;
91 +
    };
92 +
    try testing::expect(failed);
93 +
}
94 +
95 +
/// Parsing rejects short input, a bad signature and an invalid layout.
96 +
@test fn parseRejects() throws (testing::TestError) {
97 +
    let mut bytes = try image::header(layout()) catch {
98 +
        throw testing::TestError::Failed;
99 +
    };
100 +
    try parseFails(&bytes[..image::HEADER_SIZE - 1], image::Error::Truncated);
101 +
    try parseFails(&bytes[..0], image::Error::Truncated);
102 +
    set bytes[0] = 0;
103 +
    try parseFails(&bytes[..], image::Error::Signature);
104 +
    set bytes[0] = 82;
105 +
    // The rwdata address becomes unaligned.
106 +
    set bytes[32] = 1;
107 +
    try parseFails(&bytes[..], image::Error::Alignment);
108 +
}
109 +
67 110
/// Check a rejected image layout.
68 111
fn invalid(item: image::Layout, expected: image::Error) throws (testing::TestError) {
69 112
    let mut failed = false;
70 113
    try image::validate(item) catch err {
71 114
        try testing::expect(err == expected);
lib/std/fs.rad added +181 -0
1 +
//! Host-neutral file system access.
2 +
//!
3 +
//! Paths are byte strings without a terminating zero byte. Relative paths
4 +
//! resolve against the host's current directory. Every failure is a
5 +
//! [`sys::Error`].
6 +
7 +
use std::mem;
8 +
use std::sys;
9 +
use std::sys::host;
10 +
11 +
@test mod tests;
12 +
13 +
/// Kind of a file system entry.
14 +
export union Kind: Copy {
15 +
    /// Regular file.
16 +
    File,
17 +
    /// Directory.
18 +
    Directory,
19 +
    /// Any other entry.
20 +
    Other,
21 +
}
22 +
23 +
/// Size and kind of a file system entry.
24 +
export record Stat: Copy {
25 +
    /// Size in bytes. Zero for directories.
26 +
    size: u64,
27 +
    /// Kind of the entry.
28 +
    kind: Kind,
29 +
}
30 +
31 +
/// Open the file at `path` for reading and return its descriptor. The host
32 +
/// open mode is 0 to read and 1 to create or truncate for writing.
33 +
export fn openRead(path: &[u8]) -> i64 throws (sys::Error) {
34 +
    return try host::callPath(host::OPEN, path, 0, 0);
35 +
}
36 +
37 +
/// Close a descriptor. Closing a standard descriptor is `Invalid`.
38 +
export fn close(fd: i64) throws (sys::Error) {
39 +
    try host::call(host::CLOSE, fd, 0, 0, 0);
40 +
}
41 +
42 +
/// Read from `fd` until end of file. Throws `TooLarge` when the data does not
43 +
/// fit in `buf`.
44 +
fn readAll(fd: i64, buf: &mut [u8]) -> u32 throws (sys::Error) {
45 +
    let mut total: u32 = 0;
46 +
    while total < buf.len {
47 +
        let n = try host::read(fd, &mut buf[total..]);
48 +
        if n == 0 {
49 +
            return total;
50 +
        }
51 +
        set total += n;
52 +
    }
53 +
    // The buffer is full. Any further byte does not fit.
54 +
    let mut probe: [u8; 1] = [0; 1];
55 +
    if try host::read(fd, &mut probe[..]) <> 0 {
56 +
        throw sys::Error::TooLarge;
57 +
    }
58 +
    return total;
59 +
}
60 +
61 +
/// Read the file at `path` into `buf` and return the number of bytes read.
62 +
/// Throws `TooLarge` when the file does not fit.
63 +
export fn readFile(path: &[u8], buf: &mut [u8]) -> u32 throws (sys::Error) {
64 +
    let fd = try openRead(path);
65 +
    let n = try readAll(fd, buf) catch err {
66 +
        try close(fd) catch {};
67 +
        throw err;
68 +
    };
69 +
    try close(fd);
70 +
    return n;
71 +
}
72 +
73 +
/// A file open for writing. Failures are remembered rather than reported by
74 +
/// each write. Call [`Writer::close`] exactly once: it releases the descriptor
75 +
/// and reports every failure of the writer.
76 +
export record Writer: Copy {
77 +
    /// Host descriptor.
78 +
    fd: i64,
79 +
    /// First write failure. Later writes do nothing once it is set.
80 +
    failure: ?sys::Error,
81 +
}
82 +
83 +
/// Create the file at `path`, or truncate it when it exists, and open it for
84 +
/// writing.
85 +
export fn create(path: &[u8]) -> Writer throws (sys::Error) {
86 +
    return Writer { fd: try host::callPath(host::OPEN, path, 1, 0), failure: nil };
87 +
}
88 +
89 +
/// Write all of `data` to the file. After a failure this does nothing; the
90 +
/// first failure is reported by `close`.
91 +
export fn (w: &mut Writer) write(data: &[u8]) {
92 +
    let mut written: u32 = 0;
93 +
    while w.failure == nil and written < data.len {
94 +
        let n = try host::write(w.fd, &data[written..]) catch err {
95 +
            set w.failure = err;
96 +
            return;
97 +
        };
98 +
        if n == 0 {
99 +
            set w.failure = sys::Error::Io;
100 +
            return;
101 +
        }
102 +
        set written += n;
103 +
    }
104 +
}
105 +
106 +
/// Close the file. Always releases the descriptor. Throws the first write
107 +
/// failure if there was one, otherwise the failure to close, from which
108 +
/// buffered data may be lost.
109 +
export fn (w: &mut Writer) close() throws (sys::Error) {
110 +
    let mut error = w.failure;
111 +
    try close(w.fd) catch err {
112 +
        if error == nil {
113 +
            set error = err;
114 +
        }
115 +
    };
116 +
    if let first = error {
117 +
        throw first;
118 +
    }
119 +
}
120 +
121 +
/// Write the parts in order to the file at `path`. Creates the file, or
122 +
/// truncates it when it exists.
123 +
export fn writeFileParts(path: &[u8], parts: &[*[u8]]) throws (sys::Error) {
124 +
    let mut out = try create(path);
125 +
    for part in parts {
126 +
        out.write(part);
127 +
    }
128 +
    try out.close();
129 +
}
130 +
131 +
/// Write `data` to the file at `path`. Creates the file, or truncates it when
132 +
/// it exists.
133 +
export fn writeFile(path: &[u8], data: &[u8]) throws (sys::Error) {
134 +
    let mut out = try create(path);
135 +
    out.write(data);
136 +
    try out.close();
137 +
}
138 +
139 +
/// Look up the entry at `path`. Returns `nil` when it does not exist.
140 +
export fn stat(path: &[u8]) -> ?Stat throws (sys::Error) {
141 +
    let mut out: [u8; 16] = [0; 16];
142 +
    try host::callPath(host::STAT, path, host::addr(&out[..]), 0) catch err {
143 +
        if err == sys::Error::NotFound {
144 +
            return nil;
145 +
        }
146 +
        throw err;
147 +
    };
148 +
    // The host `Stat` record is a 64-bit size, a 32-bit kind (1 file, 2
149 +
    // directory) and padding.
150 +
    let code = mem::readLe(&out[..], 8, 4);
151 +
    let mut kind = Kind::Other;
152 +
    if code == 1 {
153 +
        set kind = Kind::File;
154 +
    } else if code == 2 {
155 +
        set kind = Kind::Directory;
156 +
    }
157 +
    return Stat { size: mem::readLe(&out[..], 0, 8), kind };
158 +
}
159 +
160 +
/// Create the directory at `path`. Throws `Exists` when the path is taken.
161 +
export fn makeDir(path: &[u8]) throws (sys::Error) {
162 +
    try host::callPath(host::MAKE_DIR, path, 0, 0);
163 +
}
164 +
165 +
/// Remove the file or empty directory at `path`.
166 +
export fn remove(path: &[u8]) throws (sys::Error) {
167 +
    try host::callPath(host::REMOVE, path, 0, 0);
168 +
}
169 +
170 +
/// Rename `from` to `to`. An existing file at `to` is replaced.
171 +
export fn rename(from: &[u8], to: &[u8]) throws (sys::Error) {
172 +
    try host::callPath(host::RENAME, from, host::addr(to), to.len as i64);
173 +
}
174 +
175 +
/// List the directory at `path` into `buf`. Returns the names, each followed
176 +
/// by a zero byte, in host order, which `mem::split(names, 0)` iterates. Throws
177 +
/// `TooLarge` when they do not fit.
178 +
export fn listDir(path: &[u8], buf: *mut [u8]) -> *[u8] throws (sys::Error) {
179 +
    let n = try host::callPath(host::LIST_DIR, path, host::addr(buf), buf.len as i64);
180 +
    return &buf[..n as u32];
181 +
}
lib/std/fs/tests.rad added +152 -0
1 +
//! File system tests, run in a scratch directory under `bin/`.
2 +
3 +
use std::fs;
4 +
use std::mem;
5 +
use std::sys;
6 +
use std::testing;
7 +
8 +
/// Scratch directory.
9 +
constant DIR: *[u8] = "bin/std.fs.test";
10 +
/// File in the scratch directory.
11 +
constant FILE_A: *[u8] = "bin/std.fs.test/a";
12 +
/// Second file.
13 +
constant FILE_B: *[u8] = "bin/std.fs.test/b";
14 +
/// Subdirectory.
15 +
constant SUB: *[u8] = "bin/std.fs.test/sub";
16 +
/// File in the subdirectory.
17 +
constant SUB_FILE: *[u8] = "bin/std.fs.test/sub/x";
18 +
19 +
/// Remove everything the tests create, including leftovers of a failed run.
20 +
fn clean() {
21 +
    for path in &[SUB_FILE, SUB, FILE_B, FILE_A, DIR] {
22 +
        try fs::remove(path) catch {};
23 +
    }
24 +
}
25 +
26 +
/// Start a test with an empty scratch directory.
27 +
fn setup() {
28 +
    clean();
29 +
    try fs::makeDir("bin") catch {};
30 +
    try! fs::makeDir(DIR);
31 +
}
32 +
33 +
/// Write a small file, which the tests rely on.
34 +
fn put(path: *[u8], text: *[u8]) {
35 +
    try! fs::writeFile(path, text);
36 +
}
37 +
38 +
/// Check that the file at `path` holds exactly the text.
39 +
fn expectFile(path: *[u8], text: *[u8]) throws (testing::TestError) {
40 +
    let mut buffer: [u8; 16] = [0; 16];
41 +
    let n = try! fs::readFile(path, &mut buffer[..]);
42 +
    try testing::expectBytesEq(&buffer[..n], text);
43 +
}
44 +
45 +
/// Check that the operation `op` on `path` fails with `expected`. The
46 +
/// operations are 0 read into five bytes, 1 make a directory, 2 remove, 3
47 +
/// create a file and 4 list into one byte.
48 +
unsafe fn expectFails(op: u32, path: *[u8], expected: sys::Error) throws (testing::TestError) {
49 +
    static big: [u8; 5] = [0; 5];
50 +
    static small: [u8; 1] = [0; 1];
51 +
    let mut failure: ?sys::Error = nil;
52 +
    match op {
53 +
        case 0 => try fs::readFile(path, &mut big[..]) catch err {
54 +
            set failure = err;
55 +
        },
56 +
        case 1 => try fs::makeDir(path) catch err {
57 +
            set failure = err;
58 +
        },
59 +
        case 2 => try fs::remove(path) catch err {
60 +
            set failure = err;
61 +
        },
62 +
        case 3 => try fs::create(path) catch err {
63 +
            set failure = err;
64 +
        },
65 +
        else => try fs::listDir(path, &mut small[..]) catch err {
66 +
            set failure = err;
67 +
        },
68 +
    }
69 +
    try testing::expect(failure == expected);
70 +
}
71 +
72 +
/// Written files read back unchanged, writing again truncates, an empty file
73 +
/// reads as zero bytes, parts and writer calls are written in order, and
74 +
/// rename moves a file and replaces an existing one.
75 +
@test fn files() throws (testing::TestError) {
76 +
    setup();
77 +
    for text in &["hello world", "hi", ""] {
78 +
        put(FILE_A, text);
79 +
        try expectFile(FILE_A, text);
80 +
    }
81 +
    try! fs::writeFileParts(FILE_A, &["hello", " ", "world"]);
82 +
    try expectFile(FILE_A, "hello world");
83 +
    let mut out = try! fs::create(FILE_B);
84 +
    out.write("ab");
85 +
    out.write("");
86 +
    out.write("cd");
87 +
    try! out.close();
88 +
    try! fs::rename(FILE_B, FILE_A);
89 +
    try expectFile(FILE_A, "abcd");
90 +
    try testing::expect(try! fs::stat(FILE_B) == nil);
91 +
    clean();
92 +
}
93 +
94 +
/// A file that exactly fills the buffer reads and one more byte is
95 +
/// `TooLarge`. Missing paths are `NotFound`, an existing directory is
96 +
/// `Exists`, and a directory with entries is `NotEmpty`.
97 +
@test unsafe fn errors() throws (testing::TestError) {
98 +
    setup();
99 +
    put(FILE_A, "12345");
100 +
    try expectFile(FILE_A, "12345");
101 +
    put(FILE_A, "123456");
102 +
    try expectFails(0, FILE_A, sys::Error::TooLarge);
103 +
    try expectFails(0, FILE_B, sys::Error::NotFound);
104 +
    try expectFails(1, DIR, sys::Error::Exists);
105 +
    try expectFails(1, SUB_FILE, sys::Error::NotFound);
106 +
    try expectFails(3, SUB_FILE, sys::Error::NotFound);
107 +
    try expectFails(2, FILE_B, sys::Error::NotFound);
108 +
    try! fs::makeDir(SUB);
109 +
    put(SUB_FILE, "2");
110 +
    try expectFails(2, SUB, sys::Error::NotEmpty);
111 +
    try! fs::remove(SUB_FILE);
112 +
    try! fs::remove(SUB);
113 +
    clean();
114 +
}
115 +
116 +
/// Stat reports the size and kind of files and directories, and `nil` for a
117 +
/// missing path. The listing holds each name once, an empty directory lists no
118 +
/// names, and a listing that does not fit is `TooLarge`.
119 +
@test unsafe fn statAndList() throws (testing::TestError) {
120 +
    static buffer: [u8; 64] = [0; 64];
121 +
    setup();
122 +
    let empty = try! fs::listDir(DIR, &mut buffer[..]);
123 +
    try testing::expect(empty.len == 0);
124 +
    put(FILE_A, "abc");
125 +
    put(FILE_B, "2");
126 +
    try! fs::makeDir(SUB);
127 +
    let file = try! fs::stat(FILE_A) else {
128 +
        throw testing::TestError::Failed;
129 +
    };
130 +
    let dir = try! fs::stat(SUB) else {
131 +
        throw testing::TestError::Failed;
132 +
    };
133 +
    try testing::expect(file.size == 3 and file.kind == fs::Kind::File);
134 +
    try testing::expect(dir.kind == fs::Kind::Directory and try! fs::stat(SUB_FILE) == nil);
135 +
    let listing = try! fs::listDir(DIR, &mut buffer[..]);
136 +
    let mut names = mem::split(listing, 0);
137 +
    let mut seen: u32 = 0;
138 +
    while let name = names.next() {
139 +
        if mem::eq(name, "a") {
140 +
            set seen |= 1;
141 +
        } else if mem::eq(name, "b") {
142 +
            set seen |= 2;
143 +
        } else if mem::eq(name, "sub") {
144 +
            set seen |= 4;
145 +
        } else {
146 +
            throw testing::TestError::Failed;
147 +
        }
148 +
    }
149 +
    try testing::expect(seen == 7);
150 +
    try expectFails(4, DIR, sys::Error::TooLarge);
151 +
    clean();
152 +
}
lib/std/io.rad +10 -20
1 1
//! Input/output utilities.
2 2
use std::fmt;
3 -
use std::intrinsics;
4 -
use std::sys::unix;
3 +
use std::sys::host;
5 4
6 -
/// Write the bytes to standard output.
5 +
/// Write the bytes to standard output. Failures are ignored.
7 6
export fn print(str: &[u8]) {
8 -
    unsafe {
9 -
        intrinsics::ecall(
10 -
            unix::SYS_WRITE, unix::STDOUT, str.ptr as i64, str.len as i64, 0
11 -
        );
12 -
    }
7 +
    try host::write(host::STDOUT, str) catch {};
13 8
}
14 9
15 -
/// Write the bytes to standard error.
10 +
/// Write the bytes to standard error. Failures are ignored.
16 11
export fn printError(str: &[u8]) {
17 -
    unsafe {
18 -
        intrinsics::ecall(
19 -
            unix::SYS_WRITE, unix::STDERR, str.ptr as i64, str.len as i64, 0
20 -
        );
21 -
    }
12 +
    try host::write(host::STDERR, str) catch {};
22 13
}
23 14
24 15
/// Write the bytes and a newline to standard output.
25 16
export fn printLn(str: &[u8]) {
26 17
    print(str);
46 37
    let mut buffer: [u8; fmt::BOOL_STR_LEN] = [0; fmt::BOOL_STR_LEN];
47 38
    let start = fmt::formatBool(val, &mut buffer[..]);
48 39
    print(&buffer[start..]);
49 40
}
50 41
51 -
/// Read standard input into the buffer and return the system call result.
42 +
/// Read standard input into the buffer and return the number of bytes read.
43 +
/// Returns zero at end of input or on failure.
52 44
export fn read(buf: &mut [u8]) -> u32 {
53 -
    unsafe {
54 -
        return intrinsics::ecall(
55 -
            unix::SYS_READ, unix::STDIN, buf.ptr as i64, buf.len as i64, 0
56 -
        ) as u32;
57 -
    }
45 +
    return try host::read(host::STDIN, buf) catch {
46 +
        return 0;
47 +
    };
58 48
}
59 49
60 50
export fn readToEnd(buf: *mut [u8]) -> *[u8] {
61 51
    let mut total: u32 = 0;
62 52
lib/std/lang/gen/data.rad +4 -11
169 169
                }
170 170
                for _ in 0..v.count {
171 171
                    match v.item {
172 172
                        case il::DataItem::Val { typ, val } => {
173 173
                            let size = il::typeSize(typ);
174 -
                            writeLe(buf, offset, val as u64, size);
174 +
                            mem::writeLe(buf, offset, val as u64, size);
175 175
                            set offset += size;
176 176
                        }
177 177
                        case il::DataItem::Sym(name) => {
178 178
                            let addr = lookupAddr(dataSymMap, name) else {
179 179
                                throw Error::Symbol;
180 180
                            };
181 -
                            writeLe(buf, offset, addr, @sizeOf(u64));
181 +
                            mem::writeLe(buf, offset, addr, @sizeOf(u64));
182 182
183 183
                            set offset += @sizeOf(u64);
184 184
                        }
185 185
                        case il::DataItem::Fn(name) => {
186 186
                            let functionOffset = dict::get(&fnLabels.funcs, name) else {
187 187
                                throw Error::Symbol;
188 188
                            };
189 189
                            if codeBase > mem::U64_MAX - functionOffset as u64 {
190 190
                                throw Error::Overflow;
191 191
                            }
192 -
                            writeLe(buf, offset, codeBase + functionOffset as u64, @sizeOf(u64));
192 +
                            let addr = codeBase + functionOffset as u64;
193 +
                            mem::writeLe(buf, offset, addr, @sizeOf(u64));
193 194
194 195
                            set offset += @sizeOf(*u8);
195 196
                        }
196 197
                        case il::DataItem::Str(s) => {
197 198
                            try! mem::copy(&mut buf[offset..], s);
214 215
        }
215 216
    }
216 217
    return offset;
217 218
}
218 219
219 -
/// Write the low `size` bytes of `value` to `buf` at `offset` in little-endian
220 -
/// order. The caller checks that `buf` has room for `size` bytes.
221 -
export fn writeLe(buf: &mut [u8], offset: u32, value: u64, size: u32) {
222 -
    for i in 0..size {
223 -
        set buf[offset + i] = (value >> (i * 8) as u64) as u8;
224 -
    }
225 -
}
226 -
227 220
/// Build a hash-indexed data symbol map from the laid-out symbols.
228 221
/// `table` must be an empty dictionary. The map takes the table.
229 222
/// Throws `Capacity` if the symbol count exceeds half the table length, and
230 223
/// `Symbol` for an empty or duplicate symbol name.
231 224
export fn buildMap(syms: *[DataSym], table: dict::Dict) -> DataSymMap throws (Error) {
lib/std/mem.rad +66 -0
40 40
        }
41 41
    }
42 42
    return &input[prefix.len..];
43 43
}
44 44
45 +
/// Return the offset of the first occurrence of `needle` in `text`, or `nil`.
46 +
export fn find(text: &[u8], needle: &[u8]) -> ?u32 {
47 +
    if needle.len > text.len {
48 +
        return nil;
49 +
    }
50 +
    for start in 0..text.len - needle.len + 1 {
51 +
        if eq(&text[start..start + needle.len], needle) {
52 +
            return start;
53 +
        }
54 +
    }
55 +
    return nil;
56 +
}
57 +
58 +
/// Check whether `text` ends with `suffix`.
59 +
export fn endsWith(text: &[u8], suffix: &[u8]) -> bool {
60 +
    return suffix.len <= text.len and eq(&text[text.len - suffix.len..], suffix);
61 +
}
62 +
63 +
/// Iterator over the parts of a byte string between separator bytes. A final
64 +
/// part needs no separator, and a separator at the very end adds no part.
65 +
export record Split: Copy {
66 +
    /// The whole byte string.
67 +
    text: *[u8],
68 +
    /// Separator byte.
69 +
    sep: u8,
70 +
    /// Offset of the next part.
71 +
    pos: u32,
72 +
}
73 +
74 +
/// Iterate over the parts of `text` between `sep` bytes.
75 +
export fn split(text: *[u8], sep: u8) -> Split {
76 +
    return Split { text, sep, pos: 0 };
77 +
}
78 +
79 +
/// Return the next part, or `nil` after the last.
80 +
export fn (s: &mut Split) next() -> ?*[u8] {
81 +
    if s.pos >= s.text.len {
82 +
        return nil;
83 +
    }
84 +
    let start = s.pos;
85 +
    let mut end = start;
86 +
    while end < s.text.len and s.text[end] <> s.sep {
87 +
        set end += 1;
88 +
    }
89 +
    set s.pos = end + 1;
90 +
    return &s.text[start..end];
91 +
}
92 +
45 93
/// Align value up to alignment boundary.
46 94
export fn alignUp(value: u32, alignment: u32) -> u32 {
47 95
    return (value + alignment - 1) & ~(alignment - 1);
48 96
}
49 97
117 165
    if aLen > bLen {
118 166
        return 1;
119 167
    }
120 168
    return 0;
121 169
}
170 +
171 +
/// Write the low `size` bytes of `value` to `buf` at `offset` in little-endian
172 +
/// order. The caller checks that `buf` has room for `size` bytes.
173 +
export fn writeLe(buf: &mut [u8], offset: u32, value: u64, size: u32) {
174 +
    for i in 0..size {
175 +
        set buf[offset + i] = (value >> (i * 8) as u64) as u8;
176 +
    }
177 +
}
178 +
179 +
/// Read `size` bytes of `buf` at `offset` as a little-endian unsigned value.
180 +
/// The caller checks that `buf` holds `size` bytes at `offset`.
181 +
export fn readLe(buf: &[u8], offset: u32, size: u32) -> u64 {
182 +
    let mut value: u64 = 0;
183 +
    for i in 0..size {
184 +
        set value = value | (buf[offset + i] as u64 << (i * 8) as u64);
185 +
    }
186 +
    return value;
187 +
}
lib/std/process.rad added +209 -0
1 +
//! Starting RV64 guest images through the host.
2 +
//!
3 +
//! A child is an image run by the same emulator as the parent. Guests never
4 +
//! name host executables.
5 +
6 +
use std::mem;
7 +
use std::sys;
8 +
use std::sys::host;
9 +
10 +
/// Destination of a child's standard output or standard error.
11 +
export union Output: Copy {
12 +
    /// Share the parent's stream.
13 +
    Inherit,
14 +
    /// Drop everything the child writes.
15 +
    Discard,
16 +
    /// Create or truncate the file at the path. Equal paths merge both streams.
17 +
    File(*[u8]),
18 +
}
19 +
20 +
/// Sizes of a hosted child, in KB. Zero selects the host default.
21 +
export record Memory: Copy {
22 +
    /// Guest memory size.
23 +
    memory: u32,
24 +
    /// Data segment size.
25 +
    data: u32,
26 +
    /// Stack size.
27 +
    stack: u32,
28 +
}
29 +
30 +
/// A machine interrupt source that fires once.
31 +
export record Irq: Copy {
32 +
    /// Interrupt source number. Zero is reserved for none.
33 +
    source: u32,
34 +
    /// Machine tick of the interrupt edge.
35 +
    tick: u64,
36 +
}
37 +
38 +
/// Machine configuration of a child that runs without the hosted runtime.
39 +
export record Machine: Copy {
40 +
    /// Hart count. Zero selects the host default.
41 +
    harts: u32,
42 +
    /// Memory size in KB. Zero selects the host default.
43 +
    memory: u32,
44 +
    /// One-shot interrupt, if any.
45 +
    irq: ?Irq,
46 +
}
47 +
48 +
/// How the host runs the image.
49 +
export union Target: Copy {
50 +
    /// Hosted program with the given sizes.
51 +
    Hosted(Memory),
52 +
    /// Hosted program with the parent's memory, data and stack sizes.
53 +
    Inherit,
54 +
    /// Bare machine.
55 +
    Machine(Machine),
56 +
}
57 +
58 +
/// Description of a child to start.
59 +
export record Command: Copy {
60 +
    /// Path of the RV64 image.
61 +
    image: *[u8],
62 +
    /// Arguments the child receives. Element zero is the first argument.
63 +
    args: *[*[u8]],
64 +
    /// Standard output destination.
65 +
    stdout: Output,
66 +
    /// Standard error destination.
67 +
    stderr: Output,
68 +
    /// Execution mode and sizes.
69 +
    target: Target,
70 +
    /// Instruction budget, or `nil` for the host default.
71 +
    maxSteps: ?u64,
72 +
}
73 +
74 +
/// Handle of a running child. It is valid until the child is waited for.
75 +
export record Child: Copy {
76 +
    /// Host identifier of the child.
77 +
    handle: u32,
78 +
}
79 +
80 +
/// How a child ended.
81 +
export union Outcome: Copy {
82 +
    /// The child exited with a status.
83 +
    Exited(i32),
84 +
    /// The child used up its step budget.
85 +
    StepLimit,
86 +
    /// The child faulted.
87 +
    Fault,
88 +
}
89 +
90 +
/// A finished child and how it ended.
91 +
export record Exit: Copy {
92 +
    /// The child that finished.
93 +
    child: Child,
94 +
    /// How it ended.
95 +
    outcome: Outcome,
96 +
}
97 +
98 +
/// Return the stream kind code of an output.
99 +
fn kind(output: Output) -> u64 {
100 +
    match output {
101 +
        case Output::Inherit => return 0,
102 +
        case Output::Discard => return 1,
103 +
        case Output::File(_) => return 2,
104 +
    }
105 +
}
106 +
107 +
/// Decode an `Outcome` record: a 32-bit kind (exited, step limit or fault)
108 +
/// and a 32-bit exit status.
109 +
fn decode(raw: &[u8]) -> Outcome {
110 +
    match mem::readLe(raw, 0, 4) {
111 +
        case 0 => return Outcome::Exited(mem::readLe(raw, 4, 4) as u32 as i32),
112 +
        case 1 => return Outcome::StepLimit,
113 +
        else   => return Outcome::Fault,
114 +
    }
115 +
}
116 +
117 +
/// Sequential writer of the little-endian fields of the host `Spawn` record.
118 +
/// The buffer starts zeroed, so padding and unset fields read as zero.
119 +
record Spawn {
120 +
    /// The 128-byte record.
121 +
    raw: [u8; 128],
122 +
    /// Offset of the next field.
123 +
    pos: u32,
124 +
}
125 +
126 +
/// Append an integer field of `size` bytes.
127 +
fn (s: &mut Spawn) put(value: u64, size: u32) {
128 +
    mem::writeLe(&mut s.raw[..], s.pos, value, size);
129 +
    set s.pos += size;
130 +
}
131 +
132 +
/// Append a slice field: 64-bit pointer, 32-bit length and padding.
133 +
fn (s: &mut Spawn) slice(pointer: i64, len: u32) {
134 +
    s.put(pointer as u64, 8);
135 +
    s.put(len as u64, 8);
136 +
}
137 +
138 +
/// Append the mode, harts, memory, data, stack, interrupt source and
139 +
/// interrupt tick fields. Without an interrupt the last two are zero.
140 +
fn (s: &mut Spawn) row(mode: u32, harts: u32, memory: u32, data: u32, stack: u32, irq: ?Irq) {
141 +
    for field in &[mode, harts, memory, data, stack] {
142 +
        s.put(field as u64, 4);
143 +
    }
144 +
    let mut edge = Irq { source: 0, tick: 0 };
145 +
    if let given = irq {
146 +
        set edge = given;
147 +
    }
148 +
    s.put(edge.source as u64, 4);
149 +
    s.put(edge.tick, 8);
150 +
}
151 +
152 +
/// Start a child. The child runs concurrently until it is waited for.
153 +
/// Throws `Limit` when too many children run, and `NotFound` or `Invalid`
154 +
/// when the image cannot be started.
155 +
export fn spawn(command: &Command) -> Child throws (sys::Error) {
156 +
    // The fields follow the Spawn record of the host ABI, in order.
157 +
    let mut s = Spawn { raw: [0; 128], pos: 0 };
158 +
    s.slice(host::addr(command.image), command.image.len);
159 +
    // The argument array is passed as the slice array it already is.
160 +
    unsafe {
161 +
        s.slice(command.args.ptr as i64, command.args.len);
162 +
    }
163 +
    s.put(kind(command.stdout), 4);
164 +
    s.put(kind(command.stderr), 4);
165 +
    for output in &[command.stdout, command.stderr] {
166 +
        if let case Output::File(file) = output {
167 +
            s.slice(host::addr(file), file.len);
168 +
        } else {
169 +
            s.put(0, 16);
170 +
        }
171 +
    }
172 +
    let mut flags: u64 = 0;
173 +
    match command.target {
174 +
        case Target::Hosted(m)  => s.row(0, 0, m.memory, m.data, m.stack, nil),
175 +
        case Target::Machine(m) => s.row(1, m.harts, m.memory, 0, 0, m.irq),
176 +
        case Target::Inherit => {
177 +
            s.row(0, 0, 0, 0, 0, nil);
178 +
            set flags = 1;
179 +
        }
180 +
    }
181 +
    let mut steps: u64 = 0;
182 +
    if let limit = command.maxSteps {
183 +
        set steps = limit;
184 +
    }
185 +
    s.put(steps, 8);
186 +
    s.put(flags, 4);
187 +
    let handle = try host::call(host::SPAWN, host::addr(&s.raw[..]), 0, 0, 0);
188 +
    return Child { handle: handle as u32 };
189 +
}
190 +
191 +
/// Wait for a child to finish and return how it ended.
192 +
export fn wait(child: Child) -> Outcome throws (sys::Error) {
193 +
    let mut raw: [u8; 8] = [0; 8];
194 +
    try host::call(host::WAIT, child.handle as i64, host::addr(&raw[..]), 0, 0);
195 +
    return decode(&raw[..]);
196 +
}
197 +
198 +
/// Wait for any child to finish. Throws `NotFound` when none is running.
199 +
export fn waitAny() -> Exit throws (sys::Error) {
200 +
    let mut raw: [u8; 8] = [0; 8];
201 +
    let handle = try host::call(host::WAIT_ANY, host::addr(&raw[..]), 0, 0, 0);
202 +
    return Exit { child: Child { handle: handle as u32 }, outcome: decode(&raw[..]) };
203 +
}
204 +
205 +
/// Start a child and wait for it.
206 +
export fn run(command: &Command) -> Outcome throws (sys::Error) {
207 +
    let child = try spawn(command);
208 +
    return try wait(child);
209 +
}
lib/std/sys.rad +24 -3
1 1
//! System utilities.
2 -
export mod unix;
2 +
export mod host;
3 +
4 +
/// Failure of a host operation. The host reports each failure as one of these
5 +
/// kinds, whatever the underlying operating system.
6 +
export union Error: Copy {
7 +
    /// No such file or directory, or no such child.
8 +
    NotFound,
9 +
    /// The path already exists.
10 +
    Exists,
11 +
    /// The host refused access.
12 +
    Denied,
13 +
    /// The directory is not empty.
14 +
    NotEmpty,
15 +
    /// Bad argument, mode, path, descriptor, or call number.
16 +
    Invalid,
17 +
    /// The output buffer is too small for the result.
18 +
    TooLarge,
19 +
    /// Too many open files or running children.
20 +
    Limit,
21 +
    /// Any other host failure.
22 +
    Io,
23 +
}
3 24
4 25
/// Process environment passed to the program entry point.
5 26
export record Env: Copy {
6 -
    /// Command-line arguments. Element zero is the executable name.
7 -
    /// User arguments start at element one.
27 +
    /// Command-line arguments. The host passes only the arguments that follow
28 +
    /// the image, so element zero is the first user argument.
8 29
    args: *[*[u8]],
9 30
}
lib/std/sys/host.rad added +80 -0
1 +
//! Hosted system call interface: an `ecall` with the call number and up to
2 +
//! four arguments. A negative result is the negated error code. Paths are
3 +
//! passed as pointer and length. The record layouts belong to `std::fs` and
4 +
//! `std::process`.
5 +
6 +
use std::intrinsics;
7 +
use std::sys;
8 +
9 +
/// Call number of `open`.
10 +
export constant OPEN: u32 = 0x403;
11 +
/// Call number of `close`.
12 +
export constant CLOSE: u32 = 0x404;
13 +
/// Call number of `stat`.
14 +
export constant STAT: u32 = 0x405;
15 +
/// Call number of `makeDir`.
16 +
export constant MAKE_DIR: u32 = 0x406;
17 +
/// Call number of `remove`.
18 +
export constant REMOVE: u32 = 0x407;
19 +
/// Call number of `rename`.
20 +
export constant RENAME: u32 = 0x408;
21 +
/// Call number of `listDir`.
22 +
export constant LIST_DIR: u32 = 0x409;
23 +
/// Call number of `spawn`.
24 +
export constant SPAWN: u32 = 0x40A;
25 +
/// Call number of `wait`.
26 +
export constant WAIT: u32 = 0x40B;
27 +
/// Call number of `waitAny`.
28 +
export constant WAIT_ANY: u32 = 0x40C;
29 +
30 +
/// Standard input descriptor.
31 +
export constant STDIN: i64 = 0;
32 +
/// Standard output descriptor.
33 +
export constant STDOUT: i64 = 1;
34 +
/// Standard error descriptor.
35 +
export constant STDERR: i64 = 2;
36 +
37 +
/// Return the address of the bytes as a call argument. The caller keeps the
38 +
/// storage alive until the call returns. This and [`callPath`] hold the only
39 +
/// pointer casts of the host interface.
40 +
export fn addr(bytes: &[u8]) -> i64 {
41 +
    unsafe {
42 +
        return bytes.ptr as i64;
43 +
    }
44 +
}
45 +
46 +
/// Issue a call. Returns the non-negative result, or throws the error that a
47 +
/// negative result stands for.
48 +
export fn call(number: u32, a: i64, b: i64, c: i64, d: i64) -> i64 throws (sys::Error) {
49 +
    let result = intrinsics::ecall(number, a, b, c, d);
50 +
    if result >= 0 {
51 +
        return result;
52 +
    }
53 +
    // Errors by negated result code. Codes beyond the table are `Io`.
54 +
    let errors: [sys::Error; 9] = [
55 +
        sys::Error::Io, sys::Error::NotFound, sys::Error::Exists,
56 +
        sys::Error::Denied, sys::Error::NotEmpty, sys::Error::Invalid,
57 +
        sys::Error::TooLarge, sys::Error::Limit, sys::Error::Io,
58 +
    ];
59 +
    let code = (0 - result) as u64;
60 +
    if code < errors.len as u64 {
61 +
        throw errors[code as u32];
62 +
    }
63 +
    throw sys::Error::Io;
64 +
}
65 +
66 +
/// Issue a call whose first two arguments are `path`.
67 +
export fn callPath(number: u32, path: &[u8], c: i64, d: i64) -> i64 throws (sys::Error) {
68 +
    return try call(number, addr(path), path.len as i64, c, d);
69 +
}
70 +
71 +
/// Read from a descriptor into the buffer (call 0x401). Returns the number of bytes read,
72 +
/// which is zero at end of file.
73 +
export fn read(fd: i64, buf: &mut [u8]) -> u32 throws (sys::Error) {
74 +
    return try call(0x401, fd, addr(buf), buf.len as i64, 0) as u32;
75 +
}
76 +
77 +
/// Write the buffer to a descriptor (call 0x402). Returns the number of bytes written.
78 +
export fn write(fd: i64, buf: &[u8]) -> u32 throws (sys::Error) {
79 +
    return try call(0x402, fd, addr(buf), buf.len as i64, 0) as u32;
80 +
}
lib/std/sys/unix.rad deleted +0 -184
1 -
//! Unix-specific system calls and utilities.
2 -
//! Path storage has a terminating zero byte. The kernel reads from the path
3 -
//! pointer through that byte. The slice length can exclude the terminator.
4 -
use std::intrinsics;
5 -
6 -
/// Linux RV64 openat system call number.
7 -
constant SYS_OPENAT: i64 = 56;
8 -
/// Linux RV64 close system call number.
9 -
constant SYS_CLOSE: i64 = 57;
10 -
/// Linux RV64 read system call number.
11 -
export constant SYS_READ: i64 = 63;
12 -
/// Linux RV64 write system call number.
13 -
export constant SYS_WRITE: i64 = 64;
14 -
/// Linux RV64 exit system call number.
15 -
constant SYS_EXIT: i64 = 93;
16 -
/// Output file mode: owner read/write, group and others read.
17 -
export constant FILE_MODE: i64 = 420;
18 -
19 -
/// File access modes.
20 -
export record OpenFlags(i64): Copy;
21 -
22 -
/// Open file for reading only.
23 -
export constant O_RDONLY: OpenFlags = OpenFlags(0);
24 -
25 -
/// Open file for writing only.
26 -
export constant O_WRONLY: OpenFlags = OpenFlags(1);
27 -
28 -
/// Open file for reading and writing.
29 -
export constant O_RDWR: OpenFlags = OpenFlags(2);
30 -
31 -
/// Create file if it doesn't exist.
32 -
export constant O_CREAT: OpenFlags = OpenFlags(64);
33 -
34 -
/// Truncate file to zero length.
35 -
export constant O_TRUNC: OpenFlags = OpenFlags(512);
36 -
37 -
/// Standard file descriptor for stdin.
38 -
export constant STDIN: i64 = 0;
39 -
40 -
/// Standard file descriptor for stdout.
41 -
export constant STDOUT: i64 = 1;
42 -
43 -
/// Standard file descriptor for stderr.
44 -
export constant STDERR: i64 = 2;
45 -
46 -
/// Special value representing current working directory for `openat()`.
47 -
constant AT_FDCWD: i64 = -100;
48 -
49 -
/// Opens a file at the given path and returns a file descriptor.
50 -
/// Returns a negative value on error.
51 -
/// The path storage must have a terminating zero byte.
52 -
export fn open(path: &[u8], flags: OpenFlags) -> i64 {
53 -
    unsafe {
54 -
        return intrinsics::ecall(
55 -
            SYS_OPENAT, AT_FDCWD, path.ptr as i64, *flags, 0
56 -
        );
57 -
    }
58 -
}
59 -
60 -
/// Opens a file at the given path with mode, returns a file descriptor.
61 -
/// The path storage must have a terminating zero byte.
62 -
export fn openOpts(path: &[u8], flags: OpenFlags, mode: i64) -> i64 {
63 -
    unsafe {
64 -
        return intrinsics::ecall(
65 -
            SYS_OPENAT, AT_FDCWD, path.ptr as i64, *flags, mode
66 -
        );
67 -
    }
68 -
}
69 -
70 -
/// Reads from a file descriptor into the provided buffer.
71 -
/// Returns the number of bytes read, or a negative value on error.
72 -
export fn read(fd: i64, buf: &mut [u8]) -> i64 {
73 -
    unsafe {
74 -
        return intrinsics::ecall(
75 -
            SYS_READ, fd, buf.ptr as i64, buf.len as i64, 0
76 -
        );
77 -
    }
78 -
}
79 -
80 -
/// Reads from a file descriptor until EOF or buffer is full.
81 -
/// Returns the total number of bytes read, or a negative value on error.
82 -
export fn readToEnd(fd: i64, buf: &mut [u8]) -> i64 {
83 -
    let mut total: u32 = 0;
84 -
    while total < buf.len {
85 -
        let n = read(fd, &mut buf[total..]);
86 -
87 -
        if n < 0 {
88 -
            return n;
89 -
        }
90 -
        if n == 0 {
91 -
            break;
92 -
        }
93 -
        set total += n as u32;
94 -
    }
95 -
    return total as i64;
96 -
}
97 -
98 -
/// Writes to a file descriptor from the provided buffer.
99 -
/// Returns the number of bytes written, or a negative value on error.
100 -
export fn write(fd: i64, buf: &[u8]) -> i64 {
101 -
    unsafe {
102 -
        return intrinsics::ecall(
103 -
            SYS_WRITE, fd, buf.ptr as i64, buf.len as i64, 0
104 -
        );
105 -
    }
106 -
}
107 -
108 -
/// Writes the entire contents of a buffer to a file descriptor.
109 -
/// Returns `false` when the descriptor cannot accept the full buffer.
110 -
export fn writeAll(fd: i64, data: &[u8]) -> bool {
111 -
    let mut written: u32 = 0;
112 -
    while written < data.len {
113 -
        let n = write(fd, &data[written..]);
114 -
        if n <= 0 {
115 -
            return false;
116 -
        }
117 -
        set written += n as u32;
118 -
    }
119 -
    return true;
120 -
}
121 -
122 -
/// Closes a file descriptor.
123 -
/// Returns `0` on success, or a negative value on error.
124 -
export fn close(fd: i64) -> i64 {
125 -
    return intrinsics::ecall(SYS_CLOSE, fd, 0, 0, 0);
126 -
}
127 -
128 -
/// Reads the entire contents of a file at the given path into the provided buffer.
129 -
/// Return the number of bytes read, or `nil` on error.
130 -
/// The path storage must have a terminating zero byte.
131 -
export fn readFile(path: &[u8], buf: &mut [u8]) -> ?u32 {
132 -
    let fd = open(path, O_RDONLY);
133 -
    if fd < 0 {
134 -
        return nil;
135 -
    }
136 -
    let n = readToEnd(fd, buf);
137 -
138 -
    if close(fd) < 0 {
139 -
        return nil;
140 -
    }
141 -
    if n < 0 {
142 -
        return nil;
143 -
    }
144 -
    return n as u32;
145 -
}
146 -
147 -
/// Exit the current process with the given status code.
148 -
export fn exit(status: i64) {
149 -
    intrinsics::ecall(SYS_EXIT, status, 0, 0, 0);
150 -
}
151 -
152 -
/// Writes the entire contents of a buffer to a file at the given path.
153 -
/// Creates the file if it doesn't exist, truncates if it does.
154 -
/// Returns `true` on success.
155 -
/// The path storage must have a terminating zero byte.
156 -
export fn writeFile(path: &[u8], data: &[u8]) -> bool {
157 -
    let flags = OpenFlags(*O_WRONLY | *O_CREAT | *O_TRUNC);
158 -
    let fd = openOpts(path, flags, FILE_MODE);
159 -
    if fd < 0 {
160 -
        return false;
161 -
    }
162 -
    let written = writeAll(fd, data);
163 -
    let closed = close(fd) == 0;
164 -
    return written and closed;
165 -
}
166 -
167 -
/// Writes each buffer to a file at the given path.
168 -
/// Creates the file if it doesn't exist, truncates if it does.
169 -
/// Returns `true` only when every part and the final close succeed.
170 -
/// The path storage must have a terminating zero byte.
171 -
export fn writeFileParts(path: &[u8], parts: &[*[u8]]) -> bool {
172 -
    let flags = OpenFlags(*O_WRONLY | *O_CREAT | *O_TRUNC);
173 -
    let fd = openOpts(path, flags, FILE_MODE); // 0644 in octal.
174 -
    if fd < 0 {
175 -
        return false;
176 -
    }
177 -
    for part in parts {
178 -
        if not writeAll(fd, part) {
179 -
            close(fd);
180 -
            return false;
181 -
        }
182 -
    }
183 -
    return close(fd) == 0;
184 -
}
lib/std/tests.rad +30 -15
2 2
3 3
use std::fmt;
4 4
use std::mem;
5 5
use std::vec;
6 6
use std::testing;
7 -
use std::sys::unix;
7 +
use std::sys::host;
8 8
use std::lang::alloc;
9 9
use std::io;
10 10
use std::collections::dict;
11 11
12 12
/// Safe I/O and equality accept empty buffers.
13 13
@test fn testSafeEmptyBuffers() throws (testing::TestError) {
14 14
    io::print("");
15 15
    io::printError("");
16 -
    try testing::expect(unix::write(unix::STDOUT, "") == 0);
16 +
    let n = try host::write(host::STDOUT, "") catch {
17 +
        throw testing::TestError::Failed;
18 +
    };
19 +
    try testing::expect(n == 0);
17 20
    try testing::expect(mem::eq("", ""));
18 21
}
19 22
20 23
// dict ////////////////////////////////////////////////////////////////////////
21 24
349 352
        set threw = true;
350 353
    };
351 354
    try testing::expect(threw);
352 355
}
353 356
354 -
@test fn testWriteFileParts() throws (testing::TestError) {
355 -
    let path = "/tmp/radiance-std-write-file-parts.test";
356 -
    let mut buffer: [u8; 16] = [0; 16];
357 -
358 -
    if not unix::writeFileParts(path, &["hello", " ", "world"]) {
359 -
        throw testing::TestError::Failed;
360 -
    }
361 -
    let data = unix::readFile(path, &mut buffer[..]) else {
362 -
        throw testing::TestError::Failed;
363 -
    };
364 -
    try testing::expectBytesEq(&buffer[..data], "hello world");
365 -
}
366 -
367 357
@test fn testStripPrefixMatch() throws (testing::TestError) {
368 358
    let input: *[u8] = "hello world";
369 359
    let prefix: *[u8] = "hello";
370 360
371 361
    let result = mem::stripPrefix(prefix, input) else {
398 388
399 389
    let result = mem::stripPrefix(prefix, input);
400 390
    try testing::expect(result == nil);
401 391
}
402 392
393 +
@test fn testFindAndEndsWith() throws (testing::TestError) {
394 +
    try testing::expect(mem::find("hello", "llo") == 2);
395 +
    try testing::expect(mem::find("hello", "") == 0);
396 +
    try testing::expect(mem::find("hello", "world") == nil);
397 +
    try testing::expect(mem::find("lo", "hello") == nil);
398 +
    try testing::expect(mem::endsWith("main.rad", ".rad"));
399 +
    try testing::expect(mem::endsWith("rad", "rad"));
400 +
    try testing::expectNot(mem::endsWith("rad", "main.rad"));
401 +
    try testing::expectNot(mem::endsWith("main.ras", ".rad"));
402 +
}
403 +
404 +
@test fn testSplit() throws (testing::TestError) {
405 +
    let expected: [*[u8]; 4] = ["a", "", "bc", "d"];
406 +
    let mut parts = mem::split("a,,bc,d,", ',');
407 +
    for want in &expected[..] {
408 +
        let part = parts.next() else {
409 +
            throw testing::TestError::Failed;
410 +
        };
411 +
        try testing::expectBytesEq(part, want);
412 +
    }
413 +
    try testing::expect(parts.next() == nil);
414 +
    let mut none = mem::split("", ',');
415 +
    try testing::expect(none.next() == nil);
416 +
}
417 +
403 418
@test fn testEqMatchingSlices() throws (testing::TestError) {
404 419
    let a: *[u8] = "abc";
405 420
    let b: *[u8] = "abc";
406 421
407 422
    try testing::expect(mem::eq(a, b));
scripts/fmt deleted +0 -29
1 -
#!/bin/sh
2 -
# Format Radiance sources in place with `radiance -fmt`.
3 -
# Usage: scripts/fmt <source>...
4 -
#
5 -
# Each source is formatted into a temporary file, which replaces the source
6 -
# only if formatting succeeded and the text changed. Sources are processed in
7 -
# parallel by `JOBS` workers. A failure is reported with its diagnostics and
8 -
# does not stop the others, but makes the exit status nonzero.
9 -
set -eu
10 -
11 -
compiler=${RAD_BIN:-bin/radiance.rv64.dev}
12 -
13 -
if [ "${1:-}" = --one ]; then
14 -
    source=$2
15 -
    work=$(mktemp -d)
16 -
    trap 'rm -rf "$work"' EXIT HUP INT TERM
17 -
18 -
    if ! scripts/hosted "$compiler" -fmt "$source" > "$work/out" 2> "$work/log"; then
19 -
        printf 'fmt %s ... FAILED\n%s\n' "$source" "$(cat "$work/log")"
20 -
        exit 1
21 -
    fi
22 -
    if ! cmp -s "$source" "$work/out"; then
23 -
        cat "$work/out" > "$source"
24 -
        echo "fmt $source"
25 -
    fi
26 -
    exit 0
27 -
fi
28 -
29 -
printf '%s\n' "$@" | xargs -P "${JOBS:-1}" -n 1 sh "$0" --one
scripts/hosted deleted +0 -5
1 -
#!/bin/sh
2 -
# Run hosted RV64 build tools with shared memory and stack limits.
3 -
set -eu
4 -
exec "${RAD_EMULATOR:-emulator}" -memory-size=385024 -data-size=348416 \
5 -
    -stack-size=1024 -count-instructions -run "$@"
std.lib +3 -1
6 6
lib/std/io.rad
7 7
lib/std/intrinsics.rad
8 8
lib/std/sys.rad
9 9
lib/std/collections.rad
10 10
lib/std/collections/dict.rad
11 -
lib/std/sys/unix.rad
11 +
lib/std/sys/host.rad
12 +
lib/std/fs.rad
13 +
lib/std/process.rad
12 14
lib/std/arch.rad
13 15
lib/std/arch/rv64.rad
14 16
lib/std/arch/rv64/image.rad
15 17
lib/std/arch/rv64/encode.rad
16 18
lib/std/arch/rv64/decode.rad
std.lib.test +1 -0
1 1
lib/std/testing.rad
2 2
lib/std/tests.rad
3 +
lib/std/fs/tests.rad
3 4
lib/std/char/tests.rad
4 5
lib/std/arch/rv64/tests.rad
5 6
lib/std/arch/rv64/asm/tests.rad
6 7
lib/std/arch/rv64/asm/scanner/tests.rad
7 8
lib/std/lang/alloc/tests.rad
test/Makefile +74 -49
1 1
# Test suites and machine fixture matrix. Run from the repository root.
2 -
# JOBS controls workers inside test scripts.
2 +
# JOBS controls workers in the integration driver.
3 3
JOBS ?= 1
4 -
export JOBS
5 4
TEST_HARTS   := 1 2 8
6 5
SMP_TESTS    := $(addprefix smp-test-,$(TEST_HARTS))
7 6
CYCLES_TESTS := $(addprefix cycles-test-,$(TEST_HARTS))
8 -
TESTS        := seed-test std-test fmt-test bin-test \
7 +
TESTS        := seed-test std-test fmt-test host-test bin-test \
9 8
    module-test \
10 9
    native-test shared-test sync-test verify-test kernel-test \
11 10
    kernel-boot-test trap-test loader-test dispatch-test $(SMP_TESTS) \
12 11
    termination-test runtime-test bootstrap-test scheduling-test mmio-test \
13 12
    $(CYCLES_TESTS)
17 16
18 17
# Hosted test tools share one compiler recipe.
19 18
# Runner binary: the lowering IL checker.
20 19
TEST_TOOL_SRC := test/runner.rad test/native/build.rad \
21 20
    test/shared/build.rad test/sync/build.rad test/loader/hostile.rad \
22 -
    test/verify/check.rad
21 +
    test/verify/check.rad test/host/child.rad
23 22
TEST_TOOLS := $(TEST_TOOL_SRC:.rad=.rv64)
24 23
25 24
$(TEST_TOOLS): %.rv64: %.rad $(STD_LIB) $(RAD_BIN) test/Makefile
26 25
	@$(RADIANCE) $(STD) -pkg tool -mod $< -entry tool -o $@
27 26
28 -
seed-test:
29 -
	@seed/test
27 +
# The integration driver runs the checks that start other programs: the
28 +
# compiler, the test tools and the emulator. Each of its commands works in
29 +
# bin/test/NAME.
30 +
INTEGRATION      := test/integration.rv64
31 +
INTEGRATION_SRC  := $(sort $(wildcard test/integration/*.rad))
32 +
INTEGRATION_OPTS := -compiler $(RAD_BIN) -jobs $(JOBS)
33 +
# Run the driver command $(1) with the arguments $(2).
34 +
integration = $(HOSTED) $(INTEGRATION) $(1) $(INTEGRATION_OPTS) $(2)
35 +
36 +
$(INTEGRATION): test/integration.rad $(INTEGRATION_SRC) $(STD_LIB) $(RAD_BIN) \
37 +
    test/Makefile
38 +
	@$(RADIANCE) $(STD) -pkg integration -mod test/integration.rad \
39 +
	    $(patsubst %,-mod %,$(INTEGRATION_SRC)) -entry integration -o $@
40 +
41 +
seed-test: $(INTEGRATION)
42 +
	@$(call integration,seed)
43 +
44 +
# std::process against the emulator.
45 +
host-test: $(INTEGRATION) test/host/child.rv64 $(BIN_DIR)/native.fixture.rv64
46 +
	@$(call integration,host)
30 47
31 48
module-test: $(RAD_BIN) test/compiler/discovery.rv64
32 49
	@$(HOSTED) test/compiler/discovery.rv64
33 50
34 51
# Standard Library Tests
45 62
	@rm -f lib/std.test.rv64 lib/std.test.rv64.debug \
46 63
	    lib/std.test.rv64.s lib/std.test.rv64.o
47 64
48 65
# The source formatter keeps the AST of every standard library and compiler
49 66
# source, and formats its own output to itself.
50 -
fmt-test: $(RAD_BIN) test/fmt/run
51 -
	@sh test/fmt/run
67 +
FMT_TEST_SRC := $(shell find lib compiler test/integration test/host -name '*.rad' | sort) \
68 +
    test/integration.rad
52 69
53 -
kernel-test: $(KERNEL_TEST)
70 +
fmt-test: $(RAD_BIN) $(INTEGRATION)
71 +
	@$(call integration,fmt,$(FMT_TEST_SRC))
72 +
73 +
kernel-test: $(KERNEL_TEST) $(INTEGRATION) $(RAD_BIN)
54 74
	@$(HOSTED) $(KERNEL_TEST)
55 -
	@sh test/slots/run
75 +
	@$(call integration,slots)
56 76
57 -
kernel-boot-test: $(BIN_DIR)/kernel.rv64
58 -
	@sh test/boot/run
77 +
kernel-boot-test: $(BIN_DIR)/kernel.rv64 $(INTEGRATION)
78 +
	@$(call integration,boot)
59 79
60 80
# Native image execution through the configured machine-capable emulator.
61 81
native-test: $(BIN_DIR)/native.fixture.rv64
62 -
	@$${RAD_EMULATOR:-emulator} -machine -max-steps=1000000 -run $<
82 +
	@$(RAD_EMULATOR) -machine -max-steps=1000000 -run $<
63 83
64 84
$(BIN_DIR)/native.fixture.rv64: test/native/build.rv64 test/Makefile
65 -
	@$(HOSTED) $< -- $@
85 +
	@$(HOSTED) $< $@
66 86
67 87
# Shared package execution
68 -
shared-test: test/shared/build.rv64
69 -
	@sh test/shared/run
70 -
	@sh test/shared/errors/run
88 +
shared-test: test/shared/build.rv64 $(INTEGRATION) $(RAD_BIN)
89 +
	@$(call integration,shared)
71 90
72 91
# Kernel synchronization machine tests
73 -
sync-test: test/sync/build.rv64
74 -
	@sh test/sync/run
92 +
sync-test: test/sync/build.rv64 $(INTEGRATION)
93 +
	@$(call integration,sync)
75 94
76 95
# Binary Tests
77 96
78 97
# Only tests with `//! returns:` are compiled to binaries and executed.
79 98
BIN_TEST_SRC := $(shell grep -l '^//! returns:' \
80 99
    $(wildcard test/tests/*.rad test/tests/*.ras))
81 100
BIN_TEST_BIN := $(addsuffix .rv64,$(basename $(BIN_TEST_SRC)))
82 101
83 -
bin-test: test/runner.rv64 $(BIN_TEST_BIN)
84 -
	@sh test/run
102 +
bin-test: test/runner.rv64 $(INTEGRATION) $(BIN_TEST_BIN)
103 +
	@$(call integration,bin)
85 104
86 105
# A `.rad` executable test can have a same-basename `.ras` module.
87 106
# Compile each executable test to a binary. A test with `//! uses-std` links
88 107
# the standard library. Child modules and assembly inputs are dependencies.
89 108
# Fixtures that use std must rebuild when a library source changes.
90 109
# The compiler depends on the standard library source manifest and its modules.
91 -
test/tests/%.rv64: test/tests/%.rad $(RAD_BIN) test/compile test/Makefile
92 -
	@sh test/compile $< $@
110 +
test/tests/%.rv64: test/tests/%.rad $(RAD_BIN) $(INTEGRATION) test/Makefile
111 +
	@$(call integration,compile,$< $@)
93 112
94 113
test/tests/%.rv64: test/tests/%.ras test/runner.rv64 test/Makefile
95 -
	@$(HOSTED) test/runner.rv64 -- assemble $< $@
114 +
	@$(HOSTED) test/runner.rv64 assemble $< $@
96 115
97 116
# Multi-file fixture inputs.
98 117
test/tests/asm.link.rv64: test/tests/asm.link.ras
99 118
test/tests/asm.rodata.prefix.rv64: test/tests/asm.rodata.prefix.ras
100 119
test/tests/assign.qualified.static.rv64: \
115 134
test/tests/start.exit.rv64: test/tests/start.exit.start.ras
116 135
test/tests/wildcard.import.owner.rv64: \
117 136
    test/tests/wildcard.import.owner/wildcard_import_owner.rad
118 137
119 138
clean-bin-test:
120 -
	@rm -f $(TEST_TOOLS) $(BIN_TEST_BIN) test/compiler/discovery.rv64 \
139 +
	@rm -f $(TEST_TOOLS) $(INTEGRATION) $(BIN_TEST_BIN) \
140 +
	    test/compiler/discovery.rv64 \
121 141
	    $(wildcard test/tests/*.rv64.debug test/tests/*.rv64.s) \
122 142
	    $(TEST_TOOLS:.rv64=.rv64.debug) $(TEST_TOOLS:.rv64=.rv64.s) \
123 143
	    $(TEST_TOOLS:.rv64=.rv64.o)
124 144
125 145
# Integer trap contexts and nested machine interrupts.
126 -
trap-test: test/sync/build.rv64
127 -
	@sh test/trap/run
146 +
trap-test: test/sync/build.rv64 $(INTEGRATION)
147 +
	@$(call integration,trap)
128 148
129 149
# Runtime package compilation and execution through the production kernel.
130 150
# Generator of the hostile packages that the loader must reject.
131 -
loader-test: $(BIN_DIR)/kernel.build.rv64 test/loader/hostile.rv64
132 -
	@sh test/loader/run
151 +
loader-test: $(BIN_DIR)/kernel.build.rv64 test/loader/hostile.rv64 $(INTEGRATION) $(RAD_BIN)
152 +
	@$(call integration,loader)
133 153
134 154
# Machine fixtures build one kernel image each; every hart count then runs
135 155
# the same image as its own target.
136 156
FIXTURE_DEPS := $(RAD_BIN) $(BIN_DIR)/kernel.build.rv64 \
137 -
    test/kernel/build test/kernel/embed test/kernel/fixture.rad \
157 +
    $(INTEGRATION) test/kernel/fixture.rad \
138 158
    test/kernel.rad test/Makefile kernel/mmio.rad $(KERNEL_MODULES) $(KERNEL_ASM)
139 159
DISPATCH_IMAGES := $(BIN_DIR)/dispatch.rv64 $(BIN_DIR)/smp.rv64 \
140 160
    $(BIN_DIR)/termination.rv64 $(BIN_DIR)/mmio.rv64
141 161
142 -
# Dispatch fixtures are built by test/dispatch/build from test/NAME.
162 +
# Dispatch fixtures are built from test/NAME by the integration driver.
143 163
$(DISPATCH_IMAGES): $(BIN_DIR)/%.rv64: test/%/spin.rad test/%/machine.ras \
144 -
    test/%/kernel/dispatchcheck.rad test/dispatch/build $(FIXTURE_DEPS)
145 -
	@sh test/dispatch/build $* $@
164 +
    test/%/kernel/dispatchcheck.rad $(FIXTURE_DEPS)
165 +
	@$(call integration,dispatch-build,$* $@)
146 166
147 167
$(BIN_DIR)/termination.rv64: test/termination/cases.rad
148 168
$(BIN_DIR)/mmio.rv64: test/mmio/cases.rad
149 169
$(BIN_DIR)/smp.rv64: test/smp/kernel/invariants.rad test/smp/cases.rad
150 170
171 +
# Run the machine fixture image $(2) on $(3) harts with the extra emulator
172 +
# flags $(4), and report $(1) when it exits with status zero.
173 +
MACHINE_FLAGS := -machine -memory-size=262144 -max-steps=70000000000
174 +
fixture = $(RAD_EMULATOR) $(MACHINE_FLAGS) -harts=$(3) $(4) -run $(2) \
175 +
    && echo "$(1): $(3)-hart execution passed"
176 +
151 177
# Native preemption across user code, idle, and retained machine work.
152 178
dispatch-test: $(BIN_DIR)/dispatch.rv64
153 -
	@sh test/dispatch/run dispatch $< 1
179 +
	@$(call fixture,dispatch,$<,1)
154 180
155 181
# Concurrent user contexts and metadata transactions on every online hart.
156 182
smp-test: $(SMP_TESTS)
157 183
$(SMP_TESTS): smp-test-%: $(BIN_DIR)/smp.rv64
158 -
	@sh test/dispatch/run smp $< $* 7
184 +
	@$(call fixture,smp,$<,$*,-irq=7 -irq-at=1)
159 185
160 186
# User exit, abort, and CPU faults preserve the surviving parent's execution.
161 187
termination-test: $(BIN_DIR)/termination.rv64
162 -
	@sh test/dispatch/run termination $< 1
188 +
	@$(call fixture,termination,$<,1)
163 189
164 190
# Scheduled compilation, cancellation, retry, and shared executable publication.
165 -
runtime-test: $(BIN_DIR)/kernel.build.rv64
166 -
	@sh test/runtime/run
191 +
runtime-test: $(BIN_DIR)/kernel.build.rv64 $(INTEGRATION) $(RAD_BIN)
192 +
	@$(call integration,runtime)
167 193
168 194
# Root lifetime and resource authority across the boot-package handoff.
169 -
bootstrap-test: $(BIN_DIR)/kernel.build.rv64
170 -
	@sh test/bootstrap/run
195 +
bootstrap-test: $(BIN_DIR)/kernel.build.rv64 $(INTEGRATION) $(RAD_BIN)
196 +
	@$(call integration,bootstrap)
171 197
172 198
# Fixture root and scheduler packages execute authority changes in user mode.
173 -
scheduling-test: $(BIN_DIR)/kernel.rv64
174 -
	@sh test/scheduling/run
199 +
scheduling-test: $(BIN_DIR)/kernel.rv64 $(INTEGRATION) $(RAD_BIN)
200 +
	@$(call integration,scheduling)
175 201
176 202
# Checked user-mode register access, including rejection before device effects.
177 203
mmio-test: $(BIN_DIR)/mmio.rv64
178 -
	@sh test/dispatch/run mmio $< 1
204 +
	@$(call fixture,mmio,$<,1)
179 205
180 206
# Dependency, controller, and worker loading under every hart count.
181 -
$(BIN_DIR)/cycles.rv64: test/cycles/build test/cycles/spin.rad \
207 +
$(BIN_DIR)/cycles.rv64: test/cycles/spin.rad \
182 208
    test/cycles/job.rad test/cycles/support.rad test/cycles/machine.ras \
183 209
    test/cycles/kernel/dispatchcheck.rad test/smp/kernel/invariants.rad \
184 210
    $(FIXTURE_DEPS)
185 -
	@sh test/cycles/build $@
211 +
	@$(call integration,cycles-build,$@)
186 212
187 213
cycles-test: $(CYCLES_TESTS)
188 214
$(CYCLES_TESTS): cycles-test-%: $(BIN_DIR)/cycles.rv64
189 -
	@$${RAD_EMULATOR:-emulator} -machine -harts=$* -memory-size=262144 \
190 -
	    -max-steps=10000000000 -run $<
215 +
	@$(call fixture,cycles,$<,$*)
191 216
192 217
# The verifier accepts or rejects the binary RIL of each fixture as
193 218
# test/verify/expected says.
194 -
verify-test: test/verify/check.rv64
195 -
	@sh test/verify/run
219 +
verify-test: test/verify/check.rv64 $(INTEGRATION) $(RAD_BIN)
220 +
	@$(call integration,verify)
196 221
197 222
# Exercise the production test discovery module with bounded storage.
198 223
test/compiler/discovery.rv64: test/compiler/discovery.rad \
199 224
    compiler/radiance/testgen.rad $(STD_LIB) $(RAD_BIN) test/Makefile
200 225
	@$(RADIANCE) $(STD) -pkg testgen -mod compiler/radiance/testgen.rad \
test/boot/run deleted +0 -48
1 -
#!/bin/sh
2 -
# Boot the kernel through its production initialization on each supported hart count.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
work=$(mktemp -d)
6 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 -
cat test/boot/machine.ras kernel/kernel/*.ras > "$work/boot.ras"
8 -
scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril "$work/boot.ras" "$work/boot.rv64"
9 -
for harts in 1 2 8; do
10 -
    # The deterministic emulator shares its tick budget across all harts.
11 -
    steps=$((10000000 * harts))
12 -
    if ! "$emulator" -machine -max-steps="$steps" -harts="$harts" \
13 -
        -run "$work/boot.rv64" > "$work/log" 2>&1 \
14 -
        || ! grep -q '^kernel: platform ready$' "$work/log" \
15 -
        || ! grep -q '^kernel: boot catalog ready$' "$work/log"; then
16 -
        cat "$work/log" >&2
17 -
        exit 1
18 -
    fi
19 -
    printf 'kernel boot: %s harts passed\n' "$harts"
20 -
done
21 -
status=0
22 -
"$emulator" -machine -harts=8 -max-steps=80000000 -run bin/kernel.rv64 > "$work/log" 2>&1 || status=$?
23 -
if [ "$status" -ne 2 ] || [ "$(grep -c 'wfi=1 mcause=0x0' "$work/log")" -ne 8 ]; then
24 -
    cat "$work/log" >&2
25 -
    exit 1
26 -
fi
27 -
printf 'kernel startup: all eight harts reached machine idle without traps\n'
28 -
cat test/boot/trap.ras kernel/kernel/*.ras > "$work/trap.ras"
29 -
scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril "$work/trap.ras" "$work/trap.rv64"
30 -
status=0
31 -
"$emulator" -machine -harts=2 -max-steps=20000000 -run "$work/trap.rv64" > "$work/log" 2>&1 || status=$?
32 -
if [ "$status" -ne 2 ] || [ "$(grep -c '^kernel: unexpected trap$' "$work/log")" -ne 1 ] \
33 -
    || [ "$(grep -c 'wfi=1' "$work/log")" -ne 2 ]; then
34 -
    cat "$work/log" >&2
35 -
    exit 1
36 -
fi
37 -
printf 'kernel trap: shared kernel state restored from the hart anchor\n'
38 -
39 -
# Assembly failures report the input location and return a compiler error status.
40 -
printf '.text;\ninvalid;\n' > "$work/invalid.ras"
41 -
status=0
42 -
scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril \
43 -
    "$work/invalid.ras" "$work/invalid.rv64" > "$work/invalid.log" 2>&1 || status=$?
44 -
if [ "$status" -ne 1 ] || ! grep -Fq \
45 -
    "$work/invalid.ras:2:1: error: unknown instruction" "$work/invalid.log"; then
46 -
    cat "$work/invalid.log" >&2
47 -
    exit 1
48 -
fi
test/bootstrap/run deleted +0 -16
1 -
#!/bin/sh
2 -
# Construct root from a boot package and retain authority after root exits.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
work=$(mktemp -d)
6 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 -
scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \
8 -
    -pkg loaded -mod test/bootstrap/root.rad \
9 -
    -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad \
10 -
    -entry loaded -ril "$work"
11 -
scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \
12 -
    -pkg service -mod test/bootstrap/service.rad -entry service -ril "$work"
13 -
sh test/kernel/build bootstrap "$work" "$work/bootstrap.rv64" \
14 -
    "$work/loaded.ril" "$work/service.ril"
15 -
"$emulator" -machine -harts=1 -memory-size=262144 -max-steps=2000000000 -run "$work/bootstrap.rv64"
16 -
printf 'bootstrap: root exit preserves allocation and creation authority\n'
test/compile deleted +0 -42
1 -
#!/bin/sh
2 -
# Compile a compiler fixture to an executable or a binary RIL directory.
3 -
# Usage: sh test/compile source output
4 -
set -eu
5 -
source=$1
6 -
output=$2
7 -
base=${source%.rad}
8 -
case "$output" in
9 -
    *.rv64) set -- -o "$output"; expected=$output ;;
10 -
    *)
11 -
        mkdir -p "$output"
12 -
        set -- -ril "$output"
13 -
        expected=$output/test.ril
14 -
        ;;
15 -
esac
16 -
17 -
entry=false
18 -
if grep -q '^//! uses-std' "$source"; then
19 -
    set -- "$@" -pkg std
20 -
    while IFS= read -r module; do
21 -
        set -- "$@" -mod "$module"
22 -
    done < std.lib
23 -
    entry=true
24 -
fi
25 -
if grep -q '^//! stack-check' "$source"; then
26 -
    set -- "$@" -stack-check
27 -
fi
28 -
set -- "$@" -pkg test -mod "$source"
29 -
for module in "$base"/*.rad "$base.ras"; do
30 -
    if [ -f "$module" ]; then
31 -
        set -- "$@" -mod "$module"
32 -
    fi
33 -
done
34 -
if [ -f "$base.start.ras" ]; then
35 -
    set -- "$@" -start "$base.start.ras"
36 -
fi
37 -
if [ "$entry" = true ]; then
38 -
    set -- "$@" -entry test
39 -
fi
40 -
rm -f "$expected"
41 -
timeout 60 scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@"
42 -
test -f "$expected"
test/cycles/build deleted +0 -23
1 -
#!/bin/sh
2 -
# Build the controller and worker images loaded after dispatch starts.
3 -
# Usage: build image
4 -
set -eu
5 -
image=$1
6 -
# Stage beside the image so the final move is atomic.
7 -
work=$(mktemp -d "$(dirname "$image")/.build.XXXXXX")
8 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
9 -
for package in spin job; do
10 -
    set -- -pkg "$package" -mod "test/cycles/$package.rad" \
11 -
        -mod support=test/cycles/support.rad \
12 -
        -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad
13 -
    if [ "$package" = spin ]; then
14 -
        set -- "$@" -mod mmio=kernel/mmio.rad
15 -
    fi
16 -
    scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry "$package" -ril "$work"
17 -
done
18 -
printf '//! Binary inputs for repeated domain lifecycles.\n' > "$work/inputs.rad"
19 -
for package in spin job; do
20 -
    sh test/kernel/embed "$package" "$work/$package.ril" >> "$work/inputs.rad"
21 -
done
22 -
sh test/kernel/build cycles "$work" "$work/cycles.rv64"
23 -
mv "$work/cycles.rv64" "$image"
test/dispatch/build deleted +0 -22
1 -
#!/bin/sh
2 -
# Build a production dispatch fixture image.
3 -
# Usage: build fixture image
4 -
set -eu
5 -
fixture=$1
6 -
image=$2
7 -
# Stage beside the image so the final move is atomic.
8 -
work=$(mktemp -d "$(dirname "$image")/.build.XXXXXX")
9 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
10 -
set -- -pkg spin -mod "test/$fixture/spin.rad" \
11 -
    -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad
12 -
if [ -f "test/$fixture/cases.rad" ]; then
13 -
    set -- "$@" -mod "cases=test/$fixture/cases.rad"
14 -
fi
15 -
if [ "$fixture" = mmio ]; then
16 -
    set -- "$@" -mod mmio=kernel/mmio.rad
17 -
fi
18 -
scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry spin -ril "$work"
19 -
printf '//! Binary inputs for the dispatch fixture.\n' > "$work/inputs.rad"
20 -
sh test/kernel/embed INPUT "$work/spin.ril" >> "$work/inputs.rad"
21 -
sh test/kernel/build "$fixture" "$work" "$work/dispatch.rv64"
22 -
mv "$work/dispatch.rv64" "$image"
test/dispatch/run deleted +0 -15
1 -
#!/bin/sh
2 -
# Preempt and resume U-mode and M-mode work through production dispatch.
3 -
# Usage: run fixture image harts [irq]
4 -
set -eu
5 -
emulator=${RAD_EMULATOR:-emulator}
6 -
fixture=$1
7 -
image=$2
8 -
harts=$3
9 -
irq=${4:-0}
10 -
set --
11 -
if [ "$irq" -ne 0 ]; then
12 -
    set -- "-irq=$irq" "-irq-at=1"
13 -
fi
14 -
"$emulator" -machine -harts="$harts" -memory-size=262144 -max-steps=2000000000 "$@" -run "$image"
15 -
printf '%s: %s-hart execution passed\n' "$fixture" "$harts"
test/fmt/run deleted +0 -127
1 -
#!/bin/sh
2 -
# Check the source formatter on every Radiance source under the given
3 -
# directories, which default to lib and compiler, and on its error handling.
4 -
# Usage: sh test/fmt/run [<directory>...]
5 -
#
6 -
# For each source, `radiance -fmt` must succeed, and the formatted text must
7 -
# parse to the same AST as the source, and format to itself. Sources are
8 -
# checked in parallel by `JOBS` workers, which run `run --check <source>`.
9 -
set -eu
10 -
11 -
compiler=${RAD_BIN:-bin/radiance.rv64.dev}
12 -
13 -
# Run `radiance` with the given arguments. The output goes to "$work/out", the
14 -
# diagnostics to "$work/log", and the exit status to "$status".
15 -
format() {
16 -
    status=0
17 -
    scripts/hosted "$compiler" "$@" > "$work/out" 2> "$work/log" || status=$?
18 -
}
19 -
20 -
# Check one source and print a line if it fails.
21 -
checkSource() {
22 -
    source=$1
23 -
    work=$(mktemp -d)
24 -
    trap 'rm -rf "$work"' EXIT HUP INT TERM
25 -
26 -
    format -fmt "$source"
27 -
    if [ "$status" -ne 0 ]; then
28 -
        printf 'fmt %s ... FAILED (format)\n%s\n' \
29 -
            "$source" "$(cat "$work/log")"
30 -
        exit 1
31 -
    fi
32 -
    mv "$work/out" "$work/once.rad"
33 -
    format -fmt "$work/once.rad"
34 -
    if [ "$status" -ne 0 ]; then
35 -
        printf 'fmt %s ... FAILED (format of the output)\n%s\n' \
36 -
            "$source" "$(cat "$work/log")"
37 -
        exit 1
38 -
    fi
39 -
    if ! cmp -s "$work/once.rad" "$work/out"; then
40 -
        printf 'fmt %s ... FAILED (not idempotent)\n%s\n' "$source" \
41 -
            "$(diff "$work/once.rad" "$work/out" | head -20)"
42 -
        exit 1
43 -
    fi
44 -
    format -pkg fmt -mod "$source" -dump ast
45 -
    if [ "$status" -ne 0 ]; then
46 -
        printf 'fmt %s ... FAILED (AST dump)\n%s\n' \
47 -
            "$source" "$(cat "$work/log")"
48 -
        exit 1
49 -
    fi
50 -
    mv "$work/out" "$work/source.ast"
51 -
    format -pkg fmt -mod "$work/once.rad" -dump ast
52 -
    if [ "$status" -ne 0 ]; then
53 -
        printf 'fmt %s ... FAILED (AST dump of the output)\n%s\n' \
54 -
            "$source" "$(cat "$work/log")"
55 -
        exit 1
56 -
    fi
57 -
    if ! cmp -s "$work/source.ast" "$work/out"; then
58 -
        printf 'fmt %s ... FAILED (AST changed)\n%s\n' "$source" \
59 -
            "$(diff "$work/source.ast" "$work/out" | head -20)"
60 -
        exit 1
61 -
    fi
62 -
}
63 -
64 -
if [ "${1:-}" = --check ]; then
65 -
    checkSource "$2"
66 -
    exit 0
67 -
fi
68 -
69 -
if [ $# -eq 0 ]; then
70 -
    set -- lib compiler
71 -
fi
72 -
work=$(mktemp -d)
73 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
74 -
75 -
find "$@" -name '*.rad' | sort > "$work/sources"
76 -
workers=0
77 -
xargs -P "${JOBS:-1}" -n 1 sh test/fmt/run --check < "$work/sources" \
78 -
    > "$work/report" || workers=$?
79 -
cat "$work/report"
80 -
total=$(wc -l < "$work/sources")
81 -
failed=$(grep -c '^fmt .* \.\.\. FAILED' "$work/report" || true)
82 -
broken=0
83 -
84 -
# A syntax error exits with status 1 and is reported on standard error only,
85 -
# so that standard output holds nothing but formatted text.
86 -
printf 'let x = ;\n' > "$work/bad.rad"
87 -
format -fmt "$work/bad.rad"
88 -
if [ "$status" -ne 1 ] || [ -s "$work/out" ] ||
89 -
    ! grep -q 'error: ' "$work/log"; then
90 -
    echo "fmt syntax error ... FAILED (status $status, or stdout output)"
91 -
    cat "$work/out" "$work/log"
92 -
    broken=$((broken + 1))
93 -
fi
94 -
95 -
# A wrong number of arguments exits with status 1 and prints the usage.
96 -
expectUsage() {
97 -
    format -fmt "$@"
98 -
    if [ "$status" -ne 1 ] || ! grep -q '^usage:' "$work/log"; then
99 -
        echo "fmt $* ... FAILED (status $status, or no usage)"
100 -
        cat "$work/out" "$work/log"
101 -
        broken=$((broken + 1))
102 -
    fi
103 -
}
104 -
expectUsage
105 -
expectUsage "$work/bad.rad" "$work/bad.rad"
106 -
expectUsage -fmt.wrap 0 "$work/bad.rad"
107 -
expectUsage -fmt.wrap x "$work/bad.rad"
108 -
expectUsage -fmt.wrap
109 -
110 -
# The width defaults to 100 columns and `-fmt.wrap` sets it. This statement
111 -
# is 95 columns wide, so it wraps at 80 only.
112 -
printf 'fn f() {\n    call(%s);\n}\n' \
113 -
    "$(printf 'argument, %.0s' 1 2 3 4 5 6 7 8)last" > "$work/wide.rad"
114 -
format -fmt "$work/wide.rad"
115 -
lines=$(wc -l < "$work/out")
116 -
format -fmt -fmt.wrap 80 "$work/wide.rad"
117 -
if [ "$lines" -ne 3 ] || [ "$(wc -l < "$work/out")" -le 3 ]; then
118 -
    echo "fmt wrap ... FAILED (default $lines lines)"
119 -
    broken=$((broken + 1))
120 -
fi
121 -
if [ "$workers" -ne 0 ] && [ "$failed" -eq 0 ]; then
122 -
    echo "fmt: workers exited with status $workers"
123 -
    broken=$((broken + 1))
124 -
fi
125 -
echo "fmt: $((total - failed)) source files round-trip," \
126 -
    "$((failed + broken)) failed"
127 -
test "$((failed + broken))" -eq 0
test/host/child.rad added +122 -0
1 +
//! Child program of the process tests. The first argument selects what it
2 +
//! does, so one image covers every outcome the host can report.
3 +
//!
4 +
//!     exit <status>     Exit with the decimal status, which may be negative.
5 +
//!     stdout <text>     Print the text and a newline to standard output.
6 +
//!     stderr <text>     Print the text and a newline to standard error.
7 +
//!     mixed             Print "one" to standard output, "two" to standard
8 +
//!                       error, then "three" to standard output.
9 +
//!     echo <args>...    Print each argument in angle brackets on its own line.
10 +
//!     stdin             Exit with the number of bytes read from standard
11 +
//!                       input.
12 +
//!     spin              Loop forever.
13 +
//!     fault             Access memory outside the guest.
14 +
//!     reach <kb>        Ask the host to read standard input into a buffer
15 +
//!                       that extends the given number of KB past the start of
16 +
//!                       the data segment. The host faults the guest when the
17 +
//!                       buffer leaves guest memory, so the outcome tells how
18 +
//!                       much memory the child has.
19 +
20 +
use std::fmt;
21 +
use std::intrinsics;
22 +
use std::mem;
23 +
use std::sys;
24 +
use std::sys::host;
25 +
26 +
/// Bytes in one KB, the unit of the `reach` size.
27 +
constant KB: u32 = 1024;
28 +
/// Exit status of a child that was started with unknown arguments.
29 +
constant USAGE_STATUS: i32 = 2;
30 +
/// First byte of the data segment, which the `reach` buffer starts at.
31 +
static ANCHOR: [u8; 1] = [0; 1];
32 +
33 +
/// Write the text to a descriptor.
34 +
fn say(fd: i64, text: *[u8]) {
35 +
    try! host::write(fd, text);
36 +
}
37 +
38 +
/// Parse a decimal number with an optional leading minus sign.
39 +
fn parseStatus(text: *[u8]) -> ?i32 {
40 +
    let mut digits = text;
41 +
    let mut negative = false;
42 +
    if digits.len > 0 and digits[0] == '-' {
43 +
        set negative = true;
44 +
        set digits = &digits[1..];
45 +
    }
46 +
    let literal = try fmt::parseInt(digits) catch {
47 +
        return nil;
48 +
    };
49 +
    if literal.magnitude > 0x7fffffff {
50 +
        return nil;
51 +
    }
52 +
    if negative {
53 +
        return 0 - literal.magnitude as i32;
54 +
    }
55 +
    return literal.magnitude as i32;
56 +
}
57 +
58 +
/// Ask the host to write from an address outside guest memory, which faults
59 +
/// the guest.
60 +
fn touchOutside() -> i64 {
61 +
    return intrinsics::ecall(0x402, host::STDOUT, 0x7fffffff00000000, 1, 0);
62 +
}
63 +
64 +
@default fn main(env: *sys::Env) -> i32 {
65 +
    let args = env.args;
66 +
    if args.len == 0 {
67 +
        return USAGE_STATUS;
68 +
    }
69 +
    let mode = args[0];
70 +
    if mem::eq(mode, "exit") and args.len == 2 {
71 +
        let status = parseStatus(args[1]) else {
72 +
            return USAGE_STATUS;
73 +
        };
74 +
        return status;
75 +
    }
76 +
    if mem::eq(mode, "stdout") and args.len == 2 {
77 +
        say(host::STDOUT, args[1]);
78 +
        say(host::STDOUT, "\n");
79 +
        return 0;
80 +
    }
81 +
    if mem::eq(mode, "stderr") and args.len == 2 {
82 +
        say(host::STDERR, args[1]);
83 +
        say(host::STDERR, "\n");
84 +
        return 0;
85 +
    }
86 +
    if mem::eq(mode, "mixed") {
87 +
        say(host::STDOUT, "one\n");
88 +
        say(host::STDERR, "two\n");
89 +
        say(host::STDOUT, "three\n");
90 +
        return 0;
91 +
    }
92 +
    if mem::eq(mode, "echo") {
93 +
        for arg in &args[1..] {
94 +
            say(host::STDOUT, "<");
95 +
            say(host::STDOUT, arg);
96 +
            say(host::STDOUT, ">\n");
97 +
        }
98 +
        return 0;
99 +
    }
100 +
    if mem::eq(mode, "stdin") {
101 +
        let mut probe: [u8; 64] = [0; 64];
102 +
        let n = try! host::read(host::STDIN, &mut probe[..]);
103 +
        return n as i32;
104 +
    }
105 +
    if mem::eq(mode, "spin") {
106 +
        while true {}
107 +
    }
108 +
    if mem::eq(mode, "fault") {
109 +
        return touchOutside() as i32;
110 +
    }
111 +
    if mem::eq(mode, "reach") and args.len == 2 {
112 +
        let kb = parseStatus(args[1]) else {
113 +
            return USAGE_STATUS;
114 +
        };
115 +
        unsafe {
116 +
            let buf = @sliceOf(&mut ANCHOR[0] as *mut u8, kb as u32 * KB);
117 +
            try! host::read(host::STDIN, buf);
118 +
        }
119 +
        return 0;
120 +
    }
121 +
    return USAGE_STATUS;
122 +
}
test/integration.rad added +125 -0
1 +
//! Integration test driver. It runs the checks that need other programs: the
2 +
//! compiler, the test tools and the emulator. Make starts it as
3 +
//!
4 +
//!     integration <command> [-compiler <path>] [-jobs <n>] [arguments...]
5 +
//!
6 +
//! and its exit status is zero when every check passed. Each command works in
7 +
//! `bin/test/<command>`, which it empties first and leaves in place afterwards.
8 +
//!
9 +
//! Commands:
10 +
//!
11 +
//!     host                      Check std::process against the emulator.
12 +
//!     compile <source> <out>    Compile a compiler test fixture.
13 +
//!     bin [tests...]            Check IL snapshots and run test binaries.
14 +
//!     seed                      Check the format of the checked-in seed.
15 +
//!     sync                      Run the synchronization fixture on 1, 2 and 8 harts.
16 +
//!     trap                      Run the trap fixtures.
17 +
//!     shared                    Run the shared package fixtures.
18 +
//!     slots                     Check that reservation misuse is rejected.
19 +
//!     dispatch-build <fixture> <image>
20 +
//!                               Build a dispatch fixture image.
21 +
//!     cycles-build <image>      Build the cycles fixture image.
22 +
//!     boot                      Boot the kernel and check traps.
23 +
//!     bootstrap                 Check root construction from a boot package.
24 +
//!     loader                    Check the runtime loader and hostile packages.
25 +
//!     runtime                   Check scheduled loading and cancellation.
26 +
//!     scheduling [harts...]     Check root handoff and user-space yields.
27 +
//!     fmt <sources...>          Check that the formatter round-trips sources.
28 +
//!     format <sources...>       Format sources in place.
29 +
//!     verify [checker]          Check the IL verifier on the fixtures.
30 +
31 +
mod harness;
32 +
mod fixtures;
33 +
mod format;
34 +
mod host;
35 +
mod kernel;
36 +
mod machine;
37 +
mod boot;
38 +
mod verify;
39 +
40 +
use std::io;
41 +
use std::mem;
42 +
use std::sys;
43 +
44 +
/// Compiler used when `-compiler` is not given.
45 +
constant DEFAULT_COMPILER: *[u8] = "bin/radiance.rv64.dev";
46 +
/// Exit status of a command line that cannot be run.
47 +
constant USAGE_STATUS: i32 = 2;
48 +
/// Usage message.
49 +
constant USAGE: *[u8] =
50 +
    "usage: integration <command> [-compiler <path>] [-jobs <n>] [arguments...]\nSee test/integration.rad for the commands.\n";
51 +
52 +
/// A command, how many arguments it takes, and its handler.
53 +
record Entry: Copy {
54 +
    /// Name given on the command line.
55 +
    name: *[u8],
56 +
    /// Fewest arguments.
57 +
    min: u32,
58 +
    /// Most arguments.
59 +
    max: u32,
60 +
    /// The handler.
61 +
    run: fn(&mut harness::Harness, *[*[u8]]) throws (sys::Error),
62 +
}
63 +
64 +
/// Print the usage message and return the exit status of a bad command line.
65 +
fn usage() -> i32 {
66 +
    io::printError(USAGE);
67 +
    return USAGE_STATUS;
68 +
}
69 +
70 +
/// Run the command named by the first argument. The options `-compiler` and
71 +
/// `-jobs` follow it, and the remaining arguments belong to the command.
72 +
@default fn main(env: *sys::Env, storage: *mut harness::Storage) -> i32 {
73 +
    let args = env.args;
74 +
    let mut compiler = DEFAULT_COMPILER;
75 +
    let mut jobs: u32 = 1;
76 +
    let mut first: u32 = 1;
77 +
    while first + 1 < args.len {
78 +
        if mem::eq(args[first], "-compiler") {
79 +
            set compiler = args[first + 1];
80 +
        } else if mem::eq(args[first], "-jobs") {
81 +
            set jobs = try harness::number(args[first + 1]) catch {
82 +
                return usage();
83 +
            };
84 +
        } else {
85 +
            break;
86 +
        }
87 +
        set first += 2;
88 +
    }
89 +
    if args.len == 0 or jobs < 1 or jobs > harness::MAX_JOBS {
90 +
        return usage();
91 +
    }
92 +
    // The commands.
93 +
    let commands: [Entry; 18] = [
94 +
        Entry { name: "host",           min: 0, max: 0, run: host::check },
95 +
        Entry { name: "compile",        min: 2, max: 2, run: fixtures::compile },
96 +
        Entry { name: "bin",            min: 0, max: mem::MAX_SIZE, run: fixtures::bin },
97 +
        Entry { name: "seed",           min: 0, max: 0, run: machine::seed },
98 +
        Entry { name: "sync",           min: 0, max: 0, run: machine::sync },
99 +
        Entry { name: "trap",           min: 0, max: 0, run: machine::trap },
100 +
        Entry { name: "shared",         min: 0, max: 0, run: machine::shared },
101 +
        Entry { name: "slots",          min: 0, max: 0, run: machine::slots },
102 +
        Entry { name: "dispatch-build", min: 2, max: 2, run: machine::dispatchBuild },
103 +
        Entry { name: "cycles-build",   min: 1, max: 1, run: machine::cyclesBuild },
104 +
        Entry { name: "boot",           min: 0, max: 0, run: boot::boot },
105 +
        Entry { name: "bootstrap",      min: 0, max: 0, run: boot::bootstrap },
106 +
        Entry { name: "loader",         min: 0, max: 0, run: boot::loader },
107 +
        Entry { name: "runtime",        min: 0, max: 0, run: boot::runtime },
108 +
        Entry { name: "scheduling",     min: 0, max: mem::MAX_SIZE, run: boot::scheduling },
109 +
        Entry { name: "fmt",            min: 0, max: mem::MAX_SIZE, run: format::check },
110 +
        Entry { name: "format",         min: 1, max: mem::MAX_SIZE, run: format::rewrite },
111 +
        Entry { name: "verify",         min: 0, max: 1, run: verify::check },
112 +
    ];
113 +
    for entry in &commands[..] {
114 +
        let rest = &args[first..];
115 +
        if mem::eq(entry.name, args[0]) and rest.len >= entry.min and rest.len <= entry.max {
116 +
            let mut h = harness::new(storage, compiler, jobs);
117 +
            try entry.run(&mut h, rest) catch err {
118 +
                io::printError(h.join(&[args[0], ": ", harness::errorName(err), "\n"]));
119 +
                return 1;
120 +
            };
121 +
            return h.status();
122 +
        }
123 +
    }
124 +
    return usage();
125 +
}
test/integration/boot.rad added +242 -0
1 +
//! Kernel boot and user-mode fixtures that run the production kernel image:
2 +
//! boot on every hart count, bootstrap, runtime loading, scheduling, and the
3 +
//! runtime loader with hostile packages.
4 +
5 +
use std::fs;
6 +
use std::io;
7 +
use std::process;
8 +
use std::sys;
9 +
use super::harness;
10 +
use super::kernel;
11 +
use super::harness::Harness;
12 +
use super::harness::Expect;
13 +
use std::process::Outcome;
14 +
use super::kernel::Embedded;
15 +
16 +
/// Hostile packages the generator writes, with the names of their arrays.
17 +
constant HOSTILE: [Embedded; 6] = [
18 +
    Embedded { name: "HOSTILE_FORGED",    file: "forged" },
19 +
    Embedded { name: "HOSTILE_OVERWRITE", file: "overwrite" },
20 +
    Embedded { name: "HOSTILE_LEAK",      file: "leak" },
21 +
    Embedded { name: "HOSTILE_EXTERN",    file: "extern" },
22 +
    Embedded { name: "HOSTILE_DEPENDENT", file: "dependent" },
23 +
    Embedded { name: "HOSTILE_BUFFER",    file: "buffer" },
24 +
];
25 +
/// Exit status of the hosted loaded package.
26 +
constant LOADED_STATUS: i32 = 43;
27 +
/// Number of exported functions that the runtime fixture adds to the loaded
28 +
/// package.
29 +
constant GENERATED_FUNCTIONS: u32 = 128;
30 +
31 +
/// Tick budget of the startup run. The run reaches idle on every hart and keeps
32 +
/// running, so it ends at the limit by design, and the budget is how long the
33 +
/// kernel gets to reach idle.
34 +
constant STARTUP_STEPS: u64 = 80000000;
35 +
/// Tick budget of the trap run, which ends at the limit by design after the
36 +
/// kernel reports the trap.
37 +
constant TRAP_STEPS: u64 = 20000000;
38 +
39 +
/// Build an image from the kernel assembly, led by `first`, and the installed
40 +
/// kernel binary IL. Returns the path of the image, or `nil` when it failed.
41 +
fn assemble(h: &mut Harness, name: *[u8], first: *[u8]) -> ?*[u8] throws (sys::Error) {
42 +
    let assembly = h.path(&[name, ".ras"]);
43 +
    let image = h.path(&[name, ".rv64"]);
44 +
    let what = h.join(&["build the ", name, " image"]);
45 +
    try kernel::concatAssembly(h, assembly, first);
46 +
    if kernel::link(h, what, kernel::RIL_DIR, assembly, image, &[], harness::OK, &[]) {
47 +
        return image;
48 +
    }
49 +
    return nil;
50 +
}
51 +
52 +
/// Boot the kernel on each supported hart count, run the production kernel to
53 +
/// idle on eight harts, check the trap fixture, and check that invalid assembly
54 +
/// is rejected with its location.
55 +
export fn boot(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
56 +
    try h.begin("boot");
57 +
    if let image = try assemble(h, "boot", "test/boot/machine.ras") {
58 +
        let ready = [
59 +
            Expect::Line("kernel: platform ready"),
60 +
            Expect::Line("kernel: boot catalog ready"),
61 +
        ];
62 +
        for harts in &[1, 2, 8] {
63 +
            let count = h.num(harts);
64 +
            let mark = h.failed;
65 +
            let run = h.machine(image, harts, h.out(&["boot", count, ".log"]));
66 +
            h.run(h.join(&["kernel boot on ", count, " harts"]), run, harness::OK, &ready[..]);
67 +
            h.done(mark, h.join(&["kernel boot: ", count, " harts passed"]));
68 +
        }
69 +
    }
70 +
    let mark = h.failed;
71 +
    let mut startup = h.machine("bin/kernel.rv64", 8, h.out(&["startup.log"]));
72 +
    set startup.maxSteps = STARTUP_STEPS;
73 +
    let idle = Expect::Matching { text: "wfi=1 mcause=0x0", count: 8 };
74 +
    h.run("kernel startup", startup, Outcome::StepLimit, &[idle]);
75 +
    h.done(mark, "kernel startup: all eight harts reached machine idle without traps");
76 +
    if let image = try assemble(h, "trap", "test/boot/trap.ras") {
77 +
        let mark = h.failed;
78 +
        let mut trap = h.machine(image, 2, h.out(&["trap.log"]));
79 +
        set trap.maxSteps = TRAP_STEPS;
80 +
        let reported = [
81 +
            Expect::Lines { text: "kernel: unexpected trap", count: 1 },
82 +
            Expect::Matching { text: "wfi=1",                count: 2 },
83 +
        ];
84 +
        h.run("kernel trap", trap, Outcome::StepLimit, &reported[..]);
85 +
        h.done(mark, "kernel trap: shared kernel state restored from the hart anchor");
86 +
    }
87 +
    let invalid = h.path(&["invalid.ras"]);
88 +
    let location = h.join(&[invalid, ":2:1: error: unknown instruction"]);
89 +
    try fs::writeFile(invalid, ".text;\ninvalid;\n");
90 +
    let rejected = Outcome::Exited(1);
91 +
    let expects = [Expect::Contains(location)];
92 +
    let image = h.path(&["invalid.rv64"]);
93 +
    kernel::link(
94 +
        h, "invalid assembly", kernel::RIL_DIR, invalid, image, &[], rejected, &expects[..]
95 +
    );
96 +
}
97 +
98 +
/// Construct root from a boot package and check that the kernel retains
99 +
/// authority after root exits.
100 +
export fn bootstrap(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
101 +
    try h.begin("bootstrap");
102 +
    let root = kernel::package(h, "loaded", "test/bootstrap/root.rad", &[kernel::ABI, kernel::SYS]);
103 +
    let service = kernel::package(h, "service", "test/bootstrap/service.rad", &[]);
104 +
    let image = h.path(&["bootstrap.rv64"]);
105 +
    let packages = [h.path(&["loaded.ril"]), h.path(&["service.ril"])];
106 +
    if root
107 +
        and service
108 +
        and try kernel::image(h, "bootstrap", image, &[], &packages[..])
109 +
        and h.boot("bootstrap", image, 1)
110 +
    {
111 +
        io::printLn("bootstrap: root exit preserves allocation and creation authority");
112 +
    }
113 +
}
114 +
115 +
/// Compile, cancel and retry a package through scheduled user calls.
116 +
export fn runtime(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
117 +
    try h.begin("runtime");
118 +
    let mods = ["cases=test/runtime/cases.rad", kernel::ABI, kernel::SYS];
119 +
    let spin = kernel::package(h, "spin", "test/runtime/spin.rad", &mods[..]);
120 +
    // The loaded package is the loader fixture with many added functions.
121 +
    let source = h.path(&["loaded.rad"]);
122 +
    let mut out = try fs::create(source);
123 +
    out.write(try h.load("test/loader/loaded.rad"));
124 +
    for index in 0..GENERATED_FUNCTIONS {
125 +
        let n = h.num(index);
126 +
        out.write(
127 +
            h.join(
128 +
                &[
129 +
                    "\n/// Compute a distinct exported value.\nexport fn value", n,
130 +
                    "(input: u64) -> u64 { return input + ", n, "; }\n",
131 +
                ]
132 +
            )
133 +
        );
134 +
    }
135 +
    try out.close();
136 +
    let loaded = kernel::package(h, "loaded", source, &[]);
137 +
    let image = h.path(&["dispatch.rv64"]);
138 +
    let inputs = [
139 +
        Embedded { name: "INPUT",   file: h.path(&["spin.ril"]) },
140 +
        Embedded { name: "PAYLOAD", file: h.path(&["loaded.ril"]) },
141 +
    ];
142 +
    if spin
143 +
        and loaded
144 +
        and try kernel::image(h, "runtime", image, &inputs[..], &[])
145 +
        and h.boot("runtime", image, 1)
146 +
    {
147 +
        io::printLn("runtime: scheduled cancellation, retry, and publication passed");
148 +
    }
149 +
}
150 +
151 +
/// Execute root handoff, user-space yields and window expiry in fixture
152 +
/// packages, on each hart count in `args`, or on 1, 2 and 8 harts when `args`
153 +
/// is empty.
154 +
export fn scheduling(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
155 +
    let mut harts: *mut [u32] = &mut [];
156 +
    for arg in args {
157 +
        try! harts.append(try harness::number(arg), &mut h.arena);
158 +
    }
159 +
    if args.len == 0 {
160 +
        for count in &[1, 2, 8] {
161 +
            try! harts.append(count, &mut h.arena);
162 +
        }
163 +
    }
164 +
    try h.begin("scheduling");
165 +
    let mods = [kernel::ABI, kernel::SYS, kernel::MMIO];
166 +
    let root = kernel::package(h, "root", "test/scheduling/root.rad", &mods[..2]);
167 +
    let scheduler = kernel::package(h, "scheduler", "test/scheduling/scheduler.rad", &mods[..]);
168 +
    let packages = [h.path(&["root.ril"]), h.path(&["scheduler.ril"])];
169 +
    let assembly = h.path(&["scheduling.ras"]);
170 +
    let image = h.path(&["scheduling.rv64"]);
171 +
    try kernel::concatAssembly(h, assembly, nil);
172 +
    let linked = kernel::link(
173 +
        h,
174 +
        "build the scheduling image",
175 +
        kernel::RIL_DIR,
176 +
        assembly,
177 +
        image,
178 +
        &packages[..],
179 +
        harness::OK,
180 +
        &[]
181 +
    );
182 +
    if root and scheduler and linked {
183 +
        for count in &harts[..] {
184 +
            let number = h.num(count);
185 +
            if h.boot(h.join(&["scheduling on ", number, " harts"]), image, count) {
186 +
                io::print(
187 +
                    h.join(
188 +
                        &[
189 +
                            "scheduling: ", number,
190 +
                            "-hart root handoff, four user-space yields, 513 retired windows, and checked shutdown passed\n",
191 +
                        ]
192 +
                    )
193 +
                );
194 +
            }
195 +
        }
196 +
    }
197 +
}
198 +
199 +
/// Build the loader fixture, run it, and check that the image tool rejects a
200 +
/// forged package and writes no image for it.
201 +
export fn loader(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
202 +
    try h.begin("loader");
203 +
    let mark = h.failed;
204 +
    let program = h.path(&["hosted.rv64"]);
205 +
    let il = kernel::package(h, "loaded", "test/loader/loaded.rad", &[]);
206 +
    h.add(&["-pkg", "loaded", "-mod", "test/loader/loaded.rad", "-entry", "loaded", "-o", program]);
207 +
    let image = h.compile("compile loaded to an image", h.out(&["image.log"]));
208 +
    let hosted = h.cmd(program, &[], harness::HOSTED, harness::RUN_STEPS, harness::INHERIT);
209 +
    let hostile = h.path(&["hostile"]);
210 +
    try fs::makeDir(hostile);
211 +
    let mut entries: *mut [Embedded] = &mut [];
212 +
    try! entries.append(
213 +
        Embedded { name: "INPUT", file: h.path(&["loaded.ril"]) }, &mut h.arena
214 +
    );
215 +
    for entry in &HOSTILE[..] {
216 +
        let file = h.join(&[hostile, "/", entry.file, ".ril"]);
217 +
        try! entries.append(Embedded { name: entry.name, file }, &mut h.arena);
218 +
    }
219 +
    let fixture = h.path(&["loader.rv64"]);
220 +
    let rejected = h.path(&["rejected.rv64"]);
221 +
    let work = h.work;
222 +
    let machine = h.path(&["machine.ras"]);
223 +
    let forged = [h.join(&[hostile, "/forged.ril"])];
224 +
    let diagnosed = [Expect::Contains("boot package fails verification: Type")];
225 +
    if il
226 +
        and image
227 +
        and h.run("hosted loaded package", hosted, Outcome::Exited(LOADED_STATUS), &[])
228 +
        and h.tool("generate hostile packages", "test/loader/hostile.rv64", &[hostile])
229 +
        and try kernel::image(h, "loader", fixture, &entries[..], &[])
230 +
        and h.boot("loader fixture", fixture, 0)
231 +
    {
232 +
        let status = Outcome::Exited(1);
233 +
        kernel::link(
234 +
            h, "hostile package", work, machine, rejected, &forged[..], status, &diagnosed[..]
235 +
        );
236 +
        h.expect("hostile package: no image", rejected, Expect::Absent);
237 +
        h.done(
238 +
            mark,
239 +
            "runtime loader: hosted and loaded entries agree, and hostile packages are rejected"
240 +
        );
241 +
    }
242 +
}
test/integration/fixtures.rad added +246 -0
1 +
//! Compiler test fixtures in `test/tests`: compiling one to an image or to
2 +
//! binary IL, checking the IL snapshots of all of them, and running the ones
3 +
//! that have an expected exit status.
4 +
5 +
use std::fmt;
6 +
use std::fs;
7 +
use std::io;
8 +
use std::mem;
9 +
use std::process;
10 +
use std::sys;
11 +
use std::arch::rv64::image;
12 +
use super::harness;
13 +
use super::harness::Harness;
14 +
use super::harness::Expect;
15 +
use std::process::Outcome;
16 +
use std::process::Output;
17 +
use std::process::Target;
18 +
use std::process::Command;
19 +
20 +
/// IL snapshot checker that runs each fixture with a `.ril` file.
21 +
constant RUNNER: *[u8] = "test/runner.rv64";
22 +
23 +
/// What the checks of one fixture need and where their results are.
24 +
record Case: Copy {
25 +
    /// Fixture source path.
26 +
    source: *[u8],
27 +
    /// Path of the fixture executable.
28 +
    image: *[u8],
29 +
    /// Index of the IL check in the batch, if the fixture has a snapshot.
30 +
    snapshot: ?u32,
31 +
    /// Output of the IL check.
32 +
    report: *[u8],
33 +
    /// Expected exit status as written in the source, if any.
34 +
    returns: ?*[u8],
35 +
    /// Index of the execution in the batch, if the executable exists.
36 +
    execution: ?u32,
37 +
    /// Expected stored size of the read-write data as written, if any.
38 +
    rwData: ?*[u8],
39 +
}
40 +
41 +
/// Collect the compiler arguments that compile a fixture to the executable
42 +
/// `output`, when it ends in `.rv64`, or otherwise to binary IL in the
43 +
/// directory `output`, which must then hold `test.ril`. Returns the file the
44 +
/// compilation must produce, after removing it.
45 +
export fn prepare(h: &mut Harness, source: *[u8], output: *[u8]) -> *[u8]
46 +
    throws (sys::Error)
47 +
{
48 +
    let content = try h.load(source);
49 +
    let base = harness::stripExt(source);
50 +
    let isImage = mem::endsWith(output, ".rv64");
51 +
    let produced = output if isImage else h.join(&[output, "/test.ril"]);
52 +
    let usesStd = harness::marker(content, "//! uses-std") <> nil;
53 +
    if isImage {
54 +
        h.add(&["-o", output]);
55 +
    } else {
56 +
        try harness::makeDirs(output);
57 +
        h.add(&["-ril", output]);
58 +
    }
59 +
    if usesStd {
60 +
        try h.addStd();
61 +
    }
62 +
    if harness::marker(content, "//! stack-check") <> nil {
63 +
        h.add(&["-stack-check"]);
64 +
    }
65 +
    h.add(&["-pkg", "test", "-mod", source]);
66 +
    if try harness::exists(base) {
67 +
        for path in try h.listFiles(base, ".rad") {
68 +
            h.add(&["-mod", path]);
69 +
        }
70 +
    }
71 +
    let assembly = h.join(&[base, ".ras"]);
72 +
    if try harness::exists(assembly) {
73 +
        h.add(&["-mod", assembly]);
74 +
    }
75 +
    let start = h.join(&[base, ".start.ras"]);
76 +
    if try harness::exists(start) {
77 +
        h.add(&["-start", start]);
78 +
    }
79 +
    if usesStd {
80 +
        h.add(&["-entry", "test"]);
81 +
    }
82 +
    try fs::remove(produced) catch err {
83 +
        if err <> sys::Error::NotFound {
84 +
            throw err;
85 +
        }
86 +
    };
87 +
    return produced;
88 +
}
89 +
90 +
/// Compile a fixture as [`prepare`] says. The compiler's output is not
91 +
/// captured.
92 +
export fn compile(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
93 +
    let produced = try prepare(h, args[0], args[1]);
94 +
    if h.compile(args[0], harness::INHERIT) {
95 +
        h.expect(h.join(&[args[0], " output"]), produced, Expect::Present);
96 +
    }
97 +
}
98 +
99 +
/// Print the verdict of one check of a fixture: `ok`, or `FAILED` with the
100 +
/// reason.
101 +
fn verdict(h: &mut Harness, source: *[u8], what: *[u8], reason: ?*[u8]) {
102 +
    io::print("test ");
103 +
    io::print(source);
104 +
    io::print(what);
105 +
    io::print(" ... ");
106 +
    let detail = reason else {
107 +
        io::print("ok\n");
108 +
        h.pass();
109 +
        return;
110 +
    };
111 +
    io::print("FAILED (");
112 +
    io::print(detail);
113 +
    io::print(")\n");
114 +
    set h.failed += 1;
115 +
}
116 +
117 +
/// Check the exit status of the execution of a fixture against the status
118 +
/// written in its source.
119 +
fn checkReturns(h: &mut Harness, c: Case, expected: *[u8], outcome: ?Outcome) {
120 +
    let want = try fmt::parseInt(expected) catch {
121 +
        return verdict(h, c.source, "", h.join(&["invalid expected status: ", expected]));
122 +
    };
123 +
    let actual = outcome else {
124 +
        return verdict(h, c.source, "", h.join(&["binary not found: ", c.image]));
125 +
    };
126 +
    if actual == Outcome::Exited(want.magnitude as i32) {
127 +
        return verdict(h, c.source, "", nil);
128 +
    }
129 +
    verdict(h, c.source, "", h.join(&["expected: ", expected, ", got: ", h.describe(actual)]));
130 +
}
131 +
132 +
/// Check the stored size of the read-write data of a fixture executable
133 +
/// against the size written in its source.
134 +
fn checkRwData(h: &mut Harness, c: Case, expected: *[u8]) {
135 +
    let what = " rw.data";
136 +
    let want = try fmt::parseInt(expected) catch {
137 +
        return verdict(h, c.source, what, h.join(&["invalid expected size: ", expected]));
138 +
    };
139 +
    let header = try h.load(c.image) catch {
140 +
        return verdict(h, c.source, what, h.join(&["binary not found: ", c.image]));
141 +
    };
142 +
    let layout = try image::parse(header) catch {
143 +
        return verdict(h, c.source, what, "invalid image header");
144 +
    };
145 +
    if layout.rwData.size as u64 == want.magnitude {
146 +
        return verdict(h, c.source, what, nil);
147 +
    }
148 +
    let got = h.num(layout.rwData.size);
149 +
    verdict(h, c.source, what, h.join(&["expected: ", expected, ", got: ", got]));
150 +
}
151 +
152 +
/// Run the checks of the fixtures: the IL snapshot of each fixture that has
153 +
/// one, the exit status of each that declares one, and the stored size of its
154 +
/// read-write data when declared. All children run in one batch; results are
155 +
/// printed in the order of the fixtures. With no `tests`, all fixtures in
156 +
/// `test/tests` are checked.
157 +
export fn bin(h: &mut Harness, tests: *[*[u8]]) throws (sys::Error) {
158 +
    if not try harness::exists(RUNNER) {
159 +
        io::printError(h.join(&["error: runner binary not found: ", RUNNER, "\n"]));
160 +
        io::printError("hint: run 'make test' first\n");
161 +
        set h.failed += 1;
162 +
        return;
163 +
    }
164 +
    try h.begin("bin");
165 +
    let mut sources: *mut [*[u8]] = &mut [];
166 +
    for source in tests {
167 +
        try! sources.append(source, &mut h.arena);
168 +
    }
169 +
    if tests.len == 0 {
170 +
        for ext in &[".rad", ".ras"] {
171 +
            for path in try h.listFiles("test/tests", ext) {
172 +
                try! sources.append(path, &mut h.arena);
173 +
            }
174 +
        }
175 +
    }
176 +
    let mut cases: *mut [Case] = &mut [];
177 +
    let mut batch: *mut [Command] = &mut [];
178 +
    for source in &sources[..] {
179 +
        let image = h.join(&[harness::stripExt(source), ".rv64"]);
180 +
        let content = try h.load(source) catch {
181 +
            verdict(h, source, "", "source not readable");
182 +
            continue;
183 +
        };
184 +
        let mut c = Case {
185 +
            source,
186 +
            image,
187 +
            snapshot: nil,
188 +
            report: h.path(&[h.num(cases.len), ".log"]),
189 +
            returns: harness::marker(content, "//! returns:"),
190 +
            execution: nil,
191 +
            rwData: harness::marker(content, "//! rw-data-size:"),
192 +
        };
193 +
        if try harness::exists(h.join(&[harness::stripExt(source), ".ril"])) {
194 +
            let target = Target::Hosted(process::Memory {
195 +
                memory: 0, data: 0, stack: 1024
196 +
            });
197 +
            set c.snapshot = batch.len;
198 +
            let check = h.cmd(
199 +
                RUNNER,
200 +
                &[source],
201 +
                target,
202 +
                harness::RUN_STEPS,
203 +
                Output::File(
204 +
                    c.report
205 +
                )
206 +
            );
207 +
            try! batch.append(check, &mut h.arena);
208 +
        }
209 +
        if c.returns <> nil and try harness::exists(image) {
210 +
            set c.execution = batch.len;
211 +
            let exec = h.cmd(
212 +
                image, &[], harness::HOSTED, harness::RUN_STEPS, Output::Discard
213 +
            );
214 +
            try! batch.append(exec, &mut h.arena);
215 +
        }
216 +
        try! cases.append(c, &mut h.arena);
217 +
    }
218 +
    let outcomes = h.runAll(&batch[..]);
219 +
    for c in &cases[..] {
220 +
        if let index = c.snapshot {
221 +
            io::print(h.text(c.report));
222 +
            if outcomes[index] == harness::OK {
223 +
                h.pass();
224 +
            } else {
225 +
                set h.failed += 1;
226 +
            }
227 +
        }
228 +
        if let expected = c.returns {
229 +
            let mut outcome: ?Outcome = nil;
230 +
            if let index = c.execution {
231 +
                set outcome = outcomes[index];
232 +
            }
233 +
            checkReturns(h, c, expected, outcome);
234 +
        }
235 +
        if c.returns == nil or c.execution <> nil {
236 +
            if let expected = c.rwData {
237 +
                checkRwData(h, c, expected);
238 +
            }
239 +
        }
240 +
    }
241 +
    io::print("\ntest result: ");
242 +
    io::print("ok. " if h.failed == 0 else "FAILED. ");
243 +
    let passed = h.passed;
244 +
    let failed = h.failed;
245 +
    io::print(h.join(&[h.num(passed), " passed; ", h.num(failed), " failed\n"]));
246 +
}
test/integration/format.rad added +173 -0
1 +
//! The source formatter: checking that it round-trips every source and
2 +
//! handles bad input, and formatting sources in place.
3 +
4 +
use std::fs;
5 +
use std::io;
6 +
use std::mem;
7 +
use std::process;
8 +
use std::sys;
9 +
use super::harness;
10 +
use super::harness::Harness;
11 +
use super::harness::Expect;
12 +
use std::process::Outcome;
13 +
use std::process::Output;
14 +
use std::process::Command;
15 +
16 +
/// Number of lines of the wide statement formatted at the default width.
17 +
constant WIDE_LINES: u32 = 3;
18 +
/// Number of times the argument is repeated in the wide statement. The
19 +
/// statement is 95 columns wide, so it wraps at 80 columns only.
20 +
constant WIDE_ARGUMENTS: u32 = 8;
21 +
22 +
/// A formatter run on each of a list of inputs.
23 +
union Tool: Copy {
24 +
    /// Format the input.
25 +
    Format,
26 +
    /// Dump the AST of the input.
27 +
    Dump,
28 +
}
29 +
30 +
/// Run the tool on every input, in a batch. The output of input `i` is
31 +
/// `<i><tag>.rad` for formatting and `<i><tag>.ast` for the dump, and its log is
32 +
/// that name plus `.log`. Returns the outcomes.
33 +
fn batch(
34 +
    h: &mut Harness, inputs: *[*[u8]], tool: Tool, tag: *[u8]
35 +
) -> *[Outcome] {
36 +
    let mut commands: *mut [Command] = &mut [];
37 +
    for input, i in inputs {
38 +
        let base = h.path(&[h.num(i), tag]);
39 +
        let mut out = h.join(&[base, ".ast"]);
40 +
        let mut args: *[*[u8]] = h.copy(&["-pkg", "fmt", "-mod", input, "-dump", "ast"]);
41 +
        if tool == Tool::Format {
42 +
            set out = h.join(&[base, ".rad"]);
43 +
            set args = h.copy(&["-fmt", input]);
44 +
        }
45 +
        let mut command = h.cc(args, Output::File(out));
46 +
        set command.stderr = Output::File(h.join(&[out, ".log"]));
47 +
        try! commands.append(command, &mut h.arena);
48 +
    }
49 +
    return h.runAll(&commands[..]);
50 +
}
51 +
52 +
/// Check that the formatter round-trips every one of the `sources`: each
53 +
/// source formats, its output formats to itself, and the AST of the output
54 +
/// equals the AST of the source. Then check the formatter's error handling and
55 +
/// wrap width.
56 +
export fn check(h: &mut Harness, sources: *[*[u8]]) throws (sys::Error) {
57 +
    try h.begin("fmt");
58 +
    let mut formatted: *mut [*[u8]] = &mut [];
59 +
    for i in 0..sources.len {
60 +
        try! formatted.append(h.path(&[h.num(i), "a.rad"]), &mut h.arena);
61 +
    }
62 +
    // The output of each source is formatted again and dumped.
63 +
    let again = &formatted[..];
64 +
    let format = batch(h, sources, Tool::Format, "a");
65 +
    let dump = batch(h, sources, Tool::Dump, "a");
66 +
    let formatAgain = batch(h, again, Tool::Format, "b");
67 +
    let dumpAgain = batch(h, again, Tool::Dump, "b");
68 +
    let mut roundTrips: u32 = 0;
69 +
    for source, i in sources {
70 +
        let a = h.path(&[h.num(i), "a"]);
71 +
        let b = h.path(&[h.num(i), "b"]);
72 +
        let ok = h.outcome(source, format[i], harness::OK, h.join(&[a, ".rad.log"]))
73 +
            and h.outcome(source, formatAgain[i], harness::OK, h.join(&[b, ".rad.log"]))
74 +
            and h.expect(source, h.join(&[a, ".rad"]), Expect::Same(h.join(&[b, ".rad"])))
75 +
            and h.outcome(source, dump[i], harness::OK, h.join(&[a, ".ast.log"]))
76 +
            and h.outcome(source, dumpAgain[i], harness::OK, h.join(&[b, ".ast.log"]))
77 +
            and h.expect(source, h.join(&[a, ".ast"]), Expect::Same(h.join(&[b, ".ast"])));
78 +
        if ok {
79 +
            set roundTrips += 1;
80 +
        }
81 +
    }
82 +
    try errors(h);
83 +
    let failed = h.failed;
84 +
    io::print(h.join(
85 +
        &["fmt: ", h.num(roundTrips), " source files round-trip, ", h.num(failed), " failed\n"]
86 +
    ));
87 +
}
88 +
89 +
/// Run the formatter on `args` with standard output in `out` and standard
90 +
/// error in `err`, and check that it exits with `status`.
91 +
fn format(
92 +
    h: &mut Harness,
93 +
    what: *[u8],
94 +
    args: &[*[u8]],
95 +
    status: i32,
96 +
    out: *[u8],
97 +
    err: *[u8]
98 +
) -> bool {
99 +
    let mut command = h.cc(args, Output::File(out));
100 +
    set command.stderr = Output::File(err);
101 +
    return h.run(what, command, Outcome::Exited(status), &[]);
102 +
}
103 +
104 +
/// Count the lines of the text.
105 +
fn lineCount(text: *[u8]) -> u32 {
106 +
    let mut count: u32 = 0;
107 +
    let mut lines = mem::split(text, '\n');
108 +
    while let line = lines.next() {
109 +
        set count += 1;
110 +
    }
111 +
    return count;
112 +
}
113 +
114 +
/// Check how the formatter reports a syntax error, a wrong command line, and
115 +
/// how it applies the wrap width.
116 +
fn errors(h: &mut Harness) throws (sys::Error) {
117 +
    let bad = h.path(&["bad.rad"]);
118 +
    let out = h.path(&["errors.out"]);
119 +
    let err = h.path(&["errors.err"]);
120 +
    try fs::writeFile(bad, "let x = ;\n");
121 +
    // A syntax error is reported on standard error only, so that standard
122 +
    // output holds nothing but formatted text.
123 +
    format(h, "fmt syntax error", &["-fmt", bad], 1, out, err);
124 +
    h.expect("fmt syntax error: standard output", out, Expect::Exact(""));
125 +
    h.expect("fmt syntax error: diagnostic", err, Expect::Contains("error: "));
126 +
    // A wrong command line exits with status 1 and prints the usage.
127 +
    let usage: [*[*[u8]]; 5] = [
128 +
        h.copy(&["-fmt"]),
129 +
        h.copy(&["-fmt", bad, bad]),
130 +
        h.copy(&["-fmt", "-fmt.wrap", "0", bad]),
131 +
        h.copy(&["-fmt", "-fmt.wrap", "x", bad]),
132 +
        h.copy(&["-fmt", "-fmt.wrap"]),
133 +
    ];
134 +
    for args in &usage[..] {
135 +
        format(h, "fmt usage", args, 1, h.path(&["usage.out"]), err);
136 +
        h.expect("fmt usage", err, Expect::Prefix("usage:"));
137 +
    }
138 +
    // The width defaults to 100 columns and `-fmt.wrap` sets it.
139 +
    let wide = h.path(&["wide.rad"]);
140 +
    let mut statement: *mut [u8] = &mut [];
141 +
    for i in 0..WIDE_ARGUMENTS {
142 +
        try! statement.extend("argument, ", &mut h.arena);
143 +
    }
144 +
    try fs::writeFileParts(wide, &["fn f() {\n    call(", &statement[..], "last);\n}\n"]);
145 +
    format(h, "fmt wrap: default", &["-fmt", wide], 0, out, err);
146 +
    let normal = lineCount(try h.load(out));
147 +
    format(h, "fmt wrap: 80 columns", &["-fmt", "-fmt.wrap", "80", wide], 0, out, err);
148 +
    let narrow = lineCount(try h.load(out));
149 +
    if normal == WIDE_LINES and narrow > WIDE_LINES {
150 +
        h.pass();
151 +
    } else {
152 +
        h.fail("fmt wrap", h.join(
153 +
            &["default ", h.num(normal), " lines, narrow ", h.num(narrow), " lines"]
154 +
        ));
155 +
    }
156 +
}
157 +
158 +
/// Format each source in place with the formatter. A source is rewritten only
159 +
/// when its text changes. Every source that cannot be formatted is reported and
160 +
/// fails the command.
161 +
export fn rewrite(h: &mut Harness, sources: *[*[u8]]) throws (sys::Error) {
162 +
    try h.begin("format");
163 +
    let outcomes = batch(h, sources, Tool::Format, "");
164 +
    for source, i in sources {
165 +
        let formatted = h.path(&[h.num(i), ".rad"]);
166 +
        if h.outcome(source, outcomes[i], harness::OK, h.join(&[formatted, ".log"]))
167 +
            and try h.compare(source, formatted) <> nil
168 +
        {
169 +
            try fs::writeFile(source, try h.load(formatted));
170 +
            io::print(h.join(&["fmt ", source, "\n"]));
171 +
        }
172 +
    }
173 +
}
test/integration/harness.rad added +628 -0
1 +
//! Shared state of the integration driver: the string arena, the work
2 +
//! directory, file helpers, checks, and child runs. A failed check prints what
3 +
//! was expected and what happened on standard error, prints the log it
4 +
//! concerns, and counts a failure.
5 +
6 +
use std::fmt;
7 +
use std::fs;
8 +
use std::io;
9 +
use std::mem;
10 +
use std::process;
11 +
use std::sys;
12 +
use std::lang::alloc;
13 +
14 +
/// Capacity of the string arena, in bytes (128 MB). It holds every path,
15 +
/// argument list and file the command reads, and is never reset.
16 +
export constant ARENA_CAPACITY: u32 = 134217728;
17 +
/// Most children the host runs at once, which bounds `-jobs`.
18 +
export constant MAX_JOBS: u32 = 64;
19 +
/// Parent of the work directories of the commands.
20 +
constant WORK_ROOT: *[u8] = "bin/test";
21 +
22 +
/// Instruction budget of a compiler, image tool or formatter run. The slowest,
23 +
/// the image tool over the kernel, takes 4.5 billion instructions.
24 +
export constant COMPILE_STEPS: u64 = 50000000000;
25 +
/// Instruction budget of a hosted program or tool run. The slowest, the IL
26 +
/// verifier over all fixtures, takes 379 million instructions.
27 +
export constant RUN_STEPS: u64 = 4000000000;
28 +
/// Tick budget of a machine run. The slowest, the cycles fixture on eight
29 +
/// harts, takes 6.5 billion ticks.
30 +
export constant MACHINE_STEPS: u64 = 70000000000;
31 +
32 +
/// Output destination that shares the parent's streams.
33 +
export constant INHERIT: process::Output = process::Output::Inherit;
34 +
/// Hosted target that takes the host default sizes.
35 +
export constant HOSTED: process::Target =
36 +
    process::Target::Hosted(process::Memory { memory: 0, data: 0, stack: 0 });
37 +
/// Outcome of a child that exited with status zero.
38 +
export constant OK: process::Outcome = process::Outcome::Exited(0);
39 +
40 +
/// Memory of the driver. The compiler-generated entry supplies one zero-filled
41 +
/// region of this type.
42 +
export record Storage {
43 +
    /// Backing storage of the string arena.
44 +
    arena: [u8; ARENA_CAPACITY],
45 +
}
46 +
47 +
/// State of one command run.
48 +
export record Harness {
49 +
    /// Arena of all strings and lists the command builds.
50 +
    arena: alloc::Arena,
51 +
    /// Path of the compiler image that compile steps run.
52 +
    compiler: *[u8],
53 +
    /// Most children the worker pool runs at once.
54 +
    jobs: u32,
55 +
    /// Work directory of the command. Empty before [`Harness::begin`].
56 +
    work: *[u8],
57 +
    /// Checks that passed.
58 +
    passed: u32,
59 +
    /// Checks that failed.
60 +
    failed: u32,
61 +
    /// Compiler arguments collected by [`Harness::add`].
62 +
    args: *mut [*[u8]],
63 +
    /// Number of collected arguments that [`Harness::take`] returned.
64 +
    taken: u32,
65 +
}
66 +
67 +
/// What a check requires of a text file. Line checks look at the lines of the
68 +
/// file, split at line breaks.
69 +
export union Expect: Copy {
70 +
    /// Some line equals the text.
71 +
    Line(*[u8]),
72 +
    /// Exactly `count` lines equal the text.
73 +
    Lines {
74 +
        /// Text of the lines.
75 +
        text: *[u8],
76 +
        /// Required number of lines.
77 +
        count: u32,
78 +
    },
79 +
    /// Some line contains the text.
80 +
    Contains(*[u8]),
81 +
    /// Exactly `count` lines contain the text.
82 +
    Matching {
83 +
        /// Text the lines contain.
84 +
        text: *[u8],
85 +
        /// Required number of lines.
86 +
        count: u32,
87 +
    },
88 +
    /// Some line starts with the text.
89 +
    Prefix(*[u8]),
90 +
    /// The file has exactly these bytes.
91 +
    Exact(*[u8]),
92 +
    /// The file has the same bytes as the file at the path.
93 +
    Same(*[u8]),
94 +
    /// The file exists.
95 +
    Present,
96 +
    /// Nothing exists at the path.
97 +
    Absent,
98 +
}
99 +
100 +
/// Create the state of a command run over `storage`. `jobs` must be in
101 +
/// `1..=MAX_JOBS`.
102 +
export fn new(storage: *mut Storage, compiler: *[u8], jobs: u32) -> Harness {
103 +
    assert jobs >= 1 and jobs <= MAX_JOBS;
104 +
    return Harness {
105 +
        arena: alloc::new(&mut storage.arena[..]),
106 +
        compiler,
107 +
        jobs,
108 +
        work: "",
109 +
        passed: 0,
110 +
        failed: 0,
111 +
        args: &mut [],
112 +
        taken: 0,
113 +
    };
114 +
}
115 +
116 +
/// Name of a host error, for messages.
117 +
export fn errorName(error: sys::Error) -> *[u8] {
118 +
    match error {
119 +
        case sys::Error::NotFound => return "not found",
120 +
        case sys::Error::Exists   => return "already exists",
121 +
        case sys::Error::Denied   => return "denied",
122 +
        case sys::Error::NotEmpty => return "directory not empty",
123 +
        case sys::Error::Invalid  => return "invalid",
124 +
        case sys::Error::TooLarge => return "too large",
125 +
        case sys::Error::Limit    => return "limit reached",
126 +
        case sys::Error::Io       => return "i/o error",
127 +
    }
128 +
}
129 +
130 +
/// Copy the parts, in order, into one string allocated from the arena. Panics
131 +
/// when the arena is exhausted, because the driver sizes it for every command.
132 +
export fn (h: &mut Harness) join(parts: &[*[u8]]) -> *[u8] {
133 +
    let joined = try alloc::concatBytes(&mut h.arena, parts) catch {
134 +
        panic "join: string arena exhausted";
135 +
    };
136 +
    return &joined[..];
137 +
}
138 +
139 +
/// Format a number in decimal into a string allocated from the arena.
140 +
export fn (h: &mut Harness) num(value: u32) -> *[u8] {
141 +
    let mut digits: [u8; fmt::U32_STR_LEN] = [0; fmt::U32_STR_LEN];
142 +
    let start = fmt::formatU32(value, &mut digits[..]);
143 +
    let mut text: *mut [u8] = &mut [];
144 +
    try! text.extend(&digits[start..], &mut h.arena);
145 +
    return &text[..];
146 +
}
147 +
148 +
/// Return the path in the work directory of the file whose name is the
149 +
/// concatenation of `parts`.
150 +
export fn (h: &mut Harness) path(parts: &[*[u8]]) -> *[u8] {
151 +
    return h.join(&[h.work, "/", h.join(parts)]);
152 +
}
153 +
154 +
/// Copy an argument list into the arena, as the host call takes it.
155 +
export fn (h: &mut Harness) copy(args: &[*[u8]]) -> *[*[u8]] {
156 +
    let mut list: *mut [*[u8]] = &mut [];
157 +
    for arg in args {
158 +
        try! list.append(arg, &mut h.arena);
159 +
    }
160 +
    return &list[..];
161 +
}
162 +
163 +
/// Count a passed check. Returns true.
164 +
export fn (h: &mut Harness) pass() -> bool {
165 +
    set h.passed += 1;
166 +
    return true;
167 +
}
168 +
169 +
/// Count a failed check and report it on standard error. Returns false.
170 +
export fn (h: &mut Harness) fail(what: *[u8], detail: *[u8]) -> bool {
171 +
    io::printError(h.join(&["FAILED: ", what, ": ", detail, "\n"]));
172 +
    set h.failed += 1;
173 +
    return false;
174 +
}
175 +
176 +
/// Print `line` when no check failed since `mark`, a value of `h.failed`
177 +
/// taken at the start of a group of checks.
178 +
export fn (h: &mut Harness) done(mark: u32, line: *[u8]) {
179 +
    if h.failed == mark {
180 +
        io::printLn(line);
181 +
    }
182 +
}
183 +
184 +
/// Exit status of the run: zero when no check failed.
185 +
export fn (h: &Harness) status() -> i32 {
186 +
    return 0 if h.failed == 0 else 1;
187 +
}
188 +
189 +
/// Read the file at `path` into the arena and return its contents. Throws
190 +
/// `TooLarge` when the file is larger than `4194304`.
191 +
export fn (h: &mut Harness) load(path: *[u8]) -> *[u8] throws (sys::Error) {
192 +
    let info = try fs::stat(path) else {
193 +
        throw sys::Error::NotFound;
194 +
    };
195 +
    if info.size > 4194304 as u64 {
196 +
        throw sys::Error::TooLarge;
197 +
    }
198 +
    let mut buf: *mut [u8] = &mut [];
199 +
    try! buf.resize(info.size as u32, 0, &mut h.arena);
200 +
    let n = try fs::readFile(path, &mut buf[..]);
201 +
    return &buf[..n];
202 +
}
203 +
204 +
/// Return the contents of the file at `path`, or a note that it cannot be
205 +
/// read, for messages.
206 +
export fn (h: &mut Harness) text(path: *[u8]) -> *[u8] {
207 +
    let content = try h.load(path) catch err {
208 +
        return h.join(&["(cannot read ", path, ": ", errorName(err), ")\n"]);
209 +
    };
210 +
    return content;
211 +
}
212 +
213 +
/// Return the number of the first line, counting from one, at which the files
214 +
/// differ, or `nil` when they have the same bytes. A file that ends early
215 +
/// differs at the line after its last line break.
216 +
export fn (h: &mut Harness) compare(left: *[u8], right: *[u8]) -> ?u32 throws (sys::Error) {
217 +
    let a = try h.load(left);
218 +
    let b = try h.load(right);
219 +
    let mut line: u32 = 1;
220 +
    for i in 0..(a.len if a.len < b.len else b.len) {
221 +
        if a[i] <> b[i] {
222 +
            return line;
223 +
        }
224 +
        if a[i] == '\n' {
225 +
            set line += 1;
226 +
        }
227 +
    }
228 +
    if a.len <> b.len {
229 +
        return line;
230 +
    }
231 +
    return nil;
232 +
}
233 +
234 +
/// Read the lines of the manifest at `path`, skipping empty lines.
235 +
export fn (h: &mut Harness) manifest(path: *[u8]) -> *[*[u8]] throws (sys::Error) {
236 +
    let mut entries: *mut [*[u8]] = &mut [];
237 +
    let mut lines = mem::split(try h.load(path), '\n');
238 +
    while let line = lines.next() {
239 +
        if line.len > 0 {
240 +
            try! entries.append(line, &mut h.arena);
241 +
        }
242 +
    }
243 +
    return &entries[..];
244 +
}
245 +
246 +
/// List the entries of the directory `dir` as paths below it, in host order.
247 +
export fn (h: &mut Harness) children(dir: *[u8]) -> *[*[u8]] throws (sys::Error) {
248 +
    let mut size: u32 = 4096;
249 +
    let mut found: *mut [*[u8]] = &mut [];
250 +
    while true {
251 +
        let mut buf: *mut [u8] = &mut [];
252 +
        try! buf.resize(size, 0, &mut h.arena);
253 +
        let listing = try fs::listDir(dir, buf) catch err {
254 +
            if err == sys::Error::TooLarge and size < 524288 {
255 +
                set size *= 2;
256 +
                continue;
257 +
            }
258 +
            throw err;
259 +
        };
260 +
        let mut names = mem::split(listing, 0);
261 +
        while let name = names.next() {
262 +
            try! found.append(h.join(&[dir, "/", name]), &mut h.arena);
263 +
        }
264 +
        break;
265 +
    }
266 +
    return &found[..];
267 +
}
268 +
269 +
/// Sort byte strings in ascending byte order. The sort is stable and takes
270 +
/// time proportional to the square of the count, which suits directory
271 +
/// listings of a few thousand names.
272 +
export fn sort(names: &mut [*[u8]]) {
273 +
    for i in 1..names.len {
274 +
        let item = names[i];
275 +
        let mut j = i;
276 +
        while j > 0 and mem::cmp(names[j - 1], item) > 0 {
277 +
            set names[j] = names[j - 1];
278 +
            set j -= 1;
279 +
        }
280 +
        set names[j] = item;
281 +
    }
282 +
}
283 +
284 +
/// List the entries of the directory `dir` whose names end with `suffix`, as
285 +
/// paths in ascending order.
286 +
export fn (h: &mut Harness) listFiles(dir: *[u8], suffix: *[u8]) -> *[*[u8]] throws (sys::Error) {
287 +
    let mut found: *mut [*[u8]] = &mut [];
288 +
    for path in try h.children(dir) {
289 +
        if mem::endsWith(path, suffix) {
290 +
            try! found.append(path, &mut h.arena);
291 +
        }
292 +
    }
293 +
    sort(&mut found[..]);
294 +
    return &found[..];
295 +
}
296 +
297 +
/// Return the path without its last extension, or the path itself when it has
298 +
/// none.
299 +
export fn stripExt(path: *[u8]) -> *[u8] {
300 +
    let mut end = path.len;
301 +
    while end > 0 and path[end - 1] <> '.' {
302 +
        set end -= 1;
303 +
    }
304 +
    return &path[..end - 1] if end > 0 else path;
305 +
}
306 +
307 +
/// Return the rest of the first line of `content` that starts with `prefix`,
308 +
/// without the spaces that follow it, or `nil` when no line does.
309 +
export fn marker(content: *[u8], prefix: *[u8]) -> ?*[u8] {
310 +
    let mut lines = mem::split(content, '\n');
311 +
    while let line = lines.next() {
312 +
        if let rest = mem::stripPrefix(prefix, line) {
313 +
            let mut start: u32 = 0;
314 +
            while start < rest.len and rest[start] == ' ' {
315 +
                set start += 1;
316 +
            }
317 +
            return &rest[start..];
318 +
        }
319 +
    }
320 +
    return nil;
321 +
}
322 +
323 +
/// Parse a decimal number that fits in a `u32`. Throws `Invalid` otherwise.
324 +
export fn number(text: *[u8]) -> u32 throws (sys::Error) {
325 +
    let literal = try fmt::parseInt(text) catch {
326 +
        throw sys::Error::Invalid;
327 +
    };
328 +
    if literal.magnitude > mem::U32_MAX as u64 {
329 +
        throw sys::Error::Invalid;
330 +
    }
331 +
    return literal.magnitude as u32;
332 +
}
333 +
334 +
/// Return true when something exists at `path`.
335 +
export fn exists(path: *[u8]) -> bool throws (sys::Error) {
336 +
    return try fs::stat(path) <> nil;
337 +
}
338 +
339 +
/// Write the concatenation of the files `inputs`, in order, to `output`.
340 +
export fn (h: &mut Harness) concat(output: *[u8], inputs: &[*[u8]]) throws (sys::Error) {
341 +
    let mut out = try fs::create(output);
342 +
    for input in inputs {
343 +
        out.write(
344 +
            try h.load(input) catch err {
345 +
                try out.close() catch {};
346 +
                throw err;
347 +
            }
348 +
        );
349 +
    }
350 +
    try out.close();
351 +
}
352 +
353 +
/// Create the directory `path` and any missing parents.
354 +
export fn makeDirs(path: *[u8]) throws (sys::Error) {
355 +
    for end in 1..path.len + 1 {
356 +
        if end == path.len or path[end] == '/' {
357 +
            try fs::makeDir(&path[..end]) catch err {
358 +
                if err <> sys::Error::Exists {
359 +
                    throw err;
360 +
                }
361 +
            };
362 +
        }
363 +
    }
364 +
}
365 +
366 +
/// Remove the file or directory tree at `path`, if it exists.
367 +
fn removeTree(h: &mut Harness, path: *[u8]) throws (sys::Error) {
368 +
    let info = try fs::stat(path) else {
369 +
        return;
370 +
    };
371 +
    if info.kind == fs::Kind::Directory {
372 +
        for child in try h.children(path) {
373 +
            try removeTree(h, child);
374 +
        }
375 +
    }
376 +
    try fs::remove(path);
377 +
}
378 +
379 +
/// Start a command: remove the work directory `bin/test/<command>` with
380 +
/// everything in it and create it empty. It stays in place afterwards so a
381 +
/// failure can be inspected.
382 +
export fn (h: &mut Harness) begin(command: *[u8]) throws (sys::Error) {
383 +
    try makeDirs(WORK_ROOT);
384 +
    set h.work = h.join(&[WORK_ROOT, "/", command]);
385 +
    let work = h.work;
386 +
    try removeTree(h, work);
387 +
    try fs::makeDir(work);
388 +
}
389 +
390 +
/// Return true when the line meets a line expectation.
391 +
fn hit(want: Expect, line: *[u8]) -> bool {
392 +
    match want {
393 +
        case Expect::Line(text)            => return mem::eq(line, text),
394 +
        case Expect::Lines { text, .. }    => return mem::eq(line, text),
395 +
        case Expect::Prefix(text)          => return mem::stripPrefix(text, line) <> nil,
396 +
        case Expect::Contains(text)        => return mem::find(line, text) <> nil,
397 +
        case Expect::Matching { text, .. } => return mem::find(line, text) <> nil,
398 +
        else                               => return false,
399 +
    }
400 +
}
401 +
402 +
/// Return whether the file at `path` meets the expectation. Throws when the
403 +
/// file cannot be examined.
404 +
fn satisfied(h: &mut Harness, path: *[u8], want: Expect) -> bool throws (sys::Error) {
405 +
    let mut count: ?u32 = nil;
406 +
    match want {
407 +
        case Expect::Present                   => return try exists(path),
408 +
        case Expect::Absent                    => return not try exists(path),
409 +
        case Expect::Same(right)               => return try h.compare(path, right) == nil,
410 +
        case Expect::Exact(text)               => return mem::eq(try h.load(path), text),
411 +
        case Expect::Lines { count: n, .. }    => set count = n,
412 +
        case Expect::Matching { count: n, .. } => set count = n,
413 +
        else                                   => {}
414 +
    }
415 +
    let mut hits: u32 = 0;
416 +
    let mut lines = mem::split(try h.load(path), '\n');
417 +
    while let line = lines.next() {
418 +
        if hit(want, line) {
419 +
            set hits += 1;
420 +
        }
421 +
    }
422 +
    if let n = count {
423 +
        return hits == n;
424 +
    }
425 +
    return hits > 0;
426 +
}
427 +
428 +
/// Check the file at `path` against the expectation `want`. A failure is
429 +
/// labeled `what` and shows the file. Returns whether the check passed.
430 +
export fn (h: &mut Harness) expect(what: *[u8], path: *[u8], want: Expect) -> bool {
431 +
    let ok = try satisfied(h, path, want) catch err {
432 +
        return h.fail(what, h.join(&["cannot check ", path, ": ", errorName(err)]));
433 +
    };
434 +
    if ok {
435 +
        return h.pass();
436 +
    }
437 +
    io::printError(h.text(path));
438 +
    return h.fail(what, h.join(&["unmet expectation on ", path]));
439 +
}
440 +
441 +
/// Describe how a child ended, for messages.
442 +
export fn (h: &mut Harness) describe(outcome: process::Outcome) -> *[u8] {
443 +
    match outcome {
444 +
        case process::Outcome::Exited(status) => {
445 +
            let sign = "-" if status < 0 else "";
446 +
            return h.join(&[
447 +
                "exit status ", sign, h.num((status if status >= 0 else 0 - status) as u32)
448 +
            ]);
449 +
        }
450 +
        case process::Outcome::StepLimit => return "step limit reached",
451 +
        case process::Outcome::Fault     => return "fault",
452 +
    }
453 +
}
454 +
455 +
/// Check that a child ended with the `want` outcome. The file `report`, if any,
456 +
/// is shown on failure.
457 +
export fn (h: &mut Harness) outcome(
458 +
    what: *[u8],
459 +
    actual: process::Outcome,
460 +
    want: process::Outcome,
461 +
    report: ?*[u8]
462 +
) -> bool {
463 +
    if actual == want {
464 +
        return h.pass();
465 +
    }
466 +
    if let path = report {
467 +
        io::printError(h.text(path));
468 +
    }
469 +
    return h.fail(what, h.join(&["expected ", h.describe(want), ", got ", h.describe(actual)]));
470 +
}
471 +
472 +
/// Build a command that runs `image` with the arguments. Standard output and
473 +
/// standard error both go to `output`.
474 +
export fn (h: &mut Harness) cmd(
475 +
    image: *[u8],
476 +
    args: &[*[u8]],
477 +
    target: process::Target,
478 +
    steps: u64,
479 +
    output: process::Output
480 +
) -> process::Command {
481 +
    return process::Command {
482 +
        image, args: h.copy(args), stdout: output, stderr: output, target, maxSteps: steps
483 +
    };
484 +
}
485 +
486 +
/// Build a command that runs the machine image on `harts` harts with the
487 +
/// memory of the kernel fixtures. Its output goes to `output`.
488 +
export fn (h: &mut Harness) machine(
489 +
    image: *[u8], harts: u32, output: process::Output
490 +
) -> process::Command {
491 +
    let target = process::Target::Machine(process::Machine {
492 +
        harts, memory: 262144, irq: nil
493 +
    });
494 +
    return h.cmd(image, &[], target, MACHINE_STEPS, output);
495 +
}
496 +
497 +
/// Return the output destination that creates the file in the work directory
498 +
/// whose name is the concatenation of `parts`.
499 +
export fn (h: &mut Harness) out(parts: &[*[u8]]) -> process::Output {
500 +
    return process::Output::File(h.path(parts));
501 +
}
502 +
503 +
/// Run every command of the batch, with up to `h.jobs` children at once, and
504 +
/// return the outcome of each command in the order of the batch. A child that
505 +
/// cannot start counts a failure and ends as a fault.
506 +
export fn (h: &mut Harness) runAll(commands: *[process::Command]) -> *[process::Outcome] {
507 +
    let mut outcomes: *mut [process::Outcome] = &mut [];
508 +
    try! outcomes.resize(commands.len, process::Outcome::Fault, &mut h.arena);
509 +
    let mut handles: [u32; MAX_JOBS] = [0; MAX_JOBS];
510 +
    let mut indices: [u32; MAX_JOBS] = [0; MAX_JOBS];
511 +
    let mut live: u32 = 0;
512 +
    let mut next: u32 = 0;
513 +
    while next < commands.len or live > 0 {
514 +
        while next < commands.len and live < h.jobs {
515 +
            let child = try process::spawn(&commands[next]) catch err {
516 +
                h.fail(commands[next].image, h.join(&["cannot start: ", errorName(err)]));
517 +
                set next += 1;
518 +
                continue;
519 +
            };
520 +
            set handles[live] = child.handle;
521 +
            set indices[live] = next;
522 +
            set live += 1;
523 +
            set next += 1;
524 +
        }
525 +
        if live == 0 {
526 +
            break;
527 +
        }
528 +
        let done = try! process::waitAny();
529 +
        let mut slot: u32 = 0;
530 +
        while slot < live and handles[slot] <> done.child.handle {
531 +
            set slot += 1;
532 +
        }
533 +
        assert slot < live;
534 +
        set outcomes[indices[slot]] = done.outcome;
535 +
        set live -= 1;
536 +
        set handles[slot] = handles[live];
537 +
        set indices[slot] = indices[live];
538 +
    }
539 +
    return &outcomes[..];
540 +
}
541 +
542 +
/// Run the command and check that it ends with the outcome `want` and that the
543 +
/// file it writes its output to meets the expectations, which require standard
544 +
/// output to go to a file. A child that cannot start counts a failure. Returns
545 +
/// true when no check failed.
546 +
export fn (h: &mut Harness) run(
547 +
    what: *[u8],
548 +
    command: process::Command,
549 +
    want: process::Outcome,
550 +
    expects: &[Expect]
551 +
) -> bool {
552 +
    let mark = h.failed;
553 +
    let mut report: ?*[u8] = nil;
554 +
    if let case process::Output::File(path) = command.stdout {
555 +
        set report = path;
556 +
    }
557 +
    assert expects.len == 0 or report <> nil;
558 +
    let actual = try process::run(&command) catch err {
559 +
        h.fail(command.image, h.join(&["cannot start: ", errorName(err)]));
560 +
        return false;
561 +
    };
562 +
    h.outcome(what, actual, want, report);
563 +
    if let path = report {
564 +
        for expectation in expects {
565 +
            h.expect(what, path, expectation);
566 +
        }
567 +
    }
568 +
    return h.failed == mark;
569 +
}
570 +
571 +
/// Run a hosted tool with the arguments and its output inherited, and check
572 +
/// that it exits with status zero.
573 +
export fn (h: &mut Harness) tool(what: *[u8], image: *[u8], args: &[*[u8]]) -> bool {
574 +
    let command = h.cmd(image, args, HOSTED, RUN_STEPS, INHERIT);
575 +
    return h.run(what, command, OK, &[]);
576 +
}
577 +
578 +
/// Run the machine image on `harts` harts with its output inherited, and check
579 +
/// that it exits with status zero.
580 +
export fn (h: &mut Harness) boot(what: *[u8], image: *[u8], harts: u32) -> bool {
581 +
    let command = h.machine(image, harts, INHERIT);
582 +
    return h.run(what, command, OK, &[]);
583 +
}
584 +
585 +
/// Add arguments for the next compiler run.
586 +
export fn (h: &mut Harness) add(parts: &[*[u8]]) {
587 +
    for part in parts {
588 +
        try! h.args.append(part, &mut h.arena);
589 +
    }
590 +
}
591 +
592 +
/// Add the module at `path` to the current package under the logical name
593 +
/// `name`, which may contain `/` to place it in a child module.
594 +
export fn (h: &mut Harness) named(name: *[u8], path: *[u8]) {
595 +
    h.add(&["-mod", h.join(&[name, "=", path])]);
596 +
}
597 +
598 +
/// Add the standard library as a package named `std`.
599 +
export fn (h: &mut Harness) addStd() throws (sys::Error) {
600 +
    h.add(&["-pkg", "std"]);
601 +
    for path in try h.manifest("std.lib") {
602 +
        h.add(&["-mod", path]);
603 +
    }
604 +
}
605 +
606 +
/// Return a copy of the compiler arguments collected since the last call.
607 +
export fn (h: &mut Harness) take() -> *[*[u8]] {
608 +
    let mut list: *mut [*[u8]] = &mut [];
609 +
    for arg in &h.args[h.taken..] {
610 +
        try! list.append(arg, &mut h.arena);
611 +
    }
612 +
    set h.taken = h.args.len;
613 +
    return &list[..];
614 +
}
615 +
616 +
/// Build a command that runs the compiler on `args` with the parent's memory
617 +
/// sizes. Standard output and standard error both go to `output`.
618 +
export fn (h: &mut Harness) cc(args: &[*[u8]], output: process::Output) -> process::Command {
619 +
    let compiler = h.compiler;
620 +
    return h.cmd(compiler, args, process::Target::Inherit, COMPILE_STEPS, output);
621 +
}
622 +
623 +
/// Run the compiler on the collected arguments and check that it exits with
624 +
/// status zero. Its output goes to `output`.
625 +
export fn (h: &mut Harness) compile(what: *[u8], output: process::Output) -> bool {
626 +
    let command = h.cc(h.take(), output);
627 +
    return h.run(what, command, OK, &[]);
628 +
}
test/integration/host.rad added +255 -0
1 +
//! Tests of `std::process` and the host file calls against the emulator. A
2 +
//! child program, `test/host/child.rv64`, behaves as its arguments say.
3 +
4 +
use std::fs;
5 +
use std::process;
6 +
use std::sys;
7 +
use super::harness;
8 +
use super::harness::Harness;
9 +
use super::harness::Expect;
10 +
use std::process::Outcome;
11 +
use std::process::Output;
12 +
use std::process::Target;
13 +
use std::process::Command;
14 +
15 +
/// The child program of the tests.
16 +
constant CHILD: *[u8] = "test/host/child.rv64";
17 +
/// Budget of a child that loops forever and must end at the step limit, in
18 +
/// instructions. It is small, so the child ends quickly.
19 +
constant SPIN_STEPS: u64 = 100000;
20 +
21 +
/// Build a command that runs the child with the arguments and discards its
22 +
/// output.
23 +
fn child(h: &mut Harness, args: &[*[u8]]) -> Command {
24 +
    return h.cmd(CHILD, args, harness::HOSTED, harness::RUN_STEPS, Output::Discard);
25 +
}
26 +
27 +
/// Check that a call failed with the `expected` error.
28 +
fn expectError(h: &mut Harness, what: *[u8], actual: ?sys::Error, expected: sys::Error) {
29 +
    let name = harness::errorName(expected);
30 +
    let error = actual else {
31 +
        h.fail(what, h.join(&["expected ", name, ", but the call succeeded"]));
32 +
        return;
33 +
    };
34 +
    if error == expected {
35 +
        h.pass();
36 +
    } else {
37 +
        h.fail(what, h.join(&["expected ", name, ", got ", harness::errorName(error)]));
38 +
    }
39 +
}
40 +
41 +
/// Exit statuses come back unchanged, a loop ends at its step limit, an access
42 +
/// outside memory faults, and the sizes of a hosted child are inherited, given
43 +
/// or the defaults.
44 +
fn outcomes(h: &mut Harness) {
45 +
    let statuses: [i32; 5] = [0, 1, 255, 300, -1];
46 +
    for label, i in &["0", "1", "255", "300", "-1"] {
47 +
        let command = child(h, &["exit", label]);
48 +
        h.run(h.join(&["exit status ", label]), command, Outcome::Exited(statuses[i]), &[]);
49 +
    }
50 +
    let mut spin = child(h, &["spin"]);
51 +
    set spin.maxSteps = SPIN_STEPS;
52 +
    h.run("spin ends at the step limit", spin, Outcome::StepLimit, &[]);
53 +
    h.run("access outside memory faults", child(h, &["fault"]), Outcome::Fault, &[]);
54 +
    h.run("standard input is empty", child(h, &["stdin"]), harness::OK, &[]);
55 +
    let parent = process::Memory { memory: 385024, data: 348416, stack: 1024 };
56 +
    let small = process::Memory { memory: 65536, data: 0, stack: 0 };
57 +
    let sizes: [Target; 5] = [
58 +
        harness::HOSTED,
59 +
        Target::Inherit,
60 +
        Target::Hosted(parent),
61 +
        Target::Hosted(small),
62 +
        Target::Hosted(small),
63 +
    ];
64 +
    let kb: [*[u8]; 5] = ["200000", "200000", "200000", "20000", "100000"];
65 +
    let labels: [*[u8]; 5] = [
66 +
        "default memory is too small",
67 +
        "inherited memory is the parent's",
68 +
        "explicit memory is used",
69 +
        "a buffer inside explicit memory", "a buffer outside explicit memory",
70 +
    ];
71 +
    for target, i in &sizes[..] {
72 +
        let mut command = child(h, &["reach", kb[i]]);
73 +
        set command.target = target;
74 +
        let want = Outcome::Fault if i == 0 or i == 4 else harness::OK;
75 +
        h.run(labels[i], command, want, &[]);
76 +
    }
77 +
}
78 +
79 +
/// Run the child with its output in the files `out` and `err` of the work
80 +
/// directory (`nil` drops a stream), and check that it exits with status zero,
81 +
/// that the file of standard output meets the expectations, and that the file
82 +
/// of standard error holds `errText` when given.
83 +
fn streams(
84 +
    h: &mut Harness,
85 +
    what: *[u8],
86 +
    args: &[*[u8]],
87 +
    out: ?*[u8],
88 +
    err: ?*[u8],
89 +
    expects: &[Expect],
90 +
    errText: ?*[u8]
91 +
) {
92 +
    let mut command = child(h, args);
93 +
    if let name = out {
94 +
        set command.stdout = h.out(&[name]);
95 +
    }
96 +
    if let name = err {
97 +
        set command.stderr = h.out(&[name]);
98 +
    }
99 +
    h.run(what, command, harness::OK, expects);
100 +
    if let name = err {
101 +
        if let text = errText {
102 +
            h.expect(what, h.path(&[name]), Expect::Exact(text));
103 +
        }
104 +
    }
105 +
}
106 +
107 +
/// Output goes to files, merges in order into one file, or is dropped, and
108 +
/// arguments arrive unchanged.
109 +
fn files(h: &mut Harness) throws (sys::Error) {
110 +
    let echo: [*[u8]; 6] = ["echo", "", "a b", "c'd\"e$f", "x\ny", "-quoted"];
111 +
    let merged = [
112 +
        Expect::Exact("one\ntwo\nthree\n"),
113 +
        Expect::Matching { text: "t",   count: 2 },
114 +
        Expect::Matching { text: "e",   count: 2 },
115 +
        Expect::Matching { text: "one", count: 1 },
116 +
    ];
117 +
    let separate = [Expect::Exact("one\nthree\n"), Expect::Same(h.path(&["out.txt"]))];
118 +
    let hello = [Expect::Exact("hello\n"), Expect::Line("hello")];
119 +
    let text = "<>\n<a b>\n<c'd\"e$f>\n<x\ny>\n<-quoted>\n";
120 +
    try fs::writeFile(h.path(&["out.txt"]), "an older and much longer text\n");
121 +
    streams(h, "stdout to a file", &["stdout", "hello"], "out.txt", nil, &hello[..], nil);
122 +
    streams(h, "stderr to a file", &["stderr", "oops"], nil, "err.txt", &[], "oops\n");
123 +
    streams(h, "merged streams", &["mixed"], "both.txt", "both.txt", &merged[..], nil);
124 +
    streams(h, "separate streams", &["mixed"], "out.txt", "err.txt", &separate[..], "two\n");
125 +
    streams(
126 +
        h,
127 +
        "an output file is truncated",
128 +
        &["stdout", "new"],
129 +
        "out.txt",
130 +
        nil,
131 +
        &[Expect::Exact("new\n")],
132 +
        nil
133 +
    );
134 +
    streams(h, "discarded streams", &["mixed"], nil, nil, &[], nil);
135 +
    streams(
136 +
        h, "arguments arrive unchanged", &echo[..], "echo.txt", nil, &[Expect::Exact(text)], nil
137 +
    );
138 +
    streams(
139 +
        h, "no arguments after the first", &["echo"], "echo.txt", nil, &[Expect::Exact("")], nil
140 +
    );
141 +
    h.expect("discarded output creates no file", h.path(&["none.txt"]), Expect::Absent);
142 +
}
143 +
144 +
/// `waitAny` returns each finished child once, and waiting for a child that
145 +
/// is already finished or for none at all reports `NotFound`.
146 +
fn waiting(h: &mut Harness) throws (sys::Error) {
147 +
    let mut handles: [u32; 4] = [0; 4];
148 +
    let mut seen: [bool; 4] = [false; 4];
149 +
    for i in 0..4 {
150 +
        let started = try process::spawn(&child(h, &["exit", h.num(11 + i)]));
151 +
        set handles[i] = started.handle;
152 +
    }
153 +
    for round in 0..4 {
154 +
        let done = try process::waitAny();
155 +
        let mut index: u32 = 4;
156 +
        for handle, i in &handles[..] {
157 +
            if handle == done.child.handle {
158 +
                set index = i;
159 +
            }
160 +
        }
161 +
        if index == 4 or seen[index] {
162 +
            h.fail("waitAny", "returned a child that was not started, or one twice");
163 +
            continue;
164 +
        }
165 +
        set seen[index] = true;
166 +
        h.outcome("waitAny", done.outcome, Outcome::Exited(11 + index as i32), nil);
167 +
    }
168 +
    let mut error: ?sys::Error = nil;
169 +
    try process::waitAny() catch err {
170 +
        set error = err;
171 +
    };
172 +
    expectError(h, "waitAny with no child left", error, sys::Error::NotFound);
173 +
    let only = try process::spawn(&child(h, &["exit", "7"]));
174 +
    h.outcome("wait reports the status", try process::wait(only), Outcome::Exited(7), nil);
175 +
    set error = nil;
176 +
    try process::wait(only) catch err {
177 +
        set error = err;
178 +
    };
179 +
    expectError(h, "wait for a child that was waited for", error, sys::Error::NotFound);
180 +
    set error = nil;
181 +
    try process::wait(process::Child { handle: only.handle + 1000 }) catch err {
182 +
        set error = err;
183 +
    };
184 +
    expectError(h, "wait for an unknown child", error, sys::Error::NotFound);
185 +
}
186 +
187 +
/// The host runs 64 children at once and refuses the 65th until one is
188 +
/// waited for.
189 +
fn limit(h: &mut Harness) throws (sys::Error) {
190 +
    let mut spin = child(h, &["spin"]);
191 +
    set spin.maxSteps = SPIN_STEPS;
192 +
    for i in 0..64 {
193 +
        try process::spawn(&spin);
194 +
    }
195 +
    let mut error: ?sys::Error = nil;
196 +
    try process::spawn(&spin) catch err {
197 +
        set error = err;
198 +
    };
199 +
    expectError(h, "the 65th child", error, sys::Error::Limit);
200 +
    for i in 0..64 {
201 +
        let done = try process::waitAny();
202 +
        h.outcome("a spinning child ends at its limit", done.outcome, Outcome::StepLimit, nil);
203 +
    }
204 +
    h.run(
205 +
        "a child starts after the others were waited for",
206 +
        child(h, &["exit", "0"]),
207 +
        harness::OK,
208 +
        &[]
209 +
    );
210 +
}
211 +
212 +
/// The worker pool returns the outcomes in the order of the batch, whatever
213 +
/// the number of workers.
214 +
fn pool(h: &mut Harness) {
215 +
    let saved = h.jobs;
216 +
    let mut batch: *mut [Command] = &mut [];
217 +
    for i in 0..8 {
218 +
        try! batch.append(child(h, &["exit", h.num(20 + i)]), &mut h.arena);
219 +
    }
220 +
    let commands = &batch[..];
221 +
    for workers in &[1, 3, 64] {
222 +
        set h.jobs = workers;
223 +
        let outcomes = h.runAll(commands);
224 +
        let what = h.join(&["pool with ", h.num(workers), " workers"]);
225 +
        h.outcome(what, Outcome::Exited(outcomes.len as i32), Outcome::Exited(8), nil);
226 +
        for outcome, i in outcomes {
227 +
            h.outcome(what, outcome, Outcome::Exited(20 + i as i32), nil);
228 +
        }
229 +
    }
230 +
    set h.jobs = saved;
231 +
}
232 +
233 +
/// Run all tests.
234 +
export fn check(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
235 +
    try h.begin("host");
236 +
    let mark = h.failed;
237 +
    outcomes(h);
238 +
    try files(h);
239 +
    for harts in &[0, 1, 2] {
240 +
        let target = Target::Machine(process::Machine { harts, memory: 0, irq: nil });
241 +
        let run = h.cmd(
242 +
            "bin/native.fixture.rv64", &[], target, harness::MACHINE_STEPS, Output::Discard
243 +
        );
244 +
        h.run(h.join(&["machine with ", h.num(harts), " harts"]), run, harness::OK, &[]);
245 +
    }
246 +
    try waiting(h);
247 +
    try limit(h);
248 +
    pool(h);
249 +
    // The host starts the child before it loads the image, so a missing image
250 +
    // ends as a fault.
251 +
    let mut missing = child(h, &[]);
252 +
    set missing.image = "test/host/missing.rv64";
253 +
    h.run("a missing image", missing, Outcome::Fault, &[]);
254 +
    h.done(mark, "host: process outcomes, streams, arguments, sizes, machines and limits passed");
255 +
}
test/integration/kernel.rad added +161 -0
1 +
//! Kernel fixture images: the kernel built from explicitly mapped modules,
2 +
//! assembled with a fixture's machine code, and the binary inputs a fixture
3 +
//! embeds as byte arrays.
4 +
5 +
use std::fs;
6 +
use std::mem;
7 +
use std::process;
8 +
use std::sys;
9 +
use super::harness;
10 +
use super::harness::Harness;
11 +
use super::harness::Expect;
12 +
use std::process::Outcome;
13 +
use std::process::Output;
14 +
use std::process::Target;
15 +
16 +
/// Directory of the kernel modules and assembly.
17 +
constant KERNEL_DIR: *[u8] = "kernel/kernel";
18 +
/// Module value of the kernel ABI, for the fixture packages.
19 +
export constant ABI: *[u8] = "abi=kernel/kernel/abi.rad";
20 +
/// Module value of the system calls, for the fixture packages.
21 +
export constant SYS: *[u8] = "sys=kernel/kernel/sys.rad";
22 +
/// Module value of the memory-mapped registers, for fixtures with devices.
23 +
export constant MMIO: *[u8] = "mmio=kernel/mmio.rad";
24 +
/// Directory of the binary IL of the standard library and the kernel.
25 +
export constant RIL_DIR: *[u8] = "bin";
26 +
27 +
/// A binary file to embed and the name of the array that holds it.
28 +
export record Embedded: Copy {
29 +
    /// Name of the declared array.
30 +
    name: *[u8],
31 +
    /// Path of the binary file.
32 +
    file: *[u8],
33 +
}
34 +
35 +
/// Write `inputs.rad` in the work directory: one declaration
36 +
/// `export static NAME: [u8; LENGTH] = [ ... ];` per entry, in order.
37 +
fn embed(h: &mut Harness, entries: &[Embedded]) throws (sys::Error) {
38 +
    let mut out = try fs::create(h.path(&["inputs.rad"]));
39 +
    out.write("//! Binary inputs for the fixture.\n");
40 +
    for entry in entries {
41 +
        let data = try h.load(entry.file) catch err {
42 +
            try out.close() catch {};
43 +
            throw err;
44 +
        };
45 +
        out.write(
46 +
            h.join(
47 +
                &[
48 +
                    "\n/// Binary RIL bytes for ",
49 +
                    entry.name,
50 +
                    ".\nexport static ",
51 +
                    entry.name,
52 +
                    ": [u8; ",
53 +
                    h.num(data.len), "] = [\n",
54 +
                ]
55 +
            )
56 +
        );
57 +
        for byte, i in data {
58 +
            out.write(h.join(&[h.num(byte as u32), ","]));
59 +
            if (i + 1) % 16 == 0 or i + 1 == data.len {
60 +
                out.write("\n");
61 +
            }
62 +
        }
63 +
        out.write("];\n");
64 +
    }
65 +
    try out.close();
66 +
}
67 +
68 +
/// Link the packages into a machine image with the image tool, from the
69 +
/// kernel binary IL `std.ril` and `kernel.ril` in `ril`, the assembly
70 +
/// `assembly`, and the boot packages. The tool must end with `want`, and its
71 +
/// output in `<image>.log` must meet the expectations.
72 +
export fn link(
73 +
    h: &mut Harness,
74 +
    what: *[u8],
75 +
    ril: *[u8],
76 +
    assembly: *[u8],
77 +
    image: *[u8],
78 +
    boot: &[*[u8]],
79 +
    want: Outcome,
80 +
    expects: &[Expect]
81 +
) -> bool {
82 +
    h.add(&[h.join(&[ril, "/std.ril"]), h.join(&[ril, "/kernel.ril"]), assembly, image]);
83 +
    h.add(boot);
84 +
    let output = Output::File(h.join(&[image, ".log"]));
85 +
    let tool = h.cmd(
86 +
        "bin/kernel.build.rv64", h.take(), Target::Inherit, harness::COMPILE_STEPS, output
87 +
    );
88 +
    return h.run(what, tool, want, expects);
89 +
}
90 +
91 +
/// Write to `output` the concatenation of `first`, if given, and the kernel
92 +
/// assembly files in ascending order of name.
93 +
export fn concatAssembly(h: &mut Harness, output: *[u8], first: ?*[u8])
94 +
    throws (sys::Error)
95 +
{
96 +
    let mut sources: *mut [*[u8]] = &mut [];
97 +
    if let path = first {
98 +
        try! sources.append(path, &mut h.arena);
99 +
    }
100 +
    for path in try h.listFiles(KERNEL_DIR, ".ras") {
101 +
        try! sources.append(path, &mut h.arena);
102 +
    }
103 +
    try h.concat(output, &sources[..]);
104 +
}
105 +
106 +
/// Compile the fixture package `name` from `module` and the `-mod` values
107 +
/// `mods` to binary IL in the work directory, as the entry of its own
108 +
/// compilation.
109 +
export fn package(h: &mut Harness, name: *[u8], module: *[u8], mods: &[*[u8]]) -> bool {
110 +
    let work = h.work;
111 +
    h.add(&["-pkg", name, "-mod", module]);
112 +
    for value in mods {
113 +
        h.add(&["-mod", value]);
114 +
    }
115 +
    h.add(&["-entry", name, "-ril", work]);
116 +
    return h.compile(h.join(&["compile ", name]), h.out(&[name, ".log"]));
117 +
}
118 +
119 +
/// Build the machine fixture `fixture` into `image`, working in the work
120 +
/// directory. The kernel is compiled with the fixture's modules mapped onto its
121 +
/// own by name, and with the byte arrays of the binary files `entries` as the
122 +
/// module `dispatchcheck/inputs`. `boot` lists binary IL packages to load at
123 +
/// boot. Returns whether the image was built.
124 +
export fn image(
125 +
    h: &mut Harness,
126 +
    fixture: *[u8],
127 +
    image: *[u8],
128 +
    entries: &[Embedded],
129 +
    boot: &[*[u8]]
130 +
) -> bool throws (sys::Error) {
131 +
    let dir = h.join(&["test/", fixture]);
132 +
    let work = h.work;
133 +
    try h.addStd();
134 +
    h.add(&["-pkg", "kernel", "-mod", "test/kernel.rad"]);
135 +
    for path in try h.listFiles(KERNEL_DIR, ".rad") {
136 +
        let name = &path[KERNEL_DIR.len + 1..path.len - ".rad".len];
137 +
        if not mem::eq(name, "tests") {
138 +
            h.named(name, path);
139 +
        }
140 +
    }
141 +
    h.named("fixture", "test/kernel/fixture.rad");
142 +
    h.named("dispatchcheck", h.join(&[dir, "/kernel/dispatchcheck.rad"]));
143 +
    let cases = h.join(&[dir, "/cases.rad"]);
144 +
    if try harness::exists(cases) {
145 +
        h.named("dispatchcheck/cases", cases);
146 +
    }
147 +
    if entries.len > 0 {
148 +
        try embed(h, entries);
149 +
        h.named("dispatchcheck/inputs", h.path(&["inputs.rad"]));
150 +
    }
151 +
    if mem::eq(fixture, "smp") or mem::eq(fixture, "cycles") {
152 +
        h.named("dispatchcheck/invariants", "test/smp/kernel/invariants.rad");
153 +
    }
154 +
    h.add(&["-entry", "kernel", "-ril", work]);
155 +
    if not h.compile(h.join(&["build kernel for ", fixture]), h.out(&["kernel.log"])) {
156 +
        return false;
157 +
    }
158 +
    let machine = h.path(&["machine.ras"]);
159 +
    try concatAssembly(h, machine, h.join(&[dir, "/machine.ras"]));
160 +
    return link(h, h.join(&["link ", fixture]), work, machine, image, boot, harness::OK, &[]);
161 +
}
test/integration/machine.rad added +225 -0
1 +
//! Fixtures that run on a bare machine: synchronization, traps, shared
2 +
//! packages and the kernel dispatch fixtures. Also the rejection of slot
3 +
//! reservation misuse and the check of the checked-in seed image.
4 +
5 +
use std::fs;
6 +
use std::io;
7 +
use std::mem;
8 +
use std::process;
9 +
use std::sys;
10 +
use std::arch::rv64::image;
11 +
use super::harness;
12 +
use super::kernel;
13 +
use super::harness::Harness;
14 +
use super::harness::Expect;
15 +
use std::process::Outcome;
16 +
use std::process::Target;
17 +
use super::kernel::Embedded;
18 +
19 +
/// The checked-in seed compiler image.
20 +
constant SEED: *[u8] = "seed/radiance.rv64";
21 +
22 +
/// Run `tool` on the arguments to build `image`, then run the image on
23 +
/// `harts` harts of a bare machine with the host default memory.
24 +
fn buildAndRun(
25 +
    h: &mut Harness,
26 +
    what: *[u8],
27 +
    tool: *[u8],
28 +
    args: &[*[u8]],
29 +
    image: *[u8],
30 +
    harts: u32
31 +
) -> bool {
32 +
    if not h.tool(h.join(&["build ", what]), tool, args) {
33 +
        return false;
34 +
    }
35 +
    let target = Target::Machine(process::Machine { harts, memory: 0, irq: nil });
36 +
    let run = h.cmd(image, &[], target, harness::MACHINE_STEPS, harness::INHERIT);
37 +
    return h.run(what, run, harness::OK, &[]);
38 +
}
39 +
40 +
/// Assemble the files `sources` into one image and run it on `harts` harts.
41 +
fn assembleAndRun(
42 +
    h: &mut Harness,
43 +
    what: *[u8],
44 +
    prefix: *[u8],
45 +
    sources: &[*[u8]],
46 +
    harts: u32
47 +
) throws (sys::Error) {
48 +
    let assembly = h.path(&[prefix, ".ras"]);
49 +
    let image = h.path(&[prefix, ".rv64"]);
50 +
    try h.concat(assembly, sources);
51 +
    buildAndRun(h, what, "test/sync/build.rv64", &[assembly, image], image, harts);
52 +
}
53 +
54 +
/// Run the synchronization fixture on 1, 2 and 8 harts.
55 +
export fn sync(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
56 +
    try h.begin("sync");
57 +
    let mark = h.failed;
58 +
    for harts in &[1, 2, 8] {
59 +
        let count = h.num(harts);
60 +
        let header = h.path(&["harts", count, ".ras"]);
61 +
        try fs::writeFileParts(header, &[".constant HARTS ", count, ";\n"]);
62 +
        let sources = [header, "test/sync/machine.ras", "kernel/kernel/sync.ras"];
63 +
        let what = h.join(&["synchronization on ", count, " harts"]);
64 +
        try assembleAndRun(h, what, h.join(&["sync", count]), &sources[..], harts);
65 +
    }
66 +
    h.done(
67 +
        mark, "synchronization: code publication, locks, atomics, and MMIO passed on 1/2/8 harts"
68 +
    );
69 +
}
70 +
71 +
/// Run the trap fixtures on one hart.
72 +
export fn trap(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
73 +
    try h.begin("trap");
74 +
    let mark = h.failed;
75 +
    for fixture in &["machine", "reservations"] {
76 +
        let sources = [h.join(&["test/trap/", fixture, ".ras"]), "kernel/kernel/trap.ras"];
77 +
        let what = h.join(&["trap fixture ", fixture]);
78 +
        try assembleAndRun(h, what, h.join(&["trap.", fixture]), &sources[..], 1);
79 +
    }
80 +
    h.done(
81 +
        mark,
82 +
        "trap contexts: machine/user registers, nested timer, illegal instruction, and stacks passed"
83 +
    );
84 +
    h.done(mark, "trap reservations: entry, resume, and user context isolation passed");
85 +
}
86 +
87 +
/// Compile two packages once and run them with two private state graphs, then
88 +
/// link a package built alone with an entry built beside an unrelated package,
89 +
/// whose typed error tags must agree.
90 +
export fn shared(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
91 +
    try h.begin("shared");
92 +
    let mark = h.failed;
93 +
    h.add(&["-pkg", "support", "-mod", "test/shared/support.rad"]);
94 +
    let work = h.work;
95 +
    h.add(&["-pkg", "app", "-mod", "test/shared/app.rad", "-entry", "app", "-ril", work]);
96 +
    if h.compile("compile shared packages", h.out(&["compile.log"])) {
97 +
        let image = h.path(&["native.rv64"]);
98 +
        let packages = [h.path(&["support.ril"]), h.path(&["app.ril"]), image];
99 +
        buildAndRun(h, "shared packages", "test/shared/build.rv64", &packages[..], image, 0);
100 +
    }
101 +
    h.done(
102 +
        mark, "shared packages: calls, callbacks, private pointers, and independent state passed"
103 +
    );
104 +
    let alone = h.path(&["alone"]);
105 +
    let combined = h.path(&["combined"]);
106 +
    try harness::makeDirs(alone);
107 +
    try harness::makeDirs(combined);
108 +
    h.add(&["-pkg", "support", "-mod", "test/shared/errors/support.rad", "-ril", alone]);
109 +
    let first = h.compile("compile support alone", h.out(&["alone.log"]));
110 +
    h.add(&["-pkg", "noise", "-mod", "test/shared/errors/noise.rad"]);
111 +
    h.add(&["-pkg", "support", "-mod", "test/shared/errors/support.rad"]);
112 +
    h.add(&[
113 +
        "-pkg", "app", "-mod", "test/shared/errors/app.rad", "-entry", "app", "-ril", combined
114 +
    ]);
115 +
    let second = h.compile("compile support beside noise", h.out(&["combined.log"]));
116 +
    if first and second {
117 +
        let image = h.path(&["errors.rv64"]);
118 +
        let packages = [
119 +
            h.join(&[alone, "/support.ril"]), h.join(&[combined, "/app.ril"]), image, "catch"
120 +
        ];
121 +
        buildAndRun(
122 +
            h, "typed catch across compilations", "test/shared/build.rv64", &packages[..], image, 0
123 +
        );
124 +
    }
125 +
    h.done(mark, "shared packages: typed catch agrees across separate compilations");
126 +
}
127 +
128 +
/// Check that misuse of reservations fails during compilation: one consumed
129 +
/// twice and one that is never consumed.
130 +
export fn slots(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
131 +
    try h.begin("slots");
132 +
    let names = ["duplicate", "abandon"];
133 +
    let diagnostics = ["linear value used after consumption", "linear value is not consumed"];
134 +
    for name, i in &names[..] {
135 +
        let what = h.join(&["reservation rejection: ", name]);
136 +
        h.add(
137 +
            &[
138 +
                "-pkg",
139 +
                "kernel",
140 +
                "-mod",
141 +
                "test/slots/kernel.rad",
142 +
                "-mod",
143 +
                "slots=kernel/kernel/slots.rad",
144 +
            ]
145 +
        );
146 +
        h.add(&["-mod", kernel::ABI, "-mod", "limits=kernel/kernel/limits.rad"]);
147 +
        h.add(&["-pkg", name, "-mod", h.join(&["test/slots/", name, ".rad"]), "-entry", name]);
148 +
        h.add(&["-o", h.path(&["test.rv64"])]);
149 +
        let compile = h.cc(h.take(), h.out(&[name, ".log"]));
150 +
        let diagnosed = Expect::Contains(diagnostics[i]);
151 +
        let mark = h.failed;
152 +
        h.run(what, compile, Outcome::Exited(1), &[diagnosed]);
153 +
        h.done(mark, h.join(&[what, " passed"]));
154 +
    }
155 +
}
156 +
157 +
/// Check that the seed is a self-contained RV64 image whose size is the
158 +
/// header plus its three stored sections.
159 +
export fn seed(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
160 +
    let info = try fs::stat(SEED) else {
161 +
        h.fail("seed test", h.join(&[SEED, " does not exist"]));
162 +
        return;
163 +
    };
164 +
    let header = try h.load(SEED);
165 +
    let layout = try image::parse(header) catch {
166 +
        h.fail("seed test", "seed is not a self-contained RV64 image");
167 +
        return;
168 +
    };
169 +
    let expected = image::HEADER_SIZE as u64
170 +
        + layout.code.size as u64
171 +
        + layout.roData.size as u64
172 +
        + layout.rwData.size as u64;
173 +
    if info.size <> expected {
174 +
        h.fail("seed test", h.join(&[
175 +
            "seed image size ", h.num(info.size as u32),
176 +
            " does not match the header sections ", h.num(expected as u32)
177 +
        ]));
178 +
        return;
179 +
    }
180 +
    h.pass();
181 +
    io::printLn("seed test: ok");
182 +
}
183 +
184 +
/// Build the dispatch fixture `fixture` into `image`. The image appears
185 +
/// complete or not at all: it is built in `bin/test/dispatch-build-<fixture>`
186 +
/// and renamed into place.
187 +
export fn dispatchBuild(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
188 +
    let fixture = args[0];
189 +
    try h.begin(h.join(&["dispatch-build-", fixture]));
190 +
    let cases = h.join(&["test/", fixture, "/cases.rad"]);
191 +
    let mut mods = [kernel::ABI, kernel::SYS, kernel::MMIO, h.join(&["cases=", cases])];
192 +
    let mut count: u32 = 2;
193 +
    if mem::eq(fixture, "mmio") {
194 +
        set count = 3;
195 +
    }
196 +
    if try harness::exists(cases) {
197 +
        set mods[count] = mods[3];
198 +
        set count += 1;
199 +
    }
200 +
    let module = h.join(&["test/", fixture, "/spin.rad"]);
201 +
    let staged = h.path(&["dispatch.rv64"]);
202 +
    let inputs = [Embedded { name: "INPUT", file: h.path(&["spin.ril"]) }];
203 +
    if kernel::package(h, "spin", module, &mods[..count])
204 +
        and try kernel::image(h, fixture, staged, &inputs[..], &[])
205 +
    {
206 +
        try fs::rename(staged, args[1]);
207 +
    }
208 +
}
209 +
210 +
/// Build the controller and worker images that the cycles fixture loads after
211 +
/// dispatch starts, and the kernel image that embeds them, into the image.
212 +
export fn cyclesBuild(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
213 +
    try h.begin("cycles-build");
214 +
    let mods = ["support=test/cycles/support.rad", kernel::ABI, kernel::SYS, kernel::MMIO];
215 +
    let spin = kernel::package(h, "spin", "test/cycles/spin.rad", &mods[..]);
216 +
    let job = kernel::package(h, "job", "test/cycles/job.rad", &mods[..3]);
217 +
    let staged = h.path(&["cycles.rv64"]);
218 +
    let inputs = [
219 +
        Embedded { name: "spin", file: h.path(&["spin.ril"]) },
220 +
        Embedded { name: "job",  file: h.path(&["job.ril"]) },
221 +
    ];
222 +
    if spin and job and try kernel::image(h, "cycles", staged, &inputs[..], &[]) {
223 +
        try fs::rename(staged, args[0]);
224 +
    }
225 +
}
test/integration/verify.rad added +154 -0
1 +
//! The IL verifier check: every fixture that compiles to binary IL is
2 +
//! compiled, the checker verifies the packages, and its verdicts are compared
3 +
//! with the expected rejections in `test/verify/expected`.
4 +
5 +
use std::char;
6 +
use std::io;
7 +
use std::mem;
8 +
use std::process;
9 +
use std::sys;
10 +
use super::fixtures;
11 +
use super::harness;
12 +
use super::harness::Harness;
13 +
use super::harness::Expect;
14 +
use std::process::Output;
15 +
use std::process::Target;
16 +
use std::process::Command;
17 +
18 +
/// Separator between a path and its verdict in the checker's report.
19 +
constant SEPARATOR: *[u8] = ": ";
20 +
21 +
/// Print the items of `from` that `other` lacks, each after `mark`, on
22 +
/// standard error. Returns the number of items printed.
23 +
fn missing(from: *[*[u8]], other: *[*[u8]], mark: *[u8]) -> u32 {
24 +
    let mut count: u32 = 0;
25 +
    for item in from {
26 +
        let mut found = false;
27 +
        for candidate in other {
28 +
            if mem::eq(item, candidate) {
29 +
                set found = true;
30 +
            }
31 +
        }
32 +
        if not found {
33 +
            io::printError(mark);
34 +
            io::printError(item);
35 +
            io::printError("\n");
36 +
            set count += 1;
37 +
        }
38 +
    }
39 +
    return count;
40 +
}
41 +
42 +
/// Check that two lists of distinct items have the same items. A difference
43 +
/// fails the check labeled `what`.
44 +
fn same(h: &mut Harness, what: *[u8], left: *[*[u8]], right: *[*[u8]]) -> bool {
45 +
    let count = missing(left, right, "-") + missing(right, left, "+");
46 +
    if count == 0 and left.len == right.len {
47 +
        return h.pass();
48 +
    }
49 +
    return h.fail("verify", what);
50 +
}
51 +
52 +
/// Compile every fixture that has no native assembly to binary IL, verify the
53 +
/// packages with `checker`, and compare the verdicts with the expected ones.
54 +
export fn check(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
55 +
    try h.begin("verify");
56 +
    let mut checker: *[u8] = "test/verify/check.rv64";
57 +
    if args.len == 1 {
58 +
        set checker = args[0];
59 +
    }
60 +
    // Native assembly inputs cannot be emitted as binary IL.
61 +
    let mut names: *mut [*[u8]] = &mut [];
62 +
    let mut batch: *mut [Command] = &mut [];
63 +
    for source in try h.listFiles("test/tests", ".rad") {
64 +
        let base = harness::stripExt(source);
65 +
        if try harness::exists(h.join(&[base, ".ras"]))
66 +
            or try harness::exists(h.join(&[base, ".start.ras"]))
67 +
        {
68 +
            continue;
69 +
        }
70 +
        let name = &base["test/tests/".len..];
71 +
        try fixtures::prepare(h, source, h.path(&[name]));
72 +
        try! batch.append(h.cc(h.take(), h.out(&[name, ".log"])), &mut h.arena);
73 +
        try! names.append(name, &mut h.arena);
74 +
    }
75 +
    let outcomes = h.runAll(&batch[..]);
76 +
    let mut packages: *mut [*[u8]] = &mut [];
77 +
    for name, i in &names[..] {
78 +
        let produced = h.path(&[name, "/test.ril"]);
79 +
        if h.outcome(name, outcomes[i], harness::OK, h.path(&[name, ".log"]))
80 +
            and h.expect(name, produced, Expect::Present)
81 +
        {
82 +
            try! packages.append(produced, &mut h.arena);
83 +
        }
84 +
    }
85 +
    if h.failed > 0 {
86 +
        return;
87 +
    }
88 +
    harness::sort(&mut packages[..]);
89 +
    let list = &packages[..];
90 +
    let verdicts = h.path(&["checks"]);
91 +
    let mut command = h.cmd(checker, &[], Target::Inherit, harness::RUN_STEPS, Output::File(
92 +
        verdicts
93 +
    ));
94 +
    set command.args = list;
95 +
    set command.stderr = Output::Inherit;
96 +
    if not h.run("verify checker", command, harness::OK, &[]) {
97 +
        return;
98 +
    }
99 +
    // Each package is reported exactly once.
100 +
    let mut reported: *mut [*[u8]] = &mut [];
101 +
    let mut accepted: u32 = 0;
102 +
    let mut rejected: *mut [*[u8]] = &mut [];
103 +
    let mut lines = mem::split(try h.load(verdicts), '\n');
104 +
    while let line = lines.next() {
105 +
        let at = mem::find(line, SEPARATOR) else {
106 +
            try! reported.append(line, &mut h.arena);
107 +
            continue;
108 +
        };
109 +
        let path = &line[..at];
110 +
        let verdict = &line[at + SEPARATOR.len..];
111 +
        let mut end: u32 = 0;
112 +
        while end < verdict.len and char::isAlpha(verdict[end]) {
113 +
            set end += 1;
114 +
        }
115 +
        // The fixture is the directory that holds the package.
116 +
        let mut name = path;
117 +
        if mem::endsWith(path, "/test.ril") {
118 +
            let mut parts = mem::split(&path[..path.len - "/test.ril".len], '/');
119 +
            while let part = parts.next() {
120 +
                set name = part;
121 +
            }
122 +
        }
123 +
        try! reported.append(path, &mut h.arena);
124 +
        if mem::eq(&verdict[..end], "ok") {
125 +
            set accepted += 1;
126 +
        } else {
127 +
            try! rejected.append(h.join(&[name, " ", &verdict[..end]]), &mut h.arena);
128 +
        }
129 +
    }
130 +
    if not same(h, "the checker's report does not match the packages", list, &reported[..]) {
131 +
        return;
132 +
    }
133 +
    let total = accepted + rejected.len;
134 +
    let count = rejected.len;
135 +
    io::print(
136 +
        h.join(
137 +
            &[
138 +
                "verify: ",
139 +
                h.num(total),
140 +
                " fixtures, ",
141 +
                h.num(accepted),
142 +
                " accepted, ",
143 +
                h.num(count),
144 +
                " rejected\n",
145 +
            ]
146 +
        )
147 +
    );
148 +
    same(
149 +
        h,
150 +
        "the rejected fixtures differ from the expected ones",
151 +
        try h.manifest("test/verify/expected"),
152 +
        &rejected[..]
153 +
    );
154 +
}
test/kernel/build deleted +0 -36
1 -
#!/bin/sh
2 -
# Build a machine-test fixture from explicitly mapped kernel modules.
3 -
# Usage: sh test/kernel/build fixture work image [boot-packages...]
4 -
set -eu
5 -
fixture=$1
6 -
work=$2
7 -
image=$3
8 -
shift 3
9 -
(
10 -
    set -- -pkg std
11 -
    while IFS= read -r source; do set -- "$@" -mod "$source"; done < std.lib
12 -
    set -- "$@" -pkg kernel -mod test/kernel.rad
13 -
    for source in kernel/kernel/*.rad; do
14 -
        name=${source##*/}
15 -
        name=${name%.rad}
16 -
        [ "$name" != tests ] || continue
17 -
        set -- "$@" -mod "$name=$source"
18 -
    done
19 -
    set -- "$@" -mod fixture=test/kernel/fixture.rad
20 -
    set -- "$@" -mod "dispatchcheck=test/$fixture/kernel/dispatchcheck.rad"
21 -
    if [ -f "test/$fixture/cases.rad" ]; then
22 -
        set -- "$@" -mod "dispatchcheck/cases=test/$fixture/cases.rad"
23 -
    fi
24 -
    if [ -f "$work/inputs.rad" ]; then
25 -
        set -- "$@" -mod "dispatchcheck/inputs=$work/inputs.rad"
26 -
    fi
27 -
    case $fixture in
28 -
        smp|cycles)
29 -
            set -- "$@" -mod dispatchcheck/invariants=test/smp/kernel/invariants.rad
30 -
            ;;
31 -
    esac
32 -
    scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry kernel -ril "$work"
33 -
)
34 -
cat "test/$fixture/machine.ras" kernel/kernel/*.ras > "$work/machine.ras"
35 -
scripts/hosted bin/kernel.build.rv64 -- "$work/std.ril" "$work/kernel.ril" \
36 -
    "$work/machine.ras" "$image" "$@"
test/kernel/embed deleted +0 -13
1 -
#!/bin/sh
2 -
# Write a Radiance byte-array declaration for a binary fixture.
3 -
# Usage: embed name binary-file
4 -
set -eu
5 -
name=$1
6 -
source=$2
7 -
length=$(wc -c < "$source")
8 -
bytes=$(od -An -v -tu1 "$source")
9 -
printf '\n/// Binary RIL bytes for %s.\nexport static %s: [u8; %s] = [\n' \
10 -
    "$name" "$name" "$length"
11 -
printf '%s\n' "$bytes" |
12 -
    awk '{ for (i = 1; i <= NF; i++) printf "%s,", $i; print "" }'
13 -
printf '];\n'
test/loader/hostile.rad +4 -6
4 4
//! encoded. Every package is small and well formed for the decoder, and breaks exactly one
5 5
//! rule of the verifier. The tool writes `<name>.ril` for each package into the directory
6 6
//! given as the first argument.
7 7
8 8
use std::sys;
9 -
use std::sys::unix;
9 +
use std::fs;
10 10
use std::lang::alloc;
11 11
use std::lang::il;
12 12
use std::lang::il::binary;
13 13
use std::lang::il::binary::program;
14 14
124 124
    let mut path: *mut [u8] = &mut [];
125 125
    try! path.extend(directory, &mut arena);
126 126
    try! path.extend("/", &mut arena);
127 127
    try! path.extend(name, &mut arena);
128 128
    try! path.extend(".ril", &mut arena);
129 -
    // The system call takes a terminated string.
130 -
    try! path.append(0, &mut arena);
131 -
    assert unix::writeFile(&path[..], &OUT[..length]);
129 +
    try! fs::writeFile(&path[..], &OUT[..length]);
132 130
}
133 131
134 132
/// Generate every hostile package into the directory in the first argument.
135 133
@default unsafe fn main(env: *sys::Env) -> i32 {
136 -
    assert env.args.len == 2;
137 -
    let directory = env.args[1];
134 +
    assert env.args.len == 1;
135 +
    let directory = env.args[0];
138 136
    let plain: *[*[u8]] = &["hostile", ENTRY];
139 137
    let none: *[*[u8]] = &[];
140 138
    // A word of data is dereferenced as if it were a pointer.
141 139
    emit(directory, "forged", none, &[], &[
142 140
        il::Instr::Copy { dst: il::Reg { n: 0 }, val: il::Val::Imm(0x80000000) },
test/loader/run deleted +0 -31
1 -
#!/bin/sh
2 -
# Build a kernel fixture and call code produced by its runtime loader.
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 -pkg loaded -mod test/loader/loaded.rad -entry loaded -ril "$work"
8 -
scripts/hosted bin/radiance.rv64.dev -pkg loaded -mod test/loader/loaded.rad -entry loaded -o "$work/hosted.rv64"
9 -
status=0
10 -
"$emulator" -run "$work/hosted.rv64" || status=$?
11 -
test "$status" -eq 43
12 -
printf '//! Binary packages used by the loader fixture.\n' > "$work/inputs.rad"
13 -
sh test/kernel/embed INPUT "$work/loaded.ril" >> "$work/inputs.rad"
14 -
# Hostile packages, generated as typed IL because the language cannot express them.
15 -
mkdir "$work/hostile"
16 -
"$emulator" -run test/loader/hostile.rv64 -- "$work/hostile"
17 -
for name in forged overwrite leak extern dependent buffer; do
18 -
    sh test/kernel/embed "HOSTILE_$(echo "$name" | tr a-z A-Z)" \
19 -
        "$work/hostile/$name.ril" >> "$work/inputs.rad"
20 -
done
21 -
sh test/kernel/build loader "$work" "$work/loader.rv64"
22 -
"$emulator" -machine -memory-size=262144 -max-steps=200000000 -run "$work/loader.rv64"
23 -
# The image builder verifies user packages as the loader does, and writes no image for a hostile one.
24 -
status=0
25 -
scripts/hosted bin/kernel.build.rv64 \
26 -
    -- "$work/std.ril" "$work/kernel.ril" "$work/machine.ras" "$work/rejected.rv64" "$work/hostile/forged.ril" \
27 -
    > "$work/rejected.log" 2>&1 || status=$?
28 -
test "$status" -eq 1
29 -
grep -q 'boot package fails verification: Type' "$work/rejected.log"
30 -
test ! -e "$work/rejected.rv64"
31 -
printf 'runtime loader: hosted and loaded entries agree, and hostile packages are rejected\n'
test/native/build.rad +8 -10
1 1
//! Build a native machine fixture with the reusable RV64 backend.
2 2
3 3
use std::sys;
4 -
use std::sys::unix;
4 +
use std::fs;
5 5
use std::lang::il;
6 6
use std::lang::alloc;
7 7
use std::lang::gen::data;
8 8
use std::lang::gen::labels;
9 9
use std::collections::dict;
36 36
/// Argument range of a branch that passes no block arguments.
37 37
constant NO_ARGS: il::ArgRange = il::ArgRange { start: 0, len: 0 };
38 38
39 39
/// Compile data accesses and conditional finish writes into a native image.
40 40
@default unsafe fn main(env: *sys::Env) -> i32 {
41 -
    assert env.args.len == 2;
41 +
    assert env.args.len == 1;
42 42
    let mut arena = alloc::new(&mut MEMORY[..]);
43 43
    let mut scratch = alloc::new(&mut SCRATCH[..]);
44 44
    let mut blockOffsets: *mut [i32] = &mut [];
45 45
    try! blockOffsets.resize(labels::MAX_BLOCKS_PER_FN, 0, &mut arena);
46 46
    let mut funcEntries: *mut [dict::Entry] = &mut [];
154 154
    let result = try! rv64::finishProgram(&mut generator, globals,
155 155
        rv64::Storage { dataSyms, dataSymTable: dict::init(symEntries) },
156 156
        &[], &mut ro[..], &mut rw[..]);
157 157
    let header = try! image::header(result.layout);
158 158
    let code = @sliceOf(result.code.ptr as *u8, result.code.len * 4);
159 -
    let fd = unix::openOpts(env.args[1],
160 -
        unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC), 420);
161 -
    assert fd >= 0;
162 -
    let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd, code)
163 -
        and unix::writeAll(fd, &ro[..result.roDataSize]) and unix::writeAll(fd,
164 -
        &rw[..result.rwDataSize]);
165 -
    let closed = unix::close(fd) == 0;
166 -
    assert written and closed;
159 +
    let mut out = try! fs::create(env.args[0]);
160 +
    out.write(&header[..]);
161 +
    out.write(code);
162 +
    out.write(&ro[..result.roDataSize]);
163 +
    out.write(&rw[..result.rwDataSize]);
164 +
    try! out.close();
167 165
    return 0;
168 166
}
test/run deleted +0 -113
1 -
#!/bin/sh
2 -
# Run binary tests.
3 -
# Usage: test/run [<test.rad|test.ras>...]
4 -
#
5 -
# Tests run in source order. Make controls concurrency between test suites.
6 -
#
7 -
# If no arguments are provided, runs all `.rad` and `.ras` tests in
8 -
# `test/tests/`.
9 -
#
10 -
# For each test:
11 -
#   - If a `.ril` file exists alongside it, the IL output is checked
12 -
#     against it via the runner binary.
13 -
#   - If `//! returns: N` appears in the file, the test is compiled to
14 -
#     a binary and executed; the exit code must match N.
15 -
#   - If `//! rw-data-size: N` appears in the file, the emitted image header's
16 -
#     rwdata stored size (the u32 at byte offset 40) must match N bytes.
17 -
18 -
19 -
set -eu
20 -
21 -
RUNNER="test/runner.rv64"
22 -
TEST_DIR="test/tests"
23 -
emulator=${RAD_EMULATOR:-emulator}
24 -
25 -
if [ ! -f "$RUNNER" ]; then
26 -
  echo "error: runner binary not found: $RUNNER" >&2
27 -
  echo "hint: run 'make test' first" >&2
28 -
  exit 1
29 -
fi
30 -
31 -
# Disable core dumps for tests.
32 -
ulimit -c 0
33 -
34 -
# Collect tests.
35 -
if [ $# -eq 0 ]; then
36 -
  set -- "$TEST_DIR"/*.rad "$TEST_DIR"/*.ras
37 -
fi
38 -
39 -
# Tests are independent. Each check contributes to the suite result.
40 -
passed=0
41 -
failed=0
42 -
for test in "$@"; do
43 -
  if [ ! -r "$test" ]; then
44 -
    echo "test $test ... FAILED (source not readable)"
45 -
    failed=$((failed + 1))
46 -
    continue
47 -
  fi
48 -
  base=${test%.*}
49 -
50 -
  ril="${base}.ril"
51 -
  bin="${base}.rv64"
52 -
  # IL check: run the runner if a .ril file exists.
53 -
  if [ -f "$ril" ]; then
54 -
    if timeout 10 "$emulator" -stack-size=1024 -run "$RUNNER" -- "$test"; then
55 -
      passed=$((passed + 1))
56 -
    else
57 -
      failed=$((failed + 1))
58 -
    fi
59 -
  fi
60 -
61 -
  # Execution check: run the binary if //! returns: is present.
62 -
  returns=$(sed -n 's|^//! returns: *||p' "$test")
63 -
  if [ -n "$returns" ]; then
64 -
    echo -n "test $test ... "
65 -
66 -
    if [ ! -f "$bin" ]; then
67 -
      echo "FAILED (binary not found: $bin)"
68 -
      failed=$((failed + 1))
69 -
      continue
70 -
    fi
71 -
72 -
    ret=0
73 -
    timeout 10 "$emulator" -run "$bin" >/dev/null 2>&1 || ret=$?
74 -
75 -
    if [ "$ret" -eq "$returns" ]; then
76 -
      echo "ok"
77 -
      passed=$((passed + 1))
78 -
    else
79 -
      echo "FAILED (expected: $returns, got: $ret)"
80 -
      failed=$((failed + 1))
81 -
    fi
82 -
  fi
83 -
84 -
  rw_data_size=$(sed -n 's|^//! rw-data-size: *||p' "$test")
85 -
  if [ -n "$rw_data_size" ]; then
86 -
    echo -n "test $test rw.data ... "
87 -
88 -
    if [ ! -f "$bin" ]; then
89 -
      echo "FAILED (binary not found: $bin)"
90 -
      failed=$((failed + 1))
91 -
      continue
92 -
    fi
93 -
94 -
    actual_rw_data_size=$(od -An -tu4 -j40 -N4 "$bin" | tr -d ' ')
95 -
96 -
    if [ "$actual_rw_data_size" -eq "$rw_data_size" ]; then
97 -
      echo "ok"
98 -
      passed=$((passed + 1))
99 -
    else
100 -
      echo "FAILED (expected: $rw_data_size, got: $actual_rw_data_size)"
101 -
      failed=$((failed + 1))
102 -
    fi
103 -
  fi
104 -
done
105 -
echo
106 -
107 -
if [ "$failed" -eq 0 ]; then
108 -
  echo "test result: ok. $passed passed; $failed failed"
109 -
  exit 0
110 -
else
111 -
  echo "test result: FAILED. $passed passed; $failed failed"
112 -
  exit 1
113 -
fi
test/runner.rad +23 -22
5 5
//! `assemble <input.ras> <output.rv64>` subcommand used by `bin-test`.
6 6
7 7
use std::io;
8 8
use std::mem;
9 9
use std::sys;
10 -
use std::sys::unix;
10 +
use std::fs;
11 11
use std::lang::alloc;
12 12
use std::lang::ast;
13 13
use std::lang::il::printer;
14 14
use std::lang::parser;
15 15
use std::lang::scanner;
157 157
    io::print(actual);
158 158
    io::print("\n");
159 159
}
160 160
161 161
/// Derive the `.ril` path from a `.rad` source path. Returns nil if the path
162 -
/// does not end in `.rad` or the buffer is too small. The result is
163 -
/// null-terminated for use with syscalls.
162 +
/// does not end in `.rad` or the buffer is too small.
164 163
fn deriveRilPath(sourcePath: *[u8], buf: &mut [u8]) -> ?u32 {
165 164
    let len = sourcePath.len;
166 165
    if len < SOURCE_EXT.len {
167 166
        return nil;
168 167
    }
169 168
    let extStart = len - SOURCE_EXT.len;
170 169
    // Check for .rad extension.
171 170
    if not mem::eq(&sourcePath[extStart..len], SOURCE_EXT) {
172 171
        return nil;
173 172
    }
174 -
    if len + 1 > buf.len {
173 +
    if len > buf.len {
175 174
        return nil;
176 175
    }
177 -
    // Copy full path then overwrite extension and add null terminator.
176 +
    // Copy full path then overwrite the extension.
178 177
    try mem::copy(buf, sourcePath) catch {
179 178
        return nil;
180 179
    };
181 180
    try mem::copy(&mut buf[extStart..len], SNAPSHOT_EXT) catch {
182 181
        return nil;
183 182
    };
184 -
    set buf[len] = 0;
185 -
186 183
    return len;
187 184
}
188 185
189 186
/// Create an empty string pool whose table is permanent storage in `arena`.
190 187
fn newPool(arena: &mut alloc::Arena) -> strings::Pool {
208 205
/// rodata, and rwdata payloads.
209 206
fn writeImage(
210 207
    layout: image::Layout,
211 208
    code: *[u32],
212 209
    roData: &[u8],
213 -
    rwData: *[u8],
210 +
    rwData: &[u8],
214 211
    path: *[u8]
215 212
) -> bool {
216 213
    let header = try image::header(layout) catch {
217 214
        return false;
218 215
    };
219 -
    let fd = unix::openOpts(path, unix::OpenFlags(*unix::O_WRONLY |
220 -
        *unix::O_CREAT | *unix::O_TRUNC), unix::FILE_MODE);
221 -
    if fd < 0 { return false; }
222 -
    let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd, codeBytes(code))
223 -
        and unix::writeAll(fd, roData) and unix::writeAll(fd, rwData);
224 -
    let closed = unix::close(fd) == 0;
225 -
    return written and closed;
216 +
    let mut out = try fs::create(path) catch {
217 +
        return false;
218 +
    };
219 +
    out.write(&header[..]);
220 +
    out.write(codeBytes(code));
221 +
    out.write(roData);
222 +
    out.write(rwData);
223 +
    try out.close() catch {
224 +
        return false;
225 +
    };
226 +
    return true;
226 227
}
227 228
228 229
/// Assemble the `.ras` file at `sourcePath` into an image at `outputPath`.
229 230
fn assembleBinary(sourcePath: *[u8], outputPath: *[u8], storage: *mut Storage) -> bool {
230 -
    let sourceLen = unix::readFile(sourcePath, &mut storage.source[..]) else {
231 +
    let sourceLen = try fs::readFile(sourcePath, &mut storage.source[..]) catch {
231 232
        io::printError("error: could not read source: ");
232 233
        io::printError(sourcePath);
233 234
        io::printError("\n");
234 235
        return false;
235 236
    };
271 272
        io::printLn(sourcePath);
272 273
        return false;
273 274
    };
274 275
275 276
    // Read expected IL.
276 -
    let expectedLen = unix::readFile(&rilPathBuf[..rilPathLen], &mut storage.expected[..]) else {
277 +
    let expectedLen = try fs::readFile(&rilPathBuf[..rilPathLen], &mut storage.expected[..]) catch {
277 278
        io::print("error: could not read expected IL: ");
278 279
        io::printLn(&rilPathBuf[..rilPathLen]);
279 280
        return false;
280 281
    };
281 282
282 283
    // Read source file.
283 -
    let sourceLen = unix::readFile(sourcePath, &mut storage.source[..]) else {
284 +
    let sourceLen = try fs::readFile(sourcePath, &mut storage.source[..]) catch {
284 285
        io::print("error: could not read source: ");
285 286
        io::printLn(sourcePath);
286 287
        return false;
287 288
    };
288 289
    let expected = &storage.expected[..expectedLen];
356 357
357 358
/// Run a single test specified as an argument.
358 359
@default fn main(env: *sys::Env, storage: *mut Storage) -> i32 {
359 360
    let args = env.args;
360 361
361 -
    if args.len == 4 and mem::eq(args[1], "assemble") {
362 -
        if assembleBinary(args[2], args[3], storage) {
362 +
    if args.len == 3 and mem::eq(args[0], "assemble") {
363 +
        if assembleBinary(args[1], args[2], storage) {
363 364
            return 0;
364 365
        } else {
365 366
            return 1;
366 367
        }
367 368
    }
368 -
    if args.len <> 2 {
369 +
    if args.len <> 1 {
369 370
        io::printError("error: expected test file path as argument");
370 371
        return 1;
371 372
    }
372 -
    let sourcePath = args[1];
373 +
    let sourcePath = args[0];
373 374
    io::print("test ");
374 375
    io::print(sourcePath);
375 376
    io::print(" ... ");
376 377
377 378
    if runTest(sourcePath, storage) {
test/runtime/run deleted +0 -24
1 -
#!/bin/sh
2 -
# Compile, cancel, and retry a package through scheduled user calls.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
work=$(mktemp -d)
6 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 -
scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" \
8 -
    -pkg spin -mod test/runtime/spin.rad \
9 -
    -mod cases=test/runtime/cases.rad \
10 -
    -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad -entry spin -ril "$work"
11 -
cp test/loader/loaded.rad "$work/loaded.rad"
12 -
index=0
13 -
while [ "$index" -lt 128 ]; do
14 -
    printf '\n/// Compute a distinct exported value.\nexport fn value%s(input: u64) -> u64 { return input + %s; }\n' "$index" "$index" >> "$work/loaded.rad"
15 -
    index=$((index + 1))
16 -
done
17 -
scripts/hosted bin/radiance.rv64.dev \
18 -
    -pkg loaded -mod "$work/loaded.rad" -entry loaded -ril "$work"
19 -
printf '//! Binary inputs for scheduled loading.\n' > "$work/inputs.rad"
20 -
sh test/kernel/embed INPUT "$work/spin.ril" >> "$work/inputs.rad"
21 -
sh test/kernel/embed PAYLOAD "$work/loaded.ril" >> "$work/inputs.rad"
22 -
sh test/kernel/build runtime "$work" "$work/dispatch.rv64"
23 -
"$emulator" -machine -harts=1 -memory-size=262144 -max-steps=2000000000 -run "$work/dispatch.rv64"
24 -
printf 'runtime: scheduled cancellation, retry, and publication passed\n'
test/scheduling/run deleted +0 -22
1 -
#!/bin/sh
2 -
# Execute root handoff, user-space yields, and window expiry in fixture packages.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
harts=${1:-1 2 8}
6 -
work=$(mktemp -d)
7 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
8 -
for package in root scheduler; do
9 -
    set -- -pkg "$package" -mod "test/scheduling/$package.rad" \
10 -
        -mod abi=kernel/kernel/abi.rad -mod sys=kernel/kernel/sys.rad
11 -
    if [ "$package" = scheduler ]; then
12 -
        set -- "$@" -mod mmio=kernel/mmio.rad
13 -
    fi
14 -
    scripts/hosted "${RAD_BIN:-bin/radiance.rv64.dev}" "$@" -entry "$package" -ril "$work"
15 -
done
16 -
cat kernel/kernel/*.ras > "$work/scheduling.ras"
17 -
scripts/hosted bin/kernel.build.rv64 -- bin/std.ril bin/kernel.ril \
18 -
    "$work/scheduling.ras" "$work/scheduling.rv64" "$work/root.ril" "$work/scheduler.ril"
19 -
for count in $harts; do
20 -
    "$emulator" -machine -harts="$count" -memory-size=262144 -max-steps=2000000000 -run "$work/scheduling.rv64"
21 -
    printf 'scheduling: %s-hart root handoff, four user-space yields, 513 retired windows, and checked shutdown passed\n' "$count"
22 -
done
test/shared/build.rad +14 -16
1 1
//! Build a machine fixture from separately decoded and linked packages.
2 2
3 3
use std::sys;
4 -
use std::sys::unix;
4 +
use std::fs;
5 5
use std::mem;
6 6
use std::lang::alloc;
7 7
use std::lang::il::binary;
8 8
use std::lang::il::binary::program;
9 9
use std::lang::gen::data;
87 87
/// Two private graphs followed by their package-state tables.
88 88
static STATE: [u8; STATE_CAPACITY] = [0; STATE_CAPACITY];
89 89
90 90
/// Decode trusted package IL into persistent storage.
91 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";
92 +
    let length = try fs::readFile(path, &mut INPUT[..]) catch {
93 +
        panic "load: missing package";
94 +
    };
93 95
    return try! program::decode(&INPUT[..length], arena,
94 96
        binary::Limits { registers: liveness::MAX_SSA_REGS,
95 97
        blocks: labels::MAX_BLOCKS_PER_FN });
96 98
}
97 99
129 131
    try! emit::emit(e, encode::ebreak());
130 132
}
131 133
132 134
/// Verify catalog linking, then execute one code copy against two private graphs.
133 135
@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;
136 +
    // A fourth argument selects the typed-catch scenario, whose entry returns 222.
137 +
    assert env.args.len == 3 or env.args.len == 4;
138 +
    let catchScenario = env.args.len == 4;
137 139
    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 supportInput = load(env.args[0], &mut decoder);
141 +
    let appInput = load(env.args[1], &mut decoder);
140 142
    let support = try! compile(&supportInput, 0, &[]);
141 143
    let mut far: [shared::Symbol; EXPORTS_CAPACITY] = undefined;
142 144
    for symbol, i in support.exports {
143 145
        set far[i] = symbol;
144 146
        match symbol.target {
218 220
        code: image::Segment { address: CODE_BASE, size: TEXT_CAPACITY * rv64::INSTR_SIZE as u32 },
219 221
        roData: image::Segment { address: 0, size: 0 },
220 222
        rwData: image::Segment { address: RW_BASE, size: STATE.len },
221 223
        rwTail: 0,
222 224
    });
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;
225 +
    let mut out = try! fs::create(env.args[2]);
226 +
    out.write(&header[..]);
227 +
    out.write(@sliceOf(&TEXT[0] as *u8, TEXT_CAPACITY * rv64::INSTR_SIZE as u32));
228 +
    out.write(&STATE[..]);
229 +
    try! out.close();
232 230
    return 0;
233 231
}
test/shared/errors/run deleted +0 -19
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/run deleted +0 -12
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/slots/run deleted +0 -23
1 -
#!/bin/sh
2 -
# Check that reservation misuse fails during compilation.
3 -
set -eu
4 -
work=$(mktemp -d)
5 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
6 -
for name in duplicate abandon; do
7 -
    case "$name" in
8 -
        duplicate) diagnostic='linear value used after consumption' ;;
9 -
        abandon) diagnostic='linear value is not consumed' ;;
10 -
    esac
11 -
    status=0
12 -
    scripts/hosted bin/radiance.rv64.dev \
13 -
        -pkg kernel -mod test/slots/kernel.rad \
14 -
        -mod slots=kernel/kernel/slots.rad -mod abi=kernel/kernel/abi.rad \
15 -
        -mod limits=kernel/kernel/limits.rad \
16 -
        -pkg "$name" -mod "test/slots/$name.rad" -entry "$name" \
17 -
        -o "$work/test.rv64" > "$work/log" 2>&1 || status=$?
18 -
    if [ "$status" -ne 1 ] || ! grep -q "$diagnostic" "$work/log"; then
19 -
        cat "$work/log" >&2
20 -
        exit 1
21 -
    fi
22 -
    printf 'reservation rejection: %s passed\n' "$name"
23 -
done
test/sync/build.rad +11 -13
1 1
//! Build a native image from the synchronization machine fixture.
2 2
3 3
use std::sys;
4 4
use std::io;
5 -
use std::sys::unix;
5 +
use std::fs;
6 6
use std::lang::alloc;
7 7
use std::lang::strings;
8 8
use std::arch::rv64::asm;
9 9
use std::arch::rv64::image;
10 10
use std::arch::rv64;
32 32
/// Slots of the interned assembly identifiers.
33 33
unsafe static TABLE: [*[u8]; strings::TABLE_SIZE] = undefined;
34 34
35 35
/// Assemble startup and synchronization routines into one native code segment.
36 36
@default unsafe fn main(env: *sys::Env) -> i32 {
37 -
    assert env.args.len == 3;
38 -
    let length = unix::readFile(env.args[1], &mut SOURCE[..]) else panic "main: missing assembly";
37 +
    assert env.args.len == 2;
38 +
    let length = try fs::readFile(env.args[0], &mut SOURCE[..]) catch {
39 +
        panic "main: missing assembly";
40 +
    };
39 41
    let mut arena = alloc::new(&mut MEMORY[..]);
40 42
    let mut pool = strings::pool(&mut TABLE[..]);
41 43
    let data: *mut [u8] = &mut [];
42 44
    let program = try asm::assemble(
43 -
        asm::scanner::SourceKind::File { path: env.args[1] }, &SOURCE[..length],
45 +
        asm::scanner::SourceKind::File { path: env.args[0] }, &SOURCE[..length],
44 46
        &mut TEXT[..], data, &mut arena, &mut pool, 0) catch err {
45 -
        asm::printError(err, &env.args[1][..], &SOURCE[..length]);
47 +
        asm::printError(err, &env.args[0][..], &SOURCE[..length]);
46 48
        return 1;
47 49
    };
48 50
    assert program.externalFixups.len == 0;
49 51
    let size = program.text.len * rv64::INSTR_SIZE as u32;
50 52
    let header = try! image::header(image::Layout {
51 53
        code: image::Segment { address: CODE_BASE, size },
52 54
        roData: image::Segment { address: 0, size: 0 },
53 55
        rwData: image::Segment { address: RW_BASE, size: 0 },
54 56
        rwTail: RW_SIZE,
55 57
    });
56 -
    let fd = unix::openOpts(env.args[2],
57 -
        unix::OpenFlags(*unix::O_WRONLY | *unix::O_CREAT | *unix::O_TRUNC),
58 -
            unix::FILE_MODE);
59 -
    assert fd >= 0;
60 -
    let written = unix::writeAll(fd, &header[..]) and unix::writeAll(fd,
61 -
        @sliceOf(program.text.ptr as *u8, size));
62 -
    let closed = unix::close(fd) == 0;
63 -
    assert written and closed;
58 +
    let mut out = try! fs::create(env.args[1]);
59 +
    out.write(&header[..]);
60 +
    out.write(@sliceOf(program.text.ptr as *u8, size));
61 +
    try! out.close();
64 62
    return 0;
65 63
}
test/sync/run deleted +0 -13
1 -
#!/bin/sh
2 -
# Execute the synchronization boundary under deterministic hart interleaving.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
work=$(mktemp -d)
6 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 -
for harts in 1 2 8; do
8 -
    printf '.constant HARTS %s;\n' "$harts" > "$work/sync.ras"
9 -
    cat test/sync/machine.ras kernel/kernel/sync.ras >> "$work/sync.ras"
10 -
    "$emulator" -run test/sync/build.rv64 -- "$work/sync.ras" "$work/sync.rv64"
11 -
    "$emulator" -machine -harts="$harts" -run "$work/sync.rv64"
12 -
done
13 -
printf 'synchronization: code publication, locks, atomics, and MMIO passed on 1/2/8 harts\n'
test/tests/asm.arithmetic.spacing.ras +1 -1
25 25
    li %a0 0;
26 26
    j @exit;
27 27
@fail
28 28
    li %a0 1;
29 29
@exit
30 -
    li %a7 93;
30 +
    li %a7 1024;
31 31
    ecall;
test/tests/asm.basic.text.program.ras +2 -2
3 3
.text;
4 4
@entry
5 5
	addi   %a0    %zero  42;
6 6
	sd     %a0    8(%sp);
7 7
	beq    %a0    %zero  @fail;
8 -
	li     %a7    93;
8 +
	li     %a7    1024;
9 9
	ecall;
10 10
@fail
11 11
	li     %a0    1;
12 -
	li     %a7    93;
12 +
	li     %a7    1024;
13 13
	ecall;
test/tests/asm.bleu.ras +1 -1
13 13
    li %a0 0;
14 14
    j @exit;
15 15
@fail
16 16
    li %a0 1;
17 17
@exit
18 -
    li %a7 93;
18 +
    li %a7 1024;
19 19
    ecall;
test/tests/asm.branch.comparisons.ras +2 -2
30 30
@bltuOk
31 31
	bgeu   %t5    %t4    @done;
32 32
	j      @fail;
33 33
@done
34 34
	li     %a0    17;
35 -
	li     %a7    93;
35 +
	li     %a7    1024;
36 36
	ecall;
37 37
@fail
38 38
	li     %a0    1;
39 -
	li     %a7    93;
39 +
	li     %a7    1024;
40 40
	ecall;
test/tests/asm.call.return.flow.ras +1 -1
11 11
	ret;
12 12
@leaf
13 13
	li     %a0    29;
14 14
	ret;
15 15
@exit
16 -
	li     %a7    93;
16 +
	li     %a7    1024;
17 17
	ecall;
test/tests/asm.compare.set.logic.ras +1 -1
23 23
	ori    %a2    %a2    8;
24 24
	andi   %a2    %a2    15;
25 25
	add    %a0    %a0    %a2;
26 26
	xor    %a3    %a2    %t6;
27 27
	add    %a0    %a0    %a3;
28 -
	li     %a7    93;
28 +
	li     %a7    1024;
29 29
	ecall;
test/tests/asm.csr.system.instructions.ras +1 -1
10 10
	csrc   mip   %t0;
11 11
	wfi;
12 12
	mret;
13 13
@exit
14 14
	li     %a0    88;
15 -
	li     %a7    93;
15 +
	li     %a7    1024;
16 16
	ecall;
test/tests/asm.data.directives.ras +2 -2
1 -
//! returns: 139
1 +
//! returns: 395
2 2
3 3
.constant dataBase 0x10000;
4 4
.data;
5 5
.byte 1, 'A';
6 6
.word 0x11223344;
18 18
	add    %a0    %a0    %t1;
19 19
	lbu    %t1    15(%t0);
20 20
	add    %a0    %a0    %t1;
21 21
	lbu    %t1    16(%t0);
22 22
	add    %a0    %a0    %t1;
23 -
	li     %a7    93;
23 +
	li     %a7    1024;
24 24
	ecall;
test/tests/asm.data.symbol.fixup.ras +1 -1
10 10
@entry
11 11
	li     %t0    dataBase;
12 12
	lbu    %a0    1(%t0);
13 13
	lbu    %t1    2(%t0);
14 14
	add    %a0    %a0    %t1;
15 -
	li     %a7    93;
15 +
	li     %a7    1024;
16 16
	ecall;
test/tests/asm.directive.boundary.values.ras +2 -2
1 -
//! returns: 97
1 +
//! returns: 1377
2 2
3 3
.constant dataBase 0x10000;
4 4
.data;
5 5
.word -2147483648;
6 6
.word 2147483647;
34 34
	add    %a0    %a0    %t1;
35 35
	lbu    %t1    15(%t0);
36 36
	add    %a0    %a0    %t1;
37 37
	lbu    %t1    16(%t0);
38 38
	add    %a0    %a0    %t1;
39 -
	li     %a7    93;
39 +
	li     %a7    1024;
40 40
	ecall;
test/tests/asm.divrem.edges.ras +1 -1
93 93
    li %a0 0;
94 94
    j @exit;
95 95
@fail
96 96
    li %a0 1;
97 97
@exit
98 -
    li %a7 93;
98 +
    li %a7 1024;
99 99
    ecall;
test/tests/asm.fence.ras +1 -1
1 1
//! returns: 0
2 2
3 3
.text;
4 4
	fence;
5 5
	li     %a0    0;
6 -
	li     %a7    93;
6 +
	li     %a7    1024;
7 7
	ecall;
test/tests/asm.global.scoped.symbols.ras +2 -2
3 3
.export @kernel::main;
4 4
.text;
5 5
@entry
6 6
	j      @kernel::main;
7 7
	li     %a0    1;
8 -
	li     %a7    93;
8 +
	li     %a7    1024;
9 9
	ecall;
10 10
@kernel::main
11 11
	li     %a0    7;
12 -
	li     %a7    93;
12 +
	li     %a7    1024;
13 13
	ecall;
14 14
.data;
15 15
@data::sym
16 16
.byte 1;
test/tests/asm.instruction.matrix.alu.ras +1 -1
23 23
	seqz   %t5    %t4;
24 24
	add    %a0    %a0    %t5;
25 25
	li     %t6    9;
26 26
	snez   %a6    %t6;
27 27
	add    %a0    %a0    %a6;
28 -
	li     %a7    93;
28 +
	li     %a7    1024;
29 29
	ecall;
test/tests/asm.instruction.matrix.mem.control.ras +1 -1
15 15
@helper
16 16
	ld     %a0    8(%sp);
17 17
	addi   %a0    %a0    5;
18 18
	addi   %sp    %sp    32;
19 19
@exit
20 -
	li     %a7    93;
20 +
	li     %a7    1024;
21 21
	ecall;
test/tests/asm.instruction.matrix.system.ras +1 -1
5 5
	j      @exit;
6 6
	ecall;
7 7
	ebreak;
8 8
@exit
9 9
	li     %a0    77;
10 -
	li     %a7    93;
10 +
	li     %a7    1024;
11 11
	ecall;
test/tests/asm.label.fixups.ras +1 -1
10 10
	addi   %a0    %a0    -1;
11 11
	bnez   %a0    @loop;
12 12
	j      @done;
13 13
@done
14 14
	mv     %a0    %a1;
15 -
	li     %a7    93;
15 +
	li     %a7    1024;
16 16
	ecall;
test/tests/asm.li.expressions.ras +1 -1
6 6
@entry
7 7
	li     %a0    42;
8 8
	li     %a1    VALUE;
9 9
	andi   %a1    %a1    255;
10 10
	add    %a0    %a0    %a1;
11 -
	li     %a7    93;
11 +
	li     %a7    1024;
12 12
	ecall;
test/tests/asm.li.wide.ras +1 -1
25 25
    li %a0 0;
26 26
    j @exit;
27 27
@fail
28 28
    li %a0 1;
29 29
@exit
30 -
    li %a7 93;
30 +
    li %a7 1024;
31 31
    ecall;
test/tests/asm.load.store.widths.ras +1 -1
27 27
	li     %t0    34;
28 28
	sd     %t0    16(%sp);
29 29
	ld     %a1    16(%sp);
30 30
	add    %a0    %a0    %a1;
31 31
	addi   %sp    %sp    32;
32 -
	li     %a7    93;
32 +
	li     %a7    1024;
33 33
	ecall;
test/tests/asm.mul.div.rem.ras +2 -2
1 -
//! returns: 4
1 +
//! returns: 260
2 2
3 3
.text;
4 4
@entry
5 5
	li     %t0    20;
6 6
	li     %t1    6;
27 27
	add    %a0    %a0    %a5;
28 28
	mulhu  %a6    %t0    %t1;
29 29
	add    %a0    %a0    %a6;
30 30
	mulhsu %a7    %t0    %t1;
31 31
	add    %a0    %a0    %a7;
32 -
	li     %a7    93;
32 +
	li     %a7    1024;
33 33
	ecall;
test/tests/asm.mulh.ras +1 -1
80 80
    li %a0 0;
81 81
    j @exit;
82 82
@fail
83 83
    li %a0 1;
84 84
@exit
85 -
    li %a7 93;
85 +
    li %a7 1024;
86 86
    ecall;
test/tests/asm.scoped.symbols.la.ras +2 -2
1 -
//! returns: 41
1 +
//! returns: 65577
2 2
3 3
.text;
4 4
@entry
5 5
	la     %t0    @data::sym;
6 6
	addi   %a0    %t0    40;
7 -
	li     %a7    93;
7 +
	li     %a7    1024;
8 8
	ecall;
9 9
.data;
10 10
.byte 0;
11 11
@data::sym
12 12
.byte 1;
test/tests/asm.scoped.symbols.tail.ras +2 -2
2 2
3 3
.text;
4 4
@entry
5 5
	tail   @kernel::main;
6 6
	li     %a0    1;
7 -
	li     %a7    93;
7 +
	li     %a7    1024;
8 8
	ecall;
9 9
@kernel::main
10 10
	li     %a0    9;
11 -
	li     %a7    93;
11 +
	li     %a7    1024;
12 12
	ecall;
test/tests/asm.section.switching.ras +2 -2
5 5
.ascii "ok";
6 6
.text;
7 7
@entry
8 8
	jal    %ra    @done;
9 9
	li     %a0    1;
10 -
	li     %a7    93;
10 +
	li     %a7    1024;
11 11
	ecall;
12 12
@done
13 13
	li     %a0    3;
14 -
	li     %a7    93;
14 +
	li     %a7    1024;
15 15
	ecall;
test/tests/asm.space.constant.expressions.ras +1 -1
14 14
@entry
15 15
	li     %t0    dataBase;
16 16
	lbu    %a0    4(%t0);
17 17
	lbu    %t1    8(%t0);
18 18
	add    %a0    %a0    %t1;
19 -
	li     %a7    93;
19 +
	li     %a7    1024;
20 20
	ecall;
test/tests/asm.string.directive.lists.ras +2 -2
1 -
//! returns: 180
1 +
//! returns: 436
2 2
3 3
.constant dataBase 0x10000;
4 4
.data;
5 5
.ascii "a", "b";
6 6
.ascii "x", "y";
12 12
	add    %a0    %a0    %t1;
13 13
	lbu    %t1    2(%t0);
14 14
	add    %a0    %a0    %t1;
15 15
	lbu    %t1    3(%t0);
16 16
	add    %a0    %a0    %t1;
17 -
	li     %a7    93;
17 +
	li     %a7    1024;
18 18
	ecall;
test/tests/asm.word.dword.constants.ras +2 -2
1 -
//! returns: 50
1 +
//! returns: 306
2 2
3 3
.constant WORD 0x11223344;
4 4
.constant DWORD 0x55667788;
5 5
.constant dataBase 0x10000;
6 6
.data;
14 14
	add    %a0    %a0    %t1;
15 15
	lbu    %t1    4(%t0);
16 16
	add    %a0    %a0    %t1;
17 17
	lbu    %t1    7(%t0);
18 18
	add    %a0    %a0    %t1;
19 -
	li     %a7    93;
19 +
	li     %a7    1024;
20 20
	ecall;
test/tests/asm.word.shift.ops.ras +1 -1
32 32
	slti   %a2    %a2    0;
33 33
	add    %a0    %a0    %a2;
34 34
	add    %a0    %a0    %t1;
35 35
	add    %a0    %a0    %t2;
36 36
	add    %a0    %a0    %t3;
37 -
	li     %a7    93;
37 +
	li     %a7    1024;
38 38
	ecall;
test/tests/ecall.i64.rad +2 -2
2 2
3 3
@intrinsic fn ecall(number: u32, arg1: i64, arg2: i64, arg3: i64, arg4: i64) -> i64;
4 4
5 5
/// Test that ecall can pass and return i64 values.
6 6
@default fn main() -> i32 {
7 -
    // ecall(64, fd, buf, len, 0) = write(fd, buf, len).
7 +
    // ecall(0x402, fd, buf, len, 0) = write(fd, buf, len).
8 8
    let msg: *[u8] = "ok\n";
9 9
10 10
    // Write to stdout. The pointer is passed as i64.
11 11
    unsafe {
12 -
        let n = ecall(64, 1, msg.ptr as i64, msg.len as i64, 0);
12 +
        let n = ecall(0x402, 1, msg.ptr as i64, msg.len as i64, 0);
13 13
14 14
        // Return value should be 3 (bytes written).
15 15
        if n <> 3 {
16 16
            return 1;
17 17
        }
test/tests/intrinsic.ecall.buffer.rad +1 -1
18 18
    return info.base;
19 19
}
20 20
21 21
/// A call without a buffer.
22 22
fn plain() -> i64 {
23 -
    return ecall(64, 1, 100, 5, 0);
23 +
    return ecall(0x402, 1, 100, 5, 0);
24 24
}
25 25
26 26
/// Access a page for reading.
27 27
fn read(handle: u64) -> *[u8] {
28 28
    let mut bytes: *[u8] = &[];
test/tests/intrinsic.ecall.buffer.ril +1 -1
17 17
    ret %3;
18 18
}
19 19
20 20
fn w64 $plain() {
21 21
  @entry0
22 -
    ecall %0 64 1 100 5 0;
22 +
    ecall %0 1026 1 100 5 0;
23 23
    ret %0;
24 24
}
25 25
26 26
fn void $read(ptr ref mut $L1 %0, w64 %1) {
27 27
  @entry0
test/tests/intrinsic.ecall.rad +1 -1
1 1
//! Test intrinsic ecall lowering.
2 2
3 3
@intrinsic fn ecall(num: u32, a0: i32, a1: i32, a2: i32, a3: i32) -> i32;
4 4
5 5
fn test() -> i32 {
6 -
    return ecall(64, 1, 100, 5, 0);
6 +
    return ecall(0x402, 1, 100, 5, 0);
7 7
}
test/tests/intrinsic.ecall.ril +1 -1
1 1
fn w32 $test() {
2 2
  @entry0
3 -
    ecall %0 64 1 100 5 0;
3 +
    ecall %0 1026 1 100 5 0;
4 4
    ret %0;
5 5
}
test/tests/return.never.ras +1 -1
1 1
.text;
2 2
// Terminate the hosted process with the code in a0.
3 3
.export @"test::exit";
4 4
@"test::exit"
5 -
    li %a7 93;
5 +
    li %a7 1024;
6 6
    ecall;
test/tests/start.exit.start.ras +1 -1
1 1
.text;
2 2
	li     %a0    37;
3 -
	li     %a7    93;
3 +
	li     %a7    1024;
4 4
	ecall;
test/trap/run deleted +0 -13
1 -
#!/bin/sh
2 -
# Execute the production trap boundary with a deterministic nested timer.
3 -
set -eu
4 -
emulator=${RAD_EMULATOR:-emulator}
5 -
work=$(mktemp -d)
6 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
7 -
for fixture in machine reservations; do
8 -
    cat "test/trap/$fixture.ras" kernel/kernel/trap.ras > "$work/trap.ras"
9 -
    "$emulator" -run test/sync/build.rv64 -- "$work/trap.ras" "$work/trap.rv64"
10 -
    "$emulator" -machine -max-steps=1000000 -run "$work/trap.rv64"
11 -
done
12 -
printf 'trap contexts: machine/user registers, nested timer, illegal instruction, and stacks passed\n'
13 -
printf 'trap reservations: entry, resume, and user context isolation passed\n'
test/verify/check.rad +5 -5
4 4
//! the path, then `ok`, or the violated rule and its location.
5 5
6 6
use std::io;
7 7
use std::mem;
8 8
use std::sys;
9 -
use std::sys::unix;
9 +
use std::fs;
10 10
use std::lang::alloc;
11 11
use std::lang::il;
12 12
use std::lang::il::binary;
13 13
use std::lang::il::binary::program;
14 14
use std::lang::il::verify;
82 82
/// Verify the package in the file at `path`, as a loadable package when `loadable` is set,
83 83
/// and print the result.
84 84
unsafe fn check(path: *[u8], loadable: bool) {
85 85
    io::print(path);
86 86
    io::print(": ");
87 -
    let length = unix::readFile(path, &mut IMAGE[..]) else {
87 +
    let length = try fs::readFile(path, &mut IMAGE[..]) catch {
88 88
        io::print("unreadable\n");
89 89
        return;
90 90
    };
91 91
    let mut arena = alloc::new(&mut DECODED[..]);
92 92
    let mut package = try program::decode(&IMAGE[..length], &mut arena,
106 106
    io::print("ok\n");
107 107
}
108 108
109 109
/// Verify each package named on the command line.
110 110
@default unsafe fn main(env: *sys::Env) -> i32 {
111 -
    let mut first: u32 = 1;
111 +
    let mut first: u32 = 0;
112 112
    let mut loadable = false;
113 -
    if env.args.len > 1 and mem::eq(env.args[1], "-package") {
113 +
    if env.args.len > 0 and mem::eq(env.args[0], "-package") {
114 114
        set loadable = true;
115 -
        set first = 2;
115 +
        set first = 1;
116 116
    }
117 117
    for i in first..env.args.len {
118 118
        check(env.args[i], loadable);
119 119
    }
120 120
    return 0;
test/verify/run deleted +0 -59
1 -
#!/bin/sh
2 -
# Verify source-only binary RIL fixtures against test/verify/expected.
3 -
# Usage: run [checker]
4 -
# Compile errors, crashes, timeouts, and missing output fail the run.
5 -
set -eu
6 -
7 -
hosted=$(cd "$(dirname "$0")/../.." && pwd)/scripts/hosted
8 -
9 -
# Compare one checker report with the complete package list and expected rules.
10 -
checkResults() {
11 -
    work=$1
12 -
    checker=$2
13 -
    set -- "$work"/*/test.ril
14 -
    printf '%s\n' "$@" | sort > "$work/packages"
15 -
    "$hosted" "$checker" -- "$@" > "$work/checks"
16 -
    sed 's/: .*//' "$work/checks" | sort > "$work/reported"
17 -
    if ! diff -u "$work/packages" "$work/reported"; then
18 -
        printf 'verify: checker must report each package exactly once\n' >&2
19 -
        exit 1
20 -
    fi
21 -
    sed 's#^.*/\([^/]*\)/test.ril: \([A-Za-z]*\).*$#\1 \2#' \
22 -
        "$work/checks" > "$work/outcome"
23 -
    grep -v ' ok$' "$work/outcome" | sort > "$work/rejected"
24 -
    sort test/verify/expected > "$work/expected"
25 -
    printf 'verify: %s fixtures, %s accepted, %s rejected\n' \
26 -
        "$(wc -l < "$work/outcome")" "$(grep -c ' ok$' "$work/outcome")" \
27 -
        "$(wc -l < "$work/rejected")"
28 -
    if ! diff -u "$work/expected" "$work/rejected"; then
29 -
        printf 'verify: outcome differs from test/verify/expected\n' >&2
30 -
        exit 1
31 -
    fi
32 -
}
33 -
34 -
# Report-only mode lets failure tests use packages without compiling sources.
35 -
if [ "${1:-}" = --check ]; then
36 -
    checkResults "$2" "$3"
37 -
    exit 0
38 -
fi
39 -
40 -
work=$(mktemp -d)
41 -
trap 'rm -rf "$work"' EXIT HUP INT TERM
42 -
43 -
# Native assembly inputs cannot be emitted as binary RIL.
44 -
for source in test/tests/*.rad; do
45 -
    base=${source%.rad}
46 -
    if [ ! -f "$base.ras" ] && [ ! -f "$base.start.ras" ]; then
47 -
        printf '%s\n' "$source"
48 -
    fi
49 -
done > "$work/fixtures"
50 -
if ! xargs -P "${JOBS:-1}" -I{} sh -c '
51 -
    source=$1
52 -
    name=${source##*/}
53 -
    sh test/compile "$source" "$2/${name%.rad}"
54 -
' sh {} "$work" < "$work/fixtures" > "$work/compile.log" 2>&1; then
55 -
    cat "$work/compile.log" >&2
56 -
    exit 1
57 -
fi
58 -
59 -
checkResults "$work" "${1:-test/verify/check.rv64}"