Select Git revision
machine-vuln5.c
Forked from
Toby Murray / swen90006-a2-2018
Source project has a limited visibility.
machine-vuln5.c 15.42 KiB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include "debug.h"
/** add rd rs1 rs2 =~ rd = rs1 + rs2 */
const char INSTRUCTION_ADD[] = "add";
/** sub rd rs1 rs2 =~ rd = rs1 - rs2 */
const char INSTRUCTION_SUBTRACT[] = "sub";
/** mul rd rs1 rs2 =~ rd = rs1 * rs2 */
const char INSTRUCTION_MULT[] = "mul";
/** div rd rs1 rs2 =~ rd = rs1 / rs2 */
const char INSTRUCTION_DIVIDE[] = "div";
/** ret rs =~ return rs */
const char INSTRUCTION_RETURN[] = "ret";
/** ldr rd rs offs =~ rd = rs[offs] */
const char INSTRUCTION_LOAD[] = "ldr";
/** str ra offs rb =~ ra[offs] = rb */
const char INSTRUCTION_STORE[] = "str";
/** mov rd val =~ rd = val */
const char INSTRUCTION_MOVE[] = "mov";
/** jmp offs =~ pc = pc + offs */
const char INSTRUCTION_JUMP[] = "jmp";
/** jz ra offs =~ if (ra == 0) pc = pc + offs else pc = pc + 1 */
const char INSTRUCTION_JZ[] = "jz";
#define NUM_REGS 32
#define MAX_REG (NUM_REGS - 1)
#define MEMORY_SIZE 65536 /* 4 x as much memory as a 64 */
#define MAX_ADDR (MEMORY_SIZE-1)
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
/* 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){
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)