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

test_alloc.rs 6.09 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
extern crate log;
extern crate simple_logger;
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::*;
use self::mu::runtime::thread;
use self::mu::runtime::thread::MuThread;
use self::mu::runtime::mm;
use self::mu::utils::ByteSize;

use std::sync::Arc;
use std::sync::RwLock;
use std::collections::HashMap;
use aot;

#[test]
fn test_instruction_new() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    let vm = Arc::new(alloc_new());
    
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
    
    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);
    
    let dylib = aot::link_dylib(vec![Mu("fac")], "liballoc_new.dylib");
    
    let lib = libloading::Library::new(dylib.as_os_str()).unwrap();
    unsafe {
        let alloc_new : libloading::Symbol<unsafe extern fn() -> u64> = lib.get(b"alloc_new").unwrap();
        
        // before invoking it, we need to disguise current thread as mu thread (having an allocator)
        fake_mutator_for_cur_thread(&vm);
        
        let ret = alloc_new();
        println!("return value from alloc_new is {}", ret);
        assert!(ret == 1);
    }
}

fn fake_mutator_for_cur_thread(vm: &VM) {
    // init gc
    const IMMIX_SPACE_SIZE : ByteSize = 500 << 20;
    const LO_SPACE_SIZE    : ByteSize = 500 << 20;
    mm::gc_init(IMMIX_SPACE_SIZE, LO_SPACE_SIZE, 8);
    
    let mut mutator = mm::new_mutator();
    
    let muthread : *mut MuThread = Box::into_raw(Box::new(MuThread::fake_thread(vm.next_id(), mutator)));
    
    unsafe {thread::set_thread_local(muthread)};
}

#[allow(unused_variables)]
pub fn alloc_new() -> VM {
    let vm = VM::new();
    
    // .typedef @int64 = int<64>
    // .typedef @iref_int64 = iref<int<64>>
    let type_def_int64 = vm.declare_type(vm.next_id(), MuType_::int(64));
    vm.set_name(type_def_int64.as_entity(), "int64".to_string());
    let type_def_iref_int64 = vm.declare_type(vm.next_id(), MuType_::iref(type_def_int64.clone()));
    vm.set_name(type_def_iref_int64.as_entity(), "iref_int64".to_string());
    let type_def_ref_int64 = vm.declare_type(vm.next_id(), MuType_::muref(type_def_int64.clone()));
    vm.set_name(type_def_ref_int64.as_entity(), "ref_int64".to_string());
    
    // .const @int_64_0 <@int_64> = 0
    // .const @int_64_1 <@int_64> = 1
    let const_def_int64_0 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(0));
    vm.set_name(const_def_int64_0.as_entity(), "int64_0".to_string());
    let const_def_int64_1 = vm.declare_const(vm.next_id(), type_def_int64.clone(), Constant::Int(1));
    vm.set_name(const_def_int64_1.as_entity(), "int64_1".to_string());
    
    // .funcsig @alloc_new_sig = () -> (@int64)
    let func_sig = vm.declare_func_sig(vm.next_id(), vec![], vec![type_def_int64.clone()]);
    vm.set_name(func_sig.as_entity(), "alloc_new_sig".to_string());

    // .funcdecl @alloc_new <@alloc_new_sig>
    let func = MuFunction::new(vm.next_id(), func_sig.clone());
    vm.set_name(func.as_entity(), "alloc_new".to_string());
    let func_id = func.id();
    vm.declare_func(func);
    
    // .funcdef @alloc VERSION @v1 <@alloc_new_sig>
    let mut func_ver = MuFunctionVersion::new(vm.next_id(), func_id, func_sig.clone());
    
    // %blk_0():
    let mut blk_0 = Block::new(vm.next_id());
    vm.set_name(blk_0.as_entity(), "blk_0".to_string());
    
    // %a = NEW <@int64_t>
    let blk_0_a = func_ver.new_ssa(vm.next_id(), type_def_ref_int64.clone());
    vm.set_name(blk_0_a.as_entity(), "blk_0_a".to_string());
    let blk_0_inst0 = func_ver.new_inst(vm.next_id(), Instruction{
        value: Some(vec![blk_0_a.clone_value()]),
        ops: RwLock::new(vec![]),
        v: Instruction_::New(type_def_int64.clone())
    });
    
    // %a_iref = GETIREF <@int_64> @a
    let blk_0_a_iref = func_ver.new_ssa(vm.next_id(), type_def_iref_int64.clone());
    vm.set_name(blk_0_a.as_entity(), "blk_0_a_iref".to_string());
    let blk_0_inst1 = func_ver.new_inst(vm.next_id(), Instruction{
        value: Some(vec![blk_0_a_iref.clone_value()]),
        ops: RwLock::new(vec![blk_0_a.clone()]),
        v: Instruction_::GetIRef(0)
    });
    
    // STORE <@int_64> @a @int_64_1
    let blk_0_const_int64_1 = func_ver.new_constant(vm.next_id(), const_def_int64_1.clone());
    let blk_0_inst2 = func_ver.new_inst(vm.next_id(), Instruction{
        value: None,
        ops: RwLock::new(vec![blk_0_a.clone(), blk_0_const_int64_1.clone()]),
        v: Instruction_::Store{
            is_ptr: false,
            order: MemoryOrder::Relaxed,
            mem_loc: 0,
            value: 1
        }
    });    
        
    // %x = LOAD <@int_64> @a_iref
    let blk_0_x = func_ver.new_ssa(vm.next_id(), type_def_int64.clone());
    vm.set_name(blk_0_x.as_entity(), "blk_0_x".to_string());
    let blk_0_inst3 = func_ver.new_inst(vm.next_id(), Instruction{
        value: Some(vec![blk_0_x.clone_value()]),
        ops: RwLock::new(vec![blk_0_a_iref.clone()]),
        v: Instruction_::Load{
            is_ptr: false,
            order: MemoryOrder::Relaxed,
            mem_loc: 0
        }
    });
    
    let blk_0_term = func_ver.new_inst(vm.next_id(), Instruction{
        value: None,
        ops: RwLock::new(vec![blk_0_x.clone()]),
        v: Instruction_::Return(vec![0])
    });
    
    let blk_0_content = BlockContent {
        args: vec![],
        body: vec![blk_0_inst0, blk_0_inst1, blk_0_inst2, blk_0_term],
        keepalives: None
    };
    blk_0.content = Some(blk_0_content);
    
    func_ver.define(FunctionContent{
        entry: blk_0.id(),
        blocks: {
            let mut ret = HashMap::new();
            ret.insert(blk_0.id(), blk_0);
            ret
        }
    });
    
    vm.define_func_version(func_ver);
    
    vm
}