Created
October 17, 2023 18:22
-
-
Save 0e4ef622/966c059ce00e4b3253d89f780318ef8d to your computer and use it in GitHub Desktop.
funny oort shenanigans
This file contains 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
// Tutorial: Squadron | |
// Destroy the enemy ships. They now shoot back. | |
use oort_api::prelude::*; | |
const BUF_SIZE: usize = 1024; | |
// adapted from https://github.com/rust-lang/rust/issues/114936 | |
fn hack() | |
-> impl for<'s> FnOnce(&'s mut [u8; BUF_SIZE]) -> ( | |
[&'static &'s (); 0], | |
&'static mut [u8; BUF_SIZE], | |
) | |
{ | |
|s: &mut [u8; BUF_SIZE]| ([], s) | |
} | |
fn funny() -> &'static mut [u8; BUF_SIZE] | |
{ | |
let f = hack(); | |
let mut local = Box::new([0; BUF_SIZE]); | |
let s: &'static mut [u8; BUF_SIZE] = f(&mut local).1; // <- should be rejected! | |
drop(local); | |
s | |
} | |
pub struct Ship { | |
shared: Option<&'static mut [u8; BUF_SIZE]>, | |
master: bool, | |
leaked: usize, | |
} | |
impl Ship { | |
pub fn new() -> Ship { | |
let master = position().y == 0.0; | |
Ship { shared: None, master, leaked: 0 } | |
} | |
pub fn tick(&mut self) { | |
if current_tick() == 1 { | |
// Get address of shared memory | |
self.shared = Some(funny()); | |
} | |
if current_tick() == 2 { | |
// Try to leak shared memory so something else doesn't use it | |
self.leaked = Box::leak(Box::new([0u8; BUF_SIZE])) as *const _ as usize; | |
} | |
if current_tick() > 5 { | |
debug!("leaked: {:x}", self.leaked); | |
debug!("shared addr: {:x?}", self.shared.as_mut().map(|b| b.as_ptr())); | |
if self.master { | |
debug!("i am the master"); | |
let msg = if current_tick() % 120 < 60 { | |
[69, 42] | |
} else { | |
[42, 69] | |
}; | |
self.shared.as_mut().unwrap()[..2].copy_from_slice(&msg); | |
} else { | |
debug!("read from master"); | |
debug!("{:?}", self.shared); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment