142 lines
6.0 KiB
C++
142 lines
6.0 KiB
C++
#pragma once
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <span>
|
|
|
|
// Temporary, opt-in Windows diagnostic. The only output is numeric structure
|
|
// from the two known failing Source query replies. No GUID, name or body bytes
|
|
// are copied to disk. Remove after the exact parser bounds have been fixed.
|
|
namespace gw_private_query_failure {
|
|
inline std::atomic_flag write_lock=ATOMIC_FLAG_INIT;
|
|
// Bounded fingerprint of a translator exception. This deliberately records no
|
|
// exception text, packet bytes, GUID, name, or account data.
|
|
inline uint64_t reason_fingerprint(const char* reason) noexcept {
|
|
uint64_t hash=14695981039346656037ULL;
|
|
if(!reason)return hash;
|
|
for(size_t i=0;i<160 && reason[i];++i){
|
|
hash^=uint8_t(reason[i]);hash*=1099511628211ULL;
|
|
}
|
|
return hash;
|
|
}
|
|
inline void lock_file() noexcept {
|
|
while(write_lock.test_and_set(std::memory_order_acquire))
|
|
write_lock.wait(true,std::memory_order_relaxed);
|
|
}
|
|
inline void unlock_file() noexcept {
|
|
write_lock.clear(std::memory_order_release);
|
|
write_lock.notify_one();
|
|
}
|
|
inline void record_reason(uint32_t opcode,size_t bytes,const char* reason) noexcept {
|
|
#ifdef _WIN32
|
|
const char* path=std::getenv("GW_PRIVATE_QUERY_FAILURE_NUMERIC");
|
|
if(!path || !*path || (opcode!=0x51 && opcode!=0x61 && opcode!=0xee))return;
|
|
static std::atomic<unsigned> count{0};
|
|
if(count.fetch_add(1,std::memory_order_relaxed)>=12)return;
|
|
lock_file();
|
|
if(auto* file=std::fopen(path,"ab")){
|
|
std::fprintf(file,"{\"kind\":\"source-translation-failure-v1\",\"opcode\":%u,\"bytes\":%zu,\"reason_hash\":%llu}\n",
|
|
opcode,bytes,static_cast<unsigned long long>(reason_fingerprint(reason)));
|
|
std::fclose(file);
|
|
}
|
|
unlock_file();
|
|
#else
|
|
(void)opcode;(void)bytes;(void)reason;
|
|
#endif
|
|
}
|
|
inline bool u32(std::span<const uint8_t> body, size_t& at, uint32_t& out) noexcept {
|
|
if(at > body.size() || body.size()-at < 4)return false;
|
|
out=uint32_t(body[at]) | (uint32_t(body[at+1])<<8) |
|
|
(uint32_t(body[at+2])<<16) | (uint32_t(body[at+3])<<24);
|
|
at+=4;return true;
|
|
}
|
|
inline bool string_length(std::span<const uint8_t> body,size_t& at,
|
|
size_t limit,uint32_t& length) noexcept {
|
|
const size_t start=at;
|
|
for(;at<body.size() && at-start<=limit;++at){
|
|
if(body[at]==0){length=uint32_t(at-start);++at;return true;}
|
|
}
|
|
return false;
|
|
}
|
|
inline void record(uint32_t opcode,std::span<const uint8_t> body) noexcept {
|
|
#ifdef _WIN32
|
|
const char* path=std::getenv("GW_PRIVATE_QUERY_FAILURE_NUMERIC");
|
|
if(!path || !*path || body.size()>256)return;
|
|
static std::atomic<unsigned> creature_count{0},name_count{0};
|
|
if(opcode==0x61){
|
|
if(body.size()<4)return;
|
|
size_t at=0;uint32_t raw=0;
|
|
if(!u32(body,at,raw) || (raw&0x7fffffffu)!=50042)return;
|
|
if(creature_count.fetch_add(1,std::memory_order_relaxed)>=2)return;
|
|
uint32_t names[4]{},title=0,flags=0,type=0,family=0,rank=0,
|
|
unknown0=0,unknown1=0,display=0,civilian=0,leader=0;
|
|
unsigned stage=1;
|
|
for(auto& length:names){
|
|
if(!string_length(body,at,255,length))goto creature_write;
|
|
++stage;
|
|
}
|
|
if(!string_length(body,at,255,title))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,flags))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,type))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,family))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,rank))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,unknown0))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,unknown1))goto creature_write;
|
|
++stage;
|
|
if(!u32(body,at,display))goto creature_write;
|
|
++stage;
|
|
if(at>=body.size())goto creature_write;
|
|
civilian=body[at++];
|
|
++stage;
|
|
if(at>=body.size())goto creature_write;
|
|
leader=body[at++];
|
|
++stage;
|
|
creature_write:
|
|
lock_file();
|
|
if(auto* file=std::fopen(path,"ab")){
|
|
std::fprintf(file,"{\"kind\":\"query-reject-numeric-v1\",\"opcode\":97,\"bytes\":%u,\"entry\":50042,\"stage\":%u,\"names\":[%u,%u,%u,%u],\"title\":%u,\"flags\":%u,\"type\":%u,\"family\":%u,\"rank\":%u,\"unknown0\":%u,\"unknown1\":%u,\"display\":%u,\"civilian\":%u,\"leader\":%u,\"tail\":%u}\n",
|
|
unsigned(body.size()),stage,names[0],names[1],names[2],names[3],
|
|
title,flags,type,family,rank,unknown0,unknown1,display,civilian,leader,
|
|
unsigned(body.size()-at));
|
|
std::fclose(file);
|
|
}
|
|
unlock_file();
|
|
}else if(opcode==0x51 && (body.size()==28 || body.size()==34)){
|
|
if(name_count.fetch_add(1,std::memory_order_relaxed)>=4)return;
|
|
size_t at=8;uint32_t name=0,realm=0,race=0,gender=0,cls=0;unsigned stage=0;
|
|
bool guid_zero=true,guid_high_nonzero=false;
|
|
for(size_t i=0;i<8;++i)if(body[i])guid_zero=false;
|
|
for(size_t i=4;i<8;++i)if(body[i])guid_high_nonzero=true;
|
|
if(!string_length(body,at,63,name))goto name_write;
|
|
++stage;
|
|
if(!string_length(body,at,255,realm))goto name_write;
|
|
++stage;
|
|
if(!u32(body,at,race))goto name_write;
|
|
++stage;
|
|
if(!u32(body,at,gender))goto name_write;
|
|
++stage;
|
|
if(!u32(body,at,cls))goto name_write;
|
|
++stage;
|
|
name_write:
|
|
lock_file();
|
|
if(auto* file=std::fopen(path,"ab")){
|
|
std::fprintf(file,"{\"kind\":\"query-reject-numeric-v1\",\"opcode\":81,\"bytes\":%u,\"stage\":%u,\"guid_zero\":%u,\"guid_high_nonzero\":%u,\"name_length\":%u,\"realm_length\":%u,\"race\":%u,\"gender\":%u,\"class\":%u,\"tail\":%u}\n",
|
|
unsigned(body.size()),stage,unsigned(guid_zero),unsigned(guid_high_nonzero),name,realm,race,gender,cls,unsigned(body.size()-at));
|
|
std::fclose(file);
|
|
}
|
|
unlock_file();
|
|
}
|
|
#else
|
|
(void)opcode;(void)body;
|
|
#endif
|
|
}
|
|
}
|