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

muctx.rs 16.6 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright 2017 The Australian National University
// 
// 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
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// 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 super::common::*;
16
17
use utils::Address;
//use std::os::raw::c_void;
18

19
pub struct MuCtx {
20
    /// ref to MuVM
21
    mvm: *const MuVM,
22
23
24
25
26

    /// Point to the C-visible CMuCtx so that `close_context` can deallocate itself.
    pub c_struct: *mut CMuCtx,
}

27
28
impl MuCtx {
    pub fn new(mvm: *const MuVM) -> Box<MuCtx> {
29
30
31
32
33
34
35
36
        Box::new(MuCtx {
            mvm: mvm,
            c_struct: ptr::null_mut(),
        })
    }

    #[inline(always)]
    fn get_mvm(&mut self) -> &MuVM {
37
38
        //self.mvm
        unsafe { & *self.mvm }
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
    }

    pub fn id_of(&mut self, name: MuName) -> MuID {
        self.get_mvm().id_of(name)
    }

    pub fn name_of(&mut self, id: MuID) -> CMuCString {
        self.get_mvm().name_of(id)
    }

    fn deallocate(&mut self) {
        let c_struct = self.c_struct;
        let ctx_ptr = self as *mut MuCtx;
        debug!("Deallocating MuCtx {:?} and CMuCtx {:?}...", ctx_ptr, c_struct);
        unsafe {
            Box::from_raw(c_struct);
            Box::from_raw(ctx_ptr);
        }
    }

    pub fn close_context(&mut self) {
        info!("Closing MuCtx...");
        self.deallocate();
    }

    pub fn load_bundle(&mut self, buf: &[c_char]) {
        panic!("The fast implementation does not support the text form.")
    }

    pub fn load_hail(&mut self, buf: &[c_char]) {
        panic!("The fast implementation does not support the text form.")
    }

72
    pub fn handle_from_sint8(&mut self, num: i8, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
73
        trace!("handle_from_sint8");
74
        prepare_handle((self.get_mvm().vm.handle_from_sint8(num, len as usize)))
75
76
    }

77
    pub fn handle_from_uint8(&mut self, num: u8, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
78
        trace!("handle_from_uint8");
79
        prepare_handle((self.get_mvm().vm.handle_from_uint8(num, len as usize)))
80
81
    }

82
    pub fn handle_from_sint16(&mut self, num: i16, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
83
        trace!("handle_from_sint16");
84
        prepare_handle((self.get_mvm().vm.handle_from_sint16(num, len as usize)))
85
86
    }

87
    pub fn handle_from_uint16(&mut self, num: u16, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
88
        trace!("handle_from_uint16");
89
        prepare_handle((self.get_mvm().vm.handle_from_uint16(num, len as usize)))
90
91
    }

92
    pub fn handle_from_sint32(&mut self, num: i32, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
93
        trace!("handle_from_sint32");
94
        prepare_handle((self.get_mvm().vm.handle_from_sint32(num, len as usize)))
95
96
    }

97
    pub fn handle_from_uint32(&mut self, num: u32, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
98
        trace!("handle_from_uint32");
99
        prepare_handle((self.get_mvm().vm.handle_from_uint32(num, len as usize)))
100
101
    }

102
    pub fn handle_from_sint64(&mut self, num: i64, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
103
        trace!("handle_from_sint64");
104
        prepare_handle((self.get_mvm().vm.handle_from_sint64(num, len as usize)))
105
106
    }

107
    pub fn handle_from_uint64(&mut self, num: u64, len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
108
        trace!("handle_from_uint64");
109
        prepare_handle((self.get_mvm().vm.handle_from_uint64(num, len as usize)))
110
111
    }

112
    pub fn handle_from_uint64s(&mut self, nums: &[u64], len: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
113
        trace!("handle_from_uint64s");
114
        panic!("Zebu doesnt implement int with length larger than 64bits")
115
116
    }

117
    pub fn handle_from_float(&mut self, num: f32) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
118
        trace!("handle_from_float");
119
        prepare_handle((self.get_mvm().vm.handle_from_float(num)))
120
121
    }

122
    pub fn handle_from_double(&mut self, num: f64) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
123
        trace!("handle_from_double");
124
        prepare_handle((self.get_mvm().vm.handle_from_double(num)))
125
126
    }

127
    pub fn handle_from_ptr(&mut self, mu_type: MuID, ptr: CMuCPtr) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
