476 lines
26 KiB
C
476 lines
26 KiB
C
/* One owned post-render input binds the in-process Source poll and exits. */
|
|
#include "startup_bootstrap.h"
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include "login_state_journal.h"
|
|
#ifndef GW_SOURCE_ONLY_PROFILE
|
|
#error source profile required
|
|
#endif
|
|
#if GW_SOURCE_ONLY_PROFILE != 2
|
|
#error private probe is profile2 only
|
|
#endif
|
|
typedef uint32_t u32;
|
|
typedef uint64_t u64;
|
|
typedef void *HANDLE;
|
|
extern int gw_startup_ensure_enus(void);
|
|
extern int gw_bridge_nohelper_start(void *module,u32 primary_thread);
|
|
extern int gw_bridge_nohelper_on_owned_input(void *hwnd,u32 owner_thread,u32 owner_pid);
|
|
extern u32 gw_bridge_nohelper_error(void);
|
|
extern u32 gw_bridge_nohelper_stage(void);
|
|
struct GwMemoryInfo {
|
|
void *base, *allocation_base;
|
|
u32 allocation_protect;
|
|
uint16_t partition_id, padding0;
|
|
uintptr_t region_size;
|
|
u32 state, protect, type, padding1;
|
|
};
|
|
_Static_assert(sizeof(struct GwMemoryInfo)==48,"Win64 VirtualQuery layout changed");
|
|
__declspec(dllimport) u32 __stdcall GetCurrentThreadId(void);
|
|
__declspec(dllimport) u32 __stdcall GetCurrentProcessId(void);
|
|
__declspec(dllimport) u32 __stdcall GetLastError(void);
|
|
__declspec(dllimport) void * __stdcall GetCurrentProcess(void);
|
|
__declspec(dllimport) void * __stdcall GetModuleHandleA(const char *);
|
|
__declspec(dllimport) int __stdcall GetModuleHandleExA(u32,const char *,HANDLE *);
|
|
#ifdef GW_WINDOWS_NATIVE_D3D11
|
|
__declspec(dllimport) u32 __stdcall GetSystemDirectoryW(uint16_t *,u32);
|
|
__declspec(dllimport) void * __stdcall LoadLibraryExW(const uint16_t *,void *,u32);
|
|
__declspec(dllimport) void * __stdcall GetProcAddress(void *,const char *);
|
|
#endif
|
|
__declspec(dllimport) u32 __stdcall GetEnvironmentVariableA(const char *,char *,u32);
|
|
__declspec(dllimport) u32 __stdcall GetModuleFileNameA(HANDLE,char *,u32);
|
|
__declspec(dllimport) int __stdcall CreateDirectoryA(const char *,void *);
|
|
__declspec(dllimport) void __stdcall SetLastError(u32);
|
|
__declspec(dllimport) HANDLE __stdcall CreateFileA(const char *,u32,u32,void *,u32,u32,HANDLE);
|
|
__declspec(dllimport) int __stdcall WriteFile(HANDLE,const void *,u32,u32 *,void *);
|
|
__declspec(dllimport) int __stdcall CloseHandle(HANDLE);
|
|
__declspec(dllimport) uintptr_t __stdcall VirtualQuery(const void *,struct GwMemoryInfo *,uintptr_t);
|
|
__declspec(dllimport) int __stdcall ReadProcessMemory(HANDLE,const void *,void *,uintptr_t,uintptr_t *);
|
|
typedef intptr_t (__stdcall *GwHookProc)(int,uintptr_t,intptr_t);
|
|
__declspec(dllimport) HANDLE __stdcall SetWindowsHookExA(int,GwHookProc,HANDLE,u32);
|
|
__declspec(dllimport) intptr_t __stdcall CallNextHookEx(HANDLE,int,uintptr_t,intptr_t);
|
|
__declspec(dllimport) int __stdcall UnhookWindowsHookEx(HANDLE);
|
|
__declspec(dllimport) u32 __stdcall GetWindowThreadProcessId(HANDLE,u32 *);
|
|
__declspec(dllimport) HANDLE __stdcall GetForegroundWindow(void);
|
|
__declspec(dllimport) HANDLE __stdcall GetAncestor(HANDLE,u32);
|
|
struct GwMsg { HANDLE hwnd;u32 message,pad;uintptr_t wparam;intptr_t lparam;u32 time;int32_t x,y;u32 private_field;};
|
|
_Static_assert(sizeof(struct GwMsg)==48,"Win64 MSG layout changed");
|
|
extern int __stdcall gw_marker_entry(void *,unsigned long,void *);
|
|
struct StartupStatus {
|
|
u32 abi,size,attach_count,attach_thread,first_export_count,first_export_thread;
|
|
u32 init_state,patch_result,namespace_plan_count,ready_return_count,error,reserved;
|
|
u64 module,d3d_module,lua_state;u32 namespace_thread,published_rows;
|
|
};
|
|
__declspec(dllexport) struct StartupStatus gw_startup_status={1,sizeof(struct StartupStatus)};
|
|
static char log_path[1024],summary_path[1024],login_path[1024];
|
|
static u32 summary_lock,summary_seen,summary_previous[8];
|
|
static u32 login_lock,login_seen,login_rows;
|
|
static int32_t login_previous[14];
|
|
static HANDLE tab_hook;
|
|
static volatile u32 hook_install_state,witness_done;
|
|
static volatile u32 late_bridge_started,input_retry_count;
|
|
#define GW_STARTUP_INPUT_RETRY_LIMIT 8u
|
|
/* Natural graphics and explicit Login-UI arming share one gate. A worker may
|
|
* arm a thread hook, but coordinator initialization/binding stays on the real
|
|
* owned UI input. Never manufacture first-graphics telemetry for late mode. */
|
|
static u32 input_thread, late_mode;
|
|
static HANDLE late_window, late_module;
|
|
static volatile u32 late_hook_ready;
|
|
struct GwStartupLoginUIRequest {
|
|
u32 abi,size,flags,expected_pid,expected_ui_thread,reserved;
|
|
u64 top_level_hwnd,owner_nonce;
|
|
};
|
|
_Static_assert(sizeof(struct GwStartupLoginUIRequest)==40,"Login UI request ABI");
|
|
_Static_assert(offsetof(struct GwStartupLoginUIRequest,top_level_hwnd)==24,"Login UI HWND offset");
|
|
_Static_assert(offsetof(struct GwStartupLoginUIRequest,owner_nonce)==32,"Login UI nonce offset");
|
|
/* Separate additive telemetry: existing StartupStatus ABI is unchanged.
|
|
* phase: 0 dormant, 1 armed, 2 input admitted, 3 started, 4 bound, 5 failed.
|
|
* error: 1 unreadable request, 2 layout, 3 owner, 4 image, 5 own module,
|
|
* 6 once gate claimed, 7 hook, 8 coordinator start, 9 bind, 10 unhook.
|
|
* Rejections before claiming the gate return a code without replacing an
|
|
* existing owner's status. detail is the coordinator error, not Win32 error. */
|
|
struct GwStartupLateStatus {
|
|
u32 abi,size;
|
|
volatile u32 phase,error,winerror,detail;
|
|
u32 arm_thread,ui_thread,pid,unhook_error;
|
|
u64 hwnd,owner_nonce,module;
|
|
};
|
|
_Static_assert(sizeof(struct GwStartupLateStatus)==64,"Login UI status ABI");
|
|
__declspec(dllexport) struct GwStartupLateStatus gw_startup_late_status_v1={1,sizeof(struct GwStartupLateStatus)};
|
|
static void read_startup_log(void){
|
|
static const char directory[]="Diagnostics",suffix[]="Diagnostics\\startup-state.jsonl";
|
|
static const char login_suffix[]="Diagnostics\\login-state.jsonl";
|
|
u32 saved=GetLastError(),n,i,slash=0;
|
|
n=GetEnvironmentVariableA("GW_NATIVE_STARTUP_LOG",log_path,sizeof(log_path));
|
|
if(n>=sizeof(log_path))log_path[0]=0;
|
|
n=GetModuleFileNameA(0,summary_path,sizeof(summary_path));
|
|
if(!n||n>=sizeof(summary_path)){summary_path[0]=0;goto done;}
|
|
for(i=0;i<n;i++)if(summary_path[i]=='\\'||summary_path[i]=='/')slash=i+1;
|
|
if(!slash||slash+sizeof(suffix)>sizeof(summary_path)){summary_path[0]=0;goto done;}
|
|
for(i=0;i<slash;i++)login_path[i]=summary_path[i];
|
|
for(i=0;i<sizeof(login_suffix);i++)login_path[slash+i]=login_suffix[i];
|
|
for(i=0;i<sizeof(directory);i++)summary_path[slash+i]=directory[i];
|
|
CreateDirectoryA(summary_path,0);
|
|
for(i=0;i<sizeof(suffix);i++)summary_path[slash+i]=suffix[i];
|
|
done:
|
|
SetLastError(saved);
|
|
}
|
|
|
|
static char *put(char *p,const char *s){while(*s)*p++=*s++;return p;}
|
|
static char *number(char *p,u64 x){char n[24];u32 i=0;do{n[i++]=(char)('0'+x%10);x/=10;}while(x);while(i)*p++=n[--i];return p;}
|
|
static char *signed_number(char *p,int32_t value){
|
|
if(value<0){*p++='-';return number(p,(u64)(-(int64_t)value));}
|
|
return number(p,(u32)value);
|
|
}
|
|
void gw_startup_record_login_state(const struct GwLoginJournalState *state){
|
|
static const char *keys[]={"generation","published_generation","credentials_state","consumed_generation",
|
|
"auth_state","auth_error","auth_result","discovery_result","realm_count",
|
|
"transport_prepared","frame_command","frame_error","retirement_state","retirement_result"};
|
|
u32 expected=0,same,i,saved,written=0;char line[800],*p=line;HANDLE file;
|
|
if(!state || !__atomic_compare_exchange_n(&login_lock,&expected,1,0,__ATOMIC_ACQUIRE,__ATOMIC_RELAXED))return;
|
|
const int32_t values[]={state->generation,state->published_generation,state->credentials_state,
|
|
state->consumed_generation,state->auth_state,state->auth_error,state->auth_result,
|
|
state->discovery_result,state->realm_count,state->transport_prepared,state->frame_command,
|
|
state->frame_error,state->retirement_state,state->retirement_result};
|
|
saved=GetLastError();
|
|
if(!login_path[0] || login_rows>=256)goto done;
|
|
same=login_seen;
|
|
for(i=0;i<14;i++)if(values[i]!=login_previous[i])same=0;
|
|
if(same)goto done;
|
|
p=put(p,"{\"kind\":\"login-state-v1\",\"pid\":");p=number(p,GetCurrentProcessId());
|
|
for(i=0;i<14;i++){p=put(p,",\"");p=put(p,keys[i]);p=put(p,"\":");p=signed_number(p,values[i]);}
|
|
p=put(p,"}\n");
|
|
file=CreateFileA(login_path,4,3,0,4,0x80,0);
|
|
if(file==(HANDLE)(uintptr_t)-1){login_path[0]=0;goto done;}
|
|
if(WriteFile(file,line,(u32)(p-line),&written,0) && written==(u32)(p-line)){
|
|
for(i=0;i<14;i++)login_previous[i]=values[i];
|
|
login_seen=1;++login_rows;
|
|
}
|
|
CloseHandle(file);
|
|
done:
|
|
SetLastError(saved);__atomic_store_n(&login_lock,0,__ATOMIC_RELEASE);
|
|
}
|
|
/* A default journal contains only state/counters, never the detailed probe's
|
|
* arguments, input messages, authentication data or player information. */
|
|
static void write_startup_summary(void){
|
|
static const char *keys[8]={"init","startup_error","bridge_stage","bridge_error",
|
|
"first_export","ready_returns","namespace_plans","published_rows"};
|
|
u32 values[8],i,same,expected=0,saved,written=0;
|
|
char line[400],*p=line;HANDLE file;
|
|
if(!__atomic_compare_exchange_n(&summary_lock,&expected,1,0,
|
|
__ATOMIC_ACQUIRE,__ATOMIC_RELAXED))return;
|
|
saved=GetLastError();
|
|
if(!summary_path[0])goto done;
|
|
same=summary_seen;
|
|
values[0]=gw_startup_status.init_state;values[1]=gw_startup_status.error;
|
|
values[2]=gw_bridge_nohelper_stage();values[3]=gw_bridge_nohelper_error();
|
|
values[4]=gw_startup_status.first_export_count;values[5]=gw_startup_status.ready_return_count;
|
|
values[6]=gw_startup_status.namespace_plan_count;values[7]=gw_startup_status.published_rows;
|
|
for(i=0;i<8;i++)if(values[i]!=summary_previous[i])same=0;
|
|
if(same)goto done;
|
|
p=put(p,"{\"kind\":\"startup-state-v1\"");
|
|
for(i=0;i<8;i++){p=put(p,",\"");p=put(p,keys[i]);p=put(p,"\":");p=number(p,values[i]);}
|
|
p=put(p,"}\n");
|
|
file=CreateFileA(summary_path,4,3,0,4,0x80,0);
|
|
if(file==(HANDLE)(uintptr_t)-1){summary_path[0]=0;goto done;}
|
|
if(WriteFile(file,line,(u32)(p-line),&written,0)&&written==(u32)(p-line)){
|
|
for(i=0;i<8;i++)summary_previous[i]=values[i];
|
|
summary_seen=1;
|
|
}
|
|
CloseHandle(file);
|
|
done:
|
|
SetLastError(saved);__atomic_store_n(&summary_lock,0,__ATOMIC_RELEASE);
|
|
}
|
|
|
|
static void log_probe(u32 event,u32 tid,u32 pid,uintptr_t module,uintptr_t query,
|
|
u32 state,u32 protect,u32 read_ok,u32 match,uintptr_t got,u32 error,
|
|
uintptr_t hwnd,u32 owner_tid,u32 owner_pid,u32 foreground_match){
|
|
char line[400],*p=line;u32 written=0;HANDLE file;
|
|
write_startup_summary();
|
|
if(!log_path[0])return;
|
|
p=put(p,"GW_LOGIN_SITE event=");p=number(p,event);
|
|
p=put(p," tid=");p=number(p,tid);
|
|
p=put(p," pid=");p=number(p,pid);
|
|
p=put(p," module=");p=number(p,module);
|
|
p=put(p," query=");p=number(p,query);
|
|
p=put(p," state=");p=number(p,state);
|
|
p=put(p," protect=");p=number(p,protect);
|
|
p=put(p," rpm=");p=number(p,read_ok);
|
|
p=put(p," match=");p=number(p,match);
|
|
p=put(p," got=");p=number(p,got);
|
|
p=put(p," error=");p=number(p,error);
|
|
p=put(p," hwnd=");p=number(p,hwnd);
|
|
p=put(p," owner_tid=");p=number(p,owner_tid);
|
|
p=put(p," owner_pid=");p=number(p,owner_pid);
|
|
p=put(p," foreground_match=");p=number(p,foreground_match);*p++='\n';
|
|
file=CreateFileA(log_path,4,3,0,4,0x80,0);
|
|
if(file==(HANDLE)(uintptr_t)-1)return;
|
|
WriteFile(file,line,(u32)(p-line),&written,0);CloseHandle(file);
|
|
}
|
|
int __stdcall gw_startup_module_entry(void *module,unsigned long reason,void *reserved){
|
|
int r;
|
|
/* Non-fatal: a read-only or locked WTF folder must not stop the client from loading; the updater still
|
|
* pins enUS before each launch. */
|
|
if(reason==1)(void)gw_startup_ensure_enus();
|
|
r=gw_marker_entry(module,reason,reserved);
|
|
if(reason==1){gw_startup_status.module=(u64)(uintptr_t)module;
|
|
gw_startup_status.attach_count++;gw_startup_status.attach_thread=GetCurrentThreadId();}
|
|
return r;
|
|
}
|
|
void gw_bridge_nohelper_log(u32 event,u32 result,u32 error){
|
|
log_probe(event,GetCurrentThreadId(),GetCurrentProcessId(),
|
|
(uintptr_t)GetModuleHandleA(0),result,0,0,0,result,0,error,
|
|
0,0,0,0);
|
|
}
|
|
static u32 on_owned_input(HANDLE hwnd,u32 owner_tid,u32 owner_pid,u32 foreground_match){
|
|
u32 accepted=(u32)gw_bridge_nohelper_on_owned_input(hwnd,owner_tid,owner_pid);
|
|
log_probe(2,GetCurrentThreadId(),GetCurrentProcessId(),
|
|
(uintptr_t)GetModuleHandleA(0),accepted,0,0,0,accepted,0,
|
|
gw_bridge_nohelper_error(),(uintptr_t)hwnd,
|
|
owner_tid,owner_pid,foreground_match);
|
|
return accepted;
|
|
}
|
|
static int late_input_matches(const struct GwMsg *message,u32 owner_tid,u32 owner_pid){
|
|
u32 root_pid=0,root_tid;
|
|
if(!__atomic_load_n(&late_hook_ready,__ATOMIC_ACQUIRE) ||
|
|
message->message!=0x100 || message->wparam!=9 ||
|
|
GetCurrentThreadId()!=input_thread || owner_tid!=input_thread ||
|
|
owner_pid!=GetCurrentProcessId() ||
|
|
GetAncestor(message->hwnd,2)!=late_window)return 0;
|
|
root_tid=GetWindowThreadProcessId(late_window,&root_pid);
|
|
return root_tid==input_thread && root_pid==GetCurrentProcessId() &&
|
|
GetAncestor(late_window,2)==late_window;
|
|
}
|
|
static intptr_t __stdcall tab_message_hook(int code,uintptr_t remove,intptr_t raw){
|
|
const struct GwMsg *message=(const struct GwMsg *)raw;
|
|
u32 owner_pid=0,owner_tid=0,expected=0,foreground_match=0;
|
|
u32 keep_hook=0;
|
|
intptr_t next;
|
|
if(code==0 && remove==1 && message &&
|
|
(message->message==0x100 || message->message==0x201 ||
|
|
message->message==0x204) && message->hwnd){
|
|
owner_tid=GetWindowThreadProcessId(message->hwnd,&owner_pid);
|
|
foreground_match=(u32)(GetAncestor(message->hwnd,2)==GetForegroundWindow());
|
|
if(owner_tid==input_thread &&
|
|
owner_pid==GetCurrentProcessId() && foreground_match &&
|
|
(!late_mode || late_input_matches(message,owner_tid,owner_pid)) &&
|
|
__atomic_compare_exchange_n(&witness_done,&expected,1,0,__ATOMIC_ACQ_REL,__ATOMIC_ACQUIRE)){
|
|
if(late_mode){
|
|
gw_startup_late_status_v1.phase=2;
|
|
if(!late_bridge_started && !gw_bridge_nohelper_start(late_module,input_thread)){
|
|
gw_startup_late_status_v1.detail=gw_bridge_nohelper_error();
|
|
gw_startup_late_status_v1.error=8;
|
|
gw_startup_late_status_v1.phase=5;
|
|
}else{
|
|
late_bridge_started=1;
|
|
gw_startup_late_status_v1.phase=3;
|
|
if(on_owned_input(message->hwnd,owner_tid,owner_pid,foreground_match))
|
|
gw_startup_late_status_v1.phase=4;
|
|
else{
|
|
u32 detail=gw_bridge_nohelper_error();
|
|
if(detail==2 && ++input_retry_count<GW_STARTUP_INPUT_RETRY_LIMIT){
|
|
/* Keep the exact owned-thread hook armed. The
|
|
* bridge may not have published its poll binding
|
|
* when this first Enter was observed. */
|
|
gw_startup_late_status_v1.detail=detail;
|
|
gw_startup_late_status_v1.error=0;
|
|
keep_hook=1;
|
|
}else{
|
|
gw_startup_late_status_v1.detail=detail;
|
|
gw_startup_late_status_v1.error=9;
|
|
gw_startup_late_status_v1.phase=5;
|
|
}
|
|
}
|
|
}
|
|
}else if(!on_owned_input(message->hwnd,owner_tid,owner_pid,foreground_match) &&
|
|
gw_bridge_nohelper_error()==2 &&
|
|
++input_retry_count<GW_STARTUP_INPUT_RETRY_LIMIT){
|
|
keep_hook=1;
|
|
}
|
|
next=CallNextHookEx(tab_hook,code,remove,raw);
|
|
if(keep_hook){
|
|
__atomic_store_n(&witness_done,0,__ATOMIC_RELEASE);
|
|
}else if(!UnhookWindowsHookEx(tab_hook) && late_mode){
|
|
gw_startup_late_status_v1.unhook_error=GetLastError();
|
|
if(!gw_startup_late_status_v1.error){
|
|
gw_startup_late_status_v1.error=10;
|
|
gw_startup_late_status_v1.phase=5;
|
|
}
|
|
}
|
|
return next;
|
|
}
|
|
}
|
|
return CallNextHookEx(tab_hook,code,remove,raw);
|
|
}
|
|
/* This is an arm request, not a graphics/credential/Source activation RPC.
|
|
* The controller must own the exact PID/creation-time/image/module lifetime
|
|
* and have observed original Login UI before credential submission. */
|
|
__declspec(dllexport) u32 __stdcall gw_startup_arm_login_ui_v1(void *request){
|
|
struct GwStartupLoginUIRequest r;
|
|
uintptr_t got=0;
|
|
u32 pid=0,tid,expected=0;
|
|
HANDLE own_bridge=0,window;
|
|
if(!request || !ReadProcessMemory(GetCurrentProcess(),request,&r,sizeof(r),&got) || got!=sizeof(r))return 1;
|
|
if(r.abi!=1 || r.size!=sizeof(r) || r.flags || r.reserved || !r.owner_nonce)return 2;
|
|
window=(HANDLE)(uintptr_t)r.top_level_hwnd;
|
|
if(!window || !r.expected_ui_thread || r.expected_pid!=GetCurrentProcessId())return 3;
|
|
tid=GetWindowThreadProcessId(window,&pid);
|
|
if(!tid || pid!=r.expected_pid || tid!=r.expected_ui_thread || GetAncestor(window,2)!=window)return 3;
|
|
/* All maintained owner/vtable/site addresses belong to this exact base.
|
|
* The controller separately validates the frozen file/hash identity. */
|
|
if((uintptr_t)GetModuleHandleA(0)!=0x140000000ULL)return 4;
|
|
if(!GetModuleHandleExA(6,(const char *)(uintptr_t)&gw_startup_arm_login_ui_v1,&own_bridge) || !own_bridge)return 5;
|
|
if(!__atomic_compare_exchange_n(&hook_install_state,&expected,1,0,__ATOMIC_ACQ_REL,__ATOMIC_ACQUIRE))return 6;
|
|
late_mode=1;input_thread=tid;late_window=window;late_module=own_bridge;
|
|
gw_startup_late_status_v1.arm_thread=GetCurrentThreadId();
|
|
gw_startup_late_status_v1.ui_thread=tid;gw_startup_late_status_v1.pid=pid;
|
|
gw_startup_late_status_v1.hwnd=r.top_level_hwnd;
|
|
gw_startup_late_status_v1.owner_nonce=r.owner_nonce;
|
|
gw_startup_late_status_v1.module=(u64)(uintptr_t)own_bridge;
|
|
gw_startup_late_status_v1.phase=1;
|
|
read_startup_log();
|
|
tab_hook=SetWindowsHookExA(3,tab_message_hook,own_bridge,tid);
|
|
if(!tab_hook){
|
|
gw_startup_late_status_v1.winerror=GetLastError();
|
|
gw_startup_late_status_v1.error=7;gw_startup_late_status_v1.phase=5;
|
|
return 7;
|
|
}
|
|
/* A synchronous/overlapping message before hook-handle publication is
|
|
* forwarded without consuming the rendezvous; a later owned Tab may bind. */
|
|
__atomic_store_n(&late_hook_ready,1,__ATOMIC_RELEASE);
|
|
return 0;
|
|
}
|
|
static void probe_on_real_creation(void){
|
|
u32 expected=0,ready;
|
|
HANDLE own_bridge;
|
|
if(!__atomic_compare_exchange_n(&hook_install_state,&expected,1,0,__ATOMIC_ACQ_REL,__ATOMIC_ACQUIRE))return;
|
|
read_startup_log();
|
|
input_thread=GetCurrentThreadId();
|
|
gw_startup_status.first_export_thread=input_thread;gw_startup_status.first_export_count=1;
|
|
own_bridge=GetModuleHandleA("GWNativeBridge.dll");
|
|
gw_startup_status.module=(u64)(uintptr_t)own_bridge;
|
|
ready=(u32)gw_bridge_nohelper_start(own_bridge,
|
|
gw_startup_status.first_export_thread);
|
|
if(!ready){gw_startup_status.init_state=3;gw_bridge_nohelper_log(1,0,gw_bridge_nohelper_error());return;}
|
|
tab_hook=SetWindowsHookExA(3,tab_message_hook,0,gw_startup_status.first_export_thread);
|
|
gw_startup_status.init_state=tab_hook?2:3;
|
|
log_probe(1,GetCurrentThreadId(),GetCurrentProcessId(),
|
|
(uintptr_t)GetModuleHandleA(0),(uintptr_t)tab_hook,0,0,0,0,0,
|
|
tab_hook?0:GetLastError(),0,0,0,0);
|
|
}
|
|
typedef int32_t (__stdcall *CreateDeviceFn)(void *,u32,void *,u32,const u32 *,u32,u32,const void *,void **,void **,u32 *,void **);
|
|
/* The app-local core proxy calls this only after a genuine successful
|
|
* D3D12GetInterface invocation, never during module attachment. Reuse the
|
|
* accepted creation-thread/owned-input coordinator and its once gate. */
|
|
__declspec(dllexport) void __stdcall gw_startup_D3D12CoreInterfaceReady(void *provider){
|
|
gw_startup_status.d3d_module=(u64)(uintptr_t)provider;
|
|
probe_on_real_creation();
|
|
}
|
|
#ifdef GW_WINDOWS_NATIVE_D3D11
|
|
/* The native proxy has the same basename as the system DLL. Resolve an
|
|
* absolute System32 path; a basename lookup would return the proxy itself. */
|
|
#include "dx12_carrier_forwarding.h"
|
|
static int gw_read_dx12_carrier(void *module,uint32_t offset,void *out,size_t count,int executable){
|
|
uintptr_t address=(uintptr_t)module+offset, got=0, cursor=address;
|
|
struct GwMemoryInfo info;
|
|
if(address<(uintptr_t)module || count>UINTPTR_MAX-address)return -1;
|
|
while(cursor<address+count){
|
|
size_t remaining=address+count-cursor, chunk;
|
|
if(VirtualQuery((const void *)cursor,&info,sizeof(info))!=sizeof(info) ||
|
|
cursor<(uintptr_t)info.base || cursor-(uintptr_t)info.base>=info.region_size)return -1;
|
|
chunk=info.region_size-(cursor-(uintptr_t)info.base);
|
|
if(chunk>remaining)chunk=remaining;
|
|
if(!gw_dx12_carrier_region_valid((uintptr_t)module,cursor,chunk,
|
|
(uintptr_t)info.allocation_base,(uintptr_t)info.base,info.region_size,
|
|
info.state,info.protect,info.type,executable))return -1;
|
|
cursor+=chunk;
|
|
}
|
|
return ReadProcessMemory(GetCurrentProcess(),(const void *)address,out,count,&got) && got==count;
|
|
}
|
|
/* Existing event150: result0, error1 path,2 provider load,3 export,
|
|
* 4 non-DX12 self-cycle,10 guard input,11 header,12 export bytes,13 entry,
|
|
* 20 allocation/protection,21 failed/short copy. No memory bytes are logged. */
|
|
static void *gw_d3d_forwarding_failure(u32 reason){
|
|
gw_startup_status.error=0xd3000000u|reason;
|
|
read_startup_log();gw_bridge_nohelper_log(150,0,reason);return 0;
|
|
}
|
|
static void *gw_native_d3d_export(const char *name,void **module,uint16_t version,void *self){
|
|
const uint16_t suffix[]={'\\','d','3','d','1',version,'.','d','l','l',0};
|
|
uint16_t path[512];
|
|
u32 count=GetSystemDirectoryW(path,512),i;
|
|
void *real,*resolved;u32 reason=0;
|
|
if(!count || count>=512 || count+sizeof(suffix)/sizeof(suffix[0])>512)return gw_d3d_forwarding_failure(1);
|
|
for(i=0;i<sizeof(suffix)/sizeof(suffix[0]);++i)path[count+i]=suffix[i];
|
|
real=LoadLibraryExW(path,0,0x00000800u); /* LOAD_LIBRARY_SEARCH_SYSTEM32 */
|
|
if(!real)return gw_d3d_forwarding_failure(2);
|
|
*module=real;
|
|
/* The accepted owned Wine carrier forwards CreateDevice back to this
|
|
* wrapper. Only its exact retained DX12 entry may resolve that cycle;
|
|
* ordinary Windows/non-self exports keep the original path unchanged. */
|
|
resolved=GetProcAddress(real,name);
|
|
if(!resolved)return gw_d3d_forwarding_failure(3);
|
|
if(version=='2'){
|
|
resolved=gw_dx12_carrier_resolve(real,resolved,self,gw_read_dx12_carrier,&reason);
|
|
return resolved?resolved:gw_d3d_forwarding_failure(reason);
|
|
}
|
|
return resolved==self?gw_d3d_forwarding_failure(4):resolved;
|
|
}
|
|
#endif
|
|
__declspec(dllexport) int32_t __stdcall gw_startup_D3D11CreateDeviceAndSwapChain(void *adapter,u32 driver,void *software,u32 flags,const u32 *levels,u32 level_count,u32 sdk,const void *desc,void **swap,void **device,u32 *selected,void **context){
|
|
void *d3d=0;CreateDeviceFn original;int32_t result;u32 saved_error;
|
|
#ifdef GW_WINDOWS_NATIVE_D3D11
|
|
original=(CreateDeviceFn)gw_native_d3d_export("D3D11CreateDeviceAndSwapChain",&d3d,'1',(void *)(uintptr_t)&gw_startup_D3D11CreateDeviceAndSwapChain);
|
|
if(!original)return (int32_t)0x80004005U;
|
|
#else
|
|
d3d=GetModuleHandleA("d3d11.dll");
|
|
if(!d3d)return (int32_t)0x80004005U;
|
|
original=(CreateDeviceFn)((uintptr_t)d3d+0x361d0);
|
|
#endif
|
|
gw_startup_status.d3d_module=(u64)(uintptr_t)d3d;
|
|
result=original(adapter,driver,software,flags,levels,level_count,sdk,desc,
|
|
swap,device,selected,context);
|
|
saved_error=GetLastError();
|
|
/* Failed or probe-only graphics calls leave the gate for a later renderer. */
|
|
if(result>=0 && device && *device)probe_on_real_creation();
|
|
SetLastError(saved_error);
|
|
return result;
|
|
}
|
|
typedef int32_t (__stdcall *CreateDeviceOnlyFn)(void *,u32,void *,u32,const u32 *,u32,u32,void **,u32 *,void **);
|
|
__declspec(dllexport) int32_t __stdcall gw_startup_D3D11CreateDevice(void *adapter,u32 driver,void *software,u32 flags,const u32 *levels,u32 level_count,u32 sdk,void **device,u32 *selected,void **context){
|
|
void *d3d=0;CreateDeviceOnlyFn original;int32_t result;u32 saved_error;
|
|
#ifdef GW_WINDOWS_NATIVE_D3D11
|
|
original=(CreateDeviceOnlyFn)gw_native_d3d_export("D3D11CreateDevice",&d3d,'1',(void *)(uintptr_t)&gw_startup_D3D11CreateDevice);
|
|
if(!original)return (int32_t)0x80004005U;
|
|
#else
|
|
d3d=GetModuleHandleA("d3d11.dll");
|
|
if(!d3d)return (int32_t)0x80004005U;
|
|
original=(CreateDeviceOnlyFn)((uintptr_t)d3d+0x36210);
|
|
#endif
|
|
gw_startup_status.d3d_module=(u64)(uintptr_t)d3d;
|
|
result=original(adapter,driver,software,flags,levels,level_count,sdk,
|
|
device,selected,context);
|
|
saved_error=GetLastError();
|
|
if(result>=0 && device && *device)probe_on_real_creation();
|
|
SetLastError(saved_error);
|
|
return result;
|
|
}
|
|
typedef int32_t (__stdcall *CreateDevice12Fn)(void *,u32,const void *,void **);
|
|
__declspec(dllexport) int32_t __stdcall gw_startup_D3D12CreateDevice(void *adapter,u32 minimum_feature_level,const void *riid,void **device){
|
|
void *d3d=0;CreateDevice12Fn original;int32_t result;u32 saved_error;
|
|
#ifdef GW_WINDOWS_NATIVE_D3D11
|
|
original=(CreateDevice12Fn)gw_native_d3d_export("D3D12CreateDevice",&d3d,'2',(void *)(uintptr_t)&gw_startup_D3D12CreateDevice);
|
|
if(!original)return (int32_t)0x80004005U;
|
|
#else
|
|
d3d=GetModuleHandleA("d3d12.dll");
|
|
if(!d3d)return (int32_t)0x80004005U;
|
|
/* The reviewed Wine EAT carrier retains this pinned original body.
|
|
* GetProcAddress would follow its forwarded export back into Bridge. */
|
|
original=(CreateDevice12Fn)((uintptr_t)d3d+0x11d80);
|
|
#endif
|
|
gw_startup_status.d3d_module=(u64)(uintptr_t)d3d;
|
|
result=original(adapter,minimum_feature_level,riid,device);
|
|
saved_error=GetLastError();
|
|
if(result>=0 && device && *device)probe_on_real_creation();
|
|
SetLastError(saved_error);
|
|
return result;
|
|
}
|