void created this revision.
void added reviewers: aaron.ballman, MaskRay.
Herald added a subscriber: StephenFan.
Herald added a project: All.
void requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This uses "llvm::shuffle" to stop differences in shuffle ordering on
different platforms.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124199

Files:
  clang/lib/AST/Randstruct.cpp
  clang/unittests/AST/RandstructTest.cpp

Index: clang/unittests/AST/RandstructTest.cpp
===================================================================
--- clang/unittests/AST/RandstructTest.cpp
+++ clang/unittests/AST/RandstructTest.cpp
@@ -159,7 +159,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_FALSE(RD->hasAttr<RandomizeLayoutAttr>());
   EXPECT_FALSE(RD->isRandomized());
 }
@@ -177,7 +179,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->hasAttr<NoRandomizeLayoutAttr>());
   EXPECT_FALSE(RD->isRandomized());
 }
@@ -195,7 +199,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"lettuce", "bacon", "mayonnaise", "tomato"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->hasAttr<RandomizeLayoutAttr>());
   EXPECT_TRUE(RD->isRandomized());
 }
@@ -209,8 +215,7 @@
         long long tomato;
         float mayonnaise;
     } __attribute__((no_randomize_layout));
-  )c",
-                                         true);
+  )c", true);
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -231,8 +236,7 @@
         long long tomato;
         float mayonnaise;
     } __attribute__((randomize_layout)) __attribute__((no_randomize_layout));
-  )c",
-                                         true);
+  )c", true);
 
   EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -252,8 +256,7 @@
         long long tomato;
         float mayonnaise;
     } __attribute__((no_randomize_layout)) __attribute__((randomize_layout));
-  )c",
-                                         true);
+  )c", true);
 
   EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -280,11 +283,12 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Actual = getFieldNamesFromRecord(RD);
-  const field_names Subseq = {"x", "y", "z"};
+  const std::vector Expected = {
+      "a", "b", "c", "x", "y", "z" // x, y, z needs to be a subsequnce.
+  };
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
-  EXPECT_TRUE(isSubsequence(Actual, Subseq));
 }
 
 TEST(RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
@@ -300,7 +304,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"c", "a", "b", "name"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 }
 
@@ -325,10 +331,8 @@
         long long b;
         int c[];
     } __attribute__((packed, randomize_layout));
-  )c",
-              false,
-              std::vector<std::string>(
-                  {"test_struct", "another_struct", "last_struct"}));
+  )c", false,
+  std::vector<std::string>({"test_struct", "another_struct", "last_struct"}));
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -340,7 +344,9 @@
         getRecordDeclFromAST(AST->getASTContext(), "test_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
+    const std::vector Expected = {"b", "a", "c", "d"};
 
+    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(19, Layout->getSize().getQuantity());
   }
@@ -350,7 +356,9 @@
         getRecordDeclFromAST(AST->getASTContext(), "another_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
+    const std::vector Expected = {"c", "b", "a"};
 
+    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(10, Layout->getSize().getQuantity());
   }
@@ -360,7 +368,9 @@
         getRecordDeclFromAST(AST->getASTContext(), "last_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
+    const std::vector Expected = {"a", "c", "b"};
 
+    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(9, Layout->getSize().getQuantity());
   }
@@ -378,7 +388,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"a", "b", ""};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 }
 
@@ -396,9 +408,10 @@
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
-  const RecordDecl *RD =
-      getRecordDeclFromAST(AST->getASTContext(), "test");
+  const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"a", "b", "c", "d", "e", "f"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_FALSE(RD->isRandomized());
 }
 
@@ -436,7 +449,9 @@
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
+  const std::vector Expected = {"", "l", "", "r", "s", "a", "f"};
 
+  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 
   bool AnonStructTested = false;
Index: clang/lib/AST/Randstruct.cpp
===================================================================
--- clang/lib/AST/Randstruct.cpp
+++ clang/lib/AST/Randstruct.cpp
@@ -150,14 +150,14 @@
   if (CurrentBitfieldRun)
     Buckets.push_back(std::move(CurrentBitfieldRun));
 
-  std::shuffle(std::begin(Buckets), std::end(Buckets), RNG);
+  llvm::shuffle(std::begin(Buckets), std::end(Buckets), RNG);
 
   // Produce the new ordering of the elements from the Buckets.
   SmallVector<FieldDecl *, 16> FinalOrder;
   for (const std::unique_ptr<Bucket> &B : Buckets) {
     llvm::SmallVectorImpl<FieldDecl *> &RandFields = B->fields();
     if (!B->isBitfieldRun())
-      std::shuffle(std::begin(RandFields), std::end(RandFields), RNG);
+      llvm::shuffle(std::begin(RandFields), std::end(RandFields), RNG);
 
     FinalOrder.insert(FinalOrder.end(), RandFields.begin(), RandFields.end());
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to