128
        trace!("handle_from_ptr: ty#{}, {:?}", mu_type, ptr);
129
130
131
        let addr = Address::from_mut_ptr(ptr);

        prepare_handle((self.get_mvm().vm.handle_from_uptr(mu_type, addr)))
132
133
    }

134
    pub fn handle_from_fp(&mut self, mu_type: MuID, fp: CMuCFP) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
135
        trace!("handle_from_fp: ty#{}, {:?}", mu_type, fp);
136
137
138
        let addr = Address::from_mut_ptr(fp);

        prepare_handle((self.get_mvm().vm.handle_from_ufp(mu_type, addr)))
139
140
    }

141
    pub fn handle_to_sint8(&mut self, opnd: &APIHandle) -> i8 {
qinsoon's avatar
qinsoon committed
142
        trace!("handle_to_sint8: {}", opnd);
143
        self.get_mvm().vm.handle_to_sint8(opnd)
144
145
    }

146
    pub fn handle_to_uint8(&mut self, opnd: &APIHandle) -> u8 {
qinsoon's avatar
qinsoon committed
147
        trace!("handle_to_uint8: {}", opnd);
148
        self.get_mvm().vm.handle_to_uint8(opnd)
149
150
    }

151
    pub fn handle_to_sint16(&mut self, opnd: &APIHandle) -> i16 {
qinsoon's avatar
qinsoon committed
152
        trace!("handle_to_sint16: {}", opnd);
153
        self.get_mvm().vm.handle_to_sint16(opnd)
154
155
    }

156
    pub fn handle_to_uint16(&mut self, opnd: &APIHandle) -> u16 {
qinsoon's avatar
qinsoon committed
157
        trace!("handle_to_uint16: {}", opnd);
158
        self.get_mvm().vm.handle_to_uint16(opnd)
159
160
    }

161
    pub fn handle_to_sint32(&mut self, opnd: &APIHandle) -> i32 {
qinsoon's avatar
qinsoon committed
162
        trace!("handle_to_sint32: {}", opnd);
163
        self.get_mvm().vm.handle_to_sint32(opnd)
164
165
    }

166
    pub fn handle_to_uint32(&mut self, opnd: &APIHandle) -> u32 {
qinsoon's avatar
qinsoon committed
167
        trace!("handle_to_uint32: {}", opnd);
168
        self.get_mvm().vm.handle_to_uint32(opnd)
169
170
    }

171
    pub fn handle_to_sint64(&mut self, opnd: &APIHandle) -> i64 {
qinsoon's avatar
qinsoon committed
172
        trace!("handle_to_sint64: {}", opnd);
173
        self.get_mvm().vm.handle_to_sint64(opnd)
174
175
    }

176
    pub fn handle_to_uint64(&mut self, opnd: &APIHandle) -> u64 {
qinsoon's avatar
qinsoon committed
177
        trace!("handle_to_uint64: {}", opnd);
178
        self.get_mvm().vm.handle_to_uint64(opnd)
179
180
    }

181
    pub fn handle_to_float(&mut self, opnd: &APIHandle) -> f32 {
qinsoon's avatar
qinsoon committed
182
        trace!("handle_to_float: {}", opnd);
183
        self.get_mvm().vm.handle_to_float(opnd)
184
185
    }

186
    pub fn handle_to_double(&mut self, opnd: &APIHandle) -> f64 {
qinsoon's avatar
qinsoon committed
187
        trace!("handle_to_double: {}", opnd);
188
        self.get_mvm().vm.handle_to_double(opnd)
189
190
    }

191
    pub fn handle_to_ptr(&mut self, opnd: &APIHandle) -> CMuCPtr {
qinsoon's avatar
qinsoon committed
192
        trace!("handle_to_ptr: {}", opnd);
193
        self.get_mvm().vm.handle_to_uptr(opnd).to_ptr_mut()
194
195
    }

196
    pub fn handle_to_fp(&mut self, opnd: &APIHandle) -> CMuCFP {
qinsoon's avatar
qinsoon committed
197
        trace!("handle_to_fp: {}", opnd);
198
        self.get_mvm().vm.handle_to_ufp(opnd).to_ptr_mut()
199
200
    }

201
    pub fn handle_from_const(&mut self, id: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
202
        trace!("handle_from_const");
203
        prepare_handle(self.get_mvm().vm.handle_from_const(id))
204
205
    }

