aboutsummaryrefslogtreecommitdiffstats
path: root/libtest/libtest_free.c
blob: 3dcd41c2771af5a8f4d0f23e9db57022f7da802d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


void
libtest_free(void *ptr, enum libtest_zero_check zero_checking)
{
	struct meminfo *mem;
	int saved_errno = errno;
	int unmap_err, memory_zeroed;
	uint8_t *usable_area;
	size_t i;
#ifdef WITH_BACKTRACE
	static _Thread_local int inside_free = 0;
#endif

	/* free(3) can be called with NULL */
	if (!ptr)
		return;

	/* Optionally print out trace */
#ifdef WITH_BACKTRACE
	if (!inside_free && getenv("PRETRACE_FREE")) {
		inside_free = 1;
		fprintf(stderr, "Deallocating: %p\n", ptr);
		libtest_print_backtrace(stderr, NULL, "\tat ", 0u, NULL, NULL);
		fflush(stderr);
		inside_free = 0;
	}
#endif

	/* Get the start of the allocation, which
	 * is where the book-keeping is located */
	mem = GET_MEMINFO(ptr);

	/* It is illegal to use free(3) on memory allocated with mmap(3)/mmap(2) */
	assert(mem->origin != FROM_MMAP_FILE);
	assert(mem->origin != FROM_MMAP_ANON);

	/* Delist allocation */
	if (!libtest_kill_malloc_tracking) {
		SPINLOCK(libtest_allocs_list_spinlock);
		mem->prev->next = mem->next;
		mem->next->prev = mem->prev;
		SPINUNLOCK(libtest_allocs_list_spinlock);
	}

	/* Check memory is zeroed */
	if (zero_checking && libtest_expect_zeroed && !mem->accept_leakage) {
		usable_area = mem->usable_area;
		memory_zeroed = 1;
		for (i = 0u; i < mem->usable_alloc_size; i++) {
			if (usable_area[i]) {
				memory_zeroed = 0;
				break;
			}
		}
		assert(memory_zeroed);
	}

	/* Optionally print out trace */
#ifdef WITH_BACKTRACE
	if (!inside_free && getenv("TRACE_MALLOC")) {
		inside_free = 1;
		fprintf(stderr, "Memory deallocated: %p\n (alloc-size=%zu, real-size=%zu)",
		        ptr, mem->requested_alloc_size, mem->real_alloc_size);
		if (getenv("TRACE_FREE") && !getenv("PRETRACE_FREE"))
			libtest_print_backtrace(stderr, NULL, "\tat ", 0u, NULL, NULL);
		fflush(stderr);
		inside_free = 0;
	}
#endif

	/* Deallocate memory */
	unmap_err = libtest_real_munmap(mem, mem->real_alloc_size);
	assert(!unmap_err);

	errno = saved_errno;
}


#else


CONST int
main(void)
{
	/* Tested via alloc.c */
	return 0;
}


#endif