diff options
| author | klaar36 <klas.arvidsson@liu.se> | 2015-03-20 17:30:24 +0100 |
|---|---|---|
| committer | klaar36 <klas.arvidsson@liu.se> | 2015-03-20 17:30:24 +0100 |
| commit | e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad (patch) | |
| tree | 4de97af7207676b69cb6a9aba8cb443cc134855d /src/examples/buffer_with_hole.c | |
| parent | b0418a24e709f0632d2ede5b0f327c422931939b (diff) | |
| download | pintos-rs-e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad.tar.gz | |
Initial Pintos
Diffstat (limited to 'src/examples/buffer_with_hole.c')
| -rw-r--r-- | src/examples/buffer_with_hole.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/examples/buffer_with_hole.c b/src/examples/buffer_with_hole.c new file mode 100644 index 0000000..8d81009 --- /dev/null +++ b/src/examples/buffer_with_hole.c @@ -0,0 +1,52 @@ +/* klaar@ida + + Create a buffer that spans invalid pages. Used to check memory + verification. + */ +#include <stdio.h> +#include <stdlib.h> +#include <syscall.h> + +/* global variables are placed by the compiler in the process image, + * among the initialized variables + * the process image is loaded at the bottom of the virtual memory space + */ +char global = 0; + +int main (int argc, char *argv[]) +{ + /* local variables are allocated on the proces stack during runtime + * the stack is at top of the virtual memory space + */ + char local = 0; + int fd; + int count; + unsigned l, g; + + /* both adresses will of course be valid */ + printf("global: %p\n", &global); + printf("local : %p\n", &local); + + /* and a pointer starting at the global variable adress, and ending + * at the local variable addres will have valid start and end + * but will all addresses (about 3 GB) inbetween be valid? + */ + l = (unsigned)&local; + g = (unsigned)&global; + printf("size : %u MiB\n", (l - g)/1024/1024); + + create("somefile", 1000); + fd = open("somefile"); + + /* this tries to use the buffer (it should fail verification) */ + count = read(fd, &global, (l - g + 1)); + printf("read %i out of %u bytes\n", count, (l - g + 1)); + + /* this tries to use a buffer probably ending before the stack */ + count = read(fd, &global, (l - g + 1) - 4096); + printf("read %i out of %u bytes\n", count, (l - g + 1) - 4096); + + close(fd); + + return 0; +} |
