microsoft/WSL

WSL2 (Preview) cannot run .exe files: exec format error: wsl.exe

blami opened this issue ยท 26 comments

blami commented

Version

Microsoft Windows [Version 10.0.22621.521]

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

5.15.62.1-microsoft-standard-WSL2

Distro Version

Ubuntu 22.04

Other Software

systemd 249.11-0ubuntu3.3

WSL version: 0.68.2.0
Kernel version: 5.15.62.1
WSLg version: 1.0.44
MSRDC version: 1.2.3401
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.521

Repro Steps

  • [optional] disable wslg
  • [optional] enable systemd support
  • make sure interop is not disabled
  • attempt to run wsl.exe --version from any shell inside WSL2
  • ls /proc/sys/fs/binfmt_misc/ for presence of WSLInterop

Expected Behavior

  • wsl.exe will exe will execute and output version on terminal
  • /proc/sys/fs/binfmt_misc/ will contain custom binary format for WSL interop

Actual Behavior

After upgrade to preview version of WSL2 (ver. 0.68.2.0 from Github on non-Insider build) it was possible to run Windows binaries from within WSL2 by simply having them in PATH and typing binary.exe with systemd enabled and disabled.

After Windows build 22H2 upgrade this is not longer possible. Shell sees binaries in PATH but its not possible to run them, error message being exec format error: wsl.exe. After further inspection I found WSL interop is not registered with binfmt_misc:

~ $ ls /proc/sys/fs/binfmt_misc/
llvm-15-runtime.binfmt  qemu-arm     qemu-mips64el  qemu-riscv64  register
qemu-aarch64            qemu-mips64  qemu-ppc64le   qemu-s390x    status

After upgrade I noticed Hypervisor Platform was disabled although I am sure I had it enabled before. I reenabled it but it didn't solve the issue.

Diagnostic Logs

~ $ cat /etc/wsl.conf
[boot]
systemd = true

[automount]
enabled = true
mountFsTab = true
root = /mnt/
options = "metadata,umask=2,fmask=1,case=off"

[network]
generateHosts = true
generateResolvConf = true

[interop]
enabled = true
~ $ cat /mnt/c/Users/blami/.wslconfig
[wsl2]
guiApplications = false
blami commented

I collected logs while trying to run wsl.exe.

There's repeating exception trace: D:\a\1\s\src\windows\common\svccomm.cpp(95)\wsl.exe!00007FF6EF4D3D14: (caller: 00007FF6EF4D07F6) Exception(1) tid(1840) 80070006 The handle is invalid. "

WslLogs-2022-10-05_17-08-44.zip

blami commented

Disabling systemd support in /etc/wsl.conf resolves the issue.

~ $ cat /etc/wsl.conf
[boot]
#systemd = true

[automount]
enabled = true
mountFsTab = true
root = /mnt/
options = "metadata,umask=2,fmask=1,case=off"

[network]
generateHosts = true
generateResolvConf = true

[interop]
enabled = true

After wsl.exe --shutdown and relaunch:

~ $ powershell.exe
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS Microsoft.PowerShell.Core\FileSystem::\\wsl.localhost\Ubuntu\home\blami>

~ $ ls /proc/sys/fs/binfmt_misc
register  status  WSLInterop

Is this some sort of binfmt_misc setup race condition?

That's exactly what it is. Specifically, it's #8843 , and I posted a workaround over in that issue.

blami commented

@cerebrate Thanks! Not sure why I did not find that one. Will mark mine as duplicate of it. And thanks for workaround. Did some reading and TIL how binfmt works!

Environments

  • Microsoft Windows 11 Professional 22H2 (10.0.22621.1555)
  • Windows Subsystem Linux (Version 2)
    • WSL Version 1.2.5.0
    • Kernel Version 5.15.90.1
    • WSLg Version 1.0.51
  • Ubuntu 23.04 Lunar Lobster

Details

I upgrade to Ubuntu 23.04 today by executing do-release-upgrade. After upgrade process, I find wslfetch goes wrong on my device.

