To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

handle.rs 8.19 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.

15
use ast::ir::*;
qinsoon's avatar
qinsoon committed
16
17
use ast::ptr::*;
use ast::types::*;
18
19
use utils::BitSize;
use utils::Address;
qinsoon's avatar
qinsoon committed
20
21
use std::fmt;

qinsoon's avatar
qinsoon committed
22
23
24
/// APIHandle represents the opaque handle type that the client uses to
/// communicate with Mu. Handles can refer to values, functions, signatures,
/// etc that client can inspect/query from the VM.
qinsoon's avatar
qinsoon committed
25
#[derive(Clone)]
26
27
pub struct APIHandle {
    pub id: MuID,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
28
    pub v: APIHandleValue,
29
30
}

qinsoon's avatar
qinsoon committed
31
32
33
34
35
36
37
/// when we returning an API handle to the client, we create a Box<APIHandle>,
/// then api_impl will turn it into a raw pointer, and pass the pointer the the client.
/// Thus Rust allocates the handle, but will not reclaim it. When the client explicitly
/// deletes a value, we turn the pointer back to a box type, and let Rust drop it.
pub type APIHandleResult = Box<APIHandle>;
/// when client pass a handle (*const APIHandle) to the VM, we treat it as a reference
/// to APIHandle.
38
pub type APIHandleArg<'a> = &'a APIHandle;
qinsoon's avatar
qinsoon committed
39

qinsoon's avatar
qinsoon committed
40
41
42
43
44
45
46
47
48
49
50
51
52
impl fmt::Display for APIHandle {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl fmt::Debug for APIHandle {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "Handle#{}=[{:?}]", self.id, self.v)
    }
}

#[derive(Clone)]
53
pub enum APIHandleValue {
qinsoon's avatar
qinsoon committed
54
    /// (int value, bit length)
55
    Int(u64, BitSize),
qinsoon's avatar
qinsoon committed
56
    /// float value
57
    Float(f32),
qinsoon's avatar
qinsoon committed
58
    /// double value
59
    Double(f64),
qinsoon's avatar
qinsoon committed
60
    /// unsafe pointer (type, address)
61
    UPtr(P<MuType>, Address), // uptr<T>
qinsoon's avatar
qinsoon committed
62
    /// unsafe function pointer (type, address)
63
    UFP(P<MuType>, Address), // ufuncptr<sig>
64
65

    // SeqValue
qinsoon's avatar
qinsoon committed
66
    /// struct value (a vector of field values)
67
    Struct(Vec<APIHandleValue>),
qinsoon's avatar
qinsoon committed
68
    /// array value  (a vector of element values)
69
    Array(Vec<APIHandleValue>),
qinsoon's avatar
qinsoon committed
70
    /// vector value (a vector of element values)
71
72
73
    Vector(Vec<APIHandleValue>),

    // GenRef
qinsoon's avatar
qinsoon committed
74
    /// reference value (type, address)
75
    Ref(P<MuType>, Address),
qinsoon's avatar
qinsoon committed
76
    /// internal reference value (type, address)
qinsoon's avatar
qinsoon committed
77
    IRef(P<MuType>, Address),
qinsoon's avatar
qinsoon committed
78
    /// tagref value (stored as 64-bit integers)
79
    TagRef64(u64),
qinsoon's avatar
qinsoon committed
80
    /// function reference (as ID)
81
    FuncRef(MuID),
qinsoon's avatar
qinsoon committed
82
    /// Mu thread reference
83
    ThreadRef,
qinsoon's avatar
qinsoon committed
84
    /// Mu stack reference
85
    StackRef,
qinsoon's avatar
qinsoon committed
86
87
    /// frame cursor reference
    FCRef,
88
89

    // GenRef->IR
qinsoon's avatar
qinsoon committed
90
91
    /// Mu bundle
    //  TODO: unused
92
93
94
    Bundle,

