disl.py 14.3 KB
Newer Older
1
#!/usr/bin/env python3
2 3 4 5 6 7 8 9 10 11 12 13

import argparse
import itertools
import os
import platform
import time
from subprocess import *


######################################################################
# CONSTANTS 
######################################################################
14
# default disl_home value, relative to the script
15
DEFAULT_DISL_HOME = os.path.relpath ("%s/.." % os.path.dirname(__file__))
16
# string to be substituted by the actual value of DISL_HOME in paths
17
VARIABLE_DISL_HOME = "${DISL_HOME}"
18 19 20 21 22 23 24 25 26 27


######################################################################
# DISL_HOME
#
# @return
#	DISL_HOME to be used in paths. Either default or from env. 
######################################################################
def disl_home():
	if os.getenv("DISL_HOME") is None:
28
		return DEFAULT_DISL_HOME
29 30 31 32
	else:
		return os.getenv("DISL_HOME")


33 34 35 36
######################################################################
# LIB_SUFFIX
######################################################################
def lib_suffix():
37
	if platform.system() == "Darwin":
38 39 40 41
		return ".jnilib"
	else:
		return ".so"

42 43 44 45 46
######################################################################
# GENERAL_PARSER 
######################################################################
def general_parser(parser):
	# positional variant of i for simplicity
47 48
	# both cannot be set at once
	parser.add_argument("inst", 
49 50
		default=None, 
		nargs="?",
51
		help="path to jar containing DiSL instrumentation code, same as '-i'")
52
	
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	# positional variant of c_app for simplicity
	# both cannot be set at once	
	parser.add_argument("app", 
		default=None, 
		nargs="*",
		help="client jar or class and its arguments, same as '-c_app'")
	
	parser.add_argument("-d", 
		default=disl_home(),
		dest="disl_home",
		help="disl home directory")

	parser.add_argument("-cs",
		action="store_true",
		default=True,
68
		help="start client and DiSL server")
69 70 71 72
	
	parser.add_argument("-cse",
		action="store_true",
		default=False,
73
		help="start client, DiSL server and Shadow VM server")
74 75
	
	parser.add_argument("-i", 
76 77 78
		dest="instrumentation",
		default=None, 
		metavar="PATH",
79
		help="path to jar containing DiSL instrumentation code, same as 'inst'")
80

81
	parser.add_argument("-t",
František Haas's avatar
František Haas committed
82 83 84 85
		dest="test_dir",
		default=None,
		metavar="PATH",
		help="directory to run in, default=current")
86 87 88 89 90 91
	
	#parser.add_argument("-p",
	#	action="store_true",
	#	default=False,
	#	dest="print_namespace",
	#	help="show all set options and default values")
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	return
	

######################################################################
# CLIENT_PARSER 
######################################################################
def client_parser(parser):
	group = parser.add_argument_group("CLIENT")

	group.add_argument("-c_opts",
		action="append",
		default=[],
		metavar="A",
		nargs="+",
		help="java options of the client application")
	
	group.add_argument("-c_app",
		action="append",
		default=None,
		metavar="A",
		nargs="+",
		help="client jar or class and its arguments, same as 'app'")
	
115 116 117
	#############################
	# additional arguments follow
	#############################
118
	
František Haas's avatar
František Haas committed
119 120 121 122 123 124 125 126 127 128
	group.add_argument("-c_out", 
		default=None, 
		metavar="PATH",
		help="file to save client stdout to")
	
	group.add_argument("-c_err", 
		default=None, 
		metavar="PATH",
		help="file to save client stderr to")
	
129 130 131 132 133 134 135 136 137 138 139 140 141 142
	return


######################################################################
# SERVER_PARSER 
######################################################################
def server_parser(parser):
	group = parser.add_argument_group("SERVER")
	
	group.add_argument("-s_opts",
		action="append",
		default=[],
		metavar="A",
		nargs="+",
143
		help="options for the DiSL server JVM")
144 145 146 147 148 149

	group.add_argument("-s_args",
		action="append",
		default=[],
		metavar="A",
		nargs="+",
150
		help="DiSL server arguments")
