Skip to content

[c-api]: segfault querying type information from value #72798

Closed
@johannst

Description

@johannst

Querying function type information from a function value segfaults using the c api.

Turning the LLVMValueRef -> LLVMTypeOf by calling LLVMGetElementType(LLVMTypeOf(val_ref)) segfaults.

// file: test.cpp
#include <llvm-c/Core.h>

int main() {
  LLVMContextRef ctx = LLVMContextCreate();
  LLVMModuleRef  mod = LLVMModuleCreateWithNameInContext("mod", ctx);

  LLVMTypeRef ty_void = LLVMVoidTypeInContext(ctx);
  LLVMTypeRef ty_fn   = LLVMFunctionType(ty_void, 0 /* param types */, 0 /* nargs */, 0 /* vararg */);

  LLVMValueRef func = LLVMAddFunction(mod, "func", ty_fn);

  // (1) Param type.
  unsigned cnt1 = LLVMCountParams(func);  // ok
  // llvm/lib/IR/Core.cpp:LLVMCountParams states
  //    This function is strictly redundant to
  //    LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
  unsigned cnt2 = LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(func))); // segv

  // (2) Return type.
  LLVMTypeRef ty_ret = LLVMGetReturnType(LLVMGetElementType(LLVMTypeOf(func))); // segv

  LLVMContextDispose(ctx);
  return 0;
}
clang -o test test.c `llvm-config --cflags --ldflags --system-libs --libs core`

Tested with the following llvm versions at hand:

  • 14.0.6 no segv
  • 15.0.0 segv
  • 15.0.7 segv
  • 16.0.6 segv

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions