blob: 87664aa928e82b30736eb736b545ef27ecc7a40a [file] [log] [blame]
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001#
David Srbeckybfa76b32022-06-20 21:30:56 +01002# Copyright (C) 2022 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +010015
David Srbeckybfa76b32022-06-20 21:30:56 +010016import sys, os, shutil, shlex, re, subprocess, glob
David Srbeckye4e701b2022-10-24 11:00:31 +000017from argparse import ArgumentParser, BooleanOptionalAction, Namespace
David Srbeckybfa76b32022-06-20 21:30:56 +010018from os import path
David Srbecky93749ed2022-11-09 15:24:45 +000019from os.path import isfile, isdir, basename
David Srbeckyefb61262022-11-30 16:49:33 +000020from subprocess import check_output, DEVNULL, PIPE, STDOUT
David Srbeckyd7674c22022-10-03 18:22:13 +010021from tempfile import NamedTemporaryFile
David Srbecky49506de2022-11-25 12:00:06 +000022from testrunner import env
23from typing import List
David Srbeckybfa76b32022-06-20 21:30:56 +010024
David Srbeckyf356dcd2022-11-21 12:14:50 +000025COLOR = (os.environ.get("LUCI_CONTEXT") == None) # Disable colors on LUCI.
26COLOR_BLUE = '\033[94m' if COLOR else ''
27COLOR_GREEN = '\033[92m' if COLOR else ''
28COLOR_NORMAL = '\033[0m' if COLOR else ''
29COLOR_RED = '\033[91m' if COLOR else ''
David Srbeckybfa76b32022-06-20 21:30:56 +010030
David Srbeckye4e701b2022-10-24 11:00:31 +000031def parse_args(argv):
David Srbecky94d21412022-10-10 16:02:51 +010032 argp, opt_bool = ArgumentParser(), BooleanOptionalAction
33 argp.add_argument("--64", dest="is64", action="store_true")
34 argp.add_argument("--O", action="store_true")
35 argp.add_argument("--Xcompiler-option", default=[], action="append")
36 argp.add_argument("--add-libdir-argument", action="store_true")
37 argp.add_argument("--android-art-root", default="/apex/com.android.art")
38 argp.add_argument("--android-i18n-root", default="/apex/com.android.i18n")
David Srbeckyfe3d9c92022-11-10 18:57:15 +000039 argp.add_argument("--android-log-tags", default="*:i")
David Srbecky94d21412022-10-10 16:02:51 +010040 argp.add_argument("--android-root", default="/system")
41 argp.add_argument("--android-runtime-option", default=[], action="append")
42 argp.add_argument("--android-tzdata-root", default="/apex/com.android.tzdata")
43 argp.add_argument("--app-image", default=True, action=opt_bool)
David Srbecky94d21412022-10-10 16:02:51 +010044 argp.add_argument("--baseline", action="store_true")
45 argp.add_argument("--bionic", action="store_true")
46 argp.add_argument("--boot", default="")
47 argp.add_argument("--chroot", default="")
48 argp.add_argument("--compact-dex-level")
49 argp.add_argument("--compiler-only-option", default=[], action="append")
50 argp.add_argument("--create-runner", action="store_true")
David Srbeckyfe3d9c92022-11-10 18:57:15 +000051 argp.add_argument("--diff-min-log-tag", default="E")
David Srbecky94d21412022-10-10 16:02:51 +010052 argp.add_argument("--debug", action="store_true")
53 argp.add_argument("--debug-agent")
54 argp.add_argument("--debug-wrap-agent", action="store_true")
55 argp.add_argument("--dex2oat-dm", action="store_true")
56 argp.add_argument(
57 "--dex2oat-rt-timeout", type=int,
58 default=360) # The *hard* timeout. 6 min.
59 argp.add_argument(
60 "--dex2oat-timeout", type=int, default=300) # The "soft" timeout. 5 min.
61 argp.add_argument("--dry-run", action="store_true")
62 argp.add_argument("--experimental", default=[], action="append")
63 argp.add_argument("--external-log-tags", action="store_true")
64 argp.add_argument("--gc-stress", action="store_true")
65 argp.add_argument("--gdb", action="store_true")
66 argp.add_argument("--gdb-arg", default=[], action="append")
67 argp.add_argument("--gdb-dex2oat", action="store_true")
68 argp.add_argument("--gdb-dex2oat-args", default=[], action="append")
69 argp.add_argument("--gdbserver", action="store_true")
70 argp.add_argument("--gdbserver-bin")
71 argp.add_argument("--gdbserver-port", default=":5039")
72 argp.add_argument("--host", action="store_true")
73 argp.add_argument("--image", default=True, action=opt_bool)
74 argp.add_argument("--instruction-set-features", default="")
75 argp.add_argument("--interpreter", action="store_true")
76 argp.add_argument("--invoke-with", default=[], action="append")
77 argp.add_argument("--jit", action="store_true")
78 argp.add_argument("--jvm", action="store_true")
79 argp.add_argument("--jvmti", action="store_true")
80 argp.add_argument("--jvmti-field-stress", action="store_true")
81 argp.add_argument("--jvmti-redefine-stress", action="store_true")
82 argp.add_argument("--jvmti-step-stress", action="store_true")
83 argp.add_argument("--jvmti-trace-stress", action="store_true")
84 argp.add_argument("--lib", default="")
85 argp.add_argument("--optimize", default=True, action=opt_bool)
86 argp.add_argument("--prebuild", default=True, action=opt_bool)
87 argp.add_argument("--profile", action="store_true")
88 argp.add_argument("--random-profile", action="store_true")
89 argp.add_argument("--relocate", default=False, action=opt_bool)
90 argp.add_argument("--runtime-dm", action="store_true")
91 argp.add_argument("--runtime-extracted-zipapex", default="")
92 argp.add_argument("--runtime-option", default=[], action="append")
93 argp.add_argument("--runtime-zipapex", default="")
94 argp.add_argument("--secondary", action="store_true")
95 argp.add_argument("--secondary-app-image", default=True, action=opt_bool)
96 argp.add_argument("--secondary-class-loader-context", default="")
97 argp.add_argument("--secondary-compilation", default=True, action=opt_bool)
98 argp.add_argument("--simpleperf", action="store_true")
99 argp.add_argument("--sync", action="store_true")
100 argp.add_argument("--testlib", default=[], action="append")
101 argp.add_argument("--timeout", default=0, type=int)
102 argp.add_argument("--vdex", action="store_true")
103 argp.add_argument("--vdex-arg", default=[], action="append")
David Srbeckye4e701b2022-10-24 11:00:31 +0000104 argp.add_argument("--vdex-filter", default="")
David Srbecky94d21412022-10-10 16:02:51 +0100105 argp.add_argument("--verify", default=True, action=opt_bool)
106 argp.add_argument("--verify-soft-fail", action="store_true")
107 argp.add_argument("--with-agent", default=[], action="append")
108 argp.add_argument("--zygote", action="store_true")
David Srbeckye4e701b2022-10-24 11:00:31 +0000109 argp.add_argument("--test_args", default=[], action="append")
110 argp.add_argument("--stdout_file", default="")
111 argp.add_argument("--stderr_file", default="")
112 argp.add_argument("--main", default="Main")
113 argp.add_argument("--expected_exit_code", default=0)
David Srbecky94d21412022-10-10 16:02:51 +0100114
David Srbecky94d21412022-10-10 16:02:51 +0100115 # Python parser requires the format --key=--value, since without the equals symbol
116 # it looks like the required value has been omitted and there is just another flag.
117 # For example, '--args --foo --host --64' will become '--arg=--foo --host --64'
118 # because otherwise the --args is missing its value and --foo is unknown argument.
119 for i, arg in reversed(list(enumerate(argv))):
120 if arg in [
121 "--args", "--runtime-option", "--android-runtime-option",
122 "-Xcompiler-option", "--compiler-only-option"
123 ]:
124 argv[i] += "=" + argv.pop(i + 1)
David Srbeckye4e701b2022-10-24 11:00:31 +0000125
126 # Accept single-dash arguments as if they were double-dash arguments.
127 # For exmpample, '-Xcompiler-option' becomes '--Xcompiler-option'
128 # became single-dash can be used only with single-letter arguments.
David Srbecky94d21412022-10-10 16:02:51 +0100129 for i, arg in list(enumerate(argv)):
130 if arg.startswith("-") and not arg.startswith("--"):
131 argv[i] = "-" + arg
132 if arg == "--":
133 break
David Srbecky85786e72022-09-21 23:54:01 +0100134
David Srbeckye4e701b2022-10-24 11:00:31 +0000135 return argp.parse_args(argv)
136
David Srbecky49506de2022-11-25 12:00:06 +0000137def get_target_arch(is64: bool) -> str:
138 # We may build for two arches. Get the one with the expected bitness.
139 arches = [a for a in [env.TARGET_ARCH, env.TARGET_2ND_ARCH] if a]
140 assert len(arches) > 0, "TARGET_ARCH/TARGET_2ND_ARCH not set"
141 if is64:
142 arches = [a for a in arches if a.endswith("64")]
143 assert len(arches) == 1, f"Can not find (unique) 64-bit arch in {arches}"
144 else:
145 arches = [a for a in arches if not a.endswith("64")]
146 assert len(arches) == 1, f"Can not find (unique) 32-bit arch in {arches}"
147 return arches[0]
David Srbeckye4e701b2022-10-24 11:00:31 +0000148
David Srbeckye0cbf8e2022-11-03 10:42:21 +0000149# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
150# because that's what we use for compiling the boot.art image.
151# It may contain additional modules from TEST_CORE_JARS.
152bpath_modules = ("core-oj core-libart okhttp bouncycastle apache-xml core-icu4j"
153 " conscrypt")
154
155
156# Helper function to construct paths for apex modules (for both -Xbootclasspath and
157# -Xbootclasspath-location).
158def get_apex_bootclasspath_impl(bpath_prefix: str):
159 bpath_separator = ""
160 bpath = ""
161 bpath_jar = ""
162 for bpath_module in bpath_modules.split(" "):
163 apex_module = "com.android.art"
164 if bpath_module == "conscrypt":
165 apex_module = "com.android.conscrypt"
166 if bpath_module == "core-icu4j":
167 apex_module = "com.android.i18n"
168 bpath_jar = f"/apex/{apex_module}/javalib/{bpath_module}.jar"
169 bpath += f"{bpath_separator}{bpath_prefix}{bpath_jar}"
170 bpath_separator = ":"
171 return bpath
172
173
174# Gets a -Xbootclasspath paths with the apex modules.
175def get_apex_bootclasspath(host: bool):
176 bpath_prefix = ""
177
178 if host:
179 bpath_prefix = os.environ["ANDROID_HOST_OUT"]
180
181 return get_apex_bootclasspath_impl(bpath_prefix)
182
183
184# Gets a -Xbootclasspath-location paths with the apex modules.
185def get_apex_bootclasspath_locations(host: bool):
186 bpath_location_prefix = ""
187
188 if host:
189 ANDROID_BUILD_TOP=os.environ["ANDROID_BUILD_TOP"]
190 ANDROID_HOST_OUT=os.environ["ANDROID_HOST_OUT"]
191 if ANDROID_HOST_OUT[0:len(ANDROID_BUILD_TOP)+1] == f"{ANDROID_BUILD_TOP}/":
192 bpath_location_prefix=ANDROID_HOST_OUT[len(ANDROID_BUILD_TOP)+1:]
193 else:
194 print(f"ANDROID_BUILD_TOP/ is not a prefix of ANDROID_HOST_OUT"\
195 "\nANDROID_BUILD_TOP={ANDROID_BUILD_TOP}"\
196 "\nANDROID_HOST_OUT={ANDROID_HOST_OUT}")
197 sys.exit(1)
198
199 return get_apex_bootclasspath_impl(bpath_location_prefix)
200
201
David Srbeckye4e701b2022-10-24 11:00:31 +0000202def default_run(ctx, args, **kwargs):
203 # Clone the args so we can modify them without affecting args in the caller.
204 args = Namespace(**vars(args))
205
206 # Overwrite args based on the named parameters.
207 # E.g. the caller can do `default_run(args, jvmti=True)` to modify args.jvmti.
208 for name, new_value in kwargs.items():
209 old_value = getattr(args, name)
210 assert isinstance(new_value, old_value.__class__), name + " should have type " + str(old_value.__class__)
211 if isinstance(old_value, list):
212 setattr(args, name, old_value + new_value) # Lists get merged.
213 else:
214 setattr(args, name, new_value)
215
David Srbecky93749ed2022-11-09 15:24:45 +0000216 # Store copy of stdout&stderr of command in files so that we can diff them later.
217 # This may run under 'adb shell' so we are limited only to 'sh' shell feature set.
218 def tee(cmd: str):
219 # 'tee' works on stdout only, so we need to temporarily swap stdout and stderr.
220 cmd = f"({cmd} | tee -a {DEX_LOCATION}/{basename(args.stdout_file)}) 3>&1 1>&2 2>&3"
221 cmd = f"({cmd} | tee -a {DEX_LOCATION}/{basename(args.stderr_file)}) 3>&1 1>&2 2>&3"
222 return f"set -o pipefail; {cmd}" # Use exit code of first failure in piped command.
223
David Srbeckye4e701b2022-10-24 11:00:31 +0000224 local_path = os.path.dirname(__file__)
225
David Srbeckye4e701b2022-10-24 11:00:31 +0000226 ANDROID_BUILD_TOP = os.environ.get("ANDROID_BUILD_TOP")
227 ANDROID_DATA = os.environ.get("ANDROID_DATA")
228 ANDROID_HOST_OUT = os.environ["ANDROID_HOST_OUT"]
229 ANDROID_LOG_TAGS = os.environ.get("ANDROID_LOG_TAGS", "")
230 ART_TIME_OUT_MULTIPLIER = int(os.environ.get("ART_TIME_OUT_MULTIPLIER", 1))
231 DEX2OAT = os.environ.get("DEX2OAT", "")
232 DEX_LOCATION = os.environ["DEX_LOCATION"]
233 JAVA = os.environ.get("JAVA")
234 OUT_DIR = os.environ.get("OUT_DIR")
235 PATH = os.environ.get("PATH", "")
236 SANITIZE_HOST = os.environ.get("SANITIZE_HOST", "")
237 TEST_NAME = os.environ["TEST_NAME"]
238 USE_EXRACTED_ZIPAPEX = os.environ.get("USE_EXRACTED_ZIPAPEX", "")
239
240 assert ANDROID_BUILD_TOP, "Did you forget to run `lunch`?"
241
David Srbecky94d21412022-10-10 16:02:51 +0100242 ANDROID_ROOT = args.android_root
243 ANDROID_ART_ROOT = args.android_art_root
244 ANDROID_I18N_ROOT = args.android_i18n_root
245 ANDROID_TZDATA_ROOT = args.android_tzdata_root
246 ARCHITECTURES_32 = "(arm|x86|none)"
247 ARCHITECTURES_64 = "(arm64|x86_64|none)"
248 ARCHITECTURES_PATTERN = ARCHITECTURES_32
249 GET_DEVICE_ISA_BITNESS_FLAG = "--32"
250 BOOT_IMAGE = args.boot
251 CHROOT = args.chroot
252 COMPILE_FLAGS = ""
253 DALVIKVM = "dalvikvm32"
254 DEBUGGER = "n"
255 WITH_AGENT = args.with_agent
256 DEBUGGER_AGENT = args.debug_agent
257 WRAP_DEBUGGER_AGENT = args.debug_wrap_agent
David Srbecky94d21412022-10-10 16:02:51 +0100258 DEX2OAT_NDEBUG_BINARY = "dex2oat32"
259 DEX2OAT_DEBUG_BINARY = "dex2oatd32"
260 EXPERIMENTAL = args.experimental
261 FALSE_BIN = "false"
262 FLAGS = ""
263 ANDROID_FLAGS = ""
264 GDB = ""
265 GDB_ARGS = ""
266 GDB_DEX2OAT = ""
267 GDB_DEX2OAT_ARGS = ""
268 GDBSERVER_DEVICE = "gdbserver"
269 GDBSERVER_HOST = "gdbserver"
270 HAVE_IMAGE = args.image
271 HOST = args.host
272 BIONIC = args.bionic
273 CREATE_ANDROID_ROOT = False
274 USE_ZIPAPEX = (args.runtime_zipapex != "")
275 ZIPAPEX_LOC = args.runtime_zipapex
276 USE_EXTRACTED_ZIPAPEX = (args.runtime_extracted_zipapex != "")
277 EXTRACTED_ZIPAPEX_LOC = args.runtime_extracted_zipapex
278 INTERPRETER = args.interpreter
279 JIT = args.jit
280 INVOKE_WITH = " ".join(args.invoke_with)
281 USE_JVMTI = args.jvmti
282 IS_JVMTI_TEST = False
283 ADD_LIBDIR_ARGUMENTS = args.add_libdir_argument
284 SUFFIX64 = ""
285 ISA = "x86"
286 LIBRARY_DIRECTORY = "lib"
287 TEST_DIRECTORY = "nativetest"
David Srbeckye4e701b2022-10-24 11:00:31 +0000288 MAIN = args.main
David Srbecky94d21412022-10-10 16:02:51 +0100289 OPTIMIZE = args.optimize
290 PREBUILD = args.prebuild
291 RELOCATE = args.relocate
292 SECONDARY_DEX = ""
293 TIME_OUT = "n" # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
294 TIMEOUT_DUMPER = "signal_dumper"
295 # Values in seconds.
296 TIME_OUT_EXTRA = 0
297 TIME_OUT_VALUE = args.timeout
298 USE_GDB = args.gdb
299 USE_GDBSERVER = args.gdbserver
300 GDBSERVER_PORT = args.gdbserver_port
301 USE_GDB_DEX2OAT = args.gdb_dex2oat
302 USE_JVM = args.jvm
303 VERIFY = "y" if args.verify else "n" # y=yes,n=no,s=softfail
304 ZYGOTE = ""
305 DEX_VERIFY = ""
306 INSTRUCTION_SET_FEATURES = args.instruction_set_features
307 ARGS = ""
308 VDEX_ARGS = ""
David Srbecky94d21412022-10-10 16:02:51 +0100309 DRY_RUN = args.dry_run
310 TEST_VDEX = args.vdex
311 TEST_DEX2OAT_DM = args.dex2oat_dm
312 TEST_RUNTIME_DM = args.runtime_dm
313 TEST_IS_NDEBUG = args.O
314 APP_IMAGE = args.app_image
315 SECONDARY_APP_IMAGE = args.secondary_app_image
316 SECONDARY_CLASS_LOADER_CONTEXT = args.secondary_class_loader_context
317 SECONDARY_COMPILATION = args.secondary_compilation
318 JVMTI_STRESS = False
319 JVMTI_REDEFINE_STRESS = args.jvmti_redefine_stress
320 JVMTI_STEP_STRESS = args.jvmti_step_stress
321 JVMTI_FIELD_STRESS = args.jvmti_field_stress
322 JVMTI_TRACE_STRESS = args.jvmti_trace_stress
323 PROFILE = args.profile
324 RANDOM_PROFILE = args.random_profile
325 DEX2OAT_TIMEOUT = args.dex2oat_timeout
326 DEX2OAT_RT_TIMEOUT = args.dex2oat_rt_timeout
327 CREATE_RUNNER = args.create_runner
328 INT_OPTS = ""
329 SIMPLEPERF = args.simpleperf
330 DEBUGGER_OPTS = ""
331 JVM_VERIFY_ARG = ""
332 LIB = args.lib
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100333
David Srbecky94d21412022-10-10 16:02:51 +0100334 # if True, run 'sync' before dalvikvm to make sure all files from
335 # build step (e.g. dex2oat) were finished writing.
336 SYNC_BEFORE_RUN = args.sync
Igor Murashkin271a0f82017-02-14 21:14:17 +0000337
David Srbecky94d21412022-10-10 16:02:51 +0100338 # When running a debug build, we want to run with all checks.
339 ANDROID_FLAGS += " -XX:SlowDebug=true"
340 # The same for dex2oatd, both prebuild and runtime-driven.
341 ANDROID_FLAGS += (" -Xcompiler-option --runtime-arg -Xcompiler-option "
342 "-XX:SlowDebug=true")
343 COMPILER_FLAGS = " --runtime-arg -XX:SlowDebug=true"
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700344
David Srbecky94d21412022-10-10 16:02:51 +0100345 # Let the compiler and runtime know that we are running tests.
346 COMPILE_FLAGS += " --compile-art-test"
347 ANDROID_FLAGS += " -Xcompiler-option --compile-art-test"
David Srbecky4fa07a52020-03-31 20:52:09 +0100348
David Srbecky94d21412022-10-10 16:02:51 +0100349 if USE_JVMTI:
350 IS_JVMTI_TEST = True
David Srbecky85786e72022-09-21 23:54:01 +0100351 # Secondary images block some tested behavior.
David Srbecky94d21412022-10-10 16:02:51 +0100352 SECONDARY_APP_IMAGE = False
353 if args.gc_stress:
David Srbecky85786e72022-09-21 23:54:01 +0100354 # Give an extra 20 mins if we are gc-stress.
David Srbecky94d21412022-10-10 16:02:51 +0100355 TIME_OUT_EXTRA += 1200
356 for arg in args.testlib:
357 ARGS += f" {arg}"
David Srbeckye4e701b2022-10-24 11:00:31 +0000358 for arg in args.test_args:
David Srbecky94d21412022-10-10 16:02:51 +0100359 ARGS += f" {arg}"
360 for arg in args.compiler_only_option:
361 COMPILE_FLAGS += f" {arg}"
362 for arg in args.Xcompiler_option:
363 FLAGS += f" -Xcompiler-option {arg}"
364 COMPILE_FLAGS += f" {arg}"
365 if args.secondary:
366 SECONDARY_DEX = f":{DEX_LOCATION}/{TEST_NAME}-ex.jar"
David Srbecky85786e72022-09-21 23:54:01 +0100367 # Enable cfg-append to make sure we get the dump for both dex files.
368 # (otherwise the runtime compilation of the secondary dex will overwrite
369 # the dump of the first one).
David Srbecky94d21412022-10-10 16:02:51 +0100370 FLAGS += " -Xcompiler-option --dump-cfg-append"
371 COMPILE_FLAGS += " --dump-cfg-append"
372 for arg in args.android_runtime_option:
373 ANDROID_FLAGS += f" {arg}"
374 for arg in args.runtime_option:
375 FLAGS += f" {arg}"
David Srbecky85786e72022-09-21 23:54:01 +0100376 if arg == "-Xmethod-trace":
David Srbecky94d21412022-10-10 16:02:51 +0100377 # Method tracing can slow some tests down a lot.
378 TIME_OUT_EXTRA += 1200
379 if args.compact_dex_level:
380 arg = args.compact_dex_level
381 COMPILE_FLAGS += f" --compact-dex-level={arg}"
382 if JVMTI_REDEFINE_STRESS:
David Srbecky85786e72022-09-21 23:54:01 +0100383 # APP_IMAGE doesn't really work with jvmti redefine stress
David Srbecky94d21412022-10-10 16:02:51 +0100384 SECONDARY_APP_IMAGE = False
385 JVMTI_STRESS = True
386 if JVMTI_REDEFINE_STRESS or JVMTI_STEP_STRESS or JVMTI_FIELD_STRESS or JVMTI_TRACE_STRESS:
387 USE_JVMTI = True
388 JVMTI_STRESS = True
389 if HOST:
390 ANDROID_ROOT = ANDROID_HOST_OUT
391 ANDROID_ART_ROOT = f"{ANDROID_HOST_OUT}/com.android.art"
392 ANDROID_I18N_ROOT = f"{ANDROID_HOST_OUT}/com.android.i18n"
393 ANDROID_TZDATA_ROOT = f"{ANDROID_HOST_OUT}/com.android.tzdata"
David Srbecky85786e72022-09-21 23:54:01 +0100394 # On host, we default to using the symlink, as the PREFER_32BIT
395 # configuration is the only configuration building a 32bit version of
396 # dex2oat.
David Srbecky94d21412022-10-10 16:02:51 +0100397 DEX2OAT_DEBUG_BINARY = "dex2oatd"
398 DEX2OAT_NDEBUG_BINARY = "dex2oat"
399 if BIONIC:
David Srbecky85786e72022-09-21 23:54:01 +0100400 # We need to create an ANDROID_ROOT because currently we cannot create
401 # the frameworks/libcore with linux_bionic so we need to use the normal
402 # host ones which are in a different location.
David Srbecky94d21412022-10-10 16:02:51 +0100403 CREATE_ANDROID_ROOT = True
404 if USE_ZIPAPEX:
David Srbecky85786e72022-09-21 23:54:01 +0100405 # TODO (b/119942078): Currently apex does not support
406 # symlink_preferred_arch so we will not have a dex2oatd to execute and
407 # need to manually provide
408 # dex2oatd64.
David Srbecky94d21412022-10-10 16:02:51 +0100409 DEX2OAT_DEBUG_BINARY = "dex2oatd64"
410 if WITH_AGENT:
411 USE_JVMTI = True
412 if DEBUGGER_AGENT:
413 DEBUGGER = "agent"
414 USE_JVMTI = True
415 TIME_OUT = "n"
416 if args.debug:
417 USE_JVMTI = True
418 DEBUGGER = "y"
419 TIME_OUT = "n"
420 if args.gdbserver_bin:
David Srbecky85786e72022-09-21 23:54:01 +0100421 arg = args.gdbserver_bin
David Srbecky94d21412022-10-10 16:02:51 +0100422 GDBSERVER_HOST = arg
423 GDBSERVER_DEVICE = arg
424 if args.gdbserver or args.gdb or USE_GDB_DEX2OAT:
David Srbecky94d21412022-10-10 16:02:51 +0100425 TIME_OUT = "n"
426 for arg in args.gdb_arg:
427 GDB_ARGS += f" {arg}"
428 if args.gdb_dex2oat_args:
David Srbecky85786e72022-09-21 23:54:01 +0100429 for arg in arg.split(";"):
David Srbecky94d21412022-10-10 16:02:51 +0100430 GDB_DEX2OAT_ARGS += f"{arg} "
431 if args.zygote:
432 ZYGOTE = "-Xzygote"
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000433 print("Spawning from zygote")
David Srbecky94d21412022-10-10 16:02:51 +0100434 if args.baseline:
435 FLAGS += " -Xcompiler-option --baseline"
436 COMPILE_FLAGS += " --baseline"
437 if args.verify_soft_fail:
438 VERIFY = "s"
439 if args.is64:
440 SUFFIX64 = "64"
441 ISA = "x86_64"
442 GDBSERVER_DEVICE = "gdbserver64"
443 DALVIKVM = "dalvikvm64"
444 LIBRARY_DIRECTORY = "lib64"
445 TEST_DIRECTORY = "nativetest64"
446 ARCHITECTURES_PATTERN = ARCHITECTURES_64
447 GET_DEVICE_ISA_BITNESS_FLAG = "--64"
448 DEX2OAT_NDEBUG_BINARY = "dex2oat64"
449 DEX2OAT_DEBUG_BINARY = "dex2oatd64"
450 if args.vdex_filter:
451 option = args.vdex_filter
452 VDEX_ARGS += f" --compiler-filter={option}"
453 if args.vdex_arg:
David Srbecky85786e72022-09-21 23:54:01 +0100454 arg = args.vdex_arg
David Srbecky94d21412022-10-10 16:02:51 +0100455 VDEX_ARGS += f" {arg}"
David Srbecky4c93c252022-09-08 13:17:44 +0100456
Roland Levillainb59f5fb2020-02-19 16:22:48 +0000457# HACK: Force the use of `signal_dumper` on host.
David Srbecky94d21412022-10-10 16:02:51 +0100458 if HOST:
459 TIME_OUT = "timeout"
Roland Levillain505e56b2019-11-20 08:49:24 +0000460
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700461# If you change this, update the timeout in testrunner.py as well.
David Srbecky94d21412022-10-10 16:02:51 +0100462 if not TIME_OUT_VALUE:
463 # 10 minutes is the default.
464 TIME_OUT_VALUE = 600
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700465
David Srbecky94d21412022-10-10 16:02:51 +0100466 # For sanitized builds use a larger base.
467 # TODO: Consider sanitized target builds?
468 if SANITIZE_HOST != "":
469 TIME_OUT_VALUE = 1500 # 25 minutes.
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700470
David Srbecky94d21412022-10-10 16:02:51 +0100471 TIME_OUT_VALUE += TIME_OUT_EXTRA
Andreas Gampe6ad30a22019-09-12 15:12:36 -0700472
Andreas Gampe29bfb0c2019-09-12 15:17:43 -0700473# Escape hatch for slow hosts or devices. Accept an environment variable as a timeout factor.
David Srbecky94d21412022-10-10 16:02:51 +0100474 if ART_TIME_OUT_MULTIPLIER:
475 TIME_OUT_VALUE *= ART_TIME_OUT_MULTIPLIER
Andreas Gampe29bfb0c2019-09-12 15:17:43 -0700476
Roland Levillain76cfe612017-10-30 13:14:28 +0000477# The DEX_LOCATION with the chroot prefix, if any.
David Srbecky94d21412022-10-10 16:02:51 +0100478 CHROOT_DEX_LOCATION = f"{CHROOT}{DEX_LOCATION}"
Roland Levillain76cfe612017-10-30 13:14:28 +0000479
David Srbecky94d21412022-10-10 16:02:51 +0100480 # If running on device, determine the ISA of the device.
481 if not HOST and not USE_JVM:
David Srbeckyefb61262022-11-30 16:49:33 +0000482 ISA = get_target_arch(args.is64)
Roland Levillain72f67742019-03-06 15:48:08 +0000483
David Srbecky94d21412022-10-10 16:02:51 +0100484 if not USE_JVM:
485 FLAGS += f" {ANDROID_FLAGS}"
Alex Lightbaac7e42018-06-08 15:30:11 +0000486 # we don't want to be trying to get adbconnections since the plugin might
487 # not have been built.
David Srbecky94d21412022-10-10 16:02:51 +0100488 FLAGS += " -XjdwpProvider:none"
David Srbeckybfa76b32022-06-20 21:30:56 +0100489 for feature in EXPERIMENTAL:
David Srbecky94d21412022-10-10 16:02:51 +0100490 FLAGS += f" -Xexperimental:{feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:{feature}"
491 COMPILE_FLAGS = f"{COMPILE_FLAGS} --runtime-arg -Xexperimental:{feature}"
Alex Light8a0e0332015-10-26 10:11:58 -0700492
David Srbecky94d21412022-10-10 16:02:51 +0100493 if CREATE_ANDROID_ROOT:
494 ANDROID_ROOT = f"{DEX_LOCATION}/android-root"
Alex Light680cbf22018-10-31 11:00:19 -0700495
David Srbecky94d21412022-10-10 16:02:51 +0100496 if ZYGOTE == "":
David Srbeckyc6d2a792022-08-18 14:15:43 +0100497 if OPTIMIZE:
David Srbecky94d21412022-10-10 16:02:51 +0100498 if VERIFY == "y":
499 DEX_OPTIMIZE = "-Xdexopt:verified"
500 else:
501 DEX_OPTIMIZE = "-Xdexopt:all"
David Srbeckybfa76b32022-06-20 21:30:56 +0100502 else:
David Srbecky94d21412022-10-10 16:02:51 +0100503 DEX_OPTIMIZE = "-Xdexopt:none"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100504
David Srbeckybfa76b32022-06-20 21:30:56 +0100505 if VERIFY == "y":
David Srbecky94d21412022-10-10 16:02:51 +0100506 JVM_VERIFY_ARG = "-Xverify:all"
David Srbeckybfa76b32022-06-20 21:30:56 +0100507 elif VERIFY == "s":
David Srbecky94d21412022-10-10 16:02:51 +0100508 JVM_VERIFY_ARG = "Xverify:all"
509 DEX_VERIFY = "-Xverify:softfail"
David Srbecky94d21412022-10-10 16:02:51 +0100510 else: # VERIFY == "n"
511 DEX_VERIFY = "-Xverify:none"
512 JVM_VERIFY_ARG = "-Xverify:none"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100513
David Srbecky94d21412022-10-10 16:02:51 +0100514 if DEBUGGER == "y":
515 # Use this instead for ddms and connect by running 'ddms':
516 # DEBUGGER_OPTS="-XjdwpOptions=server=y,suspend=y -XjdwpProvider:adbconnection"
517 # TODO: add a separate --ddms option?
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100518
David Srbecky94d21412022-10-10 16:02:51 +0100519 PORT = 12345
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000520 print("Waiting for jdb to connect:")
David Srbecky94d21412022-10-10 16:02:51 +0100521 if not HOST:
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000522 print(f" adb forward tcp:{PORT} tcp:{PORT}")
523 print(f" jdb -attach localhost:{PORT}")
David Srbecky94d21412022-10-10 16:02:51 +0100524 if not USE_JVM:
525 # Use the default libjdwp agent. Use --debug-agent to use a custom one.
526 DEBUGGER_OPTS = f"-agentpath:libjdwp.so=transport=dt_socket,address={PORT},server=y,suspend=y -XjdwpProvider:internal"
527 else:
528 DEBUGGER_OPTS = f"-agentlib:jdwp=transport=dt_socket,address={PORT},server=y,suspend=y"
529 elif DEBUGGER == "agent":
530 PORT = 12345
531 # TODO Support ddms connection and support target.
David Srbeckye4e701b2022-10-24 11:00:31 +0000532 assert HOST, "--debug-agent not supported yet for target!"
David Srbecky94d21412022-10-10 16:02:51 +0100533 AGENTPATH = DEBUGGER_AGENT
534 if WRAP_DEBUGGER_AGENT:
535 WRAPPROPS = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}/libwrapagentpropertiesd.so"
536 if TEST_IS_NDEBUG:
537 WRAPPROPS = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}/libwrapagentproperties.so"
538 AGENTPATH = f"{WRAPPROPS}={ANDROID_BUILD_TOP}/art/tools/libjdwp-compat.props,{AGENTPATH}"
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000539 print(f"Connect to localhost:{PORT}")
David Srbecky94d21412022-10-10 16:02:51 +0100540 DEBUGGER_OPTS = f"-agentpath:{AGENTPATH}=transport=dt_socket,address={PORT},server=y,suspend=y"
Alex Lightc281ba52017-10-11 11:35:55 -0700541
David Srbecky94d21412022-10-10 16:02:51 +0100542 for agent in WITH_AGENT:
543 FLAGS += f" -agentpath:{agent}"
Alex Light0e151e72017-10-25 10:50:35 -0700544
David Srbecky94d21412022-10-10 16:02:51 +0100545 if USE_JVMTI:
546 if not USE_JVM:
547 plugin = "libopenjdkjvmtid.so"
548 if TEST_IS_NDEBUG:
549 plugin = "libopenjdkjvmti.so"
550 # We used to add flags here that made the runtime debuggable but that is not
551 # needed anymore since the plugin can do it for us now.
552 FLAGS += f" -Xplugin:{plugin}"
Mythri Allef0d8b442021-11-03 17:28:24 +0000553
David Srbecky94d21412022-10-10 16:02:51 +0100554 # For jvmti tests, set the threshold of compilation to 1, so we jit early to
555 # provide better test coverage for jvmti + jit. This means we won't run
556 # the default --jit configuration but it is not too important test scenario for
557 # jvmti tests. This is art specific flag, so don't use it with jvm.
558 FLAGS += " -Xjitthreshold:1"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100559
Alex Light280e6c32020-03-03 13:52:07 -0800560# Add the libdir to the argv passed to the main function.
David Srbecky94d21412022-10-10 16:02:51 +0100561 if ADD_LIBDIR_ARGUMENTS:
562 if HOST:
563 ARGS += f" {ANDROID_HOST_OUT}/{TEST_DIRECTORY}/"
564 else:
565 ARGS += f" /data/{TEST_DIRECTORY}/art/{ISA}/"
David Srbeckyc6d2a792022-08-18 14:15:43 +0100566 if IS_JVMTI_TEST:
David Srbecky94d21412022-10-10 16:02:51 +0100567 agent = "libtiagentd.so"
568 lib = "tiagentd"
569 if TEST_IS_NDEBUG:
570 agent = "libtiagent.so"
571 lib = "tiagent"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100572
David Srbecky94d21412022-10-10 16:02:51 +0100573 ARGS += f" {lib}"
574 if USE_JVM:
575 FLAGS += f" -agentpath:{ANDROID_HOST_OUT}/nativetest64/{agent}={TEST_NAME},jvm"
576 else:
577 FLAGS += f" -agentpath:{agent}={TEST_NAME},art"
Artem Serov55e509f2022-01-26 11:59:22 +0000578
David Srbecky94d21412022-10-10 16:02:51 +0100579 if JVMTI_STRESS:
580 agent = "libtistressd.so"
581 if TEST_IS_NDEBUG:
582 agent = "libtistress.so"
583
584 # Just give it a default start so we can always add ',' to it.
585 agent_args = "jvmti-stress"
586 if JVMTI_REDEFINE_STRESS:
587 # We really cannot do this on RI so don't both passing it in that case.
588 if not USE_JVM:
589 agent_args = f"{agent_args},redefine"
590 if JVMTI_FIELD_STRESS:
591 agent_args = f"{agent_args},field"
592 if JVMTI_STEP_STRESS:
593 agent_args = f"{agent_args},step"
594 if JVMTI_TRACE_STRESS:
595 agent_args = f"{agent_args},trace"
596 # In the future add onto this;
597 if USE_JVM:
598 FLAGS += f" -agentpath:{ANDROID_HOST_OUT}/nativetest64/{agent}={agent_args}"
599 else:
600 FLAGS += f" -agentpath:{agent}={agent_args}"
601
602 if USE_JVM:
David Srbeckyefb61262022-11-30 16:49:33 +0000603 ctx.export(
604 ANDROID_I18N_ROOT = ANDROID_I18N_ROOT,
605 DEX_LOCATION = DEX_LOCATION,
606 JAVA_HOME = os.environ["JAVA_HOME"],
607 LANG = "en_US.UTF-8", # Needed to enable unicode and make the output is deterministic.
608 LD_LIBRARY_PATH = f"{ANDROID_HOST_OUT}/lib64",
609 )
David Srbecky94d21412022-10-10 16:02:51 +0100610 # Some jvmti tests are flaky without -Xint on the RI.
611 if IS_JVMTI_TEST:
612 FLAGS += " -Xint"
613 # Xmx is necessary since we don't pass down the ART flags to JVM.
614 # We pass the classes2 path whether it's used (src-multidex) or not.
David Srbeckye4e701b2022-10-24 11:00:31 +0000615 cmdline = f"{JAVA} {DEBUGGER_OPTS} {JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 {FLAGS} {MAIN} {ARGS}"
David Srbeckyefb61262022-11-30 16:49:33 +0000616 ctx.run(tee(cmdline), expected_exit_code=args.expected_exit_code)
617 return
David Srbecky94d21412022-10-10 16:02:51 +0100618
619 b_path = get_apex_bootclasspath(HOST)
620 b_path_locations = get_apex_bootclasspath_locations(HOST)
621
622 BCPEX = ""
623 if isfile(f"{TEST_NAME}-bcpex.jar"):
624 BCPEX = f":{DEX_LOCATION}/{TEST_NAME}-bcpex.jar"
Vladimir Marko239c9412022-01-05 16:09:09 +0000625
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000626 # Pass down the bootclasspath
David Srbecky94d21412022-10-10 16:02:51 +0100627 FLAGS += f" -Xbootclasspath:{b_path}{BCPEX}"
628 FLAGS += f" -Xbootclasspath-locations:{b_path_locations}{BCPEX}"
629 COMPILE_FLAGS += f" --runtime-arg -Xbootclasspath:{b_path}"
630 COMPILE_FLAGS += f" --runtime-arg -Xbootclasspath-locations:{b_path_locations}"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100631
David Srbecky94d21412022-10-10 16:02:51 +0100632 if not HAVE_IMAGE:
Andreas Gampe1078d152017-11-07 18:00:54 -0800633 # Disable image dex2oat - this will forbid the runtime to patch or compile an image.
David Srbecky94d21412022-10-10 16:02:51 +0100634 FLAGS += " -Xnoimage-dex2oat"
Andreas Gampe1078d152017-11-07 18:00:54 -0800635
636 # We'll abuse a second flag here to test different behavior. If --relocate, use the
637 # existing image - relocation will fail as patching is disallowed. If --no-relocate,
638 # pass a non-existent image - compilation will fail as dex2oat is disallowed.
David Srbeckyc6d2a792022-08-18 14:15:43 +0100639 if not RELOCATE:
David Srbecky94d21412022-10-10 16:02:51 +0100640 BOOT_IMAGE = "/system/non-existent/boot.art"
Nicolas Geoffray48eb8392022-02-24 16:20:18 +0000641 # App images cannot be generated without a boot image.
David Srbecky94d21412022-10-10 16:02:51 +0100642 APP_IMAGE = False
643 DALVIKVM_BOOT_OPT = f"-Ximage:{BOOT_IMAGE}"
Nicolas Geoffray288a4a22014-10-07 11:02:40 +0100644
David Srbecky94d21412022-10-10 16:02:51 +0100645 if USE_GDB_DEX2OAT:
David Srbeckye4e701b2022-10-24 11:00:31 +0000646 assert HOST, "The --gdb-dex2oat option is not yet implemented for target."
Dmitrii Ishcheikine60131a2022-10-07 11:45:44 +0000647
David Srbeckyefb61262022-11-30 16:49:33 +0000648 assert not USE_GDBSERVER, "Not supported"
David Srbecky94d21412022-10-10 16:02:51 +0100649 if USE_GDB:
David Srbeckye4e701b2022-10-24 11:00:31 +0000650 if not HOST:
David Srbecky94d21412022-10-10 16:02:51 +0100651 # We might not have any hostname resolution if we are using a chroot.
652 GDB = f"{GDBSERVER_DEVICE} --no-startup-with-shell 127.0.0.1{GDBSERVER_PORT}"
653 else:
David Srbeckyefb61262022-11-30 16:49:33 +0000654 GDB = "gdb"
655 GDB_ARGS += f" --args {DALVIKVM}"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800656
David Srbecky94d21412022-10-10 16:02:51 +0100657 if INTERPRETER:
658 INT_OPTS += " -Xint"
Nicolas Geoffrayc76fbf02021-04-06 08:59:17 +0100659
David Srbecky94d21412022-10-10 16:02:51 +0100660 if JIT:
661 INT_OPTS += " -Xusejit:true"
662 else:
663 INT_OPTS += " -Xusejit:false"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100664
David Srbecky94d21412022-10-10 16:02:51 +0100665 if INTERPRETER or JIT:
666 if VERIFY == "y":
667 INT_OPTS += " -Xcompiler-option --compiler-filter=verify"
668 COMPILE_FLAGS += " --compiler-filter=verify"
669 elif VERIFY == "s":
670 INT_OPTS += " -Xcompiler-option --compiler-filter=extract"
671 COMPILE_FLAGS += " --compiler-filter=extract"
672 DEX_VERIFY = f"{DEX_VERIFY} -Xverify:softfail"
673 else: # VERIFY == "n"
674 INT_OPTS += " -Xcompiler-option --compiler-filter=assume-verified"
675 COMPILE_FLAGS += " --compiler-filter=assume-verified"
676 DEX_VERIFY = f"{DEX_VERIFY} -Xverify:none"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100677
David Srbecky94d21412022-10-10 16:02:51 +0100678 JNI_OPTS = "-Xjnigreflimit:512 -Xcheck:jni"
679
680 COMPILE_FLAGS += " --runtime-arg -Xnorelocate"
681 if RELOCATE:
682 FLAGS += " -Xrelocate"
683 else:
684 FLAGS += " -Xnorelocate"
685
686 if BIONIC:
687 # This is the location that soong drops linux_bionic builds. Despite being
688 # called linux_bionic-x86 the build is actually amd64 (x86_64) only.
David Srbeckye4e701b2022-10-24 11:00:31 +0000689 assert path.exists(f"{OUT_DIR}/soong/host/linux_bionic-x86"), (
690 "linux_bionic-x86 target doesn't seem to have been built!")
David Srbecky94d21412022-10-10 16:02:51 +0100691 # Set TIMEOUT_DUMPER manually so it works even with apex's
692 TIMEOUT_DUMPER = f"{OUT_DIR}/soong/host/linux_bionic-x86/bin/signal_dumper"
Alex Light680cbf22018-10-31 11:00:19 -0700693
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000694 # Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
695 # when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
696 # full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
697 # remaining '/' into '@'.
David Srbecky94d21412022-10-10 16:02:51 +0100698 if HOST:
David Srbeckyefb61262022-11-30 16:49:33 +0000699 max_filename_size = int(check_output(f"getconf NAME_MAX {DEX_LOCATION}", shell=True))
David Srbecky94d21412022-10-10 16:02:51 +0100700 else:
701 # There is no getconf on device, fallback to standard value.
702 # See NAME_MAX in kernel <linux/limits.h>
703 max_filename_size = 255
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000704 # Compute VDEX_NAME.
David Srbecky94d21412022-10-10 16:02:51 +0100705 DEX_LOCATION_STRIPPED = DEX_LOCATION.lstrip("/")
706 VDEX_NAME = f"{DEX_LOCATION_STRIPPED}@{TEST_NAME}.jar@classes.vdex".replace(
707 "/", "@")
David Srbeckye4e701b2022-10-24 11:00:31 +0000708 assert len(VDEX_NAME) <= max_filename_size, "Dex location path too long"
Wojciech Staszkiewicze6636532016-09-23 10:59:55 -0700709
David Srbecky94d21412022-10-10 16:02:51 +0100710 if HOST:
711 # On host, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the `bin`
712 # directory under the "Android Root" (usually `out/host/linux-x86`).
713 #
714 # TODO(b/130295968): Adjust this if/when ART host artifacts are installed
715 # under the ART root (usually `out/host/linux-x86/com.android.art`).
716 ANDROID_ART_BIN_DIR = f"{ANDROID_ROOT}/bin"
717 else:
718 # On target, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the ART
719 # APEX's `bin` directory. This means the linker will observe the ART APEX
720 # linker configuration file (`/apex/com.android.art/etc/ld.config.txt`) for
721 # these binaries.
722 ANDROID_ART_BIN_DIR = f"{ANDROID_ART_ROOT}/bin"
Alex Light20802ca2018-12-05 15:36:03 -0800723
David Srbecky94d21412022-10-10 16:02:51 +0100724 profman_cmdline = "true"
725 dex2oat_cmdline = "true"
726 vdex_cmdline = "true"
727 dm_cmdline = "true"
728 mkdir_locations = f"{DEX_LOCATION}/dalvik-cache/{ISA}"
729 strip_cmdline = "true"
730 sync_cmdline = "true"
731 linkroot_cmdline = "true"
732 linkroot_overlay_cmdline = "true"
733 setupapex_cmdline = "true"
734 installapex_cmdline = "true"
735 installapex_test_cmdline = "true"
Alex Light680cbf22018-10-31 11:00:19 -0700736
David Srbecky94d21412022-10-10 16:02:51 +0100737 def linkdirs(host_out: str, root: str):
738 dirs = list(filter(os.path.isdir, glob.glob(os.path.join(host_out, "*"))))
739 # Also create a link for the boot image.
740 dirs.append(f"{ANDROID_HOST_OUT}/apex/art_boot_images")
741 return " && ".join(f"ln -sf {dir} {root}" for dir in dirs)
Alex Light680cbf22018-10-31 11:00:19 -0700742
David Srbecky94d21412022-10-10 16:02:51 +0100743 if CREATE_ANDROID_ROOT:
744 mkdir_locations += f" {ANDROID_ROOT}"
745 linkroot_cmdline = linkdirs(ANDROID_HOST_OUT, ANDROID_ROOT)
746 if BIONIC:
747 # TODO Make this overlay more generic.
748 linkroot_overlay_cmdline = linkdirs(
749 f"{OUT_DIR}/soong/host/linux_bionic-x86", ANDROID_ROOT)
750 # Replace the boot image to a location expected by the runtime.
751 DALVIKVM_BOOT_OPT = f"-Ximage:{ANDROID_ROOT}/art_boot_images/javalib/boot.art"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100752
David Srbecky94d21412022-10-10 16:02:51 +0100753 if USE_ZIPAPEX:
754 # TODO Currently this only works for linux_bionic zipapexes because those are
755 # stripped and so small enough that the ulimit doesn't kill us.
756 mkdir_locations += f" {DEX_LOCATION}/zipapex"
757 setupapex_cmdline = f"unzip -o -u {ZIPAPEX_LOC} apex_payload.zip -d {DEX_LOCATION}"
758 installapex_cmdline = f"unzip -o -u {DEX_LOCATION}/apex_payload.zip -d {DEX_LOCATION}/zipapex"
759 ANDROID_ART_BIN_DIR = f"{DEX_LOCATION}/zipapex/bin"
760 elif USE_EXTRACTED_ZIPAPEX:
761 # Just symlink the zipapex binaries
762 ANDROID_ART_BIN_DIR = f"{DEX_LOCATION}/zipapex/bin"
763 # Force since some tests manually run this file twice.
764 # If the {RUN} is executed multiple times we don't need to recreate the link
David Srbecky94d21412022-10-10 16:02:51 +0100765 installapex_cmdline = f"ln -s -f --verbose {EXTRACTED_ZIPAPEX_LOC} {DEX_LOCATION}/zipapex"
Alex Light20802ca2018-12-05 15:36:03 -0800766
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000767 # PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
768 # specific profile to run properly.
David Srbeckyc6d2a792022-08-18 14:15:43 +0100769 if PROFILE or RANDOM_PROFILE:
David Srbecky94d21412022-10-10 16:02:51 +0100770 profman_cmdline = f"{ANDROID_ART_BIN_DIR}/profman \
771 --apk={DEX_LOCATION}/{TEST_NAME}.jar \
772 --dex-location={DEX_LOCATION}/{TEST_NAME}.jar"
773
774 if isfile(f"{TEST_NAME}-ex.jar") and SECONDARY_COMPILATION:
775 profman_cmdline = f"{profman_cmdline} \
776 --apk={DEX_LOCATION}/{TEST_NAME}-ex.jar \
777 --dex-location={DEX_LOCATION}/{TEST_NAME}-ex.jar"
778
779 COMPILE_FLAGS = f"{COMPILE_FLAGS} --profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
780 FLAGS = f"{FLAGS} -Xcompiler-option --profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
781 if PROFILE:
782 profman_cmdline = f"{profman_cmdline} --create-profile-from={DEX_LOCATION}/profile \
783 --reference-profile-file={DEX_LOCATION}/{TEST_NAME}.prof"
784
David Srbeckybfa76b32022-06-20 21:30:56 +0100785 else:
David Srbecky94d21412022-10-10 16:02:51 +0100786 profman_cmdline = f"{profman_cmdline} --generate-test-profile={DEX_LOCATION}/{TEST_NAME}.prof \
787 --generate-test-profile-seed=0"
788
789 def get_prebuilt_lldb_path():
790 CLANG_BASE = "prebuilts/clang/host"
David Srbeckyefb61262022-11-30 16:49:33 +0000791 CLANG_VERSION = check_output(
David Srbecky94d21412022-10-10 16:02:51 +0100792 f"{ANDROID_BUILD_TOP}/build/soong/scripts/get_clang_version.py"
David Srbeckyefb61262022-11-30 16:49:33 +0000793 ).strip()
794 uname = check_output("uname -s", shell=True).strip()
David Srbecky94d21412022-10-10 16:02:51 +0100795 if uname == "Darwin":
796 PREBUILT_NAME = "darwin-x86"
797 elif uname == "Linux":
798 PREBUILT_NAME = "linux-x86"
799 else:
800 print(
801 "Unknown host $(uname -s). Unsupported for debugging dex2oat with LLDB.",
802 file=sys.stderr)
803 return
804 CLANG_PREBUILT_HOST_PATH = f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}/{CLANG_VERSION}"
805 # If the clang prebuilt directory exists and the reported clang version
806 # string does not, then it is likely that the clang version reported by the
807 # get_clang_version.py script does not match the expected directory name.
David Srbeckye4e701b2022-10-24 11:00:31 +0000808 if isdir(f"{ANDROID_BUILD_TOP}/{CLANG_BASE}/{PREBUILT_NAME}"):
809 assert isdir(CLANG_PREBUILT_HOST_PATH), (
810 "The prebuilt clang directory exists, but the specific "
811 "clang\nversion reported by get_clang_version.py does not exist in "
812 "that path.\nPlease make sure that the reported clang version "
813 "resides in the\nprebuilt clang directory!")
David Srbecky94d21412022-10-10 16:02:51 +0100814
815 # The lldb-server binary is a dependency of lldb.
816 os.environ[
817 "LLDB_DEBUGSERVER_PATH"] = f"{CLANG_PREBUILT_HOST_PATH}/runtimes_ndk_cxx/x86_64/lldb-server"
818
819 # Set the current terminfo directory to TERMINFO so that LLDB can read the
820 # termcap database.
David Srbeckyefb61262022-11-30 16:49:33 +0000821 terminfo = re.search("/.*/terminfo/", check_output("infocmp"))
David Srbecky94d21412022-10-10 16:02:51 +0100822 if terminfo:
823 os.environ["TERMINFO"] = terminfo[0]
824
825 return f"{CLANG_PREBUILT_HOST_PATH}/bin/lldb.sh"
826
827 def write_dex2oat_cmdlines(name: str):
David Srbeckye4e701b2022-10-24 11:00:31 +0000828 nonlocal dex2oat_cmdline, dm_cmdline, vdex_cmdline
David Srbecky94d21412022-10-10 16:02:51 +0100829
830 class_loader_context = ""
831 enable_app_image = False
832 if APP_IMAGE:
833 enable_app_image = True
834
835 # If the name ends in -ex then this is a secondary dex file
836 if name.endswith("-ex"):
837 # Lazily realize the default value in case DEX_LOCATION/TEST_NAME change
David Srbeckye4e701b2022-10-24 11:00:31 +0000838 nonlocal SECONDARY_CLASS_LOADER_CONTEXT
David Srbecky94d21412022-10-10 16:02:51 +0100839 if SECONDARY_CLASS_LOADER_CONTEXT == "":
840 if SECONDARY_DEX == "":
841 # Tests without `--secondary` load the "-ex" jar in a separate PathClassLoader
842 # that is a child of the main PathClassLoader. If the class loader is constructed
843 # in any other way, the test needs to specify the secondary CLC explicitly.
844 SECONDARY_CLASS_LOADER_CONTEXT = f"PCL[];PCL[{DEX_LOCATION}/{TEST_NAME}.jar]"
845 else:
846 # Tests with `--secondary` load the `-ex` jar a part of the main PathClassLoader.
847 SECONDARY_CLASS_LOADER_CONTEXT = f"PCL[{DEX_LOCATION}/{TEST_NAME}.jar]"
848 class_loader_context = f"'--class-loader-context={SECONDARY_CLASS_LOADER_CONTEXT}'"
849 enable_app_image = enable_app_image and SECONDARY_APP_IMAGE
850
851 app_image = ""
852 if enable_app_image:
853 app_image = f"--app-image-file={DEX_LOCATION}/oat/{ISA}/{name}.art --resolve-startup-const-strings=true"
854
David Srbeckye4e701b2022-10-24 11:00:31 +0000855 nonlocal GDB_DEX2OAT, GDB_DEX2OAT_ARGS
David Srbecky94d21412022-10-10 16:02:51 +0100856 if USE_GDB_DEX2OAT:
857 prebuilt_lldb_path = get_prebuilt_lldb_path()
858 GDB_DEX2OAT = f"{prebuilt_lldb_path} -f"
859 GDB_DEX2OAT_ARGS += " -- "
860
861 dex2oat_binary = DEX2OAT_DEBUG_BINARY
862 if TEST_IS_NDEBUG:
863 dex2oat_binary = DEX2OAT_NDEBUG_BINARY
864 dex2oat_cmdline = f"{INVOKE_WITH} {GDB_DEX2OAT} \
865 {ANDROID_ART_BIN_DIR}/{dex2oat_binary} \
866 {GDB_DEX2OAT_ARGS} \
867 {COMPILE_FLAGS} \
868 --boot-image={BOOT_IMAGE} \
869 --dex-file={DEX_LOCATION}/{name}.jar \
870 --oat-file={DEX_LOCATION}/oat/{ISA}/{name}.odex \
871 {app_image} \
872 --generate-mini-debug-info \
873 --instruction-set={ISA} \
874 {class_loader_context}"
875
876 if INSTRUCTION_SET_FEATURES != "":
877 dex2oat_cmdline += f" --instruction-set-features={INSTRUCTION_SET_FEATURES}"
878
879 # Add in a timeout. This is important for testing the compilation/verification time of
880 # pathological cases. We do not append a timeout when debugging dex2oat because we
881 # do not want it to exit while debugging.
882 # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
883 # now. We should try to improve this.
884 # The current value is rather arbitrary. run-tests should compile quickly.
885 # Watchdog timeout is in milliseconds so add 3 '0's to the dex2oat timeout.
886 if HOST and not USE_GDB_DEX2OAT:
887 # Use SIGRTMIN+2 to try to dump threads.
888 # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
889 dex2oat_cmdline = f"timeout -k {DEX2OAT_TIMEOUT}s -s SIGRTMIN+2 {DEX2OAT_RT_TIMEOUT}s {dex2oat_cmdline} --watchdog-timeout={DEX2OAT_TIMEOUT}000"
890 if PROFILE or RANDOM_PROFILE:
891 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --input-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex --output-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex"
892 elif TEST_VDEX:
893 if VDEX_ARGS == "":
894 # If no arguments need to be passed, just delete the odex file so that the runtime only picks up the vdex file.
895 vdex_cmdline = f"rm {DEX_LOCATION}/oat/{ISA}/{name}.odex"
896 else:
897 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --compact-dex-level=none --input-vdex={DEX_LOCATION}/oat/{ISA}/{name}.vdex"
898 elif TEST_DEX2OAT_DM:
899 vdex_cmdline = f"{dex2oat_cmdline} {VDEX_ARGS} --dump-timings --dm-file={DEX_LOCATION}/oat/{ISA}/{name}.dm"
900 dex2oat_cmdline = f"{dex2oat_cmdline} --copy-dex-files=false --output-vdex={DEX_LOCATION}/oat/{ISA}/primary.vdex"
901 dm_cmdline = f"zip -qj {DEX_LOCATION}/oat/{ISA}/{name}.dm {DEX_LOCATION}/oat/{ISA}/primary.vdex"
902 elif TEST_RUNTIME_DM:
903 dex2oat_cmdline = f"{dex2oat_cmdline} --copy-dex-files=false --output-vdex={DEX_LOCATION}/oat/{ISA}/primary.vdex"
904 dm_cmdline = f"zip -qj {DEX_LOCATION}/{name}.dm {DEX_LOCATION}/oat/{ISA}/primary.vdex"
Andreas Gampe038a1982020-03-11 23:06:42 +0000905
906# Enable mini-debug-info for JIT (if JIT is used).
Andreas Gampe038a1982020-03-11 23:06:42 +0000907
David Srbecky94d21412022-10-10 16:02:51 +0100908 FLAGS += " -Xcompiler-option --generate-mini-debug-info"
Andreas Gampe038a1982020-03-11 23:06:42 +0000909
David Srbecky94d21412022-10-10 16:02:51 +0100910 if PREBUILD:
911 mkdir_locations += f" {DEX_LOCATION}/oat/{ISA}"
Andreas Gampe038a1982020-03-11 23:06:42 +0000912
David Srbecky94d21412022-10-10 16:02:51 +0100913 # "Primary".
914 write_dex2oat_cmdlines(TEST_NAME)
915 dex2oat_cmdline = re.sub(" +", " ", dex2oat_cmdline)
916 dm_cmdline = re.sub(" +", " ", dm_cmdline)
917 vdex_cmdline = re.sub(" +", " ", vdex_cmdline)
Andreas Gampe038a1982020-03-11 23:06:42 +0000918
David Srbecky94d21412022-10-10 16:02:51 +0100919 # Enable mini-debug-info for JIT (if JIT is used).
920 FLAGS += " -Xcompiler-option --generate-mini-debug-info"
Andreas Gampe038a1982020-03-11 23:06:42 +0000921
David Srbecky94d21412022-10-10 16:02:51 +0100922 if isfile(f"{TEST_NAME}-ex.jar") and SECONDARY_COMPILATION:
923 # "Secondary" for test coverage.
Andreas Gampe038a1982020-03-11 23:06:42 +0000924
David Srbecky94d21412022-10-10 16:02:51 +0100925 # Store primary values.
926 base_dex2oat_cmdline = dex2oat_cmdline
927 base_dm_cmdline = dm_cmdline
928 base_vdex_cmdline = vdex_cmdline
Andreas Gampe038a1982020-03-11 23:06:42 +0000929
David Srbecky94d21412022-10-10 16:02:51 +0100930 write_dex2oat_cmdlines(f"{TEST_NAME}-ex")
931 dex2oat_cmdline = re.sub(" +", " ", dex2oat_cmdline)
932 dm_cmdline = re.sub(" +", " ", dm_cmdline)
933 vdex_cmdline = re.sub(" +", " ", vdex_cmdline)
Andreas Gampe4d2ef332015-08-05 09:24:45 -0700934
David Srbecky94d21412022-10-10 16:02:51 +0100935 # Concatenate.
936 dex2oat_cmdline = f"{base_dex2oat_cmdline} && {dex2oat_cmdline}"
937 dm_cmdline = base_dm_cmdline # Only use primary dm.
938 vdex_cmdline = f"{base_vdex_cmdline} && {vdex_cmdline}"
Igor Murashkin271a0f82017-02-14 21:14:17 +0000939
David Srbecky94d21412022-10-10 16:02:51 +0100940 if SYNC_BEFORE_RUN:
941 sync_cmdline = "sync"
942
943 DALVIKVM_ISA_FEATURES_ARGS = ""
944 if INSTRUCTION_SET_FEATURES != "":
945 DALVIKVM_ISA_FEATURES_ARGS = f"-Xcompiler-option --instruction-set-features={INSTRUCTION_SET_FEATURES}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100946
Nicolas Geoffrayc5256042015-12-26 19:41:37 +0000947# java.io.tmpdir can only be set at launch time.
David Srbecky94d21412022-10-10 16:02:51 +0100948 TMP_DIR_OPTION = ""
949 if not HOST:
950 TMP_DIR_OPTION = "-Djava.io.tmpdir=/data/local/tmp"
Nicolas Geoffrayc5256042015-12-26 19:41:37 +0000951
Alex Lightb8012102019-11-13 01:41:04 +0000952# The build servers have an ancient version of bash so we cannot use @Q.
David Srbecky94d21412022-10-10 16:02:51 +0100953 QUOTED_DALVIKVM_BOOT_OPT = shlex.quote(DALVIKVM_BOOT_OPT)
Alex Lightb8012102019-11-13 01:41:04 +0000954
David Srbecky94d21412022-10-10 16:02:51 +0100955 DALVIKVM_CLASSPATH = f"{DEX_LOCATION}/{TEST_NAME}.jar"
956 if isfile(f"{TEST_NAME}-aotex.jar"):
957 DALVIKVM_CLASSPATH = f"{DALVIKVM_CLASSPATH}:{DEX_LOCATION}/{TEST_NAME}-aotex.jar"
958 DALVIKVM_CLASSPATH = f"{DALVIKVM_CLASSPATH}{SECONDARY_DEX}"
Vladimir Marko239c9412022-01-05 16:09:09 +0000959
David Srbecky94d21412022-10-10 16:02:51 +0100960 # We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
961 # b/27185632
962 # b/24664297
Ulya Trafimovich7f7f6442021-11-05 15:26:13 +0000963
David Srbecky94d21412022-10-10 16:02:51 +0100964 dalvikvm_cmdline = f"{INVOKE_WITH} {GDB} {ANDROID_ART_BIN_DIR}/{DALVIKVM} \
David Srbecky93749ed2022-11-09 15:24:45 +0000965 {GDB_ARGS} \
966 {FLAGS} \
967 {DEX_VERIFY} \
968 -XXlib:{LIB} \
969 {DEX2OAT} \
970 {DALVIKVM_ISA_FEATURES_ARGS} \
971 {ZYGOTE} \
972 {JNI_OPTS} \
973 {INT_OPTS} \
974 {DEBUGGER_OPTS} \
975 {QUOTED_DALVIKVM_BOOT_OPT} \
976 {TMP_DIR_OPTION} \
977 -XX:DumpNativeStackOnSigQuit:false \
978 -cp {DALVIKVM_CLASSPATH} {MAIN} {ARGS}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +0100979
David Srbecky94d21412022-10-10 16:02:51 +0100980 if SIMPLEPERF:
981 dalvikvm_cmdline = f"simpleperf record {dalvikvm_cmdline} && simpleperf report"
Ulya Trafimovich7f7f6442021-11-05 15:26:13 +0000982
David Srbecky94d21412022-10-10 16:02:51 +0100983 def sanitize_dex2oat_cmdline(cmdline: str) -> str:
984 args = []
985 for arg in cmdline.split(" "):
986 if arg == "--class-loader-context=&":
987 arg = "--class-loader-context=\&"
988 args.append(arg)
989 return " ".join(args)
Andreas Gampe038a1982020-03-11 23:06:42 +0000990
David Srbeckyfe3d9c92022-11-10 18:57:15 +0000991 # Remove whitespace.
David Srbecky94d21412022-10-10 16:02:51 +0100992 dex2oat_cmdline = sanitize_dex2oat_cmdline(dex2oat_cmdline)
993 dalvikvm_cmdline = re.sub(" +", " ", dalvikvm_cmdline)
994 dm_cmdline = re.sub(" +", " ", dm_cmdline)
995 vdex_cmdline = sanitize_dex2oat_cmdline(vdex_cmdline)
996 profman_cmdline = re.sub(" +", " ", profman_cmdline)
Andreas Gampeb31a8e72017-05-16 10:30:24 -0700997
David Srbecky94d21412022-10-10 16:02:51 +0100998 # Use an empty ASAN_OPTIONS to enable defaults.
999 # Note: this is required as envsetup right now exports detect_leaks=0.
1000 RUN_TEST_ASAN_OPTIONS = ""
Andreas Gampe58408392017-05-16 10:45:46 -07001001
David Srbecky94d21412022-10-10 16:02:51 +01001002 # Multiple shutdown leaks. b/38341789
1003 if RUN_TEST_ASAN_OPTIONS != "":
1004 RUN_TEST_ASAN_OPTIONS = f"{RUN_TEST_ASAN_OPTIONS}:"
1005 RUN_TEST_ASAN_OPTIONS = f"{RUN_TEST_ASAN_OPTIONS}detect_leaks=0"
1006
David Srbeckyfe3d9c92022-11-10 18:57:15 +00001007 assert not args.external_log_tags, "Deprecated: use --android-log-tags=*:v"
1008
1009 ANDROID_LOG_TAGS = args.android_log_tags
Nicolas Geoffrayf39f04c2018-03-05 15:42:11 +00001010
David Srbecky94d21412022-10-10 16:02:51 +01001011 if not HOST:
Roland Levillain72f67742019-03-06 15:48:08 +00001012 # Populate LD_LIBRARY_PATH.
David Srbecky94d21412022-10-10 16:02:51 +01001013 LD_LIBRARY_PATH = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001014 if ANDROID_ROOT != "/system":
Nicolas Geoffrayeb441dd2014-10-31 15:34:50 +00001015 # Current default installation is dalvikvm 64bits and dex2oat 32bits,
1016 # so we can only use LD_LIBRARY_PATH when testing on a local
1017 # installation.
David Srbecky94d21412022-10-10 16:02:51 +01001018 LD_LIBRARY_PATH = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}"
Martin Stjernholm0d0f8df2021-04-28 16:47:01 +01001019
1020 # This adds libarttest(d).so to the default linker namespace when dalvikvm
1021 # is run from /apex/com.android.art/bin. Since that namespace is essentially
1022 # an alias for the com_android_art namespace, that gives libarttest(d).so
1023 # full access to the internal ART libraries.
David Srbecky94d21412022-10-10 16:02:51 +01001024 LD_LIBRARY_PATH = f"/data/{TEST_DIRECTORY}/com.android.art/lib{SUFFIX64}:{LD_LIBRARY_PATH}"
1025 dlib = ("" if TEST_IS_NDEBUG else "d")
1026 art_test_internal_libraries = [
1027 f"libartagent{dlib}.so",
1028 f"libarttest{dlib}.so",
1029 f"libtiagent{dlib}.so",
1030 f"libtistress{dlib}.so",
David Srbeckybfa76b32022-06-20 21:30:56 +01001031 ]
David Srbecky94d21412022-10-10 16:02:51 +01001032 NATIVELOADER_DEFAULT_NAMESPACE_LIBS = ":".join(art_test_internal_libraries)
1033 dlib = ""
1034 art_test_internal_libraries = []
Martin Stjernholm0d0f8df2021-04-28 16:47:01 +01001035
Roland Levillain72f67742019-03-06 15:48:08 +00001036 # Needed to access the test's Odex files.
David Srbecky94d21412022-10-10 16:02:51 +01001037 LD_LIBRARY_PATH = f"{DEX_LOCATION}/oat/{ISA}:{LD_LIBRARY_PATH}"
Roland Levillain72f67742019-03-06 15:48:08 +00001038 # Needed to access the test's native libraries (see e.g. 674-hiddenapi,
David Srbeckybfa76b32022-06-20 21:30:56 +01001039 # which generates `libhiddenapitest_*.so` libraries in `{DEX_LOCATION}`).
David Srbecky94d21412022-10-10 16:02:51 +01001040 LD_LIBRARY_PATH = f"{DEX_LOCATION}:{LD_LIBRARY_PATH}"
Nicolas Geoffrayeb441dd2014-10-31 15:34:50 +00001041
Roland Levillain72f67742019-03-06 15:48:08 +00001042 # Prepend directories to the path on device.
David Srbecky94d21412022-10-10 16:02:51 +01001043 PREPEND_TARGET_PATH = ANDROID_ART_BIN_DIR
David Srbeckybfa76b32022-06-20 21:30:56 +01001044 if ANDROID_ROOT != "/system":
David Srbecky94d21412022-10-10 16:02:51 +01001045 PREPEND_TARGET_PATH = f"{PREPEND_TARGET_PATH}:{ANDROID_ROOT}/bin"
Roland Levillain72f67742019-03-06 15:48:08 +00001046
David Srbecky94d21412022-10-10 16:02:51 +01001047 timeout_dumper_cmd = ""
Andreas Gampea6138082019-09-11 11:08:23 -07001048
David Srbeckybfa76b32022-06-20 21:30:56 +01001049 if TIMEOUT_DUMPER:
Andreas Gampe975d70b2019-09-11 11:00:18 -07001050 # Use "-l" to dump to logcat. That is convenience for the build bot crash symbolization.
Andreas Gampe38a28182019-09-13 11:36:11 -07001051 # Use exit code 124 for toybox timeout (b/141007616).
David Srbecky94d21412022-10-10 16:02:51 +01001052 timeout_dumper_cmd = f"{TIMEOUT_DUMPER} -l -s 15 -e 124"
Andreas Gampe975d70b2019-09-11 11:00:18 -07001053
David Srbecky94d21412022-10-10 16:02:51 +01001054 timeout_prefix = ""
David Srbeckybfa76b32022-06-20 21:30:56 +01001055 if TIME_OUT == "timeout":
Andreas Gampe975d70b2019-09-11 11:00:18 -07001056 # Add timeout command if time out is desired.
1057 #
1058 # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1059 # will induce a full thread dump before killing the process. To ensure any issues in
1060 # dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1061 # child.
1062 # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
David Srbecky94d21412022-10-10 16:02:51 +01001063 timeout_prefix = f"timeout --foreground -k 120s {TIME_OUT_VALUE}s {timeout_dumper_cmd} {cmdline}"
Andreas Gampe975d70b2019-09-11 11:00:18 -07001064
David Srbeckyefb61262022-11-30 16:49:33 +00001065 ctx.export(
1066 ASAN_OPTIONS = RUN_TEST_ASAN_OPTIONS,
1067 ANDROID_DATA = DEX_LOCATION,
1068 DEX_LOCATION = DEX_LOCATION,
1069 ANDROID_ROOT = ANDROID_ROOT,
1070 ANDROID_I18N_ROOT = ANDROID_I18N_ROOT,
1071 ANDROID_ART_ROOT = ANDROID_ART_ROOT,
1072 ANDROID_TZDATA_ROOT = ANDROID_TZDATA_ROOT,
1073 ANDROID_LOG_TAGS = ANDROID_LOG_TAGS,
1074 LD_LIBRARY_PATH = LD_LIBRARY_PATH,
1075 NATIVELOADER_DEFAULT_NAMESPACE_LIBS = NATIVELOADER_DEFAULT_NAMESPACE_LIBS,
1076 PATH = f"{PREPEND_TARGET_PATH}:$PATH",
1077 )
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001078
David Srbeckyfe3d9c92022-11-10 18:57:15 +00001079 if USE_GDB or USE_GDBSERVER:
David Srbeckyd7674c22022-10-03 18:22:13 +01001080 print(f"Forward {GDBSERVER_PORT} to local port and connect GDB")
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001081
David Srbeckyefb61262022-11-30 16:49:33 +00001082 ctx.run(f"rm -rf {DEX_LOCATION}/{{oat,dalvik-cache}}/ && mkdir -p {mkdir_locations}")
1083 ctx.run(f"{profman_cmdline}")
1084 ctx.run(f"{dex2oat_cmdline}", desc="Dex2oat")
1085 ctx.run(f"{dm_cmdline}")
1086 ctx.run(f"{vdex_cmdline}")
1087 ctx.run(f"{strip_cmdline}")
1088 ctx.run(f"{sync_cmdline}")
1089 ctx.run(tee(f"{timeout_prefix} {dalvikvm_cmdline}"),
1090 expected_exit_code=args.expected_exit_code, desc="DalvikVM")
David Srbecky94d21412022-10-10 16:02:51 +01001091 else:
Calin Juravle24bd3f92017-05-11 00:36:53 -07001092 # Host run.
David Srbeckyc6d2a792022-08-18 14:15:43 +01001093 if USE_ZIPAPEX or USE_EXRACTED_ZIPAPEX:
Alex Light20802ca2018-12-05 15:36:03 -08001094 # Put the zipapex files in front of the ld-library-path
David Srbeckyd7674c22022-10-03 18:22:13 +01001095 LD_LIBRARY_PATH = f"{ANDROID_DATA}/zipapex/{LIBRARY_DIRECTORY}:{ANDROID_ROOT}/{TEST_DIRECTORY}"
David Srbeckybfa76b32022-06-20 21:30:56 +01001096 else:
David Srbeckyd7674c22022-10-03 18:22:13 +01001097 LD_LIBRARY_PATH = f"{ANDROID_ROOT}/{LIBRARY_DIRECTORY}:{ANDROID_ROOT}/{TEST_DIRECTORY}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001098
David Srbeckyefb61262022-11-30 16:49:33 +00001099 ctx.export(
1100 ANDROID_PRINTF_LOG = "brief",
1101 ASAN_OPTIONS = RUN_TEST_ASAN_OPTIONS,
1102 ANDROID_DATA = DEX_LOCATION,
1103 DEX_LOCATION = DEX_LOCATION,
1104 ANDROID_ROOT = ANDROID_ROOT,
1105 ANDROID_I18N_ROOT = ANDROID_I18N_ROOT,
1106 ANDROID_ART_ROOT = ANDROID_ART_ROOT,
1107 ANDROID_TZDATA_ROOT = ANDROID_TZDATA_ROOT,
1108 ANDROID_LOG_TAGS = ANDROID_LOG_TAGS,
1109 LD_LIBRARY_PATH = LD_LIBRARY_PATH,
1110 PATH = f"{PATH}:{ANDROID_ART_BIN_DIR}",
1111 # Temporarily disable address space layout randomization (ASLR).
1112 # This is needed on the host so that the linker loads core.oat at the necessary address.
1113 LD_USE_LOAD_BIAS = "1",
1114 TERM = os.environ.get("TERM", ""), # Needed for GDB
1115 )
David Srbeckyc9ede382015-06-20 06:03:53 +01001116
David Srbecky94d21412022-10-10 16:02:51 +01001117 cmdline = dalvikvm_cmdline
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001118
David Srbeckybfa76b32022-06-20 21:30:56 +01001119 if TIME_OUT == "gdb":
1120 if run("uname").stdout.strip() == "Darwin":
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001121 # Fall back to timeout on Mac.
David Srbecky94d21412022-10-10 16:02:51 +01001122 TIME_OUT = "timeout"
David Srbeckybfa76b32022-06-20 21:30:56 +01001123 elif ISA == "x86":
Hiroshi Yamauchic823eff2015-09-01 16:21:35 -07001124 # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
David Srbecky94d21412022-10-10 16:02:51 +01001125 TIME_OUT = "timeout"
David Srbeckybfa76b32022-06-20 21:30:56 +01001126 else:
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001127 # Check if gdb is available.
David Srbeckye60b4a72022-09-27 14:53:06 +01001128 proc = run('gdb --eval-command="quit"', check=False, save_cmd=False)
David Srbeckybfa76b32022-06-20 21:30:56 +01001129 if proc.returncode != 0:
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001130 # gdb isn't available. Fall back to timeout.
David Srbecky94d21412022-10-10 16:02:51 +01001131 TIME_OUT = "timeout"
Hiroshi Yamauchi6ffb9cc2015-08-31 15:14:17 -07001132
David Srbeckybfa76b32022-06-20 21:30:56 +01001133 if TIME_OUT == "timeout":
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001134 # Add timeout command if time out is desired.
Andreas Gampe038bb222015-01-13 19:48:14 -08001135 #
Andreas Gampe0df2aba2019-06-10 16:53:55 -07001136 # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1137 # will induce a full thread dump before killing the process. To ensure any issues in
1138 # dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1139 # child.
Andreas Gampe4bdcf5d2018-12-14 10:48:53 -08001140 # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
David Srbecky94d21412022-10-10 16:02:51 +01001141 cmdline = f"timeout --foreground -k 120s {TIME_OUT_VALUE}s {TIMEOUT_DUMPER} -s 15 {cmdline}"
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001142
David Srbeckybfa76b32022-06-20 21:30:56 +01001143 os.chdir(ANDROID_BUILD_TOP)
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001144
Calin Juravle24bd3f92017-05-11 00:36:53 -07001145 # Make sure we delete any existing compiler artifacts.
1146 # This enables tests to call the RUN script multiple times in a row
1147 # without worrying about interference.
David Srbeckyefb61262022-11-30 16:49:33 +00001148 ctx.run(f"rm -rf {DEX_LOCATION}/{{oat,dalvik-cache}}/")
Calin Juravle24bd3f92017-05-11 00:36:53 -07001149
David Srbeckyefb61262022-11-30 16:49:33 +00001150 ctx.run(f"mkdir -p {mkdir_locations}")
1151 ctx.run(setupapex_cmdline)
1152 if USE_EXTRACTED_ZIPAPEX:
1153 ctx.run(installapex_cmdline)
1154 ctx.run(linkroot_cmdline)
1155 ctx.run(linkroot_overlay_cmdline)
1156 ctx.run(profman_cmdline)
1157 ctx.run(dex2oat_cmdline, desc="Dex2oat")
1158 ctx.run(dm_cmdline)
1159 ctx.run(vdex_cmdline)
1160 ctx.run(strip_cmdline)
1161 ctx.run(sync_cmdline)
Andreas Gampea8780822015-03-13 19:51:09 -07001162
David Srbeckyc6d2a792022-08-18 14:15:43 +01001163 if DRY_RUN:
David Srbeckye4e701b2022-10-24 11:00:31 +00001164 return
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001165
David Srbeckyc6d2a792022-08-18 14:15:43 +01001166 if USE_GDB:
Nicolas Geoffray1a58b7f2014-10-06 12:23:04 +01001167 # When running under gdb, we cannot do piping and grepping...
David Srbeckyefb61262022-11-30 16:49:33 +00001168 ctx.run(cmdline)
David Srbeckybfa76b32022-06-20 21:30:56 +01001169 else:
David Srbeckyefb61262022-11-30 16:49:33 +00001170 ctx.run(tee(cmdline), expected_exit_code=args.expected_exit_code, desc="DalvikVM")
David Srbeckyfe3d9c92022-11-10 18:57:15 +00001171
David Srbeckyefb61262022-11-30 16:49:33 +00001172 # Remove unwanted log messages from stderr before diffing with the expected output.
1173 # NB: The unwanted log line can be interleaved in the middle of wanted stderr printf.
1174 # In particular, unhandled exception is printed using several unterminated printfs.
1175 ALL_LOG_TAGS = ["V", "D", "I", "W", "E", "F", "S"]
1176 skip_tag_set = "|".join(ALL_LOG_TAGS[:ALL_LOG_TAGS.index(args.diff_min_log_tag.upper())])
1177 skip_reg_exp = fr'[[:alnum:]]+ ({skip_tag_set}) #-# #:#:# [^\n]*\n'.replace('#', '[0-9]+')
1178 ctx.run(fr"sed -i -z -E 's/{skip_reg_exp}//g' '{args.stderr_file}'")
1179 if not HAVE_IMAGE:
1180 message = "(Unable to open file|Could not create image space)"
1181 ctx.run(fr"sed -i -E '/^dalvikvm(|32|64) E .* {message}/d' '{args.stderr_file}'")
1182 if ANDROID_LOG_TAGS != "*:i" and "D" in skip_tag_set:
1183 ctx.run(fr"sed -i -E '/^(Time zone|I18n) APEX ICU file found/d' '{args.stderr_file}'")