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

test_exception.rs 9.65 KB
Newer Older
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
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 aot;

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

#[test]
fn test_exception_simple_throw_catch() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
    let vm = Arc::new(simple_throw_catch());
    
    let compiler = Compiler::new(CompilerPolicy::default(), vm.clone());
    
    let func_throw = vm.id_of("throw_exception");
    let func_catch = vm.id_of("catch_exception");    
    {
        let funcs = vm.funcs().read().unwrap();
        let func_vers = vm.func_vers().read().unwrap();
        
        {
            let func = funcs.get(&func_throw).unwrap().read().unwrap();
            let mut func_ver = func_vers.get(&func.cur_ver.unwrap()).unwrap().write().unwrap();
    
            compiler.compile(&mut func_ver);
        }
        {
            let func = funcs.get(&func_catch).unwrap().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_catch, vec![]);
    backend::emit_context(&vm);
    
    let executable = aot::link_primordial(vec![Mu("throw_exception"), Mu("catch_exception")], "simple_throw_catch_test");
    aot::execute(executable);
}

fn simple_throw_catch() -> VM {
    let vm = VM::new();
    
    // .typedef @int64 = int<64>
    // .typedef @ref_int64 = ref<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_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());
    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());    
    
    // .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());    
    
    create_throw_exception_func(&vm);
    create_catch_exception_func(&vm);
    
    vm
}

fn create_catch_exception_func (vm: &VM) {
    // .typedef @funcref_throw_exception <@throw_exception_sig>
    let throw_exception_sig = vm.get_func_sig(vm.id_of("throw_exception_sig"));
    let throw_exception_id = vm.id_of("throw_exception");
    let type_funcref_throw_exception = vm.declare_type(vm.next_id(), MuType_::funcref(throw_exception_sig));
    // .const @throw_exception_func
    let const_funcref_throw_exception = vm.declare_const(vm.next_id(), type_funcref_throw_exception, Constant::FuncRef(throw_exception_id)); 
    
    // .funcsig @catch_exception_sig = () -> ()
    let func_sig = vm.declare_func_sig(vm.next_id(), vec![], vec![]);
    vm.set_name(func_sig.as_entity(), Mu("catch_exception_sig"));
    
    // .funcdecl @catch_exception <@catch_exception_sig>
    let func = MuFunction::new(vm.next_id(), func_sig.clone());
    vm.set_name(func.as_entity(), Mu("catch_exception"));
    let func_id = func.id();
    vm.declare_func(func);
    
    // .funcdef @catch_exception VERSION @v1 <@catch_exception_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(), Mu("blk_0"));
    
    let blk_normal_cont_id = vm.next_id();
    let blk_exn_cont_id = vm.next_id();
    
    let blk_0_throw = func_ver.new_constant(vm.next_id(), const_funcref_throw_exception.clone());
    let blk_0_term = func_ver.new_inst(vm.next_id(), Instruction {
        value: None,
        ops: RwLock::new(vec![blk_0_throw]),
        v: Instruction_::Call {
            data: CallData {
                func: 0,
                args: vec![],
                convention: CallConvention::Mu
            },
            resume: ResumptionData {
                normal_dest: Destination {
                    target: blk_normal_cont_id,
                    args: vec![]
                },
                exn_dest: Destination {
                    target: blk_exn_cont_id,
                    args: vec![]
                }
            }
        }
    });
    
    let blk_0_content = BlockContent {
        args: vec![],
        exn_arg: None,
        body: vec![blk_0_term],
        keepalives: None
    };
    blk_0.content = Some(blk_0_content);
    
    // %blk_normal_cont():
    let mut blk_normal_cont = Block::new(blk_normal_cont_id);
    vm.set_name(blk_normal_cont.as_entity(), Mu("blk_normal_cont"));
    let blk_normal_cont_thread_exit = func_ver.new_inst(vm.next_id(), Instruction {
        value: None,
        ops: RwLock::new(vec![]),
        v: Instruction_::ThreadExit
    });
    blk_normal_cont.content = Some(BlockContent {
        args: vec![],
        exn_arg: None,
        body: vec![blk_normal_cont_thread_exit],
        keepalives: None
    });
    
    // %blk_exn_cont() %EXN:
    let mut blk_exn_cont = Block::new(blk_exn_cont_id);
    vm.set_name(blk_exn_cont.as_entity(), Mu("blk_exn_cont"));
160
161
162
    let type_ref_int64 = vm.get_type(vm.id_of("ref_int64"));    
    let blk_exn_cont_exception_arg = func_ver.new_ssa(vm.next_id(), type_ref_int64.clone());
    vm.set_name(blk_exn_cont_exception_arg.as_entity(), Mu("blk_0_exception_arg"));
163
164
165
166
167
168
169
    let blk_exn_cont_thread_exit = func_ver.new_inst(vm.next_id(), Instruction {
        value: None,
        ops: RwLock::new(vec![]),
        v: Instruction_::ThreadExit
    });
    blk_exn_cont.content = Some(BlockContent {
        args: vec![],
170
        exn_arg: Some(blk_exn_cont_exception_arg.clone_value()),
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
242
243
244
245
246
247
248
        body: vec![blk_exn_cont_thread_exit],
        keepalives: None
    });    
    
    func_ver.define(FunctionContent{
        entry: blk_0.id(),
        blocks: {
            let mut ret = HashMap::new();
            ret.insert(blk_0.id(), blk_0);
            ret.insert(blk_normal_cont.id(), blk_normal_cont);
            ret.insert(blk_exn_cont.id(), blk_exn_cont);
            ret
        }
    });
    
    vm.define_func_version(func_ver);
}

