windows-core 0.101.0

Core type support for COM and Windows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use super::*;
use crate::{GUID, HRESULT, IUnknown, IUnknown_Vtbl, Interface};
use core::ffi::c_void;
use core::ptr::{null_mut, without_provenance_mut};
use core::sync::atomic::{AtomicPtr, Ordering, fence};

/// A COM reference count that moves into a weak reference tear-off on demand.
///
/// The value is either a strong count, stored as `count << 1 | 1` without provenance, or a pointer
/// to the tear-off that owns the strong count once `IWeakReferenceSource` has been requested.
/// The tear-off is pointer-aligned, so its low bit is always clear.
#[repr(transparent)]
pub struct WeakRefCount(AtomicPtr<c_void>);

const fn encode(count: usize) -> *mut c_void {
    without_provenance_mut((count << 1) | 1)
}

fn count(value: *mut c_void) -> Option<usize> {
    (value.addr() & 1 == 1).then_some(value.addr() >> 1)
}

impl Default for WeakRefCount {
    fn default() -> Self {
        Self::new()
    }
}

impl WeakRefCount {
    pub const fn new() -> Self {
        Self(AtomicPtr::new(encode(1)))
    }

    #[allow(deprecated)]
    pub fn add_ref(&self) -> u32 {
        match self
            .0
            .fetch_update(Ordering::Relaxed, Ordering::Acquire, |value| {
                count(value).map(|count| {
                    if count >= RefCount::MAX as usize {
                        RefCount::ref_count_overflow();
                    }
                    encode(count + 1)
                })
            }) {
            Ok(previous) => (previous.addr() >> 1) as u32 + 1,
            Err(tear_off) => unsafe { (*tear_off.cast::<TearOff>()).strong_count.add_ref() },
        }
    }

    #[inline(always)]
    pub fn is_one(&self) -> bool {
        self.0.load(Ordering::Acquire) == encode(1)
    }

    #[allow(deprecated)]
    pub fn release(&self) -> u32 {
        match self
            .0
            .fetch_update(Ordering::Release, Ordering::Acquire, |value| {
                count(value).map(|count| {
                    assert!(count != 0, "Object has been over-released.");
                    encode(count - 1)
                })
            }) {
            Ok(previous) => {
                let remaining = (previous.addr() >> 1) as u32 - 1;

                if remaining == 0 {
                    fence(Ordering::Acquire);
                }

                remaining
            }
            Err(tear_off) => unsafe { TearOff::release_strong(tear_off.cast()) },
        }
    }

    /// # Safety
    ///
    /// `object` must be the COM identity pointer associated with this reference count and remain
    /// valid while the strong count is nonzero.
    /// `agile` must match the object's configured agility for every query.
    pub unsafe fn query(&self, iid: &GUID, object: *mut c_void, agile: bool) -> *mut c_void {
        if iid != &IWeakReferenceSource::IID {
            return null_mut();
        }

        let mut current = self.0.load(Ordering::Acquire);
        let mut tear_off: *mut TearOff = null_mut();

        unsafe {
            loop {
                let Some(count) = count(current) else {
                    // Another caller installed a tear-off first.
                    if !tear_off.is_null() {
                        drop(Box::from_raw(tear_off));
                    }

                    return TearOff::add_strong(current.cast());
                };

                if tear_off.is_null() {
                    tear_off = TearOff::new(object, count as u32, agile);
                } else {
                    (*tear_off).strong_count = RefCount::new(count as u32);
                }

                match self.0.compare_exchange_weak(
                    current,
                    tear_off.cast(),
                    Ordering::AcqRel,
                    Ordering::Acquire,
                ) {
                    Ok(_) => return TearOff::add_strong(tear_off),
                    Err(actual) => current = actual,
                }
            }
        }
    }
}

/// The weak reference tear-off.
///
/// `IWeakReferenceSource` points at `strong_vtable` and `IWeakReference` at `weak_vtable`. Both
/// interface pointers are derived from the whole allocation so either can recover the tear-off.
/// The tear-off is shared across threads and only accessed through raw pointers and atomics.
#[repr(C)]
struct TearOff {
    strong_vtable: *const IWeakReferenceSource_Vtbl,
    weak_vtable: *const IWeakReference_Vtbl,
    object: *mut c_void,
    strong_count: RefCount,
    weak_count: RefCount,
}

