Code crashing in CreateGlobalStringPtr, passes when I add code for main routine + entry

Hi All,

The following crashes in CreateGlobalStringPtr:

#include “llvm/Support/DataTypes.h”
#include “llvm/LLVMContext.h”
#include “llvm/Module.h”
#include “llvm/Constants.h”
#include “llvm/Function.h”
#include “llvm/BasicBlock.h”
#include “llvm/ExecutionEngine/ExecutionEngine.h”
#include “llvm/ExecutionEngine/GenericValue.h”
#include “llvm/Support/IRBuilder.h”

#include
#include
#include

int main()
{
llvm::LLVMContext & context = llvm::getGlobalContext();
llvm::Module *module = new llvm::Module(“asdf”, context);
llvm::IRBuilder<> builder(context);
llvm::Value *helloWorld = builder.CreateGlobalStringPtr(“hello world!\n”); // crash here
}

And this one doesn’t, any idea what’s happening here:

#include “llvm/Support/DataTypes.h”
#include “llvm/LLVMContext.h”
#include “llvm/Module.h”
#include “llvm/Constants.h”
#include “llvm/Function.h”
#include “llvm/BasicBlock.h”
#include “llvm/ExecutionEngine/ExecutionEngine.h”
#include “llvm/ExecutionEngine/GenericValue.h”
#include “llvm/Support/IRBuilder.h”

#include
#include
#include

int main()
{
llvm::LLVMContext & context = llvm::getGlobalContext();
llvm::Module *module = new llvm::Module(“asdf”, context);
llvm::IRBuilder<> builder(context);

// Added extra
llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);
llvm::Function *mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, “main”, module);
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, “entry”, mainFunc);
builder.SetInsertPoint(entry);

llvm::Value *helloWorld = builder.CreateGlobalStringPtr(“hello world!\n”);

}

Not sure how creating a main routine and an entry point can remove the crash. Thought they were unrelated. Here are my options to g++ for compiling:

llvm-config --cxxflags --ldflags --libs
-I/usr/include -DNDEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3 -fno-exceptions -fno-rtti -fno-common -Woverloaded-virtual -Wcast-qual
-L/usr/lib -lpthread -lm
-lLLVMXCoreCodeGen -lLLVMTableGen -lLLVMSystemZCodeGen -lLLVMSparcCodeGen -lLLVMPTXCodeGen -lLLVMPowerPCCodeGen -lLLVMMSP430CodeGen -lLLVMMipsCodeGen -lLLVMMCJIT -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCDisassembler -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMPTXDesc -lLLVMPTXInfo -lLLVMPTXAsmPrinter -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMBlazeDisassembler -lLLVMMBlazeAsmParser -lLLVMMBlazeCodeGen -lLLVMMBlazeDesc -lLLVMMBlazeAsmPrinter -lLLVMMBlazeInfo -lLLVMLinker -lLLVMipo -lLLVMInterpreter -lLLVMInstrumentation -lLLVMJIT -lLLVMExecutionEngine -lLLVMDebugInfo -lLLVMCppBackend -lLLVMCppBackendInfo -lLLVMCellSPUCodeGen -lLLVMCellSPUDesc -lLLVMCellSPUInfo -lLLVMCBackend -lLLVMCBackendInfo -lLLVMBlackfinCodeGen -lLLVMBlackfinDesc -lLLVMBlackfinInfo -lLLVMBitWriter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMX86Info -lLLVMAsmParser -lLLVMARMDisassembler -lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMDesc -lLLVMARMAsmPrinter -lLLVMARMInfo -lLLVMArchive -lLLVMBitReader -lLLVMAlphaCodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMMCParser -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMCore -lLLVMAlphaDesc -lLLVMAlphaInfo -lLLVMMC -lLLVMSupport

Thanks,
Arpan

Arpan Sen wrote:

Hi All,

The following crashes in CreateGlobalStringPtr:
#include "llvm/Support/DataTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/IRBuilder.h"

#include <vector>
#include <cstdio>
#include <string>

int main()
{
   llvm::LLVMContext & context = llvm::getGlobalContext();
   llvm::Module *module = new llvm::Module("asdf", context);
   llvm::IRBuilder<> builder(context);
   llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello
world!\n"); // crash here

The IRBuilder doesn't know what Module you're trying to create the string in, and ends up dereferencing a null pointer.

(Did your build have assertions enabled? I haven't checked whether there's an assert for this, but there ought to be!)

Nick