llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.65k stars 11.37k forks source link

[clang][asan] Segmentation fault when compiling with `-fsanitize=address` #87754

Open Dylan-Brotherston opened 4 months ago

Dylan-Brotherston commented 4 months ago

The following code yield a valid program, and when compiled with clang, and gcc works as expected.

#include <errno.h>
#include <stdio.h>

int main(void) {
    fopen("/doesntexist", "r");
    printf("%d\n", errno);
}
$ clang test.c -o test
$ ./test
2

$ gcc test.c -o test
$ ./test
2

but when compiled with -fsanitize=address this code will randomly start giving Segmentation fault (core dumped) errors.

$ clang -fsanitize=address test.c -o test
$ ./test
2
$ ./test
2
$ ./test
2
$ ./test
Segmentation fault (core dumped)
$ ./test
2

Note the same is true for gcc -fsanitize=address

$ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 23.10
Release:    23.10
Codename:   mantic

$ clang --version
Ubuntu clang version 16.0.6 (15)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /bin
ycc-development commented 2 months ago

I'm having exactly the same problem, compiling with gcc and g++ in the following versions:

root@docker[x86_64-linux]:/gbsys-fw# gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@docker[x86_64-linux]:/gbsys-fw# g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

When compiling, passing the flags:

set(CMAKE_C_COMPILER "/usr/bin/x86_64-linux-gnu-gcc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -fsanitize=address -static-libasan -pthread" CACHE STRING "c flags")

set(CMAKE_CXX_COMPILER "/usr/bin/x86_64-linux-gnu-g++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -fsanitize=address -static-libasan -pthread" CACHE STRING "c++ flags")

And the execution of the application, which only executes a print with "Hello world!" returns segmentation fault, as shown below:

ycc@nitro:~/gbsys-fw$ ./output/x86_64-linux/bin/example-app
Hello, World!
ycc@nitro:~/gbsys-fw$ ./output/x86_64-linux/bin/example-app
Hello, World!
ycc@nitro:~/gbsys-fw$ ./output/x86_64-linux/bin/example-app
Segmentation fault (core dumped)
ycc@nitro:~/gbsys-fw$ ./output/x86_64-linux/bin/example-app
Hello, World!

If I remove the flag -fsanitize=address -static-libasan the problem never occurs again.

AdvenamTacet commented 2 months ago

I cannot reproduce the error with clang 17.0.6 as well as git-head.

Could you test with a never version?