Repository: flutter
Issue: Fail to build Flutter Android/iOS engine with AddressSanitizer
Labels: engine,P3,team-engine,triaged-engine

## Environment

- Flutter: https://github.com/flutter/flutter/tree/3.0.2

- Flutter Engine: https://github.com/flutter/engine/commit/f15f824b57476e369b5e656f53d4c431c5b04b9a

- Linux

<details>
  <summary>flutter doctor -v</summary>

```
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[✓] Flutter (Channel stable, 3.0.2, on Ubuntu 18.04.6 LTS 5.4.0-120-generic, locale C.UTF-8)
    • Flutter version 3.0.2 at /home/longxiang/google/flutter-3.0.x
    • Upstream repository https://github.com/flutter/flutter
    • Framework revision cd41fdd495 (12 days ago), 2022-06-08 09:52:13 -0700
    • Engine revision f15f824b57
    • Dart version 2.17.3
    • DevTools version 2.12.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

Checking Android licenses is taking an unexpectedly long time...[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /home/longxiang/Android/Sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at:
      /home/longxiang/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/211.7628.21.2111.8193401/jr
      e/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
    • cmake version 3.10.2
    • ninja version 1.8.2
    • pkg-config version 0.29.1

[✓] Android Studio (version 2021.1)
    • Android Studio at
      /home/longxiang/.local/share/JetBrains/Toolbox/apps/AndroidStudio/ch-0/211.7628.21.2111.8193401
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] VS Code (version 1.68.1)
    • VS Code at /usr/share/code
    • Flutter extension version 3.40.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 18.04.6 LTS 5.4.0-120-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 102.0.5005.115

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>

- macOS

<details>
  <summary>flutter doctor -v</summary>

```
[✓] Flutter (Channel stable, 3.0.2, on macOS 12.4 21F79 darwin-x64, locale zh-Hans-CN)
    • Flutter version 3.0.2 at /Users/user/w/google/flutter-3.0.x
    • Upstream repository https://github.com/flutter/flutter
    • Framework revision cd41fdd495 (12 days ago), 2022-06-08 09:52:13 -0700
    • Engine revision f15f824b57
    • Dart version 2.17.3
    • DevTools version 2.12.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • ANDROID_HOME = /Users/user/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/user/Library/Android/sdk
    • Java binary at: /Users/user/Library/Application
      Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/212.5712.43.2112.8609683/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google
    Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 2021.2)
    • Android Studio at /Users/user/Library/Application
      Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/212.5712.43.2112.8609683/Android
      Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2022.1.2)
    • IntelliJ at /Users/user/Library/Application
      Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5787.30/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.68.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.42.0

[✓] Connected device (2 available)
    • iPhone 12 (mobile) • 9EAED05F-ADFC-4640-A563-7F519D0009FF • ios        •
      com.apple.CoreSimulator.SimRuntime.iOS-15-5 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64 • macOS 12.4 21F79
      darwin-x64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
