summaryrefslogtreecommitdiffstats
path: root/src/kernel/tests/lib/tst_buffers.c
blob: b8b597a12a390e024bd7f1bff72edb7132e67c0f (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2019 Cyril Hrubis <chrubis@suse.cz>
 */

#include <sys/mman.h>
#include <stdlib.h>
#define TST_NO_DEFAULT_MAIN
#include "tst_test.h"

struct map {
	void *addr;
	size_t size;
	size_t buf_shift;
	struct map *next;
};

static struct map *maps;

static void setup_canary(struct map *map)
{
	size_t i;
	char *buf = map->addr;

	for (i = 0; i < map->buf_shift/2; i++) {
		char c = random();
		buf[map->buf_shift - i - 1] = c;
		buf[i] = c;
	}
}

static void check_canary(struct map *map)
{
	size_t i;
	char *buf = map->addr;

	for (i = 0; i < map->buf_shift/2; i++) {
		if (buf[map->buf_shift - i - 1] != buf[i]) {
			tst_res(TWARN,
				"pid %i: buffer modified address %p[%zi]",
				getpid(), (char*)map->addr + map->buf_shift, -i-1);
		}
	}
}

void *tst_alloc(size_t size)
{
	size_t page_size = getpagesize();
	unsigned int pages = (size / page_size) + !!(size % page_size) + 1;
	void *ret;
	struct map *map = SAFE_MALLOC(sizeof(struct map));
	static int print_msg = 1;

	if (print_msg) {
		tst_res(TINFO, "Test is using guarded buffers");
		print_msg = 0;
	}

	ret = SAFE_MMAP(NULL, page_size * pages, PROT_READ | PROT_WRITE,
	                MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);

	mprotect(ret + (pages-1) * page_size, page_size, PROT_NONE);

	map->addr = ret;
	map->size = pages * page_size;
	map->next = maps;
	maps = map;

	if (size % page_size)
		map->buf_shift = page_size - (size % page_size);
	else
		map->buf_shift = 0;

	setup_canary(map);

	return ret + map->buf_shift;
}

static int count_iovec(int *sizes)
{
	int ret = 0;

	while (sizes[ret++] != -1);

	return ret - 1;
}

struct iovec *tst_iovec_alloc(int sizes[])
{
	int i, cnt = count_iovec(sizes);
	struct iovec *iovec;

	if (cnt <= 0)
		return NULL;

	iovec = tst_alloc(sizeof(struct iovec) * cnt);

	for (i = 0; i < cnt; i++) {
		if (sizes[i]) {
			iovec[i].iov_base = tst_alloc(sizes[i]);
			iovec[i].iov_len = sizes[i];
		} else {
			iovec[i].iov_base = NULL;
			iovec[i].iov_base = 0;
		}
	}

	return iovec;
}

void tst_buffers_alloc(struct tst_buffers bufs[])
{
	unsigned int i;

	for (i = 0; bufs[i].ptr; i++) {
		if (bufs[i].size)
			*((void**)bufs[i].ptr) = tst_alloc(bufs[i].size);
		else
			*((void**)bufs[i].ptr) = tst_iovec_alloc(bufs[i].iov_sizes);
	}
}

char *tst_strdup(const char *str)
{
	size_t len = strlen(str);
	char *ret = tst_alloc(len + 1);
	return strcpy(ret, str);
}

void tst_free_all(void)
{
	struct map *i = maps;

	while (i) {
		struct map *j = i;
		check_canary(i);
		SAFE_MUNMAP(i->addr, i->size);
		i = i->next;
		free(j);
	}

	maps = NULL;
}