okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// pinned_alias_test (#3431, Windows HIP): the GPU alias of every range the expert arena registers is reserved in the process's 2// own address space while the arena lives, and a range whose alias covers memory already in use is not kept registered - a 3// copy through that memory must still reach the device and come back. Needs an AMD GPU on Windows, else it skips (77). 4#include "strata/core/pinned.hpp" 5 6#include <cuda_runtime.h> 7 8#include <cstdint> 9#include <cstdio> 10#include <cstring> 11#include <vector> 12 13#if defined(_WIN32 ) 14#include <windows.h> 15#endif 16 17#if defined(_WIN32 )&& defined(STRATA_USE_HIP ) 18static int g_fail = 0 ; 19#define CHECK (c ) \ 20 do { \ 21 if (!(c)) { std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++g_fail; } \ 22 } while (0) 23 24static bool address_free (const void * p ) { 25MEMORY_BASIC_INFORMATION m {}; 26return VirtualQuery (p ,& m ,sizeof m )== sizeof m && m .State == MEM_FREE ; 27} 28 29static void * alias_of (void * host ) { 30void * d = nullptr ; 31if (cudaHostGetDevicePointer (& d ,host ,0 )!= cudaSuccess ) { 32 (void )cudaGetLastError (); 33return nullptr ; 34 } 35return d ; 36} 37 38// 16 bytes host -> device -> host through `buf` 39static bool round_trip (uint8_t * buf ) { 40const uint32_t pat [4 ]= {0x11223344u ,0x55667788u ,0x99aabbccu ,0xddeeff00u }; 41void * d = nullptr ; 42if (cudaMalloc (& d ,64 )!= cudaSuccess )return false; 43 std::memcpy (buf ,pat ,sizeof pat ); 44 std::memset (buf + 64 ,0 ,sizeof pat ); 45const bool ok = cudaMemcpy (d ,buf ,sizeof pat ,cudaMemcpyHostToDevice )== cudaSuccess && 46cudaMemcpy (buf + 64 ,d ,sizeof pat ,cudaMemcpyDeviceToHost )== cudaSuccess && 47 std::memcmp (buf + 64 ,pat ,sizeof pat )== 0 ; 48cudaFree (d ); 49return ok ; 50} 51#endif 52 53int main () { 54#if !(defined(_WIN32 )&& defined(STRATA_USE_HIP )) 55 std::printf ("pinned_alias_test: SKIP (Windows HIP only)\n" ); 56return 77 ; 57#else 58using namespace strata::core ; 59if (cudaFree (0 )!= cudaSuccess ) { 60 std::printf ("pinned_alias_test: SKIP (no GPU)\n" ); 61return 77 ; 62 } 63 (void )cudaGetLastError (); 64const uint64_t slice = 64ull <<20 ,bytes = 4 * slice ; 65const std::vector < uint64_t > bounds {0 ,slice ,2 * slice ,3 * slice ,4 * slice }; 66 67// 1. while the arena lives, nothing can be placed under the alias of what it registered; afterwards the range is free again 68void * first_alias = nullptr ; 69 { 70PinnedArena a (bytes ,bounds ); 71CHECK (a .valid ()); 72CHECK (a .registered_bytes == bytes ); 73first_alias = alias_of (a .base ); 74CHECK (first_alias != nullptr ); 75if (first_alias != nullptr && first_alias != a .base ) { 76CHECK (!a .alias_claims .empty ()); 77CHECK (!address_free (first_alias )); 78CHECK (!address_free ((uint8_t * )first_alias + bytes - 4096 )); 79 } 80 } 81if (first_alias == nullptr ) { 82 std::printf ("pinned_alias_test: %d FAILED\n" ,g_fail ); 83return 1 ; 84 } 85CHECK (address_free (first_alias )); 86 87// 2. memory already in use under the alias (in the engine: the stack of the thread that pins the arena). The runtime hands 88// the same alias out again, so the next arena's first range would cover it: that range must stay unregistered, and a 89// copy through the memory must work. Without the guard the copy reads and writes the arena instead (round trip zeros). 90uint8_t * victim = (uint8_t * )VirtualAlloc (first_alias ,1u <<20 ,MEM_RESERVE |MEM_COMMIT ,PAGE_READWRITE ); 91CHECK (victim != nullptr ); 92if (victim != nullptr ) { 93PinnedArena b (bytes ,bounds ); 94CHECK (b .valid ()); 95bool covered = false; 96for (uint64_t off = 0 ;off < b .registered_bytes ;off += slice ) { 97uint8_t * al = (uint8_t * )alias_of ((uint8_t * )b .base + off ); 98if (al != nullptr && al <=victim && victim < al + slice )covered = true; 99 } 100CHECK (!covered ); 101if (b .registered_bytes == bytes ) 102 std::printf ("pinned_alias_test: note: the runtime chose another alias this time; the refusal was not exercised\n" ); 103else 104CHECK (b .note .find ("#3431" )!= std::string::npos ); 105CHECK (round_trip (victim )); 106VirtualFree (victim ,0 ,MEM_RELEASE ); 107 } 108 std::printf (g_fail == 0 ?"pinned_alias_test: OK\n" :"pinned_alias_test: %d FAILED\n" ,g_fail ); 109return g_fail == 0 ?0 :1 ; 110#endif 111}