aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libtest/common.h1
-rw-r--r--libtest/libtest_alloc.c3
-rw-r--r--libtest/libtest_check_no_leaks.c42
-rw-r--r--libtest/libtest_fd_tracking.c9
-rw-r--r--libtest/libtest_free.c4
5 files changed, 46 insertions, 13 deletions
diff --git a/libtest/common.h b/libtest/common.h
index e4344a9..ed9272e 100644
--- a/libtest/common.h
+++ b/libtest/common.h
@@ -11,6 +11,7 @@
#include <sys/mman.h>
#include <dirent.h>
#include <errno.h>
+#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <signal.h>
diff --git a/libtest/libtest_alloc.c b/libtest/libtest_alloc.c
index dc9cc8f..96aa132 100644
--- a/libtest/libtest_alloc.c
+++ b/libtest/libtest_alloc.c
@@ -77,9 +77,7 @@ libtest_alloc(struct meminfo *meminfo)
saved_errno = errno;
meminfo_fixup(meminfo);
-#ifdef WITH_BACKTRACE
meminfo->accept_leakage |= libtest_malloc_internal_usage > 0;
-#endif
/* Get backtrace (have to do it now to calculate `backtrace_n` for allocation) */
#ifdef WITH_BACKTRACE
@@ -186,6 +184,7 @@ libtest_alloc(struct meminfo *meminfo)
libtest_allocs_tail.prev->next = meminfo;
meminfo->prev = libtest_allocs_tail.prev;
meminfo->next = &libtest_allocs_tail;
+ libtest_allocs_tail.prev = meminfo;
SPINUNLOCK(libtest_allocs_list_spinlock);
recursion_guard = 0;
}
diff --git a/libtest/libtest_check_no_leaks.c b/libtest/libtest_check_no_leaks.c
index aa99377..faaf724 100644
--- a/libtest/libtest_check_no_leaks.c
+++ b/libtest/libtest_check_no_leaks.c
@@ -46,27 +46,63 @@ out:
int
libtest_check_no_leaks(void)
{
- return libtest_fd_tracking(-1) & check_no_memory_leaks();
+ int r = 1;
+ r &= libtest_fd_tracking(-1);
+ r &= check_no_memory_leaks();
+ return r;
}
#else
+#define p libtest_ptr___
+extern unsigned char *volatile p;
+unsigned char *volatile p;
+
+
int
main(void)
{
- void *p;
+#ifdef WITH_BACKTRACE
+ int fd1, fd2;
+#endif
SET_UP_ALARM();
+#ifdef WITH_BACKTRACE
+ fd1 = dup(STDERR_FILENO);
+ assert(fd1 >= 0 || errno == EBADF);
+ assert(fd1 != STDERR_FILENO);
+ if (fd1 >= 0) {
+ close(STDERR_FILENO);
+ fd2 = open("/dev/null", O_WRONLY);
+ assert(fd2 >= 0);
+ if (fd2 != STDERR_FILENO) {
+ assert(dup2(fd2, STDERR_FILENO) == STDERR_FILENO);
+ close(fd2);
+ }
+ }
+ libtest_print_backtrace(stderr, "", "", 0u, NULL, NULL);
+ if (fd1 >= 0) {
+ assert(dup2(fd1, STDERR_FILENO) == STDERR_FILENO);
+ close(fd1);
+ }
+#endif
+
libtest_start_tracking();
- assert(p = malloc(1u));
+ assert(!libtest_kill_malloc_tracking);
+ p = malloc(1u);
+ assert(p);
libtest_suppress_leak_output = 1;
+ *p = 0;
+ atomic_thread_fence(memory_order_seq_cst);
EXPECT(!libtest_check_no_leaks());
+ atomic_thread_fence(memory_order_seq_cst);
libtest_suppress_leak_output = 0;
free(p);
libtest_stop_tracking();
+ atomic_thread_fence(memory_order_seq_cst);
EXPECT(libtest_check_no_leaks());
return 0;
diff --git a/libtest/libtest_fd_tracking.c b/libtest/libtest_fd_tracking.c
index 25cea8f..0dd69d2 100644
--- a/libtest/libtest_fd_tracking.c
+++ b/libtest/libtest_fd_tracking.c
@@ -60,7 +60,6 @@ libtest_fd_tracking(int action)
DIR *dir;
struct dirent *f;
int ret = 1, dfd, name, digit;
- int accept_memleak = libtest_malloc_accept_leakage;
size_t i, j;
char *path;
int old_tracking_state;
@@ -72,10 +71,7 @@ libtest_fd_tracking(int action)
if (old_tracking_state == action)
return 1;
- /* so libtest doesn't complain about us not zeroing before freeing,
- * and so it will not report memory leaks in fprintf from our
- * resource leak report */
- libtest_malloc_accept_leakage = 1;
+ libtest_malloc_internal_usage++;
dir = opendir("/dev/fd/");
assert(dir != NULL);
@@ -95,6 +91,7 @@ next:
continue;
new_opened = realloc(new_opened, (new_nopened + 1u) * sizeof(*new_opened));
+ assert(new_opened);
new_opened[new_nopened].name = name;
new_opened[new_nopened].leakable = action;
new_nopened += 1u;
@@ -146,7 +143,7 @@ next:
nopened = 0u;
}
- libtest_malloc_accept_leakage = accept_memleak;
+ libtest_malloc_internal_usage--;
return ret;
}
diff --git a/libtest/libtest_free.c b/libtest/libtest_free.c
index bdc214b..396c716 100644
--- a/libtest/libtest_free.c
+++ b/libtest/libtest_free.c
@@ -57,16 +57,16 @@ libtest_free(void *ptr, enum libtest_zero_check zero_checking)
break;
}
}
+#ifdef WITH_BACKTRACE
if (!memory_zeroed && mem->backtrace) {
libtest_malloc_internal_usage++;
inside_free = 1;
fprintf(stderr, "Memory not zeroed out before deallocation: %p\n", ptr);
-#ifdef WITH_BACKTRACE
libtest_print_backtrace(stderr, "\tAllocated at ", "\t at ",
0u, mem->backtrace, NULL);
-#endif
inside_free = 0;
}
+#endif
assert(memory_zeroed);
}