151 152 153 154 155 156 157 158 159 160 161 162 163 164
	
	group.add_argument("-s_out", 
		default=None, 
		metavar="PATH",
		help="file to save server stdout to")
	
	group.add_argument("-s_err", 
		default=None, 
		metavar="PATH",
		help="file to save server stderr to")
	
	#############################
	# additional arguments follow
	#############################
165 166 167 168 169

	group.add_argument("-s_debug", 
		action="store_true",
		default=False,
		help="enable debug output")
Lukáš Marek's avatar
Lukáš Marek committed
170 171 172 173

	group.add_argument("-s_noexcepthandler", 
		action="store_true",
		default=False,
174
		help="does not add exception handler around snippets (improves performance but does not protect from errors within instrumentation)")
Lukáš Marek's avatar
Lukáš Marek committed
175

176 177 178 179 180
	group.add_argument("-s_nodynamicbypass", 
		action="store_true",
		default=False,
		help="does not apply dynamic bypass")

Lukáš Marek's avatar
Lukáš Marek committed
181 182 183 184
	group.add_argument("-s_exclusionlist", 
		default=None,
		metavar="PATH",
		help="path to exclusion list")
185

186 187 188 189 190 191 192 193 194 195
	group.add_argument("-s_instrumented", 
		default=None,	
		metavar="PATH",
		help="dumps instrumented classes into specified directory")
	
	group.add_argument("-s_uninstrumented", 
		default=None,	
		metavar="PATH",
		help="dumps uninstrumented classes into specified directory")

196 197 198 199 200
	group.add_argument("-s_port", 
		default=None,	
		metavar="PORT",
		help="listening network port")

201
	return
202

203 204 205 206 207 208 209 210 211 212 213 214

######################################################################
# EVALUATION_PARSER 
######################################################################
def evaluation_parser(parser):
	group = parser.add_argument_group("EVALUATION")
	
	group.add_argument("-e_opts",
		action="append",
		default=[],
		nargs="+",
		metavar="A", 
215
		help="options for the Shadow VM server JVM")
216 217 218 219 220 221
	
	group.add_argument("-e_args",
		action="append",
		default=[],
		nargs="+",
		metavar="A", 
222
		help="Shadow VM server arguments")
223 224 225 226 227 228 229 230 231 232 233 234 235 236
	
	group.add_argument("-e_out", 
		default=None, 
		metavar="PATH",
		help="file to save evaluation stdout to")
	
	group.add_argument("-e_err", 
		default=None, 
		metavar="PATH",
		help="file to save evaluation stderr to")

	#############################
	# additional arguments follow
	#############################
237 238 239 240 241 242

	group.add_argument("-e_debug", 
		action="store_true",
		default=False,
		help="enable debug output")

243 244 245 246 247
	group.add_argument("-e_port", 
		default=None,	
		metavar="PORT",
		help="listening network port")

248 249 250 251 252 253 254 255 256 257 258
	return


######################################################################
# DOCUMENTATION_PARSER 
######################################################################
def documentation_parser(parser):

	parser.formatter_class=argparse.RawTextHelpFormatter

	parser.description = """
259
This is a DiSL application, DiSL instrumentation server, and Shadow VM server launcher.
260

261 262 263 264
By default, the script starts a DiSL instrumentation server VM and a client application
VM that will be instrumented using DiSL. For instrumentations using remote analysis
execution in Shadow VM, the Shadow VM server needs to be started as well, which needs
to be specified explicitly using the '-cse' option.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

To pass option like arguments (starting with dash) one must either use equal 
sign or positional variant of the argument if it's present. For example 
'-c_app="-jar"' instead of '-c_app "-jar"'. The positional version must be 
preceded with '--' as in the example.

The '-d' option specifies where the DiSL framework is installed. In some cases 
it might work thanks to default relative path. In other cases one must either 
specify the correct location at the command line or set 'DISL_HOME' system 
variable. 
"""

	parser.epilog = """
EXAMPLES:

To execute the example application run following:
281
	./disl.py -- build/example-inst.jar -jar build/example-app.jar
282
or	
283
	./disl.py -cs -i=build/example-inst.jar -c_app="-jar build/example-app.jar"
284 285 286 287 288 289 290 291 292
"""

