Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # Copyright 2024 The Chromium Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import pathlib |
| 8 | |
| 9 | |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 10 | def _generate(arch, hwasan, asan, env): |
| 11 | ret = """\ |
= | afaea1b | 2025-03-10 11:36:17 | [diff] [blame] | 12 | #!/system/bin/sh |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 13 | """ |
= | afaea1b | 2025-03-10 11:36:17 | [diff] [blame] | 14 | |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 15 | if hwasan: |
| 16 | # https://developer.android.com/ndk/guides/hwasan#wrapsh |
| 17 | ret += """ |
= | afaea1b | 2025-03-10 11:36:17 | [diff] [blame] | 18 | # import options file |
| 19 | _HWASAN_OPTIONS=$(cat /data/local/tmp/hwasan.options 2> /dev/null || true) |
| 20 | |
| 21 | log -t cr_wrap.sh -- "Launching with HWASAN enabled." |
= | afaea1b | 2025-03-10 11:36:17 | [diff] [blame] | 22 | """ |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 23 | env['HWASAN_OPTIONS'] = '$_HWASAN_OPTIONS' |
| 24 | env['LD_HWASAN'] = '1' |
| 25 | |
| 26 | if asan: |
| 27 | # https://github.com/google/sanitizers/wiki/AddressSanitizerOnAndroid/01f8df1ac1a447a8475cdfcb03e8b13140042dbd#running-with-wrapsh-recommended |
| 28 | ret += f""" |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 29 | HERE="$(cd "$(dirname "$0")" && pwd)" |
| 30 | # Options suggested by wiki docs: |
| 31 | _ASAN_OPTIONS="log_to_syslog=false,allow_user_segv_handler=1" |
| 32 | # Chromium-specific option (supposedly for graphics drivers): |
| 33 | _ASAN_OPTIONS="$_ASAN_OPTIONS,strict_memcmp=0,use_sigaltstack=1" |
| 34 | _LD_PRELOAD="$HERE/libclang_rt.asan-{arch}-android.so" |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 35 | |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 36 | log -t cr_wrap.sh -- "Launching with ASAN enabled." |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 37 | """ |
| 38 | env['LD_PRELOAD'] = '$_LD_PRELOAD' |
| 39 | env['ASAN_OPTIONS'] = '$_ASAN_OPTIONS' |
| 40 | |
| 41 | ret += 'log -t cr_wrap.sh -- "Command: $0 $@"\n' |
| 42 | |
| 43 | # Cannot set env vars before calling "log" commands, because they would |
| 44 | # affect the "log" executable. |
| 45 | for key, value in env.items(): |
| 46 | ret += f'log -t cr_wrap.sh -- "{key}={value}"\n' |
| 47 | |
| 48 | for key, value in env.items(): |
| 49 | ret += f'export {key}="{value}"\n' |
| 50 | |
| 51 | ret += """ |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 52 | exec "$@" |
| 53 | """ |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 54 | return ret |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def main(): |
| 58 | parser = argparse.ArgumentParser() |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 59 | parser.add_argument('--output', required=True) |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 60 | parser.add_argument('--arch', required=True) |
| 61 | parser.add_argument('--hwasan', action='store_true', default=False) |
| 62 | parser.add_argument('--asan', action='store_true', default=False) |
| 63 | parser.add_argument('--env') |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 64 | args = parser.parse_args() |
| 65 | |
Andrew Grieve | 5f466e7 | 2025-06-17 19:21:06 | [diff] [blame] | 66 | env = {} |
| 67 | if args.env: |
| 68 | for prop in args.env.split(): |
| 69 | key, value = prop.split('=', 1) |
| 70 | env[key] = value |
| 71 | pathlib.Path(args.output).write_text( |
| 72 | _generate(args.arch, args.hwasan, args.asan, env)) |
Andrew Grieve | 4def600e | 2024-04-30 14:05:50 | [diff] [blame] | 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | main() |