llvm/clang 7.0 crash

Hi,

I’m just trying to create a TargetMachine. I’m using llvm 7.0
The following snippet works as expected with gcc on Ubuntu 16.04 and 18.04 but I have a crash with clang.
Am I doing something wrong here or is it a clang issue?

#include
#include <llvm/ADT/Triple.h>
#include <llvm/CodeGen/CommandFlags.inc>
#include <llvm/Support/CodeGen.h>
#include <llvm/InitializePasses.h>
#include <llvm/LinkAllIR.h>
#include <llvm/LinkAllPasses.h>
#include <llvm/MC/SubtargetFeature.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Target/TargetMachine.h>

using namespace llvm;

// Returns the TargetMachine instance or zero if no triple is provided.
static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
StringRef FeaturesStr,
const TargetOptions &Options) {
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
Error);
// Some modules don’t specify a triple, and this is okay.
if (!TheTarget) {
return nullptr;
}

return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
FeaturesStr, Options, getRelocModel(),
getCodeModel(), CodeGenOpt::Default);
}

//-----------------------------------------------------------------------------
int main(int argc, const char** argv) {
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();

Triple ModuleTriple(sys::getProcessTriple());
std::string CPUStr, FeaturesStr;
TargetMachine* Machine = nullptr;
const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();

if (ModuleTriple.getArch()) {
CPUStr = getCPUStr();
FeaturesStr = getFeaturesStr();
Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
}
std::unique_ptr TM(Machine);
fprintf(stderr, “%s:%d %p\n”, FUNCTION, LINE, TM.get());

return 0;
}

OK : g++ -std=c++11 crash.cpp -ocrash -Illvm-config --includedir -Lllvm-config --libdir llvm-config --libs llvm-config --system-libs CRASH : clang++ -std=c++11 crash.cpp -ocrash -Illvm-config --includedir -Lllvm-config --libdir llvm-config --libs llvm-config --system-libs

Here is the valgrind output in case of binary built with clang:
==3931== Memcheck, a memory error detector
==3931== Copyright (C) 2002-2017, and GNU GPL’d, by Julian Seward et al.
==3931== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3931== Command: ./crash
==3931==
==3931== Use of uninitialised value of size 8
==3931== at 0xF04483: llvm::RegisterTargetMachinellvm::X86TargetMachine::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llv
m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
==3931== by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llvm::CodeGenOpt::Level, bool) const (in /home/christophe
/crash)
==3931== by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
==3931== by 0x4BF492: main (in /home/christophe/crash)
==3931==
==3931== Invalid read of size 1
==3931== at 0xF04483: llvm::RegisterTargetMachinellvm::X86TargetMachine::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llv
m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
==3931== by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llvm::CodeGenOpt::Level, bool) const (in /home/christophe
/crash)
==3931== by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
==3931== by 0x4BF492: main (in /home/christophe/crash)
==3931== Address 0xfefffd04 is not stack’d, malloc’d or (recently) free’d
==3931==
==3931==
==3931== Process terminating with default action of signal 11 (SIGSEGV)
==3931== Access not within mapped region at address 0xFEFFFD04
==3931== at 0xF04483: llvm::RegisterTargetMachinellvm::X86TargetMachine::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llv
m::CodeGenOpt::Level, bool) (in /home/christophe/crash)
==3931== by 0x4D52AF: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optionalllvm::Reloc::Model, llvm::Optionalllvm::CodeModel::Model, llvm::CodeGenOpt::Level, bool) const (in /home/christophe
/crash)
==3931== by 0x4C027C: GetTargetMachine(llvm::Triple, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /home/christophe/crash)
==3931== by 0x4BF492: main (in /home/christophe/crash)
==3931== If you believe this happened as a result of a stack
==3931== overflow in your program’s main thread (unlikely but
==3931== possible), you can try to increase the size of the
==3931== main thread stack using the --main-stacksize= flag.
==3931== The main thread stack size used in this run was 8388608.
==3931==

Best regards,
Christpohe

Christophe,

You’ve run into a problem I and Alastair Murray have run into (thread: https://lists.llvm.org/pipermail/llvm-dev/2018-October/126683.html).

In short, building your project with clang and linking against LLVM libraries built with GCC causes a SEGFAULT because of an ABI incompatibility.

HTH,
Kern

Thanks!