[compiler-rt] [scudo] Fix all build warnings. (PR #93755)

Christopher Ferris via llvm-commits llvm-commits at lists.llvm.org
Wed May 29 18:01:37 PDT 2024


https://github.com/cferris1000 updated https://github.com/llvm/llvm-project/pull/93755

>From 7b0c61ed61349eb6df13489a68816f4b46e71614 Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Wed, 29 May 2024 17:11:34 -0700
Subject: [PATCH 1/2] [scudo] Fix all build warnings.

Fix of all of the build warnings for the different tests and
one warning in the standalone scudo code.
---
 .../scudo/standalone/tests/bytemap_test.cpp   |  2 +-
 .../scudo/standalone/tests/combined_test.cpp  |  3 +-
 .../scudo/standalone/tests/memtag_test.cpp    |  4 ++
 .../scudo/standalone/tests/release_test.cpp   |  3 +-
 .../standalone/tests/wrappers_c_test.cpp      | 43 ++++++++++++++-----
 .../standalone/tests/wrappers_cpp_test.cpp    | 13 ++++--
 compiler-rt/lib/scudo/standalone/tsd_shared.h |  2 +-
 7 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/tests/bytemap_test.cpp b/compiler-rt/lib/scudo/standalone/tests/bytemap_test.cpp
index 4034b108fab97..bc5a237b7a63e 100644
--- a/compiler-rt/lib/scudo/standalone/tests/bytemap_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/bytemap_test.cpp
@@ -16,7 +16,7 @@
 template <typename T> void testMap(T &Map, scudo::uptr Size) {
   Map.init();
   for (scudo::uptr I = 0; I < Size; I += 7)
-    Map.set(I, (I % 100) + 1);
+    Map.set(I, static_cast<scudo::u8>((I % 100) + 1));
   for (scudo::uptr J = 0; J < Size; J++) {
     if (J % 7)
       EXPECT_EQ(Map[J], 0);
diff --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index 1a36155bcd423..6c6af5251a32a 100644
--- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
@@ -54,13 +54,14 @@ void checkMemoryTaggingMaybe(AllocatorT *Allocator, void *P, scudo::uptr Size,
                              scudo::uptr Alignment) {
   const scudo::uptr MinAlignment = 1UL << SCUDO_MIN_ALIGNMENT_LOG;
   Size = scudo::roundUp(Size, MinAlignment);
-  if (Allocator->useMemoryTaggingTestOnly())
+  if (Allocator->useMemoryTaggingTestOnly()) {
     EXPECT_DEATH(
         {
           disableDebuggerdMaybe();
           reinterpret_cast<char *>(P)[-1] = 'A';
         },
         "");
+  }
   if (isPrimaryAllocation<AllocatorT>(Size, Alignment)
           ? Allocator->useMemoryTaggingTestOnly()
           : Alignment == MinAlignment) {
diff --git a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
index 37a18858e67c2..21a5b70da651c 100644
--- a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp
@@ -103,11 +103,15 @@ TEST_F(MemtagDeathTest, AddFixedTag) {
 }
 
 TEST_F(MemtagTest, UntagPointer) {
+// The test is already skipped on anything other than 64 bit. But
+// compiling on 32 bit leads to warnings/errors, so skip compiling the test.
+#if defined(__LP64__)
   uptr UnTagMask = untagPointer(~uptr(0));
   for (u64 Top = 0; Top < 0x100; ++Top) {
     uptr Ptr = (Addr | (Top << 56)) & UnTagMask;
     EXPECT_EQ(addFixedTag(Ptr, 0), untagPointer(Ptr));
   }
+#endif
 }
 
 TEST_F(MemtagDeathTest, ScopedDisableMemoryTagChecks) {
diff --git a/compiler-rt/lib/scudo/standalone/tests/release_test.cpp b/compiler-rt/lib/scudo/standalone/tests/release_test.cpp
index 14b398a91fca4..b0454960f6647 100644
--- a/compiler-rt/lib/scudo/standalone/tests/release_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/release_test.cpp
@@ -91,8 +91,9 @@ class StringRangeRecorder {
     From >>= PageSizeScaledLog;
     To >>= PageSizeScaledLog;
     EXPECT_LT(From, To);
-    if (!ReportedPages.empty())
+    if (!ReportedPages.empty()) {
       EXPECT_LT(LastPageReported, From);
+    }
     ReportedPages.append(From - LastPageReported, '.');
     ReportedPages.append(To - From, 'x');
     LastPageReported = To;
diff --git a/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp b/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
index f5e17d7214863..bb090e0c5fdd2 100644
--- a/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
@@ -120,16 +120,19 @@ class ScudoWrappersCTest : public Test {
     }
   }
   void verifyAllocHookPtr(UNUSED void *Ptr) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Ptr, AC.Ptr);
+    }
   }
   void verifyAllocHookSize(UNUSED size_t Size) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Size, AC.Size);
+    }
   }
   void verifyDeallocHookPtr(UNUSED void *Ptr) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Ptr, DC.Ptr);
