jart / cosmopolitan

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

Failed to run redbean on Linux #51

Closed rapiz1 closed 3 years ago

rapiz1 commented 3 years ago

I have wine installed on my machine and ./redbean invokes wine. And it failed to run.

Not surprised to see wine failing. But I wonder how to run the program as ELF?

Here's the log if related. I'm not interested in running it with wine anyway.

MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

0024:err:module:process_init L"Z:\\home\\rapiz\\Downloads\\redbean-2021-02-25.com" 64-bit application not supported in 32-bit prefix
rajat1saxena commented 3 years ago

I am unable to run it on Windows 10 and Ubuntu (WSL).

On Windows 10, I am getting a "This app cannot run on your PC" error and on Ubuntu I'm getting the following error.

rajat@rajat-desktop:/mnt/c/Users/91858/Downloads$ cat index.html
<b>hello</b>
rajat@rajat-desktop:/mnt/c/Users/91858/Downloads$ zip redbean.com index.html
  adding: index.html (stored 0%)
rajat@rajat-desktop:/mnt/c/Users/91858/Downloads$ ./redbean.com -vv
./redbean.com: Invalid argument
phil294 commented 3 years ago

The wine-related problem can be reproduced even with the basic hello-world binary from the readme. Here is the output from my machine (Manjaro Linux 5.4.91-1, bash 5.1.0, wine installed):

$ wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-0.2.zip
$ unzip cosmopolitan-amalgamation-0.2.zip 
$ printf 'main() { printf("hello world\\n"); }\n' >hello.c
$ gcc -g -O -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
   -o hello.com.dbg hello.c -fuse-ld=bfd -Wl,-T,ape.lds \
   -include cosmopolitan.h crt.o ape.o cosmopolitan.a
/usr/bin/ld.bfd: warning: .note.gnu.build-id section discarded, --build-id ignored
$ objcopy -S -O binary hello.com.dbg hello.com
$ ./hello.com      # <- The typical two gecko / html popups from wine (when you start a fresh prefix) appeared
MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

Could not find Wine Gecko. HTML rendering will be disabled.
Could not find Wine Gecko. HTML rendering will be disabled.
0134:err:module:open_builtin_file failed to load .so lib "/usr/lib32/wine/winegstreamer.dll.so"
wine: configuration in L"/home/phi/.wine" has been updated.
014c:fixme:heap:CreateFileMappingNumaW Ignoring preferred node 4294967295
014c:fixme:heap:MapViewOfFileExNuma Ignoring preferred node 4294967295
hello world
$ ./hello.com
0168:fixme:heap:CreateFileMappingNumaW Ignoring preferred node 4294967295
0168:fixme:heap:MapViewOfFileExNuma Ignoring preferred node 4294967295
hello world

So I guess it's reassuring it also runs on Windows, but I somewhat expected it to not invoke wine :-D

shaleh commented 3 years ago

I bet your Linux environment is configured to run ".com" files under Wine. Try renaming the binary.

rapiz1 commented 3 years ago

Tried renaming to redbean. Got the same error.

rapiz1 commented 3 years ago

I read about the ape format and figure it out. When initially compiled or downloaded, the ape file is a valid shell script, but not a valid ELF file. When an ape firstly runs under Linux, it detects the environment and rewrites itself with an ELF header.

So at the first time, run sh ./hello.com. After that, run with ./hello.com. Note that you have to chmod +x hello.com even before the first run, using sh. Otherwise, you see hello.com: line 7: : No such file or directory. sh does not require its input file to have the executable bit set. But the internal logic of ape takes the advantage of that.

For more information, just open the ape file with a text editor and take a look on your own.

jart commented 3 years ago

Try downloading the latest redbean build here https://justine.lol/redbean/index.html

If you get errors like unable to find interpreter or other things related to wine and binfmt_misc then you should be able to fix that by running sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"

ephjos commented 3 years ago

Try downloading the latest redbean build here https://justine.lol/redbean/index.html

If you get errors like unable to find interpreter or other things related to wine and binfmt_misc then you should be able to fix that by running sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"

