llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Verifier doesn't flag out-of-module constants, BC writing crashes

Open llvmbot opened this issue 16 years ago • 5 comments

Bugzilla Link 8364
Resolution INVALID
Resolved on May 01, 2020 02:13
Version 2.7
OS All
Reporter LLVM Bugzilla Contributor
CC @ekatz

Extended Description

BitCode generation fails with "Unknown constant!" "UNREACHABLE executed at BitcodeWriter.cpp:895

Issue can be reproduced with the following: llvm::LLVMContext ctx; llvm::StringRef mn1 ("m1"); llvm::StringRef mn2 ("m2"); llvm::Module m1 (mn1, ctx); llvm::Module m2 (mn2, ctx); const llvm::Type * rt = llvm::Type::getVoidTy (ctx); const llvm::FunctionType * ft = llvm::FunctionType::get (rt, false); llvm::Function * f1 = llvm::Function::Create (ft, llvm::GlobalValue::ExternalLinkage); m1.getFunctionList ().push_back (f1); const llvm::IntegerType * it = llvm::IntegerType::get (ctx, 8); const llvm::PointerType * pt = llvm::PointerType::get (it, 0); const llvm::Constant * ce = llvm::ConstantExpr::getBitCast (f1, pt); llvm::Constant * v [1] = {(llvm::Constant *)ce}; const llvm::ArrayType * at = llvm::ArrayType::get (pt, 1); const llvm::Constant * a = llvm::ConstantArray::get (at, v, 1); const llvm::Type * gt = a->getRawType (); llvm::GlobalVariable * g = new llvm::GlobalVariable (gt, true, llvm::GlobalValue::ExternalLinkage, (llvm::Constant *)a); m2.getGlobalList ().push_back (g); std::string message; bool result = llvm::verifyModule (m2, llvm::ReturnStatusAction, &message);

llvmbot avatar Oct 12 '10 20:10 llvmbot

The testcase is incorrect. It creates a function in module "m1", while referring it in module "m2", via a constant bitcast. The correct code should declare the same function as external in module "m2", and then reference it in the bitcast (keep the define of the function in "m1").

  • Note that the function needs to be uniquely named for it to work.

Closing this bug, as this is not an issue.

ekatz avatar May 01 '20 09:05 ekatz

I don't see how this can be considered resolved, since the verifier hasn't been beefed up as requested.

llvmbot avatar Oct 15 '10 15:10 llvmbot

The best course of action for you to take is to create C code that's similar to what you want done and then generate a bitcode file from that. You can then disassemble that bitcode file to see the LLVM IR and then try to replicate that.

llvmbot avatar Oct 15 '10 06:10 llvmbot

Does that make this example invalid? Whether I use get-or-insert for the functions or globals won't change that the validator doesn't flag a constant in one module referencing a function in a different module and that code generation aborts in an UNREACHABLE branch.

llvmbot avatar Oct 15 '10 03:10 llvmbot

You probably want to use the Module::getOrInsertFunction and Module::getOrInsertGlobal methods instead of manually putting them into the Module's lists.

llvmbot avatar Oct 15 '10 03:10 llvmbot