Added more userprog tests, mostly relating to boundary conditions
[pintos-anon] / src / tests / userprog / boundary.c
1 /* Utility function for tests that try to break system calls by
2    passing them data that crosses from one virtual page to
3    another. */
4
5 #include <inttypes.h>
6 #include <round.h>
7 #include <string.h>
8 #include "tests/userprog/boundary.h"
9
10 static char dst[8192];
11
12 /* Returns the beginning of a page.  There are at least 2048
13    modifiable bytes on either side of the pointer returned. */
14 void *
15 get_boundary_area (void) 
16 {
17   char *p = (char *) ROUND_UP ((uintptr_t) dst, 4096);
18   if (p - dst < 2048)
19     p += 4096;
20   return p;
21 }
22
23 /* Returns a copy of SRC split across the boundary between two
24    pages. */
25 char *
26 copy_string_across_boundary (const char *src) 
27 {
28   char *p = get_boundary_area ();
29   p -= strlen (src) < 4096 ? strlen (src) / 2 : 4096;
30   strlcpy (p, src, 4096);
31   return p;
32 }
33
34 /* Returns an address that is invalid, but the preceding bytes
35  * are all valid. Used to position information such that the
36  * first byte of the information is valid, but not all the
37  * information is valid. */
38 void *
39 get_bad_boundary (void)
40 {
41   /* This code assumes that dst will be in the highest page
42    * allocated to the user process. */
43   return (void *) ROUND_UP ((uintptr_t) (dst + sizeof(dst) - 1), 4096);
44 }