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
use std::mem;

/// This trait supports structs allocated by C but managed by Rust.
///
/// This is useful for commonly-used data types that have a fixed size, to avoid allocating
/// the data type itself on the heap.
///
/// This approach uses a "regular" Rust type in the Rust code, and a C type with a `_reserved`
/// field to reserve the space on the C stack.  The tricky bit is to convince the C code to
/// allocate enough space to store the Rust value.  There is no constant way to determine the space
/// required for a Rust value, but it is possible to make a conservative guess, possibly leaving
/// some unused space.  The suggested C type is `struct CType([u64; N])` for some N large enough to
/// contain the Rust type on the required platforms.  In C, this type would be defined as `struct
/// ctype_t { _reserved uint64_t[N]; }` for the same N.  The types must also have the same alignment.
///
/// This type contains debug assertions regarding the size of the Rust and C types, and will fail
/// at runtime if the alignment or size of the two types is not as required.
///
/// This type provides two functions useful for initialization of a type: `to_out_param` takes an
/// "out arg" pointing to an uninitialized value, and initializes it; while `return_val` simply
/// returns a struct value that can be used to initialize a variable.  Both function similarly,
/// so choose the one that makes the most senes for your API.  For example, a constructor which
/// can also return an error may prefer to put the error in the return value and use initialize.
///
/// # Safety
///
/// C allows uninitialized values, while Rust does not.  Be careful in the documentation for the C
/// API to ensure that values are properly initialized before they are used.
pub trait OpaqueStruct: Sized {
    /// The C representation of this type.  This must have the same alignment as Self
    /// and its size must not be less than that of Self.
    type CType: Sized;

    /// Get the value of this type used to represent a NULL pointer.
    ///
    /// For types that have a natural zero value, this can provide a shortcut for a C caller:
    /// instead of initializing a struct with the zero value and passing a pointer to it, the
    /// caller can simply pass NULL.
    ///
    /// The default implementation panics.
    fn null_value() -> Self {
        panic!("NULL pointer is not allowed")
    }

    /// Call the contained function with a shared reference to the data type.
    ///
    /// # Safety
    ///
    /// * for types defining [`OpaqueStruct::null_value`]: cptr must be NULL or point to a valid CType value
    /// * for types not defining [`OpaqueStruct::null_value`]: cptr must not be NULL and must point to a valid
    ///   CType value
    /// * no other thread may mutate the value pointed to by cptr until `with_ref` returns.
    /// * ownership of the value remains with the caller.
    unsafe fn with_ref<T, F: Fn(&Self) -> T>(cptr: *const Self::CType, f: F) -> T {
        check_size_and_alignment::<Self::CType, Self>();
        if cptr.is_null() {
            return f(&Self::null_value());
        }

        // SAFETY:
        // - casting to a pointer type with the same alignment and smaller size
        f(unsafe { &*(cptr as *const Self) })
    }

    /// Call the contained function with an exclusive reference to the data type.
    ///
    /// # Safety
    ///
    /// * for types defining [`OpaqueStruct::null_value`]: cptr must be NULL or point to a valid CType value
    /// * for types not defining [`OpaqueStruct::null_value`]: cptr must not be NULL and must point to a valid
    ///   CType value
    /// * no other thread may access the value pointed to by cptr until with_ref_mut returns.
    /// * ownership of the value remains with the caller.
    unsafe fn with_ref_mut<T, F: Fn(&mut Self) -> T>(cptr: *mut Self::CType, f: F) -> T {
        check_size_and_alignment::<Self::CType, Self>();
        if cptr.is_null() {
            let mut null = Self::null_value();
            return f(&mut null);
        }

        // SAFETY:
        // - casting to a pointer type with the same alignment and smaller size
        f(unsafe { &mut *(cptr as *mut Self) })
    }

    /// Initialize the value pointed to cptr with rval, "moving" rval into the pointer.
    ///
    /// If the pointer is NULL, rval is dropped.  Use [`OpaqueStruct::to_out_param_nonnull`] to panic in this
    /// situation.
    ///
    /// # Safety
    ///
    /// * if cptr is not NULL, then it must be aligned for CType and must have enough space for
    ///   CType.
    /// * to avoid a leak, the value must eventually be moved out of *cptr and into a Rust value
    ///   to be dropped (see [`OpaqueStruct::take`])
    unsafe fn to_out_param(self, cptr: *mut Self::CType) {
        check_size_and_alignment::<Self::CType, Self>();
        if !cptr.is_null() {
            // SAFETY:
            // - casting to a pointer type with the same alignment and smaller size
            // - MaybeUninit<Self> has the same layout as Self
            let rref = unsafe { &mut *(cptr as *mut mem::MaybeUninit<Self>) };
            rref.write(self);
        }
    }