206
    pub fn handle_from_global(&mut self, id: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
207
        trace!("handle_from_global");
208
        prepare_handle(self.get_mvm().vm.handle_from_global(id))
209
210
    }

211
    pub fn handle_from_func(&mut self, id: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
212
        trace!("handle_from_func");
213
        prepare_handle(self.get_mvm().vm.handle_from_func(id))
214
215
    }

216
    pub fn handle_from_expose(&mut self, id: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
217
        trace!("handle_from_Expose");
218
219
220
        panic!("Not implemented")
    }

221
    pub fn delete_value(&mut self, opnd: &APIHandle) {
222
        delete_handle(opnd);
223
224
    }

225
    pub fn ref_eq(&mut self, lhs: &APIHandle, rhs: &APIHandle) -> bool {
226
227
228
        panic!("Not implemented")
    }

229
    pub fn ref_ult(&mut self, lhs: &APIHandle, rhs: &APIHandle) -> bool {
230
231
232
        panic!("Not implemented")
    }

233
    pub fn extract_value(&mut self, str: &APIHandle, index: c_int) -> *const APIHandle {
234
235
236
        panic!("Not implemented")
    }

237
    pub fn insert_value(&mut self, str: &APIHandle, index: c_int, newval: &APIHandle) -> *const APIHandle {
238
239
240
        panic!("Not implemented")
    }

241
    pub fn extract_element(&mut self, str: &APIHandle, index: &APIHandle) -> *const APIHandle {
242
243
244
        panic!("Not implemented")
    }

245
    pub fn insert_element(&mut self, str: &APIHandle, index: &APIHandle, newval: &APIHandle) -> *const APIHandle {
246
247
248
        panic!("Not implemented")
    }

249
    pub fn new_fixed(&mut self, mu_type: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
250
        trace!("new_fixed: ty#{}", mu_type);
251
        prepare_handle(self.get_mvm().vm.new_fixed(mu_type))
252
253
    }

254
    pub fn new_hybrid(&mut self, mu_type: MuID, length: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
255
        trace!("new_hybrid: ty#{}, len {}", mu_type, length);
256
        prepare_handle(self.get_mvm().vm.new_hybrid(mu_type, length))
257
258
    }

259
    pub fn refcast(&mut self, opnd: &APIHandle, new_type: MuID) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
260
261
        trace!("refcast: {} to ty#{}", opnd, new_type);
        prepare_handle(self.get_mvm().vm.handle_refcast(opnd, new_type))
262
263
    }

264
    pub fn get_iref(&mut self, opnd: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
265
        trace!("get_iref: {}", opnd);
266
        prepare_handle(self.get_mvm().vm.handle_get_iref(opnd))
267
268
    }

269
    pub fn get_field_iref(&mut self, opnd: &APIHandle, field: c_int) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
270
        trace!("get_field_iref: {}, field {}", opnd, field);
271
        prepare_handle(self.get_mvm().vm.handle_get_field_iref(opnd, field as usize))
272
273
    }

274
    pub fn get_elem_iref(&mut self, opnd: &APIHandle, index: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
275
        trace!("get_elem_iref: {}, index {}", opnd, index);
qinsoon's avatar
qinsoon committed
276
        prepare_handle(self.get_mvm().vm.handle_get_elem_iref(opnd, index))
277
278
    }

279
    pub fn shift_iref(&mut self, opnd: &APIHandle, offset: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
280
        trace!("shift_iref: {}, offset {}", opnd, offset);
281
        prepare_handle(self.get_mvm().vm.handle_shift_iref(opnd, offset))
282
283
    }

284
    pub fn get_var_part_iref(&mut self, opnd: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
285
        trace!("get_var_part_iref: {}", opnd);
286
        prepare_handle(self.get_mvm().vm.handle_get_var_part_iref(opnd))
287
288
    }

289
    pub fn load(&mut self, ord: CMuMemOrd, loc: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
290
        trace!("load: {}", loc);
291
        prepare_handle(self.get_mvm().vm.handle_load(impl_memorder(ord), loc))
292
293
    }

294
    pub fn store(&mut self, ord: CMuMemOrd, loc: &APIHandle, newval: &APIHandle) {
qinsoon's avatar
qinsoon committed
295
        trace!("store: {} val {}", loc, newval);
296
        self.get_mvm().vm.handle_store(impl_memorder(ord), loc, newval);
297
298
    }

