Fixes exec-bound-3 failures
[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 /* Together with statements in src/lib/user/user.lds, arranges
11    for the following array to be at the very end of the .bss
12    segment (needed for get_bad_boundary below). */
13 static char dst[8192] __attribute__ ((section (".testEndmem,\"aw\",@nobits#")));
14
15 /* Returns the beginning of a page.  There are at least 2048
16    modifiable bytes on either side of the pointer returned. */
17 void *
18 get_boundary_area (void) 
19 {
20   char *p = (char *) ROUND_UP ((uintptr_t) dst, 4096);
21   if (p - dst < 2048)
22     p += 4096;
23   return p;
24 }
25
26 /* Returns a copy of SRC split across the boundary between two
27    pages. */
28 char *
29 copy_string_across_boundary (const char *src) 
30 {
31   char *p = get_boundary_area ();
32   p -= strlen (src) < 4096 ? strlen (src) / 2 : 4096;
33   strlcpy (p, src, 4096);
34   return p;
35 }
36
37 /* Returns an address that is invalid, but the preceding bytes
38  * are all valid (the highest address in the bss segment). Used
39  * to position information such that the first byte of the
40  * information is valid, but not all the information is valid. */
41 void *
42 get_bad_boundary (void)
43 {
44   /* This code assumes that dst will be in the highest page
45    * allocated to the user process. */
46   return (void *) ROUND_UP ((uintptr_t) (dst + sizeof(dst) - 1), 4096);
47 }