Skip to content

Commit 0a3ac83

Browse files
author
bors-servo
authored
Auto merge of #349 - wez:timertypes, r=jdm
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: wezterm/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.
2 parents 0718126 + a119788 commit 0a3ac83

File tree

8 files changed

+37
-38
lines changed

8 files changed

+37
-38
lines changed

cocoa/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "cocoa"
44
description = "Bindings to Cocoa for macOS"
55
homepage = "https://github.com/servo/core-foundation-rs"
66
repository = "https://github.com/servo/core-foundation-rs"
7-
version = "0.19.1"
7+
version = "0.20.0"
88
authors = ["The Servo Project Developers"]
99
license = "MIT / Apache-2.0"
1010

@@ -16,7 +16,7 @@ crate-type = ["rlib"]
1616
block = "0.1"
1717
bitflags = "1.0"
1818
libc = "0.2"
19-
core-foundation = { path = "../core-foundation", version = "0.6" }
20-
core-graphics = { path = "../core-graphics", version = "0.18" }
19+
core-foundation = { path = "../core-foundation", version = "0.7" }
20+
core-graphics = { path = "../core-graphics", version = "0.19" }
2121
foreign-types = "0.3"
2222
objc = "0.2.3"

core-foundation-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "core-foundation-sys"
33
description = "Bindings to Core Foundation for macOS"
44
homepage = "https://github.com/servo/core-foundation-rs"
55
repository = "https://github.com/servo/core-foundation-rs"
6-
version = "0.6.3"
6+
version = "0.7.0"
77
authors = ["The Servo Project Developers"]
88
license = "MIT / Apache-2.0"
99
build = "build.rs"

core-foundation-sys/src/runloop.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ pub const kCFRunLoopAllActivities: CFOptionFlags = 0x0FFFFFFF;
5050
pub struct CFRunLoopSourceContext {
5151
pub version: CFIndex,
5252
pub info: *mut c_void,
53-
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
54-
pub release: extern "C" fn (info: *const c_void),
55-
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
56-
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
57-
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
58-
pub schedule: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
59-
pub cancel: extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef),
53+
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
54+
pub release: Option<extern "C" fn (info: *const c_void)>,
55+
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
56+
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
57+
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
58+
pub schedule: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
59+
pub cancel: Option<extern "C" fn (info: *const c_void, rl: CFRunLoopRef, mode: CFStringRef)>,
6060
pub perform: extern "C" fn (info: *const c_void),
6161
}
6262

