Files

193 lines
13 KiB
C++

/* Private proof candidate, included after creation_output in wine_pipe_host.c.
* No export or Lua registration. Caller must supply an already-owned immutable
* snapshot and a separately qualified original-thread/lifetime contract. */
#if defined(GW_SOURCE_UI_CREATE_EXPERIMENT) && defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 3 || GW_SOURCE_ONLY_PROFILE == 5)
#include "../source_creation_options.h"
struct gw_ui_create_pair { uint32_t option, choice; };
struct gw_ui_create_snapshot {
uint32_t abi, size, pipe_epoch, account_generation;
uint64_t owner, pipe, connection, queue;
uint32_t count, standard_mode_verified;
struct gw_source_creation_options creation;
uint8_t race, class_id, sex, name_bytes;
unsigned char name[63];
struct gw_ui_create_pair choices[250];
};
enum gw_ui_create_result { GW_UI_CREATE_HELD=-1, GW_UI_CREATE_BUSY=0,
GW_UI_CREATE_SENT=1, GW_UI_CREATE_LOCAL_REJECTED=2,
GW_UI_CREATE_UNCERTAIN=-2 };
static void gw_ui_create_u32(unsigned char *out,uint32_t value){
out[0]=(unsigned char)value;out[1]=(unsigned char)(value>>8);
out[2]=(unsigned char)(value>>16);out[3]=(unsigned char)(value>>24);
}
static int gw_ui_create_dispatch_locked(const struct gw_ui_create_snapshot *s){
unsigned char body[18+63+250*8];struct gw_wire_call c;
uint32_t bits,at,i,j;int result=GW_UI_CREATE_HELD;
if(!s||s->abi!=1||s->size!=sizeof(*s)||!s->pipe_epoch||!s->account_generation||
!s->owner||!s->pipe||!s->connection||!s->queue||!s->race||!s->class_id||
s->sex>1||!s->name_bytes||s->name_bytes>63||!s->count||s->count>250||
s->standard_mode_verified!=1||!gw_source_creation_options_valid(s->creation))goto wipe;
for(i=0;i<s->name_bytes;++i)if(!s->name[i])goto wipe;
for(i=0;i<s->count;++i){if(!s->choices[i].option||!s->choices[i].choice)goto wipe;
for(j=0;j<i;++j)if(s->choices[i].option==s->choices[j].option)goto wipe;}
bits=(uint32_t)s->name_bytes<<18;body[0]=(unsigned char)(bits>>16);
body[1]=(unsigned char)(bits>>8);body[2]=(unsigned char)bits;
body[3]=s->race;body[4]=s->class_id;body[5]=s->sex;
gw_ui_create_u32(body+6,s->count);gw_ui_create_u32(body+10,0xffffffffu);gw_ui_create_u32(body+14,0);
__builtin_memcpy(body+18,s->name,s->name_bytes);at=18+s->name_bytes;
for(i=0;i<s->count;++i){gw_ui_create_u32(body+at,s->choices[i].option);gw_ui_create_u32(body+at+4,s->choices[i].choice);at+=8;}
if(s->pipe_epoch!=(uint32_t)pipe_generation||s->account_generation!=gw_wire_account_generation()||
s->owner!=gw_wire_pipe_data.owner||s->pipe!=gw_wire_pipe_data.pipe||
s->connection!=gw_wire_pipe_data.connection||s->queue!=gw_wire_pipe_data.queue||
gw_wire_pipe_data.closed||!gw_wire_pipe_data.world_auth||!owned((void*)s->pipe))goto wipe;
init(&c,s->creation.challenge_mask ? GW_WIRE_CREATE_CHARACTER_CHALLENGES : GW_WIRE_CREATE_CHARACTER);c.reserved=s->creation.challenge_mask;c.opcode=0x420070;c.input=body;c.input_bytes=at;
c.output=outgoing;c.capacity=sizeof(outgoing);
/* The backend may have advanced transport state before an error. */
if(call(&c)){result=GW_UI_CREATE_UNCERTAIN;goto wipe;}
if(c.value==GW_CREATE_NONE){result=(!c.required&&c.reserved==GW_CREATE_REJECT_BUSY)?GW_UI_CREATE_BUSY:GW_UI_CREATE_UNCERTAIN;goto wipe;}
if((c.value!=GW_CREATE_SOURCE_FRAME&&c.value!=GW_CREATE_NATIVE_REPLY)||
(c.value==GW_CREATE_SOURCE_FRAME&&c.packet.opcode!=0x36)){result=GW_UI_CREATE_UNCERTAIN;goto wipe;}
/* Any output failure is ambiguous: never automatically replay a send. */
if(creation_output(&c)){result=GW_UI_CREATE_UNCERTAIN;goto wipe;}
result=c.value==GW_CREATE_SOURCE_FRAME?GW_UI_CREATE_SENT:GW_UI_CREATE_LOCAL_REJECTED;
wipe:
SecureZeroMemory(body,sizeof(body));return result;
}
/* Queue is private, not registered/exported. All fields guarded by pipe lock.
* Only native-owned callbacks drain; UI staging never calls send/submit. */
enum gw_ui_queue_state { GW_UI_Q_EMPTY=0, GW_UI_Q_PENDING=1, GW_UI_Q_SENT=2,
GW_UI_Q_LOCAL_REJECTED=3, GW_UI_Q_BUSY=4, GW_UI_Q_UNCERTAIN=5,
GW_UI_Q_EXPIRED=6, GW_UI_Q_HELD=7, GW_UI_Q_RESULT_QUEUED=8, GW_UI_Q_POLL_BUSY=9 };
static struct {uint64_t sequence,token,deadline;uint32_t state,result_code;
struct gw_ui_create_snapshot selected;} gw_ui_queue;
static void gw_ui_queue_wipe_selection(void){SecureZeroMemory(&gw_ui_queue.selected,sizeof(gw_ui_queue.selected));}
static void gw_ui_queue_retire_locked(void){gw_ui_queue_wipe_selection();gw_ui_queue.token=0;gw_ui_queue.deadline=0;gw_ui_queue.state=GW_UI_Q_EMPTY;gw_ui_queue.result_code=0;}
static int gw_ui_queue_stage(const struct gw_ui_create_snapshot*s,uint32_t ttl_ms,uint64_t*out_token){
uint64_t now;uint32_t i,j;int ok=0;
if(!s||!out_token||s->abi!=1||s->size!=sizeof(*s)||!s->owner||!s->pipe||!s->connection||!s->queue||
!s->pipe_epoch||!s->account_generation||!s->race||!s->class_id||s->sex>1||!s->name_bytes||s->name_bytes>63||
!s->count||s->count>250||s->standard_mode_verified!=1||!gw_source_creation_options_valid(s->creation)||!ttl_ms||ttl_ms>30000)return 0;
for(i=0;i<s->name_bytes;++i)if(!s->name[i])return 0;
for(i=0;i<s->count;++i){if(!s->choices[i].option||!s->choices[i].choice)return 0;
for(j=0;j<i;++j)if(s->choices[i].option==s->choices[j].option)return 0;}
*out_token=0;
if(!TryAcquireSRWLockExclusive(&lock))return 0;
now=GetTickCount64();
if(gw_ui_queue.state!=GW_UI_Q_EMPTY||gw_ui_queue.sequence==UINT64_MAX||now>UINT64_MAX-ttl_ms||
s->pipe_epoch!=(uint32_t)pipe_generation||s->account_generation!=gw_wire_account_generation()||
s->owner!=gw_wire_pipe_data.owner||s->pipe!=gw_wire_pipe_data.pipe||
s->connection!=gw_wire_pipe_data.connection||s->queue!=gw_wire_pipe_data.queue||
gw_wire_pipe_data.stage!=1||gw_wire_pipe_data.error||gw_wire_pipe_data.closed||!gw_wire_pipe_data.world_auth)goto done;
gw_ui_queue.selected=*s;gw_ui_queue.token=++gw_ui_queue.sequence;gw_ui_queue.deadline=now+ttl_ms;
gw_ui_queue.state=GW_UI_Q_PENDING;gw_ui_queue.result_code=0;*out_token=gw_ui_queue.token;ok=1;
done: ReleaseSRWLockExclusive(&lock);return ok;
}
/* Caller already holds pipe lock and has validated the genuine callback owner. */
static void gw_ui_queue_drain_locked(void*p,uint32_t epoch){
int result;if(gw_ui_queue.state!=GW_UI_Q_PENDING)return;
if(GetTickCount64()>=gw_ui_queue.deadline){gw_ui_queue_wipe_selection();gw_ui_queue.state=GW_UI_Q_EXPIRED;return;}
if(epoch!=gw_ui_queue.selected.pipe_epoch||(uint64_t)p!=gw_ui_queue.selected.pipe){gw_ui_queue_wipe_selection();gw_ui_queue.state=GW_UI_Q_HELD;return;}
result=gw_ui_create_dispatch_locked(&gw_ui_queue.selected);gw_ui_queue_wipe_selection();
switch(result){case GW_UI_CREATE_SENT:gw_ui_queue.state=GW_UI_Q_SENT;break;
case GW_UI_CREATE_LOCAL_REJECTED:gw_ui_queue.state=GW_UI_Q_LOCAL_REJECTED;break;
case GW_UI_CREATE_BUSY:gw_ui_queue.state=GW_UI_Q_BUSY;break;
case GW_UI_CREATE_UNCERTAIN:gw_ui_queue.state=GW_UI_Q_UNCERTAIN;break;
default:gw_ui_queue.state=GW_UI_Q_HELD;break;}
}
/* Existing frame-owner poll: never blocks the original loop and never sends. */
static void gw_ui_queue_owner_poll(void*owner){
if(!TryAcquireSRWLockExclusive(&lock))return;
if(gw_ui_queue.state==GW_UI_Q_PENDING){
if((uint64_t)owner!=gw_ui_queue.selected.owner||gw_ui_queue.selected.pipe_epoch!=(uint32_t)pipe_generation||
gw_ui_queue.selected.account_generation!=gw_wire_account_generation()||gw_wire_pipe_data.closed||gw_wire_pipe_data.error){
gw_ui_queue_wipe_selection();gw_ui_queue.state=GW_UI_Q_HELD;
}else if(GetTickCount64()>=gw_ui_queue.deadline){gw_ui_queue_wipe_selection();gw_ui_queue.state=GW_UI_Q_EXPIRED;}
}
ReleaseSRWLockExclusive(&lock);
}
/* Caller holds pipe lock. Called only after original output queue accepted a
* genuine terminal Create reply; the native UI has not necessarily consumed it. */
static void gw_ui_queue_terminal_reply_locked(uint32_t native_code){
if(gw_ui_queue.state==GW_UI_Q_SENT){gw_ui_queue.state=GW_UI_Q_RESULT_QUEUED;gw_ui_queue.result_code=native_code;}
}
static void gw_ui_queue_reply_failure_locked(void){
if(gw_ui_queue.state==GW_UI_Q_SENT)gw_ui_queue.state=GW_UI_Q_UNCERTAIN;
}
/* Explicit user-driven rearm only for states where this queue sent nothing.
* Sent/uncertain/queued-result require a separately proven UI-event ack. */
static int gw_ui_queue_ack_no_send(uint64_t token,uint32_t epoch,uint32_t account){
int ok=0;if(!TryAcquireSRWLockExclusive(&lock))return 0;
if(token&&token==gw_ui_queue.token&&epoch==(uint32_t)pipe_generation&&account==gw_wire_account_generation()&&
(gw_ui_queue.state==GW_UI_Q_EXPIRED||gw_ui_queue.state==GW_UI_Q_HELD||gw_ui_queue.state==GW_UI_Q_BUSY)){
gw_ui_queue_wipe_selection();gw_ui_queue.token=0;gw_ui_queue.deadline=0;gw_ui_queue.state=GW_UI_Q_EMPTY;ok=1;
}
ReleaseSRWLockExclusive(&lock);return ok;
}
/* Metadata only; polling can expire an undrained request without fabricating a result. */
static uint32_t gw_ui_queue_status(uint64_t token,uint32_t epoch,uint32_t account){
uint32_t state=GW_UI_Q_HELD;if(!TryAcquireSRWLockExclusive(&lock))return GW_UI_Q_POLL_BUSY;
if(token&&token==gw_ui_queue.token&&epoch==(uint32_t)pipe_generation&&account==gw_wire_account_generation()){
if(gw_ui_queue.state==GW_UI_Q_PENDING&&GetTickCount64()>=gw_ui_queue.deadline){gw_ui_queue_wipe_selection();gw_ui_queue.state=GW_UI_Q_EXPIRED;}
state=gw_ui_queue.state;
}
ReleaseSRWLockExclusive(&lock);return state;
}
/* Private Source-profile exports. Native binder supplies exact own-module pipe
* context; UI cannot supply or read this pointer. No transport is opened here. */
#ifdef _WIN32
#define GW_UI_QUEUE_EXPORT __declspec(dllexport)
#else
#define GW_UI_QUEUE_EXPORT
#endif
GW_UI_QUEUE_EXPORT int gw_source_create_queue_stage(const struct gw_ui_create_snapshot*s,uint32_t ttl,uint64_t*t,void*context){
if(context!=(void*)&gw_wire_pipe_data)return 0;
return gw_ui_queue_stage(s,ttl,t);
}
GW_UI_QUEUE_EXPORT uint32_t gw_source_create_queue_status(uint64_t t,uint32_t epoch,uint32_t account,void*context){
if(context!=(void*)&gw_wire_pipe_data)return GW_UI_Q_HELD;
return gw_ui_queue_status(t,epoch,account);
}
GW_UI_QUEUE_EXPORT int gw_source_create_queue_ack_no_send(uint64_t t,uint32_t epoch,uint32_t account,void*context){
if(context!=(void*)&gw_wire_pipe_data)return 0;
return gw_ui_queue_ack_no_send(t,epoch,account);
}
/* Glue-facing create route: the Wire itself binds the current owned pipe
* context (the caller never supplies it). A terminal earlier entry is retired
* first so a later Finish can stage; a pending one is never replaced. The
* queued body is only sent from a genuine pipe callback (drain). */
GW_UI_QUEUE_EXPORT int gw_source_create_stage_challenges(uint32_t mask,uint32_t race_class_sex,const unsigned char*name,
uint32_t name_bytes,const struct gw_ui_create_pair*pairs,uint32_t count,uint64_t*token){
static struct gw_ui_create_snapshot s;int ok;
struct gw_source_creation_options options={mask};
if(!gw_source_creation_options_valid(options))return 0;
if(!name||!pairs||!token||!name_bytes||name_bytes>63||!count||count>250)return 0;
if(!TryAcquireSRWLockExclusive(&lock))return 0;
if(gw_ui_queue.state==GW_UI_Q_PENDING){ReleaseSRWLockExclusive(&lock);return 0;}
gw_ui_queue_retire_locked();
SecureZeroMemory(&s,sizeof(s));s.abi=1;s.size=sizeof(s);s.creation=options;
s.pipe_epoch=(uint32_t)pipe_generation;s.account_generation=gw_wire_account_generation();
s.owner=gw_wire_pipe_data.owner;s.pipe=gw_wire_pipe_data.pipe;
s.connection=gw_wire_pipe_data.connection;s.queue=gw_wire_pipe_data.queue;
ReleaseSRWLockExclusive(&lock);
s.race=(uint8_t)race_class_sex;s.class_id=(uint8_t)(race_class_sex>>8);s.sex=(uint8_t)(race_class_sex>>16);
s.name_bytes=(uint8_t)name_bytes;__builtin_memcpy(s.name,name,name_bytes);
s.count=count;__builtin_memcpy(s.choices,pairs,count*sizeof(*pairs));s.standard_mode_verified=1;
ok=gw_ui_queue_stage(&s,30000,token);SecureZeroMemory(&s,sizeof(s));return ok;
}
GW_UI_QUEUE_EXPORT int gw_source_create_stage_current(uint32_t race_class_sex,const unsigned char*name,
uint32_t name_bytes,const struct gw_ui_create_pair*pairs,uint32_t count,uint64_t*token){
return gw_source_create_stage_challenges(0,race_class_sex,name,name_bytes,pairs,count,token);
}
GW_UI_QUEUE_EXPORT uint32_t gw_source_create_status_current(uint64_t token){
return gw_ui_queue_status(token,(uint32_t)pipe_generation,gw_wire_account_generation());
}
/* Native create result code (24 success; 25..33/96..109 named failures) of the
* genuine terminal reply for this token; 0 while none was received. */
GW_UI_QUEUE_EXPORT uint32_t gw_source_create_result_current(uint64_t token){
uint32_t code=0;if(!TryAcquireSRWLockExclusive(&lock))return 0;
if(token&&token==gw_ui_queue.token&&gw_ui_queue.state==GW_UI_Q_RESULT_QUEUED)code=gw_ui_queue.result_code;
ReleaseSRWLockExclusive(&lock);return code;
}
#undef GW_UI_QUEUE_EXPORT
#endif