Issue 91044
Summary LLVM 18 pass DILocation getFilename cast error
Labels new issue
Assignees
Reporter anbu1024
    
llvm 18 getFileName casting error:
```
DILocation *Loc = I.getDebugLoc();
StringRef file_name = Loc->getFilename();
```

I have tried 18.1.1, 18.1.3, and 18.1.5. The error exists.

The error message is as follows:
```
clang-18: /usr/lib/llvm-18/include/llvm/Support/Casting.h:706: auto llvm::cast_if_present(Y*) [with X = llvm::DIFile; Y = llvm::Metadata]: Assertion `isa<X>(Val) && "cast_if_present<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: Path_to_Installs/llvm/18.1.5/install/bin/clang-18 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name hello.cpp -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fdebug-compilation-dir=/media/StoreWWD/Project/js_fuzzers/IncreFuzz/src/test -fcoverage-compilation-dir=/media/StoreWWD/Project/js_fuzzers/IncreFuzz/src/test -resource-dir Path_to_Installs/llvm/18.1.5/install/lib/clang/18 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem Path_to_Installs/llvm/18.1.5/install/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -fpass-plugin=../install/lib/libIncreInstr.so -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/hello-d7827c.o -x c++ hello.cpp
1.      <eof> parser at end of file
2. Optimizer
```

Source files to reproduce the bug:

Mini.cpp
```
#include "llvm/IR/Function.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Pass.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"

#include <llvm/IR/DebugInfo.h>

#include <iostream>

using std::string;

using namespace llvm;

static cl::opt<bool> Wave("wave-goodbye", cl::init(false),
 cl::desc("wave good bye"));

namespace IncreFuzz {

struct Bye : PassInfoMixin<Bye> {

  bool runBye(Module &M);

 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
    if (!runBye(M))
      return PreservedAnalyses::all();
    return PreservedAnalyses::none();
  }

};

bool Bye::runBye(Module &M) {
  errs() << "Run Module!\n";

  if (Wave) {
    errs() << "Bye Module: ";
    errs().write_escaped(M.getName()) << '\n';
 }

  for (auto &F : M) {

    for (auto &BB : F) {

 for (auto &I: BB) {

        DILocation *Loc = I.getDebugLoc();

 if (Loc != nullptr) {
          StringRef file_name = Loc->getFilename();
          StringRef file_dir = Loc->getDirectory();
          unsigned int line = Loc->getLine();
 errs() << "file: " << file_name << ", line: " << line << "\n";

          if (line == commit_line_num && file_name.str() == commit_file_name) {
            commit_bb = I.getParent();
 outs() << "Find the commit basic block.\n";
          }
 }
      }
    }
  }

  return false;
}

} // namespace

/* New PM Registration */
llvm::PassPluginLibraryInfo getByePluginInfo() {
  return {LLVM_PLUGIN_API_VERSION, "Bye", LLVM_VERSION_STRING,
          [](PassBuilder &PB) {
             
 PB.registerOptimizerLastEPCallback(
                /// This extension point allows adding optimizations before the function optimization pipeline.
                [](llvm::ModulePassManager &PM, OptimizationLevel Level) {
 PM.addPass(IncreFuzz::Bye());
                });
                
 PB.registerPipelineParsingCallback(
                /// Using these callbacks, callers can parse both a single pass name, 
 /// as well as entire sub-pipelines, and populate the PassManager instance accordingly. 
                [](StringRef Name, llvm::ModulePassManager &PM,
                   ArrayRef<llvm::PassBuilder::PipelineElement>) {
                  if (Name == "goodbye") {
 PM.addPass(IncreFuzz::Bye());
                    return true;
 }
                  return false;
                });
 }};
}

#ifndef LLVM_BYE_LINK_INTO_TOOLS
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
  return getByePluginInfo();
}
#endif
```

CMakeLists.txt
```
cmake_minimum_required(VERSION 3.19)

project(IncreFuzz C CXX)

set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 20)

set(CMAKE_BUILD_TYPE Debug)

set(CMAKE_PREFIX_PATH "path_to_your_llvm_install")
find_package(LLVM 18 REQUIRED CONFIG)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
include_directories(${LLVM_INCLUDE_DIRS})

set(CMAKE_CXX_FLAGS "-Wall -fno-rtti")

add_library(Mini
    MODULE
 Mini.cpp)
```

Exec command:
```
#!/bin/bash

cxx="path_to_your_clang++"

pass="path_to_your_pass.so"

${cxx} -g -fpass-plugin=${pass} hello.cpp
```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to