Skip to content

Commit 36912b0

Browse files
committed
Change msg_send! such that callers can properly communicate mutability
This fixes a long-standing soundness issue with how message sending is done whilst mutating the receiver, see: SSheldon/rust-objc#112. We were effectively mutating behind either `&&mut T` or `&T`, where `T` is zero-sized and contains `UnsafeCell`, so while it is still uncertain exactly how much of an issue this actually is, the approach we use now is definitely sound! Also makes it clearer that `msg_send!` does not consume `Id`s, it only needs a reference to those.
1 parent 5bd6979 commit 36912b0

File tree

17 files changed

+186
-95
lines changed

17 files changed

+186
-95
lines changed

objc2-foundation/examples/custom_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ fn main() {
7070

7171
obj.set_number(7);
7272
println!("Number: {}", unsafe {
73-
let number: u32 = msg_send![obj, number];
73+
let number: u32 = msg_send![&obj, number];
7474
number
7575
});
7676

7777
unsafe {
78-
let _: () = msg_send![obj, setNumber: 12u32];
78+
let _: () = msg_send![&mut obj, setNumber: 12u32];
7979
}
8080
println!("Number: {}", obj.number());
8181
}

objc2-foundation/src/array.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,14 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
274274

275275
#[doc(alias = "removeLastObject")]
276276
pub fn pop(&mut self) -> Option<Id<T, O>> {
277-
self.last().map(|obj| {
278-
let obj = unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() };
279-
unsafe {
280-
let _: () = msg_send![self, removeLastObject];
281-
}
282-
obj
283-
})
277+
self.last()
278+
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
279+
.map(|obj| {
280+
unsafe {
281+
let _: () = msg_send![self, removeLastObject];
282+
}
283+
obj
284+
})
284285
}
285286

286287
#[doc(alias = "removeAllObjects")]

objc2-foundation/src/data.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ impl NSMutableData {
156156
impl NSMutableData {
157157
#[doc(alias = "mutableBytes")]
158158
pub fn bytes_mut(&mut self) -> &mut [u8] {
159-
let ptr: *mut c_void = unsafe { msg_send![self, mutableBytes] };
159+
let this = &mut *self; // Reborrow
160+
let ptr: *mut c_void = unsafe { msg_send![this, mutableBytes] };
160161
// The bytes pointer may be null for length zero
161162
if ptr.is_null() {
162163
&mut []

objc2-foundation/src/dictionary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
129129

130130
pub fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<V, Owned>, Shared> {
131131
unsafe {
132-
let vals = msg_send![dict, allValues];
132+
let vals = msg_send![&dict, allValues];
133133
Id::retain_autoreleased(vals).unwrap()
134134
}
135135
}

objc2-foundation/src/enumerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a, T: Message> Iterator for NSEnumerator<'a, T> {
3333
type Item = &'a T;
3434

3535
fn next(&mut self) -> Option<&'a T> {
36-
unsafe { msg_send![self.id, nextObject] }
36+
unsafe { msg_send![&mut self.id, nextObject] }
3737
}
3838
}
3939

objc2-foundation/src/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ mod tests {
177177
fn test_value_nsrange() {
178178
let val = NSValue::new(NSRange::from(1..2));
179179
assert!(NSRange::ENCODING.equivalent_to_str(val.encoding().unwrap()));
180-
let range: NSRange = unsafe { objc2::msg_send![val, rangeValue] };
180+
let range: NSRange = unsafe { objc2::msg_send![&val, rangeValue] };
181181
assert_eq!(range, NSRange::from(1..2));
182182
// NSValue -getValue is broken on GNUStep for some types
183183
#[cfg(not(feature = "gnustep-1-7"))]

objc2/CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3838
`ClassBuilder::add_method`.
3939
* Renamed `ClassDecl` and `ProtocolDecl` to `ClassBuilder` and
4040
`ProtocolBuilder`. The old names are kept as deprecated aliases.
41+
* **BREAKING**: Changed how `msg_send!` works wrt. capturing its arguments.
42+
43+
This will require changes to your code wherever you used `Id`, for example:
44+
```rust
45+
// Before
46+
let obj: Id<Object, Owned> = ...;
47+
let p: i32 = unsafe { msg_send![obj, parameter] };
48+
let _: () = unsafe { msg_send![obj, setParameter: p + 1] };
49+
// After
50+
let mut obj: Id<Object, Owned> = ...;
51+
let p: i32 = unsafe { msg_send![&obj, parameter] };
52+
let _: () = unsafe { msg_send![&mut obj, setParameter: p + 1] };
53+
```
54+
55+
Notice that we now clearly pass `obj` by reference, and therein also
56+
communicate the mutability of the object (in the first case, immutable, and
57+
in the second, mutable).
58+
59+
If you previously used `*mut Object` or `&Object` as the receiver, message
60+
sending should work exactly as before.
4161

4262
### Fixed
4363
* Properly sealed the `MessageArguments` trait (it already had a hidden

objc2/examples/introspection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ fn main() {
4242
}
4343

4444
// Invoke a method on the object
45-
let hash: usize = unsafe { msg_send![obj, hash] };
45+
let hash: usize = unsafe { msg_send![&obj, hash] };
4646
println!("NSObject hash: {}", hash);
4747
}

objc2/examples/talk_to_me.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ fn main() {
3131
let utterance: *mut Object = unsafe { msg_send![utterance, initWithString: &*string] };
3232
let utterance: Id<Object, Owned> = unsafe { Id::new(utterance).unwrap() };
3333

34-
// let _: () = unsafe { msg_send![utterance, setVolume: 90.0f32 };
35-
// let _: () = unsafe { msg_send![utterance, setRate: 0.50f32 };
36-
// let _: () = unsafe { msg_send![utterance, setPitchMultiplier: 0.80f32 };
34+
// let _: () = unsafe { msg_send![&utterance, setVolume: 90.0f32 };
35+
// let _: () = unsafe { msg_send![&utterance, setRate: 0.50f32 };
36+
// let _: () = unsafe { msg_send![&utterance, setPitchMultiplier: 0.80f32 };
3737

38-
let _: () = unsafe { msg_send![synthesizer, speakUtterance: &*utterance] };
38+
let _: () = unsafe { msg_send![&synthesizer, speakUtterance: &*utterance] };
3939
}

objc2/src/declare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ mod tests {
415415
#[test]
416416
fn test_custom_class() {
417417
// Registering the custom class is in test_utils
418-
let obj = test_utils::custom_object();
419-
let _: () = unsafe { msg_send![obj, setFoo: 13u32] };
420-
let result: u32 = unsafe { msg_send![obj, foo] };
418+
let mut obj = test_utils::custom_object();
419+
let _: () = unsafe { msg_send![&mut obj, setFoo: 13u32] };
420+
let result: u32 = unsafe { msg_send![&obj, foo] };
421421
assert_eq!(result, 13);
422422
}
423423

0 commit comments

Comments
 (0)