    // GenRef->IR->Child
qinsoon's avatar
qinsoon committed
95
    /// Mu type (as ID)
96
    Type(MuID),
qinsoon's avatar
qinsoon committed
97
    /// Mu signature (as ID)
98
    FuncSig(MuID),
qinsoon's avatar
qinsoon committed
99
    /// Mu function version (as ID)
100
    FuncVer(MuID),
qinsoon's avatar
qinsoon committed
101
102
    /// basic block
    //  TODO: unused
103
    BB,
qinsoon's avatar
qinsoon committed
104
105
    /// instruction
    //  TODO: unused
106
107
108
    Inst,

    // GenRef->IR->Child->Var->Global
qinsoon's avatar
qinsoon committed
109
    /// global cell (as ID)
110
    Global(MuID),
qinsoon's avatar
qinsoon committed
111
112
    /// exposed function
    //  TODO: unused
113
114
115
    ExpFunc,

    // GenRef->IR->Child->Var->Local
qinsoon's avatar
qinsoon committed
116
117
    /// normal parameter
    //  TODO: unused
118
    NorParam,
qinsoon's avatar
qinsoon committed
119
120
    /// exceptional parameter
    //  TODO: unused
121
    ExcParam,
qinsoon's avatar
qinsoon committed
122
123
    /// instruction result value
    //  TODO: unused
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
124
    InstRes,
125
126
}

qinsoon's avatar
qinsoon committed
127
128
129
130
131
132
133
134
135
136
impl fmt::Display for APIHandleValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl fmt::Debug for APIHandleValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::APIHandleValue::*;
        match self {
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
            &Int(val, len) => write!(f, "{} as int<{}>", val, len),
            &Float(val) => write!(f, "{}", val),
            &Double(val) => write!(f, "{}", val),
            &UPtr(ref ty, addr) => write!(f, "uptr<{}> to {}", ty, addr),
            &UFP(ref sig, addr) => write!(f, "ufp<{}> to {}", sig, addr),
            &Struct(ref vec) => write!(f, "struct{{{:?}}}", vec),
            &Array(ref vec) => write!(f, "array{{{:?}}}", vec),
            &Vector(ref vec) => write!(f, "vector{{{:?}}}", vec),
            &Ref(ref ty, addr) => write!(f, "ref<{}> to {}", ty, addr),
            &IRef(ref ty, addr) => write!(f, "iref<{}> to {}", ty, addr),
            &TagRef64(val) => write!(f, "tagref64 0x{:x}", val),
            &FuncRef(id) => write!(f, "funcref to #{}", id),
            &ThreadRef => write!(f, "threadref"),
            &StackRef => write!(f, "stackref"),
            &FCRef => write!(f, "framecursorref"),
            &Bundle => write!(f, "IR.bundle"),
            &Type(id) => write!(f, "IR.type to #{}", id),
            &FuncSig(id) => write!(f, "IR.funcsig to #{}", id),
            &FuncVer(id) => write!(f, "IR.funcver to #{}", id),
            &BB => write!(f, "IR.BB"),
            &Inst => write!(f, "IR.inst"),
            &Global(id) => write!(f, "IR.global to #{}", id),
            &ExpFunc => write!(f, "IR.expfunc"),
            &NorParam => write!(f, "IR.norparam"),
            &ExcParam => write!(f, "IR.excparam"),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
162
            &InstRes => write!(f, "IR.instres"),
qinsoon's avatar
qinsoon committed
163
164
165
166
        }
    }
}

