DragonFlyBSD Kernel Audit
DF-3012 / probe.c
← back to finding ↓ download raw
/* DF-3012 probe: does an mmap dirty+msync reach hammer strategy at all? */
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	const char *path = argv[1];
	long pageno = atol(argv[2]);
	int fd;
	char *map;
	char buf[16384];
	unsigned int *w;
	int i;

	fd = open(path, O_RDWR);
	if (fd < 0) { perror("open"); exit(1); }
	map = mmap(NULL, 16384 * 8, PROT_READ | PROT_WRITE, MAP_SHARED,
		   fd, pageno * 16384);
	if (map == MAP_FAILED) { perror("mmap"); exit(1); }

	/* dirty page 0 of the mapping with unique bytes */
	w = (unsigned int *)map;
	for (i = 0; i < 16384 / 4; i++)
		w[i] = 0xDF3012 ^ i ^ (int)pageno;

	/* pre-readback through the mapping itself */
	memcpy(buf, map, 16384);
	printf("map-readback: %s\n",
	    memcmp(buf, map, 16384) == 0 ? "ok" : "BAD");

	if (msync(map, 16384, MS_SYNC) < 0)
		printf("msync: FAILED errno=%d\n", errno);
	else
		printf("msync: ok\n");

	/* raw pread from the same offset, bypassing the mapping */
	if (pread(fd, buf, 16384, pageno * 16384) == 16384) {
		int hit = 0;
		for (i = 0; i < 16384 / 4; i++)
			if (((unsigned int *)buf)[i] == (0xDF3012u ^ i ^ (unsigned)pageno))
				hit++;
		printf("pread-hitwords: %d/4096\n", hit);
	} else {
		printf("pread: error %d\n", errno);
	}
	return (0);
}