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

crash when using zig cc to compile #18147

Open
zenspider opened this issue Nov 27, 2023 · 4 comments
Open

crash when using zig cc to compile #18147

zenspider opened this issue Nov 27, 2023 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@zenspider
Copy link

zenspider commented Nov 27, 2023

Zig Version

0.12.0-dev.105+f1992a39a

Steps to Reproduce and Observed Behavior

given:

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
  struct sockaddr_in sap;
  struct hostent* hp = gethostbyname("www.google.com");
  sap.sin_addr = *(struct in_addr *)hp->h_addr;         // BUG IS HERE
  printf("IP Address: %s\n", inet_ntoa(sap.sin_addr));

  exit(0);
}

compiling and running with zig cc crashes:

7985 % zig version; zig cc goog.c && ./a.out
0.12.0-dev.105+f1992a39a
Trace/BPT trap: 5

running with lldb points at the commented line above

Expected Behavior

same as llvm's cc:

7987 % cc --version ; cc goog.c && ./a.out
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
IP Address: 142.251.215.228
@zenspider zenspider added the bug Observed behavior contradicts documented or intended behavior label Nov 27, 2023
@zenspider
Copy link
Author

just force updated and confirmed the bug still exists:

7990 % zig version ; zig cc goog.c && ./a.out
0.12.0-dev.1746+19af8aac8
Trace/BPT trap: 5

@tenderlove
Copy link

I'm not sure how helpful this is, but I disassembled the program and it seems zig cc is emitting a brk instruction (and I guess a useless jump right before it?):

(lldb) disassemble --start-address `0x10000071c - 32` --count 15
a.out`main:
    0x1000006fc <+292>: cset   w8, eq
    0x100000700 <+296>: tbnz   w8, #0x0, 0x10000071c     ; <+324> at goog.c:11:37
    0x100000704 <+300>: b      0x100000708               ; <+304> at goog.c
    0x100000708 <+304>: ldr    x8, [sp, #0x10]
    0x10000070c <+308>: ands   x8, x8, #0x7
    0x100000710 <+312>: cset   w8, eq
    0x100000714 <+316>: tbnz   w8, #0x0, 0x100000720     ; <+328> at goog.c
    0x100000718 <+320>: b      0x10000071c               ; <+324> at goog.c:11:37
->  0x10000071c <+324>: brk    #0x5516
    0x100000720 <+328>: ldr    x8, [sp, #0x18]
    0x100000724 <+332>: ldr    x8, [x8]
    0x100000728 <+336>: ldr    w8, [x8]
    0x10000072c <+340>: stur   w8, [x29, #-0x1c]
    0x100000730 <+344>: ldur   w8, [x29, #-0x1c]
    0x100000734 <+348>: mov    x0, x8

I don't know the compiler internals, but this definitely seems like a compiler bug 😅

@ehaas
Copy link
Contributor

ehaas commented Nov 28, 2023

That's -fsanitize=undefined tripping (zig cc enables it by default; clang does not). Casting a pointer to one with a larger alignment is undefined behavior in C.

Edit: it looks like the implementation itself might be broken? https://developer.apple.com/forums/thread/116121 - in general it's not recommended to use gethostbyname anyway, it's a legacy function that doesn't support ipv6

@zenspider
Copy link
Author

zenspider commented Nov 28, 2023

Workaround: add -fno-sanitize=undefined:

8108 % zig cc -fno-sanitize=undefined goog.c ; ./a.out
IP Address: 142.251.33.68

ETA:

__attribute__((no_sanitize("undefined")))
int main(int argc, char **argv) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants