109 lines
7.1 KiB
Python
109 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a host fixture from the actual account lease/expiry/heartbeat bodies."""
|
|
from pathlib import Path
|
|
import argparse
|
|
import subprocess
|
|
from source_account_cancel_test import function
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PREFIX = r'''
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <time.h>
|
|
#include <errno.h>
|
|
#define __declspec(x)
|
|
#define WINAPI
|
|
typedef int32_t LONG;typedef int64_t LONG64;typedef uint64_t ULONGLONG;
|
|
static pthread_rwlock_t channel_lock=PTHREAD_RWLOCK_INITIALIZER;
|
|
static volatile LONG active,retirement_complete;
|
|
static struct {volatile LONG state,cancel;uint32_t retention_ms;} gw_wire_auth_data;
|
|
static struct {volatile LONG attached,heartbeats;volatile uint64_t last_heartbeat_ms;volatile LONG released;} gw_wire_session_lease_data;
|
|
static unsigned lease_reads,heartbeat_reads,clock_calls;static int poison;
|
|
static uint64_t now=1000;
|
|
static pthread_mutex_t monitor=PTHREAD_MUTEX_INITIALIZER;
|
|
static pthread_cond_t changed=PTHREAD_COND_INITIALIZER;
|
|
static int block_clock,clock_entered,clock_release,detach_started,detach_done;
|
|
static void AcquireSRWLockShared(pthread_rwlock_t* lock){assert(!pthread_rwlock_rdlock(lock));}
|
|
static void ReleaseSRWLockShared(pthread_rwlock_t* lock){assert(!pthread_rwlock_unlock(lock));}
|
|
static void AcquireSRWLockExclusive(pthread_rwlock_t* lock){assert(!pthread_rwlock_wrlock(lock));}
|
|
static void ReleaseSRWLockExclusive(pthread_rwlock_t* lock){assert(!pthread_rwlock_unlock(lock));}
|
|
static LONG InterlockedCompareExchange(volatile LONG* p,LONG value,LONG expected){
|
|
if(p==&gw_wire_session_lease_data.attached||p==&gw_wire_session_lease_data.released){assert(!poison);++lease_reads;}
|
|
__atomic_compare_exchange_n(p,&expected,value,0,__ATOMIC_SEQ_CST,__ATOMIC_SEQ_CST);return expected;
|
|
}
|
|
static LONG64 InterlockedCompareExchange64(volatile LONG64* p,LONG64 value,LONG64 expected){
|
|
assert(!poison);++heartbeat_reads;
|
|
__atomic_compare_exchange_n(p,&expected,value,0,__ATOMIC_SEQ_CST,__ATOMIC_SEQ_CST);return expected;
|
|
}
|
|
static LONG InterlockedExchange(volatile LONG* p,LONG v){return __atomic_exchange_n(p,v,__ATOMIC_SEQ_CST);}
|
|
static LONG64 InterlockedExchange64(volatile LONG64* p,LONG64 v){assert(!poison);return __atomic_exchange_n(p,v,__ATOMIC_SEQ_CST);}
|
|
static LONG InterlockedIncrement(volatile LONG* p){return __atomic_add_fetch(p,1,__ATOMIC_SEQ_CST);}
|
|
static ULONGLONG GetTickCount64(void){
|
|
assert(!poison);++clock_calls;
|
|
assert(!pthread_mutex_lock(&monitor));
|
|
if(block_clock){clock_entered=1;pthread_cond_broadcast(&changed);while(!clock_release)pthread_cond_wait(&changed,&monitor);}
|
|
pthread_mutex_unlock(&monitor);return now;
|
|
}
|
|
'''
|
|
SUFFIX = r'''
|
|
static void reset(void){
|
|
active=1;retirement_complete=0;gw_wire_auth_data.state=2;gw_wire_auth_data.cancel=0;gw_wire_auth_data.retention_ms=10000;
|
|
gw_wire_session_lease_data.attached=1;gw_wire_session_lease_data.released=0;gw_wire_session_lease_data.last_heartbeat_ms=900;
|
|
poison=0;lease_reads=heartbeat_reads=clock_calls=0;
|
|
}
|
|
static void* reader(void* unused){(void)unused;assert(!expired(0));return 0;}
|
|
static void* detacher(void* unused){
|
|
(void)unused;pthread_mutex_lock(&monitor);detach_started=1;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&monitor);
|
|
gw_wire_account_detach_engine_session();
|
|
pthread_mutex_lock(&monitor);detach_done=1;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&monitor);return 0;
|
|
}
|
|
int main(void){
|
|
reset();assert(!expired(0));assert(heartbeat_reads==1&&clock_calls==1);
|
|
/* Retired, inactive, cancelled and terminal phases cannot touch even a
|
|
lease field. The fake clock/lease accessors abort if they are reached. */
|
|
for(unsigned which=0;which<6;++which){
|
|
reset();if(which==0){active=0;}if(which==1){retirement_complete=1;}if(which==2){gw_wire_auth_data.cancel=1;}
|
|
if(which==3){gw_wire_auth_data.state=0;}if(which==4){gw_wire_auth_data.state=3;}if(which==5){gw_wire_auth_data.state=4;}
|
|
poison=1;assert(expired(0));assert(gw_wire_account_engine_heartbeat()==-1);
|
|
assert(!lease_reads&&!heartbeat_reads&&!clock_calls);
|
|
}
|
|
reset();gw_wire_auth_data.state=1;assert(!expired(0));assert(!lease_reads&&!heartbeat_reads&&clock_calls==1);
|
|
reset();gw_wire_session_lease_data.released=1;assert(!expired(0));assert(!heartbeat_reads&&clock_calls==1);
|
|
assert(gw_wire_account_engine_heartbeat()==-1);assert(!heartbeat_reads&&clock_calls==1);
|
|
reset();assert(!gw_wire_account_engine_heartbeat());assert(gw_wire_session_lease_data.last_heartbeat_ms==now);
|
|
gw_wire_account_detach_engine_session();poison=1;lease_reads=heartbeat_reads=clock_calls=0;
|
|
assert(expired(0));assert(gw_wire_account_engine_heartbeat()==-1);assert(!lease_reads&&!heartbeat_reads&&!clock_calls);
|
|
/* A real reader holds shared ownership through its clock call. Detach
|
|
must complete only after that read finishes, never invalidate it. */
|
|
reset();block_clock=1;pthread_t read_thread,detach_thread;assert(!pthread_create(&read_thread,0,reader,0));
|
|
pthread_mutex_lock(&monitor);while(!clock_entered)pthread_cond_wait(&changed,&monitor);pthread_mutex_unlock(&monitor);
|
|
assert(!pthread_create(&detach_thread,0,detacher,0));
|
|
pthread_mutex_lock(&monitor);while(!detach_started)pthread_cond_wait(&changed,&monitor);
|
|
struct timespec deadline;clock_gettime(CLOCK_REALTIME,&deadline);deadline.tv_nsec+=20000000;
|
|
if(deadline.tv_nsec>=1000000000){++deadline.tv_sec;deadline.tv_nsec-=1000000000;}
|
|
int waited=0;while(!detach_done&&waited!=ETIMEDOUT)waited=pthread_cond_timedwait(&changed,&monitor,&deadline);
|
|
assert(!detach_done);clock_release=1;pthread_cond_broadcast(&changed);pthread_mutex_unlock(&monitor);
|
|
assert(!pthread_join(read_thread,0)&&!pthread_join(detach_thread,0));assert(detach_done);
|
|
poison=1;assert(expired(0));assert(gw_wire_account_engine_heartbeat()==-1);
|
|
block_clock=0;reset();assert(!expired(0)); /* Next owned attempt remains usable. */
|
|
puts("actual account C bodies: retired lease/clock inaccessible; detach serialized; heartbeat and fresh retry PASS");return 0;
|
|
}
|
|
'''
|
|
def build(output):
|
|
source = (ROOT / 'native/wine_wire_account.c').read_text()
|
|
names = ('engine_lease_attached_locked', 'engine_lease_current_locked',
|
|
'gw_wire_account_engine_heartbeat', 'gw_wire_account_detach_engine_session', 'expired')
|
|
# The close path already owns exclusive channel_lock; never call a helper
|
|
# which tries to acquire it recursively.
|
|
close = source[source.index('close:\n'):source.index('static int account_worker_busy_locked')]
|
|
assert '&& engine_lease_current_locked())' in close
|
|
code = PREFIX + '\n'.join(function(source, name) for name in names) + SUFFIX
|
|
output = Path(output); output.parent.mkdir(parents=True, exist_ok=True)
|
|
generated = output.with_suffix('.c'); generated.write_text(code)
|
|
subprocess.run(['cc', '-std=c11', '-D_XOPEN_SOURCE=700', '-Wall', '-Wextra', '-Werror',
|
|
'-pthread', str(generated), '-o', str(output)], check=True)
|
|
if __name__ == '__main__':
|
|
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--build',type=Path,required=True)
|
|
build(parser.parse_args().build)
|