aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/buffer_with_hole.c
diff options
context:
space:
mode:
authorklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
committerklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
commite7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad (patch)
tree4de97af7207676b69cb6a9aba8cb443cc134855d /src/examples/buffer_with_hole.c
parentb0418a24e709f0632d2ede5b0f327c422931939b (diff)
downloadpintos-rs-e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad.tar.gz
Initial Pintos
Diffstat (limited to 'src/examples/buffer_with_hole.c')
-rw-r--r--src/examples/buffer_with_hole.c52
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;
+}