Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig libc crashes with out of memory error on loongarch64 #21468

Open
yxd-ym opened this issue Sep 20, 2024 · 7 comments
Open

zig libc crashes with out of memory error on loongarch64 #21468

yxd-ym opened this issue Sep 20, 2024 · 7 comments
Labels
arch-loongarch 32-bit and 64-bit LoongArch bug Observed behavior contradicts documented or intended behavior os-linux

Comments

@yxd-ym
Copy link
Contributor

yxd-ym commented Sep 20, 2024

Zig Version

0.14.0-dev.1622+2ac543388

Steps to Reproduce and Observed Behavior

  1. Bootstrap a loongarch64 linux zig with ziglang/zig-bootstrap@1ad232c , either musl or gnu.
  2. Run zig libc on a loongarch64 linux machine with the bootstrapped zig
  3. The error occurs like this:
$ ~/opt/zig/musl/zig libc
error: OutOfMemory

Expected Behavior

zig libc returns correct libc related information on loongarch64 like x86_64 or aarch64 ones

@yxd-ym yxd-ym added the bug Observed behavior contradicts documented or intended behavior label Sep 20, 2024
@yxd-ym
Copy link
Contributor Author

yxd-ym commented Sep 20, 2024

also see: ziglang/zig-bootstrap#164 (comment)

@alexrp
Copy link
Member

alexrp commented Sep 20, 2024

Does it happen under QEMU?

@yxd-ym
Copy link
Contributor Author

yxd-ym commented Sep 21, 2024

I bootstrapped another version with ReleaseSafe mode and the error is different

 $ ~/opt/zig/musl/zig libc
thread 59547 panic: incorrect alignment
Aborted (core dumped)

Here is the backtrace output.

(gdb) r libc
Starting program: /home/yxd/opt/zig/musl/zig libc
[New LWP 59504]
[New LWP 59505]
[New LWP 59506]
[New LWP 59507]
[New LWP 59508]
thread 59502 panic: incorrect alignment

Thread 1 "zig" received signal SIGABRT, Aborted.
raise (sig=<optimized out>) at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/signal/raise.c:12
warning: 12     /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/signal/raise.c: No such file or directory
(gdb) bt
#0  raise (sig=<optimized out>) at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/signal/raise.c:12
#1  0x000000000a8dc530 in abort () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/exit/abort.c:11
#2  0x0000000009e04490 in posix.abort () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/posix.zig:706
#3  0x000000000a840068 in debug.panicImpl (trace=0x0, first_trace_addr=...) at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/io/Writer.zig:19
#4  0x00000000099fef58 in builtin.default_panic (error_return_trace=0x0, ret_addr=...) at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/builtin.zig:885
#5  0x0000000009c247c4 in posix.mmap (ptr=<optimized out>, length=528, prot=<optimized out>, flags=<optimized out>, fd=<optimized out>, offset=0)
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/posix.zig:4706
#6  0x0000000009bc1a58 in crypto.tlcsprng.tlsCsprngFill () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/crypto/tlcsprng.zig:64
#7  0x0000000009c89488 in Random.bytes () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/Random.zig:55
#8  Random.int__anon_48074 () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/Random.zig:109
#9  Compilation.update (comp=0x7ffff7f9d350, main_progress_node=...) at Compilation.zig:2122
#10 0x0000000009c8fc14 in main.updateModule (comp=0x7ffff7f9d350, color=auto, prog_node=...) at main.zig:4499
#11 0x0000000009a2af0c in main.jitCmd (gpa=..., arena=..., options=...) at main.zig:5586
#12 0x00000000099feb7c in main.mainArgs (arena=...) at main.zig:303
#13 main.main () at main.zig:199
#14 start.callMain () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/start.zig:619
#15 start.callMainWithArgs () at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/start.zig:579
#16 start.main (c_argc=<optimized out>, c_argv=<optimized out>, c_envp=<optimized out>) at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/start.zig:594

update

I got the reason of this problem, my loongarch64 machine's memory page size is not 16KiB but 4KiB.

yxd@derp ~/tmp/test_zig $ getconf PAGESIZE
4096

I will check this with linux distribution's packagers.

@yxd-ym
Copy link
Contributor Author

yxd-ym commented Sep 21, 2024

Does it happen under QEMU?

I haven't try it on qemu. I will check the page size thing first.

(The two errors happen on two different machines (with same linux distribution (loongarchlinux)) with different zig optimize values (ReleaseFast vs. ReleaseSafe (no strip)))

@alexrp
Copy link
Member

alexrp commented Sep 21, 2024

Ok, seems like this would be addressed by #20511.

@yxd-ym
Copy link
Contributor Author

