summaryrefslogtreecommitdiff
path: root/regress/sys/uvm/mmap_4g/mmap_4g.c
blob: 7247f28b42204913a5160cd42604cdcfcc951ab5 (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
/*	$OpenBSD: mmap_4g.c,v 1.3 2010/06/20 17:56:07 phessler Exp $	*/

/*
 * Public domain. 2005, Otto Moerbeek <otto@drijf.net>
 */

#include <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/*
 * Write near the 4g boundary using a mmaped file and check if the
 * bytes do not wrap to offset 0.
 */

int
main()
{
	int fd, i;
	off_t offset;
	size_t sz;
	char *p, buf[100];
	const char * file = "foo";

	fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (fd == -1)
		err(1, "open");

	sz = sizeof(buf);
	offset = 4LL * 1024LL * 1024LL * 1024LL - sz/2;

	if (lseek(fd, offset, SEEK_SET) != offset)
		err(1, "lseek");
	memset(buf, 0, sz);
	if (write(fd, buf, sz) != sz)
		err(1, "write");
	close(fd);

	fd = open(file, O_RDWR, 0);
	if (fd == -1)
		err(1, "open");
	p = mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
	    fd, offset);
	if (p == MAP_FAILED)
		err(1, "mmap");
	for (i = 0; i < sz; i++)
		p[i] = i + 1;
	if (munmap(p, sz) == -1)
		err(1, "munmap");
	close(fd);

	fd = open(file, O_RDONLY, 0);
	if (fd == -1)
		err(1, "open");
	if (read(fd, buf, sz) != sz)
		err(1, "read");
	for (i = 0; i < sz; i++)
		if (buf[i])
			errx(1, "nonzero byte 0x%02x found at offset %zu", 
			    buf[i], i);

	if (lseek(fd, offset, SEEK_SET) != offset)
		err(1, "lseek");
	if (read(fd, buf, sz) != sz)
		err(1, "read");
	for (i = 0; i < sz; i++)
		if (buf[i] != i + 1)
			err(1, "incorrect value 0x%02x at offset %llx",
			    p[i], offset + i);

	close(fd);
	unlink(file);
	return 0;
}