299
    pub fn cmpxchg(&mut self, ord_succ: CMuMemOrd, ord_fail: CMuMemOrd, weak: bool, loc: &APIHandle, expected: &APIHandle, desired: &APIHandle, is_succ: *mut CMuBool) -> *const APIHandle {
300
301
302
        panic!("Not implemented")
    }

303
    pub fn atomicrmw(&mut self, ord: CMuMemOrd, op: CMuAtomicRMWOptr, loc: &APIHandle, opnd: &APIHandle) -> *const APIHandle {
304
305
306
307
308
309
310
        panic!("Not implemented")
    }

    pub fn fence(&mut self, ord: CMuMemOrd) {
        panic!("Not implemented")
    }

311
    pub fn new_stack(&mut self, func: &APIHandle) -> *const APIHandle {
312
313
314
        panic!("Not implemented")
    }

315
    pub fn new_thread_nor(&mut self, stack: &APIHandle, threadlocal: Option<&APIHandle>, vals: Vec<&APIHandle>) -> *const APIHandle {
316
317
318
        panic!("Not implemented")
    }

319
    pub fn new_thread_exc(&mut self, stack: &APIHandle, threadlocal: Option<&APIHandle>, exc: &APIHandle) -> *const APIHandle {
320
321
322
        panic!("Not implemented")
    }

323
    pub fn kill_stack(&mut self, stack: &APIHandle) {
324
325
326
        panic!("Not implemented")
    }

327
    pub fn set_threadlocal(&mut self, thread: &APIHandle, threadlocal: &APIHandle) {
328
329
330
        panic!("Not implemented")
    }

331
    pub fn get_threadlocal(&mut self, thread: &APIHandle) -> *const APIHandle {
332
333
334
        panic!("Not implemented")
    }

335
    pub fn new_cursor(&mut self, stack: &APIHandle) -> *const APIHandle {
336
337
338
        panic!("Not implemented")
    }

339
    pub fn next_frame(&mut self, cursor: &APIHandle) {
340
341
342
        panic!("Not implemented")
    }

343
    pub fn copy_cursor(&mut self, cursor: &APIHandle) -> *const APIHandle {
344
345
346
        panic!("Not implemented")
    }

347
    pub fn close_cursor(&mut self, cursor: &APIHandle) {
348
349
350
        panic!("Not implemented")
    }

351
    pub fn cur_func(&mut self, cursor: &APIHandle) -> MuID {
352
353
354
        panic!("Not implemented")
    }

355
    pub fn cur_func_ver(&mut self, cursor: &APIHandle) -> MuID {
356
357
358
        panic!("Not implemented")
    }

359
    pub fn cur_inst(&mut self, cursor: &APIHandle) -> MuID {
360
361
362
        panic!("Not implemented")
    }

363
    pub fn dump_keepalives(&mut self, cursor: &APIHandle, results: *mut CMuValue) {
364
365
366
        panic!("Not implemented")
    }

367
    pub fn pop_frames_to(&mut self, cursor: &APIHandle) {
368
369
370
        panic!("Not implemented")
    }

371
    pub fn push_frame(&mut self, stack: &APIHandle, func: &APIHandle) {
372
373
374
        panic!("Not implemented")
    }

375
    pub fn tr64_is_fp(&mut self, value: &APIHandle) -> bool {
376
        self.get_mvm().vm.handle_tr64_is_fp(value)
377
378
    }

379
    pub fn tr64_is_int(&mut self, value: &APIHandle) -> bool {
380
        self.get_mvm().vm.handle_tr64_is_int(value)
381
382
    }

383
    pub fn tr64_is_ref(&mut self, value: &APIHandle) -> bool {
384
        self.get_mvm().vm.handle_tr64_is_ref(value)
385
386
    }

387
    pub fn tr64_to_fp(&mut self, value: &APIHandle) -> *const APIHandle {
388
        prepare_handle(self.get_mvm().vm.handle_tr64_to_fp(value))
389
390
    }

391
    pub fn tr64_to_int(&mut self, value: &APIHandle) -> *const APIHandle {
392
        prepare_handle(self.get_mvm().vm.handle_tr64_to_int(value))
393
394
    }

395
    pub fn tr64_to_ref(&mut self, value: &APIHandle) -> *const APIHandle {
396
        prepare_handle(self.get_mvm().vm.handle_tr64_to_ref(value))
397
398
    }

