Compare commits

..
18 changed files with 366 additions and 188 deletions

No files matched your search

+142 -85
View File
@@ -1,8 +1,6 @@
use std::{
cell::{Cell, RefCell},
ops::Deref,
ptr,
ptr::null_mut,
ptr::NonNull,
sync::{
Arc,
atomic::{AtomicPtr, Ordering},
@@ -10,119 +8,178 @@ use std::{
};
use super::Config;
use crate::Result;
use crate::{Result, SyncMutex};
/// The configuration manager is an indirection to reload the configuration for
/// the server while it is running. In order to not burden or clutter the many
/// callsites which query for configuration items, this object implements Deref
/// for the actively loaded configuration.
///
/// Every installed `Arc<Config>` is kept at a stable heap address in `retired`
/// for the entire lifetime of `Manager` (intentionally retaining prior configs
/// across the rare, admin-triggered reloads until shutdown). Because
/// `Deref::deref(&self)` ties the returned `&Arc<Config>` lifetime to `&self`,
/// readers can perform a single `Acquire` load with no locking, no TLS, and no
/// risk of dangling or aliasing UB across reloads.
pub struct Manager {
active: AtomicPtr<Config>,
active: AtomicPtr<Arc<Config>>,
/// Append-only owner of every `Box<Arc<Config>>` ever published to
/// `active` (including the currently active config). Entries are only
/// freed when `Manager` itself is dropped (`&mut self`), guaranteeing that
/// any `&Arc<Config>` returned by `Deref::deref` remains valid and
/// immutable for the lifetime of `self`.
retired: SyncMutex<Vec<PinnedConfig>>,
}
thread_local! {
static INDEX: Cell<usize> = 0.into();
static HANDLE: RefCell<Handles> = const {
RefCell::new([const { None }; HISTORY])
};
/// Heap-pinned `Arc<Config>` stored as a raw `NonNull` rather than `Box` so
/// moving it (e.g., when `Vec` reallocates) never retags or invalidates raw
/// pointers previously published to `Manager::active`.
struct PinnedConfig(NonNull<Arc<Config>>);
// SAFETY: `Arc<Config>` is `Send + Sync`, and `PinnedConfig` uniquely owns the
// `Box<Arc<Config>>` allocation until it is dropped in `Manager::drop`.
unsafe impl Send for PinnedConfig {}
impl PinnedConfig {
fn new(config: Config) -> Self {
let ptr = Box::into_raw(Box::new(Arc::new(config)));
Self(NonNull::new(ptr).expect("Box::into_raw returned null"))
}
#[inline]
const fn as_ptr(&self) -> *mut Arc<Config> { self.0.as_ptr() }
}
type Handle = Option<Arc<Config>>;
type Handles = [Handle; HISTORY];
const HISTORY: usize = 8;
impl Drop for PinnedConfig {
fn drop(&mut self) {
// SAFETY: `self.0` was created via `Box::into_raw` in
// `PinnedConfig::new` and is only dropped when `Manager` itself is
// dropped (`&mut self`), after all borrows derived from `Deref::deref`
// have ended.
unsafe {
drop(Box::from_raw(self.0.as_ptr()));
}
}
}
impl Manager {
pub(crate) fn new(config: Config) -> Self {
let config = Arc::new(config);
let pinned = PinnedConfig::new(config);
let ptr = pinned.as_ptr();
Self {
active: AtomicPtr::new(Arc::into_raw(config).cast_mut()),
active: AtomicPtr::new(ptr),
retired: SyncMutex::new(vec![pinned]),
}
}
/// Update the active configuration, returning prior configuration.
#[tracing::instrument(skip_all, level = "info")]
pub fn update(&self, config: Config) -> Result<Arc<Config>> {
let config = Arc::new(config);
let new = Arc::into_raw(config);
let old = self.active.swap(new.cast_mut(), Ordering::AcqRel);
let pinned = PinnedConfig::new(config);
let new_ptr = pinned.as_ptr();
// SAFETY: The old active pointer was set using an Arc::into_raw().
// We're obliged to reconstitute that into Arc otherwise it will leak.
Ok(unsafe { Arc::from_raw(old) })
}
let mut retired = self.retired.lock();
// Push before publishing to `active` so ownership is recorded even if
// `Vec::push` were to unwind.
retired.push(pinned);
fn load(&self, handle: &mut [Option<Arc<Config>>]) -> &'static Arc<Config> {
let config = self.active.load(Ordering::Acquire);
// `Release` publishes the initialized `Box<Arc<Config>>` to readers
// performing `Acquire` loads in `Deref::deref`.
let old_ptr = self.active.swap(new_ptr, Ordering::AcqRel);
// Branch taken after config reload or first access by this thread.
if handle[INDEX.get()]
.as_ref()
.is_none_or(|handle| !ptr::eq(config, Arc::as_ptr(handle)))
{
INDEX.set(INDEX.get().wrapping_add(1).wrapping_rem(HISTORY));
return load_miss(handle, INDEX.get(), config);
}
// SAFETY: `old_ptr` points to a `PinnedConfig` still owned by `retired`
// and cannot be freed until `Manager` is dropped.
let old = unsafe { Arc::clone(&*old_ptr) };
let config: &Arc<Config> = handle[INDEX.get()]
.as_ref()
.expect("handle was already cached for this thread");
// SAFETY: The caller should not hold multiple references at a time
// directly into Config, as a subsequent reference might invalidate
// the thread's cache causing another reference to dangle.
//
// This is a highly unusual pattern as most config values are copied by
// value or used immediately without running overlap with another
// value. Even if it does actually occur somewhere, the window of
// danger is limited to the config being reloaded while the reference
// is held and another access is made by the same thread into a
// different config value. This is mitigated by creating a buffer
// of old configs rather than discarding at the earliest opportunity;
// the odds of this scenario are thus astronomical.
unsafe { std::mem::transmute(config) }
}
}
impl Drop for Manager {
fn drop(&mut self) {
let config = self.active.swap(null_mut(), Ordering::AcqRel);
// SAFETY: The active pointer was set using an Arc::into_raw(). We're
// obliged to reconstitute that into Arc otherwise it will leak.
unsafe { Arc::from_raw(config) };
Ok(old)
}
}
impl Deref for Manager {
type Target = Arc<Config>;
fn deref(&self) -> &Self::Target { HANDLE.with_borrow_mut(|handle| self.load(handle)) }
#[inline]
fn deref(&self) -> &Self::Target {
let ptr = self.active.load(Ordering::Acquire);
// SAFETY: `ptr` was initialized in `Manager::new` or published via
// `Ordering::AcqRel` in `Manager::update` and points to a
// `PinnedConfig` in `self.retired`. Entries in `self.retired` are never
// removed or mutated until `Manager` is dropped (`&mut self`), so the
// returned `&'self Arc<Config>` remains valid and immutable for the
// entire borrow of `self`.
unsafe { &*ptr }
}
}
#[tracing::instrument(
name = "miss",
level = "trace",
skip_all,
fields(%index, ?config)
)]
#[allow(clippy::transmute_ptr_to_ptr)]
fn load_miss(
handle: &mut [Option<Arc<Config>>],
index: usize,
config: *const Config,
) -> &'static Arc<Config> {
// SAFETY: The active pointer was set prior and always remains valid. We're
// reconstituting the Arc here but as a new reference, so the count is
// incremented. This instance will be cached in the thread-local.
let config = unsafe {
Arc::increment_strong_count(config);
Arc::from_raw(config)
};
#[cfg(test)]
mod tests {
use std::thread;
// SAFETY: See the note on the transmute above. The caller should not hold
// more than one reference at a time directly into Config, as the second
// access might invalidate the thread's cache, dangling the reference to
// the first.
unsafe { std::mem::transmute(handle[index].insert(config)) }
use super::*;
fn test_config(server_name: &str) -> Config {
use figment::{
Figment,
providers::{Format, Toml},
};
let raw = format!("server_name = \"{server_name}\"\ndatabase_path = \"/tmp\"\n");
Figment::new()
.merge(Toml::string(&raw))
.extract()
.expect("valid test config")
}
#[test]
fn deref_survives_many_reloads() {
let manager = Manager::new(test_config("initial.example"));
let first: &Arc<Config> = &manager;
let first_name: &str = first.server_name.as_str();
for i in 0..32 {
let prev = manager
.update(test_config(&format!("reload-{i}.example")))
.expect("update succeeds");
if i == 0 {
assert_eq!(prev.server_name.as_str(), "initial.example");
}
}
// Original reference remains valid and unchanged after > HISTORY
// reloads.
assert_eq!(first_name, "initial.example");
assert_eq!(manager.server_name.as_str(), "reload-31.example");
}
#[test]
fn concurrent_reads_and_updates() {
let manager = Manager::new(test_config("start.example"));
thread::scope(|s| {
for _ in 0..4 {
s.spawn(|| {
for _ in 0..200 {
let cfg: &Arc<Config> = &manager;
assert!(cfg.server_name.as_str().ends_with(".example"));
}
});
}
s.spawn(|| {
for i in 0..32 {
let _ = manager
.update(test_config(&format!("iter-{i}.example")))
.expect("update succeeds");
}
});
});
}
#[test]
fn reference_outlives_scoped_thread() {
let manager = Manager::new(test_config("scoped.example"));
let cfg_ref: &Arc<Config> = thread::scope(|s| s.spawn(|| &*manager).join().unwrap());
assert_eq!(cfg_ref.server_name.as_str(), "scoped.example");
}
}
+30 -4
View File
@@ -22,11 +22,24 @@ macro_rules! mod_dtor {
#[macro_export]
macro_rules! mod_init {
($body:block) => {
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
compile_error!("conduwuit_mods (mod_init!) requires an ELF Unix target");
#[used]
#[cfg_attr(target_family = "unix", unsafe(link_section = ".init_array"))]
// SAFETY: `.init_array` is the standard ELF constructor pointer table;
// the dynamic loader invokes `_mod_init` before `main()` or on `dlopen`.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".init_array")
)]
static MOD_INIT: unsafe extern "C" fn() = { _mod_init };
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
// SAFETY: `.text.startup` is the standard ELF text subsection for
// initialization functions.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".text.startup")
)]
unsafe extern "C" fn _mod_init() -> () $body
};
}
@@ -34,11 +47,24 @@ macro_rules! mod_init {
#[macro_export]
macro_rules! mod_fini {
($body:block) => {
#[cfg(not(all(target_family = "unix", not(target_vendor = "apple"))))]
compile_error!("conduwuit_mods (mod_fini!) requires an ELF Unix target");
#[used]
#[cfg_attr(target_family = "unix", unsafe(link_section = ".fini_array"))]
// SAFETY: `.fini_array` is the standard ELF destructor pointer table;
// the dynamic loader invokes `_mod_fini` on `dlclose` or process teardown.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".fini_array")
)]
static MOD_FINI: unsafe extern "C" fn() = { _mod_fini };
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
// SAFETY: `.text.exit` / `.text.startup` is a standard ELF text
// subsection for teardown/initialization functions.
#[cfg_attr(
all(target_family = "unix", not(target_vendor = "apple")),
unsafe(link_section = ".text.startup")
)]
unsafe extern "C" fn _mod_fini() -> () $body
};
}
+25 -7
View File
@@ -13,11 +13,21 @@ pub struct Module {
}
impl Module {
pub fn from_name(name: &str) -> Result<Self> { Self::from_path(path::from_name(name)?) }
/// # Safety
/// See [`new::from_name`]: loads a shared library via `dlopen(3)` and runs
/// its initialization routines.
pub unsafe fn from_name(name: &str) -> Result<Self> {
// SAFETY: Upheld by caller.
unsafe { Self::from_path(path::from_name(name)?) }
}
pub fn from_path(path: OsString) -> Result<Self> {
/// # Safety
/// See [`new::from_path`]: loads a shared library via `dlopen(3)` and runs
/// its initialization routines.
pub unsafe fn from_path(path: OsString) -> Result<Self> {
Ok(Self {
handle: Some(new::from_path(&path)?),
// SAFETY: Upheld by caller.
handle: Some(unsafe { new::from_path(&path)? }),
loaded: SystemTime::now(),
path,
})
@@ -38,16 +48,24 @@ impl Module {
}
}
pub fn get<Prototype>(&self, name: &str) -> Result<Symbol<Prototype>> {
/// Look up a symbol in this module via `dlsym(3)`.
///
/// # Safety
/// `Prototype` is not checked against the symbol's actual type or ABI in
/// the loaded shared library. The caller must ensure `name` refers to a
/// symbol whose type, calling convention, and ABI exactly match
/// `Prototype`.
pub unsafe fn get<Prototype>(&self, name: &str) -> Result<Symbol<'_, Prototype>> {
let cname = CString::new(name.to_owned()).expect("terminated string from provided name");
let handle = self
.handle
.as_ref()
.expect("backing library loaded by this instance");
// SAFETY: Calls dlsym(3) on unix platforms. This might not have to be
// unsafe if wrapped in libloading with_dlerror().
let sym = unsafe { handle.get::<Prototype>(cname.as_bytes()) };
// SAFETY: Caller guarantees `Prototype` matches the symbol's actual
// type and ABI. Passing `as_bytes_with_nul()` avoids a redundant copy
// inside `libloading`.
let sym = unsafe { handle.get::<Prototype>(cname.as_bytes_with_nul()) };
let sym = sym.expect("symbol found; binding successful");
Ok(sym)
+13 -5
View File
@@ -5,14 +5,22 @@ use crate::{Err, Result};
const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL;
pub fn from_name(name: &str) -> Result<Library> {
/// # Safety
/// Loads a shared library via `dlopen(3)`, which executes the library's
/// initialization routines (`.init_array` / constructors). The caller must
/// ensure the resolved module is trusted and ABI-compatible with this binary.
pub unsafe fn from_name(name: &str) -> Result<Library> {
let path = path::from_name(name)?;
from_path(&path)
// SAFETY: Upheld by caller.
unsafe { from_path(&path) }
}
pub fn from_path(path: &OsStr) -> Result<Library> {
//SAFETY: Calls dlopen(3) on unix platforms. This might not have to be
// unsafe if wrapped in with_dlerror.
/// # Safety
/// Loads a shared library via `dlopen(3)`, which executes the library's
/// initialization routines (`.init_array` / constructors). The caller must
/// ensure `path` points to a trusted, ABI-compatible shared library.
pub unsafe fn from_path(path: &OsStr) -> Result<Library> {
// SAFETY: Caller guarantees `path` is a trusted, ABI-compatible library.
let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) };
if let Err(e) = lib {
let name = path::to_name(path)?;
+47 -3
View File
@@ -62,14 +62,28 @@ macro_rules! validated {
}
#[inline]
#[allow(clippy::as_conversions)]
#[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
// Exclusive upper bound for values whose truncation fits in `usize`.
// Note that `usize::MAX as f64` rounds up to 2^64 on 64-bit targets, so a
// strict `<` check against 2^(usize::BITS) rejects all out-of-range values
// on both 32-bit and 64-bit targets.
#[allow(clippy::cast_precision_loss)]
const MAX_USIZE_EXCLUSIVE: f64 = (1_u128 << usize::BITS) as f64;
if !val.is_finite() {
return Err!(Arithmetic("Converting non-finite float to unsigned integer"));
}
if val < 0.0 {
return Err!(Arithmetic("Converting negative float to unsigned integer"));
}
//SAFETY: <https://doc.rust-lang.org/std/primitive.f64.html#method.to_int_unchecked>
Ok(unsafe { val.to_int_unchecked::<usize>() })
if val >= MAX_USIZE_EXCLUSIVE {
return Err!(Arithmetic("Converting out-of-range float to unsigned integer"));
}
Ok(val as usize)
}
#[inline]
@@ -111,3 +125,33 @@ fn try_into_err<Dst: TryFrom<Src>, Src>(e: <Dst as TryFrom<Src>>::Error) -> Erro
#[inline]
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn usize_from_f64_valid() {
assert_eq!(usize_from_f64(0.0).unwrap(), 0);
assert_eq!(usize_from_f64(-0.0).unwrap(), 0);
assert_eq!(usize_from_f64(42.9).unwrap(), 42);
assert_eq!(usize_from_f64(1_048_576.0).unwrap(), 1_048_576);
}
#[test]
fn usize_from_f64_rejects_invalid() {
usize_from_f64(-1.0).unwrap_err();
usize_from_f64(-0.5).unwrap_err();
usize_from_f64(f64::NAN).unwrap_err();
usize_from_f64(f64::INFINITY).unwrap_err();
usize_from_f64(f64::NEG_INFINITY).unwrap_err();
usize_from_f64(1e30).unwrap_err();
#[cfg(target_pointer_width = "64")]
{
// `usize::MAX as f64` rounds up to 2^64, which is out of range.
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
let rounded_max = usize::MAX as f64;
usize_from_f64(rounded_max).unwrap_err();
}
}
}
+3 -5
View File
@@ -1,7 +1,6 @@
use std::convert::Infallible;
use super::{DebugInspect, Result};
use crate::error;
use super::Result;
pub trait UnwrapInfallible<T> {
fn unwrap_infallible(self) -> T;
@@ -10,8 +9,7 @@ pub trait UnwrapInfallible<T> {
impl<T> UnwrapInfallible<T> for Result<T, Infallible> {
#[inline]
fn unwrap_infallible(self) -> T {
// SAFETY: Branchless unwrap for errors that can never happen. In debug
// mode this is asserted.
unsafe { self.debug_inspect_err(error::infallible).unwrap_unchecked() }
let Ok(val) = self;
val
}
}
+5 -4
View File
@@ -32,11 +32,12 @@ impl<'a> From<&'a str> for &'a Unquoted {
fn from(s: &'a str) -> &'a Unquoted {
let s: &'a str = s.unquote_infallible();
//SAFETY: This is a pattern I lifted from ruma-identifiers for
// strong-type strs by wrapping in a tuple-struct.
#[allow(clippy::transmute_ptr_to_ptr)]
// SAFETY: `Unquoted` is `#[repr(transparent)]` over `str`, so
// `*const str` and `*const Unquoted` have identical layout and
// metadata, and the returned reference preserves lifetime `'a`.
#[allow(clippy::as_conversions)]
unsafe {
std::mem::transmute(s)
&*(std::ptr::from_ref::<str>(s) as *const Unquoted)
}
}
}
+5 -13
View File
@@ -120,21 +120,13 @@ pub fn cores_available() -> impl Iterator<Item = Id> { from_mask(*CORES_AVAILABL
pub fn getcpu() -> Result<usize> {
use crate::{Error, utils::math};
// SAFETY: This is part of an interface with many low-level calls taking
// many raw params, but it's unclear why this specific call is unsafe.
// Nevertheless the value obtained here is semantically unsafe because it
// can change on the instruction boundary trailing its own acquisition and
// also any other time.
// SAFETY: `sched_getcpu(2)` takes no pointers and has no preconditions.
// Note that it may still return -1 with errno set (e.g. under seccomp
// filters or kernels without getcpu support), so the error branch below
// must remain live.
let ret: i32 = unsafe { libc::sched_getcpu() };
#[cfg(target_os = "linux")]
// SAFETY: On modern linux systems with a vdso if we can optimize away the branch checking
// for error (see getcpu(2)) then this system call becomes a memory access.
unsafe {
std::hint::assert_unchecked(ret >= 0);
};
if ret == -1 {
if ret < 0 {
return Err(Error::from_errno());
}
-12
View File
@@ -472,17 +472,5 @@ impl<'a, 'de: 'a> de::MapAccess<'de> for &'a mut Deserializer<'de> {
}
}
// activate when stable; too soon now
//#[cfg(debug_assertions)]
#[inline]
fn deserialize_str(input: &[u8]) -> Result<&str> { string::str_from_bytes(input) }
//#[cfg(not(debug_assertions))]
#[cfg(disable)]
#[inline]
fn deserialize_str(input: &[u8]) -> Result<&str> {
// SAFETY: Strings were written by the serializer to the database. Assuming
// no database corruption, the string will be valid. Database corruption is
// detected via rocksdb checksums.
unsafe { std::str::from_utf8_unchecked(input) }
}
+4
View File
@@ -46,6 +46,10 @@ use crate::{Engine, watchers::Watchers};
pub struct Map {
name: &'static str,
watchers: Watchers,
/// SAFETY / DROP ORDER: `cf` holds a lifetime-erased RocksDB column family
/// handle (see `open::open`) and MUST be declared before `db` so that the
/// handle is destroyed before `Engine` (and the underlying RocksDB `DB`)
/// can be dropped.
cf: Arc<ColumnFamily>,
db: Arc<Engine>,
read_options: ReadOptions,
+15 -1
View File
@@ -1,10 +1,24 @@
use std::sync::Arc;
use rocksdb::ColumnFamily;
use rocksdb::{BoundColumnFamily, ColumnFamily};
use crate::Engine;
pub(super) fn open(db: &Arc<Engine>, name: &str) -> Arc<ColumnFamily> {
// Compile-time guard: `BoundColumnFamily` and `ColumnFamily` are both
// `#[repr(Rust)]` in `rust-rocksdb`, so fail compilation if their size or
// alignment ever diverge.
const {
assert!(
size_of::<BoundColumnFamily<'_>>() == size_of::<ColumnFamily>(),
"BoundColumnFamily and ColumnFamily must have identical size"
);
assert!(
align_of::<BoundColumnFamily<'_>>() == align_of::<ColumnFamily>(),
"BoundColumnFamily and ColumnFamily must have identical alignment"
);
}
let bounded_arc = db.cf(name);
let bounded_ptr = Arc::into_raw(bounded_arc);
let cf_ptr = bounded_ptr.cast::<ColumnFamily>();
+11
View File
@@ -12,6 +12,17 @@ impl super::Map {
/// Iterate key-value entries in the map from the beginning.
///
/// - Result is deserialized
///
/// # Unsoundness
/// Because Rust's `Stream` trait does not support lending items tied to
/// `&mut self`, `stream::slice_longevity` unsafely extends the lifetime of
/// raw RocksDB iterator slices to `'a` (the borrow of `Arc<Map>`). As a
/// consequence, borrowed deserialized types (e.g. `&str`, `&UserId`,
/// `&[u8]`) reference the underlying RocksDB cursor buffer and are
/// invalidated on the next poll of the stream. Do not hold borrowed items
/// across polls (e.g. via `collect()`, `ready_chunks()`, `peekable()`,
/// `buffered()`, `buffer_unordered()`, `broadband`, or `wideband`) without
/// converting them to owned values first.
pub fn stream<'a, K, V>(
self: &'a Arc<Self>,
) -> impl Stream<Item = Result<KeyVal<'a, K, V>>> + Send
+23 -11
View File
@@ -54,8 +54,14 @@ pub(crate) struct Get {
/// Note: only initial seek is supported at this time on the assumption rocksdb
/// prefetching prevents mid-iteration polls from blocking on I/O.
pub(crate) struct Seek {
pub(crate) map: Arc<Map>,
/// SAFETY / DROP ORDER: `state` wraps a RocksDB
/// `DBRawIteratorWithThreadMode` referencing `map.db.db` whose lifetime has
/// been erased to `'static`. `state` MUST be declared before `map` so the
/// iterator is destroyed before `map` (and potentially the last
/// `Arc<Engine>`) can be dropped if `Seek` is dropped early (e.g. on
/// cancellation or queue closure).
pub(crate) state: stream::State<'static>,
pub(crate) map: Arc<Map>,
pub(crate) dir: Direction,
pub(crate) key: Option<KeyBuf>,
pub(crate) res: Option<ResultSender<stream::State<'static>>>,
@@ -416,24 +422,30 @@ impl Drop for Pool {
}
fn into_send_get(result: BatchResult<'_>) -> BatchResult<'static> {
// SAFETY: Necessary to send the Handle (rust_rocksdb::PinnableSlice)
// through the channel. The lifetime on the handle is a device by
// rust-rocksdb to associate a database lifetime with its assets. The
// Handle must be dropped before the database is dropped.
// SAFETY: Necessary to send `Handle` (which wraps
// `rocksdb::DBPinnableSlice`) through the oneshot channel. The lifetime on
// `Handle` ties it to the RocksDB `DB` instance; callers of `execute_get`
// borrow `&self.db.pool` (which is owned by `Engine`), ensuring the
// received `BatchResult<'_>` cannot outlive the database.
unsafe { std::mem::transmute(result) }
}
fn into_recv_get<'a>(result: BatchResult<'static>) -> BatchResult<'a> {
// SAFETY: This is to receive the Handle from the channel.
// SAFETY: Shrinking the erased `'static` lifetime back to `'a` (borrowed
// from `&self.db.pool`). `SmallVec<A>` is invariant over `A`, so this
// cannot be done via implicit covariance.
unsafe { std::mem::transmute(result) }
}
pub(crate) fn into_send_seek(result: stream::State<'_>) -> stream::State<'static> {
// SAFETY: Necessary to send the State through the channel; see above.
// SAFETY: Necessary to send `stream::State` through the pool channel.
// While in flight on the queue, `Seek` owns an `Arc<Map>` (declared after
// `state` so `state` drops first on early cancellation); once received back
// from `execute_iter`, the lifetime is shrunk via `into_recv_seek` to the
// caller's borrow of `&self.db.pool` (and `&'a Arc<Map>`), ensuring the
// returned `stream::State<'_>` cannot outlive the database.
unsafe { std::mem::transmute(result) }
}
fn into_recv_seek(result: stream::State<'static>) -> stream::State<'static> {
// SAFETY: This is to receive the State from the channel; see above.
unsafe { std::mem::transmute(result) }
}
#[inline]
const fn into_recv_seek<'a>(result: stream::State<'static>) -> stream::State<'a> { result }
+14 -16
View File
@@ -133,22 +133,20 @@ fn keyval_longevity<'a, 'b: 'a>(item: KeyVal<'a>) -> KeyVal<'b> {
}
fn slice_longevity<'a, 'b: 'a>(item: &'a Slice) -> &'b Slice {
// SAFETY: The lifetime of the data returned by the rocksdb cursor is only
// valid between each movement of the cursor. It is hereby unsafely
// extended to match the lifetime of the cursor itself. This is due to the
// limitation of the Stream trait where the Item is incapable of conveying
// a lifetime; this is due to GAT's being unstable during its development.
// This unsafety can be removed as soon as this limitation is addressed by
// an upcoming version.
// SAFETY: the lifetime of the data returned by
// the RocksDB cursor is only valid until the next movement of the cursor,
// but is unsafely extended here to match the lifetime `'b` of the `Map`
// borrow because `futures::Stream` is not a lending stream.
//
// We have done our best to mitigate the implications of this in conjunction
// with the deserialization API such that borrows being held across
// movements of the cursor do not happen accidentally. The compiler will
// still error when values herein produced try to leave a closure passed to
// a StreamExt API. But escapes can happen if you explicitly and
// intentionally attempt it, and there will be no compiler error or
// warning. This is primarily the case with calling collect() without a
// preceding map(ToOwned::to_owned). A collection of references here is
// illegal, but this will not be enforced by the compiler.
// Callers MUST NOT hold borrowed items (or borrowed deserialized views such
// as `&str`, `&UserId`, or `&[u8]`) across subsequent polls of the stream —
// in particular, never call `collect()`, `ready_chunks()`, `peekable()`,
// `buffered()`, `buffer_unordered()`, `broadband`, or `wideband` on a
// stream yielding borrowed references without first converting items to
// owned values (e.g. `map(ToOwned::to_owned)`). The compiler does NOT
// reject such escapes when items borrow from `'b`.
//
// TODO: Fix this, it is old code and language changes may have made it
// unnecessary
unsafe { std::mem::transmute(item) }
}
-16
View File
@@ -1,28 +1,12 @@
use conduwuit::Result;
use rocksdb::{Direction, ErrorKind, IteratorMode};
//#[cfg(debug_assertions)]
macro_rules! unhandled {
($msg:literal) => {
unimplemented!($msg)
};
}
// activate when stable; we're not ready for this yet
#[cfg(disable)] // #[cfg(not(debug_assertions))]
macro_rules! unhandled {
($msg:literal) => {
// SAFETY: Eliminates branches for serializing and deserializing types never
// encountered in the codebase. This can promote optimization and reduce
// codegen. The developer must verify for every invoking callsite that the
// unhandled type is in no way involved and could not possibly be
// encountered.
unsafe {
std::hint::unreachable_unchecked();
}
};
}
pub(crate) use unhandled;
#[inline]
+15 -4
View File
@@ -42,7 +42,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
let main_lock = server.mods.read().await;
let main_mod = (*main_lock).last().expect("main module loaded");
if starts {
let start = main_mod.get::<StartFuncProto>("start")?;
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
// "Rust" fn start` with signature matching `StartFuncProto`, compiled
// with the same toolchain and flags.
let start = unsafe { main_mod.get::<StartFuncProto>("start")? };
match start(&server.server).await {
| Ok(services) => server.services.lock().await.insert(services),
| Err(error) => {
@@ -54,7 +57,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
server.server.stopping.store(false, Ordering::Release);
let run = main_mod.get::<RunFuncProto>("run")?;
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
// "Rust" fn run` with signature matching `RunFuncProto`, compiled with the
// same toolchain and flags.
let run = unsafe { main_mod.get::<RunFuncProto>("run")? };
if let Err(error) = run(server
.services
.lock()
@@ -72,7 +78,10 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
let stops = !reloads || stale_from_mods(&main_lock)? <= restart_thresh();
let starts = reloads && stops;
if stops {
let stop = main_mod.get::<StopFuncProto>("stop")?;
// SAFETY: `conduwuit_router` exports `#[unsafe(no_mangle)] pub extern
// "Rust" fn stop` with signature matching `StopFuncProto`, compiled
// with the same toolchain and flags.
let stop = unsafe { main_mod.get::<StopFuncProto>("stop")? };
if let Err(error) = stop(
server
.services
@@ -102,7 +111,9 @@ pub(crate) async fn open(server: &Arc<Server>) -> Result<usize, Error> {
for (i, name) in MODULE_NAMES.iter().enumerate() {
if mods.get(i).is_none() {
mods.push(mods::Module::from_name(name)?);
// SAFETY: `MODULE_NAMES` are this workspace's own hot-reloadable
// shared libraries built with the same compiler/ABI.
mods.push(unsafe { mods::Module::from_name(name)? });
}
}
+5 -1
View File
@@ -8,7 +8,11 @@ use ruma::api::error::ErrorKind;
pub(crate) fn build(services: &Arc<Services>) -> (Router, Guard) {
let router = Router::<state::State>::new();
let (state, guard) = state::create(services.clone());
// SAFETY: `guard` is returned alongside `router` and held in `serve()`
// until the HTTP server has finished serving all requests and dropped
// `router`, ensuring no `State` copy can be dereferenced after `Services`
// is dropped.
let (state, guard) = unsafe { state::create(services.clone()) };
let router = conduwuit_api::router::build(router, state)
.merge(conduwuit_web::build(services))
.fallback(not_found)
+9 -1
View File
@@ -11,7 +11,15 @@ pub struct Guard {
services: Arc<Services>,
}
pub fn create(services: Arc<Services>) -> (State, Guard) {
/// Create a raw-pointer `State` handle paired with a `Guard` that keeps
/// `Services` alive.
///
/// # Safety
/// `State` is `Copy` and dereferences its raw pointer without reference
/// counting. The caller must ensure that no `State` (nor any copy of it) is
/// dereferenced after `Guard` and all other `Arc<Services>` clones have been
/// dropped.
pub unsafe fn create(services: Arc<Services>) -> (State, Guard) {
let state = State {
services: Arc::into_raw(services.clone()),
};