File: xlatmap.c

package info (click to toggle)
libkdumpfile 0.5.5-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,156 kB
  • sloc: ansic: 36,541; sh: 4,219; python: 1,569; makefile: 812
file content (334 lines) | stat: -rw-r--r-- 7,685 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Test translation map low-level handling.
   Copyright (C) 2016 Petr Tesarik <ptesarik@suse.com>

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   libkdumpfile is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <libkdumpfile/addrxlat.h>

#include "testutil.h"

#define NMAPS 15
static addrxlat_map_t *map[NMAPS];

#define CANARY	-100

static int result;

/** Current address translation map. */
static void *curmap;

static void* (*orig_realloc)(void *ptr, size_t size);

/** Evil realloc.
 * @param ptr   Allocated memory block, or @c NULL.
 * @param size  New desired size.
 *
 * When reallocating the current translation map (as denoted by @c curmap),
 * inject a pointer to a canary translation definition, so if any code
 * accesses an (uninitialized) element beyond the original array boundaries,
 * it will hit this canary.
 */
void *
realloc(void *ptr, size_t size)
{
	addrxlat_range_t *oldranges, *newranges;
	size_t oldn, newn, i;

	if (ptr != addrxlat_map_ranges(curmap))
		return orig_realloc(ptr, size);

	oldranges = ptr;
	if (size) {
		newranges = malloc(size);
		if (!newranges)
			return newranges;

		oldn = addrxlat_map_len(curmap);
		newn = size / sizeof(*newranges);
		if (newn > oldn) {
			if (oldranges)
				memcpy(newranges, oldranges,
				       oldn * sizeof(*oldranges));
			for (i = oldn; i < newn; ++i)
				newranges[i].meth = CANARY;
		} else
			memcpy(newranges, oldranges, size);
	} else
		newranges = NULL;

	free(oldranges);
	return newranges;
}

static void
printmap(const addrxlat_map_t *map)
{
	size_t i, n;
	addrxlat_addr_t addr;
	const addrxlat_range_t *range;

	n = addrxlat_map_len(map);
	addr = 0;
	range = addrxlat_map_ranges(map);
	for (i = 0; i < n; ++i) {
		printf("%"ADDRXLAT_PRIxADDR"-%"ADDRXLAT_PRIxADDR": ",
		       addr, addr + range->endoff);
		if (range->meth == ADDRXLAT_SYS_METH_NONE)
			printf("NULL\n");
		else if (range->meth == CANARY)
			printf("CANARY\n");
		else
			printf("#%ld\n", (long)range->meth);

		addr += range->endoff + 1;
		++range;
	}
}

static void
map_set(addrxlat_map_t **pmap, addrxlat_addr_t addr,
	const addrxlat_range_t *range)
{
	addrxlat_status status;

	if (!*pmap) {
		addrxlat_map_t *newmap = addrxlat_map_new();
		if (!newmap) {
			perror("Cannot allocate map");
			exit(TEST_ERR);
		}
		*pmap =	newmap;
	}

	curmap = *pmap;
	status = addrxlat_map_set(curmap, addr, range);
	curmap = NULL;
	if (status != ADDRXLAT_OK) {
		fprintf(stderr, "Cannot update map: %s\n",
			addrxlat_strerror(status));
		exit(TEST_ERR);
	}
}

static void
split_middle(addrxlat_map_t **pmap)
{
	addrxlat_range_t range;

	range.endoff = ADDRXLAT_ADDR_MAX;
	range.meth = 0;
	map_set(pmap, 0, &range);
	range.endoff = 0xffff;
	range.meth = 1;
	map_set(pmap, 0x10000, &range);
}

