test/verify/check.rad 4.0 KiB raw
1
//! Verify the programs of binary RIL packages and report the outcome of each.
2
//!
3
//! Each argument is the path of a package file. The tool prints one line per file:
4
//! the path, then `ok`, or the violated rule and its location.
5
6
use std::io;
7
use std::mem;
8
use std::sys;
9
use std::fs;
10
use std::mem::heap;
11
use std::lang::il;
12
use std::lang::il::binary;
13
use std::lang::il::binary::program;
14
use std::lang::il::verify;
15
16
/// Number of elements reserved for keep storage.
17
constant KEEP_CAPACITY: u32 = 16777216;
18
19
/// Largest package file.
20
constant IMAGE_SIZE: u32 = 8388608;
21
/// Decode arena capacity.
22
constant DECODE_SIZE: u32 = 100663296;
23
/// Verifier scratch capacity.
24
constant SCRATCH_SIZE: u32 = 33554432;
25
/// Package file storage.
26
static IMAGE: [u8; IMAGE_SIZE] = [0; IMAGE_SIZE];
27
/// Decoded package storage.
28
static DECODED: [u8; DECODE_SIZE] = [0; DECODE_SIZE];
29
/// Storage for the narrowed exports of a package.
30
static KEEP: [u8; KEEP_CAPACITY] = [0; KEEP_CAPACITY];
31
/// Verifier working storage.
32
static SCRATCH: [u8; SCRATCH_SIZE] = [0; SCRATCH_SIZE];
33
34
/// Limits of the verifier for the fixtures.
35
constant CONFIG: verify::Config = verify::Config {
36
    registers: 1048576,
37
    blocks: 65536,
38
    layoutSize: 0x7fffffff,
39
    layoutDepth: 32,
40
    params: 16,
41
    envSize: 4096,
42
};
43
44
/// Print the location of an error.
45
fn printPlace(program: &il::Program, place: verify::Place) {
46
    match place {
47
        case verify::Place::Package => io::print("package"),
48
        case verify::Place::Layout(i) => {
49
            io::print("layout ");
50
            io::printU32(i);
51
        }
52
        case verify::Place::Signature(i) => {
53
            io::print("signature ");
54
            io::printU32(i);
55
        }
56
        case verify::Place::Trait(i) => {
57
            io::print("trait ");
58
            io::printU32(i);
59
        }
60
        case verify::Place::Data(i) => {
61
            io::print("data ");
62
            io::print(program.data[i].name);
63
        }
64
        case verify::Place::Fn(i) => {
65
            io::print("fn ");
66
            io::print(program.fns[i].name);
67
        }
68
        case verify::Place::Instr { func, block, instr } => {
69
            io::print("fn ");
70
            io::print(program.fns[func].name);
71
            io::print(" block ");
72
            io::printU32(block);
73
            io::print(" instr ");
74
            io::printU32(instr);
75
        }
76
    }
77
}
78
79
/// Verify the package as a loadable one or as a program.
80
unsafe fn verifyAs(
81
    package: &mut binary::Package,
82
    loadable: bool,
83
    scratch: &heap::Heap,
84
    keep: &heap::Heap
85
) throws (verify::Error) {
86
    if loadable {
87
        try verify::verifyPackage(package, CONFIG, scratch, keep);
88
    } else {
89
        try verify::verifyProgram(&package.program, CONFIG, scratch);
90
    }
91
}
92
93
/// Verify the package in the file at `path`, as a loadable package when `loadable` is set,
94
/// and print the result.
95
unsafe fn check(path: *[u8], loadable: bool) {
96
    io::print(path);
97
    io::print(": ");
98
    let length = try fs::readFile(path, &mut IMAGE[..]) catch {
99
        io::print("unreadable\n");
100
        return;
101
    };
102
    let arena = heap::over(&mut DECODED[..]);
103
    let mut package = try program::decode(&IMAGE[.. length], arena, binary::Limits {
104
        registers: CONFIG.registers, blocks: CONFIG.blocks
105
    }) catch {
106
        io::print("undecodable\n");
107
        return;
108
    };
109
    let scratch = heap::over(&mut SCRATCH[..]);
110
    let keep = heap::over(&mut KEEP[..]);
111
    try verifyAs(&mut package, loadable, scratch, keep) catch err {
112
        io::print(verify::ruleName(err.rule));
113
        io::print(" ");
114
        printPlace(&package.program, err.place);
115
        io::print("\n");
116
        return;
117
    };
118
    io::print("ok\n");
119
}
120
121
/// Verify each package named on the command line.
122
@default unsafe fn main(env: *sys::Env) -> i32 {
123
    let mut first: u32 = 0;
124
    let mut loadable = false;
125
    if env.args.len > 0 and mem::eq(env.args[0], "-package") {
126
        set loadable = true;
127
        set first = 1;
128
    }
129
    for i in first .. env.args.len {
130
        check(env.args[i], loadable);
131
    }
132
    return 0;
133
}