};
auto MallocCallocCheck = [&](Instruction &I) {
- if (isMallocLikeFn(&I, TLI)) {
+ if (BadMallocCalls.count(&I))
+ return true;
+
+ bool IsMalloc = isMallocLikeFn(&I, TLI);
+ bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
+ if (!IsMalloc && !IsCalloc) {
+ BadMallocCalls.insert(&I);
+ return true;
+ }
+
+ if (IsMalloc) {
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
- if (!Size->getValue().sle(MaxHeapToStackSize))
- return true;
- } else if (isCallocLikeFn(&I, TLI)) {
+ if (Size->getValue().sle(MaxHeapToStackSize))
+ if (UsesCheck(I)) {
+ MallocCalls.insert(&I);
+ return true;
+ }
+ } else if (IsCalloc) {
bool Overflow = false;
if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
- if (!(Size->getValue().umul_ov(Num->getValue(), Overflow))
+ if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
.sle(MaxHeapToStackSize))
- if (!Overflow)
+ if (!Overflow && UsesCheck(I)) {
+ MallocCalls.insert(&I);
return true;
- } else {
- BadMallocCalls.insert(&I);
- return true;
+ }
}
- if (BadMallocCalls.count(&I))
- return true;
-
- if (UsesCheck(I))
- MallocCalls.insert(&I);
- else
- BadMallocCalls.insert(&I);
+ BadMallocCalls.insert(&I);
return true;
};