From 0f9fe1288f226b2c78c9ed000be74ccb678ffd3c Mon Sep 17 00:00:00 2001 From: Johannes Maier Date: Fri, 5 Jan 2024 17:51:01 +0100 Subject: [PATCH] Add read of input program. --- .clang-format | 2 +- .gitignore | 1 - vuln.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/.clang-format b/.clang-format index 1c31383..a8a9459 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,3 @@ BasedOnStyle: LLVM IndentWidth: 4 -ColumnLimit: 200 \ No newline at end of file +ColumnLimit: 200 diff --git a/.gitignore b/.gitignore index 1d18c39..2f58603 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,3 @@ build /.idea /.vscode - diff --git a/vuln.c b/vuln.c index f26b97c..9fcfc78 100644 --- a/vuln.c +++ b/vuln.c @@ -1,6 +1,69 @@ +#include +#include #include +#include + +#define MAX_PROGRAM_LEN 0x10000 + +typedef enum Instruction : uint8_t { TODO } Instruction; + +Instruction *get_program(size_t *program_len) { + puts("Now to your next program: How long should it bee?"); + + size_t len; + char len_buf[0x10]; + char *end_ptr; + do { + if (fgets(len_buf, sizeof(len_buf), stdin) == NULL) { + exit(EXIT_FAILURE); + } + len = strtoull(len_buf, &end_ptr, 0); + + if (len_buf == end_ptr) { + puts("That's not a integer, come back when you passed elementary school!"); + exit(EXIT_FAILURE); + } + + if (len <= MAX_PROGRAM_LEN) { + break; + } + + puts("Nah, that's to long. Let's try again."); + + } while (true); + + Instruction *program = malloc(len * sizeof(Instruction)); + + if (program == NULL) { + exit(EXIT_FAILURE); + } + + if (fread(program, sizeof(Instruction), len, stdin) != len) { + puts("You did not enter as many instructions as you wanted. Learn counting, idiot!"); + free(program); + exit(EXIT_FAILURE); + } + + *program_len = len; + return program; +} + +int run_jit(Instruction *program, size_t len) { return 0; } int main() { - printf("Hello, World!\n"); - return 0; + // TODO: better pun, add reference to pop-culture + puts("Welcome to JIT-aaS (Just In Time - always a Surprise)"); + + Instruction *program; + size_t program_len; + int exit_code; + + while (true) { + program = get_program(&program_len); + + exit_code = run_jit(program, program_len); + + printf("Your program exited with %d\n", exit_code); + free(program); + } }