NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.13k stars 14.16k forks source link

Segmentation fault running Android emulator provided by androidenv.composeAndroidPackages #219745

Open mzabani opened 1 year ago

mzabani commented 1 year ago

Describe the bug

Segmentation fault when launching Android emulator provided by androidenv.composeAndroidPackages environment from command line.

Steps To Reproduce

  1. Use the following flake.nix:

    {
    description = "Android app flake";
    
    inputs = {
    nixpkgs.url =
      "github:NixOS/nixpkgs/3a166f1b131d2ca49d99fe716fd1ce898bd58f03";
    };
    
    outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          system = system;
          config = {
            android_sdk.accept_license = true;
            allowUnfree = true;
          };
        };
    
        buildToolsVersions = "33.0.1";
        androidComposition = pkgs.androidenv.composeAndroidPackages {
          toolsVersion = "26.1.1";
          platformToolsVersion = "33.0.3";
          includeEmulator = true;
          emulatorVersion = "31.3.14";
          platformVersions = [ "33" ];
          buildToolsVersions = [ buildToolsVersions ];
          includeSystemImages = true;
          systemImageTypes = [ "google_apis_playstore" ];
          abiVersions = [ "x86_64" ];
          useGoogleAPIs = false;
          includeExtras = [ "extras;google;gcm" ];
        };
      in {
        devShells.default = pkgs.mkShell {
          name = "android-sdk-env";
          nativeBuildInputs = with pkgs; [
            # Development
            # android-studio
            # libcxx
    
            # Building the app
            # jdk11
            nodejs-18_x
            apksigner
            apktool
            # yarn
    
            androidComposition.androidsdk
    
            # Sniffing
            mitmproxy
          ];
    
          shellHook = ''
            export PATH="$PATH:$ANDROID_SDK_ROOT/build-tools/${buildToolsVersions}"
            echo "To create an AVD, type":
            echo "avdmanager create avd -n cmdline-created-android -k \"system-images;android-33;google_apis_playstore;x86_64\""
            echo "---------------"
            echo "To launch it, type \"emulator -avd cmdline-created-android\""
          '';
    
          ANDROID_SDK_ROOT =
            "${androidComposition.androidsdk}/libexec/android-sdk";
          GRADLE_OPTS =
            "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidComposition.androidsdk}/libexec/android-sdk/build-tools/${buildToolsVersions}/aapt2";
        };
      });
    }
  2. Run nix develop

  3. Create an AVD with avdmanager create avd -n cmdline-created-android -k "system-images;android-33;google_apis_playstore;x86_64" without a custom hardware profile.

  4. Run the emulator with emulator -avd cmdline-created-android and wait for the segmentation fault.

Expected behavior

I was expecting to see Android running inside the emulator without any errors.

Additional context

The output of the emulator is:

$ emulator -avd cmdline-created-android
INFO    | Android emulator version 31.3.14.0 (build_id 9322596) (CL:N/A)
emulator: INFO: Found systemPath /nix/store/4rajkmxxg1zj6l2gbirqcamv6xhixl79-androidsdk/libexec/android-sdk/system-images/android-33/google_apis_playstore/x86_64/
INFO    | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
WARNING | Failed to process .ini file /home/mzabani/.android/avd/cmdline-created-android.avd/quickbootChoice.ini for reading.
INFO    | Info: Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway. ((null):0, (null))

