Skip to content

Commit 728c8bc

Browse files
committed
[FuzzMutate] Prevent UB caused by parameter ABI attributes.
We make those cases unsupported as it happens now for functions accepting metadata or token types.
1 parent f1534b0 commit 728c8bc

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

llvm/include/llvm/FuzzMutate/IRMutator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ class LLVM_ABI InsertFunctionStrategy : public IRMutationStrategy {
144144

145145
using IRMutationStrategy::mutate;
146146
void mutate(BasicBlock &BB, RandomIRBuilder &IB) override;
147-
148-
private:
149-
bool isUnsupportedFunction(Function *F);
150147
};
151148

152149
/// Strategy to split a random block and insert a random CFG in between.

llvm/lib/FuzzMutate/IRMutator.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,13 @@ static uint64_t getUniqueCaseValue(SmallSet<uint64_t, 4> &CasesTaken,
356356
return tmp;
357357
}
358358

359-
bool InsertFunctionStrategy::isUnsupportedFunction(Function *F) {
359+
/// Determines whether a function is unsupported by the current mutator's
360+
/// implementation. The function returns true if any of the following criteria
361+
/// are met:
362+
/// * The function accepts metadata or token types as arguments.
363+
/// * The function has ABI attributes that could cause UB.
364+
/// * The function uses a non-callable CC that may result in UB.
365+
static bool isUnsupportedFunction(Function *F) {
360366
// Some functions accept metadata type or token type as arguments.
361367
// We don't call those functions for now.
362368
// For example, `@llvm.dbg.declare(metadata, metadata, metadata)`
@@ -370,6 +376,34 @@ bool InsertFunctionStrategy::isUnsupportedFunction(Function *F) {
370376
return true;
371377
}
372378

379+
// ABI attributes must be specified both at the function
380+
// declaration/definition and call-site, otherwise the
381+
// behavior may be undefined.
382+
// We don't call those functions for now to prevent UB from happening.
383+
auto IsABIAttribute = [](AttributeSet A) {
384+
static const Attribute::AttrKind ABIAttrs[] = {
385+
Attribute::StructRet, Attribute::ByVal,
386+
Attribute::InAlloca, Attribute::InReg,
387+
Attribute::StackAlignment, Attribute::SwiftSelf,
388+
Attribute::SwiftAsync, Attribute::SwiftError,
389+
Attribute::Preallocated, Attribute::ByRef,
390+
Attribute::ZExt, Attribute::SExt};
391+
392+
return std::any_of(
393+
std::begin(ABIAttrs), std::end(ABIAttrs),
394+
[&](Attribute::AttrKind kind) { return A.hasAttribute(kind); });
395+
};
396+
397+
auto FuncAttrs = F->getAttributes();
398+
if (IsABIAttribute(FuncAttrs.getRetAttrs())) {
399+
return true;
400+
}
401+
for (size_t i = 0; i < F->arg_size(); i++) {
402+
if (IsABIAttribute(FuncAttrs.getParamAttrs(i))) {
403+
return true;
404+
}
405+
}
406+
373407
// If it is not satisfied, the IR will be invalid.
374408
if (!isCallableCC(F->getCallingConv())) {
375409
return true;

0 commit comments

Comments
 (0)