cosmopolitan icon indicating copy to clipboard operation
cosmopolitan copied to clipboard

This C code results in "crash 0xc0000005 rip 00402867" on Windows

Open Volker-Weissmann opened this issue 2 years ago • 2 comments

This C code results in a program that works on Linux, but crashes on Windows for no reason.

I did this on Arch Linux:

[user@archVM tar]$ cat main.c
#include <stdio.h>
int tar_read(int **archive, const char verbosity) {
  if (-1 < 0) {
    printf("Error: Bad archive\n");
  }
  return 0;
}
int main(int argc, char *argv[]) {
  if (argc < 10) {
    printf("Hello World\n");
    return 0;
  }
  int *archive = NULL;
  tar_read(&archive, 0);
}
[user@archVM tar]$ gcc -g -Os -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \
  -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs -gdwarf-4 \
  -o hello.com.dbg main.c -fuse-ld=bfd -Wl,-T,ape.lds -Wl,--gc-sections \
  -include cosmopolitan.h crt.o ape-no-modify-self.o cosmopolitan.a -I/opt/cosmo -I/opt/cosmo/libc/isystem
In file included from <command-line>:
./cosmopolitan.h:28194: warning: "IFNAMSIZ" redefined
28194 | #define IFNAMSIZ    IF_NAMESIZE
      | 
./cosmopolitan.h:28091: note: this is the location of the previous definition
28091 | #define IFNAMSIZ 16
      | 
/usr/bin/ld.bfd: warning: .note.gnu.build-id section discarded, --build-id ignored
[user@archVM tar]$ objcopy -S -O binary hello.com.dbg hello.com
[user@archVM tar]$ ./hello.com
Hello World
[user@archVM tar]$ 

Then I transfered the hello.com file on a Windows 10 machine and ran:

PS C:\Users\weissmann\Downloads> .\hello.com
crash 0xc0000005 rip 00402867
PS C:\Users\weissmann\Downloads>

Volker-Weissmann avatar Feb 02 '23 17:02 Volker-Weissmann

What was the version of GCC you were using for this? I've had similar issues with newer versions of it: #824

TakuikaNinja avatar Oct 09 '23 22:10 TakuikaNinja

What was the version of GCC you were using for this? I've had similar issues with newer versions of it: #824

I'm not sure, but in February 2023, ArchLinux shipped gcc 12.2, so presumably it was gcc 12.2.

Volker-Weissmann avatar Oct 10 '23 16:10 Volker-Weissmann