summaryrefslogtreecommitdiff
path: root/lib/ccan/failtest/test/run-malloc.c
blob: 96641b85250bd97aaa012f54edb1d945742eb3b9 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "config.h"
#include <stdlib.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ccan/tap/tap.h>

/* We don't actually want it to exit... */
static jmp_buf exited;
#define exit(status) longjmp(exited, (status) + 1)

#define printf saved_printf
static int saved_printf(const char *fmt, ...);

#define fprintf saved_fprintf
static int saved_fprintf(FILE *ignored, const char *fmt, ...);

#define vfprintf saved_vfprintf
static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap);

/* Hack to avoid a memory leak which valgrind complains about. */
#define realloc set_realloc
static void *set_realloc(void *ptr, size_t size);

#define free set_free
static void set_free(void *ptr);

/* Include the C files directly. */
#include <ccan/failtest/failtest.c>

#undef realloc
#undef free

static char *buffer;
static void *set_realloc(void *ptr, size_t size)
{
	return buffer = realloc(ptr, size);
}

static void set_free(void *ptr)
{
	if (ptr == buffer)
		buffer = NULL;
	free(ptr);
}

static char *output = NULL;

static int saved_vprintf(const char *fmt, va_list ap)
{
	int ret;
	int len = 0;
	va_list ap2;

	va_copy(ap2, ap);
	ret = vsnprintf(NULL, 0, fmt, ap2);
	va_end(ap2);

	if (output)
		len = strlen(output);

	output = realloc(output, len + ret + 1);
	return vsprintf(output + len, fmt, ap);
}

static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap)
{
	return saved_vprintf(fmt, ap);
}

static int saved_printf(const char *fmt, ...)
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
	ret = saved_vprintf(fmt, ap);
	va_end(ap);
	return ret;
}

static int saved_fprintf(FILE *ignored, const char *fmt, ...)
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
	ret = saved_vprintf(fmt, ap);
	va_end(ap);
	return ret;
}

int main(void)
{
	int status;

	plan_tests(3);
	failtest_init(0, NULL);

	status = setjmp(exited);
	if (status == 0) {
		char *p = failtest_malloc(1, "run-malloc.c", 1);
		/* If we just segv, valgrind counts that as a failure.
		 * So kill ourselves creatively. */
		if (!p)
			kill(getpid(), SIGSEGV);
		fail("Expected child to crash!");
	} else {
		ok1(status == 2);
		ok1(strstr(output, "Killed by signal"));
		ok1(strstr(output, "--failpath=M\n"));
	}
	free(buffer);
	return exit_status();
}