zig icon indicating copy to clipboard operation
zig copied to clipboard

"zig cc" abort when compiling a certain C source file

Open jvo203 opened this issue 2 years ago • 6 comments

Zig Version

0.10.1

Steps to Reproduce and Observed Behavior

Trying to compile a C file as part of a bigger project: chris@studio /tmp % zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c zsh: abort zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c

On the other hand, compiling a simple test file works fine: chris@studio /tmp % zig cc -march=native -g -Ofast -fPIC -o histogram.o -c simple.c simple.c:

#include <math.h>

int sum(int a, int b)
{
	return a + b ;
}

It is impossible to attach a ".c" file in GitHub so attached please find "histogram.c.txt" instead.
[histogram.c.txt](https://github.com/ziglang/zig/files/10947839/histogram.c.txt)

The system is Apple M1 Ultra running macOS Ventura 13.2.1.

chris@studio /tmp % zig cc --version Homebrew clang version 15.0.7 Target: arm64-apple-darwin22.3.0 Thread model: posix InstalledDir: /usr/bin


### Expected Behavior

```histogram.c``` is compiled into ```histogram.o```

jvo203 avatar Mar 11 '23 06:03 jvo203

Translating histogram.c from C to ZIG also fails with a segmentation fault:

chris@studio /tmp % zig translate-c histogram.c > histogram.zig
zsh: segmentation fault  zig translate-c histogram.c > histogram.zig

The simple.c works fine:

chris@studio /tmp % zig translate-c simple.c > simple.zig
chris@studio /tmp %

jvo203 avatar Mar 11 '23 07:03 jvo203

I just built zig from source (linux) and opened this random issue and tried it out. The current git (4ea2f441df36cec61e1017f4d795d4037326c98c) seems to work for both cc and translate-c.

updated with some versions:

➜  build git:(master) zig version  
0.11.0-dev.1929+4ea2f441d

➜  build git:(master) zig cc --version 
clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

uname -a
Linux beefcake 5.14.0-1054-oem #61-Ubuntu SMP Fri Oct 14 13:05:50 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.10
Release:	22.10
Codename:	kinetic

erg avatar Mar 11 '23 18:03 erg

Thanks, it's encouraging to hear it. Do you think there might be differences between zig on Apple Silicon macOS and Linux that might affect the reproducibility of this bug?

Hopefully there might be a new zig release in the near future, and it will make its way to the open-source Homebrew package installer on macOS soon.

Edit: I guess it's time to try to build the latest development zig from source on macOS. There is only one way to find out!

jvo203 avatar Mar 11 '23 23:03 jvo203

Unfortunately building the latest development zig from source on Apple Silicon macOS does not fix the problem.

chris@vpn-39-132 /tmp % zig translate-c histogram.c > histogram.zig
zsh: segmentation fault  zig translate-c histogram.c > histogram.zig
chris@vpn-39-132 /tmp % zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c
zsh: abort      zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c
chris@vpn-39-132 /tmp % zig version
0.11.0-dev.1929+4ea2f441d
chris@vpn-39-132 /tmp % zig cc --version
Homebrew clang version 15.0.7
Target: arm64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /usr/bin

jvo203 avatar Mar 12 '23 00:03 jvo203

No luck on Intel macOS either (Monterey 12.6.3):

chris@orion /tmp % zig translate-c histogram.c > histogram.zig
zsh: illegal hardware instruction  zig translate-c histogram.c > histogram.zig
chris@orion /tmp % zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c
zsh: abort      zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c
chris@orion /tmp % zig version
0.11.0-dev.1929+4ea2f441d
chris@orion /tmp % zig cc --version
Homebrew clang version 15.0.7
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /usr/bin
chris@orion /tmp % 

jvo203 avatar Mar 12 '23 00:03 jvo203

For completeness, it works fine on Linux indeed, as reported by @erg .

[chris@fedora tmp]$ zig translate-c histogram.c > histogram.zig
[chris@fedora tmp]$ zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c
[chris@fedora tmp]$ zig version
0.11.0-dev.1929+4ea2f441d
[chris@fedora tmp]$ zig cc --version
clang version 15.0.7 (Fedora 15.0.7-1.fc37)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

jvo203 avatar Mar 12 '23 06:03 jvo203

It seems there has been some progress on this issue. The latest zig master branch with recently-merged LLVM16 support works fine on Intel macOS but not on Apple M1. Apple Silicon macOS is the last remaining platform affected by the aforementioned segmentation fault.

chris@studio ~ % zig version
0.11.0-dev.2470+c22a30ac9

chris@studio ~ % zig cc --version
Homebrew clang version 16.0.1
Target: arm64-apple-darwin22.4.0
Thread model: posix
InstalledDir: /usr/bin

jvo203 avatar Apr 09 '23 02:04 jvo203

@jvo203 could you provide the source for histogram.c file please?

kubkon avatar Jun 22 '23 08:06 kubkon

Certainly, the source file can be downloaded from https://github.com/ziglang/zig/files/10947839/histogram.c.txt . You just need to rename it (remove .txt). GitHub did not allow me to upload a .c file.

jvo203 avatar Jun 22 '23 09:06 jvo203

Closing the issue, thanks to all the incremental accumulated improvements in Zig since first reporting this bug, it finally works fine on Apple Silicon (there are no segmentation faults anymore):

chris@vpn-39-139 /tmp % zig cc -march=native -g -Ofast -fPIC -o histogram.o -c histogram.c

chris@vpn-39-139 /tmp % ls -l histogram*
-rw-r--r--@ 1 chris  wheel  153850  1 19 09:31 histogram.c
-rw-r--r--@ 1 chris  wheel  140328  1 19 09:31 histogram.o

chris@vpn-39-139 /tmp % zig translate-c histogram.c > histogram.zig
chris@vpn-39-139 /tmp % ls -l histogram.zig
-rw-r--r--@ 1 chris  wheel  1112445  1 19 09:33 histogram.zig

Zig version 0.12.0-dev.2278+7d81c952d

jvo203 avatar Jan 19 '24 00:01 jvo203