bcc: Fix segmentation fault for LLVM 15
authorHengqi Chen <chenhengqi@outlook.com>
Sun, 24 Apr 2022 12:47:28 +0000 (12:47 +0000)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 22 Feb 2024 14:17:55 +0000 (15:17 +0100)
When BCC is compiled with LLVM 15, running BCC tools will cause segfault.
The core dump reveals that the segfault is triggered by the calls to
`getPointerElementType()`. This is because LLVM is moving towards
`Opaque Pointer` ([0]). This commit follows the migration guide to fix
the issue.

  [0]: https://llvm.org/docs/OpaquePointers.html

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
src/cc/bpf_module_rw_engine.cc

index f1649880cb29a10e637d3a64687bc6a3117b6811..7ee3e111531e6d11ff4c105ffac65ff6a9b1683b 100644 (file)
@@ -49,7 +49,12 @@ void BPFModule::cleanup_rw_engine() {
 
 static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false)
 {
-#if LLVM_MAJOR_VERSION >= 14
+#if LLVM_MAJOR_VERSION >= 15
+  if (isa<AllocaInst>(addr))
+    return B.CreateLoad(dyn_cast<AllocaInst>(addr)->getAllocatedType(), addr, isVolatile);
+  else
+    return B.CreateLoad(addr->getType(), addr, isVolatile);
+#elif LLVM_MAJOR_VERSION >= 14
   return B.CreateLoad(addr->getType()->getPointerElementType(), addr, isVolatile);
 #else
   return B.CreateLoad(addr, isVolatile);
@@ -58,7 +63,12 @@ static LoadInst *createLoad(IRBuilder<> &B, Value *addr, bool isVolatile = false
 
 static Value *createInBoundsGEP(IRBuilder<> &B, Value *ptr, ArrayRef<Value *>idxlist)
 {
-#if LLVM_MAJOR_VERSION >= 14
+#if LLVM_MAJOR_VERSION >= 15
+  if (isa<GlobalValue>(ptr))
+    return B.CreateInBoundsGEP(dyn_cast<GlobalValue>(ptr)->getValueType(), ptr, idxlist);
+  else
+    return B.CreateInBoundsGEP(ptr->getType(), ptr, idxlist);
+#elif LLVM_MAJOR_VERSION >= 14
   return B.CreateInBoundsGEP(ptr->getType()->getScalarType()->getPointerElementType(),
                              ptr, idxlist);
 #else
@@ -116,7 +126,7 @@ static void finish_sscanf(IRBuilder<> &B, vector<Value *> *args, string *fmt,
   B.SetInsertPoint(label_false);
   // s = &s[nread];
   B.CreateStore(
-      createInBoundsGEP(B, createLoad(B, sptr), createLoad(B, nread, true)), sptr);
+      createInBoundsGEP(B, createLoad(B, sptr), {createLoad(B, nread, true)}), sptr);
 
   args->resize(2);
   fmt->clear();
@@ -400,10 +410,12 @@ int BPFModule::annotate() {
     table_names_[table.name] = id++;
     GlobalValue *gvar = mod_->getNamedValue(table.name);
     if (!gvar) continue;
-    if (PointerType *pt = dyn_cast<PointerType>(gvar->getType())) {
 #if LLVM_MAJOR_VERSION >= 15
-      StructType *st = dyn_cast<StructType>(pt->getPointerElementType());
+    {
+      Type *t = gvar->getValueType();
+      StructType *st = dyn_cast<StructType>(t);
 #else
+    if (PointerType *pt = dyn_cast<PointerType>(gvar->getType())) {
       StructType *st = dyn_cast<StructType>(pt->getElementType());
 #endif
       if (st) {