|
| 1 | +// Copyright 2024 Adobe. All rights reserved. |
| 2 | +// This file is licensed to you under the Apache License, |
| 3 | +// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 4 | +// or the MIT license (http://opensource.org/licenses/MIT), |
| 5 | +// at your option. |
| 6 | + |
| 7 | +// Unless required by applicable law or agreed to in writing, |
| 8 | +// this software is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or |
| 10 | +// implied. See the LICENSE-MIT and LICENSE-APACHE files for the |
| 11 | +// specific language governing permissions and limitations under |
| 12 | +// each license. |
| 13 | + |
| 14 | +use std::{ |
| 15 | + error::Error, |
| 16 | + fmt, |
| 17 | + sync::{Mutex, MutexGuard}, |
| 18 | +}; |
| 19 | + |
| 20 | +static FFI_MUTEX: Mutex<()> = Mutex::new(()); |
| 21 | + |
| 22 | +/// This mutex must be used by all code that accesses OpenSSL native code since |
| 23 | +/// the OpenSSL native code library is not re-entrant. |
| 24 | +/// |
| 25 | +/// Failure to do so has been observed to lead to unexpected behavior including |
| 26 | +/// process crashes. |
| 27 | +pub struct OpenSslMutex<'a> { |
| 28 | + // The dead code bypass is intentional. We don't need to read the () contents of this guard. We |
| 29 | + // only need to ensure that the guard is dropped when this struct is dropped. |
| 30 | + #[allow(dead_code)] |
| 31 | + guard: MutexGuard<'a, ()>, |
| 32 | +} |
| 33 | + |
| 34 | +impl OpenSslMutex<'_> { |
| 35 | + /// Acquire a mutex on OpenSSL FFI code. |
| 36 | + /// |
| 37 | + /// WARNING: Calling code MUST NOT PANIC inside this function or |
| 38 | + /// anything called by it, even in test code. This will poison the FFI mutex |
| 39 | + /// and leave OpenSSL unusable for the remainder of the process lifetime. |
| 40 | + pub fn acquire() -> Result<Self, OpenSslMutexUnavailable> { |
| 41 | + // Useful for debugging. |
| 42 | + // eprintln!( |
| 43 | + // "ACQUIRING FFI MUTEX at\n{}", |
| 44 | + // std::backtrace::Backtrace::force_capture() |
| 45 | + // ); |
| 46 | + |
| 47 | + match FFI_MUTEX.lock() { |
| 48 | + Ok(guard) => Ok(Self { guard }), |
| 49 | + Err(_) => Err(OpenSslMutexUnavailable {}), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Useful for debugging. |
| 55 | +// impl<'a> Drop for OpenSslMutex<'a> { |
| 56 | +// fn drop(&mut self) { |
| 57 | +// eprintln!("Releasing FFI mutex\n\n\n"); |
| 58 | +// } |
| 59 | +// } |
| 60 | + |
| 61 | +/// Error returned when unable to acquire the OpenSSL native code mutex. |
| 62 | +/// |
| 63 | +/// If this occurs, it's likely that a prior invocation of OpenSSL code panicked |
| 64 | +/// while holding the mutex. When this happens, the OpenSSL native code mutex is |
| 65 | +/// considered poisoned for the remainder of the process lifetime. |
| 66 | +/// |
| 67 | +/// See [Rustnomicon: Poisoning] for more information. |
| 68 | +/// |
| 69 | +/// [Rustnomicon: Poisoning]: https://doc.rust-lang.org/nomicon/poisoning.html |
| 70 | +#[derive(Debug, Eq, PartialEq)] |
| 71 | +pub struct OpenSslMutexUnavailable; |
| 72 | + |
| 73 | +impl fmt::Display for OpenSslMutexUnavailable { |
| 74 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 75 | + write!(f, "Unable to acquire OpenSSL native code mutex") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Error for OpenSslMutexUnavailable {} |
0 commit comments