126 lines
8.5 KiB
Python
126 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise production Source cancellation and selection with real host locks."""
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def function(source, name):
|
|
match = re.search(r'(?m)^[^\n;{}]*\b' + re.escape(name) + r'\([^;{}]*\)\s*\{', source)
|
|
if not match:
|
|
raise AssertionError('Missing production function ' + name)
|
|
at = source.index('{', match.start())
|
|
depth, end = 1, at + 1
|
|
while depth:
|
|
depth += (source[end] == '{') - (source[end] == '}')
|
|
end += 1
|
|
return source[match.start():end]
|
|
|
|
|
|
PREFIX = r'''
|
|
#include <assert.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <pthread.h>
|
|
#define __declspec(x)
|
|
#define WINAPI
|
|
#define GW_SESSION_EPOCH_LIMIT 2147483647u
|
|
#include "ui/source_account_cancel.h"
|
|
typedef int32_t LONG;typedef int64_t LONG64;
|
|
typedef pthread_rwlock_t SRWLOCK;
|
|
static SRWLOCK channel_lock=PTHREAD_RWLOCK_INITIALIZER;
|
|
static _Thread_local int lock_mode;
|
|
struct view {uint32_t generation;int state,cancel,transport_prepared,selected_index,genuine_discovery,realm_count;};
|
|
#define gw_wire_account_ui_state view
|
|
struct realm {char name[64],address[256];uint32_t flags;};
|
|
#define gw_native_realm realm
|
|
static struct {int state,cancel,transport_prepared,selected_index;} gw_wire_auth_data;
|
|
static struct {LONG64 request;LONG state,error,enabled;} gw_wire_account_ui_data;
|
|
static struct {LONG attached;} gw_wire_session_lease_data;
|
|
static struct {unsigned kind;} owned_endpoint;
|
|
static struct realm gw_wire_auth_realms[64];
|
|
static uint32_t credentials_generation,ui_auth_generation;
|
|
static uint64_t prepared_channel;
|
|
static LONG active;
|
|
static int legacy_rotate;
|
|
static void AcquireSRWLockShared(SRWLOCK* p){assert(!lock_mode);assert(!pthread_rwlock_rdlock(p));lock_mode=1;}
|
|
static void ReleaseSRWLockShared(SRWLOCK* p){assert(lock_mode==1);lock_mode=0;assert(!pthread_rwlock_unlock(p));
|
|
if(legacy_rotate){legacy_rotate=0;assert(!pthread_rwlock_wrlock(p));credentials_generation=ui_auth_generation=8;gw_wire_auth_data.cancel=0;assert(!pthread_rwlock_unlock(p));}}
|
|
static void AcquireSRWLockExclusive(SRWLOCK* p){assert(!lock_mode);assert(!pthread_rwlock_wrlock(p));lock_mode=2;}
|
|
static void ReleaseSRWLockExclusive(SRWLOCK* p){assert(lock_mode==2);lock_mode=0;assert(!pthread_rwlock_unlock(p));}
|
|
static LONG InterlockedCompareExchange(volatile LONG* p,LONG value,LONG expected){__atomic_compare_exchange_n(p,&expected,value,0,__ATOMIC_SEQ_CST,__ATOMIC_SEQ_CST);return expected;}
|
|
static LONG InterlockedExchange(volatile LONG* p,LONG value){return __atomic_exchange_n(p,value,__ATOMIC_SEQ_CST);}
|
|
static LONG64 InterlockedCompareExchange64(volatile LONG64* p,LONG64 value,LONG64 expected){__atomic_compare_exchange_n(p,&expected,value,0,__ATOMIC_SEQ_CST,__ATOMIC_SEQ_CST);return expected;}
|
|
static int terminated(const char* p,size_t n){return memchr(p,0,n)!=NULL;}
|
|
/* Header construction is independently covered by the production failed-header
|
|
* fixture. Here it only reads the current Source-owned fields under the lock. */
|
|
static void account_ui_header(struct view* v){assert(lock_mode);*v=(struct view){ui_auth_generation,gw_wire_auth_data.state,gw_wire_auth_data.cancel,gw_wire_auth_data.transport_prepared,gw_wire_auth_data.selected_index,1,1};}
|
|
'''
|
|
CASES = r'''
|
|
static void setup(int state){
|
|
credentials_generation=ui_auth_generation=7;active=1;prepared_channel=0;owned_endpoint.kind=1;
|
|
gw_wire_auth_data.state=state;gw_wire_auth_data.cancel=0;gw_wire_auth_data.transport_prepared=0;gw_wire_auth_data.selected_index=-1;
|
|
gw_wire_account_ui_data.request=0;gw_wire_account_ui_data.state=0;gw_wire_account_ui_data.error=0;gw_wire_account_ui_data.enabled=1;
|
|
gw_wire_session_lease_data.attached=0;memset(gw_wire_auth_realms,0,sizeof(gw_wire_auth_realms));strcpy(gw_wire_auth_realms[0].name,"Synthetic realm");strcpy(gw_wire_auth_realms[0].address,"127.0.0.1:8090");
|
|
}
|
|
static pthread_barrier_t entering;
|
|
static int cancelled_result;
|
|
static void* delayed_cancel(void* unused){(void)unused;pthread_barrier_wait(&entering);cancelled_result=source_account_cancel_generation(7);return NULL;}
|
|
int main(void){
|
|
gw_source_account_cancel_fn cancel=gw_wire_account_ui_cancel_generation;
|
|
setup(1);assert(cancel(7)==0&&gw_wire_auth_data.cancel==1);assert(gw_wire_auth_data.state==1&&active==1);assert(cancel(7)==0);
|
|
setup(5);assert(cancel(7)==0&&gw_wire_auth_data.state==5&&active==1);
|
|
/* Cancellation is intent only, and cannot be followed by a realm proposal. */
|
|
assert(gw_wire_account_ui_select(0,7)==-2&&gw_wire_account_ui_data.request==0);
|
|
setup(5);assert(gw_wire_account_ui_select(0,7)==0);assert(gw_wire_account_ui_data.request!=0);assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel);
|
|
setup(1);assert(cancel(6)==GW_SOURCE_CANCEL_NOT_CURRENT&&!gw_wire_auth_data.cancel);assert(cancel(8)==GW_SOURCE_CANCEL_NOT_CURRENT&&!gw_wire_auth_data.cancel);
|
|
assert(cancel(0)==GW_SOURCE_CANCEL_INVALID_GENERATION);assert(cancel(GW_SESSION_EPOCH_LIMIT+1u)==GW_SOURCE_CANCEL_INVALID_GENERATION);
|
|
setup(1);credentials_generation=ui_auth_generation=GW_SESSION_EPOCH_LIMIT;assert(cancel(GW_SESSION_EPOCH_LIMIT)==0);
|
|
setup(1);ui_auth_generation=6;assert(cancel(6)==GW_SOURCE_CANCEL_NOT_CURRENT&&!gw_wire_auth_data.cancel);
|
|
setup(1);active=0;assert(cancel(7)==GW_SOURCE_CANCEL_NOT_CURRENT&&!gw_wire_auth_data.cancel);
|
|
for(int state=0;state<=5;++state)if(state!=1&&state!=5){setup(state);assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel);}
|
|
setup(1);gw_wire_auth_data.selected_index=0;assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel);
|
|
setup(1);gw_wire_auth_data.transport_prepared=1;assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel);
|
|
setup(1);prepared_channel=19;assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel&&prepared_channel==19);
|
|
setup(1);gw_wire_session_lease_data.attached=1;assert(cancel(7)==GW_SOURCE_CANCEL_PAST_SELECTION&&!gw_wire_auth_data.cancel);
|
|
setup(1);assert(gw_wire_account_ui_cancel()==GW_SOURCE_CANCEL_NOT_CURRENT&&!gw_wire_auth_data.cancel);
|
|
setup(5);assert(gw_wire_account_ui_cancel()==0&&gw_wire_auth_data.cancel);
|
|
setup(5);ui_auth_generation=0;assert(cancel(0)==GW_SOURCE_CANCEL_INVALID_GENERATION&&!gw_wire_auth_data.cancel);assert(gw_wire_account_ui_cancel()==0&&gw_wire_auth_data.cancel);
|
|
/* Actual compatibility wrapper snapshots G7, but G8 wins before the typed
|
|
* method locks: it must reject G7 and leave G8 uncancelled. */
|
|
setup(5);legacy_rotate=1;assert(gw_wire_account_ui_cancel()==GW_SOURCE_CANCEL_NOT_CURRENT&&ui_auth_generation==8&&!gw_wire_auth_data.cancel);
|
|
/* Deterministic real-thread interleaving: hold generation publication's
|
|
* exclusive lock, start a delayed G7 cancellation, publish G8, then release.
|
|
* The delayed call must validate after acquiring the lock, never before it. */
|
|
setup(1);assert(!pthread_barrier_init(&entering,NULL,2));AcquireSRWLockExclusive(&channel_lock);pthread_t thread;assert(!pthread_create(&thread,NULL,delayed_cancel,NULL));pthread_barrier_wait(&entering);
|
|
credentials_generation=ui_auth_generation=8;gw_wire_auth_data.cancel=0;ReleaseSRWLockExclusive(&channel_lock);assert(!pthread_join(thread,NULL));assert(cancelled_result==GW_SOURCE_CANCEL_NOT_CURRENT&&gw_wire_auth_data.cancel==0);pthread_barrier_destroy(&entering);
|
|
/* The matching generation can still cancel after that rejection. */
|
|
assert(cancel(8)==0&&gw_wire_auth_data.cancel==1&&gw_wire_auth_data.state==1&&active==1);
|
|
puts("PASS Source generation-bound cancellation, pending acknowledgement, stale-generation real-thread race, realm proposal and active-transport guards");return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
class SourceAccountCancel(unittest.TestCase):
|
|
def test_production_source_lifecycle(self):
|
|
source = (ROOT/'native/wine_wire_account.c').read_text()
|
|
names = ['account_cancel_request_locked', 'source_account_cancel_generation',
|
|
'gw_wire_account_ui_cancel_generation', 'gw_wire_account_ui_cancel', 'gw_wire_account_ui_select']
|
|
code = PREFIX + '\n'.join(function(source, name) for name in names) + CASES
|
|
with tempfile.TemporaryDirectory(prefix='source-cancel-') as temporary:
|
|
temp = Path(temporary)
|
|
src, exe = temp/'fixture.c', temp/'fixture'
|
|
src.write_text(code)
|
|
subprocess.run(['cc', '-std=c11', '-D_XOPEN_SOURCE=700', '-Wall', '-Wextra', '-Werror',
|
|
'-pthread', '-I'+str(ROOT/'native'), str(src), '-o', str(exe)], check=True)
|
|
subprocess.run([str(exe)], check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|