test/integration/host.rad 9.8 KiB raw
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::hash::blake3;
6
use std::process;
7
use std::sys;
8
use super::harness;
9
use super::harness::Harness;
10
use super::harness::Expect;
11
use std::process::Outcome;
12
use std::process::Output;
13
use std::process::Target;
14
use std::process::Command;
15
16
/// The child program of the tests.
17
constant CHILD: *[u8] = "test/host/child.rv64";
18
/// Budget of a child that loops forever and must end at the step limit, in
19
/// instructions. It is small, so the child ends quickly.
20
constant SPIN_STEPS: u64 = 100000;
21
22
/// Build a command that runs the child with the arguments and discards its
23
/// output.
24
fn child(h: &mut Harness, args: &[*[u8]]) -> Command {
25
    return h.cmd(CHILD, args, harness::HOSTED, harness::RUN_STEPS, Output::Discard);
26
}
27
28
/// Check that a call failed with the `expected` error.
29
fn expectError(h: &mut Harness, what: *[u8], actual: ?sys::Error, expected: sys::Error) {
30
    let name = harness::errorName(expected);
31
    let error = actual else {
32
        h.fail(what, h.join(&["expected ", name, ", but the call succeeded"]));
33
        return;
34
    };
35
    if error == expected {
36
        h.pass();
37
    } else {
38
        h.fail(what, h.join(&["expected ", name, ", got ", harness::errorName(error)]));
39
    }
40
}
41
42
/// Exit statuses come back unchanged, a loop ends at its step limit, an access
43
/// outside memory faults, and the sizes of a hosted child are inherited, given
44
/// or the defaults.
45
fn outcomes(h: &mut Harness) {
46
    let statuses: [i32; 5] = [0, 1, 255, 300, -1];
47
    for label, i in &["0", "1", "255", "300", "-1"] {
48
        let command = child(h, &["exit", label]);
49
        h.run(h.join(&["exit status ", label]), command, Outcome::Exited(statuses[i]), &[]);
50
    }
51
    let mut spin = child(h, &["spin"]);
52
    set spin.maxSteps = SPIN_STEPS;
53
    h.run("spin ends at the step limit", spin, Outcome::StepLimit, &[]);
54
    h.run("access outside memory faults", child(h, &["fault"]), Outcome::Fault, &[]);
55
    h.run("standard input is empty", child(h, &["stdin"]), harness::OK, &[]);
56
    let parent = process::Memory { memory: 385024, data: 348416, stack: 1024 };
57
    let small = process::Memory { memory: 65536, data: 0, stack: 0 };
58
    let sizes: [Target; 5] = [
59
        harness::HOSTED,
60
        Target::Inherit,
61
        Target::Hosted(parent),
62
        Target::Hosted(small),
63
        Target::Hosted(small),
64
    ];
65
    let kb: [*[u8]; 5] = ["200000", "200000", "200000", "20000", "100000"];
66
    let labels: [*[u8]; 5] = [
67
        "default memory is too small",
68
        "inherited memory is the parent's",
69
        "explicit memory is used",
70
        "a buffer inside explicit memory", "a buffer outside explicit memory",
71
    ];
72
    for target, i in &sizes[..] {
73
        let mut command = child(h, &["reach", kb[i]]);
74
        set command.target = target;
75
        let want = Outcome::Fault if i == 0 or i == 4 else harness::OK;
76
        h.run(labels[i], command, want, &[]);
77
    }
78
}
79
80
/// Run the child with its output in the files `out` and `err` of the work
81
/// directory (`nil` drops a stream), and check that it exits with status zero,
82
/// that the file of standard output meets the expectations, and that the file
83
/// of standard error holds `errText` when given.
84
fn streams(
85
    h: &mut Harness,
86
    what: *[u8],
87
    args: &[*[u8]],
88
    out: ?*[u8],
89
    err: ?*[u8],
90
    expects: &[Expect],
91
    errText: ?*[u8]
92
) {
93
    let mut command = child(h, args);
94
    if let name = out {
95
        set command.stdout = h.out(&[name]);
96
    }
97
    if let name = err {
98
        set command.stderr = h.out(&[name]);
99
    }
100
    h.run(what, command, harness::OK, expects);
101
    if let name = err {
102
        if let text = errText {
103
            h.expect(what, h.path(&[name]), Expect::Exact(text));
104
        }
105
    }
106
}
107
108
/// Output goes to files, merges in order into one file, or is dropped, and
109
/// arguments arrive unchanged.
110
fn files(h: &mut Harness) throws (sys::Error) {
111
    let echo: [*[u8]; 6] = ["echo", "", "a b", "c'd\"e$f", "x\ny", "-quoted"];
112
    let merged = [
113
        Expect::Exact("one\ntwo\nthree\n"),
114
        Expect::Matching { text: "t",   count: 2 },
115
        Expect::Matching { text: "e",   count: 2 },
116
        Expect::Matching { text: "one", count: 1 },
117
    ];
118
    let separate = [Expect::Exact("one\nthree\n"), Expect::Same(h.path(&["out.txt"]))];
119
    let hello = [Expect::Exact("hello\n"), Expect::Line("hello")];
120
    let text = "<>\n<a b>\n<c'd\"e$f>\n<x\ny>\n<-quoted>\n";
121
    try fs::writeFile(h.path(&["out.txt"]), "an older and much longer text\n");
122
    streams(h, "stdout to a file", &["stdout", "hello"], "out.txt", nil, &hello[..], nil);
123
    streams(h, "stderr to a file", &["stderr", "oops"], nil, "err.txt", &[], "oops\n");
124
    streams(h, "merged streams", &["mixed"], "both.txt", "both.txt", &merged[..], nil);
125
    streams(h, "separate streams", &["mixed"], "out.txt", "err.txt", &separate[..], "two\n");
126
    streams(
127
        h,
128
        "an output file is truncated",
129
        &["stdout", "new"],
130
        "out.txt",
131
        nil,
132
        &[Expect::Exact("new\n")],
133
        nil
134
    );
135
    streams(h, "discarded streams", &["mixed"], nil, nil, &[], nil);
136
    streams(
137
        h, "arguments arrive unchanged", &echo[..], "echo.txt", nil, &[Expect::Exact(text)], nil
138
    );
139
    streams(
140
        h, "no arguments after the first", &["echo"], "echo.txt", nil, &[Expect::Exact("")], nil
141
    );
142
    h.expect("discarded output creates no file", h.path(&["none.txt"]), Expect::Absent);
143
}
144
145
/// `waitAny` returns each finished child once, and waiting for a child that
146
/// is already finished or for none at all reports `NotFound`.
147
fn waiting(h: &mut Harness) throws (sys::Error) {
148
    let mut handles: [u32; 4] = [0; 4];
149
    let mut seen: [bool; 4] = [false; 4];
150
    for i in 0 .. 4 {
151
        let started = try process::spawn(&child(h, &["exit", h.num(11 + i)]));
152
        set handles[i] = started.handle;
153
    }
154
    for round in 0 .. 4 {
155
        let done = try process::waitAny();
156
        let mut index: u32 = 4;
157
        for handle, i in &handles[..] {
158
            if handle == done.child.handle {
159
                set index = i;
160
            }
161
        }
162
        if index == 4 or seen[index] {
163
            h.fail("waitAny", "returned a child that was not started, or one twice");
164
            continue;
165
        }
166
        set seen[index] = true;
167
        h.outcome("waitAny", done.outcome, Outcome::Exited(11 + index as i32), nil);
168
    }
169
    let mut error: ?sys::Error = nil;
170
    try process::waitAny() catch err {
171
        set error = err;
172
    };
173
    expectError(h, "waitAny with no child left", error, sys::Error::NotFound);
174
    let only = try process::spawn(&child(h, &["exit", "7"]));
175
    h.outcome("wait reports the status", try process::wait(only), Outcome::Exited(7), nil);
176
    set error = nil;
177
    try process::wait(only) catch err {
178
        set error = err;
179
    };
180
    expectError(h, "wait for a child that was waited for", error, sys::Error::NotFound);
181
    set error = nil;
182
    try process::wait(process::Child { handle: only.handle + 1000 }) catch err {
183
        set error = err;
184
    };
185
    expectError(h, "wait for an unknown child", error, sys::Error::NotFound);
186
}
187
188
/// The host runs 64 children at once and refuses the 65th until one is
189
/// waited for.
190
fn limit(h: &mut Harness) throws (sys::Error) {
191
    let mut spin = child(h, &["spin"]);
192
    set spin.maxSteps = SPIN_STEPS;
193
    for i in 0 .. 64 {
194
        try process::spawn(&spin);
195
    }
196
    let mut error: ?sys::Error = nil;
197
    try process::spawn(&spin) catch err {
198
        set error = err;
199
    };
200
    expectError(h, "the 65th child", error, sys::Error::Limit);
201
    for i in 0 .. 64 {
202
        let done = try process::waitAny();
203
        h.outcome("a spinning child ends at its limit", done.outcome, Outcome::StepLimit, nil);
204
    }
205
    h.run(
206
        "a child starts after the others were waited for",
207
        child(h, &["exit", "0"]),
208
        harness::OK,
209
        &[]
210
    );
211
}
212
213
/// The worker pool returns the outcomes in the order of the batch, whatever
214
/// the number of workers.
215
fn pool(h: &mut Harness) {
216
    let saved = h.jobs;
217
    let mut batch: *mut [Command] = &mut [];
218
    for i in 0 .. 8 {
219
        try! batch.append(child(h, &["exit", h.num(20 + i)]), h.arena);
220
    }
221
    let commands = &batch[..];
222
    for workers in &[1, 3, 64] {
223
        set h.jobs = workers;
224
        let outcomes = h.runAll(commands);
225
        let what = h.join(&["pool with ", h.num(workers), " workers"]);
226
        h.outcome(what, Outcome::Exited(outcomes.len as i32), Outcome::Exited(8), nil);
227
        for outcome, i in outcomes {
228
            h.outcome(what, outcome, Outcome::Exited(20 + i as i32), nil);
229
        }
230
    }
231
    set h.jobs = saved;
232
}
233
234
/// Run all tests.
235
export fn check(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
236
    try h.begin("host");
237
    let mark = h.failed;
238
    let image = try h.load(CHILD);
239
    let digest = blake3::digest(image);
240
    let expected = h.path(&["expected-image-digest.bin"]);
241
    try fs::writeFile(expected, &digest.bytes[..]);
242
    streams(
243
        h,
244
        "host supplies the loaded image digest",
245
        &["imageDigest"],
246
        "image-digest.bin",
247
        nil,
248
        &[Expect::Same(expected)],
249
        nil
250
    );
251
    outcomes(h);
252
    try files(h);
253
    for harts in &[0, 1, 2] {
254
        let target = Target::Machine(process::Machine { harts, memory: 0, irq: nil });
255
        let run = h.cmd(
256
            "bin/native.fixture.rv64", &[], target, harness::MACHINE_STEPS, Output::Discard
257
        );
258
        h.run(h.join(&["machine with ", h.num(harts), " harts"]), run, harness::OK, &[]);
259
    }
260
    try waiting(h);
261
    try limit(h);
262
    pool(h);
263
    // The host starts the child before it loads the image, so a missing image
264
    // ends as a fault.
265
    let mut missing = child(h, &[]);
266
    set missing.image = "test/host/missing.rv64";
267
    h.run("a missing image", missing, Outcome::Fault, &[]);
268
    h.done(mark, "host: process outcomes, streams, arguments, sizes, machines and limits passed");
269
}