diff --git a/genx.sh b/genx.sh index aa11527..a1a9bd4 100755 --- a/genx.sh +++ b/genx.sh @@ -26,6 +26,7 @@ cToBuild=() sToInclude+=("library/alpha_lib_reg.s") sToInclude+=("library/alpha_driver.s") sToInclude+=("library/std.s") +sToInclude+=("library/boundscheck.s") filename="" s_name="" diff --git a/library/boundscheck.c b/library/boundscheck.c new file mode 100644 index 0000000..5e6081c --- /dev/null +++ b/library/boundscheck.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int BoundsError(int d){ + printf("You are trying to access memory that isn't yours.\n"); + exit(1); +} + +void BoundsCheck(int argc, void * start, ...){ + va_list argptr; + va_start(argptr, start); + int index = 0; + while (argc){ + argc--; + index = va_arg(argptr, int); + if(index > *(int*)start){ + BoundsError(index); + } + start += sizeof(int); + } + va_end(argptr); +} \ No newline at end of file diff --git a/library/boundscheck.s b/library/boundscheck.s new file mode 100644 index 0000000..36fa414 --- /dev/null +++ b/library/boundscheck.s @@ -0,0 +1,112 @@ + .file "boundscheck.c" + .text + .section .rodata + .align 8 +.LC0: + .string "You are trying to access memory that isn't yours." + .text + .globl BoundsError + .type BoundsError, @function +BoundsError: +.LFB6: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $16, %rsp + movl %edi, -4(%rbp) + leaq .LC0(%rip), %rax + movq %rax, %rdi + call puts@PLT + movl $1, %edi + call exit@PLT + .cfi_endproc +.LFE6: + .size BoundsError, .-BoundsError + .globl BoundsCheck + .type BoundsCheck, @function +BoundsCheck: +.LFB7: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $240, %rsp + movl %edi, -228(%rbp) + movq %rsi, -240(%rbp) + movq %rdx, -160(%rbp) + movq %rcx, -152(%rbp) + movq %r8, -144(%rbp) + movq %r9, -136(%rbp) + testb %al, %al + je .L3 + movaps %xmm0, -128(%rbp) + movaps %xmm1, -112(%rbp) + movaps %xmm2, -96(%rbp) + movaps %xmm3, -80(%rbp) + movaps %xmm4, -64(%rbp) + movaps %xmm5, -48(%rbp) + movaps %xmm6, -32(%rbp) + movaps %xmm7, -16(%rbp) +.L3: + movq %fs:40, %rax + movq %rax, -184(%rbp) + xorl %eax, %eax + movl $16, -208(%rbp) + movl $48, -204(%rbp) + leaq 16(%rbp), %rax + movq %rax, -200(%rbp) + leaq -176(%rbp), %rax + movq %rax, -192(%rbp) + movl $0, -212(%rbp) + jmp .L4 +.L8: + subl $1, -228(%rbp) + movl -208(%rbp), %eax + cmpl $47, %eax + ja .L5 + movq -192(%rbp), %rax + movl -208(%rbp), %edx + movl %edx, %edx + addq %rdx, %rax + movl -208(%rbp), %edx + addl $8, %edx + movl %edx, -208(%rbp) + jmp .L6 +.L5: + movq -200(%rbp), %rax + leaq 8(%rax), %rdx + movq %rdx, -200(%rbp) +.L6: + movl (%rax), %eax + movl %eax, -212(%rbp) + movq -240(%rbp), %rax + movl (%rax), %eax + cmpl %eax, -212(%rbp) + jle .L7 + movl -212(%rbp), %eax + movl %eax, %edi + call BoundsError +.L7: + addq $4, -240(%rbp) +.L4: + cmpl $0, -228(%rbp) + jne .L8 + nop + movq -184(%rbp), %rax + subq %fs:40, %rax + je .L9 + call __stack_chk_fail@PLT +.L9: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE7: + .size BoundsCheck, .-BoundsCheck + .ident "GCC: (GNU) 14.2.1 20250207" + .section .note.GNU-stack,"",@progbits