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 5.12 KB
Newer Older
John Zhang's avatar
John Zhang committed
1
2
3
4
import subprocess as subp
import os, sys
import ctypes
import py
5
from multiprocessing import Process, Queue, ProcessError
John Zhang's avatar
John Zhang committed
6
7
8
9
10
11
12
13

CC = os.environ.get('CC', 'clang')
proj_dir = py.path.local(__file__).join('..', '..', '..')
test_jit_dir = proj_dir.join('tests', 'test_jit')
testsuite_dir = test_jit_dir.join('suite')
bin_dir = test_jit_dir.join('temp')
if not bin_dir.exists():
    bin_dir.mkdir()
14
15
16
17
18
19
20
if sys.platform.startswith('darwin'):
    libext = '.dylib'
elif sys.platform.startswith('linux'):
    libext = '.so'
else:
    libext = '.dll'
libmu_path = proj_dir.join('target', 'debug', 'libmu' + libext)
John Zhang's avatar
John Zhang committed
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


def compile_c_script(c_src_name):
    testname = c_src_name[:-2]
    src_c = testsuite_dir.join(c_src_name)
    bin_path = bin_dir.join(testname)
    CFLAGS = [
        "-std=c11",
        "-I%(proj_dir)s/src/vm/api" % globals(),
        "-L" + libmu_path.dirname,
        "-lmu",
    ]
    cmd = [CC] + CFLAGS + ['-o', bin_path.strpath] + [src_c.strpath]

    # compile
    p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE, env=os.environ)
    out, err = p.communicate()
    if p.returncode != 0:  # failed
        sys.stdout.write(out + '\n')
        sys.stderr.write(err + '\n')
        raise subp.CalledProcessError(p.returncode, cmd)

    os.environ['LD_LIBRARY_PATH'] = "%s:%s" % ("%(proj_dir)s/target/debug" % globals(),
                                               os.environ['LD_LIBRARY_PATH'] if 'LD_LIBRARY_PATH' in os.environ else "")
    # run
    p = subp.Popen([bin_path.strpath], stdout=subp.PIPE, stderr=subp.PIPE, cwd=str(bin_dir), env=os.environ)
    out, err = p.communicate()
    if p.returncode != 0:  # failed
        sys.stdout.write(out + '\n')
        sys.stderr.write(err + '\n')
        raise subp.CalledProcessError(p.returncode, bin_path)

    return bin_dir.join('emit', '%(testname)s.dylib' % locals()).strpath


def fncptr_from_lib(lib, fnc_name, argtypes=[], restype=ctypes.c_longlong):
    fnp = getattr(lib, fnc_name)
    fnp.argtypes = argtypes
    fnp.restype = restype
    return fnp

62

John Zhang's avatar
John Zhang committed
63
64
65
66
67
def fncptr_from_c_script(c_src_name, name, argtypes=[], restype=ctypes.c_ulonglong):
    lib_path = compile_c_script(c_src_name)
    lib = ctypes.CDLL(lib_path)
    return fncptr_from_lib(lib, name, argtypes, restype), lib

68

69
def fncptr_from_py_script(py_fnc, name, argtypes=[], restype=ctypes.c_longlong, spawn_proc=True):
John Zhang's avatar
John Zhang committed
70
71
72
    # NOTE: requires mu-client-pypy
    from rpython.rlib import rmu_fast as rmu

73
74
75
    # load libmu before rffi so to load it with RTLD_GLOBAL
    libmu = ctypes.CDLL(libmu_path.strpath, ctypes.RTLD_GLOBAL)

John Zhang's avatar
John Zhang committed
76
77
78
79
80
81
82
    mu = rmu.MuVM()
    ctx = mu.new_context()
    bldr = ctx.new_ir_builder()

    id_dict = py_fnc(bldr, rmu)
    bldr.load()
    libname = 'lib%(name)s.dylib' % locals()
83
84
85
86
    if spawn_proc:
        proc_call(mu.compile_to_sharedlib, (libname, []))
    else:
        mu.compile_to_sharedlib(libname, [])
John Zhang's avatar
John Zhang committed
87
88
89
90

    lib = ctypes.CDLL('emit/%(libname)s' % locals())
    return fncptr_from_lib(lib, name, argtypes, restype), (mu, ctx, bldr)

91

92
93
94
def preload_libmu():
    # load libmu before rffi so to load it with RTLD_GLOBAL
    return ctypes.CDLL(libmu_path.strpath, ctypes.RTLD_GLOBAL)
95

96

97
def proc_call(fnc, args):
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    # call function with an extra Queue parameter to pass the return value in a separate process
    q = Queue()
    rtn = None
    proc = Process(target=lambda *args: args[-1].put(fnc(*args[:-1])), args=args + (q,))
    proc.start()
    from Queue import Empty
    while proc.is_alive():
        try:
            rtn = q.get(False)
            break
        except Empty:
            pass

    if proc.is_alive():
        proc.join()
    if proc.exitcode != 0:
        proc.join()
        raise ProcessError("calling %(fnc)s with args %(args)s crashed with " % locals() + str(proc.exitcode))
    return rtn


119
120
121
122
123
124
125
126
def call_and_check(fnc, args, check_fnc):
    res = fnc(*args)
    if res is None:
        check_fnc()
    else:
        check_fnc(res)


127
def fncptr_from_rpy_func(rpy_fnc, llargtypes, llrestype, spawn_proc=True, **kwargs):
John Zhang's avatar
John Zhang committed
128
129
130
    # NOTE: requires mu-client-pypy
    from rpython.rtyper.lltypesystem import rffi
    from rpython.translator.interactive import Translation
131
    from rpython.config.translationoption import set_opt_level
John Zhang's avatar
John Zhang committed
132

133
    preload_libmu()
134

John Zhang's avatar
John Zhang committed
135
136
137
138
139
140
    kwargs.setdefault('backend', 'mu')
    kwargs.setdefault('muimpl', 'fast')
    kwargs.setdefault('mucodegen', 'api')
    kwargs.setdefault('mutestjit', True)

    t = Translation(rpy_fnc, llargtypes, **kwargs)
141
    set_opt_level(t.config, '3')
John Zhang's avatar
John Zhang committed
142
143
144
    if kwargs['backend'] == 'mu':
        db, bdlgen, fnc_name = t.compile_mu()
        libname = 'lib%(fnc_name)s.dylib' % locals()
145
146
147
148
149
        if spawn_proc:
            # run in a different process
            proc_call(bdlgen.mu.compile_to_sharedlib, args=(libname, []))
        else:
            bdlgen.mu.compile_to_sharedlib(libname, [])
John Zhang's avatar
John Zhang committed
150
151
152
153
154
155
156
157
158
        eci = rffi.ExternalCompilationInfo(libraries=[test_jit_dir.join('emit', libname).strpath])
        extras = (db, bdlgen)
    else:
        libpath = t.compile_c()
        fnc_name = 'pypy_g_' + rpy_fnc.__name__
        eci = rffi.ExternalCompilationInfo(libraries=[libpath.strpath])
        extras = None

    return rffi.llexternal(fnc_name, llargtypes, llrestype, compilation_info=eci, _nowrapper=True), extras