This did fix my issues and got the server running for me! On Arch Linux, first the server wasn't working with wine installed. I uninstalled wine but then was getting the bash: line 1: ./redbean.com: No such file or directory message when running bash -c './redbean.com' -vv.

FWIW uname -a returns the following:

Linux desktop 5.11.1-arch1-1 #1 SMP PREEMPT Tue, 23 Feb 2021 14:05:30 +0000 x86_64 GNU/Linux

And bash --version returns:

GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)

@jart Please let me know if there's any more information that may be helpful, and for creating such an interesting piece of software!

fungos commented 3 years ago

Crashing on Manjaro:

~ ➜ ./redbean-2021-02-27.com 
error:./redbean-2021-02-27.com: check failed: 0xffffffffffffffff != 0xffffffffffffffff (98)

error: Uncaught SIGSEGV on darkbrain
/home/fungos/redbean-2021-02-27.com
ENOENT/err=2/errno:2/GetLastError:0
Linux 5.4.97-1-MANJARO #1 SMP PREEMPT Wed Feb 10 10:24:32 UTC 2021

RAX 2020202020200930 RBX 202020202020093a RDI 202020202020093a ST(0) 0.000000
RCX 000000000000000a RDX 0000000000000002 RSI 202020202020093a ST(1) 0.000000
RBP 00006ffffffffcd0 RSP 00006ffffffffcd0 RIP 0000000000422252 ST(2) 0.000000
R8  0000000000000001 R9  0000000000000021 R10 0000000000000000 ST(3) 0.000000
R11 0000000000000293 R12 00006ffffffffd08 R13 00006ffffffffd10 ST(4) 0.000000
R14 000000000045d360 R15 0000000000000029 VF PF IF PE

XMM0  00000000000000ff0000000000000000 XMM8  00000001c6e415960000000154442bd4
XMM1  2f3d4c4c454853006d6f632e37322d32 XMM9  c7ffff0c992785596d9b49152a4acc1a
XMM2  00000000000000000000000000000000 XMM10 00000000000000000000000000000000
XMM3  ffffffffffffff000000000000000000 XMM11 00000000000000000000000000000000
XMM4  00000000000000000000000000000000 XMM12 00000000000000000000000000000000
XMM5  00000000e29ed9b33e8a30a710b5f380 XMM13 00000000000000000000000000000000
XMM6  302e322e334d2c544445355453450a01 XMM14 00000000000000000000000000000000
XMM7  01c0c7ffff00009ebaffff0201020102 XMM15 00000000000000000000000000000000

mm->i == 5;
mm->p[  0]=={0x10008000,0x10008000}; /* 1 */
mm->p[  1]=={0x10008001,0x10008004}; /* 4 */
mm->p[  2]=={0x10008005,0x10008006}; /* 2 */
mm->p[  3]=={0x10008007,0x1000800a}; /* 4 */
/* 1,610,579,941 */
mm->p[  4]=={0x6ffffff0,0x6fffffff}; /* 16 */
/* 27 frames mapped w/ 1,610,579,941 frames gapped */

00400000-00429000 r-xp 00000000 08:15 530681                             /home/fungos/redbean-2021-02-27.com
00429000-0042a000 rw-p 00029000 08:15 530681                             /home/fungos/redbean-2021-02-27.com
0042a000-00460000 rw-p 00000000 00:00 0 
100080000000-100080010000 rw-p 00000000 00:00 0 
100080010000-100080042000 r--s 00000000 08:15 530681                     /home/fungos/redbean-2021-02-27.com
100080050000-100080070000 rw-p 00000000 00:00 0 
100080070000-1000800a2000 r--s 00000000 08:15 530681                     /home/fungos/redbean-2021-02-27.com
6ffffff00000-700000000000 rw-p 00000000 00:00 0 
7fff2c072000-7fff2c093000 rw-p 00000000 00:00 0                          [stack]
7fff2c1d8000-7fff2c1db000 r--p 00000000 00:00 0                          [vvar]
7fff2c1db000-7fff2c1dc000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