WARNING | cannot add library /nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/qemu/linux-x86_64/lib64/vulkan/libvulkan.so: failed
INFO    | added library /nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/lib64/vulkan/libvulkan.so
ERROR   | Failed to create Vulkan instance.
queryCoreProfileSupport: swap interval not found
INFO    | configAndStartRenderer: setting vsync to 60 hz
INFO    | Sending adb public key [QAAAAE0enzV7taD8JBIJc6j/jlnaG8Cujycx4me8YNmz37zJqYxnlg08hHqeUgDKDS0CL3ZARx/BT6cLxlhZ2KfaZnNMMg3GKPaA3PZLNHr7rVnW04tCEhUpY8gxrUAlcs97bVIMwpnessZRfbBBFUxyhgqRlpyfpcj3IvPe16M6mXxdio6J/f1JrzkPEfuqjfKsYR7ycgjpxTFtydtY2JQiE4ID2QqIcy4G4WRNZeyrHb36mjhjCOexB8Xd0mpZd4ZWbM9j7KIieAbLtCnqzgDuQD9AerhWdb27159IrZR9kTythbJpZ7ZgJnVQ51U8xkKDt2GgAiFuYVRpDZR0BiUisCYLCPqiMgL2cU2eCy2e2W12dzzY/kVuw/jbUelB2XWuIOzoalwOBOe3i7FgmGF0J9sqKr+oxYsEt7s8s8b4DkRjC/FiXcemwNy2KC7Xm3Qpt8pEF5iktxkbLTQHBhve9iKuh3/8DpJpYyMKcOSvNZtlYsegwTaIcpak4IDMKBiviyZK9CiYIb/vA/dCIdFW5sPqgfW6T55A7l4EbCSM8T7UIYbd5SY3iD0x6mhWhDK5SgoV+j1vVWbGtKJ9UdnrIuEeACIFAuiPpBBqjgUicnrLTPwzR1bG5YUw453j+B+ujHaiHCnMgojslYBYL84LPIV0pfFmEDWjJRUWHrNhZDs+kk+EOgEAAQA= mzabani@unknown]
INFO    | injectedQemuChannel!
INFO    | Informing listeners of injection.
INFO    | Rootcanal has  been activated.
WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
INFO    | Started GRPC server at 127.0.0.1:8554, security: Local, auth: none
INFO    | Advertising in: /run/user/1001/avd/running/pid_14245.ini
INFO    | setDisplayConfigs w 320 h 640 dpiX 160 dpiY 160
ERROR   | Unable to connect to adb daemon on port: 5037
LLVM ERROR: Cannot select: intrinsic %llvm.x86.sse41.pblendvb
Segmentation fault (core dumped)

Notify maintainers

