WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

test_call.rs 16.2 KB
Newer Older
1
2
extern crate libloading;

qinsoon's avatar
qinsoon committed
3
4
use mu::ast::types::*;
use mu::ast::ir::*;
5
use mu::ast::ptr::*;
qinsoon's avatar
qinsoon committed
6
7
8
9
10
11
use mu::ast::inst::*;
use mu::vm::*;
use mu::compiler::*;

use std::sync::RwLock;
use std::sync::Arc;
12
use mu::testutil;
qinsoon's avatar
qinsoon committed
13
use mu::testutil::aot;
14
use mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
15
16
17
18
19
20
21

#[test]
fn test_ccall_exit() {
    VM::start_logging_trace();

    let vm = Arc::new(ccall_exit());

qinsoon's avatar
qinsoon committed
22
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
23
24
25
26
27
28
29
30
31
32
33

    let func_id = vm.id_of("ccall_exit");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

        compiler.compile(&mut func_ver);
    }

qinsoon's avatar
qinsoon committed
34
    vm.make_primordial_thread(func_id, true, vec![]);
qinsoon's avatar
qinsoon committed
35
36
    backend::emit_context(&vm);

qinsoon's avatar
qinsoon committed
37
    let executable = aot::link_primordial(vec!["ccall_exit".to_string()], "ccall_exit_test", &vm);
qinsoon's avatar
qinsoon committed
38
39
40
41
42
43
44
45
46
    let output = aot::execute_nocheck(executable);

    assert!(output.status.code().is_some());

    let ret_code = output.status.code().unwrap();
    println!("return code: {}", ret_code);
    assert!(ret_code == 10);
}

47
pub fn gen_ccall_exit(arg: P<TreeNode>, func_ver: &mut MuFunctionVersion, vm: &VM) -> Box<TreeNode> {
qinsoon's avatar
qinsoon committed
48
49
    // .typedef @int32 = int<32>
    let type_def_int32 = vm.declare_type(vm.next_id(), MuType_::int(32));
50
    vm.set_name(type_def_int32.as_entity(), Mu("exit_int32"));
qinsoon's avatar
qinsoon committed
51
52
53
54
55
56
57
58
59
60
61
62
63

    // .typedef @exit_sig = (@int32) -> !
    let exit_sig = vm.declare_func_sig(vm.next_id(), vec![], vec![type_def_int32.clone()]);
    vm.set_name(exit_sig.as_entity(), Mu("exit_sig"));

    // .typedef @ufp_exit = ufuncptr(@exit_sig)
    let type_def_ufp_exit = vm.declare_type(vm.next_id(), MuType_::UFuncPtr(exit_sig.clone()));
    vm.set_name(type_def_ufp_exit.as_entity(), Mu("ufp_exit"));

    // .const @exit = EXTERN SYMBOL "exit"
    let const_exit = vm.declare_const(vm.next_id(), type_def_ufp_exit.clone(), Constant::ExternSym(C("exit")));
    vm.set_name(const_exit.as_entity(), Mu("exit"));

qinsoon's avatar
qinsoon committed
64
    // exprCCALL %const_exit (%const_int32_10)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    let const_exit_local = func_ver.new_constant(const_exit.clone());

    func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
        ops: RwLock::new(vec![const_exit_local, arg]),
        v: Instruction_::ExprCCall {
            data: CallData {
                func: 0,
                args: vec![1],
                convention: CallConvention::Foreign(ForeignFFI::C)
            },
            is_abort: false
        }
    })
}

fn ccall_exit() -> VM {
    let vm = VM::new();

    // .typedef @int32 = int<32>
    let type_def_int32 = vm.declare_type(vm.next_id(), MuType_::int(32));
    vm.set_name(type_def_int32.as_entity(), Mu("int32"));

qinsoon's avatar
qinsoon committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    // .const @int32_10 = 10
    let const_int32_10 = vm.declare_const(vm.next_id(), type_def_int32.clone(), Constant::Int(10));
    vm.set_name(const_int32_10.as_entity(), Mu("const_int32_10"));

    // .const @int32_0 = 0
    let const_int32_0 = vm.declare_const(vm.next_id(), type_def_int32.clone(), Constant::Int(0));
    vm.set_name(const_int32_0.as_entity(), Mu("const_int32_0"));

    // .funcsig @ccall_exit_sig = () -> !
    let ccall_exit_sig = vm.declare_func_sig(vm.next_id(), vec![], vec![]);
    vm.set_name(ccall_exit_sig.as_entity(), Mu("ccall_exit_sig"));

    // .funcdecl @ccall_exit <@ccall_exit_sig>
    let func_id = vm.next_id();
    let func = MuFunction::new(func_id, ccall_exit_sig.clone());
    vm.set_name(func.as_entity(), Mu("ccall_exit"));
    vm.declare_func(func);

    // .funcdef @ccall_exit VERSION @ccall_exit_v1 <@ccall_exit_sig>
    let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, ccall_exit_sig.clone());
    vm.set_name(func_ver.as_entity(), Mu("ccall_exit_v1"));

    // %entry():
    let mut blk_entry = Block::new(vm.next_id());
    vm.set_name(blk_entry.as_entity(), Mu("entry"));

