Skip to content

Commit b18948f

Browse files
committed
rename to_arc to clone_arc
to_arc converts a `*const C` into an `Option<Arc<T>>`. It had a lot of verbiage to explain what it was doing with reference counts. I think the conceptual model becomes a lot simpler if we rename the function to reflect what it is really doing: cloning the arc.
1 parent 0b35e62 commit b18948f

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

src/acceptor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::rslice::{rustls_slice_bytes, rustls_str};
1212
use crate::server::rustls_server_config;
1313
use crate::{
1414
ffi_panic_boundary, free_box, rustls_result, set_boxed_mut_ptr, to_box, to_boxed_mut_ptr,
15-
try_arc_from_ptr, try_callback, try_mut_from_ptr, try_ref_from_ptr, try_take, Castable,
16-
OwnershipBox,
15+
try_arc_from_ptr, try_callback, try_clone_arc, try_mut_from_ptr, try_ref_from_ptr, try_take,
16+
Castable, OwnershipBox,
1717
};
1818
use rustls_result::NullParameter;
1919

@@ -400,7 +400,7 @@ impl rustls_accepted {
400400
ffi_panic_boundary! {
401401
let accepted: &mut Option<Accepted> = try_mut_from_ptr!(accepted);
402402
let accepted = try_take!(accepted);
403-
let config: Arc<ServerConfig> = try_arc_from_ptr!(config);
403+
let config: Arc<ServerConfig> = try_clone_arc!(config);
404404
match accepted.into_connection(config) {
405405
Ok(built) => {
406406
let wrapped = crate::connection::Connection::from_server(built);

src/client.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::rslice::NulByte;
2222
use crate::rslice::{rustls_slice_bytes, rustls_slice_slice_bytes, rustls_str};
2323
use crate::{
2424
ffi_panic_boundary, free_arc, free_box, set_boxed_mut_ptr, to_arc_const_ptr, to_boxed_mut_ptr,
25-
try_arc_from_ptr, try_box_from_ptr, try_mut_from_ptr, try_ref_from_ptr, try_slice,
26-
userdata_get, Castable, OwnershipArc, OwnershipBox,
25+
try_box_from_ptr, try_clone_arc, try_mut_from_ptr, try_ref_from_ptr, try_slice, userdata_get,
26+
Castable, OwnershipArc, OwnershipBox,
2727
};
2828

2929
/// A client config being constructed. A builder can be modified by,
@@ -442,7 +442,7 @@ impl rustls_client_config_builder {
442442
let keys_ptrs: &[*const rustls_certified_key] = try_slice!(certified_keys, certified_keys_len);
443443
let mut keys: Vec<Arc<CertifiedKey>> = Vec::new();
444444
for &key_ptr in keys_ptrs {
445-
let certified_key: Arc<CertifiedKey> = try_arc_from_ptr!(key_ptr);
445+
let certified_key: Arc<CertifiedKey> = try_clone_arc!(key_ptr);
446446
keys.push(certified_key);
447447
}
448448
config.cert_resolver = Some(Arc::new(ResolvesClientCertFromChoices { keys }));
@@ -545,7 +545,7 @@ impl rustls_client_config {
545545
}
546546
CStr::from_ptr(server_name)
547547
};
548-
let config: Arc<ClientConfig> = try_arc_from_ptr!(config);
548+
let config: Arc<ClientConfig> = try_clone_arc!(config);
549549
let server_name: &str = match server_name.to_str() {
550550
Ok(s) => s,
551551
Err(std::str::Utf8Error { .. }) => return rustls_result::InvalidDnsNameError,

src/lib.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -368,19 +368,16 @@ where
368368
Arc::into_raw(Arc::new(src)) as *const _
369369
}
370370

371-
/// Convert a const pointer to a [`Castable`] to an optional `Arc` over the underlying rust type.
371+
/// Given a const pointer to a [`Castable`] representing an `Arc`, clone the `Arc` and return
372+
/// the corresponding Rust type.
372373
///
373-
/// Sometimes we create an `Arc`, then call `into_raw` and return the resulting raw pointer
374-
/// to C, e.g. using [`to_arc_const_ptr`]. C can then call a rustls-ffi function multiple times
375-
/// using that same raw pointer. On each call, we need to reconstruct the Arc, but once we reconstruct
376-
/// the Arc, its reference count will be decremented on drop. We need to reference count to stay at 1,
377-
/// because the C code is holding a copy.
374+
/// The caller still owns its copy of the `Arc`. In other words, the reference count of the
375+
/// `Arc` will be incremented by 1 by the end of this function.
378376
///
379-
/// This function turns the raw pointer back into an Arc, clones it to increment the reference count
380-
/// (which will make it 2 in this particular case), and `mem::forget`s the clone. The `mem::forget`
381-
/// prevents the reference count from being decremented when we exit this function, so it will stay
382-
/// at 2 as long as we are in Rust code. Once the caller drops its `Arc`, the reference count will
383-
/// go back down to 1, since the C code's copy remains.
377+
/// To achieve that, we need to `mem::forget` the `Arc` we get back from `into_raw`, because
378+
/// `into_raw` _does_ take back ownership. If we called `into_raw` without `mem::forget`, at the
379+
/// end of the function that Arc would be dropped and the reference count would be decremented,
380+
/// potentially to 0, causing memory to be freed.
384381
///
385382
/// Does nothing, returning `None`, when passed a `NULL` pointer. Can only be used when the
386383
/// `Castable` has specified a cast type equal to [`OwnershipArc`].
@@ -389,7 +386,7 @@ where
389386
///
390387
/// If non-null, `ptr` must be a pointer that resulted from previously calling `Arc::into_raw`,
391388
/// e.g. from using [`to_arc_const_ptr`].
392-
pub(crate) fn to_arc<C>(ptr: *const C) -> Option<Arc<C::RustType>>
389+
pub(crate) fn clone_arc<C>(ptr: *const C) -> Option<Arc<C::RustType>>
393390
where
394391
C: Castable<Ownership = OwnershipArc>,
395392
{
@@ -566,32 +563,20 @@ macro_rules! try_ref_from_ptr {
566563

567564
pub(crate) use try_ref_from_ptr;
568565

569-
/// Convert a const pointer to a [`Castable`] to an optional `Arc` over the underlying
570-
/// [`Castable::RustType`]. See [`to_arc`] for more information.
571-
///
572-
/// Does nothing, returning `None`, when passed `NULL`. Can only be used with `Castable`'s that
573-
/// specify an ownership type of [`OwnershipArc`].
574-
pub(crate) fn try_arc_from<C>(from: *const C) -> Option<Arc<C::RustType>>
575-
where
576-
C: Castable<Ownership = OwnershipArc>,
577-
{
578-
to_arc(from)
579-
}
580-
581566
/// If the provided pointer to a [`Castable`] is non-null, convert it to a reference to an `Arc` over
582567
/// the underlying rust type using [`try_arc_from`]. Otherwise, return
583568
/// [`rustls_result::NullParameter`], or an appropriate default (`false`, `0`, `NULL`) based on the
584569
/// context. See [`try_arc_from`] for more information.
585-
macro_rules! try_arc_from_ptr {
570+
macro_rules! try_clone_arc {
586571
( $var:ident ) => {
587-
match $crate::try_arc_from($var) {
572+
match $crate::clone_arc($var) {
588573
Some(c) => c,
589574
None => return $crate::panic::NullParameterOrDefault::value(),
590575
}
591576
};
592577
}
593578

594-
pub(crate) use try_arc_from_ptr;
579+
pub(crate) use try_clone_arc;
595580

596581
/// Convert a mutable pointer to a [`Castable`] to an optional `Box` over the underlying
597582
/// [`Castable::RustType`].

src/server.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::session::{
3030
};
3131
use crate::{
3232
ffi_panic_boundary, free_arc, free_box, set_boxed_mut_ptr, to_arc_const_ptr, to_boxed_mut_ptr,
33-
try_arc_from_ptr, try_box_from_ptr, try_mut_from_ptr, try_ref_from_ptr, try_slice,
34-
userdata_get, Castable, OwnershipArc, OwnershipBox, OwnershipRef,
33+
try_box_from_ptr, try_clone_arc, try_mut_from_ptr, try_ref_from_ptr, try_slice, userdata_get,
34+
Castable, OwnershipArc, OwnershipBox, OwnershipRef,
3535
};
3636

3737
/// A server config being constructed. A builder can be modified by,
@@ -170,7 +170,7 @@ impl rustls_server_config_builder {
170170
) {
171171
ffi_panic_boundary! {
172172
let builder: &mut ServerConfigBuilder = try_mut_from_ptr!(builder);
173-
let verifier: Arc<AllowAnyAuthenticatedClient> = try_arc_from_ptr!(verifier);
173+
let verifier: Arc<AllowAnyAuthenticatedClient> = try_clone_arc!(verifier);
174174
builder.verifier = verifier;
175175
}
176176
}
@@ -186,7 +186,7 @@ impl rustls_server_config_builder {
186186
) {
187187
ffi_panic_boundary! {
188188
let builder: &mut ServerConfigBuilder = try_mut_from_ptr!(builder);
189-
let verifier: Arc<AllowAnyAnonymousOrAuthenticatedClient> = try_arc_from_ptr!(verifier);
189+
let verifier: Arc<AllowAnyAnonymousOrAuthenticatedClient> = try_clone_arc!(verifier);
190190

191191
builder.verifier = verifier;
192192
}
@@ -273,7 +273,7 @@ impl rustls_server_config_builder {
273273
let keys_ptrs: &[*const rustls_certified_key] = try_slice!(certified_keys, certified_keys_len);
274274
let mut keys: Vec<Arc<CertifiedKey>> = Vec::new();
275275
for &key_ptr in keys_ptrs {
276-
let certified_key: Arc<CertifiedKey> = try_arc_from_ptr!(key_ptr);
276+
let certified_key: Arc<CertifiedKey> = try_clone_arc!(key_ptr);
277277
keys.push(certified_key);
278278
}
279279
builder.cert_resolver = Some(Arc::new(ResolvesServerCertFromChoices::new(&keys)));
@@ -333,7 +333,7 @@ impl rustls_server_config {
333333
conn_out: *mut *mut rustls_connection,
334334
) -> rustls_result {
335335
ffi_panic_boundary! {
336-
let config: Arc<ServerConfig> = try_arc_from_ptr!(config);
336+
let config: Arc<ServerConfig> = try_clone_arc!(config);
337337

338338
let server_connection = match ServerConnection::new(config) {
339339
Ok(sc) => sc,

0 commit comments

Comments
 (0)