fn create_throw_exception_func (vm: &VM) {
    let type_int64 = vm.get_type(vm.id_of("int64"));
    let type_ref_int64 = vm.get_type(vm.id_of("ref_int64"));
    let type_iref_int64 = vm.get_type(vm.id_of("iref_int64"));
    
    // .funcsig @throw_exception = () -> ()
    let func_sig = vm.declare_func_sig(vm.next_id(), vec![], vec![]);
    vm.set_name(func_sig.as_entity(), Mu("throw_exception_sig"));
    
    // .funcdecl @catch_exception <@throw_exception>
    let func = MuFunction::new(vm.next_id(), func_sig.clone());
    vm.set_name(func.as_entity(), Mu("throw_exception"));
    let func_id = func.id();
    vm.declare_func(func);
    
    // .funcdef @catch_exception VERSION @v1 <@throw_exception>
    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(), Mu("blk_0"));
    
    // %exception_obj = NEW <@int64>
    let blk_0_exception_obj = func_ver.new_ssa(vm.next_id(), type_ref_int64.clone());
    vm.set_name(blk_0_exception_obj.as_entity(), Mu("blk_0_exception_obj"));
    let blk_0_inst0 = func_ver.new_inst(vm.next_id(), Instruction {
            value: Some(vec![blk_0_exception_obj.clone_value()]),
            ops: RwLock::new(vec![]),
            v: Instruction_::New(type_ref_int64.clone())
    });
    
    // %exception_obj_iref = GETIREF <@int64> %exception_obj
    let blk_0_exception_obj_iref = func_ver.new_ssa(vm.next_id(), type_iref_int64.clone());
    vm.set_name(blk_0_exception_obj_iref.as_entity(), Mu("blk_0_exception_obj_iref"));
    let blk_0_inst1 = func_ver.new_inst(vm.next_id(), Instruction {
            value: Some(vec![blk_0_exception_obj_iref.clone_value()]),
            ops: RwLock::new(vec![blk_0_exception_obj.clone()]),
            v: Instruction_::GetIRef(0)
     });
    
    // STORE <@int64> %exception_obj_iref @int64_1
    let const_int64_1 = vm.get_const(vm.id_of("int64_1"));
    let blk_0_const_int64_1 = func_ver.new_constant(vm.next_id(), const_int64_1);
    let blk_0_inst2 = func_ver.new_inst(vm.next_id(), Instruction {
        value: None,
        ops: RwLock::new(vec![blk_0_exception_obj_iref.clone(), blk_0_const_int64_1.clone()]),
        v: Instruction_::Store {
            is_ptr: false,
            order: MemoryOrder::Relaxed,
            mem_loc: 0,
            value: 1
        }
    });
    
    let blk_0_term = func_ver.new_inst(vm.next_id(), Instruction {
        value: None,
        ops: RwLock::new(vec![blk_0_exception_obj.clone()]),
        v: Instruction_::Throw(0)
    });
    
249
250
251
252
253
254
255
256
    let blk_0_content = BlockContent {
        args: vec![],
        exn_arg: None,
        body: vec![blk_0_inst0, blk_0_inst1, blk_0_inst2, blk_0_term],
        keepalives: None
    };
    blk_0.content = Some(blk_0_content);
    
257
258
259
260
261
262
263
264
265
266
267
    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);
}