WSL icon indicating copy to clipboard operation
WSL copied to clipboard

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

Open blami opened this issue 4 years ago • 45 comments

Version

Microsoft Windows [Version 10.0.22621.521]

WSL Version

  • [X] 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 avatar Oct 05 '22 06:10 blami

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 avatar Oct 05 '22 08:10 blami

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?

blami avatar Oct 05 '22 12:10 blami

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

cerebrate avatar Oct 08 '22 00:10 cerebrate

@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!

blami avatar Oct 08 '22 08:10 blami

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+:`           [email protected]
         -+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+:`           [email protected]
         -+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

Dragon1573 avatar Apr 21 '23 13:04 Dragon1573

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...

Mystic8b avatar Apr 22 '23 16:04 Mystic8b

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

ViniciusBastosTR avatar Apr 28 '23 21:04 ViniciusBastosTR

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 avatar May 30 '23 10:05 the-moog

@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

tardis89 avatar Jun 01 '23 14:06 tardis89

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

khushal123 avatar Aug 03 '23 18:08 khushal123

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!

renanlido avatar Aug 04 '23 23:08 renanlido

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

hros avatar Aug 09 '23 01:08 hros

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!

abrahamprz avatar Aug 09 '23 03:08 abrahamprz

@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.

sanjayojha avatar Aug 19 '23 16:08 sanjayojha

@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!

maicon-cesar avatar Aug 23 '23 13:08 maicon-cesar

[like] Brian Kroth (GSL) reacted to your message:


bpkroth avatar Aug 24 '23 19:08 bpkroth

@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.

sanjaybip avatar Aug 25 '23 04:08 sanjaybip

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...

aravindsomaraj avatar Nov 06 '23 18:11 aravindsomaraj

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.

Eliezer134 avatar Nov 22 '23 02:11 Eliezer134

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?

stephanlamoureux avatar Jan 20 '24 16:01 stephanlamoureux