From 04942c3d9068647b2d4600b72ab6aaeb4ebf920c Mon Sep 17 00:00:00 2001
From: Jason Evans <jasone@canonware.com>
Date: Fri, 3 Jun 2016 20:04:30 -0700
Subject: Remove a stray memset(), and fix a junk filling test regression.

---
 include/jemalloc/internal/large.h             |  3 +++
 include/jemalloc/internal/private_symbols.txt |  1 +
 src/large.c                                   | 13 +++++++++++--
 test/unit/junk.c                              | 24 +++++++++++++++++++-----
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/include/jemalloc/internal/large.h b/include/jemalloc/internal/large.h
index afaa6c3..8345f89 100644
--- a/include/jemalloc/internal/large.h
+++ b/include/jemalloc/internal/large.h
@@ -19,8 +19,11 @@ void	*large_ralloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent,
 #ifdef JEMALLOC_JET
 typedef void (large_dalloc_junk_t)(void *, size_t);
 extern large_dalloc_junk_t *large_dalloc_junk;
+typedef void (large_dalloc_maybe_junk_t)(tsdn_t *, void *, size_t);
+extern large_dalloc_maybe_junk_t *large_dalloc_maybe_junk;
 #else
 void	large_dalloc_junk(void *ptr, size_t usize);
+void	large_dalloc_maybe_junk(tsdn_t *tsdn, void *ptr, size_t usize);
 #endif
 void	large_dalloc_junked_locked(tsdn_t *tsdn, extent_t *extent);
 void	large_dalloc(tsdn_t *tsdn, extent_t *extent);
diff --git a/include/jemalloc/internal/private_symbols.txt b/include/jemalloc/internal/private_symbols.txt
index 07e7f28..d2c882d 100644
--- a/include/jemalloc/internal/private_symbols.txt
+++ b/include/jemalloc/internal/private_symbols.txt
@@ -250,6 +250,7 @@ jemalloc_prefork
 large_dalloc
 large_dalloc_junk
 large_dalloc_junked_locked
+large_dalloc_maybe_junk
 large_malloc
 large_palloc
 large_prof_tctx_get
diff --git a/src/large.c b/src/large.c
index 5c7b445..325b5f1 100644
--- a/src/large.c
+++ b/src/large.c
@@ -76,7 +76,11 @@ large_dalloc_junk(void *ptr, size_t usize)
 large_dalloc_junk_t *large_dalloc_junk = JEMALLOC_N(n_large_dalloc_junk);
 #endif
 
-static void
+#ifdef JEMALLOC_JET
+#undef large_dalloc_maybe_junk
+#define	large_dalloc_maybe_junk JEMALLOC_N(n_large_dalloc_maybe_junk)
+#endif
+void
 large_dalloc_maybe_junk(tsdn_t *tsdn, void *ptr, size_t usize)
 {
 
@@ -87,9 +91,14 @@ large_dalloc_maybe_junk(tsdn_t *tsdn, void *ptr, size_t usize)
 		 */
 		if (!config_munmap || (have_dss && extent_in_dss(tsdn, ptr)))
 			large_dalloc_junk(ptr, usize);
-			memset(ptr, JEMALLOC_FREE_JUNK, usize);
 	}
 }
+#ifdef JEMALLOC_JET
+#undef large_dalloc_maybe_junk
+#define	large_dalloc_maybe_junk JEMALLOC_N(large_dalloc_maybe_junk)
+large_dalloc_maybe_junk_t *large_dalloc_maybe_junk =
+    JEMALLOC_N(n_large_dalloc_maybe_junk);
+#endif
 
 static bool
 large_ralloc_no_move_shrink(tsdn_t *tsdn, extent_t *extent, size_t usize)
diff --git a/test/unit/junk.c b/test/unit/junk.c
index 7a92350..dea0f61 100644
--- a/test/unit/junk.c
+++ b/test/unit/junk.c
@@ -10,6 +10,7 @@ const char *malloc_conf =
 
 static arena_dalloc_junk_small_t *arena_dalloc_junk_small_orig;
 static large_dalloc_junk_t *large_dalloc_junk_orig;
+static large_dalloc_maybe_junk_t *large_dalloc_maybe_junk_orig;
 static void *watch_for_junking;
 static bool saw_junking;
 
@@ -39,13 +40,23 @@ arena_dalloc_junk_small_intercept(void *ptr, const arena_bin_info_t *bin_info)
 static void
 large_dalloc_junk_intercept(void *ptr, size_t usize)
 {
+	size_t i;
 
 	large_dalloc_junk_orig(ptr, usize);
-	/*
-	 * The conditions under which junk filling actually occurs are nuanced
-	 * enough that it doesn't make sense to duplicate the decision logic in
-	 * test code, so don't actually check that the region is junk-filled.
-	 */
+	for (i = 0; i < usize; i++) {
+		assert_u_eq(((uint8_t *)ptr)[i], JEMALLOC_FREE_JUNK,
+		    "Missing junk fill for byte %zu/%zu of deallocated region",
+		    i, usize);
+	}
+	if (ptr == watch_for_junking)
+		saw_junking = true;
+}
+
+static void
+large_dalloc_maybe_junk_intercept(tsdn_t *tsdn, void *ptr, size_t usize)
+{
+
+	large_dalloc_maybe_junk_orig(tsdn, ptr, usize);
 	if (ptr == watch_for_junking)
 		saw_junking = true;
 }
@@ -61,6 +72,8 @@ test_junk(size_t sz_min, size_t sz_max)
 		arena_dalloc_junk_small = arena_dalloc_junk_small_intercept;
 		large_dalloc_junk_orig = large_dalloc_junk;
 		large_dalloc_junk = large_dalloc_junk_intercept;
+		large_dalloc_maybe_junk_orig = large_dalloc_maybe_junk;
+		large_dalloc_maybe_junk = large_dalloc_maybe_junk_intercept;
 	}
 
 	sz_prev = 0;
@@ -111,6 +124,7 @@ test_junk(size_t sz_min, size_t sz_max)
 	if (opt_junk_free) {
 		arena_dalloc_junk_small = arena_dalloc_junk_small_orig;
 		large_dalloc_junk = large_dalloc_junk_orig;
+		large_dalloc_maybe_junk = large_dalloc_maybe_junk_orig;
 	}
 }
 
-- 
cgit v0.12