167
impl APIHandleValue {
qinsoon's avatar
qinsoon committed
168
    /// matches the handle as ref or iref
qinsoon's avatar
qinsoon committed
169
170
    pub fn as_ref_or_iref(&self) -> (P<MuType>, Address) {
        match self {
171
172
            &APIHandleValue::Ref(ref ty, addr) | &APIHandleValue::IRef(ref ty, addr) => (
                ty.clone(),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
173
                addr,
174
            ),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
175
            _ => panic!("expected Ref or IRef handle"),
qinsoon's avatar
qinsoon committed
176
177
178
        }
    }

qinsoon's avatar
qinsoon committed
179
    /// matches the handle as ref
qinsoon's avatar
qinsoon committed
180
    pub fn as_ref(&self) -> (P<MuType>, Address) {
181
        match self {
qinsoon's avatar
qinsoon committed
182
            &APIHandleValue::Ref(ref ty, addr) => (ty.clone(), addr),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
183
            _ => panic!("expected Ref handle"),
qinsoon's avatar
qinsoon committed
184
185
186
        }
    }

qinsoon's avatar
qinsoon committed
187
    /// matches the handle as iref
qinsoon's avatar
qinsoon committed
188
189
190
    pub fn as_iref(&self) -> (P<MuType>, Address) {
        match self {
            &APIHandleValue::IRef(ref ty, addr) => (ty.clone(), addr),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
191
            _ => panic!("expected IRef handle"),
qinsoon's avatar
qinsoon committed
192
193
194
        }
    }

qinsoon's avatar
qinsoon committed
195
    /// matches iref/ref/uptr/ufp handles and extracts address
qinsoon's avatar
qinsoon committed
196
197
    pub fn as_address(&self) -> Address {
        match self {
198
199
200
201
202
203
204
205
206
207
            &APIHandleValue::IRef(_, addr) |
            &APIHandleValue::Ref(_, addr) |
            &APIHandleValue::UPtr(_, addr) |
            &APIHandleValue::UFP(_, addr) => addr,
            _ => {
                panic!(
                    "expected iref/ref/uptr/ufp which contains a pointer, found {}",
                    self
                )
            }
qinsoon's avatar
qinsoon committed
208
209
210
        }
    }

qinsoon's avatar
qinsoon committed
211
    /// matches the handle as int
qinsoon's avatar
qinsoon committed
212
213
214
    pub fn as_int(&self) -> u64 {
        match self {
            &APIHandleValue::Int(val, _) => val,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
215
            _ => panic!("expected Int handle"),
216
217
        }
    }
218

qinsoon's avatar
qinsoon committed
219
    /// matches the handle as float
220
221
222
    pub fn as_float(&self) -> f32 {
        match self {
            &APIHandleValue::Float(val) => val,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
223
            _ => panic!("expected Float handle"),
224
225
226
        }
    }

qinsoon's avatar
qinsoon committed
227
    /// matches the handle as double
228
229
230
    pub fn as_double(&self) -> f64 {
        match self {
            &APIHandleValue::Double(val) => val,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
231
            _ => panic!("expected Double handle"),
232
233
234
        }
    }

qinsoon's avatar
qinsoon committed
235
    /// matches the handle as unsafe pointer
236
237
238
    pub fn as_uptr(&self) -> (P<MuType>, Address) {
        match self {
            &APIHandleValue::UPtr(ref ty, addr) => (ty.clone(), addr),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
239
            _ => panic!("expected UPtr handle"),
240
241
242
        }
    }

qinsoon's avatar
qinsoon committed
243
    /// matches the handle as unsafe function pointer
244
245
246
    pub fn as_ufp(&self) -> (P<MuType>, Address) {
        match self {
            &APIHandleValue::UFP(ref ty, addr) => (ty.clone(), addr),
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
247
            _ => panic!("expected UFP handle"),
248
249
        }
    }
250

qinsoon's avatar
qinsoon committed
251
    /// matches the handle as function reference
252
    pub fn as_funcref(&self) -> MuID {
253
        match self {
254
            &APIHandleValue::FuncRef(id) => id,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
255
            _ => panic!("expected FuncRef"),
256
257
        }
    }
qinsoon's avatar
qinsoon committed
258
259

    /// matches the handle as tag reference's value)
260
261
262
    pub fn as_tr64(&self) -> u64 {
        match self {
            &APIHandleValue::TagRef64(val) => val,
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
263
            _ => panic!("expected TagRef64 handle"),
264
265
266
        }
    }
}