[cfe-users] clang-9 (MacOS 10.13) linker crash and optimization issues

Artur Shepilko via cfe-users cfe-users at lists.llvm.org
Wed Mar 25 18:44:39 PDT 2020


Hello,
First post on this mailing list...to report a crash-bug in clang-9.0.0
(x86_64-apple-darwin17.0.0) on MacOS 10.13 (High Sierra). The clang-9
was installed from the pre-built binary on llvm.org (not XCode).

There're two related issues uncovered:

1) clang-9 ld64.lld crashed when trying to link a default compiled
sample .c file, which has some switch statement cases. Hello-world
kind of .c test file is linking and running ok. The same crashing link
is successful when using clang-8 ld64.lld instead.

2) Executable of the sample file (same as above) produced with clang-9
(or clang-8, or even clang-7) results in "Segmentation Fault: 11",
when built with default compiler flags (apart from MacOS SDK
includes). The code appears to build and run ok with -O1 or -O2 or -O3
flags (-O0 yields SEGV in run-time). However, when bulding any real
application beyond the sample still results in some SEGV when trying
to run the built binaries at point or the other.

It took me some painful debugging to figure out at least one remedy
which appears to clear both of the issues listed above. Adding
"-fno-jump-tables"  compiler/optimizer option seems to result in
correct run-time behavior (for all of the clang versions tested:
7,8,9). This seems applicable to all optimization levels -O0 to -O3.

I wonder if at least the "-fno-jump-tables" option should be made part
of defaults in MacOS build, or maybe add some line in docs to alert
anyone that faces these issues.

Below is the sample code that triggered the linker crash and SEGV in
runtime (in the pre-built clang versions 7 through 9).  The resulting
run-time SEGV output (without -fno-jump-tables option) is shown after
that. Finally, the clang-9 linker crash dump is shown at the very end.

I also attached these outputs, just in case it gets wrapped on the mail-list.

I hope this could get fixed or maybe there're some other details about
these issues that I don't know.
I also tried the bugpoint way, but it seems to be quite challenging to
figure out given a rather terse documentation on how to use it.
Instead I had to resort to eyeballing the resulting asm files to see
the difference in switch handling between -O0 (crashing) and -O1
(somewhat ok) codes.

Thanks.

[swsegv.c]
----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

void test_sw ();

enum tag {
  tag1 = 0,
  tag2,
  tag3,
  tag4,
};

struct data {
  int c;
  enum tag t;
};

int main (int argc, char **argv, char **envp)
{
  test_sw();
  printf("DBG:%s|done\n",__FUNCTION__);
  exit(EXIT_SUCCESS);
}

static void dump_data(const struct data *d)
{
  printf("DBG:%s|data::{\n", __FUNCTION__);
  for ( ; d && d->c != '\0'; ++d) {
    const char *t = "";

    switch (d->t) {
    case tag1:
        t = "tag1";
        break;
    case tag2:
        t = "tag2";
        break;
    case tag3:
        t = "tag3";
        break;
    case tag4:
        t = "tag4";
        break;
    default:
        break;
    }
    printf("  {'%c',%s},\n",d->c, t);
  }
  printf("}::data\n");
}

void test_sw ()
{
  struct data data[] = {
    { '1', tag1 },
    { '2', tag2 },
    { '3', tag3 },
    { '4', tag4 },
    { 0, tag1 }
  };

  struct data *d;
  d = data;
  dump_data(d);

  return;
}
----------------------------------------------------

