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_global.rs 27.3 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9
10
11
12
13
14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

qinsoon's avatar
qinsoon committed
15
16
extern crate mu;
extern crate log;
17
extern crate libloading;
qinsoon's avatar
qinsoon committed
18
19

use test_ir::test_ir::global_access;
qinsoon's avatar
qinsoon committed
20
use test_compiler::test_call::gen_ccall_exit;
qinsoon's avatar
qinsoon committed
21
use self::mu::compiler::*;
qinsoon's avatar
qinsoon committed
22
use self::mu::vm::VM;
23
24
25
26
27
28
29
use self::mu::runtime::thread::MuThread;
use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use utils::Address;
use utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
30
use mu::linkutils::aot;
31
use mu::vm::handle;
qinsoon's avatar
qinsoon committed
32
use mu::linkutils;
qinsoon's avatar
qinsoon committed
33
34
35
36
37

use std::sync::Arc;

#[test]
fn test_global_access() {
qinsoon's avatar
qinsoon committed
38
39
    VM::start_logging_trace();

40
41
42
43
44
45
    let vm = Arc::new(VM::new());
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    global_access(&vm);

qinsoon's avatar
qinsoon committed
46
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
47

48
49
50
51
52
    {
        let func_id = vm.id_of("global_access");
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
53
54
55
56
57
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
58

59
60
        compiler.compile(&mut func_ver);
    }
qinsoon's avatar
qinsoon committed
61
    backend::emit_context(&vm);
qinsoon's avatar
qinsoon committed
62
}
63
64
65
66
67
68
69
70
71
72
73

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

    let vm = Arc::new(VM::new());
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    set_global_by_api(&vm);

qinsoon's avatar
qinsoon committed
74
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
75
    let func_id = vm.id_of("set_global_by_api");
76
77
78
79
80

    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
81
82
83
84
85
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
86
87
88
89
90

        compiler.compile(&mut func_ver);
    }

    // set global by api here
91
    {
qinsoon's avatar
qinsoon committed
92
        let global_id = vm.id_of("my_global");
93
94
        let global_handle = vm.handle_from_global(global_id);

qinsoon's avatar
qinsoon committed
95
        let uint64_10_handle = vm.handle_from_uint64(10, 64);
96

97
98
99
100
101
        debug!(
            "write {:?} to location {:?}",
            uint64_10_handle,
            global_handle
        );
102
        vm.handle_store(MemoryOrder::Relaxed, &global_handle, &uint64_10_handle);
103
    }
104
105

    // then emit context (global will be put into context.s
qinsoon's avatar
qinsoon committed
106
    vm.set_primordial_thread(func_id, true, vec![]);
107
108
109
    backend::emit_context(&vm);

    // link
110
111
    let executable =
        aot::link_primordial(vec![Mu("set_global_by_api")], "set_global_by_api_test", &vm);
qinsoon's avatar
qinsoon committed
112
    let output = linkutils::exec_path_nocheck(executable);
113

qinsoon's avatar
qinsoon committed
114
    assert!(output.status.code().is_some());
115

qinsoon's avatar
qinsoon committed
116
117
118
    let ret_code = output.status.code().unwrap();
    println!("return code: {} (i.e. the value set before)", ret_code);
    assert!(ret_code == 10);
119
120
121
122
123
124
}

fn set_global_by_api(vm: &VM) {
    typedef!    ((vm) int64      = mu_int(64));
    typedef!    ((vm) iref_int64 = mu_iref(int64));

qinsoon's avatar
qinsoon committed
125
    globaldef!  ((vm) <int64> my_global);
126
127
128
129
130
131
132
133

    funcsig!    ((vm) sig = () -> (int64));
    funcdecl!   ((vm) <sig> set_global_by_api);
    funcdef!    ((vm) <sig> set_global_by_api VERSION set_global_by_api_v1);

    // blk entry
    block!      ((vm, set_global_by_api_v1) blk_entry);
    ssa!        ((vm, set_global_by_api_v1) <int64> val);
qinsoon's avatar
qinsoon committed
134
    global!     ((vm, set_global_by_api_v1) blk_entry_my_global = my_global);
135
    inst!       ((vm, set_global_by_api_v1) blk_entry_load:
qinsoon's avatar
qinsoon committed
136
        val = LOAD blk_entry_my_global (is_ptr: false, order: MemoryOrder::SeqCst)
137
138
    );

qinsoon's avatar
qinsoon committed
139
140
141
    let blk_entry_exit = gen_ccall_exit(val.clone(), &mut set_global_by_api_v1, &vm);

    // won't execute this inst
142
143
144
145
146
    inst!       ((vm, set_global_by_api_v1) blk_entry_ret:
        RET (val)
    );

    define_block!   ((vm, set_global_by_api_v1) blk_entry() {
qinsoon's avatar
qinsoon committed
147
        blk_entry_load, blk_entry_exit, blk_entry_ret
148
149
150
151
152
    });

    define_func_ver!((vm) set_global_by_api_v1 (entry: blk_entry) {
        blk_entry
    });
153
}
qinsoon's avatar
qinsoon committed
154

155
156
157
158
159
160
161
162
163
164
#[test]
fn test_get_global_in_dylib() {
    VM::start_logging_trace();

    let vm = Arc::new(VM::new());
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    get_global_in_dylib(&vm);

qinsoon's avatar
qinsoon committed
165
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
166
167
168
169
170
171
    let func_id = vm.id_of("get_global_in_dylib");

    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
172
173
174
175
176
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

        compiler.compile(&mut func_ver);
    }

    // set global by api here
    {
        let global_id = vm.id_of("my_global");
        let global_handle = vm.handle_from_global(global_id);

        let uint64_10_handle = vm.handle_from_uint64(10, 64);

        debug!("write {:?} to location {:?}", uint64_10_handle, global_handle);
        vm.handle_store(MemoryOrder::Relaxed, &global_handle, &uint64_10_handle);
    }

    // then emit context (global will be put into context.s
    backend::emit_context(&vm);

    // link
qinsoon's avatar
qinsoon committed
196
197
    let libname = &linkutils::get_dylib_name("get_global_in_dylib");
    let libpath = linkutils::aot::link_dylib(vec![Mu("get_global_in_dylib")], libname, &vm);
198
    let lib = libloading::Library::new(libpath.as_os_str()).unwrap();
199
200

    unsafe {
201
202
        let get_global_in_dylib: libloading::Symbol<unsafe extern "C" fn() -> u64> =
            lib.get(b"get_global_in_dylib").unwrap();
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
        let res = get_global_in_dylib();

        println!("my_global = {}", res);
        assert_eq!(res, 10);
    }
}

