PKUOS - Pintos
Pintos source browser for PKU Operating System course
mmap-clean.c
Go to the documentation of this file.
1/** Verifies that mmap'd regions are only written back on munmap
2 if the data was actually modified in memory. */
3
4#include <string.h>
5#include <syscall.h>
6#include "tests/vm/sample.inc"
7#include "tests/lib.h"
8#include "tests/main.h"
9
10void
12{
13 static const char overwrite[] = "Now is the time for all good...";
14 static char buffer[sizeof sample - 1];
15 char *actual = (char *) 0x54321000;
16 int handle;
17 mapid_t map;
18
19 /* Open file, map, verify data. */
20 CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\"");
21 CHECK ((map = mmap (handle, actual)) != MAP_FAILED, "mmap \"sample.txt\"");
22 if (memcmp (actual, sample, strlen (sample)))
23 fail ("read of mmap'd file reported bad data");
24
25 /* Modify file. */
26 CHECK (write (handle, overwrite, strlen (overwrite))
27 == (int) strlen (overwrite),
28 "write \"sample.txt\"");
29
30 /* Close mapping. Data should not be written back, because we
31 didn't modify it via the mapping. */
32 msg ("munmap \"sample.txt\"");
33 munmap (map);
34
35 /* Read file back. */
36 msg ("seek \"sample.txt\"");
37 seek (handle, 0);
38 CHECK (read (handle, buffer, sizeof buffer) == sizeof buffer,
39 "read \"sample.txt\"");
40
41 /* Verify that file overwrite worked. */
42 if (memcmp (buffer, overwrite, strlen (overwrite))
43 || memcmp (buffer + strlen (overwrite), sample + strlen (overwrite),
44 strlen (sample) - strlen (overwrite)))
45 {
46 if (!memcmp (buffer, sample, strlen (sample)))
47 fail ("munmap wrote back clean page");
48 else
49 fail ("read surprising data from file");
50 }
51 else
52 msg ("file change was retained after munmap");
53}
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
mapid_t mmap(int fd, void *addr)
Project 3 and optionally project 4.
Definition: syscall.c:145
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
void seek(int fd, unsigned position)
Definition: syscall.c:127
int read(int fd, void *buffer, unsigned size)
Definition: syscall.c:115
void munmap(mapid_t mapid)
Definition: syscall.c:151
#define MAP_FAILED
Definition: syscall.h:13
int mapid_t
Map region identifier.
Definition: syscall.h:12
void fail(const char *format,...)
Definition: lib.c:40
void msg(const char *format,...)
Definition: lib.c:28
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
void test_main(void)
Verifies that mmap'd regions are only written back on munmap if the data was actually modified in mem...
Definition: mmap-clean.c:11
int memcmp(const void *a_, const void *b_, size_t size)
Find the first differing byte in the two blocks of SIZE bytes at A and B.
Definition: string.c:53
size_t strlen(const char *string)
Returns the length of STRING.
Definition: string.c:293