|
| 1 | +//==----- dllmain.cpp --- verify behaviour of lib on process termination ---==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +/* |
| 10 | + * This test calls DllMain on Windows. This means, the process performs actions |
| 11 | + * which are required for library unload. That said, the test requires to be a |
| 12 | + * distinct binary executable. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <CL/sycl.hpp> |
| 16 | +#include <helpers/CommonRedefinitions.hpp> |
| 17 | +#include <helpers/PiImage.hpp> |
| 18 | +#include <helpers/PiMock.hpp> |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#ifdef _WIN32 |
| 23 | +#include <windows.h> |
| 24 | + |
| 25 | +extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, |
| 26 | + LPVOID lpReserved); |
| 27 | + |
| 28 | +static std::atomic<int> TearDownCalls{0}; |
| 29 | + |
| 30 | +pi_result redefinedTearDown(void *PluginParameter) { |
| 31 | + fprintf(stderr, "intercepted tear down\n"); |
| 32 | + ++TearDownCalls; |
| 33 | + |
| 34 | + return PI_SUCCESS; |
| 35 | +} |
| 36 | +#endif |
| 37 | + |
| 38 | +TEST(Windows, DllMainCall) { |
| 39 | +#ifdef _WIN32 |
| 40 | + sycl::platform Plt{sycl::default_selector()}; |
| 41 | + if (Plt.is_host()) { |
| 42 | + printf("Test is not supported on host, skipping\n"); |
| 43 | + return; |
| 44 | + } |
| 45 | + sycl::unittest::PiMock Mock{Plt}; |
| 46 | + setupDefaultMockAPIs(Mock); |
| 47 | + Mock.redefine<sycl::detail::PiApiKind::piTearDown>(redefinedTearDown); |
| 48 | + |
| 49 | + // Teardown calls are only expected on sycl.dll library unload, not when |
| 50 | + // process gets terminated. |
| 51 | + // The first call to DllMain is to simulate library unload. The second one |
| 52 | + // is to simulate process termination |
| 53 | + fprintf(stderr, "Call DllMain for the first time\n"); |
| 54 | + DllMain((HINSTANCE)0, DLL_PROCESS_DETACH, (LPVOID)NULL); |
| 55 | + |
| 56 | + int TearDownCallsDone = TearDownCalls.load(); |
| 57 | + |
| 58 | + EXPECT_NE(TearDownCallsDone, 0); |
| 59 | + |
| 60 | + fprintf(stderr, "Call DllMain for the second time\n"); |
| 61 | + DllMain((HINSTANCE)0, DLL_PROCESS_DETACH, (LPVOID)0x01); |
| 62 | + |
| 63 | + EXPECT_EQ(TearDownCalls.load(), TearDownCallsDone); |
| 64 | +#endif |
| 65 | +} |
0 commit comments