Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load unless there's a segfault
// 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);
}
/// 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);
}
}
#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
You can’t perform that action at this time.