fn get_global_in_dylib(vm: &VM) {
    typedef!    ((vm) int64      = mu_int(64));
    typedef!    ((vm) iref_int64 = mu_iref(int64));

    globaldef!  ((vm) <int64> my_global);

    funcsig!    ((vm) sig = () -> (int64));
    funcdecl!   ((vm) <sig> get_global_in_dylib);
    funcdef!    ((vm) <sig> get_global_in_dylib VERSION get_global_in_dylib_v1);

    // blk entry
    block!      ((vm, get_global_in_dylib_v1) blk_entry);
    ssa!        ((vm, get_global_in_dylib_v1) <int64> val);
    global!     ((vm, get_global_in_dylib_v1) blk_entry_my_global = my_global);
    inst!       ((vm, get_global_in_dylib_v1) blk_entry_load:
        val = LOAD blk_entry_my_global (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // won't execute this inst
    inst!       ((vm, get_global_in_dylib_v1) blk_entry_ret:
        RET (val)
    );

    define_block!   ((vm, get_global_in_dylib_v1) blk_entry() {
        blk_entry_load, blk_entry_ret
    });

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

qinsoon's avatar
qinsoon committed
242
243
244
245
246
247
248
249
250
251
#[test]
fn test_persist_linked_list() {
    VM::start_logging_trace();

    let vm = Arc::new(VM::new());
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    persist_linked_list(&vm);

qinsoon's avatar
qinsoon committed
252
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
253
254
255
256
257
258
    let func_id = vm.id_of("persist_linked_list");

    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
259
260
261
262
263
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
qinsoon's avatar
qinsoon committed
264
265
266
267
268

        compiler.compile(&mut func_ver);
    }

    // create a linked list by api
269
    const LINKED_LIST_SIZE: usize = 5;
qinsoon's avatar
qinsoon committed
270
271
    {
        let mut i = 0;
272
        let mut last_node: Option<Box<handle::APIHandle>> = None;
qinsoon's avatar
qinsoon committed
273
274
275
276
277

        let node_tyid = vm.id_of("node");

        while i < LINKED_LIST_SIZE {
            // new node
278
            let node_ref = vm.new_fixed(node_tyid);
279
            let node_iref = vm.handle_get_iref(&node_ref);
qinsoon's avatar
qinsoon committed
280
281

            // store i as payload
282
            let payload_iref = vm.handle_get_field_iref(&node_iref, 1); // payload is the 2nd field
qinsoon's avatar
qinsoon committed
283
            let int_handle = vm.handle_from_uint64(i as u64, 64);
284
            vm.handle_store(MemoryOrder::Relaxed, &payload_iref, &int_handle);
qinsoon's avatar
qinsoon committed
285
286

            // store last_node as next
287
            let next_iref = vm.handle_get_field_iref(&node_iref, 0);
qinsoon's avatar
qinsoon committed
288
289
            if last_node.is_some() {
                let handle = last_node.take().unwrap();
290
                vm.handle_store(MemoryOrder::Relaxed, &next_iref, &handle);
qinsoon's avatar
qinsoon committed
291
292
293
294
295
296
297
298
299
300
            }

            last_node = Some(node_ref);
            i += 1;
        }

        // store last_node in global
        let global_id = vm.id_of("my_global");
        let global_handle = vm.handle_from_global(global_id);

301
302
303
        vm.handle_store(
            MemoryOrder::Relaxed,
            &global_handle,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
304
            last_node.as_ref().unwrap(),
305
        );
qinsoon's avatar
qinsoon committed
306
307
308
    }

    // then emit context (global will be put into context.s
qinsoon's avatar
qinsoon committed
309
    vm.set_primordial_thread(func_id, true, vec![]);
qinsoon's avatar
qinsoon committed
310
311
312
    backend::emit_context(&vm);

    // link
313
314
315
    let executable = aot::link_primordial(
        vec![Mu("persist_linked_list")],
        "persist_linked_list_test",
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
316
        &vm,
317
    );
qinsoon's avatar
qinsoon committed
318
    let output = linkutils::exec_path_nocheck(executable);
qinsoon's avatar
qinsoon committed
319
320
321
322

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

    let ret_code = output.status.code().unwrap();
qinsoon's avatar
qinsoon committed
323
    println!("return code: {}", ret_code);
qinsoon's avatar
qinsoon committed
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
    assert!(ret_code == 10);
}

fn persist_linked_list(vm: &VM) {
    typedef!    ((vm) int1       = mu_int(1));
    typedef!    ((vm) int64      = mu_int(64));
    typedef!    ((vm) node       = mu_struct_placeholder());
    typedef!    ((vm) iref_node  = mu_iref(node));
    typedef!    ((vm) iref_int64 = mu_iref(int64));
    typedef!    ((vm) ref_node   = mu_ref(node));
    typedef!    ((vm) iref_ref_node = mu_iref(ref_node));
    typedef!    ((vm) mu_struct_put(node, ref_node, int64));

    globaldef!  ((vm) <ref_node> my_global);

    constdef!   ((vm) <int64>    int64_0       = Constant::Int(0));
    constdef!   ((vm) <ref_node> ref_node_null = Constant::NullRef);

    funcsig!    ((vm) sig = () -> (int64));
    funcdecl!   ((vm) <sig> persist_linked_list);
    funcdef!    ((vm) <sig> persist_linked_list VERSION persist_linked_list_v1);

    // --- blk entry ---
    block!      ((vm, persist_linked_list_v1) blk_entry);
    consta!     ((vm, persist_linked_list_v1) int64_0_local = int64_0);
    global!     ((vm, persist_linked_list_v1) blk_entry_my_global = my_global);

    // %head = LOAD %blk_entry_my_global
    ssa!        ((vm, persist_linked_list_v1) <ref_node> head);
    inst!       ((vm, persist_linked_list_v1) blk_entry_load:
        head = LOAD blk_entry_my_global (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // branch blk_loop_head (%head, 0)
    block!      ((vm, persist_linked_list_v1) blk_loop_head);
    inst!       ((vm, persist_linked_list_v1) blk_entry_branch:
        BRANCH blk_loop_head (head, int64_0_local)
    );

    define_block!   ((vm, persist_linked_list_v1) blk_entry() {
        blk_entry_load, blk_entry_branch
    });

    // --- blk loop_head ---
    ssa!        ((vm, persist_linked_list_v1) <ref_node> cursor);
    ssa!        ((vm, persist_linked_list_v1) <int64> sum);

    // %cond = CMP EQ cursor NULLREF
    ssa!        ((vm, persist_linked_list_v1) <int1> cond);
    consta!     ((vm, persist_linked_list_v1) ref_node_null_local = ref_node_null);
    inst!       ((vm, persist_linked_list_v1) blk_loop_head_cmp:
        cond = CMPOP (CmpOp::EQ) cursor ref_node_null_local
    );

qinsoon's avatar
qinsoon committed
378
    // BRANCH2 cond exit[sum] loop_body[cursor, sum]
qinsoon's avatar
qinsoon committed
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
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
    block!      ((vm, persist_linked_list_v1) blk_exit);
    block!      ((vm, persist_linked_list_v1) blk_loop_body);
    inst!       ((vm, persist_linked_list_v1) blk_loop_head_branch2:
        BRANCH2 (cond, sum, cursor)
            IF (OP 0)
            THEN blk_exit (vec![1]) WITH 0.1f32,
            ELSE blk_loop_body (vec![2, 1])
    );

    define_block!   ((vm, persist_linked_list_v1) blk_loop_head(cursor, sum) {
        blk_loop_head_cmp, blk_loop_head_branch2
    });

    // --- blk loop_body ---
    ssa!        ((vm, persist_linked_list_v1) <ref_node> body_cursor);
    ssa!        ((vm, persist_linked_list_v1) <int64>    body_sum);

    // %iref_cursor = GETIREF %body_cursor
    ssa!        ((vm, persist_linked_list_v1) <iref_node> iref_cursor);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_getiref:
        iref_cursor = GETIREF body_cursor
    );

    // %iref_payload = GETFIELDIREF iref_cursor 1
    ssa!        ((vm, persist_linked_list_v1) <iref_int64> iref_payload);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_getfieldiref:
        iref_payload = GETFIELDIREF iref_cursor (is_ptr: false, index: 1)
    );

    // %payload = LOAD %iref_payload
    ssa!        ((vm, persist_linked_list_v1) <int64> payload);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_load:
        payload = LOAD iref_payload (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // %body_sum2 = BINOP ADD body_sum payload
    ssa!        ((vm, persist_linked_list_v1) <int64> body_sum2);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_add:
        body_sum2 = BINOP (BinOp::Add) body_sum payload
    );

    // %iref_next = GETFIELDIREF iref_cursor 0
    ssa!        ((vm, persist_linked_list_v1) <iref_ref_node> iref_next);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_getfieldiref2:
        iref_next = GETFIELDIREF iref_cursor (is_ptr: false, index: 0)
    );

    // %next = LOAD %iref_next
    ssa!        ((vm, persist_linked_list_v1) <ref_node> next);
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_load2:
        next = LOAD iref_next (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // BRANCH blk_loop_head (next, body_sum2)
    inst!       ((vm, persist_linked_list_v1) blk_loop_body_branch:
        BRANCH blk_loop_head (next, body_sum2)
    );

    define_block!   ((vm, persist_linked_list_v1) blk_loop_body(body_cursor, body_sum){
        blk_loop_body_getiref,
        blk_loop_body_getfieldiref,
        blk_loop_body_load,
        blk_loop_body_add,
        blk_loop_body_getfieldiref2,
        blk_loop_body_load2,
        blk_loop_body_branch
    });

    // --- blk exit ---
    ssa!       ((vm, persist_linked_list_v1) <int64> res);

    let blk_exit_exit = gen_ccall_exit(res.clone(), &mut persist_linked_list_v1, &vm);

    inst!      ((vm, persist_linked_list_v1) blk_exit_ret:
        RET (res)
    );

    define_block!   ((vm, persist_linked_list_v1) blk_exit(res) {
        blk_exit_exit, blk_exit_ret
    });

    define_func_ver!((vm) persist_linked_list_v1 (entry: blk_entry) {
        blk_entry, blk_loop_head, blk_loop_body, blk_exit
    });
qinsoon's avatar
qinsoon committed
463
464
465
466
467
468
469
470
471
472
473
474
}

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

    let vm = Arc::new(VM::new());
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    persist_hybrid(&vm);

qinsoon's avatar
qinsoon committed
475
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
476
477
478
479
480
481
    let func_id = vm.id_of("persist_hybrid");

    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
482
483
484
485
486
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
qinsoon's avatar
qinsoon committed
487
488
489
490
491

        compiler.compile(&mut func_ver);
    }

    // create hybrid by api
492
    const HYBRID_LENGTH: usize = 5;
qinsoon's avatar
qinsoon committed
493
494
    {
        let hybrid_tyid = vm.id_of("hybrid");
495
496
        let hybrid_len = vm.handle_from_uint64(HYBRID_LENGTH as u64, 64);
        let hybrid = vm.new_hybrid(hybrid_tyid, &hybrid_len);
qinsoon's avatar
qinsoon committed
497

498
        let hybrid_iref = vm.handle_get_iref(&hybrid);
499
        let hybrid_varpart = vm.handle_get_var_part_iref(&hybrid_iref);
qinsoon's avatar
qinsoon committed
500
501
502
503
504
505

        // create int64 objects to fill var part
        let int64_tyid = vm.id_of("int64");

        for i in 0..HYBRID_LENGTH {
            // new node
506
            let node_ref = vm.new_fixed(int64_tyid);
507
            let node_iref = vm.handle_get_iref(&node_ref);
qinsoon's avatar
qinsoon committed
508
509
510

            // store i into node
            let int_handle = vm.handle_from_uint64(i as u64, 64);
511
            vm.handle_store(MemoryOrder::Relaxed, &node_iref, &int_handle);
qinsoon's avatar
qinsoon committed
512
513

            // store node to hybrid
514
515
            let hybrid_cell = vm.handle_shift_iref(&hybrid_varpart, &int_handle);
            vm.handle_store(MemoryOrder::Relaxed, &hybrid_cell, &node_ref);
qinsoon's avatar
qinsoon committed
516
517
518
519
520
521
        }

        // store last_node in global
        let global_id = vm.id_of("my_global");
        let global_handle = vm.handle_from_global(global_id);

522
        vm.handle_store(MemoryOrder::Relaxed, &global_handle, &hybrid);
qinsoon's avatar
qinsoon committed
523
524
525
    }

    // then emit context (global will be put into context.s
qinsoon's avatar
qinsoon committed
526
    vm.set_primordial_thread(func_id, true, vec![Constant::Int(HYBRID_LENGTH as u64)]);
qinsoon's avatar
qinsoon committed
527
528
529
530
    backend::emit_context(&vm);

    // link
    let executable = aot::link_primordial(vec![Mu("persist_hybrid")], "persist_hybrid_test", &vm);
qinsoon's avatar
qinsoon committed
531
    let output = linkutils::exec_path_nocheck(executable);
qinsoon's avatar
qinsoon committed
532
533
534
535
536
537
538
539
540
541
542
543
544

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

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

fn persist_hybrid(vm: &VM) {
    typedef!    ((vm) int1            = mu_int(1));
    typedef!    ((vm) int64           = mu_int(64));
    typedef!    ((vm) ref_int64       = mu_ref(int64));
    typedef!    ((vm) iref_int64      = mu_iref(int64));
545
    typedef!    ((vm) hybrid          = mu_hybrid()(ref_int64));
qinsoon's avatar
qinsoon committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
    typedef!    ((vm) ref_hybrid      = mu_ref(hybrid));
    typedef!    ((vm) iref_ref_hybrid = mu_iref(ref_hybrid));
    typedef!    ((vm) iref_ref_int64  = mu_iref(ref_int64));

    globaldef!  ((vm) <ref_hybrid> my_global);

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

    funcsig!    ((vm) sig = (int64) -> (int64));
    funcdecl!   ((vm) <sig> persist_hybrid);
    funcdef!    ((vm) <sig> persist_hybrid VERSION persist_hybrid_v1);

    // --- blk entry ---
    block!      ((vm, persist_hybrid_v1) blk_entry);
    ssa!        ((vm, persist_hybrid_v1) <int64> hybrid_len);
    global!     ((vm, persist_hybrid_v1) blk_entry_my_global = my_global);

    // %h = LOAD %blk_entry_my_global
    ssa!        ((vm, persist_hybrid_v1) <ref_hybrid> h);
    inst!       ((vm, persist_hybrid_v1) blk_entry_load:
        h = LOAD blk_entry_my_global (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // %var_h = GETVARPARTIREF %h
    ssa!        ((vm, persist_hybrid_v1) <iref_ref_int64> var_h);
    inst!       ((vm, persist_hybrid_v1) blk_entry_getvarpart:
        var_h = GETVARPARTIREF h (is_ptr: false)
    );

    // BRANCH loop_head (varpart: %var_h, sum: 0, i: 0, n: %hybrid_len)
    block!      ((vm, persist_hybrid_v1) blk_loop_head);
    consta!     ((vm, persist_hybrid_v1) int64_0_local = int64_0);
    inst!       ((vm, persist_hybrid_v1) blk_entry_branch:
        BRANCH blk_loop_head (var_h, int64_0_local, int64_0_local, hybrid_len)
    );

    define_block!   ((vm, persist_hybrid_v1) blk_entry(hybrid_len) {
        blk_entry_load, blk_entry_getvarpart, blk_entry_branch
    });

    // --- blk loop_head ---
    ssa!        ((vm, persist_hybrid_v1) <iref_ref_int64> head_varpart);
    ssa!        ((vm, persist_hybrid_v1) <int64> head_sum);
    ssa!        ((vm, persist_hybrid_v1) <int64> head_i);
    ssa!        ((vm, persist_hybrid_v1) <int64> head_n);

    // %cond = CMP ULT %i %n
    ssa!        ((vm, persist_hybrid_v1) <int1> cond);
    inst!       ((vm, persist_hybrid_v1) blk_loop_head_cmp:
        cond = CMPOP (CmpOp::ULT) head_i head_n
    );

    // BRANCH2 cond loop_body[varpart, sum, i, n] exit[sum]
    block!      ((vm, persist_hybrid_v1) blk_loop_body);
    block!      ((vm, persist_hybrid_v1) blk_exit);
    inst!       ((vm, persist_hybrid_v1) blk_loop_head_branch2:
        BRANCH2 (cond, head_varpart, head_sum, head_i, head_n)
            IF (OP 0)
            THEN blk_loop_body (vec![1, 2, 3, 4]) WITH 0.9f32,
            ELSE blk_exit (vec![2])
    );

    define_block!   ((vm, persist_hybrid_v1) blk_loop_head(head_varpart, head_sum, head_i, head_n) {
        blk_loop_head_cmp, blk_loop_head_branch2
    });

    // --- blk loop_body ---
    ssa!        ((vm, persist_hybrid_v1) <iref_ref_int64> varpart);
    ssa!        ((vm, persist_hybrid_v1) <int64> sum);
    ssa!        ((vm, persist_hybrid_v1) <int64> i);
    ssa!        ((vm, persist_hybrid_v1) <int64> n);

    // %cell = SHIFTIREF %varpart %i
    ssa!        ((vm, persist_hybrid_v1) <iref_ref_int64> cell);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_shiftiref:
        cell = SHIFTIREF varpart i (is_ptr: false)
    );

    // %int_obj = LOAD %cell
    ssa!        ((vm, persist_hybrid_v1) <ref_int64> int_obj);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_load_obj:
        int_obj = LOAD cell (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // %iref_int_obj = GETIREF %int_obj
    ssa!        ((vm, persist_hybrid_v1) <iref_int64> iref_int_obj);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_getiref:
        iref_int_obj = GETIREF int_obj
    );

    // %val = LOAD %iref_int_obj
    ssa!        ((vm, persist_hybrid_v1) <int64> val);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_load_val:
        val = LOAD iref_int_obj (is_ptr: false, order: MemoryOrder::SeqCst)
    );

    // %sum2 = BINOP ADD sum val
    ssa!        ((vm, persist_hybrid_v1) <int64> sum2);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_add:
        sum2 = BINOP (BinOp::Add) sum val
    );

    // %i2 = BINOP ADD %i 1
    ssa!        ((vm, persist_hybrid_v1) <int64> i2);
    consta!     ((vm, persist_hybrid_v1) int64_1_local = int64_1);
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_add2:
        i2 = BINOP (BinOp::Add) i int64_1_local
    );

    // BRANCH loop_head (%varpart, %sum2, %i2, %n)
    inst!       ((vm, persist_hybrid_v1) blk_loop_body_branch:
        BRANCH blk_loop_head (varpart, sum2, i2, n)
    );

    define_block!   ((vm, persist_hybrid_v1) blk_loop_body(varpart, sum, i, n) {
        blk_loop_body_shiftiref,
        blk_loop_body_load_obj,
        blk_loop_body_getiref,
        blk_loop_body_load_val,
        blk_loop_body_add,
        blk_loop_body_add2,
        blk_loop_body_branch
    });

    // --- blk exit ---
    ssa!        ((vm, persist_hybrid_v1) <int64> res);

    let blk_exit_exit = gen_ccall_exit(res.clone(), &mut persist_hybrid_v1, &vm);

    inst!       ((vm, persist_hybrid_v1) blk_exit_ret:
        RET (res)
    );

    define_block!   ((vm, persist_hybrid_v1) blk_exit(res) {
        blk_exit_exit, blk_exit_ret
    });

    define_func_ver! ((vm) persist_hybrid_v1 (entry: blk_entry) {
        blk_entry, blk_loop_head, blk_loop_body, blk_exit
    });
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
}

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

    let vm = Arc::new(VM::new_with_opts("init_mu --disable-inline"));
    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
    }
    persist_funcref(&vm);

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

    let func_ret42_id = vm.id_of("ret42");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_ret42_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
