Skip to content

Commit

Permalink
Auto merge of #349 - wez:timertypes, r=jdm
Browse files Browse the repository at this point in the history
Fix CFRunLoop types to avoid UB

A number of callback functions associated with the CFRunLoop types are
allowed to be NULL to evoke default behavior.

Prior to this commit the definition of those structs prevented passing
in null which meant resorting to transmute or other tricks to
effectively set null, but with recent compiler versions the compiler
emits `ud2` instructions and triggers a fault at runtime.

The correct resolution for this is to define these fields as `Option`al
callbacks and that is what this commit does.

I've used this approach successfully here in another project:
Refs: wez/wezterm@398f333

I've bumped up the version for the crate as part of this commit
because it effectively changes the API around these structs.
  • Loading branch information
bors-servo committed Nov 12, 2019
2 parents 0718126 + a119788 commit 0a3ac83
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
6 changes: 3 additions & 3 deletions cocoa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "cocoa"
description = "Bindings to Cocoa for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.19.1"
version = "0.20.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"

Expand All @@ -16,7 +16,7 @@ crate-type = ["rlib"]
block = "0.1"
bitflags = "1.0"
libc = "0.2"
core-foundation = { path = "../core-foundation", version = "0.6" }
core-graphics = { path = "../core-graphics", version = "0.18" }
core-foundation = { path = "../core-foundation", version = "0.7" }
core-graphics = { path = "../core-graphics", version = "0.19" }
foreign-types = "0.3"
objc = "0.2.3"
2 changes: 1 addition & 1 deletion core-foundation-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "core-foundation-sys"
description = "Bindings to Core Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.6.3"
version = "0.7.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"
build = "build.rs"
Expand Down
38 changes: 19 additions & 19 deletions core-foundation-sys/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ pub const kCFRunLoopAllActivities: CFOptionFlags = 0x0FFFFFFF;
pub struct CFRunLoopSourceContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
pub schedule: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
pub cancel: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
pub schedule: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
pub cancel: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
pub perform: extern "C" fn (info: *const c_void),
}

#[repr(C)]
pub struct CFRunLoopSourceContext1 {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
// note that the following two fields are platform dependent in the C header, the ones here are for macOS
pub getPort: extern "C" fn (info: *mut c_void) -> mach_port_t,
pub perform: extern "C" fn (msg: *mut c_void, size: CFIndex, allocator: CFAllocatorRef, info: *mut c_void) -> *mut c_void,
Expand All @@ -78,9 +78,9 @@ pub struct CFRunLoopSourceContext1 {
pub struct CFRunLoopObserverContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
}

pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverRef, activity: CFRunLoopActivity, info: *mut c_void);
Expand All @@ -89,15 +89,15 @@ pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverR
pub struct CFRunLoopTimerContext {
pub version: CFIndex,
pub info: *mut c_void,
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
pub release: extern "C" fn (info: *const c_void),
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
pub release: Option<extern "C" fn (info: *const c_void)>,
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
}

pub type CFRunLoopTimerCallBack = extern "C" fn (timer: CFRunLoopTimerRef, info: *mut c_void);

#[repr(C)]
pub struct __CFRunLoopTimer;
pub struct __CFRunLoopTimer(c_void);

pub type CFRunLoopTimerRef = *mut __CFRunLoopTimer;

Expand Down
4 changes: 2 additions & 2 deletions core-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ name = "core-foundation"
description = "Bindings to Core Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.6.5"
version = "0.7.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"
categories = ["os::macos-apis"]
keywords = ["macos", "framework", "objc"]

[dependencies.core-foundation-sys]
path = "../core-foundation-sys"
version = "0.6.1"
version = "0.7"

[dependencies]
libc = "0.2"
Expand Down
11 changes: 5 additions & 6 deletions core-foundation/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,13 @@ mod test {
start_time: now,
elapsed_tx,
};
let mut context = unsafe { CFRunLoopTimerContext {
let mut context = CFRunLoopTimerContext {
version: 0,
info: &mut info as *mut _ as *mut c_void,
retain: mem::zeroed(),
release: mem::zeroed(),
copyDescription: mem::zeroed(),
} };

retain: None,
release: None,
copyDescription: None,
};

let run_loop_timer = CFRunLoopTimer::new(now + 0.20f64, 0f64, 0, 0, timer_popped, &mut context);
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions core-graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "core-graphics"
description = "Bindings to Core Graphics for macOS"
homepage = "https://github.com/servo/core-graphics-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.18.0"
version = "0.19.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"

Expand All @@ -14,6 +14,6 @@ highsierra = []

[dependencies]
bitflags = "1.0"
core-foundation = { path = "../core-foundation", version = "0.6" }
core-foundation = { path = "../core-foundation", version = "0.7" }
foreign-types = "0.3.0"
libc = "0.2"
6 changes: 3 additions & 3 deletions core-text/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-text"
version = "14.0.0"
version = "15.0.0"
authors = ["The Servo Project Developers"]
description = "Bindings to the Core Text framework."
license = "MIT/Apache-2.0"
Expand All @@ -15,5 +15,5 @@ mountainlion = []
[dependencies]
foreign-types = "0.3"
libc = "0.2"
core-foundation = { path = "../core-foundation", version = "0.6.2" }
core-graphics = { path = "../core-graphics", version = "0.18" }
core-foundation = { path = "../core-foundation", version = "0.7" }
core-graphics = { path = "../core-graphics", version = "0.19" }
4 changes: 2 additions & 2 deletions io-surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ name = "io-surface"
description = "Bindings to IO Surface for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.12.1"
version = "0.13.0"
authors = ["The Servo Project Developers"]
license = "MIT / Apache-2.0"

[dependencies]
libc = "0.2"
gleam = "0.7"
core-foundation = { path = "../core-foundation", version = "0.6" }
core-foundation = { path = "../core-foundation", version = "0.7" }
cgl = "0.3"
leaky-cow = "0.1.1"

0 comments on commit 0a3ac83

Please sign in to comment.