Watch babby's first rust program... one commit at a time

Working on some ideas to potentially replace WaterboxHost
This commit is contained in:
nattthebear 2020-06-19 20:43:09 -04:00
parent 129d454a67
commit ee6c2ba679
6 changed files with 171 additions and 0 deletions

37
waterbox/waterboxhost/Cargo.lock generated Normal file
View File

@ -0,0 +1,37 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "libc"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
[[package]]
name = "waterboxhost"
version = "0.1.0"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -0,0 +1,18 @@
[package]
name = "waterboxhost"
version = "0.1.0"
authors = ["nattthebear <goyuken@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.8", features = ["memoryapi"] }
[target.'cfg(unix)'.dependencies]
libc = "0.2.71"
[lib]
doctest = false

View File

@ -0,0 +1,15 @@
#![crate_type = "cdylib"]
const PAGESIZE: usize = 0x1000;
const PAGEMASK: usize = 0xfff;
const PAGESHIFT: i32 = 12;
mod memory_block;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -0,0 +1 @@
mod snapshot;

View File

@ -0,0 +1,99 @@
use std::ptr::{null_mut, NonNull};
use core::ffi::c_void;
use crate::*;
/// wraps the allocation of a single PAGESIZE bytes of ram, and is safe-ish to call within a signal handler
pub struct Snapshot {
ptr: NonNull<u8>,
}
impl Snapshot {
pub fn new() -> Snapshot {
unsafe {
let ptr = alloc();
if ptr == null_mut() {
panic!("Snapshot could not allocate memory!");
} else {
Snapshot {
ptr: NonNull::new_unchecked(ptr as *mut u8)
}
}
}
}
pub fn slice<'a>(&'a self) -> &'a [u8] {
unsafe {
std::slice::from_raw_parts(self.ptr.as_ptr(), PAGESIZE)
}
}
pub fn slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
unsafe {
std::slice::from_raw_parts_mut(self.ptr.as_ptr(), PAGESIZE)
}
}
}
impl Drop for Snapshot {
fn drop(&mut self) {
unsafe {
let res = free(self.ptr.as_ptr() as *mut c_void);
if !res {
eprintln!("Snapshot could not free memory!");
}
}
}
}
#[cfg(windows)]
use winapi::um::memoryapi::*;
#[cfg(windows)]
use winapi::um::winnt::*;
#[cfg(windows)]
unsafe fn alloc() -> *mut c_void {
VirtualAlloc(null_mut(), PAGESIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) as *mut c_void
}
#[cfg(windows)]
unsafe fn free(ptr: *mut c_void) -> bool {
match VirtualFree(ptr as *mut winapi::ctypes::c_void, PAGESIZE, MEM_RELEASE) {
0 => false,
_ => true
}
}
#[cfg(unix)]
use libc::*;
#[cfg(unix)]
unsafe fn alloc() -> *mut c_void {
let ptr = mmap(null_mut(), PAGESIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
match ptr {
MAP_FAILED => null_mut(),
_ => ptr
}
}
#[cfg(unix)]
unsafe fn free(ptr: *mut c_void) -> bool {
let res = munmap(ptr, PAGESIZE);
match res {
0 => true,
_ => false
}
}
#[cfg(test)]
#[test]
fn basic_test() {
let mut s = Snapshot::new();
for x in s.slice().iter() {
assert!(*x == 0);
}
let ml = s.slice_mut();
for i in 0..PAGESIZE {
ml[i] = i as u8;
}
let sl = s.slice();
for i in 0..PAGESIZE {
assert!(sl[i] == i as u8);
}
}

View File

@ -0,0 +1 @@
{"rustc_fingerprint":11446813852324310650,"outputs":{"1164083562126845933":["rustc 1.43.0\nbinary: rustc\ncommit-hash: unknown\ncommit-date: unknown\nhost: x86_64-pc-windows-gnu\nrelease: 1.43.0\nLLVM version: 10.0\n",""],"4476964694761187371":["___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Develop\\msys64\\mingw64\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"windows\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n",""]},"successes":{}}