[compiler-rt] r309601 - [msan] Check for pvalloc overflow

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 11:45:17 PDT 2017


Author: cryptoad
Date: Mon Jul 31 11:45:17 2017
New Revision: 309601

URL: http://llvm.org/viewvc/llvm-project?rev=309601&view=rev
Log:
[msan] Check for pvalloc overflow

Summary:
`CheckForPvallocOverflow` was introduced with D35818 to detect when pvalloc
would wrap when rounding up to the next multiple of the page size.

Add this check to MSan's pvalloc implementation.

Reviewers: alekseyshl

Reviewed By: alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D36093

Modified:
    compiler-rt/trunk/lib/msan/msan_allocator.cc
    compiler-rt/trunk/lib/msan/tests/msan_test.cc

Modified: compiler-rt/trunk/lib/msan/msan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_allocator.cc?rev=309601&r1=309600&r2=309601&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_allocator.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_allocator.cc Mon Jul 31 11:45:17 2017
@@ -255,8 +255,12 @@ void *msan_valloc(uptr size, StackTrace
 
 void *msan_pvalloc(uptr size, StackTrace *stack) {
   uptr PageSize = GetPageSizeCached();
+  if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
+    errno = errno_ENOMEM;
+    return Allocator::FailureHandler::OnBadRequest();
+  }
   // pvalloc(0) should allocate one page.
-  size = size == 0 ? PageSize : RoundUpTo(size, PageSize);
+  size = size ? RoundUpTo(size, PageSize) : PageSize;
   return SetErrnoOnNull(MsanAllocate(stack, size, PageSize, false));
 }
 

Modified: compiler-rt/trunk/lib/msan/tests/msan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/tests/msan_test.cc?rev=309601&r1=309600&r2=309601&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/tests/msan_test.cc (original)
+++ compiler-rt/trunk/lib/msan/tests/msan_test.cc Mon Jul 31 11:45:17 2017
@@ -3449,6 +3449,17 @@ TEST(MemorySanitizer, pvalloc) {
   EXPECT_EQ(0U, (uintptr_t)p % PageSize);
   EXPECT_EQ(PageSize, __sanitizer_get_allocated_size(p));
   free(p);
+
+  // Overflows in pvalloc should be caught.
+  errno = 0;
+  p = pvalloc((uintptr_t)-PageSize);
+  EXPECT_EQ(p, nullptr);
+  EXPECT_EQ(errno, ENOMEM);
+
+  errno = 0;
+  p = pvalloc((uintptr_t)-1);
+  EXPECT_EQ(p, nullptr);
+  EXPECT_EQ(errno, ENOMEM);
 }
 #endif
 




More information about the llvm-commits mailing list