systemd enabled
21:39:41 ~/Documents 0ms $ wslfetch --colorbar --options "windows-build,windows-rel-branch,wsl-release,wsl-kernel,windows-uptime"
grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory
/usr/bin/wslfetch: line 233: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory
/usr/bin/wslsys: line 233: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 233: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 233: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 441: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 233: /mnt/c/Windows/System32/reg.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 267: [: : integer expression expected
/usr/bin/wslsys: line 260: /mnt/c/Windows/System32/chcp.com: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 268: /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe: cannot execute binary file: Exec format error
/usr/bin/wslsys: line 270: [: : integer expression expected
/usr/bin/wslsys: line 260: /mnt/c/Windows/System32/chcp.com: cannot execute binary file: Exec format error
               .-/+oossssoo+/-.               Windows Subsystem for Linux (WSL1)
           `:+ssssssssssssssssss+:`           dragon1573@ubuntu.localhost
         -+ssssssssssssssssssyyssss+-         Build:
       .ossssssssssssssssssdMMMNysssso.       Branch:
      /ssssssssssshdmmNNmmyNMMMMhssssss/      Release: Ubuntu 23.04
     +ssssssssshmydMMMMMMMNddddyssssssss+     Kernel: Linux 5.15.90.1-microsoft-standard-WSL2
    /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Uptime (Windows): 0d 0h 0m
   .ssssssssdMMMNhsssssssssshNMMMdssssssss.
   +sssshhhyNMMNyssssssssssssyNMMMysssssss+
   ossyNMMMNyMMhsssssssssssssshmmmhssssssso
   ossyNMMMNyMMhsssssssssssssshmmmhssssssso
   +sssshhhyNMMNyssssssssssssyNMMMysssssss+
   .ssssssssdMMMNhsssssssssshNMMMdssssssss.
    /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/
     +sssssssssdmydMMMMMMMMddddyssssssss+
      /ssssssssssshdmNNNNmyNMMMMhssssss/
       .ossssssssssssssssssdMMMNysssso.
         -+sssssssssssssssssyyyssss+-
           `:+ssssssssssssssssss+:`
               .-/+oossssoo+/-.
21:39:45 ~/Documents 245ms $ ls /proc/sys/fs/binfmt_misc
jar  python3.11  register  status

21:40:41 ~/Documents 3ms $ code .
/usr/local/bin/code: 61: /mnt/d/Applications/WinGet/Microsoft/VSCode/Code.exe: Exec format error

When switching systemd = false in /etc/wsl.conf, this issue does solved with some other informations.

systemd disabled
21:42:41 ~/Documents 0ms $ ls /proc/sys/fs/binfmt_misc
register  status  WSLInterop
21:42:44 ~/Documents 9ms $ wslfetch --colorbar --options "windows-build,windows-rel-branch,wsl-release,wsl-kernel,windows-uptime"
ๆ‰พไธๅˆฐโ€œGetEncodingโ€็š„้‡่ฝฝ๏ผŒๅ‚ๆ•ฐ่ฎกๆ•ฐไธบ:โ€œ0โ€ใ€‚
ๆ‰€ๅœจไฝ็ฝฎ ่กŒ:1 ๅญ—็ฌฆ: 59
+ ... ing]::UTF8; [Console]::InputEncoding = [System.Text.Encoding]::GetEnc ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

               .-/+oossssoo+/-.               Windows Subsystem for Linux (WSL2)
           `:+ssssssssssssssssss+:`           dragon1573@ubuntu.localhost
         -+ssssssssssssssssssyyssss+-         Build: 22621
       .ossssssssssssssssssdMMMNysssso.       Branch: ni_release
      /ssssssssssshdmmNNmmyNMMMMhssssss/      Release: Ubuntu 23.04
     +ssssssssshmydMMMMMMMNddddyssssssss+     Kernel: Linux 5.15.90.1-microsoft-standard-WSL2
    /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Uptime (Windows): 0d 5h 12m
   .ssssssssdMMMNhsssssssssshNMMMdssssssss.
   +sssshhhyNMMNyssssssssssssyNMMMysssssss+
   ossyNMMMNyMMhsssssssssssssshmmmhssssssso
   ossyNMMMNyMMhsssssssssssssshmmmhssssssso
   +sssshhhyNMMNyssssssssssssyNMMMysssssss+
   .ssssssssdMMMNhsssssssssshNMMMdssssssss.
    /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/
     +sssssssssdmydMMMMMMMMddddyssssssss+
      /ssssssssssshdmNNNNmyNMMMMhssssss/
       .ossssssssssssssssssdMMMNysssso.
         -+sssssssssssssssssyyyssss+-
           `:+ssssssssssssssssss+:`
               .-/+oossssoo+/-.

My /etc/wsl.conf looks like follows:

[boot]
systemd = false

[network]
hostname = "ubuntu.localhost"

[interop]
enabled = true
appendWindowsPath = false

[user]
default = dragon1573

[automount]
enabled = true
mountFsTab = true
root = /mnt/
options = "metadata,umask=022,fmask=011"

[filesystem]
umask = 022
fmask = 011

Same problem after upgrade to Ubuntu 23.04.

โฏ code
/mnt/c/Users/mystic/AppData/Local/Programs/Microsoft VS Code/bin/code: 61: /mnt/c/Users/mystic/AppData/Local/Programs/Microsoft VS Code/Code.exe: Exec format error
โฏ explorer.exe .
zsh: exec format error: explorer.exe

Shutting down systemd didn't solve the problem


Win11 22h2 22621.1413
WSL2


upd
Same problem was after updating to 22.10. Last time and now the problem was solved by complete reinstall distro.. If I didnt have my own env-build project, I'd be just mad. Solve this issue...

Same issue here.

/etc/release

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Only works when disabling systemd

[user]
default=vinicius

[boot]
systemd=false

The issue is that enabling systemd somehow alters /proc/sys/fs/binfmt_misc/
The file (or filelike??) entry WSLInterop.conf goes missing.

I've re-written the answer here rather than just linking as the original reference thread is getting rather long and has dead links.
The oneliner elimiates that problem.

The solution (from here) is to replace that file. Windows executable files start with the two letters MZ
The following tells Linux what to do when trying to run files that start with those letters.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl restart systemd-binfmt

I;m using the 23.04 image from links in this post
I guess this is a bug in packaging, or during installation, but I've no idea who to tell.

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

just in case if anyone is still facing the issue, try disabling systemd in /etc/wsl.conf in the distro.

just in case if anyone is still facing the issue, try disabling systemd in /etc/wsl.conf in the distro.

Thanks man! This worked for me!

hros commented

Thanks @tardis89
The command solves the issue, and now code starts from wsl.
Can you explain what was the problem, and how do these command solve the issue?

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

The issue is that enabling systemd somehow alters /proc/sys/fs/binfmt_misc/ The file (or filelike??) entry WSLInterop.conf goes missing.

I've re-written the answer here rather than just linking as the original reference thread is getting rather long and has dead links. The oneliner elimiates that problem.

The solution (from here) is to replace that file. Windows executable files start with the two letters MZ The following tells Linux what to do when trying to run files that start with those letters.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl restart systemd-binfmt

I;m using the 23.04 image from links in this post I guess this is a bug in packaging, or during installation, but I've no idea who to tell.

Thanks for the solution!

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

Thank you so much. It solved my problem.
Not only this I had problem in running ddev launch command it also fixed it.

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

Thank you so much. It solved my problem. Not only this I had problem in running ddev launch command it also fixed it.

For me this helps but it doesn't solve it, because the problem reappears later and I need to run these commands again!

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

Thank you so much. It solved my problem. Not only this I had problem in running ddev launch command it also fixed it.

For me this helps but it doesn't solve it, because the problem reappears later and I need to run these commands again!

Not sure why, but there is no such problem on my side. The mentioned solution fixed it for once and all.

The issue is that enabling systemd somehow alters /proc/sys/fs/binfmt_misc/ The file (or filelike??) entry WSLInterop.conf goes missing.

I've re-written the answer here rather than just linking as the original reference thread is getting rather long and has dead links. The oneliner elimiates that problem.

The solution (from here) is to replace that file. Windows executable files start with the two letters MZ The following tells Linux what to do when trying to run files that start with those letters.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl restart systemd-binfmt

I;m using the 23.04 image from links in this post I guess this is a bug in packaging, or during installation, but I've no idea who to tell.

Works perfectly! Thank you...

The issue is that enabling systemd somehow alters /proc/sys/fs/binfmt_misc/ The file (or filelike??) entry WSLInterop.conf goes missing.
I've re-written the answer here rather than just linking as the original reference thread is getting rather long and has dead links. The oneliner elimiates that problem.
The solution (from here) is to replace that file. Windows executable files start with the two letters MZ The following tells Linux what to do when trying to run files that start with those letters.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl restart systemd-binfmt

I;m using the 23.04 image from links in this post I guess this is a bug in packaging, or during installation, but I've no idea who to tell.

Thanks for the solution!

Muchรญsimas gracias, esos 2 comandos me solucionaron el problema.

I am getting this error because of minikube. The commands above would fix the problem until I ran minikube again. Are there any other options?

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

Thank you so much. It solved my problem. Not only this I had problem in running ddev launch command it also fixed it.

For me this helps but it doesn't solve it, because the problem reappears later and I need to run these commands again!

me too getting the same problem

I am getting this error because of minikube. The commands above would fix the problem until I ran minikube again. Are there any other options?

My only fix was to stop using Docker Desktop and to manually install docker onto WSL. After that, minikube stopped breaking my command line.

@the-moog's solution worked! The only extra step I had to add was unmasking the systemd-binfmt service in order to get it working. So to add to his steps. I unmask the service, restart it, then re-mask it to return it back to its previous protected state besides the new modification.

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'
sudo systemctl unmask systemd-binfmt.service
sudo systemctl restart systemd-binfmt
sudo systemctl mask systemd-binfmt.service

thanks that worked for me, just had to also restart wsl

Solutions here didnt work for me. For vscode ๐Ÿ˜ญ

/mnt/d/Microsoft VS Code/bin/code: 61: /mnt/d/Microsoft VS Code/Code.exe: Exec format error

The only thing that temporarily fixes the issue for me:

  1. open cmd
  2. run wsl
  3. rm -rf ~/.vscode-server
  4. exit
  5. back in cmd terminal, run: wsl --shutdown
  6. relaunch wsl

VS code will work for a short period before randomly the error comes back and I have to repeat my steps.

Anybody else having this issue or know of a permanent fix?

In my case, I found out my Docker Desktop wasn't running (:

For me restarting the distribution was sufficient. @surgiie have you tried it without uninstall vscode server? It should not be related to the problem, as it is Windows interop in general