|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#ifdef TARGET_WINDOWS |
| 5 | +#include "windows.h" |
| 6 | +#else |
| 7 | +#include "dlfcn.h" |
| 8 | +#endif |
| 9 | +#include <cstdint> |
| 10 | +#include <string> |
| 11 | +#include <memory> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +#ifndef TARGET_WINDOWS |
| 15 | +#define __stdcall |
| 16 | +#endif |
| 17 | + |
| 18 | +// typedef for shared lib exported methods |
| 19 | +using f_MultiplyIntegers = int32_t(__stdcall *)(int32_t, int32_t); |
| 20 | +using f_getBaseDirectory = const char*(__stdcall *)(); |
| 21 | + |
| 22 | +#ifdef TARGET_WINDOWS |
| 23 | +template<typename T> |
| 24 | +struct CoTaskMemDeleter |
| 25 | +{ |
| 26 | + void operator()(T* p) const |
| 27 | + { |
| 28 | + CoTaskMemFree((void*)p); |
| 29 | + } |
| 30 | +}; |
| 31 | +template<typename T> |
| 32 | +using CoTaskMemPtr = std::unique_ptr<T, CoTaskMemDeleter<T>>; |
| 33 | +#else |
| 34 | +template<typename T> |
| 35 | +using CoTaskMemPtr = std::unique_ptr<T>; |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifdef TARGET_WINDOWS |
| 39 | +int __cdecl main(int argc, char* argv[]) |
| 40 | +#else |
| 41 | +int main(int argc, char* argv[]) |
| 42 | +#endif |
| 43 | +{ |
| 44 | + std::string pathToSubdir = argv[0]; |
| 45 | + // Step out of the current directory and the parent directory. |
| 46 | + pathToSubdir = pathToSubdir.substr(0, pathToSubdir.find_last_of("/\\")); |
| 47 | + pathToSubdir = pathToSubdir.substr(0, pathToSubdir.find_last_of("/\\")); |
| 48 | +#ifdef TARGET_WINDOWS |
| 49 | + pathToSubdir += "subdir\\"; |
| 50 | + // We need to include System32 to find system dependencies of SharedLibraryDependencyLoading.dll |
| 51 | + HINSTANCE handle = LoadLibraryEx("..\\subdir\\SharedLibraryDependencyLoading.dll", nullptr, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 52 | +#else |
| 53 | +#if TARGET_APPLE |
| 54 | + constexpr char const* ext = ".dylib"; |
| 55 | +#else |
| 56 | + constexpr char const* ext = ".so"; |
| 57 | +#endif |
| 58 | + |
| 59 | + pathToSubdir += "subdir/"; |
| 60 | + std::string path = pathToSubdir + "SharedLibraryDependencyLoading"; |
| 61 | + path += ext; |
| 62 | + void* handle = dlopen(path.c_str(), RTLD_LAZY); |
| 63 | +#endif |
| 64 | + |
| 65 | + if (!handle) |
| 66 | + return 1; |
| 67 | + |
| 68 | +#ifdef TARGET_WINDOWS |
| 69 | + f_MultiplyIntegers multiplyIntegers = (f_MultiplyIntegers)GetProcAddress(handle, "MultiplyIntegers"); |
| 70 | +#else |
| 71 | + f_MultiplyIntegers multiplyIntegers = (f_MultiplyIntegers)dlsym(handle, "MultiplyIntegers"); |
| 72 | +#endif |
| 73 | + |
| 74 | + if (multiplyIntegers(10, 7) != 70) |
| 75 | + return 2; |
| 76 | + |
| 77 | + CoTaskMemPtr<const char> baseDirectory; |
| 78 | +#ifdef TARGET_WINDOWS |
| 79 | + f_getBaseDirectory getBaseDirectory = (f_getBaseDirectory)GetProcAddress(handle, "GetBaseDirectory"); |
| 80 | +#else |
| 81 | + f_getBaseDirectory getBaseDirectory = (f_getBaseDirectory)dlsym(handle, "GetBaseDirectory"); |
| 82 | +#endif |
| 83 | + |
| 84 | + baseDirectory.reset(getBaseDirectory()); |
| 85 | + if (baseDirectory == nullptr) |
| 86 | + return 3; |
| 87 | + |
| 88 | + if (pathToSubdir != baseDirectory.get()) |
| 89 | + { |
| 90 | + std::cout << "Expected base directory: " << pathToSubdir << std::endl; |
| 91 | + std::cout << "Actual base directory: " << baseDirectory.get() << std::endl; |
| 92 | + return 4; |
| 93 | + } |
| 94 | + |
| 95 | + return 100; |
| 96 | +} |
| 97 | + |
| 98 | +extern "C" const char* __stdcall __asan_default_options() |
| 99 | +{ |
| 100 | + // NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library. |
| 101 | + // Disable leak detection as we don't care about these leaks as of now. |
| 102 | + return "detect_leaks=0 use_sigaltstack=0"; |
| 103 | +} |
0 commit comments