+    }
   }
   void verifyReallocHookPtrs(UNUSED void *OldPtr, void *NewPtr, size_t Size) {
     if (SCUDO_ENABLE_HOOKS_TESTS) {
@@ -161,16 +164,18 @@ TEST_F(ScudoWrappersCDeathTest, Malloc) {
   verifyAllocHookPtr(P);
   verifyAllocHookSize(Size);
 
-  // An update to this warning in Clang now triggers in this line, but it's ok
-  // because the check is expecting a bad pointer and should fail.
-#if defined(__has_warning) && __has_warning("-Wfree-nonheap-object")
+#if defined(__has_warning)
+#if __has_warning("-Wfree-nonheap-object")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
+#endif
 #endif
   EXPECT_DEATH(
       free(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(P) | 1U)), "");
-#if defined(__has_warning) && __has_warning("-Wfree-nonheap-object")
+#if defined(__has_warning)
+#if __has_warning("-Wfree-nonheap-object")
 #pragma GCC diagnostic pop
+#endif
 #endif
 
   free(P);
@@ -182,7 +187,10 @@ TEST_F(ScudoWrappersCDeathTest, Malloc) {
   free(P);
 
   errno = 0;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   EXPECT_EQ(malloc(SIZE_MAX), nullptr);
+#pragma GCC diagnostic pop
   EXPECT_EQ(errno, ENOMEM);
 }
 
@@ -204,16 +212,20 @@ TEST_F(ScudoWrappersCTest, Calloc) {
   EXPECT_NE(P, nullptr);
   free(P);
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   errno = 0;
   EXPECT_EQ(calloc(SIZE_MAX, 1U), nullptr);
   EXPECT_EQ(errno, ENOMEM);
   errno = 0;
   EXPECT_EQ(calloc(static_cast<size_t>(LONG_MAX) + 1U, 2U), nullptr);
-  if (SCUDO_ANDROID)
+  if (SCUDO_ANDROID) {
     EXPECT_EQ(errno, ENOMEM);
+  }
   errno = 0;
   EXPECT_EQ(calloc(SIZE_MAX, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
+#pragma GCC diagnostic pop
 }
 
 TEST_F(ScudoWrappersCTest, SmallAlign) {
@@ -262,9 +274,12 @@ TEST_F(ScudoWrappersCTest, Memalign) {
     verifyDeallocHookPtr(P);
   }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   EXPECT_EQ(memalign(4096U, SIZE_MAX), nullptr);
   EXPECT_EQ(posix_memalign(&P, 15U, Size), EINVAL);
   EXPECT_EQ(posix_memalign(&P, 4096U, SIZE_MAX), ENOMEM);
+#pragma GCC diagnostic pop
 
   // Android's memalign accepts non power-of-2 alignments, and 0.
   if (SCUDO_ANDROID) {
@@ -354,6 +369,8 @@ TEST_F(ScudoWrappersCDeathTest, Realloc) {
 
   EXPECT_DEATH(P = realloc(P, Size), "");
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   errno = 0;
   EXPECT_EQ(realloc(nullptr, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
@@ -363,6 +380,7 @@ TEST_F(ScudoWrappersCDeathTest, Realloc) {
   EXPECT_EQ(realloc(P, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
   free(P);
+#pragma GCC diagnostic pop
 
   // Android allows realloc of memalign pointers.
   if (SCUDO_ANDROID) {
@@ -427,15 +445,18 @@ TEST_F(ScudoWrappersCTest, OtherAlloc) {
 #endif
 
 #if HAVE_VALLOC
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   EXPECT_EQ(valloc(SIZE_MAX), nullptr);
+#pragma GCC diagnostic pop
 #endif
 }
 
 template<typename FieldType>
 void MallInfoTest() {
   // mallinfo is deprecated.
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
   const FieldType BypassQuarantineSize = 1024U;
   struct mallinfo MI = mallinfo();
   FieldType Allocated = MI.uordblks;
@@ -448,7 +469,7 @@ void MallInfoTest() {
   free(P);
   MI = mallinfo();
   EXPECT_GE(MI.fordblks, Free + BypassQuarantineSize);
-#pragma clang diagnostic pop
+#pragma GCC diagnostic pop
 }
 
 #if !SCUDO_FUCHSIA
diff --git a/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp b/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
index c802ed22fbad0..786a95d9f4b35 100644
--- a/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
@@ -66,19 +66,25 @@ class ScudoWrappersCppTest : public Test {
   }
 
   void verifyAllocHookPtr(UNUSED void *Ptr) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Ptr, AC.Ptr);
+    }
   }
   void verifyAllocHookSize(UNUSED size_t Size) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Size, AC.Size);
+    }
   }
   void verifyDeallocHookPtr(UNUSED void *Ptr) {
-    if (SCUDO_ENABLE_HOOKS_TESTS)
+    if (SCUDO_ENABLE_HOOKS_TESTS) {
       EXPECT_EQ(Ptr, DC.Ptr);
+    }
   }
 
   template <typename T> void testCxxNew() {
+    // Disable warning about writing on a class object.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wclass-memaccess"
     T *P = new T;
     EXPECT_NE(P, nullptr);
     verifyAllocHookPtr(P);
@@ -127,6 +133,7 @@ class ScudoWrappersCppTest : public Test {
     memset(A, 0x42, sizeof(T) * N);
     delete[] A;
     verifyDeallocHookPtr(A);
+#pragma GCC diagnostic pop
   }
 };
 using ScudoWrappersCppDeathTest = ScudoWrappersCppTest;
diff --git a/compiler-rt/lib/scudo/standalone/tsd_shared.h b/compiler-rt/lib/scudo/standalone/tsd_shared.h
index dade16dad9f2d..450587ddf6714 100644
--- a/compiler-rt/lib/scudo/standalone/tsd_shared.h
+++ b/compiler-rt/lib/scudo/standalone/tsd_shared.h
@@ -196,7 +196,7 @@ struct TSDRegistrySharedT {
   }
 
   void setDisableMemInit(bool B) {
-    *getTlsPtr() &= ~1ULL;
+    *getTlsPtr() &= ~static_cast<uptr>(1);
     *getTlsPtr() |= B;
   }
 

>From cca6d557e0ff8182c8008e197f643683e44e9531 Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Wed, 29 May 2024 18:00:57 -0700
Subject: [PATCH 2/2] Check if some warning exist before turning them off.

---
 .../standalone/tests/wrappers_c_test.cpp      | 36 +++++++++++++++++++
 .../standalone/tests/wrappers_cpp_test.cpp    |  8 +++++
 2 files changed, 44 insertions(+)

diff --git a/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp b/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
index bb090e0c5fdd2..1f7fff35d87c0 100644
--- a/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/wrappers_c_test.cpp
@@ -187,10 +187,18 @@ TEST_F(ScudoWrappersCDeathTest, Malloc) {
   free(P);
 
   errno = 0;
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
+#endif
+#endif
   EXPECT_EQ(malloc(SIZE_MAX), nullptr);
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic pop
+#endif
+#endif
   EXPECT_EQ(errno, ENOMEM);
 }
 
@@ -212,8 +220,12 @@ TEST_F(ScudoWrappersCTest, Calloc) {
   EXPECT_NE(P, nullptr);
   free(P);
 
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
+#endif
+#endif
   errno = 0;
   EXPECT_EQ(calloc(SIZE_MAX, 1U), nullptr);
   EXPECT_EQ(errno, ENOMEM);
@@ -225,7 +237,11 @@ TEST_F(ScudoWrappersCTest, Calloc) {
   errno = 0;
   EXPECT_EQ(calloc(SIZE_MAX, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic pop
+#endif
+#endif
 }
 
 TEST_F(ScudoWrappersCTest, SmallAlign) {
@@ -274,12 +290,16 @@ TEST_F(ScudoWrappersCTest, Memalign) {
     verifyDeallocHookPtr(P);
   }
 
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
   EXPECT_EQ(memalign(4096U, SIZE_MAX), nullptr);
   EXPECT_EQ(posix_memalign(&P, 15U, Size), EINVAL);
   EXPECT_EQ(posix_memalign(&P, 4096U, SIZE_MAX), ENOMEM);
 #pragma GCC diagnostic pop
+#endif
+#endif
 
   // Android's memalign accepts non power-of-2 alignments, and 0.
   if (SCUDO_ANDROID) {
@@ -369,8 +389,12 @@ TEST_F(ScudoWrappersCDeathTest, Realloc) {
 
   EXPECT_DEATH(P = realloc(P, Size), "");
 
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
+#endif
+#endif
   errno = 0;
   EXPECT_EQ(realloc(nullptr, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
@@ -380,7 +404,11 @@ TEST_F(ScudoWrappersCDeathTest, Realloc) {
   EXPECT_EQ(realloc(P, SIZE_MAX), nullptr);
   EXPECT_EQ(errno, ENOMEM);
   free(P);
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic pop
+#endif
+#endif
 
   // Android allows realloc of memalign pointers.
   if (SCUDO_ANDROID) {
@@ -445,11 +473,19 @@ TEST_F(ScudoWrappersCTest, OtherAlloc) {
 #endif
 
 #if HAVE_VALLOC
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
+#endif
+#endif
   EXPECT_EQ(valloc(SIZE_MAX), nullptr);
+#if defined(__has_warning)
+#if __has_warning("-Walloc-size-larger-than=")
 #pragma GCC diagnostic pop
 #endif
+#endif
+#endif
 }
 
 template<typename FieldType>
diff --git a/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp b/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
index 786a95d9f4b35..28dadef1ec562 100644
--- a/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/wrappers_cpp_test.cpp
@@ -83,8 +83,12 @@ class ScudoWrappersCppTest : public Test {
 
   template <typename T> void testCxxNew() {
     // Disable warning about writing on a class object.
+#if defined(__has_warning)
+#if __has_warning("-Wclass-memaccess")
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wclass-memaccess"
+#endif
+#endif
     T *P = new T;
     EXPECT_NE(P, nullptr);
     verifyAllocHookPtr(P);
@@ -133,7 +137,11 @@ class ScudoWrappersCppTest : public Test {
     memset(A, 0x42, sizeof(T) * N);
     delete[] A;
     verifyDeallocHookPtr(A);
+#if defined(__has_warning)
+#if __has_warning("-Wclass-memaccess")
 #pragma GCC diagnostic pop
+#endif
+#endif
   }
 };
 using ScudoWrappersCppDeathTest = ScudoWrappersCppTest;



More information about the llvm-commits mailing list