260 lines
13 KiB
Python
260 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise exact PE creation dispatch C with synthetic callback outputs only."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def braced(source,marker):
|
|
start=source.index(marker);brace=source.index('{',start);end=brace+1;depth=1
|
|
while depth:
|
|
depth+=(source[end]=='{')-(source[end]=='}');end+=1
|
|
return source[start:end]
|
|
|
|
|
|
HARNESS=r'''
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "wine_wire_abi.h"
|
|
#include "login_timing.h"
|
|
/* Login-timing diagnostics in extracted handlers (Q113b): inert here. */
|
|
static struct gw_login_timing login_timing;
|
|
static uint64_t GetTickCount64(void){return 0;}
|
|
static void pipe_note(const char* text,uint32_t value){(void)text;(void)value;}
|
|
#define SecureZeroMemory(p,n) ((void)memset((p),0,(n)))
|
|
static unsigned char outgoing[65535], captured[65535];
|
|
static unsigned sent,submitted,calls,last_opcode,last_size,last_operation,last_input_bytes;
|
|
static uint32_t next_action,next_opcode,next_size;
|
|
static int next_error,emit_error;
|
|
static const unsigned char* last_input;
|
|
static void init(struct gw_wire_call* c,uint32_t op){memset(c,0,sizeof(*c));c->operation=op;}
|
|
static int send_wire(const unsigned char* body,uint32_t size){++sent;last_size=size;memcpy(captured,body,size);return emit_error;}
|
|
static int submit(uint32_t opcode,const unsigned char* body,uint32_t size){++submitted;last_opcode=opcode;last_size=size;memcpy(captured,body,size);return emit_error;}
|
|
static int call(struct gw_wire_call* c){
|
|
++calls;last_operation=c->operation;last_opcode=c->opcode;last_input=c->input;last_input_bytes=c->input_bytes;
|
|
if(next_error)return next_error;
|
|
c->value=next_action;c->packet.opcode=next_opcode;c->required=next_size;
|
|
if(next_size<=c->capacity)memset(c->output,0xa5,next_size);
|
|
next_action=GW_CREATE_NONE;next_opcode=next_size=0;
|
|
return 0;
|
|
}
|
|
@FUNCTIONS@
|
|
static struct pipe_state { int world_auth; unsigned unsupported_out; } gw_wire_pipe_data;
|
|
static struct { int login_sent; } gw_wire_world_data;
|
|
static unsigned service_calls, drop_calls;
|
|
static int service_packets(void){++service_calls;return 0;}
|
|
static void diagnostic_client_drop(uint32_t opcode,uint32_t size){
|
|
(void)opcode;(void)size;++drop_calls;
|
|
}
|
|
static int outgoing_setting(uint32_t opcode,const unsigned char* data,uint32_t bytes){
|
|
struct packet_view {const unsigned char* data;uint32_t bytes;} packet={data,bytes};
|
|
const struct packet_view* v=&packet;
|
|
struct gw_wire_call c;
|
|
int error=0;
|
|
do {
|
|
@OUTGOING_SETTING@
|
|
return 1; /* No match in the setting branch. */
|
|
} while(0);
|
|
done:
|
|
return error;
|
|
}
|
|
static int outgoing_rename(uint32_t opcode,const unsigned char* data,uint32_t bytes){
|
|
struct view { const unsigned char* data; uint32_t bytes; } packet={data,bytes};
|
|
const struct view* v=&packet;
|
|
int error=0;
|
|
do { @OUTGOING_RENAME@ } while(0);
|
|
done:
|
|
return error;
|
|
}
|
|
static int incoming_rename(uint32_t opcode){
|
|
struct pipe_state* d=&gw_wire_pipe_data;
|
|
@INCOMING_RENAME@
|
|
return 0;
|
|
}
|
|
#define REQUIRE(x) do{if(!(x)){fprintf(stderr,"creation fixture line%d\n",__LINE__);return 1;}}while(0)
|
|
int main(int argc,char** argv){
|
|
struct gw_wire_call c;unsigned char body[3]={7,8,9};int id;
|
|
if(argc!=2)return 2;
|
|
id=atoi(argv[1]);
|
|
REQUIRE(GW_WIRE_CREATE_CHARACTER==40 && GW_WIRE_CREATE_RESULT==41);
|
|
init(&c,GW_WIRE_CREATE_RESULT);c.output=outgoing;c.capacity=sizeof(outgoing);
|
|
switch(id){
|
|
case 1:
|
|
REQUIRE(creation_output(&c)==0 && sent==0 && submitted==0);break;
|
|
case 2:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x36;c.required=9;memset(outgoing,0xa5,9);
|
|
REQUIRE(creation_output(&c)==0 && sent==1 && submitted==0 && last_size==9);
|
|
REQUIRE(captured[0]==0xa5 && captured[8]==0xa5 && outgoing[0]==0 && outgoing[8]==0);break;
|
|
case 3:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x37;c.required=6;
|
|
REQUIRE(creation_output(&c)==0 && sent==1 && submitted==0 && last_size==6);break;
|
|
case 4:
|
|
c.value=GW_CREATE_NATIVE_REPLY;c.packet.opcode=0x4701ac;c.required=1;outgoing[0]=25;
|
|
REQUIRE(creation_output(&c)==0 && submitted==1 && sent==0 && last_opcode==0x4701ac && captured[0]==25);break;
|
|
case 5:
|
|
c.required=1;REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 6:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x39;c.required=6;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 7:
|
|
c.value=GW_CREATE_NATIVE_REPLY;c.packet.opcode=0x470018;c.required=1;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 8:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x36;c.required=5;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);
|
|
c.value=GW_CREATE_NATIVE_REPLY;c.packet.opcode=0x4701ac;c.required=0;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 9:
|
|
c.value=GW_CREATE_NATIVE_REPLY;c.packet.opcode=0x4701ac;c.required=65536;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);
|
|
c.required=2;c.capacity=1;REQUIRE(creation_output(&c)==-1 && !sent && !submitted);
|
|
c.required=1;c.capacity=sizeof(outgoing);c.output=body;
|
|
REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 10:
|
|
c.value=3;c.required=1;REQUIRE(creation_output(&c)==-1 && !sent && !submitted);break;
|
|
case 11:
|
|
next_action=GW_CREATE_SOURCE_FRAME;next_opcode=0x36;next_size=9;
|
|
REQUIRE(creation_request(body,sizeof(body))==0 && sent==1 && submitted==0 && calls==1);
|
|
REQUIRE(last_operation==GW_WIRE_CREATE_CHARACTER && last_opcode==0x420070 && last_input==body && last_input_bytes==3);break;
|
|
case 12:
|
|
next_action=GW_CREATE_SOURCE_FRAME;next_opcode=0x37;next_size=6;
|
|
REQUIRE(creation_result()==0 && sent==1 && submitted==0 && calls==1);
|
|
REQUIRE(last_operation==GW_WIRE_CREATE_RESULT && last_input==NULL && last_input_bytes==0);
|
|
REQUIRE(creation_result()==0 && sent==1 && submitted==0 && calls==2);break;
|
|
case 13:
|
|
next_action=GW_CREATE_NATIVE_REPLY;next_opcode=0x4701ac;next_size=1;
|
|
REQUIRE(creation_request(body,sizeof(body))==0 && sent==0 && submitted==1);break;
|
|
case 14:
|
|
next_error=-3;REQUIRE(creation_request(body,sizeof(body))==-1 && sent==0 && submitted==0 && calls==1);
|
|
REQUIRE(creation_result()==-1 && sent==0 && submitted==0 && calls==2);break;
|
|
case 15:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x36;c.required=6;emit_error=-1;
|
|
REQUIRE(creation_output(&c)==-1 && sent==1 && submitted==0);break;
|
|
case 16:{
|
|
const unsigned char descriptor[]={0xf0,0x00,0x44,0x00,7,8,9};
|
|
REQUIRE(outgoing_rename(0x4400f0,descriptor,sizeof(descriptor))==0 && calls==0);
|
|
gw_wire_pipe_data.world_auth=1;gw_wire_world_data.login_sent=1;
|
|
REQUIRE(outgoing_rename(0x4400f0,descriptor,sizeof(descriptor))==0 && calls==0);
|
|
gw_wire_world_data.login_sent=0;next_action=GW_CREATE_SOURCE_FRAME;next_opcode=0x2c7;next_size=17;
|
|
REQUIRE(outgoing_rename(0x4400f0,descriptor,sizeof(descriptor))==0 && calls==1 && sent==1 && !submitted);
|
|
REQUIRE(last_operation==GW_WIRE_CREATE_CHARACTER && last_opcode==0x4400f0 && last_input==descriptor+4 && last_input_bytes==3);
|
|
REQUIRE(last_size==17 && captured[16]==0xa5 && outgoing[16]==0);
|
|
break;
|
|
}
|
|
case 17:{
|
|
REQUIRE(incoming_rename(0x2c8)==0 && calls==0);
|
|
gw_wire_pipe_data.world_auth=1;gw_wire_world_data.login_sent=1;
|
|
REQUIRE(incoming_rename(0x2c8)==0 && calls==0);
|
|
gw_wire_world_data.login_sent=0;next_action=GW_CREATE_SOURCE_FRAME;next_opcode=0x37;next_size=6;
|
|
REQUIRE(incoming_rename(0x2c8)==0 && calls==1 && sent==1 && submitted==0 && last_size==6);
|
|
REQUIRE(last_operation==GW_WIRE_CREATE_RESULT);
|
|
next_action=GW_CREATE_NATIVE_REPLY;next_opcode=0x47020e;next_size=11;
|
|
REQUIRE(creation_result()==0 && calls==2 && sent==1 && submitted==1);
|
|
REQUIRE(last_opcode==0x47020e && last_size==11 && captured[10]==0xa5 && outgoing[10]==0);
|
|
break;
|
|
}
|
|
case 18:
|
|
c.value=GW_CREATE_SOURCE_FRAME;c.packet.opcode=0x38;c.required=14;
|
|
REQUIRE(creation_output(&c)==0 && sent==1 && !submitted);
|
|
c.value=GW_CREATE_NATIVE_REPLY;c.packet.opcode=0x4701ad;c.required=4;
|
|
REQUIRE(creation_output(&c)==0 && submitted==1 && last_opcode==0x4701ad);break;
|
|
case 19:{
|
|
const unsigned char packet[]={0xb1,0x01,0x44,0x00,0x80};
|
|
REQUIRE(outgoing_setting(0x4401b1,packet,sizeof(packet))==1 && !calls);
|
|
gw_wire_pipe_data.world_auth=1;
|
|
for(int world=0;world<=1;++world){
|
|
gw_wire_world_data.login_sent=world;
|
|
REQUIRE(!outgoing_setting(0x4401b1,packet,sizeof(packet)));
|
|
REQUIRE(last_operation==GW_WIRE_GAMEPLAY && last_opcode==0x4401b1);
|
|
REQUIRE(last_input==packet+4 && last_input_bytes==1 && calls==(unsigned)(world+1));
|
|
REQUIRE(!sent && !submitted && !service_calls && !drop_calls);
|
|
}
|
|
next_error=-1;
|
|
REQUIRE(!outgoing_setting(0x4401b1,packet,sizeof(packet)));
|
|
REQUIRE(gw_wire_pipe_data.unsupported_out==1 && drop_calls==1 && !service_calls);
|
|
next_error=0;
|
|
REQUIRE(!outgoing_setting(0x4400c3,packet,sizeof(packet)) && service_calls==1);
|
|
REQUIRE(!outgoing_setting(0x4400c4,packet,sizeof(packet)) && service_calls==2);
|
|
break;
|
|
}
|
|
default:return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
class WineCreationDispatchTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.temp=tempfile.TemporaryDirectory();cls.addClassCleanup(cls.temp.cleanup)
|
|
source=(ROOT/'native/wine_pipe_host.c').read_text()
|
|
helpers=['creation_output','creation_request_opcode','creation_request','creation_result']
|
|
functions='\n'.join(braced(source,'static int '+name+'(') for name in helpers)
|
|
fixture=HARNESS.replace('@FUNCTIONS@',functions)
|
|
fixture=fixture.replace('@OUTGOING_RENAME@',braced(source,'if(opcode==0x4400f0){'))
|
|
fixture=fixture.replace('@INCOMING_RENAME@',braced(source,'if(opcode==0x2c8){'))
|
|
fixture=fixture.replace('@OUTGOING_SETTING@',braced(source,'if((opcode==0x4400c4'))
|
|
unit=Path(cls.temp.name)/'fixture.c';unit.write_text(fixture)
|
|
cls.exe=Path(cls.temp.name)/'fixture'
|
|
result=subprocess.run([os.environ.get('CC','cc'),'-std=c11','-Wall','-Wextra','-Werror',
|
|
'-I'+str(ROOT/'native'),str(unit),'-o',str(cls.exe)],capture_output=True,text=True)
|
|
if result.returncode:raise AssertionError('Synthetic C fixture compile failed: '+result.stderr)
|
|
|
|
def case(self,value):
|
|
result=subprocess.run([str(self.exe),str(value)],capture_output=True,text=True)
|
|
self.assertEqual(result.returncode,0,result.stderr)
|
|
|
|
def test_none_and_busy_emit_nothing(self):self.case(1)
|
|
def test_encrypted_source_create_frame_exact_and_cleared(self):self.case(2)
|
|
def test_single_source_roster_followup_frame(self):self.case(3)
|
|
def test_native_result_uses_original_queue(self):self.case(4)
|
|
def test_none_may_not_hide_payload(self):self.case(5)
|
|
def test_unrelated_source_opcode_rejected(self):self.case(6)
|
|
def test_unrelated_native_opcode_rejected(self):self.case(7)
|
|
def test_truncated_or_empty_outputs_rejected(self):self.case(8)
|
|
def test_capacity_and_output_ownership_checked(self):self.case(9)
|
|
def test_unknown_action_rejected(self):self.case(10)
|
|
def test_actual70334_request_and_body_contract(self):self.case(11)
|
|
def test_result_poll_empty_and_no_duplicate_output(self):self.case(12)
|
|
def test_local_unsupported_reply_is_not_source_request(self):self.case(13)
|
|
def test_capacity_error_never_emits(self):self.case(14)
|
|
def test_callback_error_preserved(self):self.case(15)
|
|
|
|
def test_rename_request_is_source_bound_and_preworld_only(self):self.case(16)
|
|
def test_rename_result_and_roster_follow_up_are_preworld_only(self):self.case(17)
|
|
def test_delete_still_flows_through_both_output_paths(self):self.case(18)
|
|
def test_authenticated_sdhd_setter_at_roster_and_in_world(self):self.case(19)
|
|
|
|
def test_actual_source_reply_routes_before_native_roster(self):
|
|
source=(ROOT/'native/wine_pipe_host.c').read_text()
|
|
lifecycle=braced(source,'if(opcode==0x3a || opcode==0x3c){')
|
|
self.assertIn('if(!d->world_auth || creation_result())return -1;',lifecycle)
|
|
roster=braced(source,'if(opcode==0x3b){')
|
|
self.assertLess(roster.index('creation_result()'),roster.index('GW_WIRE_ENCODE_ROSTER,0x470018'))
|
|
self.assertIn('if(opcode==0x440014&&v->bytes==4)c.opcode=0x37;',source)
|
|
|
|
def test_actual_native_request_body_and_dispatch(self):
|
|
source=(ROOT/'native/wine_pipe_host.c').read_text()
|
|
start=source.index('if(opcode==0x420070){');end=source.index('if(opcode==0x440016)',start)
|
|
self.assertIn('creation_request(v->data+4,v->bytes-4)',source[start:end])
|
|
self.assertIn('continue;',source[start:end])
|
|
self.assertNotIn('send_wire(',source[start:end])
|
|
|
|
def test_both_operations_share_existing_authenticated_transport_gate(self):
|
|
source=(ROOT/'native/wine_wire_account.c').read_text()
|
|
start=source.index('case GW_WIRE_CHARACTER_AT:');end=source.index('return status;',start)
|
|
block=source[start:end]
|
|
self.assertIn('case GW_WIRE_CREATE_CHARACTER:case GW_WIRE_CREATE_RESULT:',block)
|
|
self.assertIn('!prepared_channel || gw_wire_auth_data.state!=2 || gw_wire_auth_data.cancel',block)
|
|
self.assertLess(block.index('gw_wire_auth_data.state!=2'),block.index('gw_wire_dispatch(call)'))
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|