qinsoon's avatar
qinsoon committed
115
    // exprCCALL %const_exit (%const_int32_10)
qinsoon's avatar
qinsoon committed
116
    let const_int32_10_local = func_ver.new_constant(const_int32_10.clone());
117
    let blk_entry_ccall = gen_ccall_exit(const_int32_10_local.clone(), &mut func_ver, &vm);
qinsoon's avatar
qinsoon committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

    // RET %const_int32_0
    let const_int32_0_local = func_ver.new_constant(const_int32_0.clone());

    let blk_entry_ret = func_ver.new_inst(Instruction{
        hdr: MuEntityHeader::unnamed(vm.next_id()),
        value: None,
        ops: RwLock::new(vec![const_int32_0_local]),
        v: Instruction_::Return(vec![0])
    });

    blk_entry.content = Some(BlockContent {
        args: vec![],
        exn_arg: None,
        body: vec![blk_entry_ccall, blk_entry_ret],
        keepalives: None
    });

136
137
138
    func_ver.define(FunctionContent::new(
        blk_entry.id(),
        {
139
140
141
            let mut map = LinkedHashMap::new();
            map.insert(blk_entry.id(), blk_entry);
            map
qinsoon's avatar
qinsoon committed
142
        }
143
    ));
qinsoon's avatar
qinsoon committed
144
145
146
147

    vm.define_func_version(func_ver);

    vm
148
}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

#[test]
fn test_pass_1arg_by_stack() {
    VM::start_logging_trace();
    let vm = Arc::new(pass_1arg_by_stack());

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_foo = vm.id_of("foo7");
    let func_main = vm.id_of("pass_1arg_by_stack");
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();

        {
            let func = funcs.get(&func_foo).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
        {
            let func = funcs.get(&func_main).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
    }

    vm.make_primordial_thread(func_main, true, vec![]);
    backend::emit_context(&vm);

    let executable = aot::link_primordial(vec![Mu("foo7"), Mu("pass_1arg_by_stack")], "test_pass_1arg_by_stack", &vm);
    let output = aot::execute_nocheck(executable);

    // exit with (1)
    assert!(output.status.code().is_some());
    assert_eq!(output.status.code().unwrap(), 1);
}

fn pass_1arg_by_stack() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));
    constdef!   ((vm) <int64> int64_2 = Constant::Int(2));

    // foo7
    funcsig!    ((vm) foo7_sig = (int64, int64, int64, int64, int64, int64, int64) -> (int64));
    funcdecl!   ((vm) <foo7_sig> foo7);
    funcdef!    ((vm) <foo7_sig> foo7 VERSION foo7_v1);

    // blk_entry
    ssa!        ((vm, foo7_v1) <int64> v0);
    ssa!        ((vm, foo7_v1) <int64> v1);
    ssa!        ((vm, foo7_v1) <int64> v2);
    ssa!        ((vm, foo7_v1) <int64> v3);
    ssa!        ((vm, foo7_v1) <int64> v4);
    ssa!        ((vm, foo7_v1) <int64> v5);
    ssa!        ((vm, foo7_v1) <int64> v6);
    block!      ((vm, foo7_v1) blk_entry);

    inst!       ((vm, foo7_v1) blk_entry_ret:
        RET (v6)
    );

    define_block!((vm, foo7_v1) blk_entry(v0, v1, v2, v3, v4, v5, v6) {
        blk_entry_ret
    });

    define_func_ver!((vm) foo7_v1 (entry: blk_entry) {blk_entry});

    // pass_1arg_by_stack
    funcsig!    ((vm) sig = () -> ());
    funcdecl!   ((vm) <sig> pass_1arg_by_stack);
    funcdef!    ((vm) <sig> pass_1arg_by_stack VERSION pass_1arg_by_stack_v1);

    typedef!    ((vm) type_funcref_foo7 = mu_funcref(foo7_sig));
    constdef!   ((vm) <type_funcref_foo7> const_funcref_foo7 = Constant::FuncRef(vm.id_of("foo7")));

    // blk_entry
    consta!     ((vm, pass_1arg_by_stack_v1) int64_0_local = int64_0);
    consta!     ((vm, pass_1arg_by_stack_v1) int64_1_local = int64_1);

    block!      ((vm, pass_1arg_by_stack_v1) blk_entry);
    block!      ((vm, pass_1arg_by_stack_v1) blk_main);
    inst!       ((vm, pass_1arg_by_stack_v1) blk_entry_branch:
        BRANCH blk_main (
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,

            int64_1_local
        )
    );

    define_block!((vm, pass_1arg_by_stack_v1) blk_entry() {blk_entry_branch});

    // blk_main
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a0);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a1);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a2);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a3);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a4);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a5);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> a6);

    consta!     ((vm, pass_1arg_by_stack_v1) const_funcref_foo7_local = const_funcref_foo7);
    ssa!        ((vm, pass_1arg_by_stack_v1) <int64> retval);
    inst!       ((vm, pass_1arg_by_stack_v1) blk_main_call:
        retval = EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_foo7_local (a0, a1, a2, a3, a4, a5, a6)
    );

    let blk_main_exit = gen_ccall_exit(retval.clone(), &mut pass_1arg_by_stack_v1, &vm);

    inst!       ((vm, pass_1arg_by_stack_v1) blk_main_ret:
        RET
    );

    define_block!((vm, pass_1arg_by_stack_v1) blk_main(a0, a1, a2, a3, a4, a5, a6) {
        blk_main_call,
        blk_main_exit,
        blk_main_ret
    });

    define_func_ver!((vm) pass_1arg_by_stack_v1 (entry: blk_entry) {
        blk_entry,
        blk_main
    });

    vm
}

