forked from Dead2/stabilizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
szcc
executable file
·314 lines (254 loc) · 9.82 KB
/
szcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
import os
import sys
import shlex
import shutil
import random
import argparse
import subprocess
from types import SimpleNamespace
from distutils import util
# Utility functions
def verboseprint(*args, **kwargs):
""" Print input to stderr if verbose is True. """
if verbose:
print(*args, file=sys.stderr, **kwargs)
def arg(flag, values):
""" Build string of compiler parmeters from list of values. """
if not isinstance(values, list):
values = [values]
cmd = ''
for val in values:
if val == True:
cmd += f" -{flag}"
elif val is None or val == False:
pass
else:
cmd += f" -{flag}{val}"
return cmd
def findexec(filename,path,failonmissing=False):
""" Find filename in path, returns None if not found. """
tmp = shutil.which(filename,path=path)
if tmp and os.path.split(tmp)[0] == os.path.dirname(__file__):
tmp = None
if not tmp and failonmissing:
sys.exit(f"Could not find program {filename}, check that it is installed and available in env PATH.")
return tmp
def runcommand(command, env=os.environ, stoponfail=False, silent=False):
""" Run program, optionally silenced, optionally stop on non-zero retcode """
if env == '':
env = dict()
verboseprint(f"Executing: {command}")
args = shlex.split(command)
if silent:
devnull = open('/dev/null', 'w')
retval = subprocess.Popen(args,stdout=devnull,stderr=sys.stderr,env=env).wait()
devnull.close()
else:
retval = subprocess.Popen(args,stdout=sys.stdout,stderr=sys.stderr,env=env).wait()
if retval != 0 and stoponfail:
print(f"Failed {command}, retcode {retval}")
sys.exit(retval)
return retval
def getplatform():
""" Detect selected or current platform, exit if unsupported. """
platform = None
if 'SZ_PLATFORM' in os.environ:
if os.environ['SZ_PLATFORM'] == 'linux':
platform = 'linux'
elif os.environ['SZ_PLATFORM'] == 'osx':
platform = 'osx'
if not platform:
if util.get_platform().startswith('linux'):
platform = 'linux'
elif util.get_platform().startswith('macosx'):
platform = 'osx'
if platform not in ['linux','osx']:
print( 'Unsupported platform')
exit(2)
return platform
# Main stabilizer functions
def compile(input):
""" Compile source files. """
if os.path.splitext(input)[1] in ['.o','.a','.lo','.la','.so']:
return input
verboseprint("Entering Compile")
cmd = f"{compiler} -c -emit-llvm {gcctoolchain} -o {outfile} {optlevel} {extraparams} {input}"
runcommand(cmd,stoponfail=True)
return outfile
def link(inputs):
""" Link object files, optionally shuffle object file order. """
verboseprint("Entering Link")
tmpfile = f"{outfile}.bc"
cmd = f"{exe.llvmlink} -o {tmpfile} "
if 'SZ_LINK' in os.environ and os.environ['SZ_LINK'] == '1':
random.shuffle(inputs)
cmd += ' '.join(inputs)
runcommand(cmd,stoponfail=True)
return tmpfile
def transform(input):
""" Transform objects. """
verboseprint("Entering Transform")
tmpfile = f"{outfile}.opt.bc"
cmd = f"{exe.opt} -o {tmpfile} {input} {optlevel} -load={stabilizerlib} {randomizers}"
cmd += " -enable-new-pm=0"
runcommand(cmd,stoponfail=True)
return tmpfile
def codegen(input):
""" Run codegen """
verboseprint("Entering Codegen")
tmpfile = f"{outfile}.s"
cmd = f"{exe.llc} -relocation-model=pic --frame-pointer=all {optlevel} -o {tmpfile} {input}"
runcommand(cmd,stoponfail=True)
return tmpfile
def linkend(input):
""" Link the finaly binary. """
verboseprint("Entering LinkEnd")
cmd = f"{compiler} {gcctoolchain} {input} -o {outfile} {optlevel} {extraparams} {linkparams} {sofiles}"
runcommand(cmd,stoponfail=True)
return outfile
def init():
""" Initialize variables and parse arguments """
global verbose,compiler,stabilize,STABILIZER_HOME,exe,outfile,unknown,opts,optlevel,args
global gcctoolchain,stabilizerlib,randomizers,extraparams,linkparams,sofiles
verbose = False
if 'SZ_VERBOSE' in os.environ and os.environ['SZ_VERBOSE'] == '1':
verbose = True
STABILIZER_HOME = os.path.dirname(__file__)
envpath = os.environ['PATH']
exe = SimpleNamespace()
exe.clang = findexec('clang',envpath)
exe.clangxx = findexec('clang++',envpath)
exe.llvmlink = findexec('llvm-link',envpath,failonmissing=True)
exe.llvmas = findexec('llvm-as',envpath,failonmissing=True)
exe.opt = findexec('opt',envpath,failonmissing=True)
exe.llc = findexec('llc',envpath,failonmissing=True)
# Parse arguments
parser = argparse.ArgumentParser(description="SZCC - Stabilizer Compiler Wrapper")
# Compiler arguments
parser.add_argument('-c', '--compile', action='store_true')
parser.add_argument('-o', '--output', default=None)
parser.add_argument('-MT', default=None)
parser.add_argument('-MF', default=None)
parser.add_argument('-O', default=None)
parser.add_argument('-L', action='append', default=[])
parser.add_argument('-l', action='append', default=[])
parser.add_argument('-isystem', action='append', default=[])
parser.add_argument('input', nargs='*', default=[])
args,unknownarr = parser.parse_known_args()
verboseprint(sys.argv)
verboseprint(args)
verboseprint(unknownarr)
# Select compiler
compiler = None
if sys.argv[0][-7:] == 'clang++' or sys.argv[0][-6:] == 'szcc++':
verboseprint('SZCC Running in C++ mode')
compiler = exe.clangxx
else:
verboseprint('SZCC Running in C mode')
compiler = exe.clang
# Add unrecognized parameters that are also files to input file list
for ifile in unknownarr:
if os.path.isfile(ifile):
verboseprint(f"Unrecognized parameter seems to be a file, adding as input file: {ifile}")
args.input.append(ifile)
unknown = ' '.join(unknownarr)
# Print found and missing input files
if verbose:
for ifile in args.input:
if os.path.isfile(ifile):
verboseprint(f"Input file exists: {ifile}, current cwd: {os.getcwd()}")
else:
verboseprint(f"Input file missing: {ifile}, current cwd: {os.getcwd()}")
# Filter out .so files for linking to
sofilesarr = []
for ifile in args.input:
if ifile and len(ifile) >= 1 and ifile[0] and os.path.splitext(ifile)[1] == '.so':
sofilesarr.append(ifile)
for sfile in sofilesarr:
args.input.remove(sfile)
sofiles = ' '.join(sofilesarr)
# Handle missing output filename
if args.output == None:
if len(args.input) >= 1 and args.input[0] and os.path.splitext(args.input[0])[1] in ['.c','.cxx','.cpp']:
verboseprint("No output, attempting to create output name")
outfile = f"{args.input[0][:-2]}.o"
else:
print("No input or output file names")
sys.exit(1)
else:
outfile = args.output
# Detect platform
if getplatform() == 'osx':
LIBSUFFIX = 'dylib'
else:
LIBSUFFIX = 'so'
# Enable selected randomizations
opts = []
stabilize = False
if 'SZ_CODE' in os.environ and os.environ['SZ_CODE'] == '1':
verboseprint("Enabling randomized code location")
stabilize = True
opts.append('stabilize-code')
if 'SZ_STACK' in os.environ and os.environ['SZ_STACK'] == '1':
verboseprint("Enabling randomized stack location")
stabilize = True
opts.append('stabilize-stack')
if 'SZ_HEAP' in os.environ and os.environ['SZ_HEAP'] == '1':
verboseprint("Enabling randomized heap location")
stabilize = True
opts.append('stabilize-heap')
if 'SZ_LOWER' in os.environ and os.environ['SZ_LOWER'] == '1':
verboseprint("Enabling Switch/Invoke/Intrinsic lowering")
opts.append('lower-intrinsics')
opts.append('lowerswitch')
opts.append('lowerinvoke')
if 'SZ_LINK' in os.environ and os.environ['SZ_LINK'] == '1':
verboseprint("Enabling randomized link order")
if stabilize:
args.L.append(STABILIZER_HOME)
args.l.append('stabilizer')
opts.append('stabilize')
else:
verboseprint("Warning: No Stabilizer options enabled.")
# Prepare compiler arguments
if args.O:
optlevel = ' -O'+args.O
else:
optlevel = ''
gcctoolchain = ''
if 'COMPILER_PATH' in os.environ:
gcctoolchain = ' --gcc-toolchain='+os.environ["COMPILER_PATH"]
stabilizerlib = f"{STABILIZER_HOME}/LLVMStabilizer.{LIBSUFFIX}"
randomizers = arg('', opts)
extraparams = arg('isystem', args.isystem)
extraparams += arg('MT', args.MT)
extraparams += arg('MF', args.MF)
extraparams += ' '+unknown
args.l.append('stdc++')
linkparams = arg('L', args.L)
linkparams += arg('l', args.l)
def main():
""" Run the compilation and linking stages """
# Shortcut for running compiler directly if not in compile mode and no input files
if not args.compile and not args.input:
cmd = f"{compiler} {' '.join(sys.argv[1:])}"
sys.exit(runcommand(cmd))
# Compile source files
object_files_tmp = list(map(compile, args.input))
if not args.compile:
# Filter out any missing object files
object_files = []
for ofile in object_files_tmp:
if ofile and os.path.isfile(ofile):
object_files.append(ofile)
else:
verboseprint(f"Object file missing, ignoring: {ofile}, current cwd: {os.getcwd()}")
verboseprint(object_files)
linked = link(object_files)
transformed = transform(linked)
coded = codegen(transformed)
linkend(coded)
if __name__ == '__main__':
init()
main()