tests: allow brk to fail with ENOMEM

Any caller of brk needs to handle failure by falling back to mmap as
there's no guarantee that other mappings are not placed above the brk
heap. Since jemalloc starts with mmap in the first place and dlmalloc
knows how to fall back to mmap, it's already a legacy API in Android.

Forcing it to succeed rules out stronger ASLR implementations where the
brk heap might not have any room to grow. It's also valid for the brk
implementation to simply return -1 with errno set to ENOMEM in every
case and that's a good way of finding anything relying on it. It would
make sense for it to be an optional kernel feature.

The brk heap is also not guaranteed to be initially page aligned. PaX
ASLR randomizes the internal bits. It was only mentioned in a comment to
explain the design of the test though.

Bug: 24233096
Change-Id: I16e9bc8677e796c73915b830b99b0ce39c02b31d
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 79b0fa5..841641f 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -58,14 +58,24 @@
 TEST(UNISTD_TEST, brk) {
   void* initial_break = get_brk();
 
-  // The kernel aligns the break to a page.
   void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
-  ASSERT_EQ(0, brk(new_break));
-  ASSERT_GE(get_brk(), new_break);
+  int ret = brk(new_break);
+  if (ret == -1) {
+    ASSERT_EQ(errno, ENOMEM);
+  } else {
+    ASSERT_EQ(0, ret);
+    ASSERT_GE(get_brk(), new_break);
+  }
 
+  // Expand by a full page to force the mapping to expand
   new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
-  ASSERT_EQ(0, brk(new_break));
-  ASSERT_EQ(get_brk(), new_break);
+  ret = brk(new_break);
+  if (ret == -1) {
+    ASSERT_EQ(errno, ENOMEM);
+  } else {
+    ASSERT_EQ(0, ret);
+    ASSERT_EQ(get_brk(), new_break);
+  }
 }
 
 TEST(UNISTD_TEST, brk_ENOMEM) {