lib/std/lang/il/binary/tests.rad 30.4 KiB raw
1
//! Binary RIL encoding fixtures.
2
3
@test export mod decode;
4
5
use std::lang::il;
6
use std::lang::il::binary;
7
use std::lang::il::binary::reader;
8
use std::lang::il::binary::writer;
9
use std::mem::heap;
10
use std::testing;
11
12
/// Decode arena backing storage. Tests reset it before each use.
13
static MEMORY: [u8; 512] = [0; 512];
14
15
/// Check little-endian encoding for every integer width.
16
@test unsafe fn integers() throws (testing::TestError) {
17
    static buffer: [u8; 15] = [0; 15];
18
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
19
    try writer::integer(&mut out, 0x12, 1) catch {
20
        throw testing::TestError::Failed;
21
    };
22
    try writer::integer(&mut out, 0x3456, 2) catch {
23
        throw testing::TestError::Failed;
24
    };
25
    try writer::integer(&mut out, 0x789abcde, 4) catch {
26
        throw testing::TestError::Failed;
27
    };
28
    try writer::integer(&mut out, 0x0123456789abcdef, 8) catch {
29
        throw testing::TestError::Failed;
30
    };
31
    try testing::expectBytesEq(
32
        &buffer[..],
33
        &[
34
            0x12, 0x56, 0x34, 0xde, 0xbc, 0x9a, 0x78, 0xef, 0xcd,
35
            0xab, 0x89, 0x67, 0x45, 0x23, 0x01,
36
        ]
37
    );
38
}
39
40
/// Argument pool of the function that owns the test instructions.
41
constant ARGS: [il::Val; 1] = [il::Val::Undef];
42
43
/// Case pool of the function that owns the test instructions.
44
constant CASES: [il::SwitchCase; 1] = [
45
    il::SwitchCase {
46
        value: -1, target: 2, args: il::ArgRange { start: 0, len: 1 }
47
    },
48
];
49
50
/// Argument range selecting the single value of `ARGS`.
51
constant ONE_ARG: il::ArgRange = il::ArgRange { start: 0, len: 1 };
52
53
/// Create a cursor with the symbols and bounds of the instruction fixtures.
54
fn cursor(wire: &'wire [u8], arena: &heap::Heap) -> reader::Reader['wire] {
55
    let mut input = reader::new(wire);
56
    set input.symbols = &["data", "fn"];
57
    set input.registers = 16;
58
    set input.blocks = 4;
59
    set input.layouts = 4;
60
    set input.signatures = 2;
61
    return input;
62
}
63
64
/// Compare one instruction with its fixed wire representation.
65
unsafe fn instruction(item: il::Instr, expected: &'wire [u8]) throws (testing::TestError) {
66
    let mut symbols = writer::Listed { names: &["data", "fn"] };
67
    let func = il::Fn {
68
        name: "f",
69
        params: &[],
70
        returnType: il::ValType::Data(il::Type::W64),
71
        isExtern: false,
72
        isLeaf: true,
73
        blocks: &[],
74
        vals: &ARGS[..],
75
        cases: &CASES[..],
76
        borrows: &[],
77
    };
78
    static buffer: [u8; 256] = [0; 256];
79
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
80
    try writer::instr(&mut out, &func, item, &mut symbols) catch {
81
        throw testing::TestError::Failed;
82
    };
83
    try testing::expectBytesEq(&buffer[.. out.offset], expected);
84
    for capacity in 0 .. expected.len {
85
        let mut short = writer::new(&mut buffer[.. capacity] as *mut [u8]);
86
        let mut failed = false;
87
        try writer::instr(&mut short, &func, item, &mut symbols) catch err {
88
            try testing::expect(err == binary::Error::Capacity);
89
            set failed = true;
90
        };
91
        try testing::expect(failed);
92
        try testing::expect(short.offset <= capacity);
93
    }
94
    let arena = heap::over(&mut MEMORY[..]);
95
    let mut input = cursor(expected, arena);
96
    let decoded = try input.instr(arena) catch {
97
        throw testing::TestError::Failed;
98
    };
99
    try testing::expect(input.offset == expected.len);
100
    let vals = input.vals.take();
101
    let cases = input.cases.take();
102
    let decodedFn = il::Fn {
103
        name: "f",
104
        params: &[],
105
        returnType: il::ValType::Data(il::Type::W64),
106
        isExtern: false,
107
        isLeaf: true,
108
        blocks: &[],
109
        vals: &vals[..],
110
        cases: &cases[..],
111
        borrows: &[],
112
    };
113
    set out.offset = 0;
114
    try writer::instr(&mut out, &decodedFn, decoded, &mut symbols) catch {
115
        throw testing::TestError::Failed;
116
    };
117
    try testing::expectBytesEq(&buffer[.. out.offset], expected);
118
    for length in 0 .. expected.len {
119
        heap::reset(arena);
120
        set input = cursor(&expected[.. length], arena);
121
        let mut failed = false;
122
        try input.instr(arena) catch err {
123
            try testing::expect(err == binary::Error::Truncated);
124
            set failed = true;
125
        };
126
        try testing::expect(failed);
127
    }
128
}
129
130
/// Check all instruction tags and their field order.
131
@test unsafe fn instructions() throws (testing::TestError) {
132
    try instruction(il::Instr::Reserve {
133
        dst: il::Reg { n: 1 }, layout: 3
134
    }, &[0, 1, 0, 0, 0, 3, 0, 0, 0]);
135
    try instruction(il::Instr::Load {
136
        typ: il::Type::W8, dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: -1
137
    }, &[1, 1, 1, 0, 0, 0, 2, 0, 0, 0, 255, 255, 255, 255]);
138
    try instruction(il::Instr::Sload {
139
        typ: il::Type::W16, dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 3
140
    }, &[2, 2, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]);
141
    try instruction(il::Instr::Store {
142
        typ: il::Type::W32, src: il::Val::Undef, dst: il::Reg { n: 2 }, offset: 3
143
    }, &[3, 4, 4, 2, 0, 0, 0, 3, 0, 0, 0]);
144
    try instruction(il::Instr::Blit {
145
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3
146
    }, &[4, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]);
147
    try instruction(il::Instr::Copy {
148
        dst: il::Reg { n: 1 }, val: il::Val::Undef
149
    }, &[5, 1, 0, 0, 0, 4]);
150
    try instruction(il::Instr::Field {
151
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8
152
    }, &[21, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0]);
153
    try instruction(il::Instr::Elem {
154
        dst: il::Reg { n: 1 }, base: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3, count: 5
155
    }, &[22, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 0, 0]);
156
    try instruction(il::Instr::BytesLoad {
157
        typ: il::Type::W64, dst: il::Reg { n: 1 }, slice: il::Reg { n: 2 }, offset: il::Val::Undef
158
    }, &[40, 8, 1, 0, 0, 0, 2, 0, 0, 0, 4]);
159
    try instruction(il::Instr::BytesStore {
160
        typ: il::Type::W32, slice: il::Reg { n: 2 }, offset: il::Val::Undef, value: il::Val::Undef
161
    }, &[41, 4, 2, 0, 0, 0, 4, 4]);
162
    try instruction(il::Instr::SliceElem {
163
        dst: il::Reg { n: 1 }, slice: il::Reg { n: 2 }, index: il::Val::Undef, layout: 3
164
    }, &[23, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0]);
165
    try instruction(il::Instr::SliceNew {
166
        dst: il::Reg { n: 1 },
167
        base: il::Val::Undef,
168
        layout: 3,
169
        count: 5,
170
        start: il::Val::Undef,
171
        end: il::Val::Undef,
172
    }, &[24, 1, 0, 0, 0, 4, 3, 0, 0, 0, 5, 0, 0, 0, 4, 4]);
173
    try instruction(il::Instr::SliceSub {
174
        dst: il::Reg { n: 1 },
175
        src: il::Reg { n: 2 },
176
        layout: 3,
177
        start: il::Val::Undef,
178
        end: il::Val::Undef,
179
    }, &[25, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 4]);
180
    try instruction(il::Instr::SliceLen {
181
        dst: il::Reg { n: 1 }, layout: 3, length: il::Val::Undef
182
    }, &[33, 1, 0, 0, 0, 3, 0, 0, 0, 4]);
183
    try instruction(il::Instr::SliceCopy {
184
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, layout: 3
185
    }, &[34, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]);
186
    try instruction(il::Instr::SliceGrow {
187
        ok: il::Reg { n: 1 },
188
        slice: il::Reg { n: 2 },
189
        heap: il::Val::Undef,
190
        layout: 3,
191
        capacity: il::Val::Imm(8),
192
    }, &[35, 1, 0, 0, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0, 1, 8, 0, 0, 0, 0, 0, 0, 0]);
193
    try instruction(il::Instr::LoadPtr {
194
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: -1, nullable: false
195
    }, &[26, 1, 0, 0, 0, 2, 0, 0, 0, 255, 255, 255, 255, 0]);
196
    try instruction(il::Instr::LoadPtr {
197
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, offset: 8, nullable: true
198
    }, &[26, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 1]);
199
    try instruction(il::Instr::StorePtr {
200
        src: il::Val::Undef, dst: il::Reg { n: 2 }, offset: 8
201
    }, &[27, 4, 2, 0, 0, 0, 8, 0, 0, 0]);
202
    try instruction(il::Instr::BinOp {
203
        op: il::BinOp::Add,
204
        typ: il::Type::W64,
205
        dst: il::Reg { n: 1 },
206
        a: il::Val::Undef,
207
        b: il::Val::Undef,
208
    }, &[6, 0, 8, 1, 0, 0, 0, 4, 4]);
209
    try instruction(il::Instr::UnOp {
210
        op: il::UnOp::Neg, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef
211
    }, &[7, 0, 8, 1, 0, 0, 0, 4]);
212
    try instruction(il::Instr::Zext {
213
        typ: il::Type::W8, dst: il::Reg { n: 1 }, val: il::Val::Undef
214
    }, &[8, 1, 1, 0, 0, 0, 4]);
215
    try instruction(il::Instr::Sext {
216
        typ: il::Type::W16, dst: il::Reg { n: 1 }, val: il::Val::Undef
217
    }, &[9, 2, 1, 0, 0, 0, 4]);
218
    try instruction(il::Instr::Call {
219
        retTy: il::ValType::Data(il::Type::W64),
220
        dst: nil,
221
        func: il::Val::FnAddr("fn"),
222
        args: il::ArgRange { start: 0, len: 0 },
223
    }, &[10, 1, 8, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0]);
224
    try instruction(il::Instr::Call {
225
        retTy: il::ValType::Data(il::Type::W64),
226
        dst: il::Reg { n: 2 },
227
        func: il::Val::FnAddr(
228
            "fn"
229
        ),
230
        args: ONE_ARG,
231
    }, &[10, 1, 8, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
232
    try instruction(il::Instr::Call {
233
        retTy: il::ValType::Void,
234
        dst: nil,
235
        func: il::Val::FnAddr("fn"),
236
        args: il::ArgRange {
237
            start: 0, len: 0
238
        },
239
    }, &[10, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0]);
240
    try instruction(il::Instr::Call {
241
        retTy: il::ValType::Ptr(il::PtrType {
242
            layout: 2, own: false, mutable: true, nullable: false
243
        }),
244
        dst: il::Reg { n: 2 },
245
        func: il::Val::FnAddr("fn"),
246
        args: ONE_ARG,
247
    }, &[10, 2, 2, 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
248
    try instruction(il::Instr::Call {
249
        retTy: il::ValType::Fn(1), dst: il::Reg { n: 2 }, func: il::Val::FnAddr("fn"), args: ONE_ARG
250
    }, &[10, 3, 1, 0, 0, 0, 1, 2, 0, 0, 0, 3, 1, 0, 0, 0, 1, 0, 0, 0, 4]);
251
    try instruction(il::Instr::Ret {
252
        val: nil
253
    }, &[11, 0]);
254
    try instruction(il::Instr::Ret {
255
        val: il::Val::Undef
256
    }, &[11, 1, 4]);
257
    try instruction(il::Instr::Jmp {
258
        target: 2, args: ONE_ARG
259
    }, &[12, 2, 0, 0, 0, 1, 0, 0, 0, 4]);
260
    try instruction(il::Instr::Br {
261
        op: il::CmpOp::Eq,
262
        typ: il::Type::W32,
263
        a: il::Val::Undef,
264
        b: il::Val::Undef,
265
        thenTarget: 1,
266
        thenArgs: ONE_ARG,
267
        elseTarget: 2,
268
        elseArgs: ONE_ARG,
269
    }, &[13, 0, 4, 4, 4, 1, 0, 0, 0, 1, 0, 0, 0, 4, 2, 0, 0, 0, 1, 0, 0, 0, 4]);
270
    try instruction(
271
        il::Instr::Switch {
272
            val: il::Val::Undef,
273
            defaultTarget: 1,
274
            defaultArgs: ONE_ARG,
275
            cases: il::CaseRange { start: 0, len: 1 },
276
        },
277
        &[
278
            14, 4, 1, 0, 0, 0, 1, 0, 0, 0, 4, 1,
279
            0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 2,
280
            0, 0, 0, 1, 0, 0, 0, 4,
281
        ]
282
    );
283
    try instruction(il::Instr::Unreachable, &[15]);
284
    try instruction(il::Instr::Ecall {
285
        dst: il::Reg { n: 1 },
286
        num: il::Val::Undef,
287
        a0: il::Val::Undef,
288
        a1: il::Val::Undef,
289
        a2: il::Val::Undef,
290
        a3: il::Val::Undef,
291
        buf: nil,
292
    }, &[16, 1, 0, 0, 0, 4, 4, 4, 4, 4, 0]);
293
    try instruction(il::Instr::Ecall {
294
        dst: il::Reg { n: 1 },
295
        num: il::Val::Undef,
296
        a0: il::Val::Undef,
297
        a1: il::Val::Undef,
298
        a2: il::Val::Undef,
299
        a3: il::Val::Undef,
300
        buf: il::EcallBuf {
301
            ptr: il::Reg { n: 2 }, size: 24
302
        },
303
    }, &[16, 1, 0, 0, 0, 4, 4, 4, 4, 4, 1, 2, 0, 0, 0, 24, 0, 0, 0]);
304
    try instruction(il::Instr::PageSlice {
305
        err: il::Reg { n: 1 },
306
        slot: il::Reg { n: 2 },
307
        handle: il::Val::Undef,
308
        write: true,
309
        layout: 3,
310
    }, &[28, 1, 0, 0, 0, 2, 0, 0, 0, 4, 1, 3, 0, 0, 0]);
311
    try instruction(il::Instr::PageHeap {
312
        err: il::Reg { n: 1 }, slot: il::Reg { n: 2 }, handle: il::Val::Undef
313
    }, &[36, 1, 0, 0, 0, 2, 0, 0, 0, 4]);
314
    try instruction(il::Instr::HeapReset { heap: il::Val::Undef }, &[37, 4]);
315
    try instruction(il::Instr::TraitNew {
316
        dst: il::Reg { n: 1 }, recv: il::Reg { n: 2 }, table: il::Val::Undef
317
    }, &[29, 1, 0, 0, 0, 2, 0, 0, 0, 4]);
318
    try instruction(il::Instr::TraitCall {
319
        retTy: il::ValType::Void,
320
        dst: nil,
321
        obj: il::Reg { n: 2 },
322
        method: 1,
323
        receiver: 0,
324
        args: ONE_ARG,
325
    }, &[30, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4]);
326
    try instruction(il::Instr::TraitCall {
327
        retTy: il::ValType::Data(il::Type::W64),
328
        dst: il::Reg { n: 3 },
329
        obj: il::Reg { n: 2 },
330
        method: 0,
331
        receiver: 0,
332
        args: ONE_ARG,
333
    }, &[30, 1, 8, 1, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4]);
334
    try instruction(il::Instr::TraitCopy {
335
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }
336
    }, &[31, 1, 0, 0, 0, 2, 0, 0, 0]);
337
    try instruction(il::Instr::UnionGet {
338
        dst: il::Reg { n: 1 }, src: il::Reg { n: 2 }, tag: 3, offset: 8, layout: 3
339
    }, &[38, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 8, 0, 0, 0, 3, 0, 0, 0]);
340
    try instruction(il::Instr::UnionSet {
341
        dst: il::Reg { n: 1 }, tag: 3, offset: 8, src: il::Val::Reg(il::Reg { n: 2 }), layout: 3
342
    }, &[39, 1, 0, 0, 0, 3, 0, 0, 0, 8, 0, 0, 0, 0, 2, 0, 0, 0, 1, 3, 0, 0, 0]);
343
    try instruction(il::Instr::UnionSet {
344
        dst: il::Reg { n: 1 }, tag: 2, offset: 8, src: il::Val::Undef, layout: nil
345
    }, &[39, 1, 0, 0, 0, 2, 0, 0, 0, 8, 0, 0, 0, 4, 0]);
346
    try instruction(il::Instr::Ebreak, &[17]);
347
    try instruction(il::Instr::MemoryFence, &[18]);
348
    for typ in [il::Type::W8, il::Type::W16, il::Type::W32, il::Type::W64] {
349
        let mut read: [u8; 8] = [19, 0, 1, 0, 0, 0, 4, 4];
350
        let mut write: [u8; 5] = [20, 0, 4, 4, 4];
351
        set read[1] = il::typeSize(typ) as u8;
352
        set write[1] = il::typeSize(typ) as u8;
353
        try instruction(
354
            il::Instr::DeviceRead {
355
                typ, dst: il::Reg { n: 1 }, handle: il::Val::Undef, offset: il::Val::Undef
356
            },
357
            &read[..]
358
        );
359
        try instruction(
360
            il::Instr::DeviceWrite {
361
                typ, handle: il::Val::Undef, offset: il::Val::Undef, value: il::Val::Undef
362
            },
363
            &write[..]
364
        );
365
    }
366
}
367
368
/// Check every value tag and empty and nonempty sequences.
369
@test unsafe fn values() throws (testing::TestError) {
370
    let mut symbols = writer::Listed { names: &["data", "fn"] };
371
    static buffer: [u8; 64] = [0; 64];
372
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
373
    try writer::values(&mut out, &[], &mut symbols) catch {
374
        throw testing::TestError::Failed;
375
    };
376
    try writer::values(
377
        &mut out,
378
        &[
379
            il::Val::Reg(il::Reg { n: 0x12345678 }),
380
            il::Val::Imm(-2), il::Val::DataSym("data"),
381
            il::Val::FnAddr("fn"), il::Val::Undef,
382
        ],
383
        &mut symbols
384
    ) catch {
385
        throw testing::TestError::Failed;
386
    };
387
    try testing::expectBytesEq(
388
        &buffer[.. out.offset],
389
        &[
390
            0, 0, 0, 0, 5, 0, 0, 0,
391
            0, 0x78, 0x56, 0x34, 0x12,
392
            1, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393
            2, 0, 0, 0, 0, 3, 1, 0, 0, 0, 4,
394
        ]
395
    );
396
    let arena = heap::withCapacity(&mut MEMORY[..], 256);
397
    let wire: &'wire [u8] = &buffer[.. out.offset] {
398
        let mut input = cursor(wire, arena);
399
        set input.registers = 0x12345679;
400
        let empty = try input.values(arena) catch {
401
            throw testing::TestError::Failed;
402
        };
403
        let decoded = try input.values(arena) catch {
404
            throw testing::TestError::Failed;
405
        };
406
        try testing::expect(empty.len == 0);
407
        try testing::expect(decoded.len == 5);
408
        try testing::expect(input.offset == out.offset);
409
        let pool = input.vals.take();
410
        static repeated: [u8; 64] = [0; 64];
411
        let mut copy = writer::new(&mut repeated[..] as *mut [u8]);
412
        try writer::values(
413
            &mut copy, &pool[empty.start .. empty.start + empty.len], &mut symbols
414
        ) catch {
415
            throw testing::TestError::Failed;
416
        };
417
        try writer::values(
418
            &mut copy, &pool[decoded.start .. decoded.start + decoded.len], &mut symbols
419
        ) catch {
420
            throw testing::TestError::Failed;
421
        };
422
        try testing::expectBytesEq(wire, &repeated[.. copy.offset]);
423
    }
424
}
425
426
/// Check initializer bytes and repetition counts.
427
@test unsafe fn initializers() throws (testing::TestError) {
428
    let mut symbols = writer::Listed { names: &["data", "fn"] };
429
    static buffer: [u8; 128] = [0; 128];
430
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
431
    try writer::dataValue(
432
        &mut out,
433
        il::DataValue {
434
            item: il::DataItem::Val {
435
                typ: il::Type::W8, val: -1
436
            },
437
            count: 0,
438
        },
439
        &mut symbols
440
    ) catch {
441
        throw testing::TestError::Failed;
442
    };
443
    try writer::dataValue(
444
        &mut out,
445
        il::DataValue {
446
            item: il::DataItem::Val {
447
                typ: il::Type::W16, val: -2
448
            },
449
            count: 1,
450
        },
451
        &mut symbols
452
    ) catch {
453
        throw testing::TestError::Failed;
454
    };
455
    try writer::dataValue(
456
        &mut out,
457
        il::DataValue {
458
            item: il::DataItem::Val {
459
                typ: il::Type::W32, val: -3
460
            },
461
            count: 2,
462
        },
463
        &mut symbols
464
    ) catch {
465
        throw testing::TestError::Failed;
466
    };
467
    try writer::dataValue(
468
        &mut out,
469
        il::DataValue {
470
            item: il::DataItem::Val {
471
                typ: il::Type::W64, val: -4
472
            },
473
            count: 3,
474
        },
475
        &mut symbols
476
    ) catch {
477
        throw testing::TestError::Failed;
478
    };
479
    try writer::dataValue(
480
        &mut out,
481
        il::DataValue {
482
            item: il::DataItem::Sym("data"), count: 4
483
        },
484
        &mut symbols
485
    ) catch {
486
        throw testing::TestError::Failed;
487
    };
488
    try writer::dataValue(
489
        &mut out,
490
        il::DataValue {
491
            item: il::DataItem::Fn("fn"), count: 5
492
        },
493
        &mut symbols
494
    ) catch {
495
        throw testing::TestError::Failed;
496
    };
497
    try writer::dataValue(
498
        &mut out,
499
        il::DataValue {
500
            item: il::DataItem::Str("ab"), count: 6
501
        },
502
        &mut symbols
503
    ) catch {
504
        throw testing::TestError::Failed;
505
    };
506
    try writer::dataValue(
507
        &mut out, il::DataValue { item: il::DataItem::Str(""), count: 7 }, &mut symbols
508
    ) catch {
509
        throw testing::TestError::Failed;
510
    };
511
    try writer::dataValue(
512
        &mut out, il::DataValue { item: il::DataItem::Undef, count: 8 }, &mut symbols
513
    ) catch {
514
        throw testing::TestError::Failed;
515
    };
516
    try testing::expectBytesEq(
517
        &buffer[.. out.offset],
518
        &[
519
            0, 1, 255, 0, 0, 0, 0, 0, 2, 254, 255, 1, 0,
520
            0,
521
            0,
522
            0,
523
            4,
524
            253,
525
            255,
526
            255,
527
            255,
528
            2,
529
            0,
530
            0,
531
            0,
532
            0,
533
            8,
534
            252,
535
            255,
536
            255,
537
            255,
538
            255,
539
            255,
540
            255,
541
            255,
542
            3,
543
            0,
544
            0,
545
            0,
546
            1,
547
            0,
548
            0,
549
            0,
550
            0,
551
            4,
552
            0,
553
            0,
554
            0,
555
            2,
556
            1,
557
            0,
558
            0,
559
            0,
560
            5,
561
            0,
562
            0,
563
            0,
564
            3,
565
            2,
566
            0,
567
            0,
568
            0,
569
            97,
570
            98,
571
            6,
572
            0,
573
            0, 0, 3, 0, 0, 0, 0, 7, 0, 0, 0, 4, 8, 0, 0, 0,
574
        ]
575
    );
576
}
577
578
/// Reject invalid widths, insufficient storage, and absent symbols.
579
@test unsafe fn errors() throws (testing::TestError) {
580
    let mut symbols = writer::Listed { names: &[] };
581
    static buffer: [u8; 8] = [0; 8];
582
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
583
    let mut failures: u32 = 0;
584
    try writer::integer(&mut out, 1, 3) catch err {
585
        try testing::expect(err == binary::Error::Invalid);
586
        set failures += 1;
587
    };
588
    try writer::symbol(&mut out, "absent", &mut symbols) catch err {
589
        try testing::expect(err == binary::Error::Symbol);
590
        set failures += 1;
591
    };
592
    try testing::expect(out.offset == 0);
593
    try writer::integer(&mut out, 0, 8) catch {
594
        throw testing::TestError::Failed;
595
    };
596
    try writer::integer(&mut out, 1, 1) catch err {
597
        try testing::expect(err == binary::Error::Capacity);
598
        set failures += 1;
599
    };
600
    try testing::expect(failures == 3);
601
    try testing::expect(out.offset == 8);
602
}
603
604
/// Check operation tags independently of the native union representation.
605
@test unsafe fn operations() throws (testing::TestError) {
606
    let binaryOps = &[
607
        il::BinOp::Add, il::BinOp::Sub, il::BinOp::Mul,
608
        il::BinOp::Sdiv, il::BinOp::Udiv, il::BinOp::Srem,
609
        il::BinOp::Urem, il::BinOp::Eq, il::BinOp::Ne,
610
        il::BinOp::Slt, il::BinOp::Sge, il::BinOp::Ult,
611
        il::BinOp::Uge, il::BinOp::And, il::BinOp::Or,
612
        il::BinOp::Xor, il::BinOp::Shl, il::BinOp::Sshr,
613
        il::BinOp::Ushr, il::BinOp::RotL, il::BinOp::RotR,
614
    ];
615
    for op, tag in binaryOps {
616
        try instruction(il::Instr::BinOp {
617
            op, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef, b: il::Val::Undef
618
        }, &[6, tag as u8, 8, 1, 0, 0, 0, 4, 4]);
619
    }
620
    for op, tag in &[
621
        il::UnOp::Neg, il::UnOp::Not, il::UnOp::LeadingZeros,
622
        il::UnOp::TrailingZeros, il::UnOp::PopCount, il::UnOp::ByteSwap,
623
    ] {
624
        try instruction(il::Instr::UnOp {
625
            op, typ: il::Type::W64, dst: il::Reg { n: 1 }, a: il::Val::Undef
626
        }, &[7, tag as u8, 8, 1, 0, 0, 0, 4]);
627
    }
628
    let empty = il::ArgRange { start: 0, len: 0 };
629
    for op, tag in &[il::CmpOp::Eq, il::CmpOp::Ne, il::CmpOp::Slt, il::CmpOp::Ult] {
630
        try instruction(il::Instr::Br {
631
            op,
632
            typ: il::Type::W8,
633
            a: il::Val::Undef,
634
            b: il::Val::Undef,
635
            thenTarget: 1,
636
            thenArgs: empty,
637
            elseTarget: 2,
638
            elseArgs: empty,
639
        }, &[13, tag as u8, 1, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]);
640
    }
641
}
642
643
/// Check byte-sequence bounds at every output size.
644
@test unsafe fn byteCapacity() throws (testing::TestError) {
645
    static buffer: [u8; 7] = [0; 7];
646
    for capacity in 0 .. 7 {
647
        let mut out = writer::new(&mut buffer[.. capacity] as *mut [u8]);
648
        let mut failed = false;
649
        try writer::bytes(&mut out, "abc") catch err {
650
            try testing::expect(err == binary::Error::Capacity);
651
            set failed = true;
652
        };
653
        try testing::expect(failed);
654
        try testing::expect(out.offset <= capacity);
655
    }
656
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
657
    try writer::bytes(&mut out, "abc") catch {
658
        throw testing::TestError::Failed;
659
    };
660
    try testing::expectBytesEq(&buffer[..], &[3, 0, 0, 0, 97, 98, 99]);
661
}
662
663
/// Check the wire form of value types, and that the reader checks table indices.
664
@test unsafe fn valTypes() throws (testing::TestError) {
665
    let ptr = il::PtrType { layout: 3, own: true, mutable: false, nullable: true };
666
    let fixtures: [il::ValType; 9] = [
667
        il::ValType::Void, il::ValType::Data(il::Type::W16), il::ValType::Ptr(ptr),
668
        il::ValType::Ptr(il::PtrType { layout: 0, own: false, mutable: true, nullable: false }),
669
        il::ValType::Fn(1), il::ValType::Fn(0),
670
        il::ValType::Heap(il::HeapType::Plain),
671
        il::ValType::Heap(il::HeapType::Typed),
672
        il::ValType::Heap(il::HeapType::Reset),
673
    ];
674
    let expected: [*[u8]; 9] = [
675
        &[0], &[1, 2], &[2, 3, 0, 0, 0, 5], &[2, 0, 0, 0, 0, 2], &[3, 1, 0, 0, 0], &[3, 0, 0, 0, 0],
676
        &[4, 0], &[4, 1], &[4, 2],
677
    ];
678
    for item, i in &fixtures[..] {
679
        static buffer: [u8; 8] = [0; 8];
680
        let mut out = writer::new(&mut buffer[..] as *mut [u8]);
681
        try writer::valType(&mut out, item) catch {
682
            throw testing::TestError::Failed;
683
        };
684
        try testing::expectBytesEq(&buffer[.. out.offset], expected[i]);
685
686
        let arena = heap::withCapacity(&mut MEMORY[..], 128);
687
        let wire: &'wire [u8] = &expected[i][..] {
688
            let mut input = cursor(wire, arena);
689
            let decoded = try input.valType() catch {
690
                throw testing::TestError::Failed;
691
            };
692
            try testing::expect(decoded == item);
693
        }
694
    }
695
    // Indices outside the tables, unknown tags, and unknown flags are invalid. A heap never has
696
    // both of its flags.
697
    let invalid: [*[u8]; 6] = [
698
        &[2, 4, 0, 0, 0, 0], &[3, 2, 0, 0, 0], &[5], &[2, 0, 0, 0, 0, 8], &[1, 3], &[4, 3]
699
    ];
700
    for bytes in &invalid[..] {
701
        let arena = heap::withCapacity(&mut MEMORY[..], 128);
702
        let wire: &'wire [u8] = &bytes[..] {
703
            let mut input = cursor(wire, arena);
704
            let mut failed = false;
705
            try input.valType() catch err {
706
                try testing::expect(err == binary::Error::Invalid);
707
                set failed = true;
708
            };
709
            try testing::expect(failed);
710
        }
711
    }
712
}
713
714
/// Check the wire form of a layout with every kind of slot, and of a signature.
715
@test unsafe fn layoutsAndSignatures() throws (testing::TestError) {
716
    static buffer: [u8; 160] = [0; 160];
717
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
718
    let layout = il::Layout {
719
        size: 64,
720
        alignment: 8,
721
        slots: &[
722
            il::Slot {
723
                offset: 0,
724
                kind: il::SlotKind::Ptr(il::PtrType {
725
                    layout: 1, own: true, mutable: false, nullable: true
726
                }),
727
                regions: &[],
728
            },
729
            il::Slot {
730
                offset: 8,
731
                kind: il::SlotKind::Slice(il::PtrType {
732
                    layout: 2, own: false, mutable: true, nullable: true
733
                }),
734
                regions: &[1],
735
            },
736
            il::Slot { offset: 24, kind: il::SlotKind::Fn(1), regions: &[] },
737
            il::Slot { offset: 32, kind: il::SlotKind::Sub(0), regions: &[] },
738
            il::Slot {
739
                offset: 40, kind: il::SlotKind::Array { layout: 3, count: 2 }, regions: &[]
740
            },
741
        ],
742
        regions: 2,
743
    };
744
    try writer::layout(&mut out, layout) catch {
745
        throw testing::TestError::Failed;
746
    };
747
    try testing::expectBytesEq(
748
        &buffer[.. out.offset],
749
        &[
750
            64, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0,
751
            0, 0, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0,
752
            8, 0, 0, 0, 1, 2, 0, 0, 0, 6, 1, 0, 0, 0, 1, 0, 0, 0,
753
            24, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0,
754
            32, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
755
            40, 0, 0, 0, 4, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
756
        ]
757
    );
758
    set out.offset = 0;
759
    try writer::signature(&mut out, il::Signature {
760
        params: &[il::ValType::Data(il::Type::W32), il::ValType::Fn(1)],
761
        ret: il::ValType::Void,
762
        borrows: &[],
763
    }) catch {
764
        throw testing::TestError::Failed;
765
    };
766
    try testing::expectBytesEq(&buffer[.. out.offset], &[
767
        2, 0, 0, 0, 1, 4, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0
768
    ]);
769
770
    // Each borrowed leaf names its result position, parameter and source leaf.
771
    set out.offset = 0;
772
    try writer::signature(&mut out, il::Signature {
773
        params: &[il::ValType::Data(il::Type::W32)],
774
        ret: il::ValType::Void,
775
        borrows: &[il::Borrow { target: nil, result: 0, param: 0, source: nil }],
776
    }) catch {
777
        throw testing::TestError::Failed;
778
    };
779
    try testing::expectBytesEq(&buffer[.. out.offset], &[
780
        1, 0, 0, 0, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
781
    ]);
782
    set out.offset = 0;
783
    try writer::signature(&mut out, il::Signature {
784
        params: &[il::ValType::Data(il::Type::W32)],
785
        ret: il::ValType::Void,
786
        borrows: &[il::Borrow { target: nil, result: 0, param: 0, source: 0 }],
787
    }) catch {
788
        throw testing::TestError::Failed;
789
    };
790
    try testing::expectBytesEq(&buffer[.. out.offset], &[
791
        1, 0, 0, 0, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0
792
    ]);
793
    // A target names the parameter whose pointee receives the source.
794
    set out.offset = 0;
795
    try writer::signature(&mut out, il::Signature {
796
        params: &[il::ValType::Data(il::Type::W32)],
797
        ret: il::ValType::Void,
798
        borrows: &[il::Borrow { target: 2, result: 1, param: 0, source: nil }],
799
    }) catch {
800
        throw testing::TestError::Failed;
801
    };
802
    try testing::expectBytesEq(&buffer[.. out.offset], &[
803
        1, 0, 0, 0, 1, 4, 0, 1, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
804
    ]);
805
}
806
807
/// Check the wire form of a layout with a union slot.
808
@test unsafe fn unionSlotWire() throws (testing::TestError) {
809
    static buffer: [u8; 64] = [0; 64];
810
    let mut out = writer::new(&mut buffer[..] as *mut [u8]);
811
    let layout = il::Layout {
812
        size: 16,
813
        alignment: 8,
814
        slots: &[
815
            il::Slot {
816
                offset: 0,
817
                kind: il::SlotKind::Union {
818
                    offset: 8,
819
                    size: 16,
820
                    variants: &[
821
                        il::UnionVariant { tag: 0, layout: 1, regions: &[] },
822
                        il::UnionVariant { tag: 2, layout: nil, regions: &[] },
823
                    ],
824
                },
825
                regions: &[],
826
            },
827
        ],
828
        regions: 0,
829
    };
830
    try writer::layout(&mut out, layout) catch {
831
        throw testing::TestError::Failed;
832
    };
833
    try testing::expectBytesEq(
834
        &buffer[.. out.offset],
835
        &[
836
            16, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
837
            0, 0, 0, 0, 7, 8, 0, 0, 0, 16, 0, 0, 0, 2, 0, 0, 0,
838
            0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
839
            2, 0, 0, 0, 0, 0, 0, 0, 0,
840
            0, 0, 0, 0,
841
        ]
842
    );
843
}
844
845
/// Export lookup finds every name of a sorted table and nothing between them.
846
@test fn exportLookup() throws (testing::TestError) {
847
    let exports: *[binary::Export] = &[
848
        binary::Export { name: "p::a", kind: binary::ExportKind::Data(1), startable: false },
849
        binary::Export { name: "p::c", kind: binary::ExportKind::Function(0), startable: false },
850
        binary::Export { name: "p::e", kind: binary::ExportKind::Data(0), startable: false },
851
    ];
852
    for item in exports {
853
        let found = binary::find(exports, item.name) else {
854
            throw testing::TestError::Failed;
855
        };
856
        try testing::expect(found.kind == item.kind);
857
    }
858
    try testing::expect(binary::find(exports, "p::") == nil);
859
    try testing::expect(binary::find(exports, "p::b") == nil);
860
    try testing::expect(binary::find(exports, "p::d") == nil);
861
    try testing::expect(binary::find(exports, "p::f") == nil);
862
    try testing::expect(binary::find(&[], "p::a") == nil);
863
}