706
707
708
709
710
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
711
712
713
714
715
716
717
718
719

        compiler.compile(&mut func_ver);
    }

    let func_my_main_id = vm.id_of("my_main");
    {
        let funcs = vm.funcs().read().unwrap();
        let func = funcs.get(&func_my_main_id).unwrap().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
720
721
722
723
724
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743

        compiler.compile(&mut func_ver);
    }

    // store funcref to ret42 in the global
    {
        let global_id = vm.id_of("my_global");
        let global_handle = vm.handle_from_global(global_id);

        let func_ret42_handle = vm.handle_from_func(func_ret42_id);

        debug!("write {:?} to location {:?}", func_ret42_handle, global_handle);
        vm.handle_store(MemoryOrder::Relaxed, &global_handle, &func_ret42_handle);
    }

    let my_main_handle = vm.handle_from_func(func_my_main_id);

    // make boot image
    vm.make_boot_image(
744
745
746
747
748
749
750
751
        vec![func_ret42_id, func_my_main_id], // whitelist
        Some(&my_main_handle),
        None, // primoridal func, stack
        None, // threadlocal
        vec![],
        vec![], // sym fields/strings
        vec![],
        vec![], // reloc fields/strings
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
752
        "test_persist_funcref".to_string(),
753
754
755
756
757
758
759
760
761
762
    );

    // link
    let executable = {
        use std::path;
        let mut path = path::PathBuf::new();
        path.push(&vm.vm_options.flag_aot_emit_dir);
        path.push("test_persist_funcref");
        path
    };
