okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
16e08b50c
main
1// Windows selftest for the named-section backing of PinnedArena (the replicas' shared expert arena). 2// Parent: two arenas on one section name (same bytes, header refused for another pack / a bigger size, the load state word), 3// then a CHILD PROCESS that opens the same section, reads what the parent wrote and writes back; the parent closes its first 4// arena while the second still holds the section (the pages must stay), and a third arena joins the live section. After the 5// last close, a new arena with the same name starts empty (the section is gone with its last handle). 6#ifdef _WIN32 7#include "strata/core/pinned.hpp" 8 9#include <windows.h> 10 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <string> 15#include <vector> 16 17namespace { 18constexpr uint64_t kBytes = 64u <<10 ; 19constexpr uint64_t kPackHash = 0x0123456789abcdefull ; 20}// namespace 21 22int main (int argc ,char ** argv ) { 23const std::vector < uint64_t > no_bounds ; 24if (argc >=3 && std::strcmp (argv [1 ],"child" )== 0 ) { 25const std::string name = argv [2 ]; 26 strata::core::PinnedArena a (kBytes ,no_bounds ,0 ,name ,kPackHash ); 27if (!a .valid ()) { std::fprintf (stderr ,"child: cannot open: %s\n" ,a .note .c_str ());return 2 ; } 28if (a .data ()[123 ]!= 0x5a ) { std::fprintf (stderr ,"child: did not see the parent's byte\n" );return 3 ; } 29if (a .shared_state == nullptr || a .shared_state -> load ()!= 4242u ) { std::fprintf (stderr ,"child: state word is not the parent's\n" );return 4 ; } 30a .data ()[456 ]= 0xa5 ; 31a .shared_state -> store (~uint64_t {0 });// (filled) 32return 0 ; 33 } 34 35int fail = 0 ; 36const std::string name = "win:Local\\strata-pinned-test-" + std::to_string ((unsigned long long )GetCurrentProcessId ()); 37 { 38auto * first = new strata::core::PinnedArena (kBytes ,no_bounds ,0 ,name ,kPackHash ); 39if (!first -> valid ()) { 40 std::fprintf (stderr ,"first section failed: %s\n" ,first -> note .c_str ()); 41return 1 ; 42 } 43first -> data ()[123 ]= 0x5a ; 44if (first -> shared_state == nullptr ) { std::fprintf (stderr ,"no state word\n" );fail = 1 ; } 45else if (first -> shared_state -> load ()!= 0 ) { std::fprintf (stderr ,"a new section's state word is not 0\n" );fail = 1 ; } 46else first -> shared_state -> store (4242u ); 47 48auto * second = new strata::core::PinnedArena (kBytes ,no_bounds ,0 ,name ,kPackHash ); 49if (!second -> valid ()) { std::fprintf (stderr ,"second section failed: %s\n" ,second -> note .c_str ());fail = 1 ; } 50else { 51if (second -> data ()[123 ]!= 0x5a ) { std::fprintf (stderr ,"same name did not expose the same bytes\n" );fail = 1 ; } 52if (second -> shared_state == nullptr || second -> shared_state -> load ()!= 4242u ) { std::fprintf (stderr ,"state word not shared\n" );fail = 1 ; } 53 } 54 { 55 strata::core::PinnedArena wrong_pack (kBytes ,no_bounds ,0 ,name ,kPackHash + 1 ); 56if (wrong_pack .valid ()|| wrong_pack .note .find ("pack hash" )== std::string::npos ) { 57 std::fprintf (stderr ,"pack mismatch was not refused: %s\n" ,wrong_pack .note .c_str ()); 58fail = 1 ; 59 } 60 strata::core::PinnedArena wrong_size (kBytes * 2 ,no_bounds ,0 ,name ,kPackHash ); 61if (wrong_size .valid ()) { std::fprintf (stderr ,"a bigger size was not refused\n" );fail = 1 ; } 62 } 63 64// a child process opens the live section 65char exe [MAX_PATH ]; 66GetModuleFileNameA (nullptr ,exe ,MAX_PATH ); 67 std::string cmd = std::string ("\"" )+ exe + "\" child \"" + name + "\"" ; 68STARTUPINFOA si {}; 69si .cb = sizeof si ; 70PROCESS_INFORMATION pi {}; 71 std::vector < char > cl (cmd .begin (),cmd .end ()); 72cl .push_back ('\0' ); 73if (!CreateProcessA (nullptr ,cl .data (),nullptr ,nullptr , FALSE,0 ,nullptr ,nullptr ,& si ,& pi )) { 74 std::fprintf (stderr ,"cannot start the child (error %lu)\n" ,GetLastError ()); 75fail = 1 ; 76 }else { 77WaitForSingleObject (pi .hProcess ,120000 ); 78DWORD code = 99 ; 79GetExitCodeProcess (pi .hProcess ,& code ); 80CloseHandle (pi .hProcess ); 81CloseHandle (pi .hThread ); 82if (code != 0 ) { std::fprintf (stderr ,"the child failed with %lu\n" ,code );fail = 1 ; } 83if (first -> data ()[456 ]!= 0xa5 ) { std::fprintf (stderr ,"the child's byte did not arrive\n" );fail = 1 ; } 84if (first -> shared_state -> load ()!= ~uint64_t {0 }) { std::fprintf (stderr ,"the child's state word did not arrive\n" );fail = 1 ; } 85 } 86 87delete first ;// the creator is gone; the second still holds the section 88if (second != nullptr && second -> valid ()) { 89if (second -> data ()[123 ]!= 0x5a || second -> data ()[456 ]!= 0xa5 ) { std::fprintf (stderr ,"bytes lost when the first arena closed\n" );fail = 1 ; } 90 strata::core::PinnedArena third (kBytes ,no_bounds ,0 ,name ,kPackHash ); 91if (!third .valid ()|| third .data ()[123 ]!= 0x5a ) { std::fprintf (stderr ,"a late arena did not join the live section\n" );fail = 1 ; } 92 } 93delete second ; 94 } 95 { 96 strata::core::PinnedArena again (kBytes ,no_bounds ,0 ,name ,kPackHash );// (every handle was closed: a new, empty section) 97if (!again .valid ()|| again .data ()[123 ]!= 0 || again .shared_state -> load ()!= 0 ) { 98 std::fprintf (stderr ,"the section survived its last handle\n" ); 99fail = 1 ; 100 } 101 } 102 std::printf ("pinned_shared_win_test: %s\n" ,fail ?"FAILED" :"OK" ); 103return fail ; 104} 105#else 106int main () {return 77 ; }// (skipped: the Linux build has pinned_shared_test) 107#endif