From a8a5900e8c0ec5ca7f71a82e0aca042168f7be9f Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 18 May 2006 19:02:35 +0000 Subject: [PATCH] Randomize the bits that bitmap_scan() returns, to make student errors that assume contiguous memory allocation show up. --- TODO | 5 ----- src/lib/kernel/bitmap.c | 18 ++++++++++++------ src/threads/init.c | 6 +++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/TODO b/TODO index a4e3e31..c493bce 100644 --- a/TODO +++ b/TODO @@ -7,11 +7,6 @@ that span multiple pages, where some are mapped and some are not. An implementation that only checks the first page, rather than all pages that can be touched during a call to read()/write() passes all tests. -- In Project 2, we're missing a test that would fail if they assumed -that contiguous user-virtual addresses are laid out contiguously -in memory. The loading code should ensure that non-contiguous -physical pages are allocated for the data segment (at least.) - - Need some tests that test that illegal accesses lead to process termination. I have written some, will add them. In P2, obviously, this would require that the students break this functionality since diff --git a/src/lib/kernel/bitmap.c b/src/lib/kernel/bitmap.c index d323b89..77edf22 100644 --- a/src/lib/kernel/bitmap.c +++ b/src/lib/kernel/bitmap.c @@ -1,6 +1,7 @@ #include "bitmap.h" #include #include +#include #include #include #include "threads/malloc.h" @@ -289,7 +290,7 @@ bitmap_all (const struct bitmap *b, size_t start, size_t cnt) /* Finding set or unset bits. */ -/* Finds and returns the starting index of the first group of CNT +/* Finds and returns the starting index of a group of CNT consecutive bits in B at or after START that are all set to VALUE. If there is no such group, returns BITMAP_ERROR. */ @@ -302,15 +303,20 @@ bitmap_scan (const struct bitmap *b, size_t start, size_t cnt, bool value) if (cnt <= b->bit_cnt) { size_t last = b->bit_cnt - cnt; - size_t i; - for (i = start; i <= last; i++) - if (!bitmap_contains (b, i, cnt, !value)) - return i; + size_t middle = start + random_ulong () % (last - start + 1); + size_t i = middle; + do + { + if (!bitmap_contains (b, i, cnt, !value)) + return i; + i = i != last ? i + 1 : start; + } + while (i != middle); } return BITMAP_ERROR; } -/* Finds the first group of CNT consecutive bits in B at or after +/* Finds a group of CNT consecutive bits in B at or after START that are all set to VALUE, flips them all to !VALUE, and returns the index of the first bit in the group. If there is no such group, returns BITMAP_ERROR. diff --git a/src/threads/init.c b/src/threads/init.c index 57797be..ef82cb1 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -90,6 +90,9 @@ main (void) argv = read_command_line (); argv = parse_options (argv); + /* Set random seed if parse_options() didn't. */ + random_init (0); + /* Initialize memory system. */ palloc_init (); malloc_init (); @@ -101,9 +104,6 @@ main (void) gdt_init (); #endif - /* Set random seed if parse_options() didn't. */ - random_init (0); - /* Initialize interrupt handlers. */ intr_init (); timer_init (); -- 2.30.2