metamod/engineinfo.cpp
changeset 250 1f5fb7d1d9b3
child 267 b75724ad6b58
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/metamod/engineinfo.cpp	Mon Apr 17 17:04:06 2006 +0000
     1.3 @@ -0,0 +1,392 @@
     1.4 +
     1.5 +// vi: set ts=4 sw=4 :
     1.6 +// vim: set tw=75 :
     1.7 +
     1.8 +// engineinfo.cpp - get info about HL engine, like file used
     1.9 +//                  and test segment range
    1.10 +//
    1.11 +
    1.12 +
    1.13 +#ifdef _WIN32
    1.14 +   // Don't include winspool.h; clashes with SERVER_EXECUTE from engine
    1.15 +#  define _WINSPOOL_H	
    1.16 +#  include <windows.h>
    1.17 +#  include <winnt.h>     // Header structures
    1.18 +#else 
    1.19 +#  ifndef _GNU_SOURCE
    1.20 +#    define _GNU_SOURCE
    1.21 +#  endif
    1.22 +#  include <dlfcn.h>			// dladdr()
    1.23 +#  include <link.h>				// ElfW(Phdr/Ehdr) macros.
    1.24 +                  				// _DYNAMIC, r_debug, link_map, etc.
    1.25 +#endif /* _WIN32 */ 
    1.26 +
    1.27 +#include <string.h>				// strlen(), strrchr(), strcmp()
    1.28 +#include <stdio.h>				// printf()
    1.29 +
    1.30 +#include "engineinfo.h"			// me
    1.31 +#include "log_meta.h"			// META_DEV()
    1.32 +
    1.33 +
    1.34 +// Current mask for checking engine function addresses 
    1.35 +// in VAC protected engine dlls on Win32. This is gonna fail
    1.36 +// on Win64.
    1.37 +const unsigned long c_VacDllEngineFuncsRangeMask  = 0xFFF00000;
    1.38 +const unsigned long c_VacDllEngineFuncsRangeMark  = 0x01D00000;
    1.39 +void* const c_VacDllEngineFuncsRangeStart = (void*)0x01D00000;
    1.40 +void* const c_VacDllEngineFuncsRangeEnd   = (void*)0x01E00000;
    1.41 +
    1.42 +
    1.43 +bool EngineInfo::check_for_engine_module( const char* _pName )
    1.44 +{
    1.45 +	const char* pC;
    1.46 +	size_t size;
    1.47 +
    1.48 +	if ( NULL == _pName ) return false;
    1.49 +
    1.50 +#ifdef _WIN32
    1.51 +
    1.52 +	// The engine module is either sw.dll or hw.dll or swds.dll
    1.53 +	pC = strrchr( _pName, '.' );
    1.54 +
    1.55 +	pC -= 2;
    1.56 +	if ( 0 != strcmp(pC, "sw.dll") && 0 != strcmp(pC, "hw.dll") ) {
    1.57 +		pC -= 2;
    1.58 +		if ( 0 != strcmp(pC, "swds.dll") ) return false;
    1.59 +	}
    1.60 +
    1.61 +	HMODULE hModule = GetModuleHandle( pC );
    1.62 +	if ( NULL == hModule ) {
    1.63 +		return false;
    1.64 +	}
    1.65 +
    1.66 +	// Ok, we found the string sw(ds).dll, thus we deduct that this is the
    1.67 +	// name of the engine's shared object. We copy the string for future 
    1.68 +	// reference and return successfully.
    1.69 +	size = 0;
    1.70 +	while ( *pC != '.' && size < c_EngineInfo__typeLen-1 ) {
    1.71 +		m_type[size++] = *pC++;
    1.72 +	}
    1.73 +	m_type[size] = '\0';
    1.74 +
    1.75 +#else /* _WIN32 */
    1.76 +
    1.77 +	const char* pType;
    1.78 +
    1.79 +
    1.80 +	size = strlen( _pName );
    1.81 +	if ( size < 11 ) {
    1.82 +		// Forget it, this string is too short to even be 'engine_.so', so
    1.83 +		// it can't the name of the engine shared library.
    1.84 +		return false;
    1.85 +	}
    1.86 +
    1.87 +	// Start parsing at the end. Since we know that the string is at least
    1.88 +	// 11 characters long we can safely do our parsing without being afraid
    1.89 +	// of leaving the string.
    1.90 +	pC = _pName + size -1;
    1.91 +
    1.92 +	// First we see if the string ends in '.so'
    1.93 +	if ( *pC-- != 'o' || *pC-- != 's' || *pC-- != '.' ) return false;
    1.94 +
    1.95 +	// Now find the '_' which would be the seperator between 'engine' and
    1.96 +	// the architecture.
    1.97 +	while ( *pC != '_' && pC != _pName ) --pC;
    1.98 +	if ( pC == _pName ) return false;
    1.99 +
   1.100 +	// We are at the '_', thus the architecture identifier must start at
   1.101 +	// the next character.
   1.102 +	pType = pC +1;
   1.103 +
   1.104 +	// Now we walk further back and check if we find the string 'engine'
   1.105 +	// backwards.
   1.106 +	--pC;
   1.107 +	if ( *pC-- != 'e' || *pC-- != 'n' || *pC-- != 'i' ||
   1.108 +		 *pC-- != 'g' || *pC-- != 'n' || *pC != 'e' ) {
   1.109 +		return false;
   1.110 +	}
   1.111 +
   1.112 +	// Ok, we found the string engine_*.so, thus we deduct that this is the
   1.113 +	// name of the engine's shared object. We copy the architecture string
   1.114 +	// for future reference and return successfully.
   1.115 +	size = 0;
   1.116 +	while ( *pType != '.' && size < c_EngineInfo__typeLen-1 ) {
   1.117 +		m_type[size++] = *pType++;
   1.118 +	}
   1.119 +	m_type[size] = '\0';
   1.120 +
   1.121 +
   1.122 +#endif /* _WIN32 */
   1.123 +
   1.124 +
   1.125 +	return true;
   1.126 +}
   1.127 +
   1.128 +
   1.129 +#ifdef _WIN32
   1.130 +
   1.131 +int EngineInfo::nthdr_module_name( void )
   1.132 +{
   1.133 +	PIMAGE_DOS_HEADER pDosHeader;
   1.134 +	PIMAGE_NT_HEADERS pNTHeader;
   1.135 +	unsigned char* pBaseAddr;
   1.136 +	const char* pName = "sw.dll";
   1.137 +
   1.138 +	if ( ! check_for_engine_module(pName) ) {
   1.139 +		pName = "hw.dll";
   1.140 +		if ( ! check_for_engine_module(pName) ) {
   1.141 +			pName = "swds.dll";
   1.142 +			if ( ! check_for_engine_module(pName) ) {
   1.143 +				return MODULE_NAME_NOTFOUND;
   1.144 +			}
   1.145 +		}
   1.146 +	}
   1.147 +	
   1.148 +	// Get the module handle for the engine dll we found.
   1.149 +	// This is also the modules base address.
   1.150 +	HMODULE hModule = GetModuleHandle( pName );
   1.151 +
   1.152 +	pBaseAddr = (unsigned char*)hModule;
   1.153 +
   1.154 +
   1.155 +	// Check if we find a DOS header
   1.156 +	pDosHeader = (PIMAGE_DOS_HEADER)hModule;
   1.157 +    if ( pDosHeader->e_magic != IMAGE_DOS_SIGNATURE ) {
   1.158 +		return INVALID_DOS_SIGN;
   1.159 +	}
   1.160 +
   1.161 +	// Check if we find a PE header
   1.162 +	pNTHeader = (PIMAGE_NT_HEADERS)(pBaseAddr + pDosHeader->e_lfanew);
   1.163 +	if ( pNTHeader->Signature != IMAGE_NT_SIGNATURE ) {
   1.164 +		return INVALID_NT_SIGN;
   1.165 +	}
   1.166 +
   1.167 +	set_code_range( pBaseAddr, pNTHeader );
   1.168 +	return 0;
   1.169 +}
   1.170 +
   1.171 +
   1.172 +int EngineInfo::vac_pe_approx( enginefuncs_t* _pFuncs )
   1.173 +{
   1.174 +
   1.175 +    if ( NULL == _pFuncs ) return INVALID_ARG;
   1.176 +	
   1.177 +	// There is really no good and easy way to do this. Right now what 
   1.178 +	// we do is assume that Steam listenservers will normally be 
   1.179 +	// up-to-date and hence have all function pointers set correctly.
   1.180 +	// So we just check for some anomality and then set some approximated
   1.181 +	// values.
   1.182 +
   1.183 +	// The known addresses at the time of writing all start with 0x01D and
   1.184 +	// this lie in the range between 0x01D00000 and 0x01E00000. What we do
   1.185 +	// is check all functions pointers that are known to be good to start
   1.186 +	// with 0x01D. If that is the case then we know that the loading address
   1.187 +	// of the engine functions hasn't changed and we assume the above stated
   1.188 +	// range to be the range of valid engine function addresses.
   1.189 +	// If we find functions outside this range we can't determine a valid
   1.190 +	// range and have to just give up.
   1.191 +
   1.192 +	unsigned long* pengfuncs = (unsigned long*)_pFuncs;
   1.193 +	unsigned int i, invals = 0;
   1.194 +	for ( i = 0, pengfuncs += i; i < 140; i++, pengfuncs++ ) {
   1.195 +		if ( ((*pengfuncs) & c_VacDllEngineFuncsRangeMask) != c_VacDllEngineFuncsRangeMark ) {
   1.196 +			invals++;
   1.197 +			break;
   1.198 +		}
   1.199 +	}
   1.200 +
   1.201 +	if ( invals > 0 ) {
   1.202 +		META_DEV( "Found %d engine functions out of range 0x%08lx. "
   1.203 +			      "Unable to determine valid engine code address range.", 
   1.204 +			      invals, c_VacDllEngineFuncsRangeMark );
   1.205 +
   1.206 +		strncpy( m_type, "vacdll+?", c_EngineInfo__typeLen );
   1.207 +        m_state = STATE_INVALID;
   1.208 +		return STATE_INVALID;
   1.209 +	}
   1.210 +
   1.211 +	m_codeStart = c_VacDllEngineFuncsRangeStart;
   1.212 +	m_codeEnd = c_VacDllEngineFuncsRangeEnd;
   1.213 +
   1.214 +	strncpy( m_type, "vacdll", c_EngineInfo__typeLen );
   1.215 +
   1.216 +    m_state = STATE_VALID;
   1.217 +
   1.218 +	return 0;
   1.219 +}
   1.220 +
   1.221 +
   1.222 +void EngineInfo::set_code_range( unsigned char* _pBase, PIMAGE_NT_HEADERS _pNThdr )
   1.223 +{
   1.224 +	m_codeStart = _pBase + _pNThdr->OptionalHeader.BaseOfCode;
   1.225 +	m_codeEnd = _pBase + _pNThdr->OptionalHeader.BaseOfCode + _pNThdr->OptionalHeader.SizeOfCode;
   1.226 +
   1.227 +    m_state = STATE_VALID;
   1.228 +
   1.229 +}
   1.230 +
   1.231 +
   1.232 +#else /* _WIN32 */
   1.233 +
   1.234 +
   1.235 +int EngineInfo::phdr_elfhdr( void* _pElfHdr )
   1.236 +{
   1.237 +	ElfW(Ehdr)* pEhdr = (ElfW(Ehdr)*)_pElfHdr;
   1.238 +	ElfW(Phdr)* pPhdr;
   1.239 +
   1.240 +    if ( NULL == _pElfHdr ) return INVALID_ARG;
   1.241 +	
   1.242 +    if ( pEhdr->e_ident[0] == 0x7f
   1.243 +	  && pEhdr->e_ident[1] == 'E'
   1.244 +	  && pEhdr->e_ident[2] == 'L'
   1.245 +	  && pEhdr->e_ident[3] == 'F' ) {
   1.246 +
   1.247 +		// Looking good, we found an ELF signature.
   1.248 +		// Let's see if the rest of the data at this address would fit an ELH header, too.
   1.249 +		if (  (pEhdr->e_ident[EI_CLASS] == ELFCLASS32 //ELFW(CLASS)
   1.250 +			|| pEhdr->e_ident[EI_CLASS] == ELFCLASS64)
   1.251 +
   1.252 +		   && (pEhdr->e_ident[EI_DATA] == ELFDATA2LSB
   1.253 +            || pEhdr->e_ident[EI_DATA] == ELFDATA2MSB)
   1.254 +
   1.255 +           && pEhdr->e_type == ET_DYN ) {
   1.256 +
   1.257 +			// Ok, we believe that this is a shared object's ELF header.
   1.258 +			// Let's find the program header for the segment that includes
   1.259 +			// the text section and return it.
   1.260 +        
   1.261 +			pPhdr = (ElfW(Phdr) *) ((char *) pEhdr + pEhdr->e_phoff);
   1.262 +			for ( int i = 0; i < pEhdr->e_phnum; i++, pPhdr++ ) {
   1.263 +
   1.264 +				if ( PT_LOAD == pPhdr->p_type 
   1.265 +					&& (pPhdr->p_flags & PF_R) 
   1.266 +					&& (pPhdr->p_flags & PF_X) ) {
   1.267 +
   1.268 +					// The text section gets loaded and has read and
   1.269 +					// execute flags set. So this must be it.
   1.270 +					set_code_range( pEhdr, pPhdr );
   1.271 +					return 0;
   1.272 +				}
   1.273 +			}
   1.274 +        }
   1.275 +    }
   1.276 +
   1.277 +	return HEADER_NOTFOUND;
   1.278 +}
   1.279 +
   1.280 +
   1.281 +
   1.282 +
   1.283 +int EngineInfo::phdr_dladdr( void* _pMem )
   1.284 +{
   1.285 +	Dl_info info;
   1.286 +
   1.287 +	if ( 0 != dladdr(_pMem, &info) ) {
   1.288 +		// Check if this is the engine module
   1.289 +		if ( check_for_engine_module(info.dli_fname) ) {
   1.290 +		   return phdr_elfhdr( info.dli_fbase );
   1.291 +		}
   1.292 +	}
   1.293 +
   1.294 +	return NOTFOUND;
   1.295 +}
   1.296 +
   1.297 +
   1.298 +int EngineInfo::phdr_r_debug( void )
   1.299 +{
   1.300 +    ElfW(Dyn)* pDyn; 
   1.301 +    struct r_debug* pr_debug;
   1.302 +    struct link_map* pMap;
   1.303 +
   1.304 +
   1.305 +	// Search if we have a DT_DEBUG symbol in our DYNAMIC segment, which
   1.306 +	// ought to be set to the r_debug structure's address.
   1.307 +    for (pDyn = _DYNAMIC; pDyn->d_tag != DT_NULL; ++pDyn) {
   1.308 +        if (pDyn->d_tag == DT_DEBUG) {
   1.309 +            pr_debug = (struct r_debug *) pDyn->d_un.d_ptr;
   1.310 +            break;
   1.311 +        }
   1.312 +    }
   1.313 +
   1.314 +    if ( DT_NULL == pDyn->d_tag ) {
   1.315 +    }
   1.316 +	else if ( NULL == pr_debug ) {
   1.317 +	}
   1.318 +    else {
   1.319 +		pMap = pr_debug->r_map;
   1.320 +
   1.321 +		// Walk to the start of the list
   1.322 +        while ( pMap->l_prev != NULL ) pMap = pMap->l_prev;
   1.323 +        do {
   1.324 +			if ( check_for_engine_module(pMap->l_name) ) {
   1.325 +				return phdr_elfhdr( (void*)pMap->l_addr );
   1.326 +			}
   1.327 +
   1.328 +            pMap = pMap->l_next;
   1.329 +        } while ( NULL != pMap );
   1.330 +        
   1.331 +    }
   1.332 +
   1.333 +	return NOTFOUND;
   1.334 +}
   1.335 +
   1.336 +void EngineInfo::set_code_range( void* _pBase, ElfW(Phdr)* _pPhdr )
   1.337 +{
   1.338 +	unsigned char* pBase = (unsigned char*)_pBase;
   1.339 +	m_codeStart = pBase + _pPhdr->p_vaddr;
   1.340 +	m_codeEnd   = pBase + _pPhdr->p_vaddr + _pPhdr->p_memsz;
   1.341 +
   1.342 +    m_state = STATE_VALID;
   1.343 +}
   1.344 +
   1.345 +
   1.346 +#endif /* _WIN32 */
   1.347 +
   1.348 +
   1.349 +int EngineInfo::initialise( enginefuncs_t* _pFuncs )
   1.350 +{
   1.351 +	int ret = 0;
   1.352 +
   1.353 +	// Have to do this only once.
   1.354 +	if ( NULL != m_codeStart ) {
   1.355 +		return 0;
   1.356 +	}
   1.357 +
   1.358 +#ifdef _WIN32
   1.359 +
   1.360 +    ret = nthdr_module_name();
   1.361 +	if ( MODULE_NAME_NOTFOUND == ret && (! _pFuncs->pfnIsDedicatedServer()) ) {
   1.362 +		// We could not find the engine dll by name and we are running on
   1.363 +		// a listen server. This usually means that that we are dealing with
   1.364 +		// a VAC protected engine dll. No other way than approximating the 
   1.365 +		// range of valid engine function pointers in this case.
   1.366 +		ret = vac_pe_approx( _pFuncs );
   1.367 +	}
   1.368 +
   1.369 +#else /* _WIN32 */
   1.370 +
   1.371 +	// If we have no reference pointer to start from we can only try to use
   1.372 +	// the r_debug symbol.
   1.373 +	if ( NULL == _pFuncs ) {
   1.374 +		ret = phdr_r_debug();
   1.375 +	}
   1.376 +
   1.377 +	// If we have a refererence pointer we try to use it first.
   1.378 +	if ( 0 != phdr_dladdr(_pFuncs) ) {
   1.379 +		ret = phdr_r_debug();
   1.380 +	}
   1.381 +
   1.382 +#endif /* _WIN32 */
   1.383 +
   1.384 +	if ( 0 != ret ) {
   1.385 +		META_DEV( "Unable to determine engine code address range!" );
   1.386 +	}
   1.387 +	else {
   1.388 +		META_DEV( "Set engine code range: start address = %p, end address = %p", 
   1.389 +			 m_codeStart, m_codeEnd );
   1.390 +	}
   1.391 +
   1.392 +    return 0;
   1.393 +}
   1.394 +
   1.395 +