feat: basic flag challenge working + fix elf loading

This commit is contained in:
2026-05-08 00:20:26 +02:00
parent e4b65d544f
commit 5ec7d85b4e
8 changed files with 279 additions and 39 deletions

View File

@@ -15,14 +15,28 @@
int main(){
const int maxninput = 255;
printf("Please enter the flag: ");
fflush(stdout);
char userinput[maxninput];
fgets(userinput, sizeof(userinput), stdin);
// Replace newline with null terminator
userinput[strcspn(userinput, "\r\n")] = 0;
uint8_t memory[EMU_MEM_SIZE]; //4kb of internal memory
tinyriscv_core cpu; //create the emulation core
memset(&cpu, 0, sizeof(cpu));
cpu.mem = memory; //use the uint8_t array as the memory
cpu.mem_size = EMU_MEM_SIZE;
memset(memory, 0, sizeof(memory));
uint8_t real_len = strnlen(userinput, maxninput);
int load_status = load_elf_object_sections(EMU_PROGRAM_PATH, memory, cpu.mem_size);
// Load user data, first, the string length
memory[SECTION_USERDATA] = real_len;
memcpy(memory + SECTION_USERDATA + 1, userinput, real_len);
// Load elf, keep 512 bytes as safe buffer
int load_status = load_elf_object_sections(EMU_PROGRAM_PATH, memory, cpu.mem_size - SIZE_USERDATA);
if (load_status != LOAD_OK) {
fprintf(stderr, "ELF load failed with code %d for '%s'\n", load_status, EMU_PROGRAM_PATH);
fflush(stderr);
@@ -43,9 +57,7 @@ int main(){
tinyriscv_init(&cpu); //initialise core for execution
cpu.pc = tinyriscv_MEM_OFFSET + entry_offset;
// Call operation(a, b) using RV32 calling convention:
// a0=x10, return value in a0, return address in ra=x1.
cpu.x[10] = TEST_A;
cpu.x[10] = tinyriscv_MEM_OFFSET + SECTION_USERDATA;
cpu.x[1] = tinyriscv_MEM_OFFSET + cpu.mem_size;
uint32_t steps = 0u;
@@ -59,15 +71,12 @@ int main(){
return 2;
}
uint32_t actual = (uint32_t)cpu.x[10];
uint32_t expected = ((TEST_A << 3) ^ 0xFF) >> 3;
printf("operation(%u) -> %u (expected %u)\n",
(unsigned)TEST_A,
(unsigned)actual, (unsigned)expected);
uint32_t result = (uint32_t)cpu.x[10];
if (actual != expected) {
fprintf(stderr, "Verification failed\n");
return 3;
if(result == 1) {
printf("Congrats! That's the flag!");
} else {
printf("Nope, sorry, that's incorrect!");
}
return 0;