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 5.68 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
extern crate libloading;
16
extern crate log;
qinsoon's avatar
qinsoon committed
17
18
19
extern crate mu;

use self::mu::ast::inst::*;
20
21
use self::mu::ast::ir::*;
use self::mu::ast::types::*;
qinsoon's avatar
qinsoon committed
22
use self::mu::compiler::*;
23
24
use self::mu::runtime::thread::MuThread;
use self::mu::utils::Address;
25
use self::mu::utils::LinkedHashMap;
26
use self::mu::vm::*;
qinsoon's avatar
qinsoon committed
27

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

32
33
#[test]
fn test_allocation_fastpath() {
34
    build_and_run_test!(allocation_fastpath, allocation_fastpath_test1);
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
}

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});
82
83
84
85
86
87

    emit_test! ((vm)
        allocation_fastpath, allocation_fastpath_test1, allocation_fastpath_test1_v1,
        sig,
    );

88
89
90
    vm
}

qinsoon's avatar
qinsoon committed
91
92
#[test]
fn test_instruction_new() {
93
    build_and_run_test!(alloc_new, alloc_new_test1);
qinsoon's avatar
qinsoon committed
94
95
}

96
97
98
99
#[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)
100
101
// 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
102
103
104
105
106
fn test_instruction_new_on_cur_thread() {
    VM::start_logging_trace();

    // compile
    let vm = Arc::new(alloc_new());
qinsoon's avatar
qinsoon committed
107
    let compiler = Compiler::new(CompilerPolicy::default(), &vm);
108
109
110
111
112
    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();
113
114
115
116
117
        let mut func_ver = func_vers
            .get(&func.cur_ver.unwrap())
            .unwrap()
            .write()
            .unwrap();
118
119
120
121
122
123
124

        compiler.compile(&mut func_ver);
    }

    backend::emit_context(&vm);

    // link
qinsoon's avatar
qinsoon committed
125
    let libname = &linkutils::get_dylib_name("alloc_new_on_cur_thread");
qinsoon's avatar
qinsoon committed
126
    let dylib = aot::link_dylib(vec![Mu("alloc_new")], libname, &vm);
127
128
129
130
    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();

    unsafe {
        MuThread::current_thread_as_mu_thread(Address::zero(), vm.clone());
Javad Ebrahimian Amiri's avatar
Javad Ebrahimian Amiri committed
131
        let func: libloading::Symbol<unsafe extern "C" fn() -> ()> =
132
            lib.get(b"alloc_new").unwrap();
133
134
135
136
137

        func();
    }
}

qinsoon's avatar
qinsoon committed
138
139
140
#[allow(unused_variables)]
pub fn alloc_new() -> VM {
    let vm = VM::new();
141
142
143
144

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

146
147
148
149
150
151
    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);
152

qinsoon's avatar
qinsoon committed
153
    // %blk_0():
154
    block!      ((vm, alloc_new_v1) blk_0);
155

qinsoon's avatar
qinsoon committed
156
    // %a = NEW <@int64_t>
157
158
159
160
    ssa!        ((vm, alloc_new_v1) <ref_int64> blk_0_a);
    inst!       ((vm, alloc_new_v1) blk_0_new:
        blk_0_a = NEW <int64>
    );
161

qinsoon's avatar
qinsoon committed
162
    // %a_iref = GETIREF <@int_64> @a
163
164
165
166
    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
    );
167

168
    // STORE <@int_64> @a_iref @int_64_1
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    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
187
    });
188
189
190
191
192
193

    emit_test! ((vm)
        alloc_new, alloc_new_test1, alloc_new_test1_v1,
        alloc_new_sig,
    );

qinsoon's avatar
qinsoon committed
194
    vm
195
}