Added bountry checking on memory access to ensure out of bounds access does not occur
This commit is contained in:
parent
1fa2f125f3
commit
bfde853549
2
vm.c
2
vm.c
|
@ -44,7 +44,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
/* Perform all the essential stages in order */
|
/* Perform all the essential stages in order */
|
||||||
struct lilith* vm;
|
struct lilith* vm;
|
||||||
vm = create_vm(1 << 22);
|
vm = create_vm(1 << 20);
|
||||||
load_program(vm, argv);
|
load_program(vm, argv);
|
||||||
execute_vm(vm);
|
execute_vm(vm);
|
||||||
destroy_vm(vm);
|
destroy_vm(vm);
|
||||||
|
|
2
vm.h
2
vm.h
|
@ -8,6 +8,7 @@
|
||||||
struct lilith
|
struct lilith
|
||||||
{
|
{
|
||||||
uint8_t *memory;
|
uint8_t *memory;
|
||||||
|
size_t amount_of_Ram;
|
||||||
uint32_t reg[16];
|
uint32_t reg[16];
|
||||||
uint32_t ip;
|
uint32_t ip;
|
||||||
bool halted;
|
bool halted;
|
||||||
|
@ -247,3 +248,4 @@ struct lilith* create_vm(size_t size);
|
||||||
void destroy_vm(struct lilith* vm);
|
void destroy_vm(struct lilith* vm);
|
||||||
void read_instruction(struct lilith* vm, struct Instruction *current);
|
void read_instruction(struct lilith* vm, struct Instruction *current);
|
||||||
void eval_instruction(struct lilith* vm, struct Instruction* current);
|
void eval_instruction(struct lilith* vm, struct Instruction* current);
|
||||||
|
void outside_of_world(struct lilith* vm, uint32_t place, char* message);
|
||||||
|
|
16
vm_decode.c
16
vm_decode.c
|
@ -13,6 +13,7 @@ struct lilith* create_vm(size_t size)
|
||||||
struct lilith* vm;
|
struct lilith* vm;
|
||||||
vm = calloc(1, sizeof(struct lilith));
|
vm = calloc(1, sizeof(struct lilith));
|
||||||
vm->memory = calloc(size, sizeof(uint8_t));
|
vm->memory = calloc(size, sizeof(uint8_t));
|
||||||
|
vm->amount_of_Ram = size;
|
||||||
vm->halted = false;
|
vm->halted = false;
|
||||||
vm->exception = false;
|
vm->exception = false;
|
||||||
return vm;
|
return vm;
|
||||||
|
@ -25,6 +26,19 @@ void destroy_vm(struct lilith* vm)
|
||||||
free(vm);
|
free(vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Validate Memory Addresses*/
|
||||||
|
void outside_of_world(struct lilith* vm, uint32_t place, char* message)
|
||||||
|
{
|
||||||
|
if(vm->amount_of_Ram <= place)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Invalid state reached after: %i instructions\n", performance_counter);
|
||||||
|
fprintf(stderr, "%i: %s\n", place, message);
|
||||||
|
vm->halted = true;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/* Deal with 4OP */
|
/* Deal with 4OP */
|
||||||
void decode_4OP(struct Instruction* c)
|
void decode_4OP(struct Instruction* c)
|
||||||
{
|
{
|
||||||
|
@ -148,6 +162,8 @@ void read_instruction(struct lilith* vm, struct Instruction *current)
|
||||||
/* Store IP for debugging */
|
/* Store IP for debugging */
|
||||||
current->ip = vm->ip;
|
current->ip = vm->ip;
|
||||||
|
|
||||||
|
outside_of_world(vm, current->ip, "Instruction outside of World");
|
||||||
|
|
||||||
/* Read the actual bytes and increment the IP */
|
/* Read the actual bytes and increment the IP */
|
||||||
current->raw0 = (uint8_t)vm->memory[vm->ip];
|
current->raw0 = (uint8_t)vm->memory[vm->ip];
|
||||||
vm->ip = vm->ip + 1;
|
vm->ip = vm->ip + 1;
|
||||||
|
|
|
@ -19,6 +19,8 @@ void writeout_Reg(struct lilith* vm, uint32_t p, uint32_t value)
|
||||||
tmp = tmp/0x100;
|
tmp = tmp/0x100;
|
||||||
raw0 = tmp%0x100;
|
raw0 = tmp%0x100;
|
||||||
|
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
|
|
||||||
vm->memory[p] = raw0;
|
vm->memory[p] = raw0;
|
||||||
vm->memory[p + 1] = raw1;
|
vm->memory[p + 1] = raw1;
|
||||||
vm->memory[p + 2] = raw2;
|
vm->memory[p + 2] = raw2;
|
||||||
|
@ -28,6 +30,8 @@ void writeout_Reg(struct lilith* vm, uint32_t p, uint32_t value)
|
||||||
/* Allow the use of native data format for Register operations */
|
/* Allow the use of native data format for Register operations */
|
||||||
uint32_t readin_Reg(struct lilith* vm, uint32_t p)
|
uint32_t readin_Reg(struct lilith* vm, uint32_t p)
|
||||||
{
|
{
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
|
|
||||||
uint8_t raw0, raw1, raw2, raw3;
|
uint8_t raw0, raw1, raw2, raw3;
|
||||||
uint32_t sum;
|
uint32_t sum;
|
||||||
raw0 = vm->memory[p];
|
raw0 = vm->memory[p];
|
||||||
|
@ -46,12 +50,14 @@ uint32_t readin_Reg(struct lilith* vm, uint32_t p)
|
||||||
/* Unify byte write functionality */
|
/* Unify byte write functionality */
|
||||||
void writeout_byte(struct lilith* vm, uint32_t p, uint32_t value)
|
void writeout_byte(struct lilith* vm, uint32_t p, uint32_t value)
|
||||||
{
|
{
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
vm->memory[p] = (uint8_t)(value%0x100);
|
vm->memory[p] = (uint8_t)(value%0x100);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unify byte read functionality*/
|
/* Unify byte read functionality*/
|
||||||
uint32_t readin_byte(struct lilith* vm, uint32_t p, bool Signed)
|
uint32_t readin_byte(struct lilith* vm, uint32_t p, bool Signed)
|
||||||
{
|
{
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
if(Signed)
|
if(Signed)
|
||||||
{
|
{
|
||||||
int32_t raw0;
|
int32_t raw0;
|
||||||
|
@ -72,6 +78,8 @@ void writeout_doublebyte(struct lilith* vm, uint32_t p, uint32_t value)
|
||||||
utmp = utmp/0x100;
|
utmp = utmp/0x100;
|
||||||
uraw0 = utmp%0x100;
|
uraw0 = utmp%0x100;
|
||||||
|
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
|
|
||||||
vm->memory[p] = uraw0;
|
vm->memory[p] = uraw0;
|
||||||
vm->memory[p + 1] = uraw1;
|
vm->memory[p + 1] = uraw1;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +87,8 @@ void writeout_doublebyte(struct lilith* vm, uint32_t p, uint32_t value)
|
||||||
/* Unify doublebyte read functionality*/
|
/* Unify doublebyte read functionality*/
|
||||||
uint32_t readin_doublebyte(struct lilith* vm, uint32_t p, bool Signed)
|
uint32_t readin_doublebyte(struct lilith* vm, uint32_t p, bool Signed)
|
||||||
{
|
{
|
||||||
|
outside_of_world(vm, p, "Address is outside of World");
|
||||||
|
|
||||||
if(Signed)
|
if(Signed)
|
||||||
{
|
{
|
||||||
int8_t raw0, raw1;
|
int8_t raw0, raw1;
|
||||||
|
|
Loading…
Reference in New Issue