impl TearOff {
    fn new(object: *mut c_void, strong_count: u32, agile: bool) -> *mut Self {
        #[cfg(windows_cast_diagnostics)]
        live_objects::WEAK_REFERENCES.add();
        Box::into_raw(Box::new(Self {
            strong_vtable: &Self::STRONG_VTABLE,
            weak_vtable: if agile {
                &Self::WEAK_VTABLE
            } else {
                &Self::NON_AGILE_WEAK_VTABLE
            },
            object,
            strong_count: RefCount::new(strong_count),
            weak_count: RefCount::new(1),
        }))
    }

    const STRONG_VTABLE: IWeakReferenceSource_Vtbl = IWeakReferenceSource_Vtbl {
        base__: IUnknown_Vtbl {
            QueryInterface: Self::StrongQueryInterface,
            AddRef: Self::StrongAddRef,
            Release: Self::StrongRelease,
        },
        GetWeakReference: Self::StrongDowngrade,
    };

    const WEAK_VTABLE: IWeakReference_Vtbl = IWeakReference_Vtbl {
        base__: IUnknown_Vtbl {
            QueryInterface: Self::WeakQueryInterface::<true>,
            AddRef: Self::WeakAddRef,
            Release: Self::WeakRelease,
        },
        Resolve: Self::WeakUpgrade,
    };

    const NON_AGILE_WEAK_VTABLE: IWeakReference_Vtbl = IWeakReference_Vtbl {
        base__: IUnknown_Vtbl {
            QueryInterface: Self::WeakQueryInterface::<false>,
            ..Self::WEAK_VTABLE.base__
        },
        ..Self::WEAK_VTABLE
    };

    fn strong(this: *mut Self) -> *mut c_void {
        this.cast()
    }

    unsafe fn weak(this: *mut Self) -> *mut c_void {
        unsafe { (&raw mut (*this).weak_vtable).cast() }
    }

    fn from_strong(ptr: *mut c_void) -> *mut Self {
        ptr.cast()
    }

    unsafe fn from_weak(ptr: *mut c_void) -> *mut Self {
        unsafe { ptr.cast::<*const IWeakReference_Vtbl>().sub(1).cast() }
    }

    /// Adds a strong reference and returns the `IWeakReferenceSource` pointer.
    unsafe fn add_strong(this: *mut Self) -> *mut c_void {
        unsafe {
            (*this).strong_count.add_ref();
        }
        Self::strong(this)
    }

    unsafe fn release_strong(this: *mut Self) -> u32 {
        unsafe {
            let remaining = (*this).strong_count.release();

            // The last strong reference also releases its implied weak reference.
            if remaining == 0 {
                Self::release_weak(this);
            }

            remaining
        }
    }

    unsafe fn release_weak(this: *mut Self) -> u32 {
        unsafe {
            let remaining = (*this).weak_count.release();

            // The object has already been destroyed, so the tear-off is no longer reachable.
            if remaining == 0 {
                drop(Box::from_raw(this));
            }

            remaining
        }
    }

    unsafe fn object(this: *mut Self) -> (*mut c_void, &'static IUnknown_Vtbl) {
        unsafe {
            let object = (*this).object;
            // The object may be packed even though its vtable is aligned.
            let vtable = object.cast::<*const IUnknown_Vtbl>().read_unaligned();
            (object, &*vtable)
        }
    }

    unsafe extern "system" fn StrongQueryInterface(
        ptr: *mut c_void,
        iid: *const GUID,
        interface: *mut *mut c_void,
    ) -> HRESULT {
        unsafe {
            if iid.is_null() || interface.is_null() {
                return E_POINTER;
            }

            if *iid == IWeakReferenceSource::IID {
                *interface = Self::add_strong(Self::from_strong(ptr));
                return HRESULT(0);
            }

            // The tear-off shares the object's identity, so other queries go to the object.
            let (object, vtable) = Self::object(Self::from_strong(ptr));
            (vtable.QueryInterface)(object, iid, interface)
        }
    }

    unsafe extern "system" fn WeakQueryInterface<const AGILE: bool>(
        ptr: *mut c_void,
        iid: *const GUID,
        interface: *mut *mut c_void,
    ) -> HRESULT {
        unsafe {
            let this = Self::from_weak(ptr);

            if iid.is_null() || interface.is_null() {
                return E_POINTER;
            }

            // The weak reference has its own COM identity and does not delegate to the object.
            *interface = if *iid == IWeakReference::IID
                || *iid == IUnknown::IID
                || (AGILE && *iid == IAgileObject::IID)
            {
                ptr
            } else {
                #[cfg(windows)]
                if AGILE && *iid == IMarshal::IID {
                    (*this).weak_count.add_ref();
                    return marshaler(IUnknown::from_raw(ptr), interface);
                }

                null_mut()
            };

            if (*interface).is_null() {
                E_NOINTERFACE
            } else {
                (*this).weak_count.add_ref();
                HRESULT(0)
            }
        }
    }

