To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

test_alloc.rs 6.14 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
5
6
7
8
9
extern crate log;
extern crate libloading;
extern crate mu;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::vm::*;
use self::mu::compiler::*;
10
11
use self::mu::runtime::thread::MuThread;
use self::mu::utils::Address;
12
use self::mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
13
14

use std::sync::Arc;
15
use self::mu::testutil;
16
use self::mu::testutil::aot;
qinsoon's avatar
qinsoon committed
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#[test]
fn test_allocation_fastpath() {
    VM::start_logging_trace();

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

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

    let func_id = vm.id_of("allocation_fastpath");
    {
        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);
    }

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

    let executable = aot::link_primordial(vec!["allocation_fastpath".to_string()], "allocation_fastpath_test", &vm);
    aot::execute(executable);
}

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

    typedef!    ((vm) int1         = mu_int(1));
    typedef!    ((vm) int64        = mu_int(64));
    typedef!    ((vm) ref_int64    = mu_ref(int64));
    typedef!    ((vm) struct_t     = mu_struct(int64, int64, ref_int64));
    typedef!    ((vm) ref_struct_t = mu_ref(struct_t));

    funcsig!    ((vm) sig = () -> ());
    funcdecl!   ((vm) <sig> allocation_fastpath);
    funcdef!    ((vm) <sig> allocation_fastpath VERSION allocation_fastpath_v1);

    block!      ((vm, allocation_fastpath_v1) blk_entry);

    // a = NEW <struct_t>
    ssa!        ((vm, allocation_fastpath_v1) <ref_struct_t> a);
    inst!       ((vm, allocation_fastpath_v1) blk_entry_new1:
        a = NEW <struct_t>
    );

    inst!       ((vm, allocation_fastpath_v1) blk_entry_print1:
        PRINTHEX a
    );

    ssa!        ((vm, allocation_fastpath_v1) <ref_struct_t> b);
    inst!       ((vm, allocation_fastpath_v1) blk_entry_new2:
        b = NEW <struct_t>
    );

    inst!       ((vm, allocation_fastpath_v1) blk_entry_print2:
        PRINTHEX b
    );

    inst!       ((vm, allocation_fastpath_v1) blk_entry_threadexit:
        THREADEXIT
    );

    define_block!   ((vm, allocation_fastpath_v1) blk_entry() {
        blk_entry_new1, blk_entry_print1,
        blk_entry_new2, blk_entry_print2,
        blk_entry_threadexit
    });

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

    vm
}

qinsoon's avatar
qinsoon committed
92
93
#[test]
fn test_instruction_new() {
qinsoon's avatar
qinsoon committed
94
    VM::start_logging_trace();
qinsoon's avatar
qinsoon committed
95
96
97
    
    let vm = Arc::new(alloc_new());
    
qinsoon's avatar
qinsoon committed
98
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
qinsoon's avatar
qinsoon committed
99
100
101
102
103
104
105
106
107
108
109
    
    let func_id = vm.id_of("alloc_new");
    {
        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
110
    vm.make_primordial_thread(func_id, true, vec![]);
qinsoon's avatar
qinsoon committed
111
112
    backend::emit_context(&vm);
    
qinsoon's avatar
qinsoon committed
113
    let executable = aot::link_primordial(vec!["alloc_new".to_string()], "alloc_new_test", &vm);
114
    aot::execute(executable);
qinsoon's avatar
qinsoon committed
115
116
}

117
118
119
120
121
122
123
124
125
126
127
#[allow(dead_code)]
//#[test]
// The test won't work, since the generated dylib wants to use 'alloc_slow'.
// but in current process, there is no 'alloc_slow' (rust mangles it)
// The solution would be starting mu vm with libmu.so, then create IR from there.
// test_jit should contains a test for it. So I do not test it here
fn test_instruction_new_on_cur_thread() {
    VM::start_logging_trace();

    // compile
    let vm = Arc::new(alloc_new());
qinsoon's avatar
qinsoon committed
128
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
129
130
131
132
133
134
135
136
137
138
139
140
141
    let func_id = vm.id_of("alloc_new");
    {
        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);
    }

    backend::emit_context(&vm);

    // link
142
    let libname = &testutil::get_dylib_name("alloc_new_on_cur_thread");
qinsoon's avatar
qinsoon committed
143
    let dylib = aot::link_dylib(vec![Mu("alloc_new")], libname, &vm);
144
145
146
147
148
149
150
151
152
153
    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();

    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
        let func : libloading::Symbol<unsafe extern fn() -> ()> = lib.get(b"alloc_new").unwrap();

        func();
    }
}

qinsoon's avatar
qinsoon committed
154
155
156
#[allow(unused_variables)]
pub fn alloc_new() -> VM {
    let vm = VM::new();
157
158
159
160

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) ref_int64  = mu_ref(int64));
    typedef!    ((vm) iref_int64 = mu_iref(int64));
qinsoon's avatar
qinsoon committed
161
    
162
163
164
165
166
167
    constdef!   ((vm) <int64> int64_0 = Constant::Int(0));
    constdef!   ((vm) <int64> int64_1 = Constant::Int(1));

    funcsig!    ((vm) alloc_new_sig = () -> (int64));
    funcdecl!   ((vm) <alloc_new_sig> alloc_new);
    funcdef!    ((vm) <alloc_new_sig> alloc_new VERSION alloc_new_v1);
qinsoon's avatar
qinsoon committed
168
169
    
    // %blk_0():
170
    block!      ((vm, alloc_new_v1) blk_0);
qinsoon's avatar
qinsoon committed
171
172
    
    // %a = NEW <@int64_t>
173
174
175
176
    ssa!        ((vm, alloc_new_v1) <ref_int64> blk_0_a);
    inst!       ((vm, alloc_new_v1) blk_0_new:
        blk_0_a = NEW <int64>
    );
qinsoon's avatar
qinsoon committed
177
178
    
    // %a_iref = GETIREF <@int_64> @a
179
180
181
182
    ssa!        ((vm, alloc_new_v1) <iref_int64> blk_0_a_iref);
    inst!       ((vm, alloc_new_v1) blk_0_getiref:
        blk_0_a_iref = GETIREF blk_0_a
    );
qinsoon's avatar
qinsoon committed
183
    
184
    // STORE <@int_64> @a_iref @int_64_1
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    consta!     ((vm, alloc_new_v1) int64_1_local = int64_1);
    inst!       ((vm, alloc_new_v1) blk_0_store:
        STORE blk_0_a_iref int64_1_local (is_ptr: false, order: MemoryOrder::Relaxed)
    );

    inst!       ((vm, alloc_new_v1) blk_0_term:
        THREADEXIT
    );

    define_block!((vm, alloc_new_v1) blk_0() {
        blk_0_new,
        blk_0_getiref,
        blk_0_store,
        blk_0_term
    });

    define_func_ver!((vm) alloc_new_v1 (entry: blk_0) {
        blk_0
qinsoon's avatar
qinsoon committed
203
204
205
    });
    
    vm
206
}