<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Crash in verifyFunction due to bad use of GlobalVariable"
   href="https://bugs.llvm.org/show_bug.cgi?id=45967">45967</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Crash in verifyFunction due to bad use of GlobalVariable
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>rarrum@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This is occurring for me on version 10.0.0 (both the downloaded Release and
locally built Debug).  From glancing at the code corresponding to the backtrace
below it looks like the master branch has this same problem, though I have not
tried to build it to verify for certain.

--

Backtrace for 10.0.0:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff74d47f0 in std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::length() const () from
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff74d47f0 in std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >::length() const () from
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000555555834b5f in llvm::raw_ostream::operator<< (this=0x7fffffffd640, 
    Str=<error reading variable: Cannot access memory at address 0xb8>)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/Support/raw_ostream.h:205
#2  0x0000555555a27b81 in llvm::VerifierSupport::Write (this=0x7fffffffceb0,
M=0x0)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:141
#3  0x0000555555a446fa in llvm::VerifierSupport::WriteTs<llvm::Module*>
(this=0x7fffffffceb0, 
    V1=@0x7fffffffcbe0: 0x0)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:204
#4  0x0000555555a426bf in llvm::VerifierSupport::WriteTs<llvm::GlobalValue*,
llvm::Module*> (
    this=0x7fffffffceb0, V1=@0x7fffffffcb20: 0x555559076ed8)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:205
#5  0x0000555555a3ecfd in llvm::VerifierSupport::WriteTs<llvm::Module const*,
llvm::GlobalValue*, llvm::Module*> (this=0x7fffffffceb0, V1=@0x7fffffffcb30:
0x555559077a20)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:205
#6  0x0000555555a397de in llvm::VerifierSupport::WriteTs<llvm::Instruction*,
llvm::Module const*, llvm::GlobalValue*, llvm::Module*> (this=0x7fffffffceb0,
V1=@0x7fffffffcb28: 0x55555907b0d8)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:205
#7  0x0000555555a2fd88 in
llvm::VerifierSupport::CheckFailed<llvm::Instruction*, llvm::Module const*,
llvm::GlobalValue*, llvm::Module*> (this=0x7fffffffceb0, Message=..., 
    V1=@0x7fffffffcb28: 0x55555907b0d8)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:229
#8  0x0000555555a1c2d3 in (anonymous namespace)::Verifier::visitInstruction (
    this=0x7fffffffceb0, I=...)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:4170
#9  0x0000555555a17ebd in (anonymous namespace)::Verifier::visitLoadInst
(this=0x7fffffffceb0, 
    LI=...) at
/home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:3429
#10 0x0000555555a24b6b in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visitLoad (
    this=0x7fffffffceb0, I=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/Instruction.def:172
#11 0x0000555555a23e40 in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visit (
    this=0x7fffffffceb0, I=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/Instruction.def:172
#12 0x0000555555a05680 in (anonymous namespace)::Verifier::visit
(this=0x7fffffffceb0, I=...)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:550
#13 0x0000555555a25669 in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visit<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction,
true, false, void>, false, false> > (this=0x7fffffffceb0, Start=..., End=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/InstVisitor.h:91
#14 0x0000555555a2529a in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visit (
    this=0x7fffffffceb0, BB=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/InstVisitor.h:106
#15 0x0000555555a24695 in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visit<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::BasicBlock,
true, false, void>, false, false> > (this=0x7fffffffceb0, Start=..., End=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/InstVisitor.h:91
#16 0x0000555555a23ae2 in llvm::InstVisitor<(anonymous namespace)::Verifier,
void>::visit (
    this=0x7fffffffceb0, F=...)
    at
/home/luke/code/llvmgit/llvm-project/llvm/include/llvm/IR/InstVisitor.h:102
#17 0x0000555555a05146 in (anonymous namespace)::Verifier::verify
(this=0x7fffffffceb0, F=...)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:353
#18 0x0000555555a21c05 in llvm::verifyFunction (f=..., OS=0x7fffffffd640)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:5103
#19 0x0000555555806384 in (anonymous
namespace)::CompilerData::AddGlobalFunctionBody (
    this=0x7fffffffd8b0, fi=..., generalSourceLocation=...)
    at /home/luke/code/cbreak/CBreakCompiler/src/IRCompiler.cpp:270
#20 0x0000555555807f81 in CBreakCompiler::GenerateIR (
    parsedFragments=std::vector of length 1, capacity 1 = {...})
    at /home/luke/code/cbreak/CBreakCompiler/src/IRCompiler.cpp:455
#21 0x00005555557eaf60 in main (argc=6, argv=0x7fffffffdee8)
    at /home/luke/code/cbreak/CBreakCompiler/src/main.cpp:137

--

The problem starts with Verifier.cpp - Verifier::visitInstruction (line 4170 in
10.0.0):

Assert(GV->getParent() == &M, "Referencing global in another module!", &I,
    &M, GV, GV->getParent());

(gdb) frame 8
#8  0x0000555555a1c2d3 in (anonymous namespace)::Verifier::visitInstruction (
    this=0x7fffffffceb0, I=...)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:4170
4170          Assert(GV->getParent() == &M, "Referencing global in another
module!", &I,
(gdb) print &M
$1 = (const llvm::Module *) 0x555559077a20
(gdb) print GV
$2 = (llvm::GlobalValue *) 0x555559076ed8
(gdb) print GV->getParent()
$3 = (llvm::Module *) 0x0

Note that GV->getParent() which is nullptr is passed on as a parameter to stuff
which eventually calls VerifierSupport::WriteTs which eventually calls
VerifierSupport::Write.

(gdb) frame 2
#2  0x0000555555a27b81 in llvm::VerifierSupport::Write (this=0x7fffffffceb0,
M=0x0)
    at /home/luke/code/llvmgit/llvm-project/llvm/lib/IR/Verifier.cpp:141
141         *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
(gdb) print M
$6 = (const llvm::Module *) 0x0

M->getModuleIdentifier() is a nullptr dereference.

--

Some context on how I made this happen: I'm working on adding global variable
support to my compiler, and I suspect I've done something wrong with how I
create the GlobalVariable object.  Summary of what my code should be doing
here..

During setup, after creating LLVMContext and such:
  GlobalsBlock = llvm::BasicBlock::Create(LLVMContext, "globals");

During an early part of the code where I'm setting up globals:
  IRBuilder.SetInsertPoint(GlobalsBlock);
  std::make_unique<llvm::GlobalVariable>(type->IRType, false,
llvm::GlobalValue::InternalLinkage, nullptr, vi.Name);

During a later part of the code where I'm filling in function bodies:
  llvm::BasicBlock *bbFunction = llvm::BasicBlock::Create(LLVMContext,
"function " + fi.Name, fd.IRFunction);
  IRBuilder.SetInsertPoint(bbFunction);
During that while evaluating an expression:
  IRBuilder.CreateLoad(gvar->second.IRGlobalVariable.get());

It's that last call to CreateLoad that appears to be triggering the crash in
the verifier.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>