[swsegv.c: ./swsegv run-time output without -fno-jump-tables]
----------------------------------------------------
DBG:dump_data|data::{
DBG:dump_data|data::{
Segmentation fault: 11
----------------------------------------------------

[swsegv.c: clang-9 link crash dump when building without -fno-jump-tables]
----------------------------------------------------
$CC -x c -v $CFLAGS swsegv.c $LDFLAGS
clang version 9.0.0 (git://github.com/llvm/llvm-project.git
0399d5a9682b3cef71c653373e38890c63c4c365)
Target: x86_64-apple-darwin17.0.0
Thread model: posix
InstalledDir: /usr/local/opt/clang+llvm-9.0.0/bin
 "/usr/local/opt/clang+llvm-9.0.0/bin/clang-9" -cc1 -triple
x86_64-apple-macosx10.13.0 -Wdeprecated-objc-isa-usage
-Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free
-disable-llvm-verifier -discard-value-names -main-file-name swsegv.c
-mrelocation-model pic -pic-level 2 -mthread-model posix
-mdisable-fp-elim -masm-verbose -munwind-tables
-faligned-alloc-unavailable -target-cpu penryn -dwarf-column-info
-debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb
-ggnu-pubnames -target-linker-version 512.4 -v -resource-dir
/usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0 -I
/usr/local/opt/clang+llvm-9.0.0/include -I
/usr/local/opt/clang+llvm-9.0.0/include/c++/v1 -I
/usr/local/opt/SDKs/MacOSX10.13.sdk/usr/include -D
__STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS
-F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
-internal-isystem /usr/local/include -internal-isystem
/usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/include
-internal-externc-isystem /usr/include -Wall
-Wno-nullability-completeness -fdebug-compilation-dir
/Users/tester/work -ferror-limit 19 -fmessage-length 111
-stack-protector 1 -fblocks -fencode-extended-block-signature
-fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.13.0
-fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o
/var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -x c
swsegv.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target
x86_64-apple-darwin17.0.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/opt/clang+llvm-9.0.0/include
 /usr/local/opt/clang+llvm-9.0.0/include/c++/v1
 /usr/local/opt/SDKs/MacOSX10.13.sdk/usr/include
 /usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
(framework directory)
 /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/local/opt/clang+llvm-9.0.0/bin/ld64.lld" -demangle -lto_library
/usr/local/opt/clang+llvm-9.0.0/lib/libLTO.dylib -no_deduplicate
-dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out
-L/usr/local/opt/clang+llvm-9.0.0/lib
-L/usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib
/var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -v
-sdk_version 10.13.0 -rpath /usr/local/opt/clang+llvm-9.0.0/lib
-lSystem /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
-F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
ld64.lld: warning: ignoring unknown argument: -no_deduplicate
Library search paths:
    /usr/local/opt/clang+llvm-9.0.0/lib
    /usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib
    /usr/lib
    /usr/local/lib
Framework search paths:
    /usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
    /Library/Frameworks
    /System/Library/Frameworks
Stack dump:
0.    Program arguments: /usr/local/opt/clang+llvm-9.0.0/bin/ld64.lld
-demangle -lto_library
/usr/local/opt/clang+llvm-9.0.0/lib/libLTO.dylib -no_deduplicate
-dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out
-L/usr/local/opt/clang+llvm-9.0.0/lib
-L/usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib
/var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -v
-sdk_version 10.13.0 -rpath /usr/local/opt/clang+llvm-9.0.0/lib
-lSystem /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
-F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
0  ld64.lld                 0x000000010fd78468
llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  ld64.lld                 0x000000010fd77488
llvm::sys::RunSignalHandlers() + 248
2  ld64.lld                 0x000000010fd78ae9 SignalHandler(int) + 281
3  libsystem_platform.dylib 0x00007fff7f105f5a _sigtramp + 26
4  libxml2.2.dylib          0x00007ffff0f22f98 xmlXPathNAN + 953925304
5  ld64.lld                 0x0000000110010b5d
lld::mach_o::ArchHandler_x86_64::generateAtomContent(lld::DefinedAtom
const&, bool, std::__1::function<unsigned long long (lld::Atom
const&)>, std::__1::function<unsigned long long (lld::Atom const&)>,
unsigned long long, llvm::MutableArrayRef<unsigned char>) + 637
6  ld64.lld                 0x0000000110031a61
lld::mach_o::normalized::normalizedFromAtoms(lld::File const&,
lld::MachOLinkingContext const&) + 14801
7  ld64.lld                 0x0000000110056ffe
lld::mach_o::MachOWriter::writeFile(lld::File const&, llvm::StringRef)
+ 62
8  ld64.lld                 0x0000000110064d33
lld::LinkingContext::writeFile(lld::File const&) const + 51
9  ld64.lld                 0x000000010fe1b297
lld::mach_o::link(llvm::ArrayRef<char const*>, bool,
llvm::raw_ostream&) + 1527
10 ld64.lld                 0x000000010fcc341b main + 859
11 libdyld.dylib            0x00007fff7ee85145 start + 1
12 libdyld.dylib            0x0000000000000017 start + 18446603338387009235
clang-9: error: unable to execute command: Segmentation fault: 11
clang-9: error: linker command failed due to signal (use -v to see invocation)
----------------------------------------------------
-------------- next part --------------
ISSUE:crash-bug in clang-9.0.0 (x86_64-apple-darwin17.0.0) on MacOS 10.13
WORKAROUND: add "-fno-jump-tables" clang option.


[swsegv.c]
--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

void test_sw ();

enum tag {
  tag1 = 0,
  tag2,
  tag3,
  tag4,
};

struct data {
  int c;
  enum tag t;
};

int main (int argc, char **argv, char **envp)
{
  test_sw();
  printf("DBG:%s|done\n",__FUNCTION__);
  exit(EXIT_SUCCESS);
}

static void dump_data(const struct data *d)
{
  printf("DBG:%s|data::{\n", __FUNCTION__);
  for ( ; d && d->c != '\0'; ++d) {
    const char *t = "";

    switch (d->t) {
    case tag1:
        t = "tag1";
        break;
    case tag2:
        t = "tag2";
        break;
    case tag3:
        t = "tag3";
        break;
    case tag4:
        t = "tag4";
        break;
    default:
        break;
    }
    printf("  {'%c',%s},\n",d->c, t);
  }
  printf("}::data\n");
}

void test_sw ()
{
  struct data data[] = {
    { '1', tag1 },
    { '2', tag2 },
    { '3', tag3 },
    { '4', tag4 },
    { 0, tag1 }
  };

  struct data *d;
  d = data;
  dump_data(d);

  return;
}
--------------------------------------------------------------------------------


[swsegv.c: ./swsegv run-time output without -fno-jump-tables]
--------------------------------------------------------------------------------
DBG:dump_data|data::{
DBG:dump_data|data::{
Segmentation fault: 11
--------------------------------------------------------------------------------


[swsegv.c: clang-9 link crash dump when building without -fno-jump-tables]
--------------------------------------------------------------------------------
$CC -x c -v $CFLAGS swsegv.c $LDFLAGS
clang version 9.0.0 (git://github.com/llvm/llvm-project.git 0399d5a9682b3cef71c653373e38890c63c4c365)
Target: x86_64-apple-darwin17.0.0
Thread model: posix
InstalledDir: /usr/local/opt/clang+llvm-9.0.0/bin
 "/usr/local/opt/clang+llvm-9.0.0/bin/clang-9" -cc1 -triple x86_64-apple-macosx10.13.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name swsegv.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -faligned-alloc-unavailable -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 512.4 -v -resource-dir /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0 -I /usr/local/opt/clang+llvm-9.0.0/include -I /usr/local/opt/clang+llvm-9.0.0/include/c++/v1 -I /usr/local/opt/SDKs/MacOSX10.13.sdk/usr/include -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks -internal-isystem /usr/local/include -internal-isystem /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/include -internal-externc-isystem /usr/include -Wall -Wno-nullability-completeness -fdebug-compilation-dir /Users/tester/work -ferror-limit 19 -fmessage-length 111 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.13.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -x c swsegv.c
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-apple-darwin17.0.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/opt/clang+llvm-9.0.0/include
 /usr/local/opt/clang+llvm-9.0.0/include/c++/v1
 /usr/local/opt/SDKs/MacOSX10.13.sdk/usr/include
 /usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks (framework directory)
 /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/local/opt/clang+llvm-9.0.0/bin/ld64.lld" -demangle -lto_library /usr/local/opt/clang+llvm-9.0.0/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out -L/usr/local/opt/clang+llvm-9.0.0/lib -L/usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib /var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -v -sdk_version 10.13.0 -rpath /usr/local/opt/clang+llvm-9.0.0/lib -lSystem /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a -F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
ld64.lld: warning: ignoring unknown argument: -no_deduplicate
Library search paths:
    /usr/local/opt/clang+llvm-9.0.0/lib
    /usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib
    /usr/lib
    /usr/local/lib
Framework search paths:
    /usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
    /Library/Frameworks
    /System/Library/Frameworks
Stack dump:
0.    Program arguments: /usr/local/opt/clang+llvm-9.0.0/bin/ld64.lld -demangle -lto_library /usr/local/opt/clang+llvm-9.0.0/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.13.0 -o a.out -L/usr/local/opt/clang+llvm-9.0.0/lib -L/usr/local/opt/SDKs/MacOSX10.13.sdk/usr/lib /var/folders/nc/3vskx63d685ddh17hg_rkrkm0000gn/T/swsegv-e930c3.o -v -sdk_version 10.13.0 -rpath /usr/local/opt/clang+llvm-9.0.0/lib -lSystem /usr/local/opt/clang+llvm-9.0.0/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a -F/usr/local/opt/SDKs/MacOSX10.13.sdk/System/Library/Frameworks
0  ld64.lld                 0x000000010fd78468 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  ld64.lld                 0x000000010fd77488 llvm::sys::RunSignalHandlers() + 248
2  ld64.lld                 0x000000010fd78ae9 SignalHandler(int) + 281
3  libsystem_platform.dylib 0x00007fff7f105f5a _sigtramp + 26
4  libxml2.2.dylib          0x00007ffff0f22f98 xmlXPathNAN + 953925304
5  ld64.lld                 0x0000000110010b5d lld::mach_o::ArchHandler_x86_64::generateAtomContent(lld::DefinedAtom const&, bool, std::__1::function<unsigned long long (lld::Atom const&)>, std::__1::function<unsigned long long (lld::Atom const&)>, unsigned long long, llvm::MutableArrayRef<unsigned char>) + 637
6  ld64.lld                 0x0000000110031a61 lld::mach_o::normalized::normalizedFromAtoms(lld::File const&, lld::MachOLinkingContext const&) + 14801
7  ld64.lld                 0x0000000110056ffe lld::mach_o::MachOWriter::writeFile(lld::File const&, llvm::StringRef) + 62
8  ld64.lld                 0x0000000110064d33 lld::LinkingContext::writeFile(lld::File const&) const + 51
9  ld64.lld                 0x000000010fe1b297 lld::mach_o::link(llvm::ArrayRef<char const*>, bool, llvm::raw_ostream&) + 1527
10 ld64.lld                 0x000000010fcc341b main + 859
11 libdyld.dylib            0x00007fff7ee85145 start + 1
12 libdyld.dylib            0x0000000000000017 start + 18446603338387009235
clang-9: error: unable to execute command: Segmentation fault: 11
clang-9: error: linker command failed due to signal (use -v to see invocation)
--------------------------------------------------------------------------------



More information about the cfe-users mailing list