<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56224>56224</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            SEGV caused by READ Memory Access when creating `llvm::Function`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          AldrinMathew
      </td>
    </tr>
</table>

<pre>
    The following is the original error
```
Initialising context
Context initialised
Getting module name
Creating module
Created module
Got void type
Got function type
Creating function
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==65054==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x00003c018220 (pc 0x7f6b73ffa0f5 bp 0x000007933074 sp 0x7fff08d5fe40 T65054)
==65054==The signal is caused by a READ memory access.
    #0 0x7f6b73ffa0f5 in llvm::StringMapImpl::LookupBucketFor(llvm::StringRef) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xde60f5) (BuildId: 52065316c710ac1be836ac50bf4d5004db3205e5)
    #1 0x7f6b741f32c1 in llvm::ValueSymbolTable::reinsertValue(llvm::Value*) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xfdf2c1) (BuildId: 52065316c710ac1be836ac50bf4d5004db3205e5)
    #2 0x7f6b741579fe in llvm::Function::Function(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, unsigned int, llvm::Twine const&, llvm::Module*) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xf439fe) (BuildId: 52065316c710ac1be836ac50bf4d5004db3205e5)
    #3 0x55e8f55ac6b1 in llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) /usr/include/llvm/IR/Function.h:147:16
    #4 0x55e8f55ac31e in main /mnt/Main/DEV/qatlang/qat/src/qat.cpp:25:3
    #5 0x7f6b72a86082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
    #6 0x55e8f555a14d in _start (/mnt/Main/DEV/qatlang/qat/build/qat+0x2e14d) (BuildId: d1417a87f8a16567)

UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xde60f5) (BuildId: 52065316c710ac1be836ac50bf4d5004db3205e5) in llvm::StringMapImpl::LookupBucketFor(llvm::StringRef)
==65054==ABORTING
```

And the code that causes the error is as follows
```cpp
int main(int count, const char **args) {
  using qat::CLI::Config;

  SHOW("Initialising context")
  llvm::LLVMContext ctx{};
  SHOW("Context initialised")
  SHOW("Getting module name")
  llvm::StringRef mdname("other");
  SHOW("Creating module")
  auto mod = new llvm::Module{mdname, ctx};
  SHOW("Created module")
  auto voidTy = llvm::Type::getVoidTy(ctx);
  SHOW("Got void type")
  auto fnTy = llvm::FunctionType::get(voidTy, false);
  SHOW("Got function type")
  llvm::StringRef name("hello");
  SHOW("Creating function")
  llvm::Function::Create(
      fnTy, llvm::GlobalValue::LinkageTypes::ExternalWeakLinkage, name, mod);
  SHOW("Created function")

  return 0;

  auto cli = Config::init(count, args);
  if (cli->should_exit()) {
    return 0;
  }
  auto sitter = qat::QatSitter();
  sitter.init();
  Config::destroy();
  return 0;
}
```

Seeing `StringMapImpl`, I initially thought that the error is related to StringRef. I separated function calls to individual statements and added debug prints to see the flow of execution upto the error. Turns out, it is caused by by `llvm::Function::Create` when it is trying to see if the `llvm::Value` exists already in the bucket.

Later made a few changes to the code and added a step for setting Target triple of the module:
```cpp
int main(int count, const char **args) {
  using qat::CLI::Config;

  SHOW("Initialising context")
  llvm::LLVMContext ctx;
  SHOW("Context initialised")
  SHOW("Getting module name")
  llvm::StringRef mdname("other");
  SHOW("Creating module")
  auto mod = new llvm::Module{mdname, ctx};
  SHOW("Module created")
  SHOW("Setting target triple")
  mod->setTargetTriple(LLVM_HOST_TRIPLE);
  SHOW("Target triple set")
  auto voidTy = llvm::Type::getVoidTy(ctx);
  SHOW("Got void type")
  auto fnTy = llvm::FunctionType::get(voidTy, false);
  SHOW("Got function type")
  llvm::StringRef name("hello");
  SHOW("Creating function")
  llvm::Function::Create(
      fnTy, llvm::GlobalValue::LinkageTypes::ExternalWeakLinkage, name, mod);
  SHOW("Created function")

  return 0;

  auto cli = Config::init(count, args);
  if (cli->should_exit()) {
    return 0;
  }
  if (cli->is_compile()) {
    for (auto path : cli->get_paths()) {
      std::cout << path.string() << "\n";
    }
  }
  auto sitter = qat::QatSitter();
  sitter.init();
  Config::destroy();
  return 0;
}
```
And guess what, the error happens at a different spot
```
Initialising context
Context initialised
Getting module name
Creating module
Module created
Setting target triple
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:3390:18: runtime error: applying non-zero offset 18446744073709551592 to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:3390:18 in 
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==64806==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0xfffffffffffffff8 (pc 0x55e858bb4b07 bp 0x7fff5ddcbda0 sp 0x7fff5ddcbd80 T64806)
==64806==The signal is caused by a READ memory access.
    #0 0x55e858bb4b07 in std::string::_Rep::_M_is_leaked() const /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:3258:18
    #1 0x55e858bb46a5 in std::string::swap(std::string&) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.tcc:962:21
    #2 0x55e858bb3fc9 in std::string::operator=(std::string&&) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:3798:8
    #3 0x55e858bb36ff in llvm::Module::setTargetTriple(llvm::StringRef) /usr/include/llvm/IR/Module.h:300:52
    #4 0x55e858bb339e in main /mnt/Main/DEV/qatlang/qat/src/qat.cpp:20:8
    #5 0x7fc81bf7a082 in __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
    #6 0x55e858b6127d in _start (/mnt/Main/DEV/qatlang/qat/build/qat+0x3027d) (BuildId: 409a2d3ccd60c9ff)

UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/basic_string.h:3258:18 in std::string::_Rep::_M_is_leaked() const
==64806==ABORTING
```
I still think this might be caused by the `llvm::StringRef` and how I am initialising the value.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWVlv4zgS_jXOCxGDuuUHPziHMwaS6V3bk8E8GZRE2dyWRa1E5ehfP1WkZEs-0j3dwWZm0YYgSzyKdfGrYimSyet4ueEklVkmn0W-JqIiChpkKdYiZxnhZSnLAb0Z0MnAp82lX2e5UIJlosJpscwVf1Gm59q8ENGO4InpuONK4eitTOqMk5xteTOj5KzT02nkSa_tTiryJEVC1GvRaUrrPFZC5p3mHcm2zzT_lic8FTlPrviGPQlZLhhy-YWXA2dyczu5uf9jMbv7dXLfyOzcwOV71HPN4-18_mkOQ8mbhMji9u6RAD91_jmXzzlhSVLyqiL0hcLPiakV2jYlAzssYmgMUj8KnDRlNPVIVDTDaDByHBq4pCr0mDSlYeKl3KVkaViyR2fYRKNWYo0WBIvGrAYbkOiVMDIHGcmWb2UJb3EMTA0NDQK_ge3QQ3ZETrLsaQtSwbVQJej0gRWzbZGZpnspP9fFVR1_5moKvmKHh8PnPAVOUdiBPc1EBPeX0F_57mUm8vrlcp3XpuP-_vHh0nKHlRxaA_uKviTcBxaayVe1yJJZgur1bOp7juXHgUVZbEU8dHwWezRK3cSj1E0ix6Ye93YKaoSzWuFcK3Xs2OoL98iymi9et5HMliwCl9OtJRd5xUule3vSNS2T7xQuTVJg4f2Es_fCecEo5X3hpu026L915W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