Revert "[randstruct] Check final randomized layout ordering"
authorNico Weber <thakis@chromium.org>
Fri, 22 Apr 2022 12:27:32 +0000 (08:27 -0400)
committerNico Weber <thakis@chromium.org>
Fri, 22 Apr 2022 12:27:32 +0000 (08:27 -0400)
This reverts commit a7815d33bf8f955f2a1888abbccf974bd4858f79.
Test fails on Windows, see comments on https://reviews.llvm.org/D124199

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

index 7d996fb..fadc727 100644 (file)
@@ -150,14 +150,14 @@ void randomizeStructureLayoutImpl(const ASTContext &Context,
   if (CurrentBitfieldRun)
     Buckets.push_back(std::move(CurrentBitfieldRun));
 
-  llvm::shuffle(std::begin(Buckets), std::end(Buckets), RNG);
+  std::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())
-      llvm::shuffle(std::begin(RandFields), std::end(RandFields), RNG);
+      std::shuffle(std::begin(RandFields), std::end(RandFields), RNG);
 
     FinalOrder.insert(FinalOrder.end(), RandFields.begin(), RandFields.end());
   }
index 3c3f6a7..134752f 100644 (file)
@@ -159,9 +159,7 @@ TEST(RANDSTRUCT_TEST, UnmarkedStruct) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_FALSE(RD->hasAttr<RandomizeLayoutAttr>());
   EXPECT_FALSE(RD->isRandomized());
 }
@@ -179,9 +177,7 @@ TEST(RANDSTRUCT_TEST, MarkedNoRandomize) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->hasAttr<NoRandomizeLayoutAttr>());
   EXPECT_FALSE(RD->isRandomized());
 }
@@ -199,9 +195,7 @@ TEST(RANDSTRUCT_TEST, MarkedRandomize) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"lettuce", "bacon", "mayonnaise", "tomato"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->hasAttr<RandomizeLayoutAttr>());
   EXPECT_TRUE(RD->isRandomized());
 }
@@ -215,7 +209,8 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsDeclVsDef) {
         long long tomato;
         float mayonnaise;
     } __attribute__((no_randomize_layout));
-  )c", true);
+  )c",
+                                         true);
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -236,7 +231,8 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsRandomizeVsNoRandomize) {
         long long tomato;
         float mayonnaise;
     } __attribute__((randomize_layout)) __attribute__((no_randomize_layout));
-  )c", true);
+  )c",
+                                         true);
 
   EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -256,7 +252,8 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsNoRandomizeVsRandomize) {
         long long tomato;
         float mayonnaise;
     } __attribute__((no_randomize_layout)) __attribute__((randomize_layout));
-  )c", true);
+  )c",
+                                         true);
 
   EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -283,16 +280,14 @@ TEST(RANDSTRUCT_TEST, CheckAdjacentBitfieldsRemainAdjacentAfterRandomization) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {
-      "a", "b", "c", "x", "y", "z" // x, y, z needs to be a subsequnce.
-  };
+  const field_names Actual = getFieldNamesFromRecord(RD);
+  const field_names Subseq = {"x", "y", "z"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
+  EXPECT_TRUE(isSubsequence(Actual, Subseq));
 }
 
-// FIXME: Enable when fix for flexible arrays is submitted.
-TEST(RANDSTRUCT_TEST, DISABLED_CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
+TEST(RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
   std::unique_ptr<ASTUnit> AST = makeAST(R"c(
     struct test {
         int a;
@@ -305,9 +300,7 @@ TEST(RANDSTRUCT_TEST, DISABLED_CheckVariableLengthArrayMemberRemainsAtEndOfStruc
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"c", "a", "b", "name"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 }
 
@@ -332,7 +325,10 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
         long long b;
         int c[];
     } __attribute__((packed, randomize_layout));
-  )c", false, field_names({"test_struct", "another_struct", "last_struct"}));
+  )c",
+              false,
+              std::vector<std::string>(
+                  {"test_struct", "another_struct", "last_struct"}));
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
@@ -344,9 +340,7 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
         getRecordDeclFromAST(AST->getASTContext(), "test_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
-    const field_names Expected = {"b", "a", "c", "d"};
 
-    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(19, Layout->getSize().getQuantity());
   }
@@ -356,9 +350,7 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
         getRecordDeclFromAST(AST->getASTContext(), "another_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
-    const field_names Expected = {"c", "b", "a"};
 
-    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(10, Layout->getSize().getQuantity());
   }
@@ -368,9 +360,7 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
         getRecordDeclFromAST(AST->getASTContext(), "last_struct");
     const ASTRecordLayout *Layout =
         &AST->getASTContext().getASTRecordLayout(RD);
-    const field_names Expected = {"a", "c", "b"};
 
-    EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
     EXPECT_TRUE(RD->isRandomized());
     EXPECT_EQ(9, Layout->getSize().getQuantity());
   }
@@ -388,9 +378,7 @@ TEST(RANDSTRUCT_TEST, ZeroWidthBitfieldsSeparateAllocationUnits) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"a", "b", ""};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 }
 
@@ -408,10 +396,9 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotRandomizeUnionFieldOrder) {
 
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
-  const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"a", "b", "c", "d", "e", "f"};
+  const RecordDecl *RD =
+      getRecordDeclFromAST(AST->getASTContext(), "test");
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_FALSE(RD->isRandomized());
 }
 
@@ -449,9 +436,7 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
   EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
 
   const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
-  const field_names Expected = {"", "l", "", "r", "s", "a", "f"};
 
-  EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
   EXPECT_TRUE(RD->isRandomized());
 
   bool AnonStructTested = false;