Created
April 7, 2020 21:26
-
-
Save porglezomp/b0bdcc978a500af9a6a5084703403ded to your computer and use it in GitHub Desktop.
Load unless there's a segfault
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc example.c try_load.c | |
#include "try_load.h" | |
#include <stdio.h> | |
void try_to_load(const uint8_t *address) { | |
uint8_t value; | |
if (try_load_u8(address, &value)) { | |
printf("Successfully load from address %p: %d\n", address, value); | |
} else { | |
printf("Failed to load from address %p\n", address); | |
} | |
} | |
int main() { | |
uint8_t data[] = {0, 1, 2, 3}; | |
try_to_load((uint8_t*)0x123123); | |
try_to_load(data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Erika @rrika9 https://twitter.com/rrika9/status/1247491367686807552 | |
/// it'd be nice if userspace had something like "load_or_branch_on_segfault" | |
#include "try_load.h" | |
#include <stdlib.h> | |
#include <unistd.h> | |
enum Pipes {READ, WRITE}; | |
bool try_load_u8(const uint8_t *address, uint8_t *value) { | |
int pipes[2]; | |
pipe(pipes); | |
if (fork()) { | |
close(pipes[WRITE]); | |
ssize_t read_amount = read(pipes[READ], value, 1); | |
if (read_amount <= 0) { return false; } | |
close(pipes[READ]); | |
return true; | |
} else { | |
close(pipes[READ]); | |
write(pipes[WRITE], address, 1); | |
close(pipes[WRITE]); | |
exit(0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <stdint.h> | |
#include <stdbool.h> | |
/// Returns true on success, false on failure. | |
bool try_load_u8(const uint8_t *address, uint8_t *value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment