diff --git a/src/machine-vuln1.c b/src/machine-vuln1.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine-vuln1.c +++ b/src/machine-vuln1.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; } diff --git a/src/machine-vuln2.c b/src/machine-vuln2.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine-vuln2.c +++ b/src/machine-vuln2.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; } diff --git a/src/machine-vuln3.c b/src/machine-vuln3.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine-vuln3.c +++ b/src/machine-vuln3.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; } diff --git a/src/machine-vuln4.c b/src/machine-vuln4.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine-vuln4.c +++ b/src/machine-vuln4.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; } diff --git a/src/machine-vuln5.c b/src/machine-vuln5.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine-vuln5.c +++ b/src/machine-vuln5.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; } diff --git a/src/machine.c b/src/machine.c index 46e79b602159e31fb170a9aab356db42057e2ecc..916c9f6ed43469855e6f477af7110dec840c9726 100644 --- a/src/machine.c +++ b/src/machine.c @@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz"; /* we force building with -fwrapv to ensure that signed overflow is defined to wrap around */ -int32_t memory[MEMORY_SIZE]; -int32_t regs[NUM_REGS]; +/* we dynamically allocate memory and regs since AdddressSanitizer appears + * to more reliably catch out of bounds memory accesses for heap allocated + * buffers than for global variables */ +int32_t * memory = NULL; +int32_t * regs = NULL; unsigned int count = 0; /* counts number of instructions executed so far */ static void machine_init(void){ - memset(memory,0,sizeof(memory)); - memset(regs,0,sizeof(regs)); + memory = malloc(sizeof(int32_t)*MEMORY_SIZE); + regs = malloc(sizeof(int32_t)*NUM_REGS); + memset(memory,0,sizeof(int32_t)*MEMORY_SIZE); + memset(regs,0,sizeof(int32_t)*NUM_REGS); count = 0; } +static void machine_free(void){ + free(memory); + free(regs); +} + static void do_add(unsigned int dest, unsigned int src1, unsigned int src2) { regs[dest] = regs[src1] + regs[src2]; @@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){ const int res = read_program(filename); if (res < 0){ fprintf(stderr,"Error reading program from %s\n",filename); + machine_free(); return 1; } @@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){ int32_t result; if (execute(numLines,cycles,&result) != 0){ fprintf(stderr,"Program %s didn't execute successfully!\n",filename); + machine_free(); return 1; } printf("Program %s executed successfully. Result: %d\n",filename,result); + machine_free(); return 0; }