```

</details>

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

### Build Flutter Android engine on `Linux`

1. [Setting up the Engine development environment](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment)
2. [Compile the engine](https://github.com/flutter/flutter/wiki/Compiling-the-engine) without AddressSanitizer successfully

```sh
$ ./flutter/tools/gn --unoptimized && autoninja -C out/host_debug_unopt
$ ./flutter/tools/gn --android --unoptimized --android-cpu arm64 && autoninja -C out/android_debug_unopt_arm64
```

3. Compiling the engine with AddressSanitizer failed

```sh
$ ./flutter/tools/gn --android --unoptimized --android-cpu arm64 --asan && autoninja -C out/android_debug_unopt_arm64
```

<details>
  <summary>ld.lld: error: cannot open /home/ubuntu/engine-3.0.x/src/buildtools/linux-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory</summary>

```
ninja: Entering directory `out/android_debug_unopt_arm64/'
[1/22] LINK ./flutter_shell_native_unittests
FAILED: flutter_shell_native_unittests exe.stripped/flutter_shell_native_unittests
../../buildtools/linux-x64/clang/bin/clang++ -Wl,--fatal-warnings -fsanitize=address -fPIC -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro --gcc-toolchain=../../third_party/android_tools/ndk/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -Wl,--no-undefined -Wl,--exclude-libs,ALL -fuse-ld=lld -Wl,--icf=all --target=aarch64-linux-android -nostdlib++ -nostdlib -L/home/ubuntu/engine-3.0.x/src/third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/22  -Bdynamic -Wl,-z,nocopyreloc -pie -L../../third_party/android_tools/ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a  -o ./flutter_shell_native_unittests -Wl,--build-id=sha1 -Wl,--start-group @./flutter_shell_native_unittests.rsp  -Wl,--end-group ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/22/crtbegin_dynamic.o -lgcc -lc -ldl -lm -landroid -lEGL -lGLESv2 -llog -ljnigraphics ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/22/crtend_android.o && ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip --strip-unneeded -o exe.stripped/./flutter_shell_native_unittests ./flutter_shell_native_unittests
ld.lld: error: cannot open /home/ubuntu/engine-3.0.x/src/buildtools/linux-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
[2/22] SOLINK ./libflutter.so
FAILED: libflutter.so libflutter.so.TOC lib.stripped/libflutter.so
../../buildtools/linux-x64/clang/bin/clang++ -shared -Wl,--version-script=/home/ubuntu/engine-3.0.x/src/flutter/shell/platform/android/android_exports.lst -Wl,--fatal-warnings -fsanitize=address -fPIC -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro --gcc-toolchain=../../third_party/android_tools/ndk/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -Wl,--no-undefined -Wl,--exclude-libs,ALL -fuse-ld=lld -Wl,--icf=all --target=aarch64-linux-android -nostdlib++ -nostdlib -L/home/ubuntu/engine-3.0.x/src/third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/22  -L../../third_party/android_tools/ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a  -o ./libflutter.so -Wl,--build-id=sha1 -Wl,-soname=libflutter.so @./libflutter.so.rsp && { ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-readelf -d ./libflutter.so | grep SONAME ; ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-nm -gD -f posix ./libflutter.so | cut -f1-2 -d' '; } > ./libflutter.so.tmp && if ! cmp -s ./libflutter.so.tmp ./libflutter.so.TOC; then mv ./libflutter.so.tmp ./libflutter.so.TOC; fi && ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip --strip-unneeded -o lib.stripped/libflutter.so.tmp libflutter.so && if ! cmp -s lib.stripped/libflutter.so.tmp lib.stripped/libflutter.so; then mv lib.stripped/libflutter.so.tmp lib.stripped/libflutter.so; fi
ld.lld: error: cannot open /home/ubuntu/engine-3.0.x/src/buildtools/linux-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
```
</details>

However there is no `linux` under `buildtools/linux-x64/clang/lib/clang/14.0.0/lib/`

```sh
$ ls buildtools/linux-x64/clang/lib/clang/14.0.0/lib/
aarch64-unknown-fuchsia        i386-unknown-fuchsia     x86_64-unknown-fuchsia
aarch64-unknown-linux-gnu      i386-unknown-linux-gnu   x86_64-unknown-linux-gnu
armv7-unknown-linux-gnueabihf  riscv64-unknown-fuchsia
```

### Build Flutter Android engine on `macOS`

