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.

util.py 3.1 KB
Newer Older
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
16
import os, subprocess, ctypes, sys;
import struct;
17
18
19

muc = os.environ.get('MUC', 'muc'); #type: str
emit = os.environ.get('MU_EMIT_DIR', 'emit'); #type: str
20
21
libext = '.dylib' if sys.platform.startswith('darwin') else \
         '.so'    if sys.platform.startswith('linux') else sys.exit("Unsupported platform");
22
23

prelude = """
24
25
26
27
28
29
30
        /*--------------------------------------------------------*/
        .funcsig exit_sig = (int<32>) -> ()
        .typedef exit_type = ufuncptr<exit_sig>
        .const exit <exit_type> = EXTERN "exit"
        .typedef char = int<8>
        .funcsig main_sig = (int<32> uptr<uptr<char>>)->(int<32>)
        /*--------------------------------------------------------*/
31
32
33
34
35
"""

""" Makes a primordial function that calls the given 'main' function (which should have the same signature as a C main function) """
def make_primordial(main): # type: (str) -> str
    return """
36
        /*--------------------------------------------------------*/
37
38
39
40
41
42
        .funcdef primordial <(int<32> uptr<uptr<char>>)->()>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                res = CALL <main_sig> """ + main + """(argc argv)
                CCALL #DEFAULT <exit_type exit_sig> exit(res) 
                RET // Unreachable
43
44
45
        }
        /*--------------------------------------------------------*/
        """;
46
47
48
49
50

def get_output_file(name): # type: (str) -> str
    return os.path.join(emit, name);

def execute_muc(bundle, name, primordial=None): # type: (str, str, Optional[str]) -> None
51
    sys.stderr.write(bundle);
52
53
54
55
56
57
58
    muc_proc = subprocess.Popen([muc, "-r"]
        + (["-f", primordial] if primordial is not None else [])
        +  ["/dev/stdin", get_output_file(name)],
        stdin = subprocess.PIPE); #type: subprocess.Popen
    muc_proc.communicate(bundle); # Send the bundle to muc
    assert (muc_proc.returncode == 0); # Check that muc worked

59
def compile_bundle(bundle, name, main = None): # type: (str, str, Optional[str]) -> None
60
    execute_muc(prelude + bundle + make_primordial(main if main is not None else name), name, "primordial");
61
62

def execute(name, args = []): # type: (str, Optional[List[str]]) -> int
63
64
65
    return subprocess.call([get_output_file(name)] + args);

def load_bundle(bundle, name): # type: (str, str) -> ctypes.CDLL
66
67
    execute_muc(prelude + bundle, name + libext);
    return ctypes.CDLL(get_output_file(name + libext));
68
69
70
71

def get_function(func, argtypes, restype): # type: (ctypes._FuncPtr) -> (ctypes._FuncPtr)
    func.argtypes = argtypes;
    func.restype = restype;
72
    return func;