Add queue overflow check

This commit is contained in:
0x4261756D 2023-07-28 04:55:08 +02:00
parent 09ac457b9d
commit 92f7ec405d
2 changed files with 45 additions and 2 deletions

View File

@ -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<Operation>, functions: &Vec<Func
data.code += "\tmov rdi, -1\n";
data.code += "\tsyscall\n";
}
if data.code.contains("exception_queue_read_out_of_bounds")
{
data.strings += "\texception_queue_oob_msg db \"Queue overflow\", 10\n";
data.code += "exception_queue_read_out_of_bounds:\n";
//TODO: report the passed sizes
data.code += "\tmov rax, 1\n";
data.code += "\tmov rdi, 2\n";
// size
data.code += "\tmov rdx, 37\n";
// data
data.code += "\tmov rsi, exception_queue_oob_msg\n";
data.code += "\tsyscall\n";
data.code += "\tmov rax, 60\n";
data.code += "\tmov rdi, -1\n";
data.code += "\tsyscall\n";
}
return fs::write("out.asm", format!("{}{}{}{}", ASSEMBLY_LINUX_X64_HEADER, data.code, data.arrays, data.strings));
}
@ -452,6 +468,13 @@ fn generate_assembly_linux_x64_array_oob_check(length: i64) -> 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<Operation>, functions: &Vec<Function>, intrinsics: &HashMap<&str, (Vec<Datatype>, Vec<Datatype>)>, arrays: &Vec<Arr>, debug: bool) -> AssemblyData
{
let mut data = AssemblyData
@ -501,6 +524,7 @@ fn generate_assembly_linux_x64_block(operations: &Vec<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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

View File

@ -371,4 +371,4 @@ function int int => int coordToIndex
{
// y x
req 29 req * +
}
}