test/integration/verify.rad 7.2 KiB raw
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::lang::scanner;
8
use std::lang::strings;
9
use std::mem;
10
use std::process;
11
use std::sys;
12
use super::fixtures;
13
use super::harness;
14
use super::harness::Harness;
15
use super::harness::Expect;
16
use std::process::Output;
17
use std::process::Target;
18
use std::process::Command;
19
20
/// Separator between a path and its verdict in the checker's report.
21
constant SEPARATOR: *[u8] = ": ";
22
23
/// Whether `source` has the keyword `unsafe`. A comment or string that mentions it does not count.
24
fn usesUnsafe(source: *[u8], pool: &mut strings::Pool) -> bool {
25
    let mut s = scanner::scanner(scanner::SourceLoc::String, source, pool);
26
    loop {
27
        match scanner::next(&mut s, pool).kind {
28
            case scanner::TokenKind::Unsafe => return true,
29
            case scanner::TokenKind::Eof    => return false,
30
            else                            => {}
31
        }
32
    }
33
}
34
35
/// Check that a package with no `unsafe` passes verification, so that the compiler rejects what the
36
/// verifier would. A fixture that uses std is outside this rule: it needs another package, which
37
/// the verifier refuses. Each item of `rejected` starts with the name of a fixture.
38
fn checkSafeRejections(h: &mut Harness, rejected: &[*[u8]]) throws (sys::Error) {
39
    let mut table: *mut [*[u8]] = &mut [];
40
    try! table.resize(strings::TABLE_SIZE, "", h.arena);
41
    let mut pool = strings::freshPool(table);
42
    for item in rejected {
43
        let end = mem::find(item, " ") else panic "checkSafeRejections: no verdict";
44
        let name = &item[.. end];
45
        // A package fixture's root module is `test.rad` in its directory.
46
        let mut path = h.join(&["test/tests/", name, ".rad"]);
47
        if not try harness::exists(path) {
48
            set path = h.join(&["test/tests/", name, "/test.rad"]);
49
        }
50
        let source = try h.load(path);
51
        if harness::marker(source, "//! uses-std") <> nil or usesUnsafe(source, &mut pool) {
52
            continue;
53
        }
54
        h.fail("verify", h.join(&["safe fixture fails verification: ", item]));
55
    }
56
}
57
58
/// Print the items of `from` that `other` lacks, each after `mark`, on
59
/// standard error. Returns the number of items printed.
60
fn missing(from: *[*[u8]], other: *[*[u8]], mark: *[u8]) -> u32 {
61
    let mut count: u32 = 0;
62
    for item in from {
63
        let mut found = false;
64
        for candidate in other {
65
            if mem::eq(item, candidate) {
66
                set found = true;
67
            }
68
        }
69
        if not found {
70
            io::printError(mark);
71
            io::printError(item);
72
            io::printError("\n");
73
            set count += 1;
74
        }
75
    }
76
    return count;
77
}
78
79
/// Check that two lists of distinct items have the same items. A difference
80
/// fails the check labeled `what`.
81
fn same(h: &mut Harness, what: *[u8], left: *[*[u8]], right: *[*[u8]]) -> bool {
82
    let count = missing(left, right, "-") + missing(right, left, "+");
83
    if count == 0 and left.len == right.len {
84
        return h.pass();
85
    }
86
    return h.fail("verify", what);
87
}
88
89
/// Compile every fixture that has no native assembly and that the compiler
90
/// accepts to binary IL, verify the packages with `checker`, and compare the
91
/// verdicts with the expected ones.
92
export fn check(h: &mut Harness, args: *[*[u8]]) throws (sys::Error) {
93
    try h.begin("verify");
94
    let mut checker: *[u8] = "test/verify/check.rv64";
95
    if args.len == 1 {
96
        set checker = args[0];
97
    }
98
    let mut names: *mut [*[u8]] = &mut [];
99
    let mut batch: *mut [Command] = &mut [];
100
    for source in try fixtures::fixtures(h) {
101
        // A package fixture's name is its directory.
102
        let mut base = harness::stripExt(source);
103
        let directory = harness::directory(source);
104
        if try harness::exists(h.join(&[directory, "/test.manifest"])) {
105
            set base = directory;
106
            // Native assembly inputs cannot be emitted as binary IL.
107
            let assembly = try h.listFiles(directory, ".ras");
108
            if assembly.len > 0 {
109
                continue;
110
            }
111
        }
112
        let name = &base["test/tests/".len ..];
113
        // Rejection fixtures produce no IL.
114
        if harness::marker(try h.load(source), fixtures::REJECTS) <> nil {
115
            continue;
116
        }
117
        try fixtures::prepare(h, source, h.path(&[name]));
118
        try! batch.append(h.cc(h.take(), h.out(&[name, ".log"])), h.arena);
119
        try! names.append(name, h.arena);
120
    }
121
    let outcomes = h.runAll(&batch[..]);
122
    let mut packages: *mut [*[u8]] = &mut [];
123
    for name, i in &names[..] {
124
        let produced = h.path(&[name, "/test/test.ril.bin"]);
125
        if h.outcome(name, outcomes[i], harness::OK, h.path(&[name, ".log"]))
126
            and h.expect(name, produced, Expect::Present)
127
        {
128
            try! packages.append(produced, h.arena);
129
        }
130
    }
131
    if h.failed > 0 {
132
        return;
133
    }
134
    harness::sort(&mut packages[..]);
135
    let list = &packages[..];
136
    let verdicts = h.path(&["checks"]);
137
    let mut command = h.cmd(checker, &[], Target::Inherit, harness::RUN_STEPS, Output::File(
138
        verdicts
139
    ));
140
    set command.args = list;
141
    set command.stderr = Output::Inherit;
142
    if not h.run("verify checker", command, harness::OK, &[]) {
143
        return;
144
    }
145
    // Each package is reported exactly once.
146
    let mut reported: *mut [*[u8]] = &mut [];
147
    let mut accepted: u32 = 0;
148
    let mut rejected: *mut [*[u8]] = &mut [];
149
    let mut lines = mem::split(try h.load(verdicts), '\n');
150
    while let line = lines.next() {
151
        let at = mem::find(line, SEPARATOR) else {
152
            try! reported.append(line, h.arena);
153
            continue;
154
        };
155
        let path = &line[.. at];
156
        let verdict = &line[at + SEPARATOR.len ..];
157
        let mut end: u32 = 0;
158
        while end < verdict.len and char::isAlpha(verdict[end]) {
159
            set end += 1;
160
        }
161
        // The fixture is the directory that holds the package.
162
        let mut name = path;
163
        if mem::endsWith(path, "/test/test.ril.bin") {
164
            let mut parts = mem::split(&path[.. path.len - "/test/test.ril.bin".len], '/');
165
            while let part = parts.next() {
166
                set name = part;
167
            }
168
        }
169
        try! reported.append(path, h.arena);
170
        if mem::eq(&verdict[.. end], "ok") {
171
            set accepted += 1;
172
        } else {
173
            try! rejected.append(h.join(&[name, " ", &verdict[.. end]]), h.arena);
174
        }
175
    }
176
    if not same(h, "the checker's report does not match the packages", list, &reported[..]) {
177
        return;
178
    }
179
    let total = accepted + rejected.len;
180
    let count = rejected.len;
181
    io::print(
182
        h.join(
183
            &[
184
                "verify: ",
185
                h.num(total),
186
                " fixtures, ",
187
                h.num(accepted),
188
                " accepted, ",
189
                h.num(count),
190
                " rejected\n",
191
            ]
192
        )
193
    );
194
    try checkSafeRejections(h, &rejected[..]);
195
    same(
196
        h,
197
        "the rejected fixtures differ from the expected ones",
198
        try h.manifest("test/verify/expected"),
199
        &rejected[..]
200
    );
201
}