######################################################################
# MAKE_PARSER 
######################################################################
def make_parser():
	parser = argparse.ArgumentParser()
	general_parser(parser)
	client_parser(parser)
293 294
	server_parser(parser)
	evaluation_parser(parser)
295 296 297 298 299 300 301 302 303 304 305 306 307 308
	documentation_parser(parser)
	
	return parser


######################################################################
# FLATTEN_ALL
#	Makes a list from nested lists or even a single non list
#	argument. Does not slice strings.
######################################################################
def flatten_all(object):
	if object is None:
		return None

309
	if isinstance(object, str):
310 311 312 313
		return [object]

	result = []
	for x in object:
314
		if hasattr(x, "__iter__") and not isinstance(x, str):
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
			result.extend(flatten_all(x))
		else:
			result.append(x)
	return result

	#return list(itertools.chain.from_iterable(object))


######################################################################
# PARSE_ARGUMENTS 
######################################################################
def parse_arguments(parser):
	args = parser.parse_args()

	args.c_opts = flatten_all(args.c_opts)
	args.c_app = flatten_all(args.c_app)
	args.app = flatten_all(args.app)

	args.s_opts = flatten_all(args.s_opts)
	args.s_args = flatten_all(args.s_args)
335 336 337 338 339
	if args.s_debug is True:
		args.s_opts+= ["-Ddebug=true"]
	if args.s_port is not None:
		args.s_opts+= ["-Ddislserver.port="+args.s_port]
	if args.s_noexcepthandler is True:
340
		args.s_opts+= ["-Ddisl.noexcepthandler=true"]
341 342
	if args.s_nodynamicbypass is True:
		args.s_opts+= ["-Ddislserver.disablebypass=true"]
343 344
	if args.s_exclusionlist is not None:
		args.s_opts+= ["-Ddisl.exclusionList="+args.s_exclusionlist]
345
	if args.s_instrumented is not None:
346
		args.s_opts+= ["-Ddislserver.instrumented="+args.s_instrumented]
347
	if args.s_uninstrumented is not None:
348
		args.s_opts+= ["-Ddislserver.uninstrumented="+args.s_uninstrumented]
349 350 351

	args.e_opts = flatten_all(args.e_opts)
	args.e_args = flatten_all(args.e_args)
352 353 354 355
	if args.e_debug is True:
		args.e_opts+= ["-Ddebug=true"]
	if args.e_port is not None:
		args.e_opts+= ["-Ddislreserver.port="+args.e_port]
356

357 358 359 360 361
	# supply instrumentation from positional 'inst' if set
	if args.instrumentation is not None and args.inst is not None:
		parser.error("-i and -inst set both")
	if args.inst is not None:
		args.instrumentation = args.inst
362 363 364

	# supply c_app from positional app if set
	if args.c_app is not None and args.app is not None:
365
		parser.error("-c_app and -app set both")
366 367 368
	if args.app is not None:
		args.c_app = args.app
	
369 370 371 372
	#if args.print_namespace:
	#	for key in args.__dict__:
	#		value = args.__dict__[key]
	#		print key + "=" + str(value)
373 374 375 376 377

	return args


######################################################################
378
# TRY_KILL
379
######################################################################
380
def try_kill(pid_file_name):
381
	try:
382 383 384 385
		with open(os.devnull, "w") as devnull:
			with open(pid_file_name, "r") as pid_file:
				pid = pid_file.readline()
				kill = Popen(["kill", pid], stdout=devnull, stderr=devnull, shell=False)
386 387 388 389
	except IOError:
		pass


390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
######################################################################
# TRY_REMOVE_FILE
######################################################################
def try_remove_file(file_name):
	if os.path.isfile(file_name):
		os.remove(file_name)


######################################################################
# RUN
######################################################################
def run(cmd, out, err):
	out_f = None
	err_f = None	
	if out is not None:
		out_f = open(out, "w")
	if err is not None:
		err_f = open(err, "w")
	
409
	process = Popen(cmd, stdout=out_f, stderr=err_f, shell=False)
410 411 412 413 414 415 416 417
	return process	


######################################################################
# RUN_SERVER
######################################################################
def run_server(args, parser):
	if args.instrumentation is None:
418
		parser.error("argument instr (-i) is required to run the client")