399
    pub fn tr64_to_tag(&mut self, value: &APIHandle) -> *const APIHandle {
400
        prepare_handle(self.get_mvm().vm.handle_tr64_to_tag(value))
401
402
    }

403
    pub fn tr64_from_fp(&mut self, value: &APIHandle) -> *const APIHandle {
404
        prepare_handle(self.get_mvm().vm.handle_tr64_from_fp(value))
405
406
    }

407
    pub fn tr64_from_int(&mut self, value: &APIHandle) -> *const APIHandle {
408
        prepare_handle(self.get_mvm().vm.handle_tr64_from_int(value))
409
410
    }

411
    pub fn tr64_from_ref(&mut self, reff: &APIHandle, tag: &APIHandle) -> *const APIHandle {
412
        prepare_handle(self.get_mvm().vm.handle_tr64_from_ref(reff, tag))
413
414
415
416
417
418
419
420
421
422
    }

    pub fn enable_watchpoint(&mut self, wpid: CMuWPID) {
        panic!("Not implemented")
    }

    pub fn disable_watchpoint(&mut self, wpid: CMuWPID) {
        panic!("Not implemented")
    }

423
    pub fn pin(&mut self, loc: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
424
        prepare_handle(self.get_mvm().vm.handle_pin_object(loc))
425
426
    }

427
    pub fn unpin(&mut self, loc: &APIHandle) {
qinsoon's avatar
qinsoon committed
428
        self.get_mvm().vm.handle_unpin_object(loc)
429
430
    }

431
    pub fn get_addr(&mut self, loc: &APIHandle) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
432
        prepare_handle(self.get_mvm().vm.handle_get_addr(loc))
433
434
    }

435
    pub fn expose(&mut self, func: &APIHandle, call_conv: CMuCallConv, cookie: &APIHandle) -> *const APIHandle {
436
437
438
        panic!("Not implemented")
    }

439
    pub fn unexpose(&mut self, call_conv: CMuCallConv, value: &APIHandle) {
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
        panic!("Not implemented")
    }

    pub fn new_ir_builder(&mut self) -> *mut CMuIRBuilder {
        info!("Creating MuIRBuilder...");

        let b: Box<MuIRBuilder> = MuIRBuilder::new(self.mvm);

        let b_ptr = Box::into_raw(b);

        debug!("The MuIRBuilder address: {:?}", b_ptr);

        let cb = make_new_MuIRBuilder(b_ptr as *mut c_void);

        debug!("The C-visible CMuIRBuilder struct address: {:?}", cb);

        unsafe{ (*b_ptr).c_struct = cb; }

        cb
    }

461
    pub fn make_boot_image(&mut self, whitelist: Vec<MuID>, primordial_func: Option<&APIHandle>, primordial_stack: Option<&APIHandle>, primordial_threadlocal: Option<&APIHandle>, sym_fields: Vec<&APIHandle>, sym_strings: Vec<String>, reloc_fields: Vec<&APIHandle>, reloc_strings: Vec<String>, output_file: String) {
462
        self.get_mvm().vm.make_boot_image(whitelist, primordial_func, primordial_stack, primordial_threadlocal, sym_fields, sym_strings, reloc_fields, reloc_strings, output_file);
463
464
465
    }

}
466
467

fn prepare_handle(handle: APIHandleResult) -> *const APIHandle {
qinsoon's avatar
qinsoon committed
468
469
    trace!("got handle: {:?}", handle);

470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    Box::into_raw(handle)
}

fn delete_handle(handle: APIHandleArg) {
    unsafe {Box::from_raw((handle as *const APIHandle) as *mut APIHandle);}
}

use ast::inst::MemoryOrder;
fn impl_memorder(order: CMuMemOrd) -> MemoryOrder {
    match order {
        CMU_ORD_NOT_ATOMIC => MemoryOrder::NotAtomic,
        CMU_ORD_RELAXED    => MemoryOrder::Relaxed,
        CMU_ORD_CONSUME    => MemoryOrder::Consume,
        CMU_ORD_ACQUIRE    => MemoryOrder::Acquire,
        CMU_ORD_RELEASE    => MemoryOrder::Release,
        CMU_ORD_ACQ_REL    => MemoryOrder::AcqRel,
        CMU_ORD_SEQ_CST    => MemoryOrder::SeqCst,
        _ => panic!("invalid CMuMemOrd flag: {}", order)
    }
489
}