WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

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_pre_instsel.rs 7.34 KB
Newer Older
1
2
3
4
extern crate mu;
extern crate log;
extern crate simple_logger;

qinsoon's avatar
qinsoon committed
5
use common::*;
6
use test_ir::test_ir::factorial;
qinsoon's avatar
qinsoon committed
7
8
use test_ir::test_ir::sum;
use self::mu::ast::ir::*;
9
10
use self::mu::compiler::*;

11
12
use std::sync::Arc;

qinsoon's avatar
qinsoon committed
13
14
15
16
#[test]
fn test_use_count() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
17
    let vm_context = Arc::new(factorial());
qinsoon's avatar
qinsoon committed
18
19
    let compiler = Compiler::new(CompilerPolicy::new(
        vec![Box::new(passes::DefUse::new())]
20
    ), vm_context.clone());
qinsoon's avatar
qinsoon committed
21
    
qinsoon's avatar
qinsoon committed
22
    let funcs = vm_context.funcs().read().unwrap();
23
24
25
26
27
28
29
30
31
32
33
34
35
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
    
    compiler.compile(&mut factorial_func_ver);
    
    assert!(factorial_func_ver.context.get_value_by_tag("blk_0_n_3").unwrap().use_count.get() == 2, "blk_0_n_3 use should be 2");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_0_v48").unwrap().use_count.get() == 1, "blk_0_v48 use should be 1");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_2_v53").unwrap().use_count.get() == 1, "blk_2_v53 use should be 1");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_1_n_3").unwrap().use_count.get() == 2, "blk_1_n_3 use should be 2");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_1_v50").unwrap().use_count.get() == 1, "blk_1_v50 use should be 1");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_1_v51").unwrap().use_count.get() == 1, "blk_1_v51 use should be 1");
    assert!(factorial_func_ver.context.get_value_by_tag("blk_1_v52").unwrap().use_count.get() == 1, "blk_1_v52 use should be 1");
qinsoon's avatar
qinsoon committed
36
37
}

38
#[test]
39
fn test_build_tree() {
qinsoon's avatar
qinsoon committed
40
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
41
    
42
    let vm_context = Arc::new(factorial());
qinsoon's avatar
qinsoon committed
43
44
45
    let compiler = Compiler::new(CompilerPolicy::new(
        vec![Box::new(passes::DefUse::new()),
             Box::new(passes::TreeGen::new())]
46
    ), vm_context.clone());
47
    
qinsoon's avatar
qinsoon committed
48
    let funcs = vm_context.funcs().read().unwrap();
49
50
51
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
52
    
53
    compiler.compile(&mut factorial_func_ver);
54
55
56
}

#[test]
qinsoon's avatar
qinsoon committed
57
fn test_cfa_factorial() {
58
59
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
60
    let vm_context = Arc::new(factorial());
61
62
63
64
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new())
65
    ]), vm_context.clone());
66
    
qinsoon's avatar
qinsoon committed
67
    let funcs = vm_context.funcs().read().unwrap();
68
69
70
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
71
    
72
    compiler.compile(&mut factorial_func_ver);
qinsoon's avatar
qinsoon committed
73
74
    
    // assert cfa
75
    let content = factorial_func_ver.content.as_ref().unwrap();
qinsoon's avatar
qinsoon committed
76
77
78
    
    // blk_0: preds=[], succs=[blk_2, blk_1]
    let blk_0 = content.get_block("blk_0");
79
80
    assert_vector_no_order(&blk_0.control_flow.preds, &vec![]);
    assert_vector_no_order(&block_edges_into_vec(&blk_0.control_flow.succs), &vec!["blk_2", "blk_1"]);
qinsoon's avatar
qinsoon committed
81
82
83
    
    // blk_2: preds=[blk_0, blk_1], succs=[]
    let blk_2 = content.get_block("blk_2");
84
85
    assert_vector_no_order(&blk_2.control_flow.preds, &vec!["blk_0", "blk_1"]);
    assert_vector_no_order(&block_edges_into_vec(&blk_2.control_flow.succs), &vec![]);
qinsoon's avatar
qinsoon committed
86
87
88
    
    // blk_1: preds=[blk_0], succs=[blk_2]
    let blk_1 = content.get_block("blk_1");
89
90
    assert_vector_no_order(&blk_1.control_flow.preds, &vec!["blk_0"]);
    assert_vector_no_order(&block_edges_into_vec(&blk_1.control_flow.succs), &vec!["blk_2"]);
