Merge remote-tracking branch 'origin/swift-3.0-branch' into stable
* origin/swift-3.0-branch:
Fixup for the cherrypick of 4b0d2c12 (r277458) into clang-800. The lit test used "nullptr", which clang-800 complains about, so let's just use NULL instead.
Follow-up for r277458: Update the tsan_mman_test.cc unit test.
[tsan] Fix behavior of realloc(nullptr, 0) on Darwin
diff --git a/lib/tsan/rtl/tsan_mman.cc b/lib/tsan/rtl/tsan_mman.cc
index cbfb67a..1d9644e 100644
--- a/lib/tsan/rtl/tsan_mman.cc
+++ b/lib/tsan/rtl/tsan_mman.cc
@@ -143,20 +143,16 @@
}
void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
- void *p2 = 0;
// FIXME: Handle "shrinking" more efficiently,
// it seems that some software actually does this.
- if (sz) {
- p2 = user_alloc(thr, pc, sz);
- if (p2 == 0)
- return 0;
- if (p) {
- uptr oldsz = user_alloc_usable_size(p);
- internal_memcpy(p2, p, min(oldsz, sz));
- }
- }
- if (p)
+ void *p2 = user_alloc(thr, pc, sz);
+ if (p2 == 0)
+ return 0;
+ if (p) {
+ uptr oldsz = user_alloc_usable_size(p);
+ internal_memcpy(p2, p, min(oldsz, sz));
user_free(thr, pc, p);
+ }
return p2;
}
diff --git a/lib/tsan/tests/unit/tsan_mman_test.cc b/lib/tsan/tests/unit/tsan_mman_test.cc
index 609141c..60dea3d 100644
--- a/lib/tsan/tests/unit/tsan_mman_test.cc
+++ b/lib/tsan/tests/unit/tsan_mman_test.cc
@@ -53,9 +53,9 @@
uptr pc = 0;
{
void *p = user_realloc(thr, pc, 0, 0);
- // Strictly saying this is incorrect, realloc(NULL, N) is equivalent to
- // malloc(N), thus must return non-NULL pointer.
- EXPECT_EQ(p, (void*)0);
+ // Realloc(NULL, N) is equivalent to malloc(N), thus must return
+ // non-NULL pointer.
+ EXPECT_NE(p, (void*)0);
}
{
void *p = user_realloc(thr, pc, 0, 100);
@@ -68,7 +68,7 @@
EXPECT_NE(p, (void*)0);
memset(p, 0xde, 100);
void *p2 = user_realloc(thr, pc, p, 0);
- EXPECT_EQ(p2, (void*)0);
+ EXPECT_NE(p2, (void*)0);
}
{
void *p = user_realloc(thr, pc, 0, 100);
diff --git a/test/tsan/Darwin/realloc-zero.cc b/test/tsan/Darwin/realloc-zero.cc
new file mode 100644
index 0000000..9826246
--- /dev/null
+++ b/test/tsan/Darwin/realloc-zero.cc
@@ -0,0 +1,20 @@
+// Test that realloc(nullptr, 0) return a non-NULL pointer.
+
+// RUN: %clang_tsan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <malloc/malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+int main() {
+ void *p = realloc(NULL, 0);
+ if (!p) {
+ abort();
+ }
+ fprintf(stderr, "Okay.\n");
+ return 0;
+}
+
+// CHECK: Okay.