wip: only center guard on fault in it

This commit is contained in:
Matthew LeVan 2024-02-05 13:25:42 -05:00
parent 0a8bcfe16f
commit fdc3010169
3 changed files with 24 additions and 27 deletions

View File

@ -44,12 +44,10 @@ name = "ares"
path = "src/main.rs" path = "src/main.rs"
[profile.dev] [profile.dev]
opt-level = 0 opt-level = 3
debug = true
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 0 opt-level = 3
debug = true
# run with e.g. 'cargo build --features check_forwarding,check_acyclic' # run with e.g. 'cargo build --features check_forwarding,check_acyclic'
[features] [features]

View File

@ -143,7 +143,7 @@ impl Context {
snapshot: Option<Snapshot>, snapshot: Option<Snapshot>,
constant_hot_state: &[HotEntry], constant_hot_state: &[HotEntry],
) -> Self { ) -> Self {
let mut stack = NockStack::new(1024 << 10 << 10, 0); let mut stack = NockStack::new(256 << 10 << 10, 0);
let newt = Newt::new(); let newt = Newt::new();
let cache = Hamt::<Noun>::new(&mut stack); let cache = Hamt::<Noun>::new(&mut stack);

View File

@ -1,6 +1,8 @@
#include <setjmp.h> #include <setjmp.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
@ -38,12 +40,15 @@ _focus_guard()
return guard_weird; return guard_weird;
} }
// Unmark the old guard page. // Unmark the old guard page if one exists.
void *old_guard_p = guard_p; void *old_guard_p = guard_p;
if (old_guard_p != NULL) {
fprintf(stderr, "guard: retiring old guard page\r\n");
if (old_guard_p != 0 if (old_guard_p != 0
&& mprotect(old_guard_p, GD_PAGESIZE, PROT_READ | PROT_WRITE) == -1) { && mprotect(old_guard_p, GD_PAGESIZE, PROT_READ | PROT_WRITE) == -1) {
return guard_armor; return guard_armor;
} }
}
// Place the new guard page in the low-aligned center. // Place the new guard page in the low-aligned center.
guard_p = (uint64_t *)low_p + ((high_p - low_p) / 2); guard_p = (uint64_t *)low_p + ((high_p - low_p) / 2);
@ -51,10 +56,13 @@ _focus_guard()
// Mark the new guard page. // Mark the new guard page.
if (guard_p != old_guard_p) { if (guard_p != old_guard_p) {
fprintf(stderr, "guard: focused guard page\r\n");
if (mprotect(guard_p, GD_PAGESIZE, PROT_NONE) == -1) { if (mprotect(guard_p, GD_PAGESIZE, PROT_NONE) == -1) {
return guard_armor; return guard_armor;
} }
} else { } else {
fprintf(stderr, "guard: spent; exiting\r\n");
exit(1);
return guard_spent; return guard_spent;
} }
@ -81,12 +89,12 @@ _signal_handler(int sig, siginfo_t *si, void *unused)
if (si->si_addr >= (void *)guard_p && if (si->si_addr >= (void *)guard_p &&
si->si_addr < (void *)guard_p + GD_PAGESIZE) si->si_addr < (void *)guard_p + GD_PAGESIZE)
{ {
fprintf(stderr, "guard: fault in guard: %p\r\n", si->si_addr); fprintf(stderr, "guard: hit: %p\r\n", si->si_addr);
err = _focus_guard(); err = _focus_guard();
break; break;
} }
else { else {
fprintf(stderr, "guard: fault outside guard %p\r\n", si->si_addr); fprintf(stderr, "guard: weird hit: %p\r\n", si->si_addr);
if (NULL != prev_sigsegv_handler) { if (NULL != prev_sigsegv_handler) {
prev_sigsegv_handler(sig, si, unused); prev_sigsegv_handler(sig, si, unused);
break; break;
@ -126,7 +134,6 @@ _register_handler()
} }
prev_sigsegv_handler = prev_sa.sa_sigaction; prev_sigsegv_handler = prev_sa.sa_sigaction;
// fprintf(stderr, "guard: registered handler\r\n");
return guard_sound; return guard_sound;
} }
@ -150,17 +157,14 @@ guard(
const uint64_t *low_p = low_f(bounds_data, context_p); const uint64_t *low_p = low_f(bounds_data, context_p);
const uint64_t *high_p = high_f(bounds_data, context_p); const uint64_t *high_p = high_f(bounds_data, context_p);
// fprintf(stderr, "guard: low: %p high: %p\r\n", (void *)low_p, (void *)high_p); if (guard_p == NULL) {
// const unsigned long free_mb = (unsigned long)(high_p - low_p) / 1024 / 1024;
// fprintf(stderr, "guard: free space: %lu MB\r\n", free_mb);
guard_err focus_err = _focus_guard(); guard_err focus_err = _focus_guard();
if (focus_err != guard_sound && focus_err != guard_spent) { if (focus_err != guard_sound && focus_err != guard_spent) {
fprintf(stderr, "guard: failed to install guard page\r\n"); fprintf(stderr, "guard: failed to install guard page\r\n");
err = focus_err; err = focus_err;
goto fail; goto fail;
} }
}
if (_register_handler() != guard_sound) { if (_register_handler() != guard_sound) {
err = guard_weird; err = guard_weird;
@ -179,11 +183,6 @@ guard(
*(void **)ret = result; *(void **)ret = result;
if (mprotect(guard_p, GD_PAGESIZE, PROT_READ | PROT_WRITE) == -1) {
err = guard_armor;
goto fail;
}
return guard_sound; return guard_sound;
fail: fail: