]> git.saurik.com Git - apple/xnu.git/blob - tests/intrusive_shared_ptr_src/abi.caller.smart.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / intrusive_shared_ptr_src / abi.caller.smart.cpp
1 //
2 // This tests that we can call functions implemented using raw pointers from
3 // an API vending itself as returning shared pointers, because both are ABI
4 // compatible.
5 //
6 // In this TU, SharedPtr<T> is intrusive_shared_ptr<T>, since USE_SHARED_PTR
7 // is defined.
8 //
9
10 #define USE_SHARED_PTR
11
12 #include <libkern/c++/intrusive_shared_ptr.h>
13 #include <darwintest.h>
14 #include "abi_helper.h"
15
16 static_assert(sizeof(SharedPtr<T>) == sizeof(T*));
17 static_assert(alignof(SharedPtr<T>) == alignof(T*));
18
19 // Receive a shared pointer from a function that actually returns a raw pointer
20 T_DECL(abi_caller_smart, "intrusive_shared_ptr.abi.caller.smart") {
21 T obj{3};
22 T* expected = &obj;
23 SharedPtr<T> result = return_raw_as_shared(expected);
24 CHECK(result.get() == expected);
25
26 // Sometimes the test above passes even though it should fail, if the
27 // right address happens to be on the stack in the right location. This
28 // can happen if abi.caller.raw is run just before this test. This second
29 // test makes sure it fails when it should.
30 CHECK(result->i == 3);
31 }