1. [Setting up the Engine development environment](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment)
2. [Compiling the engine](https://github.com/flutter/flutter/wiki/Compiling-the-engine) without AddressSanitizer

```sh
$ ./flutter/tools/gn --unoptimized && autoninja -C out/host_debug_unopt
$ ./flutter/tools/gn --android --unoptimized --android-cpu arm64 && autoninja -C out/android_debug_unopt_arm64
```

3. Compiling the engine with AddressSanitizer failed

```sh
$ ./flutter/tools/gn --android --unoptimized --android-cpu arm64 --asan && autoninja -C out/android_debug_unopt_arm64
```

<details>
  <summary>ld.lld: error: cannot open /Users/user/w/google/engine-3.0.x/src/buildtools/mac-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory</summary>

```
ninja: Entering directory `out/android_debug_unopt_arm64/'
[3/24] LINK ./flutter_shell_native_unittests
FAILED: flutter_shell_native_unittests exe.stripped/flutter_shell_native_unittests
../../buildtools/mac-x64/clang/bin/clang++ -Wl,--fatal-warnings -fsanitize=address -fPIC -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro --gcc-toolchain=../../third_party/android_tools/ndk/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 -Wl,--no-undefined -Wl,--exclude-libs,ALL -fuse-ld=lld -Wl,--icf=all --target=aarch64-linux-android -nostdlib++ -nostdlib -L/Users/user/w/google/engine-3.0.x/src/third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/22  -Bdynamic -Wl,-z,nocopyreloc -pie -L../../third_party/android_tools/ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a  -o ./flutter_shell_native_unittests -Wl,--build-id=sha1 -Wl,--start-group @./flutter_shell_native_unittests.rsp  -Wl,--end-group ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/22/crtbegin_dynamic.o -lgcc -lc -ldl -lm -landroid -lEGL -lGLESv2 -llog -ljnigraphics ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/22/crtend_android.o && ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --strip-unneeded -o exe.stripped/./flutter_shell_native_unittests ./flutter_shell_native_unittests
ld.lld: error: cannot open /Users/user/w/google/engine-3.0.x/src/buildtools/mac-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
[4/24] SOLINK ./libflutter.so
FAILED: libflutter.so libflutter.so.TOC lib.stripped/libflutter.so
../../buildtools/mac-x64/clang/bin/clang++ -shared -Wl,--version-script=/Users/user/w/google/engine-3.0.x/src/flutter/shell/platform/android/android_exports.lst -Wl,--fatal-warnings -fsanitize=address -fPIC -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro --gcc-toolchain=../../third_party/android_tools/ndk/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 -Wl,--no-undefined -Wl,--exclude-libs,ALL -fuse-ld=lld -Wl,--icf=all --target=aarch64-linux-android -nostdlib++ -nostdlib -L/Users/user/w/google/engine-3.0.x/src/third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/aarch64-linux-android/22  -L../../third_party/android_tools/ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a  -o ./libflutter.so -Wl,--build-id=sha1 -Wl,-soname=libflutter.so @./libflutter.so.rsp && { ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-readelf -d ./libflutter.so | grep SONAME ; ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-nm -gD -f posix ./libflutter.so | cut -f1-2 -d' '; } > ./libflutter.so.tmp && if ! cmp -s ./libflutter.so.tmp ./libflutter.so.TOC; then mv ./libflutter.so.tmp ./libflutter.so.TOC; fi && ../../third_party/android_tools/ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android-strip --strip-unneeded -o lib.stripped/libflutter.so.tmp libflutter.so && if ! cmp -s lib.stripped/libflutter.so.tmp lib.stripped/libflutter.so; then mv lib.stripped/libflutter.so.tmp lib.stripped/libflutter.so; fi
ld.lld: error: cannot open /Users/user/w/google/engine-3.0.x/src/buildtools/mac-x64/clang/lib/clang/14.0.0/lib/linux/libclang_rt.asan-aarch64-android.so: No such file or directory
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
```
</details>

However there is no `linux` under `buildtools/mac-x64/clang/lib/clang/14.0.0/lib/`

```
$ ls buildtools/mac-x64/clang/lib/clang/14.0.0/lib
aarch64-unknown-fuchsia		darwin				riscv64-unknown-fuchsia
aarch64-unknown-linux-gnu	i386-unknown-fuchsia		x86_64-unknown-fuchsia
armv7-unknown-linux-gnueabihf	i386-unknown-linux-gnu		x86_64-unknown-linux-gnu
```

### Build Flutter `iOS` engine on macOS

1. [Setting up the Engine development environment](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment)
2. [Compiling the engine](https://github.com/flutter/flutter/wiki/Compiling-the-engine) without AddressSanitizer

```sh
$ ./flutter/tools/gn --unoptimized && autoninja -C out/host_debug_unopt
$ ./flutter/tools/gn --ios --simulator --unoptimized && autoninja -C out/ios_debug_sim_unopt
```

3. Compiling the engine with AddressSanitizer failed

```sh
$ ./flutter/tools/gn --ios --simulator --unoptimized --asan && autoninja -C out/ios_debug_sim_unopt
```

<details>
<summary>ninja: error: '../../buildtools/mac-x64/clang/lib/clang/13.0.0/lib/darwin/libclang_rt.asan_iossim_dynamic.dylib'</summary>

```
ninja: Entering directory `out/ios_debug_sim_unopt/'
ninja: error: '../../buildtools/mac-x64/clang/lib/clang/13.0.0/lib/darwin/libclang_rt.asan_iossim_dynamic.dylib', needed by 'libclang_rt.asan_iossim_dynamic.dylib', missing and no known rule to make it
```
</details>

However there is no `13.0.0` under `buildtools/mac-x64/clang/lib/clang`

```
$ ls buildtools/mac-x64/clang/lib/clang/
14.0.0
```