jart / cosmopolitan

build-once run-anywhere c library
ISC License
18.36k stars 630 forks source link

WSL 2 oddities #6

Closed abl closed 4 years ago

abl commented 4 years ago

Really interesting project!

Started looking at the example under WSL 2 (Ubuntu 20.04.1 LTS) since I had that handy.

❯ make -j1 o//tool/viz/printvideo.com
MKDEPS         o//depend
PACKAGE        o//libc/stubs/stubs.a.pkg
error:package.com: check failed: 0x4480 == 0x88 (E?/err=0/errno:0/GetLastError:187 The operation completed successfully.)
make: *** [build/rules.mk:76: o//libc/stubs/stubs.a.pkg] Error 32

Running with -j8 fails with errnos 5 and 32.

WSL 2 does have a binfmt_misc set up for windows interop; disabling it with sudo sh -c 'echo 0 > /proc/sys/fs/binfmt_misc/status' allows make -j8 to succeed - deleted my clone and pulled a fresh one after disabling.

~/cosmopolitan master*
❯ o//tool/viz/printvideo.com cats.mpg
zsh: exec format error: o//tool/viz/printvideo.com

~/cosmopolitan master*
❯ file o//tool/viz/printvideo.com
o//tool/viz/printvideo.com: DOS/MBR boot sector

Re-enabling binfmt_misc afterwards gets a little farther:

~/cosmopolitan master*
❯ sudo sh -c 'echo 1 > /proc/sys/fs/binfmt_misc/status'

# The following happens regardless of answering y or n

~/cosmopolitan master*
❯ o//tool/viz/printvideo.com cats.mpg
ffplay not found; continue without audio? [yn] libc/runtime/mmap.c:143:gap > 0

Attempting to run the executable from Windows directly produces the same (lack of) output; ffplay is installed on the WSL side, fwiw.

If anything jumps out as far as what I should try/test please let me know - I've just started to trace and debug now.

jart commented 4 years ago

Try exploring the contents of ls /proc/sys/fs/binfmt_misc/ and report back. Microsoft might have installed some MZ executable shell script hook even though WSL can't run Windows binaries.

As far as playing sound is concerned, all you need to do is apt install either ffmpeg or sox. No system interface consensus exists for sound so we do it by piping raw audio into those commands.

abl commented 4 years ago

Yeah, binfmt_misc is clearly part of the problem; here's the one registered definition.

❯ \cat /proc/sys/fs/binfmt_misc/WSLInterop
enabled
interpreter /tools/init
flags: F
offset 0
magic 4d5a

I had a chance to read your αpε writeup and noticed the zsh issue; sh o/tool/viz/printvideo.com cats.mpg worked with binfmt_misc on!

...then I toggled it off, and it didn't:

❯ sh o/tool/viz/printvideo.com cats.mpg
o/tool/viz/printvideo.com: 1: ELF: not found
o/tool/viz/printvideo.com: 2: @8�@�����H�1ҽ��8��p1���׉��X�^��k����u����ގ�1�1���@���1���U��0���1�1���@t#�.�����0�1ɉ��]�ƃ�: not found
o/tool/viz/printvideo.com: 3: Syntax error: ")" unexpected

...then I toggled it back on, and it still didn't, same output.

However, o/tool/viz/printvideo.com cats.mpg now works directly in ZSH regardless of binfmt_misc status. sh -c o/tool/viz/printvideo.com cats.mpg works, bash works, running the command directly in sh/bash works.

jart commented 4 years ago

Could you try running the following command and let me know if it fixes the issue?

sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"
abl commented 4 years ago

Registered that format:

───────┬──────────────────────────────────────────────────
       │ File: /proc/sys/fs/binfmt_misc/APE
───────┼──────────────────────────────────────────────────
   1   │ enabled
   2   │ interpreter /bin/sh
   3   │ flags:
   4   │ offset 0
   5   │ magic 4d5a71467044
───────┴──────────────────────────────────────────────────

No effect with binfmt_misc enabled/disabled.

FWIW /bin/sh is symlinked to dash?

jart commented 4 years ago

I'm having difficulties figuring out how to get binfmt_misc to work. I would recommend disabling it entirely if possible. Hopefully someone else can come along who understands it and show us how to configure it for the people who do.

jart commented 4 years ago

Another user is reporting in #2 that the one-liner workaround I posted a few comments earlier should be OK. I'm going to close out this issue. Thanks for the report!

jart commented 3 years ago

Marking as duplicate of #2 for posterity. Thanks again for using Cosmopolitan!