int MapFile(LogicalAddress addr, natural pos, natural nbytes, int permissions, int fd) { #ifdef WINDOWS #if 0 /* Lots of hair in here: mostly alignment issues, but also address space reservation */ HANDLE hFile, hFileMapping; LPVOID rc; DWORD desiredAccess; if (permissions == MEMPROTECT_RWX) { permissions |= PAGE_WRITECOPY; desiredAccess = FILE_MAP_READ|FILE_MAP_WRITE|FILE_MAP_COPY|FILE_MAP_EXECUTE; } else { desiredAccess = FILE_MAP_READ|FILE_MAP_COPY|FILE_MAP_EXECUTE; } hFile = _get_osfhandle(fd); hFileMapping = CreateFileMapping(hFile, NULL, permissions, (nbytes >> 32), (nbytes & 0xffffffff), NULL); if (!hFileMapping) { wperror("CreateFileMapping"); return false; } rc = MapViewOfFileEx(hFileMapping, desiredAccess, (pos >> 32), (pos & 0xffffffff), nbytes, addr); #else size_t count, total = 0; size_t opos; opos = LSEEK(fd, 0, SEEK_CUR); CommitMemory(addr, nbytes); LSEEK(fd, pos, SEEK_SET); while (total < nbytes) { count = read(fd, addr + total, nbytes - total); total += count; // fprintf(dbgout, "read " DECIMAL " bytes, for a total of " DECIMAL " out of " DECIMAL " so far\n", count, total, nbytes); if (!(count > 0)) return false; } LSEEK(fd, opos, SEEK_SET); return true; #endif #else return mmap(addr, nbytes, permissions, MAP_PRIVATE|MAP_FIXED, fd, pos) != MAP_FAILED; #endif }
bool AVMPI_commitMemory(void* address, size_t size) { bool success = false; MEMORY_BASIC_INFORMATION mbi; do { VirtualQuery(address, &mbi, sizeof(MEMORY_BASIC_INFORMATION)); size_t commitSize = size > mbi.RegionSize ? mbi.RegionSize : size; success = CommitMemory(address, commitSize); address = (char*)address + commitSize; size -= commitSize; } while(size > 0 && success); return success; }
void CRecompMemory::CheckRecompMem() { uint32_t Size = (uint32_t)((uint8_t *)m_RecompPos - (uint8_t *)m_RecompCode); if ((Size + 0x20000) < m_RecompSize) { return; } if (m_RecompSize == MaxCompileBufferSize) { g_Recompiler->ResetRecompCode(true); return; } void * MemAddr = CommitMemory(m_RecompCode + m_RecompSize, IncreaseCompileBufferSize, MEM_EXECUTE_READWRITE); if (MemAddr == NULL) { WriteTrace(TraceRecompiler, TraceError, "failed to increase buffer"); g_Notify->FatalError(MSG_MEM_ALLOC_ERROR); } m_RecompSize += IncreaseCompileBufferSize; }
bool CRecompMemory::AllocateMemory() { uint8_t * RecompCodeBase = (uint8_t *)AllocateAddressSpace(MaxCompileBufferSize + 4); if (RecompCodeBase == NULL) { WriteTrace(TraceRecompiler, TraceError, "failed to allocate RecompCodeBase"); g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR); return false; } m_RecompCode = (uint8_t *)CommitMemory(RecompCodeBase, InitialCompileBufferSize, MEM_EXECUTE_READWRITE); if (m_RecompCode == NULL) { WriteTrace(TraceRecompiler, TraceError, "failed to commit initial buffer"); FreeAddressSpace(RecompCodeBase,MaxCompileBufferSize + 4); g_Notify->DisplayError(MSG_MEM_ALLOC_ERROR); return false; } m_RecompSize = InitialCompileBufferSize; m_RecompPos = m_RecompCode; memset(m_RecompCode, 0, InitialCompileBufferSize); return true; }
void CRemoteCode::PushAllParameters( bool right_to_left ) { if( m_CurrentInvokeInfo.params.size() == 0 ) return; DebugShout( "Parameters for function [%i]", m_CurrentInvokeInfo.params.size() ); vector<parameter_info_t> currentParams = m_CurrentInvokeInfo.params; vector<parameter_info_t> pushOrder; if( right_to_left == false ) { //left-to-right for( int i = 0; i < (int)m_CurrentInvokeInfo.params.size(); i++ ) { pushOrder.push_back( m_CurrentInvokeInfo.params.at( i ) ); DebugShout( "Parameter found [%i][%i]", i, m_CurrentInvokeInfo.params.at( i ).ptype ); } } else { //right-to-left if( m_CurrentInvokeInfo.params.size() == 1 ) { pushOrder.push_back( m_CurrentInvokeInfo.params.at( 0 ) ); } else { int iBegin = (int)m_CurrentInvokeInfo.params.size() - 1; while( iBegin != -1 ) { pushOrder.push_back( m_CurrentInvokeInfo.params.at( iBegin ) ); DebugShout( "Parameter found [%i][%s]", iBegin, ParameterTypeToString( m_CurrentInvokeInfo.params.at( iBegin ).ptype ).c_str() ); iBegin--; } } } for( int p = 0; p < (int)pushOrder.size(); p++ ) { parameter_info_t *paraminfo = &pushOrder[p]; if( paraminfo == NULL ) continue; DebugShout( "Function Iter [%i]", p ); DebugShout( "Function Parameter [%s]", ParameterTypeToString( paraminfo->ptype ).c_str() ); if( paraminfo->pparam == NULL ) { AddByteToBuffer( 0x68 ); AddLongToBuffer( 0 ); continue; } switch( paraminfo->ptype ) { case PARAMETER_TYPE_SHORT: case PARAMETER_TYPE_POINTER: case PARAMETER_TYPE_INT: case PARAMETER_TYPE_FLOAT: { if( paraminfo->pparam ) { unsigned long ulParam = *(unsigned long *)paraminfo->pparam; AddByteToBuffer( 0x68 ); AddLongToBuffer( ulParam ); } else { //if it is PARAMETER_TYPE_POINTER with a NULL pointer //we don't want to crash AddByteToBuffer( 0x68 ); AddLongToBuffer( NULL ); } break; } case PARAMETER_TYPE_BYTE: { unsigned char ucParam = *(unsigned char *)paraminfo->pparam; AddByteToBuffer( 0x6A ); AddByteToBuffer( ucParam ); break; } case PARAMETER_TYPE_BOOL: { bool bParam = *(bool *)paraminfo->pparam; unsigned char ucParam = (bParam) ? 1 : 0; AddByteToBuffer( 0x6A ); AddByteToBuffer( ucParam ); break; } case PARAMETER_TYPE_STRING: { char *szParameter = (char *)paraminfo->pparam; void *AllocatedString = CommitMemory( szParameter, strlen( szParameter ) + 1 ); if( AllocatedString == NULL ) { DebugShout( "NULL Allocated ANSI string pointer...." ); continue; //bad beans } DebugShout( "Allocated string pointer at [0x%X]", AllocatedString ); AddByteToBuffer( 0x68 ); AddLongToBuffer( ( unsigned long ) AllocatedString ); break; } case PARAMETER_TYPE_WSTRING: { wchar_t *szParameter = (wchar_t *)paraminfo->pparam; void *AllocatedString = CommitMemory( szParameter, (wcslen( szParameter ) * 2) + 1 ); if( AllocatedString == NULL ) { DebugShout( "NULL Allocated UNICODE string pointer...." ); continue; //bad beans } AddByteToBuffer( 0x68 ); AddLongToBuffer((unsigned long)AllocatedString); break; } default: { DebugShout( "Unable to locate parameter type %i", paraminfo->ptype ); break; } } } }