int
main(int argc, char **argv)
{
	addrxlat_range_t range;
	unsigned i;

	result = TEST_OK;

	orig_realloc = dlsym(RTLD_NEXT, "realloc");
	if (!orig_realloc) {
		fprintf(stderr, "Cannot get original realloc() address: %s\n",
			dlerror());
		return TEST_ERR;
	}

	puts("empty -> single region:");
	range.endoff = ADDRXLAT_ADDR_MAX;
	range.meth = 0;
	map_set(&map[0], 0, &range);
	printmap(map[0]);

	puts("\nreplace single region:");
	range.endoff = ADDRXLAT_ADDR_MAX;
	range.meth = 1;
	map_set(&map[0], 0, &range);
	printmap(map[0]);

	puts("\nempty -> begin:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[1], 0, &range);
	printmap(map[1]);

	puts("\nempty -> end:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[2], ADDRXLAT_ADDR_MAX - range.endoff, &range);
	printmap(map[2]);

	puts("\nempty -> middle:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[3], 0x10000, &range);
	printmap(map[3]);

	puts("\nsplit begin:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[0], 0, &range);
	printmap(map[0]);

	puts("\nsplit end:");
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[0], ADDRXLAT_ADDR_MAX - range.endoff, &range);
	printmap(map[0]);

	puts("\nsplit middle:");
	split_middle(&map[4]);
	printmap(map[4]);

	puts("\nreplace middle:");
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[4], 0x10000, &range);
	printmap(map[4]);

	puts("\nmerge down:");
	range.endoff= 0xffff;
	range.meth = 2;
	map_set(&map[4], 0, &range);
	printmap(map[4]);

	puts("\nmerge up:");
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[4], 0x20000, &range);
	printmap(map[4]);

	puts("\nmerge both:");
	split_middle(&map[5]);
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[5], 0x10000, &range);
	printmap(map[5]);

	puts("\nmerge overlap down:");
	split_middle(&map[6]);
	range.endoff = 0x7fff;
	range.meth = 1;
	map_set(&map[6], 0xc000, &range);
	printmap(map[6]);

	puts("\nmerge overlap up:");
	range.endoff = 0x7fff;
	range.meth = 1;
	map_set(&map[6], 0x1c000, &range);
	printmap(map[6]);

	puts("\nmerge inner:");
	range.endoff = 0x7fff;
	range.meth = 1;
	map_set(&map[6], 0x14000, &range);
	printmap(map[6]);

	puts("\noverlap down:");
	split_middle(&map[7]);
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[7], 0x8000, &range);
	printmap(map[7]);

	puts("\noverlap up:");
	split_middle(&map[8]);
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[8], 0x18000, &range);
	printmap(map[8]);

	puts("\noverlap both:");
	split_middle(&map[9]);
	range.endoff = 0x1ffff;
	range.meth = 2;
	map_set(&map[9], 0x8000, &range);
	printmap(map[9]);

	puts("\noverlap multiple:");
	split_middle(&map[10]);
	range.endoff = 0xffff;
	range.meth = 2;
	map_set(&map[10], 0x20000, &range);
	range.endoff = 0x2ffff;
	range.meth = 3;
	map_set(&map[10], 0x8000, &range);
	printmap(map[10]);

	puts("\npunch hole:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[11], 0x10000, &range);
	range.endoff = 0xfff;
	range.meth = ADDRXLAT_SYS_METH_NONE;
	map_set(&map[11], 0x18000, &range);
	printmap(map[11]);

	puts("\nmerge hole:");
	range.endoff = 0xffff;
	range.meth = ADDRXLAT_SYS_METH_NONE;
	map_set(&map[12], 0x10000, &range);
	printmap(map[12]);

	puts("\nno merge beyond end of map:");
	range.endoff = 0xffff;
	range.meth = 0;
	map_set(&map[13], 0, &range);
	map_set(&map[13], 0x20000, &range);
	/* remove the last element, keeping a known value at [n+1] */
	range.endoff = ADDRXLAT_ADDR_MAX - 0x30000;
	map_set(&map[13], 0x30000, &range);
	range.endoff = 0xffff;
	range.meth = ADDRXLAT_SYS_METH_NONE;
	map_set(&map[13], ADDRXLAT_ADDR_MAX - range.endoff, &range);
	printmap(map[13]);

	puts("\nduplicate punch hole:");
	map[14] = addrxlat_map_copy(map[11]);
	printmap(map[14]);

	/* Cleanup must not crash and reference count must drop to zero */
	for (i = 0; i < NMAPS; ++i) {
		unsigned long refcnt = addrxlat_map_decref(map[i]);
		if (refcnt) {
			printf("Leaked %lu references to map #%d\n",
			       refcnt, i);
			result = TEST_FAIL;
		}
	}

	return result;
}