[FuzzMutate] Avoid calling function with metadata/token parameter/return type for...
authorHenry Yu <hnryu@ucdavis.edu>
Thu, 1 Jun 2023 20:56:20 +0000 (13:56 -0700)
committerPeter Rong <PeterRong96@gmail.com>
Thu, 1 Jun 2023 20:59:47 +0000 (13:59 -0700)
When there is a function with metadata/token parameter/return type, `InsertFunctionStrategy` will crash.

This patch fixes the problem by falling back to create function declaration when the sampled function contains metadata/token parameter/return type.

Reviewed By: Peter

Differential Revision: https://reviews.llvm.org/D150627

llvm/lib/FuzzMutate/IRMutator.cpp
llvm/unittests/FuzzMutate/StrategiesTest.cpp

index 90dd532..df191ca 100644 (file)
@@ -360,7 +360,11 @@ void InsertFunctionStrategy::mutate(BasicBlock &BB, RandomIRBuilder &IB) {
 
   auto RS = makeSampler(IB.Rand, Functions);
   Function *F = RS.getSelection();
-  if (!F) {
+  auto IsUnsupportedTy = [](Type *T) {
+    return T->isMetadataTy() || T->isTokenTy();
+  };
+  if (!F || IsUnsupportedTy(F->getReturnType()) ||
+      any_of(F->getFunctionType()->params(), IsUnsupportedTy)) {
     F = IB.createFunctionDeclaration(*M);
   }
 
index b89ca10..dc2e498 100644 (file)
@@ -374,7 +374,7 @@ TEST(InstModificationIRStrategyTest, DidntShuffleFRem) {
   VerfyDivDidntShuffle(Source);
 }
 
-TEST(FunctionIRStrategy, Func) {
+TEST(InsertFunctionStrategy, Func) {
   LLVMContext Ctx;
   const char *Source = "";
   auto Mutator = createMutator<InsertFunctionStrategy>();
@@ -388,6 +388,23 @@ TEST(FunctionIRStrategy, Func) {
   }
 }
 
+TEST(InsertFunctionStrategy, AvoidCallingFunctionWithSpecialParam) {
+  LLVMContext Ctx;
+  StringRef Source = "\n\
+      declare void @llvm.dbg.value(metadata %0, metadata %1, metadata %2)\n\
+      declare i1 @llvm.experimental.gc.result.i1(token %0)\n\
+      define i32 @test(i32 %0) gc \"statepoint-example\" {\n\
+        ret i32 %0 \n\
+      }";
+  auto Mutator = createMutator<InsertFunctionStrategy>();
+  auto M = parseAssembly(Source.data(), Ctx);
+  srand(Seed);
+  for (int i = 0; i < 100; i++) {
+    Mutator->mutateModule(*M, rand(), 1024);
+    EXPECT_TRUE(!verifyModule(*M, &errs()));
+  }
+}
+
 TEST(InstModificationIRStrategy, Exact) {
   LLVMContext Ctx;
   StringRef Source = "\n\