I am trying to insert malloc calls in my function pass, yet it keeps complaining about the parameter type:
Call parameter type does not match function signature!
i8* %malloccall = tail call i8* @malloc(i64 4096)
Comparing this to a call generated by simply having a malloc in the C code, I cannot spot significant differences. The following (generated by clang) works just fine:
%2 = call noalias i8* @malloc(i64 4096) #2
Why does one work but the other doesn’t? Below is the code I use to generate the instruction:
Type *int8Type = Type::getInt8Ty(this->currentFunction->getContext());
Type *int64Type = Type::getInt64Ty(this->currentFunction->getContext());
auto allocSize = ConstantInt::get(int64Type, 4096);
Instruction* mallocInstr = CallInst::CreateMalloc(this->currentFunction->getEntryBlock().getFirstNonPHI(),
int8Type->getPointerTo(), int8Type, allocSize,
nullptr, nullptr, “”);
I tried changing up the types already but didn’t find a combination that works yet. Any hints would be appreciated.
Might be missing the noalias attribute on the result?
I have tried adding attributes like noalias as well, but that didn’t help either.
Generated by clang (works): %2 = call noalias i8* @malloc(i64 4096) #2
Generated by my pass (fails): i8* %malloccall = call noalias i8* @malloc(i64 4096)
I had a look at CallInstr::CreateMalloc as well and found out that it uses module->getOrInsertFunction(), which seems a bit odd to me. I have then also tried to get a pointer to the malloc function itself with module->getFunction() and it turns out it couldn’t find it. So that would raise two additional questions:
- Why does CallInstr::CreateMalloc define a new malloc function which seems to be broken if it can’t find the real one?
- Why does it not find the malloc function in the first place if clang can find it?
It looks like you have tried to declare that the size argument integer has type
int8Type->getPointerTo(), while the IntPtr type should be the same as given in the DataLayout
dataLayout.getIntPtrType(Context, 0), which appears to be i64 in your case.