From 92f7ec405da5a0d348bbe59e9cef72f3d55197fd Mon Sep 17 00:00:00 2001 From: 0x4261756D <38735823+0x4261756D@users.noreply.github.com> Date: Fri, 28 Jul 2023 04:55:08 +0200 Subject: [PATCH] Add queue overflow check --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++- tests/conway_diehard.qbl | 2 +- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8572fe9..7df9701 100644 --- a/src/main.rs +++ b/src/main.rs @@ -357,7 +357,7 @@ fn merge_assemblies(data: &mut AssemblyData, data2: AssemblyData) data.strings += data2.strings.as_str(); } -const ASSEMBLY_LINUX_X64_QUEUE_LENGTH: u32 = 4096; +const ASSEMBLY_LINUX_X64_QUEUE_LENGTH: u32 = 1024; const ASSEMBLY_LINUX_X64_HEADER: &str = "format ELF64 executable 3\n"; const ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE: &str = "\tcmp r12, r13\n\tcmove r12, r14\n\tcmove r13, r14\n"; const ASSEMBLY_LINUX_X64_EXIT: &str = "\tmov rax, 60\n\tmov rdi, 0\n\tsyscall\n"; @@ -436,6 +436,22 @@ fn generate_assembly_linux_x64(operations: &Vec, functions: &Vec String return data.clone(); } +fn generate_assembly_linux_x64_queue_oob_check() -> String +{ + return "\t\t;;Queue bounds check\n".to_string() + + format!("\tcmp qword r13, {}\n", ASSEMBLY_LINUX_X64_QUEUE_LENGTH).as_str() + + "\tjge exception_queue_read_out_of_bounds\n\t\t;;Queue bounds over\n"; +} + fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Vec, intrinsics: &HashMap<&str, (Vec, Vec)>, arrays: &Vec, debug: bool) -> AssemblyData { let mut data = AssemblyData @@ -501,6 +524,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve } } data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } Operation::Requeue(line, col) => { @@ -510,6 +534,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } Operation::Swap(line, col) => { @@ -521,6 +546,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov [queue+8*r13], rbx\n"; data.code += "\tmov [queue+8*r13+8], rax\n"; data.code += "\tadd r13, 2\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } Operation::While(while_operations, line, col) => { @@ -561,6 +587,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov rax, [queue+8*r12]\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } Operation::Intrinsic(name, line, col) => { @@ -616,6 +643,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tcall intToStr\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "-" => { @@ -627,6 +655,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tsub rax, rbx\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "+" => { @@ -638,6 +667,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tadd rax, rbx\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "*" => { @@ -649,6 +679,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmul rbx\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "divmod" => { @@ -661,8 +692,10 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tidiv rbx\n"; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); data.code += "\tmov [queue+8*r13], rdx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } ">" => { @@ -675,6 +708,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "<" => { @@ -687,6 +721,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } ">=" => { @@ -699,6 +734,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "<=" => { @@ -711,6 +747,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "==" => { @@ -723,6 +760,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "!=" => { @@ -735,6 +773,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "&&" => { @@ -745,6 +784,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } _ => todo!("intrinsic {} {}:{}", name, line, col) } @@ -764,6 +804,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += format!("\tmov qword rbx, [arr_{}+8*rax]\n", name).as_str(); data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } "write" => { @@ -780,6 +821,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve { data.code += format!("\tmov qword [queue+8*r13], {}\n", array.length).as_str(); data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } _ => todo!("apply {}", word) } @@ -795,6 +837,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; + data.code += generate_assembly_linux_x64_queue_oob_check().as_str(); } data.code += "\t;; move pointers\n"; // save the current base diff --git a/tests/conway_diehard.qbl b/tests/conway_diehard.qbl index 6516274..ef6c89f 100644 --- a/tests/conway_diehard.qbl +++ b/tests/conway_diehard.qbl @@ -371,4 +371,4 @@ function int int => int coordToIndex { // y x req 29 req * + -} \ No newline at end of file +}