#[test]
fn test_pass_2args_by_stack() {
    VM::start_logging_trace();
    let vm = Arc::new(pass_2args_by_stack());

    let compiler = Compiler::new(CompilerPolicy::default(), &vm);

    let func_foo = vm.id_of("foo8");
    let func_main = vm.id_of("pass_2args_by_stack");
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();

        {
            let func = funcs.get(&func_foo).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
        {
            let func = funcs.get(&func_main).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();

            compiler.compile(&mut func_ver);
        }
    }

    vm.make_primordial_thread(func_main, true, vec![]);
    backend::emit_context(&vm);

    let executable = aot::link_primordial(vec![Mu("foo8"), Mu("pass_2args_by_stack")], "test_pass_2args_by_stack", &vm);
    let output = aot::execute_nocheck(executable);

    // exit with (2)
    assert!(output.status.code().is_some());
    assert_eq!(output.status.code().unwrap(), 2);
}

fn pass_2args_by_stack() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));
    constdef!   ((vm) <int64> int64_2 = Constant::Int(2));

    // foo8
    funcsig!    ((vm) foo8_sig = (int64, int64, int64, int64, int64, int64, int64, int64) -> (int64));
    funcdecl!   ((vm) <foo8_sig> foo8);
    funcdef!    ((vm) <foo8_sig> foo8 VERSION foo8_v1);

    // blk_entry
    ssa!        ((vm, foo8_v1) <int64> v0);
    ssa!        ((vm, foo8_v1) <int64> v1);
    ssa!        ((vm, foo8_v1) <int64> v2);
    ssa!        ((vm, foo8_v1) <int64> v3);
    ssa!        ((vm, foo8_v1) <int64> v4);
    ssa!        ((vm, foo8_v1) <int64> v5);
    ssa!        ((vm, foo8_v1) <int64> v6);
    ssa!        ((vm, foo8_v1) <int64> v7);
    block!      ((vm, foo8_v1) blk_entry);

    inst!       ((vm, foo8_v1) blk_entry_ret:
        RET (v7)
    );

    define_block!((vm, foo8_v1) blk_entry(v0, v1, v2, v3, v4, v5, v6, v7) {
        blk_entry_ret
    });

    define_func_ver!((vm) foo8_v1 (entry: blk_entry) {blk_entry});

    // pass_2args_by_stack
    funcsig!    ((vm) sig = () -> ());
    funcdecl!   ((vm) <sig> pass_2args_by_stack);
    funcdef!    ((vm) <sig> pass_2args_by_stack VERSION pass_2args_by_stack_v1);

    typedef!    ((vm) type_funcref_foo8 = mu_funcref(foo8_sig));
    constdef!   ((vm) <type_funcref_foo8> const_funcref_foo8 = Constant::FuncRef(vm.id_of("foo8")));

    // blk_entry
    consta!     ((vm, pass_2args_by_stack_v1) int64_0_local = int64_0);
    consta!     ((vm, pass_2args_by_stack_v1) int64_1_local = int64_1);
    consta!     ((vm, pass_2args_by_stack_v1) int64_2_local = int64_2);

    block!      ((vm, pass_2args_by_stack_v1) blk_entry);
    block!      ((vm, pass_2args_by_stack_v1) blk_main);
    inst!       ((vm, pass_2args_by_stack_v1) blk_entry_branch:
        BRANCH blk_main (
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,
            int64_0_local,

            int64_1_local,
            int64_2_local
        )
    );

    define_block!((vm, pass_2args_by_stack_v1) blk_entry() {blk_entry_branch});

    // blk_main
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a0);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a1);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a2);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a3);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a4);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a5);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a6);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> a7);

    consta!     ((vm, pass_2args_by_stack_v1) const_funcref_foo8_local = const_funcref_foo8);
    ssa!        ((vm, pass_2args_by_stack_v1) <int64> retval);
    inst!       ((vm, pass_2args_by_stack_v1) blk_main_call:
        retval = EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_foo8_local (a0, a1, a2, a3, a4, a5, a6, a7)
    );

    let blk_main_exit = gen_ccall_exit(retval.clone(), &mut pass_2args_by_stack_v1, &vm);

    inst!       ((vm, pass_2args_by_stack_v1) blk_main_ret:
        RET
    );

    define_block!((vm, pass_2args_by_stack_v1) blk_main(a0, a1, a2, a3, a4, a5, a6, a7) {
        blk_main_call,
        blk_main_exit,
        blk_main_ret
    });

    define_func_ver!((vm) pass_2args_by_stack_v1 (entry: blk_entry) {
        blk_entry,
        blk_main
    });

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
    vm
}

