Skip to content

Commit c76f92d

Browse files
authored
Merge branch 'master' into linlin/lazy-allocation
2 parents bc25529 + 7afd499 commit c76f92d

6 files changed

Lines changed: 59 additions & 35 deletions

File tree

quiche/src/ffi.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,11 @@ pub extern "C" fn quiche_conn_new_with_tls_and_client_dcid(
649649
let local = std_addr_from_c(local, local_len);
650650
let peer = std_addr_from_c(peer, peer_len);
651651

652-
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
652+
let tls = match unsafe { tls::Handshake::from_ptr(ssl) } {
653+
Ok(v) => v,
654+
655+
Err(_) => return ptr::null_mut(),
656+
};
653657

654658
match Connection::with_tls(
655659
&scid,
@@ -706,7 +710,11 @@ pub extern "C" fn quiche_conn_new_with_tls(
706710
let local = std_addr_from_c(local, local_len);
707711
let peer = std_addr_from_c(peer, peer_len);
708712

709-
let tls = unsafe { tls::Handshake::from_ptr(ssl) };
713+
let tls = match unsafe { tls::Handshake::from_ptr(ssl) } {
714+
Ok(v) => v,
715+
716+
Err(_) => return ptr::null_mut(),
717+
};
710718

711719
match Connection::with_tls(
712720
&scid, retry_cids, None, local, peer, config, tls, is_server,

quiche/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ impl Config {
637637
pub fn with_boring_ssl_ctx_builder(
638638
version: u32, tls_ctx_builder: boring::ssl::SslContextBuilder,
639639
) -> Result<Config> {
640-
Self::with_tls_ctx(version, tls::Context::from_boring(tls_ctx_builder))
640+
Self::with_tls_ctx(version, tls::Context::from_boring(tls_ctx_builder)?)
641641
}
642642

643643
fn with_tls_ctx(version: u32, tls_ctx: tls::Context) -> Result<Config> {
@@ -2755,7 +2755,7 @@ impl<F: BufFactory> Connection<F> {
27552755
// a borrowed view of `ssl`. The caller retains ownership of the
27562756
// underlying BoringSSL object.
27572757
let mut handshake = ManuallyDrop::new(unsafe {
2758-
tls::Handshake::from_ptr(ssl.as_ptr() as _)
2758+
tls::Handshake::from_ptr(ssl.as_ptr() as _)?
27592759
});
27602760

27612761
handshake.set_quic_transport_params(&params, is_server)

quiche/src/tls/mod.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use std::ffi;
2828
use std::mem::ManuallyDrop;
2929
use std::ptr;
30+
use std::ptr::NonNull;
3031
use std::slice;
3132

3233
use std::io::Write;
@@ -128,14 +129,15 @@ pub static QUICHE_EX_DATA_INDEX: LazyLock<c_int> = LazyLock::new(|| unsafe {
128129
SSL_get_ex_new_index(0, ptr::null(), ptr::null(), ptr::null(), ptr::null())
129130
});
130131

131-
pub struct Context(*mut SSL_CTX);
132+
pub struct Context(NonNull<SSL_CTX>);
132133

133134
impl Context {
134135
// Note: some vendor-specific methods are implemented in the boringssl
135136
// submodule.
136137
pub fn new() -> Result<Context> {
137138
unsafe {
138-
let ctx_raw = SSL_CTX_new(TLS_method());
139+
let ctx_raw =
140+
NonNull::new(SSL_CTX_new(TLS_method())).ok_or(Error::TlsFail)?;
139141

140142
let mut ctx = Context(ctx_raw);
141143

@@ -150,18 +152,23 @@ impl Context {
150152
#[cfg(feature = "boringssl-boring-crate")]
151153
pub fn from_boring(
152154
ssl_ctx_builder: boring::ssl::SslContextBuilder,
153-
) -> Context {
155+
) -> Result<Context> {
154156
use foreign_types_shared::ForeignType;
155157

156-
let mut ctx = Context(ssl_ctx_builder.build().into_ptr() as _);
158+
let ctx_raw = NonNull::new(ssl_ctx_builder.build().into_ptr() as _)
159+
.ok_or(Error::TlsFail)?;
160+
161+
let mut ctx = Context(ctx_raw);
157162
ctx.set_session_callback();
158163

159-
ctx
164+
Ok(ctx)
160165
}
161166

162167
pub fn new_handshake(&mut self) -> Result<Handshake> {
163168
unsafe {
164-
let ssl = SSL_new(self.as_mut_ptr());
169+
let ssl =
170+
NonNull::new(SSL_new(self.as_mut_ptr())).ok_or(Error::TlsFail)?;
171+
165172
Ok(Handshake::new(ssl))
166173
}
167174
}
@@ -330,15 +337,13 @@ impl Context {
330337
}
331338

332339
fn as_mut_ptr(&mut self) -> *mut SSL_CTX {
333-
self.0
340+
self.0.as_ptr()
334341
}
335342
}
336343

337-
// NOTE: These traits are not automatically implemented for Context due to the
338-
// raw pointer it wraps. However, the underlying data is not aliased (as Context
339-
// should be its only owner), and there is no interior mutability, as the
340-
// pointer is not accessed directly outside of this module, and the Context
341-
// object API should preserve Rust's borrowing guarantees.
344+
// These traits are not automatically implemented because NonNull does not
345+
// convey ownership. Context uniquely owns the underlying data, and its API
346+
// preserves Rust's borrowing guarantees.
342347
unsafe impl Send for Context {}
343348
unsafe impl Sync for Context {}
344349

@@ -349,8 +354,7 @@ impl Drop for Context {
349354
}
350355

351356
pub struct Handshake {
352-
/// Raw pointer
353-
ptr: *mut SSL,
357+
ptr: NonNull<SSL>,
354358
/// SSL_process_quic_post_handshake should be called when whenever
355359
/// SSL_provide_quic_data is called to process the provided data.
356360
provided_data_outstanding: bool,
@@ -360,11 +364,13 @@ impl Handshake {
360364
// Note: some vendor-specific methods are implemented in the boringssl
361365
// submodule.
362366
#[cfg(any(feature = "ffi", feature = "boringssl-boring-crate"))]
363-
pub unsafe fn from_ptr(ssl: *mut c_void) -> Handshake {
364-
Handshake::new(ssl as *mut SSL)
367+
pub unsafe fn from_ptr(ssl: *mut c_void) -> Result<Handshake> {
368+
let ptr = NonNull::new(ssl.cast()).ok_or(Error::TlsFail)?;
369+
370+
Ok(Handshake::new(ptr))
365371
}
366372

367-
fn new(ptr: *mut SSL) -> Handshake {
373+
fn new(ptr: NonNull<SSL>) -> Handshake {
368374
Handshake {
369375
ptr,
370376
provided_data_outstanding: false,
@@ -590,11 +596,11 @@ impl Handshake {
590596
}
591597

592598
fn as_ptr(&self) -> *const SSL {
593-
self.ptr
599+
self.ptr.as_ptr()
594600
}
595601

596602
fn as_mut_ptr(&mut self) -> *mut SSL {
597-
self.ptr
603+
self.ptr.as_ptr()
598604
}
599605

600606
fn map_result_ssl(&mut self, bssl_result: c_int) -> Result<()> {
@@ -674,11 +680,9 @@ impl Handshake {
674680
}
675681
}
676682

677-
// NOTE: These traits are not automatically implemented for Handshake due to the
678-
// raw pointer it wraps. However, the underlying data is not aliased (as
679-
// Handshake should be its only owner), and there is no interior mutability, as
680-
// the pointer is not accessed directly outside of this module, and the
681-
// Handshake object API should preserve Rust's borrowing guarantees.
683+
// These traits are not automatically implemented because NonNull does not
684+
// convey ownership. Handshake uniquely owns the underlying data, and its API
685+
// preserves Rust's borrowing guarantees.
682686
unsafe impl Send for Handshake {}
683687
unsafe impl Sync for Handshake {}
684688

@@ -987,7 +991,13 @@ extern "C" fn select_alpn(
987991
}
988992

989993
extern "C" fn new_session(ssl: *mut SSL, session: *mut SSL_SESSION) -> c_int {
990-
let ex_data = match ExData::from_ssl_ptr(ssl) {
994+
let ssl = match NonNull::new(ssl) {
995+
Some(v) => v,
996+
997+
None => return 0,
998+
};
999+
1000+
let ex_data = match ExData::from_ssl_ptr(ssl.as_ptr()) {
9911001
Some(v) => v,
9921002

9931003
None => return 0,

tokio-quiche/src/quic/connection/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ where
445445
match handshake_fut.await {
446446
Ok(running) => Self::resume(running),
447447
Err(e) => {
448-
log::error!("QUIC handshake failed in IQC::start"; "error" => e)
448+
log::error!("QUIC handshake failed in IQC::start"; "error" => e);
449449
},
450450
}
451451
};

tokio-quiche/src/quic/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ where
269269
// drive the packet router:
270270
tokio::spawn(async move {
271271
match router.await {
272-
Ok(()) => log::debug!("incoming packet router finished"),
272+
Ok(()) => {
273+
log::debug!("incoming packet router finished");
274+
},
273275
Err(error) => {
274-
log::error!("incoming packet router failed"; "error"=>error)
276+
log::error!("incoming packet router failed"; "error"=>error);
275277
},
276278
}
277279
});
@@ -334,9 +336,11 @@ where
334336

335337
crate::metrics::tokio_task::spawn("quic_udp_listener", metrics, async move {
336338
match socket_driver.await {
337-
Ok(()) => log::trace!("incoming packet router finished"),
339+
Ok(()) => {
340+
log::trace!("incoming packet router finished");
341+
},
338342
Err(error) => {
339-
log::error!("incoming packet router failed"; "error"=>error)
343+
log::error!("incoming packet router failed"; "error"=>error);
340344
},
341345
}
342346
});

tokio-quiche/src/settings/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ impl Config {
7575
};
7676
let keylog_file = keylog_path.and_then(|path| if KEYLOGFILE_ENABLED {
7777
File::options().create(true).append(true).open(path)
78-
.inspect_err(|e| log::warn!("failed to open SSLKEYLOGFILE"; "error" => e))
78+
.inspect_err(|e| {
79+
log::warn!("failed to open SSLKEYLOGFILE"; "error" => e);
80+
})
7981
.ok()
8082
} else {
8183
log::warn!("SSLKEYLOGFILE is set, but `--cfg capture_keylogs` was not enabled. No keys will be logged.");

0 commit comments

Comments
 (0)