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_alloc.rs 6.88 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
17
18
19
20
21
22
23
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::*;
24
25
use self::mu::runtime::thread::MuThread;
use self::mu::utils::Address;
26
use self::mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
27
28

use std::sync::Arc;
qinsoon's avatar
qinsoon committed
29
30
use self::mu::linkutils;
use self::mu::linkutils::aot;
qinsoon's avatar
qinsoon committed
31

32
33
34
35
36
37
38
39
40
41
42
43
44
#[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();
45
46
47
48
49
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
50
51
52
53

        compiler.compile(&mut func_ver);
    }

qinsoon's avatar
qinsoon committed
54
    vm.set_primordial_thread(func_id, true, vec![]);
55
56
    backend::emit_context(&vm);

57
58
59
    let executable = aot::link_primordial(
        vec!["allocation_fastpath".to_string()],
        "allocation_fastpath_test",
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
60
        &vm,
61
    );
qinsoon's avatar
qinsoon committed
62
    linkutils::exec_path(executable);
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
}

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
114
115
#[test]
fn test_instruction_new() {
qinsoon's avatar
qinsoon committed
116
    VM::start_logging_trace();
117

qinsoon's avatar
qinsoon committed
118
    let vm = Arc::new(alloc_new());
119

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

qinsoon's avatar
qinsoon committed
122
123
124
125
126
    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();
127
128
129
130
131
132
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();

qinsoon's avatar
qinsoon committed
133
134
        compiler.compile(&mut func_ver);
    }
135

qinsoon's avatar
qinsoon committed
136
    vm.set_primordial_thread(func_id, true, vec![]);
qinsoon's avatar
qinsoon committed
137
    backend::emit_context(&vm);
138

qinsoon's avatar
qinsoon committed
139
    let executable = aot::link_primordial(vec!["alloc_new".to_string()], "alloc_new_test", &vm);
qinsoon's avatar
qinsoon committed
140
    linkutils::exec_path(executable);
qinsoon's avatar
qinsoon committed
141
142
}

143
144
145
146
147
148
149
150
151
152
153
#[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
154
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
155
156
157
158
159
    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();
160
161
162
163
164
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
165
166
167
168
169
170
171

        compiler.compile(&mut func_ver);
    }

    backend::emit_context(&vm);

    // link
qinsoon's avatar
qinsoon committed
172
    let libname = &linkutils::get_dylib_name("alloc_new_on_cur_thread");
qinsoon's avatar
qinsoon committed
173
    let dylib = aot::link_dylib(vec![Mu("alloc_new")], libname, &vm);
174
175
176
177
    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();

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

        func();
    }
}

qinsoon's avatar
qinsoon committed
184
185
186
#[allow(unused_variables)]
pub fn alloc_new() -> VM {
    let vm = VM::new();
187
188
189
190

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) ref_int64  = mu_ref(int64));
    typedef!    ((vm) iref_int64 = mu_iref(int64));
191

192
193
194
195
196
197
    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);
198

qinsoon's avatar
qinsoon committed
199
    // %blk_0():
200
    block!      ((vm, alloc_new_v1) blk_0);
201

qinsoon's avatar
qinsoon committed
202
    // %a = NEW <@int64_t>
203
204
205
206
    ssa!        ((vm, alloc_new_v1) <ref_int64> blk_0_a);
    inst!       ((vm, alloc_new_v1) blk_0_new:
        blk_0_a = NEW <int64>
    );
207

qinsoon's avatar
qinsoon committed
208
    // %a_iref = GETIREF <@int_64> @a
209
210
211
212
    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
    );
213

214
    // STORE <@int_64> @a_iref @int_64_1
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    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
233
    });
234

qinsoon's avatar
qinsoon committed
235
    vm
236
}