./redbean-2021-02-27.com 
~ took 26s ➜ ./redbean-2021-02-27.com
error:./redbean-2021-02-27.com: check failed: 0xffffffffffffffff != 0xffffffffffffffff (98)
6fffffffff60 0000004026bf UNKNOWN
6fffffffff70 000000402415 UNKNOWN
6fffffffffc0 0000004019e3 UNKNOWN
6fffffffffe0 000000401c11 UNKNOWN
7ffe74934510 000000401467 UNKNOWN

If I run redbean with debug info:

~ ➜ ./redbean-2021-02-27.com.dbg 
error:./redbean-2021-02-27.com.dbg: check failed: 0x0 != 0x0 (8)
0x00000000004026be: __die at libc/log/die.c:40
0x00000000004024aa: RedBean at tool/net/redbean.c:1141
0x00000000004019e2: main at tool/net/redbean.c:1188
0x0000000000401c10: cosmo at libc/runtime/cosmo.S:65
0x0000000000401466: _start at libc/crt/crt.S:67

But that may be because the .dbg is not a valid zip archive.

cache-return commented 3 years ago

Try downloading the latest redbean build here https://justine.lol/redbean/index.html

If you get errors like unable to find interpreter or other things related to wine and binfmt_misc then you should be able to fix that by running sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"

This solution worked for me (on Arch), but it doesn't survive a reboot. What does it mean? e.g. Is this a problem with how the distro configures binfmt_misc and they should change to support APE, or is it a bug?

Also, thanks. This is amazing.

dimovich commented 3 years ago

Try downloading the latest redbean build here https://justine.lol/redbean/index.html

If you get errors like unable to find interpreter or other things related to wine and binfmt_misc then you should be able to fix that by running sudo sh -c "echo ':APE:M::MZqFpD::/bin/sh:' >/proc/sys/fs/binfmt_misc/register"

Fixed the error for me on Ubuntu 16.04.

jart commented 3 years ago

This solution worked for me (on Arch), but it doesn't survive a reboot.

I can't offer support on system configuration because every Linux does /etc/rc.local differently and it changes every few years.

Fixed the error for me on Ubuntu 16.04.

Awesome to hear. I've also now pushed Address Sanitizer hardened builds of redbean to the website. ASAN makes C a nearly memory safe language. That way programming mistakes such as buffer overflows should result in a process crashing rather than undefined behavior. Since redbean is a forking web server, crashes will only impact the client that attempted to exploit the bug and a backtrace will be logged with symbols if you have the .com.dbg file in the same folder, so please report them. https://justine.lol/redbean/index.html

fungos commented 3 years ago

FYI, having dbg on the side helped find the issue, which was simple enough but a bummer as it could be more self-explaining.

error:tool/net/redbean.c:1159:redbean-asan-2021-03-02.com: check failed on darkbrain pid 3863397
CHECK_NE(-1, bind(server, &serveraddr, sizeof(serveraddr)));
→ 0xffffffffffffffff (-1)
!= 0xffffffffffffffff (bind(server, &serveraddr, sizeof(serveraddr)))
EADDRINUSE/err=98/errno:98/GetLastError:0
./redbean-asan-2021-03-02.com
0x00000000004078d5: __die at libc/log/die.c:40
0x0000000000407899: __check_fail at libc/log/checkfail.c:88
0x0000000000407488: RedBean at tool/net/redbean.c:1159
0x0000000000402817: main at tool/net/redbean.c:1194
0x000000000040346e: cosmo at libc/runtime/cosmo.S:65
0x0000000000402247: _start at libc/crt/crt.S:67
6ffffffffd70 0000004078d6 __die+0x3c
6ffffffffd80 00000040789a __check_fail+0x270
6ffffffffed0 000000407489 RedBean.cold+0x39
6fffffffffc0 000000402818 main+0x28
6fffffffffe0 00000040346f cosmo+0x40
7fff5b79ccc0 000000402248 _start+0x6a

Which means, my port 8080 was in use. Using -P works. As we can see in practice here, asserting (and crashing) is not a good way to report user errors :)

jart commented 3 years ago

You're right @fungos. I've made a change based on your feedback. My default course is normally to use the CHECK macros since any system call can potentially fail with a variety of errors and it's hard to know which ones are going to happen in practice ahead of time. Now that we know, we should add the additional lines of code that present better error messages, since it aids usability.