From 9d4ca9450e3310285c6fcf58658dedf7038dc086 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Mon, 26 Feb 2024 19:22:19 +0100 Subject: [PATCH] Add --with-asan option --- python/servo/build_commands.py | 36 ++++++++++++++++++++++++++++++---- python/servo/command_base.py | 12 +++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/python/servo/build_commands.py b/python/servo/build_commands.py index df8843d61a4fa..9b63d97c9fada 100644 --- a/python/servo/build_commands.py +++ b/python/servo/build_commands.py @@ -29,6 +29,7 @@ Command, ) from mach.registrar import Registrar +from python.servo.platform.base import NIGHTLY_RUST import servo.platform import servo.util @@ -53,11 +54,12 @@ class MachCommands(CommandBase): @CommandArgument('--very-verbose', '-vv', action='store_true', help='Print very verbose output') + @CommandArgument('--with-asan', action='store_true', help="Enable AddressSanitizer") @CommandArgument('params', nargs='...', help="Command-line arguments to be passed through to Cargo") @CommandBase.common_command_arguments(build_configuration=True, build_type=True) def build(self, build_type: BuildType, jobs=None, params=None, no_package=False, - verbose=False, very_verbose=False, **kwargs): + verbose=False, very_verbose=False, with_asan=False, **kwargs): opts = params or [] if build_type.is_release(): @@ -78,10 +80,29 @@ def build(self, build_type: BuildType, jobs=None, params=None, no_package=False, self.ensure_bootstrapped() self.ensure_clobbered() - build_start = time() - host = servo.platform.host_triple() target_triple = self.cross_compile_target or servo.platform.host_triple() + + if with_asan: + if target_triple not in ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"]: + print("AddressSanitizer is currently not supported on this platform\n", + "See https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html") + sys.exit(1) + env["RUSTFLAGS"] = env.get("RUSTFLAGS", "") + " -Zsanitizer=address" + opts += ["-Zbuild-std"] + kwargs["target"] = target_triple + # sanitizers are only available on nightly + kwargs["toolchain"] = f"+{NIGHTLY_RUST}" + # do not use crown (clashes with different rust version) + env["RUSTC"] = "rustc" + # env.setdefault("CFLAGS", "") + # env.setdefault("CXXFLAGS", "") + # env["CFLAGS"] += " -fsanitize=address" + # env["CXXFLAGS"] += " -fsanitize=address" + + build_start = time() + if host != target_triple and 'windows' in target_triple: if os.environ.get('VisualStudioVersion') or os.environ.get('VCINSTALLDIR'): print("Can't cross-compile for Windows inside of a Visual Studio shell.\n" @@ -104,10 +125,17 @@ def build(self, build_type: BuildType, jobs=None, params=None, no_package=False, if status == 0: built_binary = self.get_binary_path( build_type, - target=self.cross_compile_target, + target=target_triple if with_asan else self.cross_compile_target, android=self.is_android_build, ) + if with_asan: + shutil.copy(built_binary, self.get_binary_path( + build_type, + target=self.cross_compile_target, + android=self.is_android_build, + )) + if self.is_android_build and not no_package: flavor = None if "googlevr" in self.features: diff --git a/python/servo/command_base.py b/python/servo/command_base.py index 63b61b07bf071..632268d902da1 100644 --- a/python/servo/command_base.py +++ b/python/servo/command_base.py @@ -815,9 +815,10 @@ def is_media_enabled(self, media_stack: Optional[str]): def run_cargo_build_like_command( self, command: str, cargo_args: List[str], - env=None, verbose=False, + env=None, verbose=False, toolchain: Optional[str] = None, debug_mozjs=False, with_debug_assertions=False, with_frame_pointer=False, without_wgl=False, + target: Optional[str] = None, **_kwargs ): env = env or self.build_env() @@ -843,7 +844,9 @@ def run_cargo_build_like_command( "--manifest-path", path.join(self.context.topdir, "ports", port, "Cargo.toml"), ] - if self.cross_compile_target: + if target: + args += ["--target", target] + elif self.cross_compile_target: args += ["--target", self.cross_compile_target] if "-p" not in cargo_args: # We're building specific package, that may not have features @@ -867,7 +870,10 @@ def run_cargo_build_like_command( if with_debug_assertions or self.config["build"]["debug-assertions"]: env['RUSTFLAGS'] = env.get('RUSTFLAGS', "") + " -C debug_assertions" - return call(["cargo", command] + args + cargo_args, env=env, verbose=verbose) + args.insert(0, command) + if toolchain: + args.insert(0, toolchain) + return call(["cargo"] + args + cargo_args, env=env, verbose=verbose) def android_adb_path(self, env): if "ANDROID_SDK_ROOT" in env: