mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-21 16:07:35 +00:00
Compare commits
22
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08098c5e88 | ||
|
|
8e54ea0853 | ||
|
|
5bb55a1285 | ||
|
|
3a849c2687 | ||
|
|
9e02f5b607 | ||
|
|
98d4022768 | ||
|
|
7294a3589d | ||
|
|
6aed1895b0 | ||
|
|
a9377fb896 | ||
|
|
94f5026d57 | ||
|
|
c04446b86e | ||
|
|
b4f163aacb | ||
|
|
6c1a6099b9 | ||
|
|
97585f872c | ||
|
|
08eef0c00f | ||
|
|
b73eace66f | ||
|
|
fc4ad66af1 | ||
|
|
daabb53092 | ||
|
|
806a63cb29 | ||
|
|
cd51a01fac | ||
|
|
ffa535e0d5 | ||
|
|
4fa966212e |
No files matched your search
@@ -1 +0,0 @@
|
||||
The former configuration alias `allow_check_for_updates` is now explicitly deprecated in favor of `allow_announcements_check`. Additionally, `request_ip_source` is now deprecated in favor of `accepted_ip_sources`.
|
||||
@@ -412,7 +412,7 @@
|
||||
# env: CONTINUWUITY_IP_LOOKUP_STRATEGY
|
||||
#ip_lookup_strategy = 5
|
||||
|
||||
# DEPRECATED: The preferred source to use for getting the real client IP.
|
||||
# The preferred source to use for getting the real client IP.
|
||||
#
|
||||
# Takes any of the following options:
|
||||
#
|
||||
@@ -431,10 +431,9 @@
|
||||
# to set this header" check; if the header selected is present, it is
|
||||
# used.
|
||||
#
|
||||
# Ignored if `accepted_ip_sources` is set. `accepted_ip_sources` is
|
||||
# the preferred alternative.
|
||||
# Ignored if `accepted_ip_sources` is set.
|
||||
#
|
||||
# If not set, `direct` is used. Additionally, `direct` is used
|
||||
# If not set set, `direct` is used. Additionally, `direct` is used
|
||||
# as a fallback if the chosen header is not found.
|
||||
#
|
||||
# env: CONTINUWUITY_REQUEST_IP_SOURCE
|
||||
|
||||
+85
-142
@@ -1,6 +1,8 @@
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
ops::Deref,
|
||||
ptr::NonNull,
|
||||
ptr,
|
||||
ptr::null_mut,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicPtr, Ordering},
|
||||
@@ -8,178 +10,119 @@ use std::{
|
||||
};
|
||||
|
||||
use super::Config;
|
||||
use crate::{Result, SyncMutex};
|
||||
use crate::Result;
|
||||
|
||||
/// 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<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>>,
|
||||
active: AtomicPtr<Config>,
|
||||
}
|
||||
|
||||
/// 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() }
|
||||
thread_local! {
|
||||
static INDEX: Cell<usize> = 0.into();
|
||||
static HANDLE: RefCell<Handles> = const {
|
||||
RefCell::new([const { None }; HISTORY])
|
||||
};
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
type Handle = Option<Arc<Config>>;
|
||||
type Handles = [Handle; HISTORY];
|
||||
|
||||
const HISTORY: usize = 8;
|
||||
|
||||
impl Manager {
|
||||
pub(crate) fn new(config: Config) -> Self {
|
||||
let pinned = PinnedConfig::new(config);
|
||||
let ptr = pinned.as_ptr();
|
||||
let config = Arc::new(config);
|
||||
Self {
|
||||
active: AtomicPtr::new(ptr),
|
||||
retired: SyncMutex::new(vec![pinned]),
|
||||
active: AtomicPtr::new(Arc::into_raw(config).cast_mut()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the active configuration, returning prior configuration.
|
||||
#[tracing::instrument(skip_all, level = "info")]
|
||||
pub fn update(&self, config: Config) -> Result<Arc<Config>> {
|
||||
let pinned = PinnedConfig::new(config);
|
||||
let new_ptr = pinned.as_ptr();
|
||||
let config = Arc::new(config);
|
||||
let new = Arc::into_raw(config);
|
||||
let old = self.active.swap(new.cast_mut(), Ordering::AcqRel);
|
||||
|
||||
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);
|
||||
// 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) })
|
||||
}
|
||||
|
||||
// `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);
|
||||
fn load(&self, handle: &mut [Option<Arc<Config>>]) -> &'static Arc<Config> {
|
||||
let config = self.active.load(Ordering::Acquire);
|
||||
|
||||
// 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) };
|
||||
// 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);
|
||||
}
|
||||
|
||||
Ok(old)
|
||||
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) };
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Manager {
|
||||
type Target = Arc<Config>;
|
||||
|
||||
#[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 }
|
||||
}
|
||||
fn deref(&self) -> &Self::Target { HANDLE.with_borrow_mut(|handle| self.load(handle)) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::thread;
|
||||
#[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)
|
||||
};
|
||||
|
||||
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");
|
||||
}
|
||||
// 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)) }
|
||||
}
|
||||
+7
-10
@@ -195,7 +195,7 @@ pub struct Config {
|
||||
/// announcements or major updates. This is not an update check endpoint.
|
||||
///
|
||||
/// default: true
|
||||
#[serde(default = "true_fn")]
|
||||
#[serde(alias = "allow_check_for_updates", default = "true_fn")]
|
||||
pub allow_announcements_check: bool,
|
||||
|
||||
/// Set this to any float value to multiply continuwuity's in-memory LRU
|
||||
@@ -490,7 +490,7 @@ pub struct Config {
|
||||
#[serde(default = "default_ip_lookup_strategy")]
|
||||
pub ip_lookup_strategy: u8,
|
||||
|
||||
/// DEPRECATED: The preferred source to use for getting the real client IP.
|
||||
/// The preferred source to use for getting the real client IP.
|
||||
///
|
||||
/// Takes any of the following options:
|
||||
///
|
||||
@@ -509,10 +509,9 @@ pub struct Config {
|
||||
/// to set this header" check; if the header selected is present, it is
|
||||
/// used.
|
||||
///
|
||||
/// Ignored if `accepted_ip_sources` is set. `accepted_ip_sources` is
|
||||
/// the preferred alternative.
|
||||
/// Ignored if `accepted_ip_sources` is set.
|
||||
///
|
||||
/// If not set, `direct` is used. Additionally, `direct` is used
|
||||
/// If not set set, `direct` is used. Additionally, `direct` is used
|
||||
/// as a fallback if the chosen header is not found.
|
||||
#[serde(default)]
|
||||
pub request_ip_source: Option<String>,
|
||||
@@ -2994,16 +2993,11 @@ impl<'de, K: Hash + Eq + Clone> Deserialize<'de> for PerMinuteRatelimiter<K> {
|
||||
}
|
||||
|
||||
const DEPRECATED_KEYS: &[&str] = &[
|
||||
"allow_check_for_updates",
|
||||
"allow_moderators_to_view_redacted_content",
|
||||
"allow_sticky_events",
|
||||
"cache_capacity",
|
||||
"conduit_cache_capacity_modifier",
|
||||
"max_concurrent_requests",
|
||||
"otlp_protocol",
|
||||
"request_ip_source",
|
||||
"roomid_spacehierarchy_cache_capacity",
|
||||
"unredacted_content_max_age_secs",
|
||||
"well_known_client",
|
||||
"well_known_server",
|
||||
"well_known_support_page",
|
||||
@@ -3011,6 +3005,9 @@ const DEPRECATED_KEYS: &[&str] = &[
|
||||
"well_known_support_email",
|
||||
"well_known_support_mxid",
|
||||
"well_known.rtc_focus_server_urls",
|
||||
"allow_sticky_events",
|
||||
"allow_moderators_to_view_redacted_content",
|
||||
"unredacted_content_max_age_secs",
|
||||
];
|
||||
|
||||
impl Config {
|
||||
|
||||
+4
-30
@@ -22,24 +22,11 @@ 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]
|
||||
// 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")
|
||||
)]
|
||||
#[cfg_attr(target_family = "unix", unsafe(link_section = ".init_array"))]
|
||||
static MOD_INIT: unsafe extern "C" fn() = { _mod_init };
|
||||
|
||||
// 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")
|
||||
)]
|
||||
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
|
||||
unsafe extern "C" fn _mod_init() -> () $body
|
||||
};
|
||||
}
|
||||
@@ -47,24 +34,11 @@ 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]
|
||||
// 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")
|
||||
)]
|
||||
#[cfg_attr(target_family = "unix", unsafe(link_section = ".fini_array"))]
|
||||
static MOD_FINI: unsafe extern "C" fn() = { _mod_fini };
|
||||
|
||||
// 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")
|
||||
)]
|
||||
#[cfg_attr(target_family = "unix", unsafe(link_section = ".text.startup"))]
|
||||
unsafe extern "C" fn _mod_fini() -> () $body
|
||||
};
|
||||
}
|
||||
+7
-25
@@ -13,21 +13,11 @@ pub struct Module {
|
||||
}
|
||||
|
||||
impl Module {
|
||||
/// # 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_name(name: &str) -> Result<Self> { Self::from_path(path::from_name(name)?) }
|
||||
|
||||
/// # 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> {
|
||||
pub fn from_path(path: OsString) -> Result<Self> {
|
||||
Ok(Self {
|
||||
// SAFETY: Upheld by caller.
|
||||
handle: Some(unsafe { new::from_path(&path)? }),
|
||||
handle: Some(new::from_path(&path)?),
|
||||
loaded: SystemTime::now(),
|
||||
path,
|
||||
})
|
||||
@@ -48,24 +38,16 @@ impl Module {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>> {
|
||||
pub 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: 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()) };
|
||||
// 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()) };
|
||||
let sym = sym.expect("symbol found; binding successful");
|
||||
|
||||
Ok(sym)
|
||||
|
||||
+5
-13
@@ -5,22 +5,14 @@ use crate::{Err, Result};
|
||||
|
||||
const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL;
|
||||
|
||||
/// # 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> {
|
||||
pub fn from_name(name: &str) -> Result<Library> {
|
||||
let path = path::from_name(name)?;
|
||||
// SAFETY: Upheld by caller.
|
||||
unsafe { from_path(&path) }
|
||||
from_path(&path)
|
||||
}
|
||||
|
||||
/// # 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.
|
||||
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.
|
||||
let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) };
|
||||
if let Err(e) = lib {
|
||||
let name = path::to_name(path)?;
|
||||
|
||||
+3
-47
@@ -62,28 +62,14 @@ macro_rules! validated {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
||||
#[allow(clippy::as_conversions)]
|
||||
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"));
|
||||
}
|
||||
|
||||
if val >= MAX_USIZE_EXCLUSIVE {
|
||||
return Err!(Arithmetic("Converting out-of-range float to unsigned integer"));
|
||||
}
|
||||
|
||||
Ok(val as usize)
|
||||
//SAFETY: <https://doc.rust-lang.org/std/primitive.f64.html#method.to_int_unchecked>
|
||||
Ok(unsafe { val.to_int_unchecked::<usize>() })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -125,33 +111,3 @@ 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::convert::Infallible;
|
||||
|
||||
use super::Result;
|
||||
use super::{DebugInspect, Result};
|
||||
use crate::error;
|
||||
|
||||
pub trait UnwrapInfallible<T> {
|
||||
fn unwrap_infallible(self) -> T;
|
||||
@@ -9,7 +10,8 @@ pub trait UnwrapInfallible<T> {
|
||||
impl<T> UnwrapInfallible<T> for Result<T, Infallible> {
|
||||
#[inline]
|
||||
fn unwrap_infallible(self) -> T {
|
||||
let Ok(val) = self;
|
||||
val
|
||||
// SAFETY: Branchless unwrap for errors that can never happen. In debug
|
||||
// mode this is asserted.
|
||||
unsafe { self.debug_inspect_err(error::infallible).unwrap_unchecked() }
|
||||
}
|
||||
}
|
||||
@@ -32,12 +32,11 @@ impl<'a> From<&'a str> for &'a Unquoted {
|
||||
fn from(s: &'a str) -> &'a Unquoted {
|
||||
let s: &'a str = s.unquote_infallible();
|
||||
|
||||
// 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)]
|
||||
//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)]
|
||||
unsafe {
|
||||
&*(std::ptr::from_ref::<str>(s) as *const Unquoted)
|
||||
std::mem::transmute(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,13 +120,21 @@ pub fn cores_available() -> impl Iterator<Item = Id> { from_mask(*CORES_AVAILABL
|
||||
pub fn getcpu() -> Result<usize> {
|
||||
use crate::{Error, utils::math};
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
let ret: i32 = unsafe { libc::sched_getcpu() };
|
||||
|
||||
if ret < 0 {
|
||||
#[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 {
|
||||
return Err(Error::from_errno());
|
||||
}
|
||||
|
||||
|
||||
@@ -472,5 +472,17 @@ 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) }
|
||||
}
|
||||
@@ -46,10 +46,6 @@ 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,
|
||||
|
||||
@@ -1,24 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use rocksdb::{BoundColumnFamily, ColumnFamily};
|
||||
use rocksdb::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>();
|
||||
|
||||
@@ -12,17 +12,6 @@ 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
|
||||
|
||||
+11
-23
@@ -54,14 +54,8 @@ 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 {
|
||||
/// 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) state: stream::State<'static>,
|
||||
pub(crate) dir: Direction,
|
||||
pub(crate) key: Option<KeyBuf>,
|
||||
pub(crate) res: Option<ResultSender<stream::State<'static>>>,
|
||||
@@ -422,30 +416,24 @@ impl Drop for Pool {
|
||||
}
|
||||
|
||||
fn into_send_get(result: BatchResult<'_>) -> BatchResult<'static> {
|
||||
// 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.
|
||||
// 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.
|
||||
unsafe { std::mem::transmute(result) }
|
||||
}
|
||||
|
||||
fn into_recv_get<'a>(result: BatchResult<'static>) -> BatchResult<'a> {
|
||||
// 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.
|
||||
// SAFETY: This is to receive the Handle from the channel.
|
||||
unsafe { std::mem::transmute(result) }
|
||||
}
|
||||
|
||||
pub(crate) fn into_send_seek(result: stream::State<'_>) -> stream::State<'static> {
|
||||
// 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.
|
||||
// SAFETY: Necessary to send the State through the channel; see above.
|
||||
unsafe { std::mem::transmute(result) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn into_recv_seek<'a>(result: stream::State<'static>) -> stream::State<'a> { 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) }
|
||||
}
|
||||
+16
-14
@@ -133,20 +133,22 @@ 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 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.
|
||||
// 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.
|
||||
//
|
||||
// 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
|
||||
// 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.
|
||||
unsafe { std::mem::transmute(item) }
|
||||
}
|
||||
@@ -1,12 +1,28 @@
|
||||
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]
|
||||
|
||||
+4
-15
@@ -42,10 +42,7 @@ 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 {
|
||||
// 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")? };
|
||||
let start = main_mod.get::<StartFuncProto>("start")?;
|
||||
match start(&server.server).await {
|
||||
| Ok(services) => server.services.lock().await.insert(services),
|
||||
| Err(error) => {
|
||||
@@ -57,10 +54,7 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
|
||||
|
||||
server.server.stopping.store(false, Ordering::Release);
|
||||
|
||||
// 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")? };
|
||||
let run = main_mod.get::<RunFuncProto>("run")?;
|
||||
if let Err(error) = run(server
|
||||
.services
|
||||
.lock()
|
||||
@@ -78,10 +72,7 @@ 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 {
|
||||
// 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")? };
|
||||
let stop = main_mod.get::<StopFuncProto>("stop")?;
|
||||
if let Err(error) = stop(
|
||||
server
|
||||
.services
|
||||
@@ -111,9 +102,7 @@ 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() {
|
||||
// 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)? });
|
||||
mods.push(mods::Module::from_name(name)?);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,7 @@ use ruma::api::error::ErrorKind;
|
||||
|
||||
pub(crate) fn build(services: &Arc<Services>) -> (Router, Guard) {
|
||||
let router = Router::<state::State>::new();
|
||||
// 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 (state, guard) = state::create(services.clone());
|
||||
let router = conduwuit_api::router::build(router, state)
|
||||
.merge(conduwuit_web::build(services))
|
||||
.fallback(not_found)
|
||||
|
||||
@@ -11,15 +11,7 @@ pub struct Guard {
|
||||
services: Arc<Services>,
|
||||
}
|
||||
|
||||
/// 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) {
|
||||
pub fn create(services: Arc<Services>) -> (State, Guard) {
|
||||
let state = State {
|
||||
services: Arc::into_raw(services.clone()),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user