    unsafe extern "system" fn StrongAddRef(ptr: *mut c_void) -> u32 {
        unsafe { (*Self::from_strong(ptr)).strong_count.add_ref() }
    }

    unsafe extern "system" fn WeakAddRef(ptr: *mut c_void) -> u32 {
        unsafe { (*Self::from_weak(ptr)).weak_count.add_ref() }
    }

    unsafe extern "system" fn StrongRelease(ptr: *mut c_void) -> u32 {
        unsafe {
            // The object owns destruction. Its release decrements the tear-off's strong count
            // and releases the implied weak reference when it reaches zero.
            let (object, vtable) = Self::object(Self::from_strong(ptr));
            (vtable.Release)(object)
        }
    }

    unsafe extern "system" fn WeakRelease(ptr: *mut c_void) -> u32 {
        unsafe { Self::release_weak(Self::from_weak(ptr)) }
    }

    unsafe extern "system" fn StrongDowngrade(
        ptr: *mut c_void,
        interface: *mut *mut c_void,
    ) -> HRESULT {
        unsafe {
            // A strong reference implies a weak reference, so this cannot fail.
            let this = Self::from_strong(ptr);
            (*this).weak_count.add_ref();
            *interface = Self::weak(this);
            HRESULT(0)
        }
    }

    unsafe extern "system" fn WeakUpgrade(
        ptr: *mut c_void,
        iid: *const GUID,
        interface: *mut *mut c_void,
    ) -> HRESULT {
        unsafe {
            let this = Self::from_weak(ptr);

            // Stabilize the object for the duration of the `QueryInterface` call.
            if !(*this).strong_count.try_add_ref() {
                *interface = null_mut();
                return HRESULT(0);
            }

            let (object, vtable) = Self::object(this);
            let result = (vtable.QueryInterface)(object, iid, interface);

            // Release through the object so that it is destroyed if this was the last reference.
            (vtable.Release)(object);
            result
        }
    }
}

#[cfg(windows_cast_diagnostics)]
impl Drop for TearOff {
    fn drop(&mut self) {
        live_objects::WEAK_REFERENCES.remove();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn count_limit_does_not_wrap() {
        let count = WeakRefCount(AtomicPtr::new(encode(RefCount::MAX as usize - 1)));
        assert_eq!(count.add_ref(), RefCount::MAX);
        assert!(!count.is_one());
        assert_eq!(count.release(), RefCount::MAX - 1);

        let count = RefCount::new(RefCount::MAX - 1);
        assert_eq!(count.add_ref(), RefCount::MAX);
        assert_eq!(count.release(), RefCount::MAX - 1);
        assert!(count.try_add_ref());
        assert_eq!(count.release(), RefCount::MAX - 1);
        assert!(!RefCount::new(0).try_add_ref());
    }

    #[test]
    #[cfg(not(miri))]
    fn count_overflow_aborts() {
        extern crate std;
        use alloc::string::String;

        const CHILD: &str = "WINDOWS_CORE_TEST_REFCOUNT_OVERFLOW";
        if let Ok(case) = std::env::var(CHILD) {
            // Catching an ordinary panic must not allow the corrupted count to be reused.
            let _ = std::panic::catch_unwind(|| match case.as_str() {
                "inline" => {
                    WeakRefCount(AtomicPtr::new(encode(RefCount::MAX as usize))).add_ref();
                }
                "tear_off_or_delegate" => {
                    RefCount::new(RefCount::MAX).add_ref();
                }
                "upgrade" => {
                    RefCount::new(RefCount::MAX).try_add_ref();
                }
                _ => panic!("unknown overflow test"),
            });
            return;
        }

        for case in ["inline", "tear_off_or_delegate", "upgrade"] {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "imp::weak_ref_count::tests::count_overflow_aborts",
                    "--nocapture",
                ])
                .env(CHILD, case)
                .output()
                .unwrap();
            assert!(!output.status.success(), "{case}");
            assert!(
                String::from_utf8_lossy(&output.stderr).contains("COM reference count overflow."),
                "{case}: {output:?}"
            );
        }
    }
}