yxd-ym commented Sep 23, 2024

Ok, seems like this would be addressed by #20511.

OK, I will wait for this PR merged and check again.


However, I bootstrapped a 4KiB page size version of loongarch64-linux-musl zig for my 4KiB page size loongarchlinux machine.

For this version, zig libc works, but only with ZIG_DEBUG_CMD=1 set.

yxd@derp ~/tmp/test_zig $ ZIG_DEBUG_CMD=1 ~/opt/zig/musl/zig libc
# The directory that contains `stdlib.h`.
# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
include_dir=/usr/include

# The system-specific include directory. May be the same as `include_dir`.
# On Windows it's the directory that includes `vcruntime.h`.
# On POSIX it's the directory that includes `sys/errno.h`.
sys_include_dir=/usr/include

# The directory that contains `crt1.o` or `crt2.o`.
# On POSIX, can be found with `cc -print-file-name=crt1.o`.
# Not needed when targeting MacOS.
crt_dir=/usr/lib/gcc/loongarch64-unknown-linux-gnu/14.1.1/../../..

# The directory that contains `vcruntime.lib`.
# Only needed when targeting MSVC on Windows.
msvc_lib_dir=

# The directory that contains `kernel32.lib`.
# Only needed when targeting MSVC on Windows.
kernel32_lib_dir=

# The directory that contains `crtbeginS.o` and `crtendS.o`
# Only needed when targeting Haiku.
gcc_dir=

It will crash if this environment variable is not set.

yxd@derp ~/tmp/test_zig $ ~/opt/zig/musl/zig libc
Segmentation fault (core dumped)

I checked the code here

https://github.com/ziglang/zig-bootstrap/blob/9b6a22d685f78799eeb69487161df82b6c943116/zig/src/main.zig#L5452-L5455

So it seems that running jitCmd with ReleaseFast for loongarch might cause some error.

@yxd-ym
Copy link
Contributor Author

yxd-ym commented Sep 23, 2024

Backtrace of

yxd@derp ~/tmp/test_zig $ ~/opt/zig/musl/zig libc
Segmentation fault (core dumped)

is like this after I disabled strip in jitCmd function for ReleaseFast.

(gdb) r libc
Starting program: /home/yxd/.cache/zig/o/524096a6eb1b49fde3b21dc390171077/libc libc

Program received signal SIGSEGV, Segmentation fault.
0x0000000001083b6c in posix.openatZ (dir_fd=<optimized out>, file_path=0x7ffffffef9b8 "/usr/bin/env", flags=<optimized out>, mode=0)
    at /home/yxd/opt/zig/musl-debug/lib/std/posix.zig:1775
1775            switch (errno(rc)) {
(gdb) bt
#0  0x0000000001083b6c in posix.openatZ (dir_fd=<optimized out>, file_path=0x7ffffffef9b8 "/usr/bin/env", flags=<optimized out>, mode=0)
    at /home/yxd/opt/zig/musl-debug/lib/std/posix.zig:1775
#1  0x00000000010753c0 in fs.Dir.openFileZ (sub_path=0x7ffffffef9b8 "/usr/bin/env", flags=...) at /home/yxd/opt/zig/musl-debug/lib/std/fs/Dir.zig:882
#2  fs.Dir.openFile (flags=...) at /home/yxd/opt/zig/musl-debug/lib/std/fs/Dir.zig:829
#3  0x000000000107c93c in fs.openFileAbsolute () at /home/yxd/opt/zig/musl-debug/lib/std/fs.zig:280
#4  zig.system.detectAbiAndDynamicLinker (os=..., query=...) at /home/yxd/opt/zig/musl-debug/lib/std/zig/system.zig:1037
#5  zig.system.resolveTargetQuery (query=...) at /home/yxd/opt/zig/musl-debug/lib/std/zig/system.zig:348
#6  zig.resolveTargetQueryOrFatal (target_query=...) at /home/yxd/opt/zig/musl-debug/lib/std/zig.zig:613
#7  libc.main () at libc.zig:66
#8  0x00000000010752a4 in start.callMain () at /home/yxd/opt/zig/musl-debug/lib/std/start.zig:619
#9  start.callMainWithArgs () at /home/yxd/opt/zig/musl-debug/lib/std/start.zig:579
#10 start.posixCallMainAndExit (argc_argv_ptr=<optimized out>) at /home/yxd/opt/zig/musl-debug/lib/std/start.zig:535
#11 0x0000000000000000 in ?? ()
Backtrace stopped: frame did not save the PC

and in frame 0, rc = 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-loongarch 32-bit and 64-bit LoongArch bug Observed behavior contradicts documented or intended behavior os-linux
Projects
None yet
Development

No branches or pull requests

2 participants