cleaned up the error checking in the run_binary code and fixed some bugs in kmalloc
authorKevin Klues <klueska@eecs.berkeley.edu>
Tue, 30 Jun 2009 03:33:22 +0000 (20:33 -0700)
committerKevin Klues <klueska@eecs.berkeley.edu>
Wed, 1 Jul 2009 20:33:37 +0000 (13:33 -0700)
kern/src/kmalloc.c
kern/src/syscall.c
user/apps/parlib/run_binary.c

index 0412a42..4004353 100644 (file)
@@ -20,6 +20,8 @@ void kmalloc_init() {
 }
 
 void* kmalloc(size_t size, int flags) {
+       if(size == 0)
+               return NULL;
        int npages = ROUNDUP(size, PGSIZE) / PGSIZE;
        
        // Find 'npages' free consecutive pages
@@ -28,11 +30,11 @@ void* kmalloc(size_t size, int flags) {
        kmallocdebug("npages: %u\n", npages);
        for(int i=(naddrpage-1); i>=(npages-1); i--) {
                int j;
-               for(j=i; j>=i-(npages-1); j--) {
+               for(j=i; j>=(i-(npages-1)); j--) {
                        if( !page_is_free(j) )
                                break;
                }
-               if( j == i-(npages-1)-1 ) {
+               if( j == (i-(npages-1)-1)) {
                        first = j+1;
                        break;
                }
@@ -40,18 +42,6 @@ void* kmalloc(size_t size, int flags) {
        //If we couldn't find them, return NULL
        if( first == -1 )
                return NULL;
-       /*
-       cprintf("Starting page check.\n");
-       for (int i = 0; i < naddrpage; i++) {
-               page_t* page = ppn2page(i);
-               if (((page->pp_link.le_next != NULL) && (page->pp_ref != 0)) || ((page->pp_link.le_next == NULL) && (page->pp_ref == 0))) {
-                       cprintf("Out of sync on page: %u\n", i);
-                       cprintf("-->pp_link: %p\n", page->pp_link);
-                       cprintf("-->pp_ref: %p\n", page->pp_ref);
-               }
-       }
-       cprintf("Ending page check.\n");
-       */
                
        //Otherwise go ahead and allocate them to ourselves now
        for(int i=0; i<npages; i++) {
index 0be42f8..8b5b3b2 100644 (file)
@@ -75,6 +75,8 @@ static ssize_t sys_serial_read(env_t* e, char *DANGEROUS buf, size_t len)
 
 static ssize_t sys_run_binary(env_t* e, void* binary_buf, void* arg, size_t len) {
        uint8_t* new_binary = kmalloc(len, 0);
+       if(new_binary == NULL)
+               return -E_NO_MEM;
        memcpy(new_binary, binary_buf, len);
 
        env_t* env = env_create((uint8_t*)new_binary, len);
index 64f2236..5efbbdb 100644 (file)
@@ -13,6 +13,26 @@ extern char * readline(const char *prompt);
 #define READ_SIZE       1024
 uint8_t* binary_buf;
 
+static void fd_error() {
+       fprintf(stderr, "Error: Unable to run remote binary: %s\n", strerror(errno));
+}
+
+static void malloc_error() {
+       fprintf(stderr, "Error: Unable to run remote binary: No more memory avaialable!\n");
+}
+
+static void read_error(void* buf, int fd) {
+       free(binary_buf);
+       close(fd);
+       fprintf(stderr, "Error: Unable to run remote binary: %s\n", strerror(errno));
+}
+
+static void realloc_error(void* buf, int fd) {
+       free(binary_buf);
+       close(fd);
+       fprintf(stderr, "Error: Unable to run remote binary: No more memory available!\n");
+}
+
 void run_binary()
 {      
        char * readline_result = readline("\nEnter name of binary to execute: ");
@@ -24,20 +44,31 @@ void run_binary()
        char * file_name = malloc(strlen(readline_result) + 8);
        sprintf(file_name, "./test/%s", readline_result);
        int fd = open(file_name, O_RDONLY, 0);
+       if(fd < 0) { fd_error(); return; };
        
        int iters = 1;
        binary_buf = malloc(READ_SIZE);
+       if(binary_buf == NULL) { malloc_error(); return; }
        
        int total_bytes_read = 0;
        int bytes_read = read(fd, binary_buf, READ_SIZE);
+       if(bytes_read < 0) { read_error(binary_buf, fd); return; }
+       
        while(bytes_read > 0) {
                total_bytes_read += bytes_read; 
-               binary_buf = realloc(binary_buf, READ_SIZE*(++iters));
+               void* temp_buf = realloc(binary_buf, READ_SIZE*(++iters));
+               if(temp_buf == NULL) { realloc_error(binary_buf, fd); return; } 
+               binary_buf = temp_buf;
                bytes_read = read(fd, binary_buf+total_bytes_read, READ_SIZE);
+               if(bytes_read < 0) { read_error(binary_buf, fd); return; }
        }
        printf("Loading Binary: %s, ROMSIZE: %d\n", readline_result, total_bytes_read);
-       sys_run_binary(binary_buf, NULL, total_bytes_read);
+       ssize_t error = sys_run_binary(binary_buf, NULL, total_bytes_read);
+       if(error < 0) {
+               fprintf(stderr, "Error: Unable to run remote binary\n");
+       }
        free(binary_buf);
        close(fd);
+
 }