6363
#[repr(C)]
6464
pub struct CFRunLoopSourceContext1 {
6565
pub version: CFIndex,
6666
pub info: *mut c_void,
67-
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
68-
pub release: extern "C" fn (info: *const c_void),
69-
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
70-
pub equal: extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean,
71-
pub hash: extern "C" fn (info: *const c_void) -> CFHashCode,
67+
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
68+
pub release: Option<extern "C" fn (info: *const c_void)>,
69+
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
70+
pub equal: Option<extern "C" fn (info1: *const c_void, info2: *const c_void) -> Boolean>,
71+
pub hash: Option<extern "C" fn (info: *const c_void) -> CFHashCode>,
7272
// note that the following two fields are platform dependent in the C header, the ones here are for macOS
7373
pub getPort: extern "C" fn (info: *mut c_void) -> mach_port_t,
7474
pub perform: extern "C" fn (msg: *mut c_void, size: CFIndex, allocator: CFAllocatorRef, info: *mut c_void) -> *mut c_void,
@@ -78,9 +78,9 @@ pub struct CFRunLoopSourceContext1 {
7878
pub struct CFRunLoopObserverContext {
7979
pub version: CFIndex,
8080
pub info: *mut c_void,
81-
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
82-
pub release: extern "C" fn (info: *const c_void),
83-
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
81+
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
82+
pub release: Option<extern "C" fn (info: *const c_void)>,
83+
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
8484
}
8585

8686
pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverRef, activity: CFRunLoopActivity, info: *mut c_void);
@@ -89,15 +89,15 @@ pub type CFRunLoopObserverCallBack = extern "C" fn (observer: CFRunLoopObserverR
8989
pub struct CFRunLoopTimerContext {
9090
pub version: CFIndex,
9191
pub info: *mut c_void,
92-
pub retain: extern "C" fn (info: *const c_void) -> *const c_void,
93-
pub release: extern "C" fn (info: *const c_void),
94-
pub copyDescription: extern "C" fn (info: *const c_void) -> CFStringRef,
92+
pub retain: Option<extern "C" fn (info: *const c_void) -> *const c_void>,
93+
pub release: Option<extern "C" fn (info: *const c_void)>,
94+
pub copyDescription: Option<extern "C" fn (info: *const c_void) -> CFStringRef>,
9595
}
9696

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

9999
#[repr(C)]
100-
pub struct __CFRunLoopTimer;
100+
pub struct __CFRunLoopTimer(c_void);
101101

102102
pub type CFRunLoopTimerRef = *mut __CFRunLoopTimer;
103103

core-foundation/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ name = "core-foundation"
33
description = "Bindings to Core Foundation for macOS"
44
homepage = "https://github.com/servo/core-foundation-rs"
55
repository = "https://github.com/servo/core-foundation-rs"
6-
version = "0.6.5"
6+
version = "0.7.0"
77
authors = ["The Servo Project Developers"]
88
license = "MIT / Apache-2.0"
99
categories = ["os::macos-apis"]
1010
keywords = ["macos", "framework", "objc"]
1111

1212
[dependencies.core-foundation-sys]
1313
path = "../core-foundation-sys"
14-
version = "0.6.1"
14+
version = "0.7"
1515

1616
[dependencies]
1717
libc = "0.2"

core-foundation/src/runloop.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,13 @@ mod test {
165165
start_time: now,
166166
elapsed_tx,
167167
};
168-
let mut context = unsafe { CFRunLoopTimerContext {
168+
let mut context = CFRunLoopTimerContext {
169169
version: 0,
170170
info: &mut info as *mut _ as *mut c_void,
171-
retain: mem::zeroed(),
172-
release: mem::zeroed(),
173-
copyDescription: mem::zeroed(),
174-
} };
175-
171+
retain: None,
172+
release: None,
173+
copyDescription: None,
174+
};
176175

177176
let run_loop_timer = CFRunLoopTimer::new(now + 0.20f64, 0f64, 0, 0, timer_popped, &mut context);
178177
unsafe {

core-graphics/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "core-graphics"
33
description = "Bindings to Core Graphics for macOS"
44
homepage = "https://github.com/servo/core-graphics-rs"
55
repository = "https://github.com/servo/core-foundation-rs"
6-
version = "0.18.0"
6+
version = "0.19.0"
77
authors = ["The Servo Project Developers"]
88
license = "MIT / Apache-2.0"
99

@@ -14,6 +14,6 @@ highsierra = []
1414

1515
[dependencies]
1616
bitflags = "1.0"
17-
core-foundation = { path = "../core-foundation", version = "0.6" }
17+
core-foundation = { path = "../core-foundation", version = "0.7" }
1818
foreign-types = "0.3.0"
1919
libc = "0.2"

core-text/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "core-text"
3-
version = "14.0.0"
3+
version = "15.0.0"
44
authors = ["The Servo Project Developers"]
55
description = "Bindings to the Core Text framework."
66
license = "MIT/Apache-2.0"
@@ -15,5 +15,5 @@ mountainlion = []
1515
[dependencies]
1616
foreign-types = "0.3"
1717
libc = "0.2"
18-
core-foundation = { path = "../core-foundation", version = "0.6.2" }
19-
core-graphics = { path = "../core-graphics", version = "0.18" }
18+
core-foundation = { path = "../core-foundation", version = "0.7" }
19+
core-graphics = { path = "../core-graphics", version = "0.19" }

io-surface/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ name = "io-surface"
33
description = "Bindings to IO Surface for macOS"
44
homepage = "https://github.com/servo/core-foundation-rs"
55
repository = "https://github.com/servo/core-foundation-rs"
6-
version = "0.12.1"
6+
version = "0.13.0"
77
authors = ["The Servo Project Developers"]
88
license = "MIT / Apache-2.0"
99

1010
[dependencies]
1111
libc = "0.2"
1212
gleam = "0.7"
13-
core-foundation = { path = "../core-foundation", version = "0.6" }
13+
core-foundation = { path = "../core-foundation", version = "0.7" }
1414
cgl = "0.3"
1515
leaky-cow = "0.1.1"

0 commit comments

Comments
 (0)