    /// Initialize the value pointed to cptr with rval, "moving" rval into the pointer.
    ///
    /// If the pointer is NULL, this method will panic.
    ///
    /// # Safety
    ///
    /// * cptr must not be NULL, must be aligned for CType, and must have enough space for CType.
    /// * to avoid a leak, the value must eventually be moved out of *cptr and into a Rust value
    ///   to be dropped (see [`OpaqueStruct::take`])
    unsafe fn to_out_param_nonnull(self, cptr: *mut Self::CType) {
        check_size_and_alignment::<Self::CType, Self>();
        if cptr.is_null() {
            panic!("out param pointer is NULL");
        }

        // SAFETY:
        // - casting to a pointer type with the same alignment and smaller size
        // - MaybeUninit<Self> has the same layout as Self
        let rref = unsafe { &mut *(cptr as *mut mem::MaybeUninit<Self>) };
        rref.write(self);
    }

    /// Return a CType containing self, moving self in the process.
    ///
    /// # Safety
    ///
    /// * to avoid a leak, ownership of the value must eventually be returned to Rust.
    unsafe fn return_val(self) -> Self::CType {
        check_size_and_alignment::<Self::CType, Self>();
        // create a new value of type Self::CType, uninitialized, and make a pointer to it
        let mut cval = mem::MaybeUninit::<Self::CType>::uninit();
        let cptr = &mut cval as *mut mem::MaybeUninit<Self::CType>;

        // create a pointer to self
        let selfptr = (&mem::MaybeUninit::<Self>::new(self)) as *const mem::MaybeUninit<Self>;

        // cast cptr to a pointer to Self
        // SAFETY:
        // - casting to a pointer type with the same alignment and smaller size
        let dest = unsafe { cptr as *mut mem::MaybeUninit<Self> };

        // copy the data
        // SAFETY:
        // - selfptr is valid for a read of 1 x Self (it's of type MaybeUninit, but was
        //   initialized)
        // - dest is valid for write of 1 x Self
        // - both are properly aligned (Rust ensures this)
        unsafe { std::ptr::copy(selfptr, dest, 1) };

        // SAFETY: dest pointed to cval, which is now valid
        unsafe { cval.assume_init() }
    }

    /// Take a CType and return an owned value.
    ///
    /// This method is intended for C API functions that take the value by value and are
    /// documented as taking ownership of the value.  However, this means that C retains
    /// an expired "copy" of the value and could lead to use-after-free errors.
    ///
    /// Where compatible with the API design, prefer to use pointers in the C API and use
    /// [`OpaqueStruct::take_ptr`] to ensure the old value is invalidated.
    ///
    /// # Safety
    ///
    /// * cval must be a valid CType value
    unsafe fn take(cval: Self::CType) -> Self {
        check_size_and_alignment::<Self::CType, Self>();

        // SAFETY:
        //  - cval is a valid instance of CType, so its bytes interpreted as Self are valid
        //  (see docstring)
        //  - CType is larger than Self (guaranteed by check_size_and_alignment)
        let rval = unsafe { mem::transmute_copy(&cval) };
        // cval is still a valid value, but its bits have been copied, so indicate to Rust that it
        // is no longer needed and its Drop should not run.  In typical usage CType does not have a
        // Drop implementation anyway.
        mem::forget(cval);
        rval
    }

    /// Take a pointer to a CType and return an owned value.
    ///
    /// This is intended for C API functions that take a value by reference (pointer), but still
    /// "take ownership" of the value.  It leaves behind an invalid value, where any non-padding
    /// bytes of the Rust type are zeroed.  This makes use-after-free errors in the C code more
    /// likely to crash instead of silently working.  Which is about as good as it gets in C.
    ///
    /// Do _not_ pass a pointer to a Rust value to this function:
    ///
    /// ```ignore
    /// let rust_value = RustType::take_ptr(&mut c_value); // BAD!
    /// ```
    ///
    /// This creates undefined behavior as Rust will assume `c_value` is still initialized. Use
    /// `take` in this situation.
    ///
    /// # Safety
    ///
    /// * for types defining [`OpaqueStruct::null_value`]: cptr must be NULL or point to a valid CType value
    /// * for types not defining [`OpaqueStruct::null_value`]: cptr must not be NULL and must point to a valid
    ///   CType value
    /// * the memory pointed to by cptr is uninitialized when this function returns.
    unsafe fn take_ptr(cptr: *mut Self::CType) -> Self {
        check_size_and_alignment::<Self::CType, Self>();
        if cptr.is_null() {
            return Self::null_value();
        }

        // convert cptr to a reference to MaybeUninit<Self> (which is, for the moment,
        // actually initialized)

        // SAFETY:
        // - casting to a pointer type with the same alignment and smaller size
        let rref = unsafe { &mut *(cptr as *mut mem::MaybeUninit<Self>) };
        let mut owned = mem::MaybeUninit::<Self>::zeroed();
        // swap the actual value for the zeroed value
        mem::swap(rref, &mut owned);

        // SAFETY:
        //  - owned contains what cptr was pointing to, which the caller guaranteed to be valid
        unsafe { owned.assume_init() }
    }
}

