153 lines
10 KiB
Python
153 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Synthetic project numeric dispatch and standalone ordinary cancellation ABI."""
|
|
from pathlib import Path
|
|
import ctypes
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import shutil
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from source_account_cancel_test import function
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PREFIX = r'''
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#define WINAPI
|
|
#define __declspec(x)
|
|
typedef int32_t LONG;typedef void* HMODULE;typedef void* FARPROC;
|
|
static struct {uint64_t lua_state;uint32_t callbacks_seen;} gw_registration_data;
|
|
static struct {uint32_t abi,size,ready,error;uint64_t module,snapshot,select;} gw_registration_realm_binding;
|
|
static int64_t args[3],pushed;static int resolver_calls,cancel_calls,legacy_calls,create_calls;static uint32_t last_generation,current_generation=7;static int export_available=1,forced_result;
|
|
static int32_t cancel_generation(uint32_t expected){++cancel_calls;last_generation=expected;return forced_result?forced_result:(expected==current_generation?0:-2);}
|
|
static LONG cancel_legacy(void){++legacy_calls;return -10;}
|
|
static FARPROC GetProcAddress(HMODULE module,const char* name){assert(module==(void*)0x1234);++resolver_calls;if(!strcmp(name,"gw_wire_account_ui_cancel_generation"))return export_available?(void*)cancel_generation:NULL;if(!strcmp(name,"gw_wire_remember_password_query"))return NULL;assert(!strcmp(name,"gw_wire_account_ui_cancel"));return (void*)cancel_legacy;}
|
|
/* Model only the declared numeric-reader contract, never engine memory. */
|
|
static int clogin_argument(void* L,int position,uint32_t max,uint32_t* out){assert(L==(void*)0x99&&position>=1&&position<=3);int64_t v=args[position-1];if(v<0||(uint64_t)v>max)return 0;*out=(uint32_t)v;return 1;}
|
|
static void clogin_push(void* L,int64_t value){assert(L==(void*)0x99);pushed=value;}
|
|
static uint32_t clogin_create_challenge_mask;
|
|
static int64_t clogin_create(uint32_t query,uint32_t row,uint32_t chunk){(void)row;(void)chunk;++create_calls;return 800+query;}
|
|
static int64_t clogin_update_action(uint32_t query){return query == 14 ? 2 : 1;}
|
|
static int clogin_captured;
|
|
static int clogin_capture(void){return 0;}
|
|
static struct {uint32_t copied_realms;} clogin_header;
|
|
static struct {char name[4],address[4];} clogin_realms[64];
|
|
static int64_t clogin_header_field(uint32_t field){return 100+field;}
|
|
static int64_t clogin_row_field(const void* row,uint32_t field){(void)row;return 200+field;}
|
|
static int64_t clogin_text_chunk(const char* text,size_t n,uint32_t chunk){(void)text;(void)n;return 300+chunk;}
|
|
'''
|
|
CASES = r'''
|
|
static int64_t call(int64_t query,int64_t row,int64_t chunk){args[0]=query;args[1]=row;args[2]=chunk;pushed=9999;assert(gw_clogin_snapshot_numeric((void*)0x99)==1);return pushed;}
|
|
int main(void){
|
|
gw_registration_data.lua_state=0x99;gw_registration_realm_binding.abi=2;gw_registration_realm_binding.size=40;gw_registration_realm_binding.ready=1;gw_registration_realm_binding.module=0x1234;
|
|
assert(call(11,7,0)==0&&cancel_calls==1&&last_generation==7&&create_calls==0&&legacy_calls==0);
|
|
assert(call(11,6,0)==-2&&last_generation==6);current_generation=8;assert(call(11,7,0)==-2&&last_generation==7);assert(call(11,8,0)==0&&last_generation==8);
|
|
int before=cancel_calls,lookups=resolver_calls;assert(call(11,0,0)==-4005);assert(call(11,8,1)==-4005);assert(call(11,8,0x7fffffff)==-4005);assert(call(11,-1,0)==-4005);assert(call(11,0x80000000LL,0)==-4005);assert(call(12,8,0)==-4005);assert(cancel_calls==before&&resolver_calls==lookups&&create_calls==0);
|
|
export_available=0;assert(call(11,8,0)==-4001&&cancel_calls==before&&legacy_calls==0);export_available=1;
|
|
gw_registration_realm_binding.ready=0;assert(call(11,8,0)==-4001);gw_registration_realm_binding.ready=1;
|
|
gw_registration_realm_binding.error=9;assert(call(11,8,0)==-4001);gw_registration_realm_binding.error=0;
|
|
gw_registration_realm_binding.abi=1;assert(call(11,8,0)==-4001);gw_registration_realm_binding.abi=2;
|
|
gw_registration_realm_binding.size=39;assert(call(11,8,0)==-4001);gw_registration_realm_binding.size=40;
|
|
gw_registration_realm_binding.module=0;assert(call(11,8,0)==-4001);gw_registration_realm_binding.module=0x1234;
|
|
current_generation=0x7fffffffU;assert(call(11,0x7fffffffLL,0)==0&&last_generation==0x7fffffffU);forced_result=-3;assert(call(11,0x7fffffffLL,0)==-3);forced_result=0;
|
|
assert(call(14,0,0)==2 && call(15,0,0)==1);
|
|
assert(call(14,1,0)==-4005 && call(15,0,1)==-4005 && call(24,0,0)==-4005 && call(16,0,0)==-4001);
|
|
assert(call(22,0,0)==0 && call(23,0,0)==0 && call(22,1,0)==-4005 && call(22,2,1)==-4005 && call(23,1,0)==-4005);
|
|
/* All pre-existing routing remains distinguishable from cancellation. */
|
|
before=cancel_calls;assert(call(5,1,0)==805&&call(9,1,0)==809&&call(10,1,0)==810&&create_calls==3);assert(call(4,0,0)==-10&&legacy_calls==1);assert(cancel_calls==before);
|
|
assert(call(0,0,0)==0);assert(call(0,3,0)==103);assert(gw_clogin_snapshot_numeric((void*)0x98)==0&&cancel_calls==before);
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
def pe_exports(path):
|
|
raw = path.read_bytes()
|
|
pe = struct.unpack_from('<I', raw, 0x3c)[0]
|
|
optional = pe + 24
|
|
count = struct.unpack_from('<H', raw, pe + 6)[0]
|
|
size = struct.unpack_from('<H', raw, pe + 20)[0]
|
|
sections = [struct.unpack_from('<IIII', raw, optional + size + i*40 + 8) for i in range(count)]
|
|
def off(rva):
|
|
for virtual_size, va, raw_size, position in sections:
|
|
if va <= rva < va + max(virtual_size, raw_size):
|
|
return position + rva - va
|
|
raise AssertionError('Export outside PE sections')
|
|
table = off(struct.unpack_from('<I', raw, optional + 112)[0])
|
|
base, _, names, _, name_table, ordinals = struct.unpack_from('<IIIIII', raw, table + 16)
|
|
result = {}
|
|
for i in range(names):
|
|
start = off(struct.unpack_from('<I', raw, off(name_table) + 4*i)[0])
|
|
name = raw[start:raw.index(b'\0', start)].decode()
|
|
result[name] = base + struct.unpack_from('<H', raw, off(ordinals) + 2*i)[0]
|
|
return result
|
|
|
|
|
|
class SourceCancelDispatch(unittest.TestCase):
|
|
def test_export_ordinal_contract(self):
|
|
text = (ROOT/'native/nohelper/wire_exports.def').read_text()
|
|
exports = {name:int(number) for name,number in re.findall(r'^\s*(\w+)\s+@(\d+)', text, re.M)}
|
|
self.assertEqual(len(exports), len(set(exports.values())))
|
|
old = {name:number for name,number in exports.items() if number <= 80}
|
|
digest = hashlib.sha256(json.dumps(old,sort_keys=True,separators=(',',':')).encode()).hexdigest()
|
|
self.assertEqual(digest, '64ac6fadbc16b854d784fe40d3715444b530c959f516721dc0c985d6e979309d')
|
|
self.assertEqual(exports['gw_wire_account_ui_cancel_generation'], 81)
|
|
|
|
def test_actual_numeric_dispatch(self):
|
|
source = (ROOT/'native/nohelper/clogin_realm_rows.c').read_text()
|
|
code = PREFIX + function(source, 'clogin_wire_proc') + function(source, 'gw_clogin_snapshot_numeric') + CASES
|
|
with tempfile.TemporaryDirectory(prefix='cancel-dispatch-') as temporary:
|
|
path = Path(temporary)
|
|
(path/'dispatch.c').write_text(code)
|
|
subprocess.run(['cc','-std=c11','-Wall','-Wextra','-Werror',str(path/'dispatch.c'),'-o',str(path/'dispatch')], check=True)
|
|
subprocess.run([str(path/'dispatch')], check=True)
|
|
|
|
def test_standalone_project_abi(self):
|
|
source = (ROOT/'native/wine_wire_account.c').read_text()
|
|
wrapper = function(source, 'gw_wire_account_ui_cancel_generation')
|
|
prefix = ('#include "source_account_cancel.h"\n'
|
|
'static int32_t source_account_cancel_generation(uint32_t value){return value==7?0:-2;}\n')
|
|
with tempfile.TemporaryDirectory(prefix='cancel-abi-') as temporary:
|
|
path = Path(temporary)
|
|
shutil.copyfile(ROOT/'native/ui/source_account_cancel.h', path/'source_account_cancel.h')
|
|
(path/'host.c').write_text('#define __declspec(x)\n#define WINAPI\n'+prefix+wrapper)
|
|
subprocess.run(['cc','-shared','-fPIC',str(path/'host.c'),'-o',str(path/'host.so')], check=True)
|
|
library = ctypes.CDLL(str(path/'host.so'))
|
|
callback = library.gw_wire_account_ui_cancel_generation
|
|
callback.argtypes, callback.restype = [ctypes.c_uint32], ctypes.c_int32
|
|
self.assertEqual([callback(x) for x in (7,6,8,0,0xffffffff)], [0,-2,-2,-2,-2])
|
|
clang, linker = shutil.which('clang'), shutil.which('lld-link')
|
|
if not clang or not linker:
|
|
self.skipTest('PE ABI probe requires Clang and lld-link; host ABI passed')
|
|
(path/'probe.c').write_text('#define WINAPI __stdcall\n'+prefix+wrapper)
|
|
(path/'probe.def').write_text('LIBRARY SourceCancelAbiProbe\nEXPORTS\n gw_wire_account_ui_cancel_generation @81\n')
|
|
subprocess.run([clang,'--target=x86_64-pc-windows-msvc','-ffreestanding','-Werror',
|
|
'-c',str(path/'probe.c'),'-o',str(path/'probe.obj')], check=True)
|
|
subprocess.run([linker,'/dll','/noentry','/nodefaultlib','/out:'+str(path/'probe.dll'),'/def:'+str(path/'probe.def'),str(path/'probe.obj')], check=True)
|
|
self.assertEqual(pe_exports(path/'probe.dll'), {'gw_wire_account_ui_cancel_generation':81})
|
|
if os.environ.get('GW_SOURCE_CANCEL_ABI_PROOF'):
|
|
evidence = Path(os.environ['GW_SOURCE_CANCEL_ABI_PROOF']).resolve()
|
|
self.assertTrue(evidence.is_relative_to(ROOT/'runtime'))
|
|
evidence.mkdir()
|
|
files = ('source_account_cancel.h','probe.c','probe.def','probe.obj','probe.dll')
|
|
for name in files:
|
|
shutil.copy2(path/name,evidence/name)
|
|
report = {'state':'STANDALONE_PROJECT_CANCEL_HOST_ABI_AND_AMD64_PE_EXPORT_PASS',
|
|
'symbol':'gw_wire_account_ui_cancel_generation','ordinal':81,
|
|
'source_wrapper':'native/wine_wire_account.c','synthetic_provider':True,
|
|
'actual_Source_semantics_separately_tested':'source_account_cancel_test.py',
|
|
'files':{name:hashlib.sha256((evidence/name).read_bytes()).hexdigest()
|
|
for name in files},
|
|
'full_component_build':False,'loaded_in_game':False}
|
|
(evidence/'receipt.private.json').write_text(json.dumps(report,indent=2)+'\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|