@@ -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