[PATCH] D37853: [scudo] Fix bad request handling when allocator has not been initialized

Kostya Kortchinsky via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 10:42:02 PDT 2017


cryptoad created this revision.

In a few functions (`scudoMemalign` and the like), we would call
`ScudoAllocator::FailureHandler::OnBadRequest` if the parameters didn't check
out. The issue is that if the allocator had not been initialized (eg: if this
is the first heap related function called), we would use variables like
`allocator_may_return_null` and `exitcode` that still had their default value
(as opposed to the one set by the user or the initialization path).

To solve this, we introduce `handleBadRequest` that will call `initThreadMaybe`,
allowing the options to be correctly initialized.

Unfortunately, the tests were passing because `exitcode` was still 0, so the
results looked like success. Change those tests to do what they were supposed
to.


https://reviews.llvm.org/D37853

Files:
  lib/scudo/scudo_allocator.cpp
  test/scudo/memalign.cpp
  test/scudo/valloc.cpp


Index: test/scudo/valloc.cpp
===================================================================
--- test/scudo/valloc.cpp
+++ test/scudo/valloc.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_scudo %s -o %t
-// RUN: %run %t valid   2>&1
-// RUN: %run %t invalid 2>&1
+// RUN:                                               %run %t valid   2>&1
+// RUN:                                           not %run %t invalid 2>&1
+// RUN: SCUDO_OPTIONS=allocator_may_return_null=1     %run %t invalid 2>&1
 
 // Tests that valloc and pvalloc work as intended.
 
Index: test/scudo/memalign.cpp
===================================================================
--- test/scudo/memalign.cpp
+++ test/scudo/memalign.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_scudo %s -o %t
-// RUN: %run %t valid   2>&1
-// RUN: %run %t invalid 2>&1
+// RUN:                                               %run %t valid   2>&1
+// RUN:                                           not %run %t invalid 2>&1
+// RUN: SCUDO_OPTIONS=allocator_may_return_null=1     %run %t invalid 2>&1
 
 // Tests that the various aligned allocation functions work as intended. Also
 // tests for the condition where the alignment is not a power of 2.
Index: lib/scudo/scudo_allocator.cpp
===================================================================
--- lib/scudo/scudo_allocator.cpp
+++ lib/scudo/scudo_allocator.cpp
@@ -620,6 +620,11 @@
     BackendAllocator.getStats(stats);
     return stats[StatType];
   }
+
+  void *handleBadRequest() {
+    initThreadMaybe();
+    return FailureHandler::OnBadRequest();
+  }
 };
 
 static ScudoAllocator Instance(LINKER_INITIALIZED);
@@ -677,7 +682,7 @@
   uptr PageSize = GetPageSizeCached();
   if (UNLIKELY(CheckForPvallocOverflow(Size, PageSize))) {
     errno = errno_ENOMEM;
-    return ScudoAllocator::FailureHandler::OnBadRequest();
+    return Instance.handleBadRequest();
   }
   // pvalloc(0) should allocate one page.
   Size = Size ? RoundUpTo(Size, PageSize) : PageSize;
@@ -687,14 +692,14 @@
 void *scudoMemalign(uptr Alignment, uptr Size) {
   if (UNLIKELY(!IsPowerOfTwo(Alignment))) {
     errno = errno_EINVAL;
-    return ScudoAllocator::FailureHandler::OnBadRequest();
+    return Instance.handleBadRequest();
   }
   return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMemalign));
 }
 
 int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) {
   if (UNLIKELY(!CheckPosixMemalignAlignment(Alignment))) {
-    ScudoAllocator::FailureHandler::OnBadRequest();
+    Instance.handleBadRequest();
     return errno_EINVAL;
   }
   void *Ptr = Instance.allocate(Size, Alignment, FromMemalign);
@@ -707,7 +712,7 @@
 void *scudoAlignedAlloc(uptr Alignment, uptr Size) {
   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(Alignment, Size))) {
     errno = errno_EINVAL;
-    return ScudoAllocator::FailureHandler::OnBadRequest();
+    return Instance.handleBadRequest();
   }
   return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMalloc));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37853.115245.patch
Type: text/x-patch
Size: 2960 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170914/68c4e5b0/attachment.bin>


More information about the llvm-commits mailing list