refactor: remove unnecessary unsafe (#5802)

unsafe impls for `Send` and `Sync` should not be added by default. in
the case of `SlotGuard` removing them does not cause any issues, as the
compiler automatically derives those.

This PR adds requirement to document the unsafety (see
[clippy::undocumented_unsafe_blocks]) and opportunistically adds
`#![deny(unsafe_code)]` to most places where we don't have unsafe code
right now.

TRPL on Send and Sync:
https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html

[clippy::undocumented_unsafe_blocks]:
https://rust-lang.github.io/rust-clippy/master/#/undocumented_unsafe_blocks
This commit is contained in:
Joonas Koivunen
2023-11-07 12:26:25 +02:00
committed by GitHub
parent a394f49e0d
commit 4be6bc7251
25 changed files with 59 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
//!
//! Various tools and helpers to handle cluster / compute node (Postgres) //! Various tools and helpers to handle cluster / compute node (Postgres)
//! configuration. //! configuration.
//! #![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
pub mod checker; pub mod checker;
pub mod config; pub mod config;
pub mod configurator; pub mod configurator;

View File

@@ -262,7 +262,7 @@ where
P: Into<Utf8PathBuf>, P: Into<Utf8PathBuf>,
{ {
let path: Utf8PathBuf = path.into(); let path: Utf8PathBuf = path.into();
// SAFETY // SAFETY:
// pre_exec is marked unsafe because it runs between fork and exec. // pre_exec is marked unsafe because it runs between fork and exec.
// Why is that dangerous in various ways? // Why is that dangerous in various ways?
// Long answer: https://github.com/rust-lang/rust/issues/39575 // Long answer: https://github.com/rust-lang/rust/issues/39575

View File

@@ -1,11 +1,10 @@
// //! Local control plane.
// Local control plane. //!
// //! Can start, configure and stop postgres instances running as a local processes.
// Can start, configure and stop postgres instances running as a local processes. //!
// //! Intended to be used in integration tests and in CLI tools for
// Intended to be used in integration tests and in CLI tools for //! local installations.
// local installations. #![deny(clippy::undocumented_unsafe_blocks)]
//
pub mod attachment_service; pub mod attachment_service;
mod background_process; mod background_process;

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
pub mod requests; pub mod requests;
pub mod responses; pub mod responses;
pub mod spec; pub mod spec;

View File

@@ -1,6 +1,6 @@
//!
//! Shared code for consumption metics collection //! Shared code for consumption metics collection
//! #![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@@ -2,6 +2,7 @@
//! make sure that we use the same dep version everywhere. //! make sure that we use the same dep version everywhere.
//! Otherwise, we might not see all metrics registered via //! Otherwise, we might not see all metrics registered via
//! a default registry. //! a default registry.
#![deny(clippy::undocumented_unsafe_blocks)]
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use prometheus::core::{AtomicU64, Collector, GenericGauge, GenericGaugeVec}; use prometheus::core::{AtomicU64, Collector, GenericGauge, GenericGaugeVec};
pub use prometheus::opts; pub use prometheus::opts;

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use const_format::formatcp; use const_format::formatcp;
/// Public API types /// Public API types

View File

@@ -2,6 +2,8 @@
//! To use, create PostgresBackend and run() it, passing the Handler //! To use, create PostgresBackend and run() it, passing the Handler
//! implementation determining how to process the queries. Currently its API //! implementation determining how to process the queries. Currently its API
//! is rather narrow, but we can extend it once required. //! is rather narrow, but we can extend it once required.
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use anyhow::Context; use anyhow::Context;
use bytes::Bytes; use bytes::Bytes;
use futures::pin_mut; use futures::pin_mut;

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use itertools::Itertools; use itertools::Itertools;
use std::borrow::Cow; use std::borrow::Cow;

View File

@@ -8,6 +8,7 @@
// modules included with the postgres_ffi macro depend on the types of the specific version's // modules included with the postgres_ffi macro depend on the types of the specific version's
// types, and trigger a too eager lint. // types, and trigger a too eager lint.
#![allow(clippy::duplicate_mod)] #![allow(clippy::duplicate_mod)]
#![deny(clippy::undocumented_unsafe_blocks)]
use bytes::Bytes; use bytes::Bytes;
use utils::bin_ser::SerializeError; use utils::bin_ser::SerializeError;
@@ -20,6 +21,7 @@ macro_rules! postgres_ffi {
pub mod bindings { pub mod bindings {
// bindgen generates bindings for a lot of stuff we don't need // bindgen generates bindings for a lot of stuff we don't need
#![allow(dead_code)] #![allow(dead_code)]
#![allow(clippy::undocumented_unsafe_blocks)]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
include!(concat!( include!(concat!(

View File

@@ -1,6 +1,7 @@
//! Postgres protocol messages serialization-deserialization. See //! Postgres protocol messages serialization-deserialization. See
//! <https://www.postgresql.org/docs/devel/protocol-message-formats.html> //! <https://www.postgresql.org/docs/devel/protocol-message-formats.html>
//! on message formats. //! on message formats.
#![deny(clippy::undocumented_unsafe_blocks)]
pub mod framed; pub mod framed;

View File

@@ -6,6 +6,8 @@
//! * [`s3_bucket`] uses AWS S3 bucket as an external storage //! * [`s3_bucket`] uses AWS S3 bucket as an external storage
//! * [`azure_blob`] allows to use Azure Blob storage as an external storage //! * [`azure_blob`] allows to use Azure Blob storage as an external storage
//! //!
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
mod azure_blob; mod azure_blob;
mod local_fs; mod local_fs;

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use const_format::formatcp; use const_format::formatcp;
/// Public API types /// Public API types

View File

@@ -1,4 +1,6 @@
//! Synthetic size calculation //! Synthetic size calculation
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
mod calculation; mod calculation;
pub mod svg; pub mod svg;

View File

@@ -32,6 +32,8 @@
//! .init(); //! .init();
//! } //! }
//! ``` //! ```
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
use opentelemetry::sdk::Resource; use opentelemetry::sdk::Resource;
use opentelemetry::KeyValue; use opentelemetry::KeyValue;

View File

@@ -120,6 +120,8 @@ impl Id {
chunk[0] = HEX[((b >> 4) & 0xf) as usize]; chunk[0] = HEX[((b >> 4) & 0xf) as usize];
chunk[1] = HEX[(b & 0xf) as usize]; chunk[1] = HEX[(b & 0xf) as usize];
} }
// SAFETY: vec constructed out of `HEX`, it can only be ascii
unsafe { String::from_utf8_unchecked(buf) } unsafe { String::from_utf8_unchecked(buf) }
} }
} }

View File

@@ -1,5 +1,6 @@
//! `utils` is intended to be a place to put code that is shared //! `utils` is intended to be a place to put code that is shared
//! between other crates in this repository. //! between other crates in this repository.
#![deny(clippy::undocumented_unsafe_blocks)]
pub mod backoff; pub mod backoff;

View File

@@ -1,6 +1,7 @@
/// Immediately terminate the calling process without calling /// Immediately terminate the calling process without calling
/// atexit callbacks, C runtime destructors etc. We mainly use /// atexit callbacks, C runtime destructors etc. We mainly use
/// this to protect coverage data from concurrent writes. /// this to protect coverage data from concurrent writes.
pub fn exit_now(code: u8) { pub fn exit_now(code: u8) -> ! {
// SAFETY: exiting is safe, the ffi is not safe
unsafe { nix::libc::_exit(code as _) }; unsafe { nix::libc::_exit(code as _) };
} }

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
#![cfg(target_os = "linux")] #![cfg(target_os = "linux")]
use anyhow::Context; use anyhow::Context;

View File

@@ -1,3 +1,5 @@
#![deny(clippy::undocumented_unsafe_blocks)]
mod auth; mod auth;
pub mod basebackup; pub mod basebackup;
pub mod config; pub mod config;

View File

@@ -1446,9 +1446,6 @@ pub struct SlotGuard {
_completion: utils::completion::Completion, _completion: utils::completion::Completion,
} }
unsafe impl Send for SlotGuard {}
unsafe impl Sync for SlotGuard {}
impl SlotGuard { impl SlotGuard {
fn new( fn new(
tenant_id: TenantId, tenant_id: TenantId,

View File

@@ -596,21 +596,21 @@ trait CloseFileDescriptors: CommandExt {
impl<C: CommandExt> CloseFileDescriptors for C { impl<C: CommandExt> CloseFileDescriptors for C {
fn close_fds(&mut self) -> &mut Command { fn close_fds(&mut self) -> &mut Command {
// SAFETY: Code executed inside pre_exec should have async-signal-safety,
// which means it should be safe to execute inside a signal handler.
// The precise meaning depends on platform. See `man signal-safety`
// for the linux definition.
//
// The set_fds_cloexec_threadsafe function is documented to be
// async-signal-safe.
//
// Aside from this function, the rest of the code is re-entrant and
// doesn't make any syscalls. We're just passing constants.
//
// NOTE: It's easy to indirectly cause a malloc or lock a mutex,
// which is not async-signal-safe. Be careful.
unsafe { unsafe {
self.pre_exec(move || { self.pre_exec(move || {
// SAFETY: Code executed inside pre_exec should have async-signal-safety,
// which means it should be safe to execute inside a signal handler.
// The precise meaning depends on platform. See `man signal-safety`
// for the linux definition.
//
// The set_fds_cloexec_threadsafe function is documented to be
// async-signal-safe.
//
// Aside from this function, the rest of the code is re-entrant and
// doesn't make any syscalls. We're just passing constants.
//
// NOTE: It's easy to indirectly cause a malloc or lock a mutex,
// which is not async-signal-safe. Be careful.
close_fds::set_fds_cloexec_threadsafe(3, &[]); close_fds::set_fds_cloexec_threadsafe(3, &[]);
Ok(()) Ok(())
}) })

View File

@@ -1,3 +1,5 @@
#![deny(clippy::undocumented_unsafe_blocks)]
use std::convert::Infallible; use std::convert::Infallible;
use anyhow::{bail, Context}; use anyhow::{bail, Context};

View File

@@ -1,3 +1,5 @@
#![deny(unsafe_code)]
#![deny(clippy::undocumented_unsafe_blocks)]
pub mod checks; pub mod checks;
pub mod cloud_admin_api; pub mod cloud_admin_api;
pub mod garbage; pub mod garbage;

View File

@@ -1,3 +1,4 @@
#![deny(clippy::undocumented_unsafe_blocks)]
use camino::Utf8PathBuf; use camino::Utf8PathBuf;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use remote_storage::RemoteStorageConfig; use remote_storage::RemoteStorageConfig;