From 398f333c32fb1ff4ccd9cc001a66ae22522eb60c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 11 Nov 2019 19:10:20 -0800 Subject: [PATCH] macos: workaround UB and undefined instruction issue The compiler emitted `ud2` right around this code, effectively breaking wezterm on startup. In talking this through with @dtolnay, the definition of the structs in the core_foundation crate makes it undefined behavior to pass a null pointer to its fields, despite that being a valid and documented way to use the struct. This commit works around this by defining our own local versions of the types and functions with the safe signature. I'll follow up with the owners of the core_foundation crate to submit an equivalent patch upstream. --- window/src/os/macos/connection.rs | 47 ++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index 4a2f0d536..b1108b2d6 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -69,6 +69,45 @@ impl Connection { } } +/* Begin: workaround UB in CFRunLoopTimerContext struct. + * This can be removed once an equivalent change is upstreamed to the + * core_foundation crate */ + +use core_foundation::string::__CFString; +use std::ffi::c_void; + +#[repr(transparent)] +pub struct __CFRunLoopTimer(c_void); + +#[repr(C)] +#[allow(non_snake_case)] +pub struct CFRunLoopTimerContext { + pub version: i64, + pub info: *mut c_void, + pub retain: Option *const c_void>, + pub release: Option, + pub copyDescription: Option *const __CFString>, +} + +extern "C" { + fn CFRunLoopTimerCreate( + allocator: *const c_void, + fireDate: f64, + interval: f64, + flags: u32, + order: i64, + callout: extern "C" fn(*mut __CFRunLoopTimer, *mut c_void), + context: *mut CFRunLoopTimerContext, + ) -> *mut __CFRunLoopTimer; + fn CFRunLoopAddTimer( + rl: *mut __CFRunLoop, + timer: *mut __CFRunLoopTimer, + mode: *const __CFString, + ); +} + +/* End: UB workaround */ + impl ConnectionOps for Connection { fn terminate_message_loop(&self) { unsafe { @@ -103,7 +142,7 @@ impl ConnectionOps for Connection { let callback = Box::into_raw(Box::new(callback)); extern "C" fn timer_callback( - _timer_ref: CFRunLoopTimerRef, + _timer_ref: *mut __CFRunLoopTimer, callback_ptr: *mut std::ffi::c_void, ) { unsafe { @@ -126,10 +165,10 @@ impl ConnectionOps for Connection { 0, timer_callback::, &mut CFRunLoopTimerContext { - copyDescription: std::mem::MaybeUninit::zeroed().assume_init(), + copyDescription: None, info: callback as _, - release: release_callback::, - retain: std::mem::MaybeUninit::zeroed().assume_init(), + release: Some(release_callback::), + retain: None, version: 0, }, )