X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/eb6b6ca394357805f2bdba989abae309f718b4d8..f427ee49d309d8fc33ebf3042c3a775f2f530ded:/tests/intrusive_shared_ptr_src/abi.caller.smart.cpp diff --git a/tests/intrusive_shared_ptr_src/abi.caller.smart.cpp b/tests/intrusive_shared_ptr_src/abi.caller.smart.cpp new file mode 100644 index 000000000..a37fe144a --- /dev/null +++ b/tests/intrusive_shared_ptr_src/abi.caller.smart.cpp @@ -0,0 +1,31 @@ +// +// This tests that we can call functions implemented using raw pointers from +// an API vending itself as returning shared pointers, because both are ABI +// compatible. +// +// In this TU, SharedPtr is intrusive_shared_ptr, since USE_SHARED_PTR +// is defined. +// + +#define USE_SHARED_PTR + +#include +#include +#include "abi_helper.h" + +static_assert(sizeof(SharedPtr) == sizeof(T*)); +static_assert(alignof(SharedPtr) == alignof(T*)); + +// Receive a shared pointer from a function that actually returns a raw pointer +T_DECL(abi_caller_smart, "intrusive_shared_ptr.abi.caller.smart") { + T obj{3}; + T* expected = &obj; + SharedPtr result = return_raw_as_shared(expected); + CHECK(result.get() == expected); + + // Sometimes the test above passes even though it should fail, if the + // right address happens to be on the stack in the right location. This + // can happen if abi.caller.raw is run just before this test. This second + // test makes sure it fails when it should. + CHECK(result->i == 3); +}