71 lines
3.8 KiB
C++
71 lines
3.8 KiB
C++
#if defined(GW_SOURCE_ONLY_PROFILE) && (GW_SOURCE_ONLY_PROFILE == 2 || GW_SOURCE_ONLY_PROFILE == 5)
|
|
/* MinGW header sets can omit the Windows CAS128 wrapper. Keep
|
|
* the same full-barrier atomic operation and comparand-result contract. */
|
|
#if defined(__MINGW32__)
|
|
__extension__ typedef unsigned __int128 gw_club_uint128;
|
|
__attribute__((target("cx16")))
|
|
static unsigned char gw_club_compare_exchange128(volatile LONG64* destination,
|
|
LONG64 high, LONG64 low, LONG64* comparand) {
|
|
const gw_club_uint128 expected = ((gw_club_uint128)(unsigned long long)comparand[1] << 64)
|
|
| (unsigned long long)comparand[0];
|
|
const gw_club_uint128 desired = ((gw_club_uint128)(unsigned long long)high << 64)
|
|
| (unsigned long long)low;
|
|
const gw_club_uint128 observed = __sync_val_compare_and_swap(
|
|
(volatile gw_club_uint128*)destination, expected, desired);
|
|
comparand[0] = (LONG64)(unsigned long long)observed;
|
|
comparand[1] = (LONG64)(unsigned long long)(observed >> 64);
|
|
return observed == expected;
|
|
}
|
|
#define GW_CLUB_COMPARE_EXCHANGE128 gw_club_compare_exchange128
|
|
#else
|
|
#define GW_CLUB_COMPARE_EXCHANGE128 InterlockedCompareExchange128
|
|
#endif
|
|
|
|
/* Original70334 own-account ObjectGuid slot: startup2c1562e reads low;
|
|
* type consumer2ba6675 requires high>>58 ==30. Source-only profiles2/5 supply
|
|
* a private name-derived key after real M2/world auth. No code patch, original
|
|
* function call, permission change, tag, persona or auth-outcome fabrication. */
|
|
#include "../include/gateway/source_account_key.h"
|
|
static uint32_t club_source_key,club_source_generation,club_source_published;
|
|
static volatile LONG64* club_source_slot(void){
|
|
MEMORY_BASIC_INFORMATION m={0};uintptr_t a=UINT64_C(0x14574b3c0),end;DWORD p;
|
|
if((uintptr_t)GetModuleHandleA(NULL)!=UINT64_C(0x140000000) ||
|
|
VirtualQuery((void*)a,&m,sizeof(m))!=sizeof(m) || m.State!=MEM_COMMIT ||
|
|
(m.Protect&(PAGE_GUARD|PAGE_NOACCESS)))return NULL;
|
|
end=(uintptr_t)m.BaseAddress+m.RegionSize;p=m.Protect&0xff;
|
|
if(end<a || end-a<16 || (p!=PAGE_READWRITE && p!=PAGE_WRITECOPY &&
|
|
p!=PAGE_EXECUTE_READWRITE && p!=PAGE_EXECUTE_WRITECOPY))return NULL;
|
|
return (volatile LONG64*)a;
|
|
}
|
|
static void club_source_prepare(const char* name,uint32_t bytes,uint32_t generation){
|
|
club_source_key=gw_source_account_key(name,bytes);club_source_generation=generation;
|
|
}
|
|
/* Keep the Windows CAS128 intrinsic in hardware; the freestanding PE has
|
|
* no libatomic runtime. Owned x64 binding uses CMPXCHG16B. */
|
|
__attribute__((target("cx16"))) static int club_source_publish(void){
|
|
volatile LONG64* slot;LONG64 expected[2] __attribute__((aligned(16)))={0,0};
|
|
if(!club_source_key || !club_source_generation || club_source_generation!=credentials_generation ||
|
|
gw_wire_auth_credentials_data.consumed_generation!=club_source_generation)return -1;
|
|
slot=club_source_slot();if(!slot)return -2;
|
|
if(GW_CLUB_COMPARE_EXCHANGE128(slot,(LONG64)GW_SOURCE_INTERNAL_ACCOUNT_HIGH,
|
|
(LONG64)club_source_key,expected)){
|
|
club_source_published=club_source_generation;return 0;
|
|
}
|
|
if(club_source_published==club_source_generation && expected[0]==club_source_key &&
|
|
(uint64_t)expected[1]==GW_SOURCE_INTERNAL_ACCOUNT_HIGH)return 0;
|
|
return -3; /* Never replace an unrelated native identity. */
|
|
}
|
|
__attribute__((target("cx16"))) static void club_source_clear(void){
|
|
volatile LONG64* slot=club_source_slot();
|
|
LONG64 expected[2] __attribute__((aligned(16)))={(LONG64)club_source_key,(LONG64)GW_SOURCE_INTERNAL_ACCOUNT_HIGH};
|
|
if(slot && club_source_published)GW_CLUB_COMPARE_EXCHANGE128(slot,0,0,expected);
|
|
club_source_key=club_source_generation=club_source_published=0;
|
|
}
|
|
|
|
#else
|
|
static int club_source_publish(void){return 0;}
|
|
static void club_source_clear(void){}
|
|
#endif
|
|
|
|
#undef GW_CLUB_COMPARE_EXCHANGE128
|