419 420 421

	try_kill(".server.pid")

422 423
	s_jar = args.disl_home+"/lib/disl-server.jar"
	s_class = "ch.usi.dag.dislserver.DiSLServer"
424 425 426

	s_cmd = ["java"]
	s_cmd+= args.s_opts
427
	s_cmd+= ["-cp", args.instrumentation + ":" + s_jar]
428 429 430 431
	s_cmd+= [s_class]
	s_cmd+= args.s_args

	#print s_cmd
František Haas's avatar
František Haas committed
432

433
	server = run(s_cmd, args.s_out, args.s_err)
434 435 436
	
	with open(".server.pid", "w") as pid_file:
		pid_file.write(str(server.pid))
437 438
	
	return
439

440 441 442 443 444 445

######################################################################
# RUN_EVALUATION
######################################################################
def run_evaluation(args, parser):
	if args.instrumentation is None:
446
		parser.error("argument instr (-i) is required to run the client")
447 448 449

	try_kill(".evaluation.pid")

450 451
	e_jar = args.disl_home+"/lib/dislre-server.jar"
	e_class = "ch.usi.dag.dislreserver.DiSLREServer"
452 453 454 455 456 457 458 459 460 461 462

	e_cmd = ["java"]
	e_cmd+= args.s_opts
	e_cmd+= ["-Xms1G", "-Xmx2G"]
	e_cmd+= ["-cp", args.instrumentation + ":" + e_jar]
	e_cmd+= [e_class]
	e_cmd+= args.e_args

	#print e_cmd
	
	evaluation = run(e_cmd, args.e_out, args.e_err)
463
	
464 465 466
	with open(".evaluation.pid", "w") as pid_file:
		pid_file.write(str(evaluation.pid))

467 468 469 470 471 472 473 474
	return


######################################################################
# RUN_CLIENT
######################################################################
def run_client(args, parser):
	if args.c_app is None:
475
		parser.error("argument app (-c_app) is required to run the client")
476 477
	
	if args.instrumentation is None:
478
		parser.error("argument instr (-i) is required to run the client")
479

480 481
	cagent = args.disl_home+"/lib/libdislagent"+lib_suffix()
	eagent = args.disl_home+"/lib/libdislreagent"+lib_suffix()
482
	bypass = args.disl_home+"/lib/disl-bypass.jar"
483
	dispatch = args.disl_home+"/lib/dislre-dispatch.jar"
484

485 486
	c_cmd = ["java"]
	c_cmd+= args.c_opts
487 488 489 490 491 492
	c_cmd+= ["-agentpath:"+cagent]
	
	if args.cse == True:
		c_cmd+= ["-agentpath:"+eagent]

	if args.cse == True:
493
		c_cmd+= ["-Xbootclasspath/a:"+bypass+":"+args.instrumentation+":"+dispatch]
494
	else:
495
		c_cmd+= ["-Xbootclasspath/a:"+bypass+":"+args.instrumentation]
496

497 498 499
	c_cmd+= args.c_app

	#print c_cmd
František Haas's avatar
František Haas committed
500

501
	client = run(c_cmd, args.c_out, args.c_err)
502
	client.wait()
503 504 505

	# let server and evaluation finish
	time.sleep(1)
František Haas's avatar
František Haas committed
506
	
507 508 509 510
	try_kill(".server.pid")
	try_kill(".evaluation.pid")
	try_remove_file(".server.pid")
	try_remove_file(".evaluation.pid")
511 512 513 514 515 516 517 518 519 520 521

	return


######################################################################
# MAIN
######################################################################
def main():
	parser = make_parser()
	args = parse_arguments(parser)

František Haas's avatar
František Haas committed
522 523 524
	if args.test_dir is not None:
		os.chdir(args.test_dir)

525
	if args.cs == True:
526
		run_server(args, parser)
527 528 529
	
	if args.cse == True:
		run_evaluation(args, parser)
530

531 532
	if args.cs == True or args.cse == True:
		time.sleep(3)
533 534 535 536 537 538 539 540
		run_client(args, parser)

	return


######################################################################
# ENTRY_POINT
######################################################################
541

542 543 544
if __name__ == "__main__":
	main()