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