[compiler-rt] Allow to override allocation buffer size with a separate env var (PR #71703)

Florian Mayer via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 05:01:22 PST 2023


https://github.com/fmayer updated https://github.com/llvm/llvm-project/pull/71703

>From e58dcb6726532cc81b101547e4a5fe2080ea4340 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Wed, 8 Nov 2023 04:48:47 -0800
Subject: [PATCH 1/5] Allow to override allocation buffer size with a separate
 env var

We don't allow SCUDO_OPTIONS to be preserved across SELinux transitions,
so introducing a more constrained one that we can preserve.
---
 compiler-rt/lib/scudo/standalone/flags.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/compiler-rt/lib/scudo/standalone/flags.cpp b/compiler-rt/lib/scudo/standalone/flags.cpp
index de5153b288b14e3..a3095d4f655ff3d 100644
--- a/compiler-rt/lib/scudo/standalone/flags.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags.cpp
@@ -11,6 +11,7 @@
 #include "flags_parser.h"
 
 #include "scudo/interface.h"
+#include <stdlib.h>
 
 namespace scudo {
 
@@ -68,6 +69,13 @@ void initFlags() {
   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
   Parser.parseString(getScudoDefaultOptions());
   Parser.parseString(getEnv("SCUDO_OPTIONS"));
+  if (const char* V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
+    char* End = nullptr;
+    int I = static_cast<int>(strtol(V, &End, 10));
+    if (End && *End == '\0') {
+      F->allocation_ring_buffer_size = I;
+    }
+  }
 }
 
 } // namespace scudo

>From 14df1200439287c8378ab287f30a645089cf2a5f Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Wed, 8 Nov 2023 09:22:47 -0800
Subject: [PATCH 2/5] fix format

---
 compiler-rt/lib/scudo/standalone/flags.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/flags.cpp b/compiler-rt/lib/scudo/standalone/flags.cpp
index a3095d4f655ff3d..ab0472f319bd49b 100644
--- a/compiler-rt/lib/scudo/standalone/flags.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags.cpp
@@ -69,8 +69,8 @@ void initFlags() {
   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
   Parser.parseString(getScudoDefaultOptions());
   Parser.parseString(getEnv("SCUDO_OPTIONS"));
-  if (const char* V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
-    char* End = nullptr;
+  if (const char *V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
+    char *End = nullptr;
     int I = static_cast<int>(strtol(V, &End, 10));
     if (End && *End == '\0') {
       F->allocation_ring_buffer_size = I;

>From 353c155256dce23f71640aefa27104a6b0dace02 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Thu, 9 Nov 2023 04:40:01 -0800
Subject: [PATCH 3/5] fixup

---
 compiler-rt/lib/scudo/standalone/flags.cpp        |  9 ++-------
 compiler-rt/lib/scudo/standalone/flags_parser.cpp | 11 ++++++++---
 compiler-rt/lib/scudo/standalone/flags_parser.h   |  3 ++-
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/flags.cpp b/compiler-rt/lib/scudo/standalone/flags.cpp
index ab0472f319bd49b..a9ed9d171304930 100644
--- a/compiler-rt/lib/scudo/standalone/flags.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags.cpp
@@ -11,7 +11,6 @@
 #include "flags_parser.h"
 
 #include "scudo/interface.h"
-#include <stdlib.h>
 
 namespace scudo {
 
@@ -69,12 +68,8 @@ void initFlags() {
   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
   Parser.parseString(getScudoDefaultOptions());
   Parser.parseString(getEnv("SCUDO_OPTIONS"));
-  if (const char *V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
-    char *End = nullptr;
-    int I = static_cast<int>(strtol(V, &End, 10));
-    if (End && *End == '\0') {
-      F->allocation_ring_buffer_size = I;
-    }
+  if (const char* V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
+    Parser.parseStringPair("allocation_ring_buffer_size=", V);
   }
 }
 
diff --git a/compiler-rt/lib/scudo/standalone/flags_parser.cpp b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
index be39fcd4f8879ba..feba11d625bcabd 100644
--- a/compiler-rt/lib/scudo/standalone/flags_parser.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
@@ -80,7 +80,7 @@ void FlagParser::parseFlag() {
       ++Pos;
     Value = Buffer + ValueStart;
   }
-  if (!runHandler(Name, Value))
+  if (!runHandler(Name, Value, '='))
     reportError("flag parsing failed.");
 }
 
@@ -122,10 +122,15 @@ inline bool parseBool(const char *Value, bool *b) {
   return false;
 }
 
-bool FlagParser::runHandler(const char *Name, const char *Value) {
+void FlagParser::parseStringPair(const char *Name, const char *Value) {
+    if (!runHandler(Name, Value, '\0'))
+      reportError("flag parsing failed.");
+}
+
+bool FlagParser::runHandler(const char *Name, const char *Value, const char Sep) {
   for (u32 I = 0; I < NumberOfFlags; ++I) {
     const uptr Len = strlen(Flags[I].Name);
-    if (strncmp(Name, Flags[I].Name, Len) != 0 || Name[Len] != '=')
+    if (strncmp(Name, Flags[I].Name, Len) != 0 || Name[Len] != Sep)
       continue;
     bool Ok = false;
     switch (Flags[I].Type) {
diff --git a/compiler-rt/lib/scudo/standalone/flags_parser.h b/compiler-rt/lib/scudo/standalone/flags_parser.h
index ba832adbd90982d..ded496fda3b9787 100644
--- a/compiler-rt/lib/scudo/standalone/flags_parser.h
+++ b/compiler-rt/lib/scudo/standalone/flags_parser.h
@@ -27,6 +27,7 @@ class FlagParser {
                     void *Var);
   void parseString(const char *S);
   void printFlagDescriptions();
+  void parseStringPair(const char *Name, const char *Value);
 
 private:
   static const u32 MaxFlags = 20;
@@ -45,7 +46,7 @@ class FlagParser {
   void skipWhitespace();
   void parseFlags();
   void parseFlag();
-  bool runHandler(const char *Name, const char *Value);
+  bool runHandler(const char *Name, const char *Value, char Sep);
 };
 
 void reportUnrecognizedFlags();

>From 16f80b1e3eee77b7593f021c69a7eed4fedcce19 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Thu, 9 Nov 2023 04:41:56 -0800
Subject: [PATCH 4/5] fixup

---
 compiler-rt/lib/scudo/standalone/flags.cpp        | 4 ++--
 compiler-rt/lib/scudo/standalone/flags_parser.cpp | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/flags.cpp b/compiler-rt/lib/scudo/standalone/flags.cpp
index a9ed9d171304930..f498edfbd326ac8 100644
--- a/compiler-rt/lib/scudo/standalone/flags.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags.cpp
@@ -68,8 +68,8 @@ void initFlags() {
   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
   Parser.parseString(getScudoDefaultOptions());
   Parser.parseString(getEnv("SCUDO_OPTIONS"));
-  if (const char* V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
-    Parser.parseStringPair("allocation_ring_buffer_size=", V);
+  if (const char *V = getEnv("SCUDO_ALLOCATION_RING_BUFFER_SIZE")) {
+    Parser.parseStringPair("allocation_ring_buffer_size", V);
   }
 }
 
diff --git a/compiler-rt/lib/scudo/standalone/flags_parser.cpp b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
index feba11d625bcabd..5e2bba029b05023 100644
--- a/compiler-rt/lib/scudo/standalone/flags_parser.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
@@ -123,11 +123,12 @@ inline bool parseBool(const char *Value, bool *b) {
 }
 
 void FlagParser::parseStringPair(const char *Name, const char *Value) {
-    if (!runHandler(Name, Value, '\0'))
-      reportError("flag parsing failed.");
+  if (!runHandler(Name, Value, '\0'))
+    reportError("flag parsing failed.");
 }
 
-bool FlagParser::runHandler(const char *Name, const char *Value, const char Sep) {
+bool FlagParser::runHandler(const char *Name, const char *Value,
+                            const char Sep) {
   for (u32 I = 0; I < NumberOfFlags; ++I) {
     const uptr Len = strlen(Flags[I].Name);
     if (strncmp(Name, Flags[I].Name, Len) != 0 || Name[Len] != Sep)

>From 27c069a911f5adb4d08fc1b2951997db5b845ad3 Mon Sep 17 00:00:00 2001
From: Florian Mayer <fmayer at google.com>
Date: Thu, 9 Nov 2023 05:00:53 -0800
Subject: [PATCH 5/5] limits

---
 compiler-rt/lib/scudo/standalone/flags_parser.cpp | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/flags_parser.cpp b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
index 5e2bba029b05023..10da6754be821bf 100644
--- a/compiler-rt/lib/scudo/standalone/flags_parser.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "report.h"
 
+#include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -142,8 +143,17 @@ bool FlagParser::runHandler(const char *Name, const char *Value,
       break;
     case FlagType::FT_int:
       char *ValueEnd;
-      *reinterpret_cast<int *>(Flags[I].Var) =
-          static_cast<int>(strtol(Value, &ValueEnd, 10));
+      long V = strtol(Value, &ValueEnd, 10);
+      static_assert(INT_MAX <= LONG_MAX);
+      static_assert(INT_MIN >= LONG_MIN);
+      // strtol returns LONG_MAX on overflow and LONG_MIN on underflow.
+      // This is why we compare-equal here (and lose INT_MIN and INT_MAX as a
+      // value, but that's okay)
+      if (V >= INT_MAX || V <= INT_MIN) {
+        reportInvalidFlag("int", Value);
+        return false;
+      }
+      *reinterpret_cast<int *>(Flags[I].Var) = static_cast<int>(V);
       Ok =
           *ValueEnd == '"' || *ValueEnd == '\'' || isSeparatorOrNull(*ValueEnd);
       if (!Ok)



More information about the llvm-commits mailing list