qinsoon's avatar
qinsoon committed
91
92
93
94
95
96
}

#[test]
fn test_cfa_sum() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
97
    let vm_context = Arc::new(sum());
qinsoon's avatar
qinsoon committed
98
99
100
101
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new())
102
    ]), vm_context.clone());
qinsoon's avatar
qinsoon committed
103
    
qinsoon's avatar
qinsoon committed
104
    let funcs = vm_context.funcs().read().unwrap();
105
106
107
    let sum_func = funcs.get("sum").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut sum_func_ver = func_vers.get(&(sum_func.fn_name, sum_func.cur_ver.unwrap())).unwrap().borrow_mut();
qinsoon's avatar
qinsoon committed
108
    
109
    compiler.compile(&mut sum_func_ver);
qinsoon's avatar
qinsoon committed
110
111
    
    // assert cfa
112
    let content = sum_func_ver.content.as_ref().unwrap();
qinsoon's avatar
qinsoon committed
113
114
115
    
    // entry: preds=[], succs=[head]
    let entry = content.get_block("entry");
116
117
    assert_vector_no_order(&entry.control_flow.preds, &vec![]);
    assert_vector_no_order(&block_edges_into_vec(&entry.control_flow.succs), &vec!["head"]);
qinsoon's avatar
qinsoon committed
118
119
120
    
    // head: preds=[entry, head], succs=[head, ret]
    let head = content.get_block("head");
121
122
    assert_vector_no_order(&head.control_flow.preds, &vec!["entry", "head"]);
    assert_vector_no_order(&block_edges_into_vec(&head.control_flow.succs), &vec!["ret", "head"]);
qinsoon's avatar
qinsoon committed
123
124
125
    
    // ret: preds=[head], succs=[]
    let ret = content.get_block("ret");
126
127
    assert_vector_no_order(&ret.control_flow.preds, &vec!["head"]);
    assert_vector_no_order(&block_edges_into_vec(&ret.control_flow.succs), &vec![]);
qinsoon's avatar
qinsoon committed
128
129
130
131
132
133
134
135
}

fn block_edges_into_vec(edges: &Vec<BlockEdge>) -> Vec<&str> {
    let mut ret = vec![];
    for edge in edges {
        ret.push(edge.target);
    }
    ret
136
137
138
139
140
141
}

#[test]
fn test_trace_factorial() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
142
    let vm_context = Arc::new(factorial());
143
144
145
146
147
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new()),
            Box::new(passes::TraceGen::new())
148
    ]), vm_context.clone());
149
    
qinsoon's avatar
qinsoon committed
150
    let funcs = vm_context.funcs().read().unwrap();
151
152
153
    let factorial_func = funcs.get("fac").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut factorial_func_ver = func_vers.get(&(factorial_func.fn_name, factorial_func.cur_ver.unwrap())).unwrap().borrow_mut();
154
    
155
    compiler.compile(&mut factorial_func_ver);
156
    
157
    assert_vector_ordered(factorial_func_ver.block_trace.as_ref().unwrap(), &vec!["blk_0", "blk_1", "blk_2"]);
158
159
160
161
162
163
}

#[test]
fn test_trace_sum() {
    simple_logger::init_with_level(log::LogLevel::Trace).ok();
    
164
    let vm_context = Arc::new(sum());
165
166
167
168
169
    let compiler = Compiler::new(CompilerPolicy::new(vec![
            Box::new(passes::DefUse::new()),
            Box::new(passes::TreeGen::new()),
            Box::new(passes::ControlFlowAnalysis::new()),
            Box::new(passes::TraceGen::new())
170
    ]), vm_context.clone());
171
    
qinsoon's avatar
qinsoon committed
172
    let funcs = vm_context.funcs().read().unwrap();
173
174
175
    let sum_func = funcs.get("sum").unwrap().borrow();
    let func_vers = vm_context.func_vers().read().unwrap();
    let mut sum_func_ver = func_vers.get(&(sum_func.fn_name, sum_func.cur_ver.unwrap())).unwrap().borrow_mut();
176
    
177
    compiler.compile(&mut sum_func_ver);
178
    
179
    assert_vector_ordered(sum_func_ver.block_trace.as_ref().unwrap(), &vec!["entry", "head", "ret"]);
180
}