.githooks/
compiler/
kernel/
lib/
scripts/
seed/
sublime/
test/
boot/
bootstrap/
compiler/
cycles/
dispatch/
format/
host/
child.rad
4.1 KiB
integration/
kernel/
loader/
manifests/
mmio/
native/
packages/
runtime/
scheduling/
shared/
slots/
smp/
std/
sync/
termination/
tests/
trap/
verify/
Makefile
9.2 KiB
kernel.rad
776 B
runner.rad
11.6 KiB
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
test/host/child.rad
raw
| 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 | //! imageDigest Write the loaded image digest to standard output. |
| 11 | //! stdin Exit with the number of bytes read from standard |
| 12 | //! input. |
| 13 | //! spin Loop forever. |
| 14 | //! fault Access memory outside the guest. |
| 15 | //! reach <kb> Ask the host to read standard input into a buffer |
| 16 | //! that extends the given number of KB past the start of |
| 17 | //! the data segment. The host faults the guest when the |
| 18 | //! buffer leaves guest memory, so the outcome tells how |
| 19 | //! much memory the child has. |
| 20 | |
| 21 | use std::fmt; |
| 22 | use std::intrinsics; |
| 23 | use std::mem; |
| 24 | use std::sys; |
| 25 | use std::sys::host; |
| 26 | |
| 27 | /// Bytes in one KB, the unit of the `reach` size. |
| 28 | constant KB: u32 = 1024; |
| 29 | /// Exit status of a child that was started with unknown arguments. |
| 30 | constant USAGE_STATUS: i32 = 2; |
| 31 | /// First byte of the data segment, which the `reach` buffer starts at. |
| 32 | static ANCHOR: [u8; 1] = [0; 1]; |
| 33 | |
| 34 | /// Write the text to a descriptor. |
| 35 | fn say(fd: i64, text: *[u8]) { |
| 36 | try! host::write(fd, text); |
| 37 | } |
| 38 | |
| 39 | /// Parse a decimal number with an optional leading minus sign. |
| 40 | fn parseStatus(text: *[u8]) -> ?i32 { |
| 41 | let mut digits = text; |
| 42 | let mut negative = false; |
| 43 | if digits.len > 0 and digits[0] == '-' { |
| 44 | set negative = true; |
| 45 | set digits = &digits[1 ..]; |
| 46 | } |
| 47 | let literal = try fmt::parseInt(digits) catch { |
| 48 | return nil; |
| 49 | }; |
| 50 | if literal.magnitude > 0x7fffffff { |
| 51 | return nil; |
| 52 | } |
| 53 | if negative { |
| 54 | return 0 - literal.magnitude as i32; |
| 55 | } |
| 56 | return literal.magnitude as i32; |
| 57 | } |
| 58 | |
| 59 | /// Ask the host to write from an address outside guest memory, which faults |
| 60 | /// the guest. |
| 61 | fn touchOutside() -> i64 { |
| 62 | return intrinsics::ecall(0x402, host::STDOUT, 0x7fffffff00000000, 1, 0); |
| 63 | } |
| 64 | |
| 65 | @default fn main(env: *sys::Env) -> i32 { |
| 66 | let args = env.args; |
| 67 | if args.len == 0 { |
| 68 | return USAGE_STATUS; |
| 69 | } |
| 70 | let mode = args[0]; |
| 71 | if mem::eq(mode, "imageDigest") and args.len == 1 { |
| 72 | try! host::write(host::STDOUT, &env.imageDigest[..]); |
| 73 | return 0; |
| 74 | } |
| 75 | if mem::eq(mode, "exit") and args.len == 2 { |
| 76 | let status = parseStatus(args[1]) else { |
| 77 | return USAGE_STATUS; |
| 78 | }; |
| 79 | return status; |
| 80 | } |
| 81 | if mem::eq(mode, "stdout") and args.len == 2 { |
| 82 | say(host::STDOUT, args[1]); |
| 83 | say(host::STDOUT, "\n"); |
| 84 | return 0; |
| 85 | } |
| 86 | if mem::eq(mode, "stderr") and args.len == 2 { |
| 87 | say(host::STDERR, args[1]); |
| 88 | say(host::STDERR, "\n"); |
| 89 | return 0; |
| 90 | } |
| 91 | if mem::eq(mode, "mixed") { |
| 92 | say(host::STDOUT, "one\n"); |
| 93 | say(host::STDERR, "two\n"); |
| 94 | say(host::STDOUT, "three\n"); |
| 95 | return 0; |
| 96 | } |
| 97 | if mem::eq(mode, "echo") { |
| 98 | for arg in &args[1 ..] { |
| 99 | say(host::STDOUT, "<"); |
| 100 | say(host::STDOUT, arg); |
| 101 | say(host::STDOUT, ">\n"); |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | if mem::eq(mode, "stdin") { |
| 106 | let mut probe: [u8; 64] = [0; 64]; |
| 107 | let n = try! host::read(host::STDIN, &mut probe[..]); |
| 108 | return n as i32; |
| 109 | } |
| 110 | if mem::eq(mode, "spin") { |
| 111 | while true {} |
| 112 | } |
| 113 | if mem::eq(mode, "fault") { |
| 114 | return touchOutside() as i32; |
| 115 | } |
| 116 | if mem::eq(mode, "reach") and args.len == 2 { |
| 117 | let kb = parseStatus(args[1]) else { |
| 118 | return USAGE_STATUS; |
| 119 | }; |
| 120 | unsafe { |
| 121 | let buf = @sliceOf(&mut ANCHOR[0] as *mut u8, kb as u32 * KB); |
| 122 | try! host::read(host::STDIN, buf); |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | return USAGE_STATUS; |
| 127 | } |