#[test]
fn test_pass_fp_arg() {
    let lib = testutil::compile_fncs("pass_fp_arg", vec!["pass_fp_arg", "foo"], &pass_fp_arg);

    unsafe {
        let pass_fp_arg : libloading::Symbol<unsafe extern fn (f64) -> f64> = lib.get(b"pass_fp_arg").unwrap();

        let res1 = pass_fp_arg(0f64);
        println!("pass_fp_arg(0.0) = {}", res1);
        assert!(res1 == 0f64);

        let res2 = pass_fp_arg(3.14f64);
        println!("pass_fp_arg(3.14) = {}", res2);
        assert!(res2 == 3.14f64);
    }
}

fn pass_fp_arg() -> VM {
    let vm = VM::new_with_opts("init_mu --disable-inline");

    typedef!    ((vm) double = mu_double);

    // foo
    funcsig!    ((vm) foo_sig = (double) -> (double));
    funcdecl!   ((vm) <foo_sig> foo);
    funcdef!    ((vm) <foo_sig> foo VERSION foo_v1);

    // blk_entry
    ssa!        ((vm, foo_v1) <double> x);
    block!      ((vm, foo_v1) blk_entry);

    inst!       ((vm, foo_v1) blk_entry_ret:
        RET (x)
    );

    define_block!   ((vm, foo_v1) blk_entry(x) {
        blk_entry_ret
    });

    define_func_ver!((vm) foo_v1 (entry: blk_entry) {
        blk_entry
    });

    // pass_fp_arg
    funcsig!    ((vm) sig = (double) -> (double));
    funcdecl!   ((vm) <sig> pass_fp_arg);
    funcdef!    ((vm) <sig> pass_fp_arg VERSION pass_fp_arg_v1);

    typedef!    ((vm) type_funcref_foo = mu_funcref(foo_sig));
    constdef!   ((vm) <type_funcref_foo> const_funcref_foo = Constant::FuncRef(vm.id_of("foo")));

    // blk_entry
    ssa!        ((vm, pass_fp_arg_v1) <double> x);
    block!      ((vm, pass_fp_arg_v1) blk_entry);

    ssa!        ((vm, pass_fp_arg_v1) <double> res);
    consta!     ((vm, pass_fp_arg_v1) const_funcref_foo_local = const_funcref_foo);
    inst!       ((vm, pass_fp_arg_v1) blk_entry_call:
        res = EXPRCALL (CallConvention::Mu, is_abort: false) const_funcref_foo_local (x)
    );

    inst!       ((vm, pass_fp_arg_v1) blk_entry_ret:
        RET (res)
    );

    define_block!((vm, pass_fp_arg_v1) blk_entry(x) {
        blk_entry_call,
        blk_entry_ret
    });

    define_func_ver!((vm) pass_fp_arg_v1 (entry: blk_entry) {
        blk_entry
    });

500
501
    vm
}