/// Verify the size and alignment requirements are met.  These will compile to nothing if the
/// requirements are met, and will compile to `debug_assert!(false)` if they are not met, causing
/// all trait methods to panic.  That should be enough to get someone's attention!
fn check_size_and_alignment<CType: Sized, RType: Sized>() {
    debug_assert!(mem::size_of::<RType>() <= mem::size_of::<CType>());
    debug_assert!(mem::align_of::<RType>() == mem::align_of::<CType>());
}

mod test {
    mod size_panic {
        use crate::opaque::*;
        struct TwoInts(u64, u64);
        struct OneInt(u64);

        impl OpaqueStruct for TwoInts {
            type CType = OneInt; // uhoh! smaller than TwoInts!
        }

        #[test]
        #[should_panic]
        fn test() {
            let cval = OneInt(10);
            unsafe {
                TwoInts::with_ref(&cval as *const OneInt, |_rval| {});
            }
        }
    }

    mod align_panic {
        use crate::opaque::*;
        struct OneInt(u64);
        struct EightBytes([u8; 8]);

        impl OpaqueStruct for OneInt {
            type CType = EightBytes; // uhoh! different alignment than OneInt!
        }

        #[test]
        #[should_panic]
        fn test() {
            let cval = EightBytes([0u8; 8]);
            unsafe {
                OneInt::with_ref(&cval as *const EightBytes, |_rval| {});
            }
        }
    }

    mod init_and_use {
        use crate::opaque::*;
        struct RType(u32, u64);
        struct CType([u64; 3]); // NOTE: larger than RType

        impl OpaqueStruct for RType {
            type CType = CType;
        }

        #[test]
        fn intialize_and_with_methods() {
            unsafe {
                let mut cval = mem::MaybeUninit::<CType>::uninit();
                RType(10, 20).to_out_param(cval.as_mut_ptr());
                let mut cval = cval.assume_init();

                RType::with_ref(&cval, |rref| {
                    assert_eq!(rref.0, 10);
                    assert_eq!(rref.1, 20);
                });

                RType::with_ref_mut(&mut cval, |rref| {
                    assert_eq!(rref.0, 10);
                    assert_eq!(rref.1, 20);
                    rref.0 = 30;
                });

                RType::with_ref(&cval, |rref| {
                    assert_eq!(rref.0, 30);
                    assert_eq!(rref.1, 20);
                });

                RType::take(cval); // ..and implicitly drop
            }
        }

        #[test]
        fn return_val_and_with_methods() {
            unsafe {
                let mut cval = RType(10, 20).return_val();

                RType::with_ref(&cval, |rref| {
                    assert_eq!(rref.0, 10);
                    assert_eq!(rref.1, 20);
                });

                RType::with_ref_mut(&mut cval, |rref| {
                    assert_eq!(rref.0, 10);
                    assert_eq!(rref.1, 20);
                    rref.0 = 30;
                });

                RType::with_ref(&cval, |rref| {
                    assert_eq!(rref.0, 30);
                    assert_eq!(rref.1, 20);
                });

                RType::take(cval); // ..and implicitly drop
            }
        }

        #[test]
        fn take_ptr() {
            unsafe {
                // allocate enough bytes for a cval without initializing them
                let cval = Box::new(mem::MaybeUninit::<CType>::uninit());
                let cvalptr = Box::into_raw(cval) as *mut CType;

                // initialize the value
                RType(10, 20).to_out_param(cvalptr);

                // take the value and leave behind zeroed memory
                let rval = RType::take_ptr(cvalptr);
                assert_eq!(rval.0, 10);
                assert_eq!(rval.1, 20);

                // Verify that the memory is zeroed -- don't do this IRL!  NOTE: in practice only
                // the non-padding bytes of the value are actually zeroed, so we cannot assert that
                // all of the bytes pointed to by cvalptr are zero.
                let zeroedref = unsafe { &*(cvalptr as *const RType) };
                assert_eq!(zeroedref.0, 0);
                assert_eq!(zeroedref.1, 0);

                // deallocate by turning cvalptr back into a Box and dropping the Box, but
                // using MaybeUninit to prevent dropping the (invalid) enclosed CType.
                unsafe { Box::from_raw(cvalptr as *mut mem::MaybeUninit<CType>) };
            }
        }
    }
}