Didn't know how to find a meta.maintainers list, so citing from recent git history of this file: @hadilq @jakobkukla @VergeDX @Artturin

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.15, NixOS, 23.05 (Stoat), 23.05.20230304.3a166f1`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.13.3`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`
jakobkukla commented 1 year ago

Not a maintainer, but still an interesting issue. What CPU model are you running?

mzabani commented 1 year ago

I'm running an AMD Ryzen 9 5950X 16-Core Processor with KVM enabled.

hadilq commented 1 year ago

Hey @mzabani I cannot reproduce it here! The emulator is launched.

NFO    | Android emulator version 31.3.14.0 (build_id 9322596) (CL:N/A)
emulator: INFO: Found systemPath /nix/store/4rajkmxxg1zj6l2gbirqcamv6xhixl79-androidsdk/libexec/android-sdk/system-images/android-33/google_apis_playstore/x86_64/
INFO    | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
WARNING | Failed to process .ini file /home/hadi/.android/avd/cmdline-created-android.avd/quickbootChoice.ini for reading.
WARNING | cannot add library /nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/qemu/linux-x86_64/lib64/vulkan/libvulkan.so: failed
INFO    | added library /nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/lib64/vulkan/libvulkan.so
ERROR   | Failed to create Vulkan instance.
queryCoreProfileSupport: swap interval not found
INFO    | configAndStartRenderer: setting vsync to 60 hz
INFO    | Sending adb public key [QAAAAFsEUvktdJTa16hcU3PHtcYPukNnFM40omsyANfyvDtEf2iGv22Xlo8NWsTUN39qpPgQFpvNfsPcYNLgfpdP+Y7b/Lp+vT4GJs6ya1OnotYt2nrLg1vWxZY473QAGyXsWObf5DKsjHv9ixiRLj6VxwOzwsXNb4ZpJL50XDn+TyqcUOLXcXetxULN26A5pcWifEQdHIfMF+QxncVYZ8V3mJb6Z34sUzAtiLaXkzUQdnoL7VfgXHGDs7XGlQH8q75yIaOYSvlwBhImjvWUEOZdFTe2uoncAE5NQoepozHBW7qVQIW4yp+BFtsFLO2d7ZseI55vW9LtM6dXpaavH23xsbJvCMauglH9UTYXg7z97olYhDtHAPpr4aHj/Uc5StNJE4bZaCgOkjYX3PB+2w4Jcgrsv9xFH0BP8AY40yiv8YO2AyF6Z0ZEZ7YKKPZOrggNcW8vvaSB1TJ1FnttAcg6dbfudKFtM6wb4I9dMEvXNNlsbZKF32DTRKy02/Z1W7zgTzQKJpR1I7aZTYDvCVI4S1jdWt0w98PPStvsQiG7NmbpSs9I851Rw8J5SyxPQWwwvbwr9l75pKWDQ4rw2lZcrS1FQFA5bVClwNL6zodNMu6LxIt7lZBOf7C73umWgU4Wfl8x86HKUw7wEqADLmWp4CP59UQ+FX5BLYJVsXXMsGqIXVe7RAEAAQA= hadi@unknown]
INFO    | injectedQemuChannel!
INFO    | Informing listeners of injection.
INFO    | Rootcanal has  been activated.
WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
INFO    | Started GRPC server at 127.0.0.1:8554, security: Local, auth: none
INFO    | Advertising in: /run/user/1000/avd/running/pid_108813.ini
INFO    | setDisplayConfigs w 320 h 640 dpiX 160 dpiY 160
ERROR   | Unable to connect to adb daemon on port: 5037
INFO    | Your emulator is out of date, please update by launching Android Studio:
 - Start Android Studio
 - Select menu "Tools > Android > SDK Manager"
 - Click "SDK Tools" tab
 - Check "Android Emulator" checkbox
 - Click "OK"

glTexImage2D: got err pre :( 0x502 internal 0x8229 format 0x1903 type 0x1401
glTexImage2D: got err pre :( 0x502 internal 0x8058 format 0x1908 type 0x1401
glTexImage2D: got err pre :( 0x506 internal 0x8058 format 0x1908 type 0x1401
INFO    | boot completed
INFO    | boot time 122084 ms
INFO    | Increasing screen off timeout, logcat buffer size to 2M.
ERROR   | Unable to connect to adb daemon on port: 5037
INFO    | Revoking microphone permissions for Google App.

Have no clue why it's working differently! I searched and found https://stackoverflow.com/questions/15023592/llvm-cannot-select-intrinsic-llvm-spu-si-sf, so I guess it's a problem related to LLVM+CLANG, where in my shell I found

CC=gcc

Still have no clue!

mzabani commented 1 year ago

Thanks for the quick answer! I also have CC=gcc in my environment.

One thing I noticed different in the outputs of our runs is mine has the following line:

INFO    | Info: Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway. ((null):0, (null))

I tried setting that environment variable but got a different error (and running the emulator under nix develop -i triggers this error as well):

$ QT_QPA_PLATFORM=wayland emulator -avd cmdline-created-android
INFO    | Android emulator version 31.3.14.0 (build_id 9322596) (CL:N/A)
emulator: INFO: Found systemPath /nix/store/4rajkmxxg1zj6l2gbirqcamv6xhixl79-androidsdk/libexec/android-sdk/system-images/android-33/google_apis_playstore/x86_64/
INFO    | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
INFO    | Info: Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway. ((null):0, (null))

INFO    | Warning: Could not find the Qt platform plugin "wayland" in "/nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/lib64/qt/plugins" ((null):0, (null))

Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.
 ((null):0, (null))
INFO    | Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.
 ((null):0, (null))

Aborted (core dumped)

Which is strange, since it says it's ignoring XDG_SESSION_TYPE again, but then crashes with a very different error.

Suspecting my graphical stack, I tried using -no-window, and indeed it doesn't crash (only last lines pasted):

$ emulator -avd cmdline-created-android -no-window
...
...
INFO    | setDisplayConfigs w 320 h 640 dpiX 160 dpiY 160
INFO    | boot completed
INFO    | boot time 19692 ms
INFO    | Increasing screen off timeout, logcat buffer size to 2M.
INFO    | Revoking microphone permissions for Google App.

So it seems it might be Wayland + my GPU/driver?

Oh, before I finish this, I discovered a new option and tried it, and it worked! It's emulator -avd cmdline-created-android -gpu swiftshader_indirect.

I wonder if we should dismiss all of this as a non-supported LLVM thing in my driver/graphical stack or not? Or if at least nix develop -i should've worked?

Tanish2002 commented 1 year ago

I can reproduce the error on my setup as well. Here is my flake.nix

{
  description = "Flutter devshell";
  inputs = { flake-utils.url = "github:numtide/flake-utils"; };
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          config = {
            android_sdk.accept_license = true;
            allowUnfree = true;
          };
        };
        buildToolsVersion = "28.0.3";
        androidComposition = pkgs.androidenv.composeAndroidPackages {
          buildToolsVersions = [ buildToolsVersion ];
          platformVersions = [ "28" "31" ];
          abiVersions = [ "x86_64" ];
          includeEmulator = true;
          includeSystemImages = true;
          systemImageTypes = [ "google_apis_playstore" ];
          useGoogleAPIs = true;
        };
        androidSdk = androidComposition.androidsdk;
      in {
        devShell = with pkgs;
          mkShell {
            ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
            buildInputs = [ flutter androidSdk jdk11 ];
          };
      });
}

here is what happens when I do emulator -avd "Pixel6Pro"

INFO    | Android emulator version 31.3.14.0 (build_id 9322596) (CL:N/A)
emulator: INFO: Found systemPath /nix/store/r7kss2pcdiqrsnv46q8x4cxad48359ig-androidsdk/libexec/android-sdk/system-images/android-28/google_apis_playstore/x86_64/
INFO    | Duplicate loglines will be removed, if you wish to see each indiviudal line launch with the -log-nofilter flag.
queryCoreProfileSupport: swap interval not found
INFO    | configAndStartRenderer: setting vsync to 60 hz
WARNING | cannot add library /nix/store/cma58q6n83hdhn4bv4gzfmpabg4z2nbr-emulator-31.3.14/libexec/android-sdk/emulator/qemu/linux-x86_64/lib64/vulkan/libvulkan.so: failed
INFO    | added library /nix/store/cma58q6n83hdhn4bv4gzfmpabg4z2nbr-emulator-31.3.14/libexec/android-sdk/emulator/lib64/vulkan/libvulkan.so
WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
INFO    | Started GRPC server at 127.0.0.1:8554, security: Local, auth: none
INFO    | Advertising in: /run/user/1000/avd/running/pid_43588.ini
INFO    | setDisplayConfigs w 1440 h 3120 dpiX 560 dpiY 560
WARNING | change of renderer detected.
WARNING | EGL config mismatch, fallback to default configs
LLVM ERROR: Cannot select: intrinsic %llvm.x86.sse41.pblendvb

also it works if I use -gpu swiftshader_indirect

mzabani commented 1 year ago

Interesting. @Tanish2002 what GPU do you have and what driver are you using? Are you on Wayland or on X?

I'm on Wayland, using the amdgpu driver with a Radeon RX 6700 XT card:

$ lspci -v
09:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Navi 22 [Radeon RX 6700/6700 XT/6750 XT / 6800M/6850M XT] (rev c1) (prog-if 00 [VGA controller])
        Subsystem: ASRock Incorporation Device 5222
        Flags: bus master, fast devsel, latency 0, IRQ 100, IOMMU group 18
        Memory at d0000000 (64-bit, prefetchable) [size=256M]
        Memory at e0000000 (64-bit, prefetchable) [size=2M]
        I/O ports at e000 [size=256]
        Memory at fca00000 (32-bit, non-prefetchable) [size=1M]
        Expansion ROM at 000c0000 [disabled] [size=128K]
        Capabilities: <access denied>
        Kernel driver in use: amdgpu
        Kernel modules: amdgpu

I wonder if some other graphical driver would work.. I might have to try it out some time.

hadilq commented 1 year ago

This error

Available platform plugins are: xcb.
 ((null):0, (null))
INFO    | Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.
 ((null):0, (null))

Aborted (core dumped)

reminds me that I already had a similar problem! I'm not sure how did I solve it in the end, but did you try to enable libvirtd? Based on https://nixos.wiki/wiki/Libvirt you need to add

virtualisation.libvirtd.enable = true;

in the configuration.nix file of NixOS. Then add your user to libvirtd group.

mzabani commented 1 year ago

I have libvirtd enabled and my user belongs to the libvirtd group, so I think sadly it's something else.

I ran the emulator with QT_DEBUG_PLUGINS=1 set and found that xcb might be kind of a red herring since it's actually loaded like many other plugins that appear in stdout:

INFO    | Debug: QFactoryLoader::QFactoryLoader() looking at "/nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/lib64/qt/plugins/platforms/libqxcb.so" ((null):0, (null))

INFO    | Debug: Got keys from plugin meta data ("xcb") ((null):0, (null))

However, I think the key in the original error is that xcb is a platform plugin, and indeed it's the only one in the folder:

$ ls /nix/store/pqv3rgrv790myywlk8qlcrvf50h7mj3k-emulator-31.3.14/libexec/android-sdk/emulator/lib64/qt/plugins/platforms/
libqxcb.so

So my hypothesis is that running this on XWayland is broken, but not on pure X, but that still doesn't cover why that strange LLVM error is happening. I do have qt5 installed independently, but I don't see any wayland-like platform there:

$ ls /nix/store/w1700kgv91n8aliqc0zmww4ijhq4sh4c-qtbase-5.15.5-bin/lib/qt-5.15.5/plugins/platforms
libqeglfs.so  libqlinuxfb.so  libqminimalegl.so  libqminimal.so  libqoffscreen.so  libqvnc.so  libqxcb.so

There is a qtwayland in nixpkgs, but the names of the libraries in that don't resemble something that could work. Also, I don't know how to make the emulator look for platform plugins in other folders anyway. Some people in the internet mention a QT_QPA_PLATFORM_PLUGIN_PATH variable for that, but it seems to be ignored when I set it.

Anyway, this is starting to look like a XWayland/QT bug. I'm not sure if I'll keep investigating as I found a workaround, but I'll keep posting in case I do.

camoles commented 1 year ago

I also have this problem and the workaround I found is to use either:

-gpu guest or -gpu swiftshader_indirect

when calling the emulator

tgwow commented 1 year ago

idk why, but -gpu guest works here, thank you!!!

virchau13 commented 1 year ago

I managed to get it working without changing GPU settings by adding vulkan-loader and libGL to a FHS environment (although I'm not sure if a FHS environment is required).

Note that -gpu guest / -gpu swiftshader_indirect are both software rendering and probably a lot more inefficient. I believe the issue is simply that the emulator tries to get -gpu host GPU hardware acceleration working, but can't find Vulkan and OpenGL.

mahmoudajawad commented 1 year ago

@K900 helped me with this issue in Matrix channel today when suggested defining LD_LIBRARY_PATH in shell:

          LD_LIBRARY_PATH="${pkgs.libglvnd}/lib";
darichey commented 1 year ago
LD_LIBRARY_PATH="${pkgs.libglvnd}/lib";

This worked for me!

My working flake:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          config = {
            android_sdk.accept_license = true;
            allowUnfree = true;
          };
        };
        androidComposition = pkgs.androidenv.composeAndroidPackages {
          cmdLineToolsVersion = "11.0";
          toolsVersion = "26.1.1";
          platformToolsVersion = "34.0.4";
          buildToolsVersions = [ "34.0.0" ];
          includeEmulator = true;
          emulatorVersion = "32.1.14";
          platformVersions = [ "34" ];
          includeSources = false;
          includeSystemImages = true;
          systemImageTypes = [ "google_apis_playstore" ];
          abiVersions = [ "x86_64" ];
          cmakeVersions = [ ];
          includeNDK = false;
          useGoogleAPIs = false;
          useGoogleTVAddOns = false;
          includeExtras = [];
          extraLicenses = [
            "android-sdk-preview-license"
            "android-googletv-license"
            "android-sdk-arm-dbt-license"
            "google-gdk-license"
            "intel-android-extra-license"
            "intel-android-sysimage-license"
            "mips-android-sysimage-license"
          ];
        };
        androidsdk = androidComposition.androidsdk;
      in
      with pkgs;
      {
        devShells.default = mkShell {
          buildInputs = [
            androidsdk
            flutter
            jdk17
          ];
          ANDROID_SDK_ROOT = "${androidsdk}/libexec/android-sdk";
          LD_LIBRARY_PATH="${pkgs.libglvnd}/lib";
        };
      }
    );
}

Running X on an RX 6950XT and a Ryzen 5 3600 with KVM enabled.

nixos-discourse commented 1 week ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-extend-environment-variables-in-modules/55492/9