qinsoon's avatar
qinsoon committed
763
    let output = linkutils::exec_path_nocheck(executable);
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834

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

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

fn persist_funcref(vm: &VM) {
    typedef!    ((vm) int64 = mu_int(64));
    constdef!   ((vm) <int64> int64_42 = Constant::Int(42));

    funcsig!    ((vm) ret42_sig = () -> (int64));
    funcdecl!   ((vm) <ret42_sig> ret42);
    funcdef!    ((vm) <ret42_sig> ret42 VERSION ret42_v1);

    typedef!    ((vm) funcref_to_ret42 = mu_funcref(ret42_sig));
    globaldef!  ((vm) <funcref_to_ret42> my_global);

    // ---ret42---
    {
        // blk entry
        block!      ((vm, ret42_v1) blk_entry);
        consta!     ((vm, ret42_v1) int64_42_local = int64_42);
        inst!       ((vm, ret42_v1) blk_entry_ret:
            RET (int64_42_local)
        );
        define_block!((vm, ret42_v1) blk_entry() {
            blk_entry_ret
        });

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

    // ---my_main---
    {
        funcsig!    ((vm) my_main_sig = () -> ());
        funcdecl!   ((vm) <my_main_sig> my_main);
        funcdef!    ((vm) <my_main_sig> my_main VERSION my_main_v1);

        // blk entry
        block!      ((vm, my_main_v1) blk_entry);
        global!     ((vm, my_main_v1) blk_entry_global = my_global);
        ssa!        ((vm, my_main_v1) <funcref_to_ret42> func);

        inst!       ((vm, my_main_v1) blk_entry_load:
            func = LOAD blk_entry_global (is_ptr: false, order: MemoryOrder::SeqCst)
        );

        ssa!        ((vm, my_main_v1) <int64> blk_entry_res);
        inst!       ((vm, my_main_v1) blk_entry_call:
            blk_entry_res = EXPRCALL (CallConvention::Mu, is_abort: false) func ()
        );

        let blk_entry_exit = gen_ccall_exit(blk_entry_res.clone(), &mut my_main_v1, &vm);

        inst!       ((vm, my_main_v1) blk_entry_ret:
            RET
        );

        define_block!   ((vm, my_main_v1) blk_entry() {
            blk_entry_load,
            blk_entry_call,
            blk_entry_exit,
            blk_entry_ret
        });

        define_func_ver!((vm) my_main_v1 (entry: blk_entry) {
            blk_entry
        });
    }
835
}