Files
WoW-Hybrid-gateway/tests/wine_session_retirement_test.py

343 lines
29 KiB
Python

#!/usr/bin/env python3
"""Compile actual source-owned retirement functions against a synthetic Win32 host."""
import re,subprocess,tempfile,unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
def function(source,name):
match=re.search(r'(?m)^[^\n;{}]*\b'+re.escape(name)+r'\([^\n;]*\)\s*\{',source)
if not match:raise AssertionError('Missing production function '+name)
start=match.start();position=source.index('{',match.start());depth=1;end=position+1
while depth:
if source[end]=='{':depth+=1
elif source[end]=='}':depth-=1
end+=1
return source[start:end]
class RetirementSourceTests(unittest.TestCase):
def test_source_owned_generation_retirement(self):
account=(ROOT/'native/wine_wire_account.c').read_text()
pipe=(ROOT/'native/wine_pipe_host.c').read_text()
policy=(ROOT/'native/wine_channel_policy.c').read_text()
realm=(ROOT/'native/wine_engine_realm.c').read_text()
coordinator=(ROOT/'native/wine_session_retirement.c').read_text()
frame=(ROOT/'native/wine_wire_windows.c').read_text()
globals=[]
for source in [account,pipe,policy,realm]:
globals.extend(line for line in source.splitlines() if re.match(r'__declspec\(dllexport\) struct \w+ gw_wire_\w+=',line) and 'session_pipe_' not in line)
# Data table spans multiple lines, included separately with its wrappers.
begin=pipe.index('uint32_t WINAPI gw_wire_pipe_generation(')
end=pipe.index('static int retirement_readable(',begin)
callbacks=pipe[begin:end]
prefix=r'''
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WINAPI
#ifndef __declspec
#define __declspec(x)
#endif
#define GW_SOURCE_ONLY_PROFILE 2
#define MemoryBarrier() __sync_synchronize()
typedef int32_t LONG;typedef void* HANDLE;typedef int SRWLOCK;
typedef uint32_t DWORD;
typedef struct {int unused;} CRITICAL_SECTION;
#define SRWLOCK_INIT 0
#define WAIT_OBJECT_0 0
#define WAIT_TIMEOUT 258
#define SecureZeroMemory(p,n) memset(p,0,n)
#include "wine_wire_host.h"
#include "wine_pipe_host.h"
#include "wine_channel_policy.h"
#include "wine_engine_realm.h"
#include "wine_session_retirement.h"
static unsigned asserts,reads,writes,closes,frame_resets;static int worker_signaled=1,busy=0,public_rows_ready=1,auto_disabled;
static SRWLOCK channel_lock,lock,worker_lock;
_Alignas(8) static unsigned char owner_object[0xd00];static void* fixture_owner;static uint64_t holder_value;
static unsigned native_depth,pipe_depth,native_entries,native_exits;
static int native_busy,owner_readable=1,change_owner_on_native_enter;
static int original_cleanup_result,send_discard_result;static unsigned original_cleanups,send_discards;
struct GwWirePreOpenHook {uint32_t abi,size;LONG ready,error;void* owner;DWORD primary_thread,reserved;void* bridge_module;void* callback;};
static struct GwWirePreOpenHook gw_wire_preopen_hook_data;
static DWORD GetCurrentThreadId(void){return 36;}
#define CURRENT_OWNER fixture_owner
static LONG InterlockedCompareExchange(volatile LONG* p,LONG value,LONG expected){LONG old=*p;if(old==expected)*p=value;return old;}
static LONG InterlockedExchange(volatile LONG* p,LONG value){LONG old=*p;*p=value;return old;}
static int public_rows_retirement_ready(uint32_t expected){(void)expected;return public_rows_ready;}
static void public_rows_retire_generation(void){public_rows_ready=0;}
struct FixtureFrameData {LONG command,completed,initialize_status,error;};
static struct FixtureFrameData gw_wire_frame_data;
static void wire_frame_retire_generation(void){++frame_resets;gw_wire_frame_data.command=2;}
static unsigned WaitForSingleObject(HANDLE h,unsigned ms){(void)h;(void)ms;return worker_signaled?WAIT_OBJECT_0:WAIT_TIMEOUT;}
static int CloseHandle(HANDLE h){(void)h;++closes;return 1;}
static void AcquireSRWLockExclusive(SRWLOCK* p){(void)p;}
static int TryAcquireSRWLockExclusive(SRWLOCK* p){
if(p==&lock){if(!native_depth)abort();if(busy)return 0;++pipe_depth;return 1;}
return !busy;
}
static void ReleaseSRWLockExclusive(SRWLOCK* p){if(p==&lock){if(!native_depth||pipe_depth!=1)abort();--pipe_depth;}}
static int TryEnterCriticalSection(CRITICAL_SECTION* p){
if(p!=(CRITICAL_SECTION*)(owner_object+0x70)||(!native_depth&&pipe_depth))abort();
if(!native_depth&&native_busy)return 0;
++native_depth;++native_entries;
if(change_owner_on_native_enter){fixture_owner=0;change_owner_on_native_enter=0;}
return 1;
}
static void LeaveCriticalSection(CRITICAL_SECTION* p){
if(p!=(CRITICAL_SECTION*)(owner_object+0x70)||!native_depth||(native_depth==1&&pipe_depth))abort();
--native_depth;++native_exits;
}
struct gw_native_realm gw_wire_auth_realms[64];
static volatile LONG retirement_complete;
static LONG active;static uint32_t credentials_generation;static HANDLE worker_handle;
static uint64_t prepared_channel;
static uint32_t preference_generation;static unsigned char preference_digest[32];static uint32_t ui_auth_generation;static LONG identity_note_generation,gw_wire_realm_entered_original,gw_wire_realm_exit_gate;
static struct gw_native_realm prepared_realm;static uint32_t prepared_realm_index,prepared_profile_kind;
static uint16_t prepared_port;static struct gw_wire_endpoint_profile owned_endpoint;
static volatile LONG pipe_generation=1;static unsigned char challenge[8],incoming[65533],outgoing[65535],converted[1048576],queued[1048580];
static uint64_t last_source_ping_ms;
static uint32_t challenge_size;static unsigned protection_waits;static uint64_t protection_started;static int awaiting_world;
static int retirement_readable(const void* p,size_t n){return p && n && n<=sizeof(owner_object);}
static int readable(const void* p,size_t n){return retirement_readable(p,n)&&(!(p==owner_object&&n==0x230)||owner_readable);}
static void club_source_clear(void){}
static void clear_transfer_bridge(void){}
static void spell_barrier_reset(void){}
static void WINAPI pipe_read_current(void* p,const struct gw_pipe_view* v,uint32_t epoch){(void)p;(void)v;if(epoch==(uint32_t)pipe_generation && gw_wire_pipe_receiver_allowed((uintptr_t)p))++reads;}
static void WINAPI pipe_write_current(void* p,const struct gw_pipe_view* v,int n,uint32_t epoch){(void)p;(void)v;(void)n;if(epoch==(uint32_t)pipe_generation && gw_wire_pipe_receiver_allowed((uintptr_t)p))++writes;}
#define CHECK(x) do{++asserts;if(!(x)){fprintf(stderr,"assertion line%d\n",__LINE__);return 1;}}while(0)
'''
functions=[]
for name in ['credentials_wipe','account_worker_busy_locked','gw_wire_account_active','gw_wire_account_generation','gw_wire_account_retirement_ready','gw_wire_account_retire_generation']:
functions.append(function(account,name))
for name in ['clear_world_failure','gw_wire_pipe_retirement_lock','gw_wire_pipe_retirement_unlock','gw_wire_pipe_retirement_ready','gw_wire_pipe_retire_generation']:
functions.append(function(pipe,name).replace('*(void**)0x145e7a098ULL','fixture_owner'))
functions.extend(function(policy,n) for n in ['gw_wire_channel_retirement_lock','gw_wire_channel_retirement_unlock','gw_wire_channel_retirement_ready','gw_wire_channel_retire_generation'])
functions.append(function(realm,'gw_wire_realm_retire_generation'))
functions.append(function(coordinator,'gw_wire_session_finish_native_close'))
functions.extend(function(coordinator,n) for n in ['gw_wire_session_retirement_request_if_terminal','gw_wire_session_retire'])
functions.append(function(frame,'wire_frame_retirement_poll'))
# Keep the fixture scoped to globals referenced by the extracted
# production functions; unrelated diagnostics have private types.
referenced=set(re.findall(r'\bgw_wire_\w+\b','\n'.join(functions)))
globals=[line for line in globals if re.search(r'\b(gw_wire_\w+)=',line).group(1) in referenced]
data='\n'.join(globals)+'\nstruct gw_session_retirement_control gw_wire_session_retirement_data={.abi=1,.size=56,.next_credentials_generation=1};\n'
# The real coordinator is compiled below. Original engine methods and
# allocations have their own focused fixtures; these doubles enforce
# their call boundary and lock order without invoking an engine.
data+=r'''
static int gw_native_disconnect_readable(const void* p,size_t n){
return (p==owner_object&&n<=sizeof(owner_object)) || (p==&holder_value&&n==8);
}
static int gw_native_disconnect_retained_primary(void* owner,DWORD thread){
if(owner!=owner_object||thread!=36||!native_depth||pipe_depth||active||!retirement_complete||prepared_channel)abort();
if(original_cleanup_result)return original_cleanup_result;
if(*(uint64_t*)(owner_object+0x1a0)){++original_cleanups;*(uint64_t*)(owner_object+0x1a0)=0;holder_value=0;}
return 0;
}
static int gw_closed_send_queues_discard(void* owner){
if(owner!=owner_object||!native_depth||pipe_depth||*(uint64_t*)(owner_object+0x1a0)||holder_value)abort();
++send_discards;if(send_discard_result)return send_discard_result;
for(unsigned i=0;i<2;++i){void** h=(void**)(owner_object+0x1e8+i*0x18);h[0]=h;h[1]=(void*)((uintptr_t)h|1);}
return 0;
}
'''
cases=r'''
static void canonical_empty(void){
void** head=(void**)(owner_object+0x200);head[0]=head;head[1]=(void*)((uintptr_t)head|1);
}
static void setup(uint32_t generation){
if(native_depth||pipe_depth||native_entries!=native_exits)abort();
native_busy=0;owner_readable=1;change_owner_on_native_enter=0;
gw_wire_preopen_hook_data=(struct GwWirePreOpenHook){0};original_cleanup_result=send_discard_result=0;original_cleanups=send_discards=0;
active=0;retirement_complete=1;credentials_generation=generation;pipe_generation=(LONG)generation;worker_handle=0;worker_signaled=1;busy=0;public_rows_ready=1;frame_resets=0;
gw_wire_frame_data=(struct FixtureFrameData){.command=10,.completed=1};
prepared_channel=0;prepared_port=0;prepared_realm_index=prepared_profile_kind=0;
gw_wire_auth_data=(struct gw_wire_auth_control){.abi=1,.size=sizeof(gw_wire_auth_data),.state=4,.auth_port=3724,.retention_ms=30000,.error=9};
gw_wire_auth_credentials_data=(struct gw_wire_auth_credentials){.abi=1,.size=sizeof(gw_wire_auth_credentials_data),.state=3};
gw_wire_session_lease_data=(struct gw_wire_session_lease){.abi=1,.size=sizeof(gw_wire_session_lease_data),.released=1};
memset(owner_object,0,sizeof(owner_object));fixture_owner=owner_object;holder_value=0;memset(gw_wire_session_pipe_receivers.objects,0,sizeof(gw_wire_session_pipe_receivers.objects));*(uint64_t*)(owner_object+0x228)=(uintptr_t)&holder_value;
canonical_empty();
gw_wire_pipe_data=(struct gw_pipe_control){.abi=1,.size=sizeof(gw_wire_pipe_data),.stage=1,.closed=1,.owner=(uintptr_t)owner_object,.pipe=(uintptr_t)owner_object,.error=23};
gw_wire_world_failure=(struct gw_pipe_world_failure){.abi=1,.size=48,.result=-2};
gw_wire_channel_policy=(struct gw_channel_policy_control){.abi=1,.size=48,.owner=(uintptr_t)owner_object,.enabled=1,.error=7};
gw_wire_session_retirement_data=(struct gw_session_retirement_control){.abi=1,.size=56,.state=1,.expected_credentials_generation=generation,.next_credentials_generation=generation};
}
int main(void){
CHECK(sizeof(struct gw_session_retirement_control)==56);CHECK(offsetof(struct gw_session_retirement_control,owner)==48);
CHECK(offsetof(struct gw_session_retirement_control,previous_frame_error)==44);CHECK(sizeof(gw_wire_session_pipe_callbacks)==1040);CHECK(sizeof(gw_wire_session_pipe_tables)==5136);CHECK(sizeof(gw_wire_session_pipe_receivers)==528);
setup(1);worker_handle=(HANDLE)1;worker_signaled=0;CHECK(gw_wire_account_retirement_ready(1)==-2);CHECK(closes==0);CHECK(worker_handle!=0);
worker_signaled=1;CHECK(gw_wire_account_retirement_ready(1)==0);CHECK(closes==1);CHECK(worker_handle==0);
active=1;CHECK(gw_wire_account_retirement_ready(1)==-2);active=0;
retirement_complete=0;CHECK(gw_wire_account_retirement_ready(1)==-7);retirement_complete=1;
CHECK(gw_wire_account_retirement_ready(0)==-1);CHECK(gw_wire_account_retirement_ready(2)==-1);
gw_wire_auth_data.state=2;CHECK(gw_wire_account_retirement_ready(1)==-3);gw_wire_auth_data.state=4;
prepared_channel=22;CHECK(gw_wire_account_retirement_ready(1)==-4);prepared_channel=0;
gw_wire_auth_credentials_data.state=1;CHECK(gw_wire_account_retirement_ready(1)==-5);gw_wire_auth_credentials_data.state=3;
gw_wire_session_lease_data.released=0;CHECK(gw_wire_account_retirement_ready(1)==-5);gw_wire_session_lease_data.released=1;
*(uint64_t*)(owner_object+0x1a0)=1;CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==-12);*(uint64_t*)(owner_object+0x1a0)=0;
holder_value=1;CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==-13);holder_value=0;
fixture_owner=0;CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==-11);fixture_owner=owner_object;
gw_wire_pipe_data.closed=0;CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==-15);gw_wire_pipe_data.closed=1;
*(uint64_t*)(owner_object+0x208)=0x1000;CHECK(gw_wire_channel_retirement_ready(owner_object)==-22);
*(uint64_t*)(owner_object+0x208)=1;CHECK(gw_wire_channel_retirement_ready(owner_object)==-22);
*(uint64_t*)(owner_object+0x208)=0;CHECK(gw_wire_channel_retirement_ready(owner_object)==-22);
canonical_empty();*(uint64_t*)(owner_object+0x200)=0;CHECK(gw_wire_channel_retirement_ready(owner_object)==-22);
canonical_empty();CHECK(gw_wire_channel_retirement_ready(owner_object)==0);
CHECK(native_depth==0&&pipe_depth==0&&native_entries==native_exits);
fixture_owner=owner_object+1;gw_wire_channel_policy.owner=0;
CHECK(gw_wire_channel_retirement_ready(owner_object+1)==-21);
fixture_owner=owner_object;gw_wire_channel_policy.owner=(uintptr_t)owner_object;
// A historical address is not the current object identity. The live owner,
// pipe binding and empty current epoch still permit native allocator reuse.
gw_wire_session_pipe_receivers.objects[5]=(uintptr_t)owner_object;CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==0);gw_wire_session_pipe_receivers.objects[5]=0;
gw_wire_session_pipe_receivers.count=63;CHECK(!gw_wire_pipe_receiver_allowed((uintptr_t)owner_object));CHECK(gw_wire_pipe_retirement_ready(owner_object,1)==-18);gw_wire_session_pipe_receivers.count=64;
busy=1;gw_wire_session_retire(owner_object,5);CHECK(gw_wire_session_retirement_data.state==3);CHECK(gw_wire_session_retirement_data.result==-17);CHECK(gw_wire_auth_data.state==4);CHECK(pipe_generation==1);
// A real -12 failure stays dormant while the original primary remains live.
setup(1);*(uint64_t*)(owner_object+0x1a0)=0x1234;
gw_wire_session_retire(owner_object,5);CHECK(gw_wire_session_retirement_data.state==3);CHECK(gw_wire_session_retirement_data.result==-12);
for(unsigned i=0;i<100;i++)wire_frame_retirement_poll(owner_object,2);
CHECK(gw_wire_session_retirement_data.state==3);CHECK(gw_wire_session_retirement_data.result==-12);
CHECK(*(uint64_t*)(owner_object+0x1a0)==0x1234);CHECK(frame_resets==0);CHECK(pipe_generation==1);CHECK(credentials_generation==1);CHECK(gw_wire_auth_data.state==4);
// Only the fixture's original-client action clears the pointer. All other
// generation, owner, account, holder and pending-queue guards still apply.
*(uint64_t*)(owner_object+0x1a0)=0;
gw_wire_session_retirement_data.owner=0;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.owner=(uintptr_t)owner_object;
gw_wire_session_retirement_data.expected_credentials_generation=2;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.expected_credentials_generation=1;
gw_wire_session_retirement_data.completed_credentials_generation=1;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.completed_credentials_generation=0;
gw_wire_session_retirement_data.next_credentials_generation=2;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.next_credentials_generation=1;
active=1;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);active=0;
public_rows_ready=0;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);public_rows_ready=1;
holder_value=1;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);holder_value=0;
*(uint64_t*)(owner_object+0x208)=0x1000;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);CHECK(*(uint64_t*)(owner_object+0x208)==0x1000);canonical_empty();
wire_frame_retirement_poll(owner_object,1);CHECK(frame_resets==0);
wire_frame_retirement_poll(owner_object,2);
CHECK(frame_resets==1);CHECK(gw_wire_frame_data.command==2);CHECK(gw_wire_session_retirement_data.state==2);
CHECK(gw_wire_session_retirement_data.completed_credentials_generation==1);CHECK(gw_wire_session_retirement_data.next_credentials_generation==2);
CHECK(pipe_generation==2);CHECK(credentials_generation==1);CHECK(gw_wire_auth_data.state==0);CHECK(gw_wire_auth_credentials_data.state==0);
for(unsigned i=0;i<100;i++)wire_frame_retirement_poll(owner_object,2);
CHECK(frame_resets==1);CHECK(pipe_generation==2);CHECK(credentials_generation==1);CHECK(gw_wire_auth_data.state==0);
// Other terminal failures remain held even when the native pointer is clear.
setup(1);busy=1;gw_wire_session_retire(owner_object,0);busy=0;
wire_frame_retirement_poll(owner_object,2);CHECK(gw_wire_session_retirement_data.result==-17);CHECK(frame_resets==0);CHECK(pipe_generation==1);
// A busy native owner lock is bounded. Request-stage contention publishes
// nothing; commit-stage contention retries only this same guarded epoch.
setup(1);gw_wire_session_retirement_data.state=0;gw_wire_session_retirement_data.expected_credentials_generation=0;
gw_wire_frame_data.command=4;native_busy=1;wire_frame_retirement_poll(owner_object,2);
CHECK(gw_wire_session_retirement_data.state==0&&gw_wire_frame_data.command==4&&pipe_generation==1);
CHECK(native_depth==0&&pipe_depth==0);native_busy=0;
owner_readable=0;wire_frame_retirement_poll(owner_object,2);CHECK(gw_wire_session_retirement_data.state==0&&pipe_depth==0);owner_readable=1;
change_owner_on_native_enter=1;wire_frame_retirement_poll(owner_object,2);
CHECK(!fixture_owner&&gw_wire_session_retirement_data.state==0&&native_depth==0&&pipe_depth==0);fixture_owner=owner_object;
setup(1);native_busy=1;gw_wire_session_retire(owner_object,0);
CHECK(gw_wire_session_retirement_data.state==3&&gw_wire_session_retirement_data.result==-23);
CHECK(gw_wire_auth_data.state==4&&pipe_generation==1&&frame_resets==0&&native_depth==0&&pipe_depth==0);
native_busy=0;gw_wire_frame_data.command=10;
gw_wire_session_retirement_data.owner=0;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.owner=(uintptr_t)owner_object;
gw_wire_session_retirement_data.completed_credentials_generation=1;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.completed_credentials_generation=0;
gw_wire_session_retirement_data.expected_credentials_generation=2;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.expected_credentials_generation=1;
gw_wire_session_retirement_data.next_credentials_generation=2;wire_frame_retirement_poll(owner_object,2);CHECK(frame_resets==0);gw_wire_session_retirement_data.next_credentials_generation=1;
wire_frame_retirement_poll(owner_object,2);
CHECK(gw_wire_session_retirement_data.state==2&&pipe_generation==2&&frame_resets==1);
CHECK(native_depth==0&&pipe_depth==0&&native_entries==native_exits);
// A legitimate native producer can enqueue after the request precheck.
// The repeated commit guard holds NativeCS before PipeSRW and must refuse
// that node without advancing or clearing any old epoch/account/queue.
setup(1);gw_wire_session_retirement_data.state=0;gw_wire_session_retirement_data.expected_credentials_generation=0;
CHECK(gw_wire_session_retirement_request_if_terminal(owner_object)==1);
CHECK(native_depth==0&&pipe_depth==0);
CHECK(TryEnterCriticalSection((CRITICAL_SECTION*)(owner_object+0x70)));
*(uint64_t*)(owner_object+0x208)=0x1000;
LeaveCriticalSection((CRITICAL_SECTION*)(owner_object+0x70));
gw_wire_session_retire(owner_object,0);
CHECK(gw_wire_session_retirement_data.state==3&&gw_wire_session_retirement_data.result==-22);
CHECK(*(uint64_t*)(owner_object+0x208)==0x1000&&pipe_generation==1&&credentials_generation==1);
CHECK(gw_wire_auth_data.state==4&&public_rows_ready&&frame_resets==0&&native_depth==0&&pipe_depth==0);
setup(1);gw_wire_pipe_data.read_calls=77;gw_wire_world_data.self_create=1;gw_wire_entity_data.captured=4;
gw_wire_realm_data.stage=2;gw_wire_realm_previous.current=99;awaiting_world=1;protection_waits=42;
memset(challenge,99,sizeof(challenge));memset(queued,88,sizeof(queued));challenge_size=8;
gw_wire_session_pipe_tables.methods[0][1]=1234;
gw_wire_session_retire(owner_object,5);
CHECK(gw_wire_session_retirement_data.state==2);CHECK(gw_wire_session_retirement_data.result==0);
CHECK(gw_wire_session_retirement_data.completed_credentials_generation==1);CHECK(gw_wire_session_retirement_data.next_credentials_generation==2);
CHECK(gw_wire_session_retirement_data.previous_account_error==9);CHECK(gw_wire_session_retirement_data.previous_pipe_error==23);
CHECK(gw_wire_session_retirement_data.previous_world_error==-2);CHECK(gw_wire_session_retirement_data.previous_policy_error==7);CHECK(gw_wire_session_retirement_data.previous_frame_error==5);
CHECK(credentials_generation==1);CHECK(pipe_generation==2);CHECK(gw_wire_auth_data.state==0);CHECK(gw_wire_auth_data.auth_port==0);CHECK(gw_wire_auth_data.retention_ms==0);CHECK(gw_wire_endpoint_profile_data.abi==1);CHECK(gw_wire_endpoint_profile_data.size==sizeof(gw_wire_endpoint_profile_data));
CHECK(gw_wire_auth_credentials_data.state==0);CHECK(gw_wire_pipe_data.stage==0);CHECK(gw_wire_pipe_data.read_calls==0);CHECK(gw_wire_world_data.self_create==0);CHECK(gw_wire_entity_data.captured==0);
CHECK(gw_wire_realm_data.stage==0);CHECK(gw_wire_realm_previous.current==0);CHECK(gw_wire_channel_policy.enabled==0);CHECK(awaiting_world==0);CHECK(protection_waits==0);
CHECK(challenge_size==0);CHECK(challenge[0]==0);CHECK(queued[0]==0);CHECK(gw_wire_session_pipe_tables.methods[0][1]==1234);CHECK(gw_wire_session_pipe_receivers.objects[0]==(uintptr_t)owner_object);CHECK(!gw_wire_pipe_receiver_allowed((uintptr_t)owner_object));CHECK(!gw_wire_pipe_receiver_allowed((uintptr_t)&holder_value));
// Before a new binding, neither address is accepted. Once the original
// allocator reuses the address, only the current epoch callback may enter.
((void (*)(void*,const struct gw_pipe_view*,int))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[0][0])(owner_object,0,1);CHECK(writes==0);
((void (*)(void*,const struct gw_pipe_view*))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[0][1])(owner_object,0);CHECK(reads==0);
((void (*)(void*,const struct gw_pipe_view*,int))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[1][0])(owner_object,0,1);CHECK(writes==0);
gw_wire_pipe_data.stage=1;gw_wire_pipe_data.pipe=(uintptr_t)owner_object;
CHECK(gw_wire_pipe_receiver_allowed((uintptr_t)owner_object));CHECK(!gw_wire_pipe_receiver_allowed((uintptr_t)&holder_value));
((void (*)(void*,const struct gw_pipe_view*,int))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[0][0])(owner_object,0,1);CHECK(writes==0);
((void (*)(void*,const struct gw_pipe_view*))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[0][1])(owner_object,0);CHECK(reads==0);
((void (*)(void*,const struct gw_pipe_view*,int))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[1][0])(owner_object,0,1);CHECK(writes==1);
((void (*)(void*,const struct gw_pipe_view*,int))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[1][0])(&holder_value,0,1);CHECK(writes==1);
((void (*)(void*,const struct gw_pipe_view*))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[1][1])(owner_object,0);CHECK(reads==1);
((void (*)(void*,const struct gw_pipe_view*))(uintptr_t)gw_wire_session_pipe_callbacks.callbacks[1][1])(&holder_value,0);CHECK(reads==1);
gw_wire_session_retire(owner_object,0);CHECK(gw_wire_session_retirement_data.completed_credentials_generation==1);CHECK(pipe_generation==2);
credentials_generation=2;public_rows_ready=1;gw_wire_auth_data.state=3;gw_wire_auth_credentials_data.state=3;gw_wire_session_lease_data.released=1;
gw_wire_pipe_data.stage=1;gw_wire_pipe_data.closed=1;gw_wire_pipe_data.owner=(uintptr_t)owner_object;gw_wire_pipe_data.pipe=(uintptr_t)owner_object;
gw_wire_session_retirement_data.state=1;gw_wire_session_retirement_data.expected_credentials_generation=2;
gw_wire_session_retire(owner_object,0);CHECK(gw_wire_session_retirement_data.state==2);CHECK(pipe_generation==3);CHECK(credentials_generation==2);CHECK(gw_wire_session_pipe_receivers.objects[0]==(uintptr_t)owner_object);CHECK(gw_wire_session_pipe_receivers.objects[1]==(uintptr_t)owner_object);
setup(64);gw_wire_session_retire(owner_object,0);CHECK(gw_wire_session_retirement_data.state==3);CHECK(pipe_generation==64);CHECK(gw_wire_auth_data.state==4);
setup(1);gw_wire_session_retirement_data.size=55;gw_wire_session_retire(owner_object,0);CHECK(gw_wire_auth_data.state==4);CHECK(pipe_generation==1);
// Actual Back leaves state2 with a retained primary/holder and suspended
// packets. The qualified original cleanup runs without Pipe SRW; only then
// do the same strict readiness/retirement functions advance the generation.
for(unsigned scenario=0;scenario<6;++scenario){
setup(1);gw_wire_session_retirement_data.state=0;gw_wire_session_retirement_data.expected_credentials_generation=0;
gw_wire_frame_data.command=8;*(LONG*)(owner_object+0x98)=2;
*(uint64_t*)(owner_object+0x1a0)=0x1234;holder_value=(uintptr_t)owner_object;
*(uint64_t*)(owner_object+0x208)=0x1000;
gw_wire_pipe_data.connection=0x1234;gw_wire_pipe_data.world_auth=1;
gw_wire_preopen_hook_data=(struct GwWirePreOpenHook){.abi=1,.size=48,.ready=1,.owner=owner_object,.primary_thread=36,.bridge_module=(void*)1,.callback=(void*)1};
if(scenario==1)prepared_channel=1;
if(scenario==2)gw_wire_pipe_data.closed=0;
if(scenario==3)gw_wire_pipe_data.connection=0x5678;
if(scenario==4)original_cleanup_result=-43;
if(scenario==5)send_discard_result=-83;
wire_frame_retirement_poll(owner_object,2);
if(scenario==0){
CHECK(original_cleanups==1&&send_discards==1);CHECK(frame_resets==1);
CHECK(gw_wire_session_retirement_data.state==2&&gw_wire_session_retirement_data.next_credentials_generation==2);
CHECK(pipe_generation==2&&credentials_generation==1&&gw_wire_auth_data.state==0);
}else{
CHECK(frame_resets==0&&pipe_generation==1&&credentials_generation==1);
CHECK(gw_wire_auth_data.state==4&&gw_wire_session_retirement_data.state==0);
if(scenario<5)CHECK(original_cleanups==0&&send_discards==0);
else CHECK(original_cleanups==1&&send_discards==1&&gw_wire_session_retirement_data.result==-83);
}
}
// A release failure remains held even if a later list snapshot looks empty.
setup(1);gw_wire_session_retirement_data.state=0;gw_wire_session_retirement_data.expected_credentials_generation=0;
gw_wire_frame_data.command=8;*(LONG*)(owner_object+0x98)=2;*(uint64_t*)(owner_object+0x1a0)=0x1234;holder_value=(uintptr_t)owner_object;
*(uint64_t*)(owner_object+0x208)=0x1000;gw_wire_pipe_data.connection=0x1234;gw_wire_pipe_data.world_auth=1;
gw_wire_preopen_hook_data=(struct GwWirePreOpenHook){.abi=1,.size=48,.ready=1,.owner=owner_object,.primary_thread=36,.bridge_module=(void*)1,.callback=(void*)1};
send_discard_result=-85;wire_frame_retirement_poll(owner_object,2);CHECK(original_cleanups==1&&send_discards==1&&frame_resets==0);
send_discard_result=0;wire_frame_retirement_poll(owner_object,2);CHECK(original_cleanups==1&&send_discards==1&&frame_resets==0&&pipe_generation==1);
CHECK(native_depth==0&&pipe_depth==0&&native_entries==native_exits);
puts("PASS source retirement guards, lifecycle,64epochs,immutabletables,oldcallbacks,canonicalempty,native-before-pipe,noauth");return 0;
}
'''
# Under-lock guard is part of the actual callback implementation too.
for name in ['pipe_read_current','pipe_write_current']:
body=function(pipe,name)
self.assertLess(body.index('AcquireSRWLockExclusive'),body.index('if(epoch!='))
self.assertLess(body.index('if(epoch!='),body.index('if(!owned(p)'))
self.assertLess(account.index('InterlockedExchange(&retirement_complete,(!call.transport && !call.login)?1:0);'),account.index('InterlockedExchange(&active,0)',account.index('InterlockedExchange(&retirement_complete,(!call.transport && !call.login)?1:0);')))
source=prefix+data+callbacks+'\n'.join(functions)+cases
with tempfile.TemporaryDirectory() as folder:
c=Path(folder)/'retirement.c';exe=Path(folder)/'retirement';c.write_text(source)
built=subprocess.run(['cc','-std=c11','-Wall','-Wextra','-Werror','-Wno-unused-variable','-Wno-unused-function','-I'+str(ROOT/'native'),str(c),'-o',str(exe)],capture_output=True,text=True)
self.assertEqual(built.returncode,0,built.stderr)
result=subprocess.run([str(exe)],capture_output=True,text=True)
self.assertEqual(result.returncode,0,result.stderr)
self.assertIn('PASS source retirement guards',result.stdout)
if __name__=='__main__':unittest.main()