Пример #1
0
void VDMInject32(HANDLE hProcess, unsigned int hwnd, const GUID* from)
{
	TCHAR szPath[MAX_PATH] = {0};
	GetPreferredModuleName(g_hModule, true, szPath);
	
	// construct arguments
	auto shared = VirtualAllocEx(hProcess, nullptr, sizeof(VDM::Shared32), MEM_COMMIT, PAGE_READWRITE);
	WriteProcessMemory(hProcess, RVA(shared, AddressOf(VDM::Shared32, hwnd)), &hwnd, sizeof(HWND), nullptr);
	WriteProcessMemory(hProcess, RVA(shared, AddressOf(VDM::Shared32, guid)), from, sizeof(*from), nullptr);

	MODULEINFO kernel32;
	GetRemoteModuleInfo(hProcess, _T("kernel32.dll"), &kernel32, true);
	auto fnLoadLibrary = GetRemoteProcAddress(hProcess, (HMODULE)kernel32.lpBaseOfDll, "LoadLibraryW", true);
	auto remote = VirtualAllocEx(hProcess, nullptr, _countof(szPath), MEM_COMMIT, PAGE_READWRITE);
	WriteProcessMemory(hProcess, remote, szPath, _countof(szPath), nullptr);
	CallOnRemoteThread(hProcess, fnLoadLibrary, remote);

	MODULEINFO vdmhelper;
	GetRemoteModuleInfo(hProcess, _T("VDMHelper32.dll"), &vdmhelper, true);
	auto fnVDMProcess = GetRemoteProcAddress(hProcess, (HMODULE)vdmhelper.lpBaseOfDll, "VDMProcess", true);
	CallOnRemoteThread(hProcess, fnVDMProcess, shared);

	auto fnFreeLibrary = GetRemoteProcAddress(hProcess, (HMODULE)kernel32.lpBaseOfDll, "FreeLibrary", true);
	CallOnRemoteThread(hProcess, fnFreeLibrary, vdmhelper.lpBaseOfDll);

	VirtualFreeEx(hProcess, remote, 0, MEM_RELEASE);
	VirtualFreeEx(hProcess, shared, 0, MEM_RELEASE);
}
Пример #2
0
void asCString::Allocate(size_t len, bool keepData)
{
	// If we stored the capacity of the dynamically allocated buffer it would be possible
	// to save some memory allocations if a string decreases in size then increases again,
	// but this would require extra bytes in the string object itself, or a decrease of 
	// the static buffer, which in turn would mean extra memory is needed. I've tested each
	// of these options, and it turned out that the current choice is what best balanced
	// the number of allocations against the size of the allocations.

	if( len > 11 && len > length )
	{
		// Allocate a new dynamic buffer if the new one is larger than the old
		char *buf = asNEWARRAY(char,len+1);

		if( keepData )
		{
			int l = (int)len < (int)length ? (int)len : (int)length;
			memcpy(buf, AddressOf(), l);
		}

		if( length > 11 )
		{
			asDELETEARRAY(dynamic);
		}

		dynamic = buf;
	}
Пример #3
0
	bool Encode(uchar symbol, Output& result)
	{
		if(symbol == ' ')
		{
			LoadFromAddress(AddressOf(Space), result);
			return true;
		}

		if(!ConvertSymbol(symbol, symbol))
		{
			return false;
		}

		LoadFromAddress(AddressOf(MorseTable[symbol - '0']), result);
	
		return true;
	}
void asCString::Allocate(size_t len, bool keepData)
{
    if( len > 11 )
    {
        char *buf = asNEWARRAY(char,len+1);

        if( keepData )
        {
            int l = (int)len < (int)length ? (int)len : (int)length;
            memcpy(buf, AddressOf(), l);
        }

        if( length > 11 )
        {
            asDELETEARRAY(dynamic);
        }

        dynamic = buf;
    }
Пример #5
0
void CCodeSection::GenerateSectionLinkage()
{
    CCodeSection * TargetSection[] = { m_ContinueSection, m_JumpSection };
    CJumpInfo * JumpInfo[] = { &m_Cont, &m_Jump };
    int i;

    for (i = 0; i < 2; i++)
    {
        if (JumpInfo[i]->LinkLocation == NULL &&
            JumpInfo[i]->FallThrough == false)
        {
            JumpInfo[i]->TargetPC = (uint32_t)-1;
        }
    }

    if ((m_RecompilerOps->GetCurrentPC() & 0xFFC) == 0xFFC)
    {
        g_Notify->BreakPoint(__FILE__, __LINE__);
#ifdef legacycode
        //Handle Fall througth
        uint8_t * Jump = NULL;
        for (i = 0; i < 2; i ++)
        {
            if (!JumpInfo[i]->FallThrough) { continue; }
            JumpInfo[i]->FallThrough = false;
            if (JumpInfo[i]->LinkLocation != NULL)
            {
                SetJump32(JumpInfo[i]->LinkLocation,(uint32_t *)*g_RecompPos);
                JumpInfo[i]->LinkLocation = NULL;
                if (JumpInfo[i]->LinkLocation2 != NULL)
                {
                    SetJump32(JumpInfo[i]->LinkLocation2,(uint32_t *)*g_RecompPos);
                    JumpInfo[i]->LinkLocation2 = NULL;
                }
            }
            PushImm32(stdstr_f("0x%08X",JumpInfo[i]->TargetPC).c_str(),JumpInfo[i]->TargetPC);
            if (JumpInfo[(i + 1) & 1]->LinkLocation == NULL) { break; }
            JmpLabel8("FinishBlock",0);
            Jump = *g_RecompPos - 1;
        }
        for (i = 0; i < 2; i ++)
        {
            if (JumpInfo[i]->LinkLocation == NULL) { continue; }
            JumpInfo[i]->FallThrough = false;
            if (JumpInfo[i]->LinkLocation != NULL)
            {
                SetJump32(JumpInfo[i]->LinkLocation,(uint32_t *)*g_RecompPos);
                JumpInfo[i]->LinkLocation = NULL;
                if (JumpInfo[i]->LinkLocation2 != NULL)
                {
                    SetJump32(JumpInfo[i]->LinkLocation2,(uint32_t *)*g_RecompPos);
                    JumpInfo[i]->LinkLocation2 = NULL;
                }
            }
            PushImm32(stdstr_f("0x%08X",JumpInfo[i]->TargetPC).c_str(),JumpInfo[i]->TargetPC);
            if (JumpInfo[(i + 1) & 1]->LinkLocation == NULL) { break; }
            JmpLabel8("FinishBlock",0);
            Jump = *g_RecompPos - 1;
        }
        if (Jump != NULL)
        {
            CPU_Message("      $FinishBlock:");
            SetJump8(Jump,*g_RecompPos);
        }
        //MoveConstToVariable(m_RecompilerOps->GetCurrentPC() + 4,_PROGRAM_COUNTER,"PROGRAM_COUNTER");
        m_RegWorkingSet.WriteBackRegisters();
        m_RecompilerOps->UpdateCounters(m_RegWorkingSet,false,true);
        //		WriteBackRegisters(Section);
        //		if (g_SyncSystem) {
        MoveConstToX86reg((uint32_t)g_BaseSystem,x86_ECX);
        Call_Direct(AddressOf(&CN64System::SyncSystem), "CN64System::SyncSystem");
        //}
        //	MoveConstToVariable(DELAY_SLOT,&m_NextInstruction,"m_NextInstruction");
        PushImm32(stdstr_f("0x%08X",m_RecompilerOps->GetCurrentPC() + 4).c_str(),m_RecompilerOps->GetCurrentPC() + 4);

        // check if there is an existing section

        MoveConstToX86reg((uint32_t)g_Recompiler,x86_ECX);
        Call_Direct(AddressOf(&CRecompiler::CompileDelaySlot), "CRecompiler::CompileDelaySlot");
        JmpDirectReg(x86_EAX);
        ExitCodeBlock();
        return;
#endif
    }

    // Handle Perm Loop
    if (m_RecompilerOps->GetCurrentPC() == m_Jump.TargetPC && (m_Cont.FallThrough == false))
    {
        if (!DelaySlotEffectsJump(m_RecompilerOps->GetCurrentPC()))
        {
            m_RecompilerOps->CompileInPermLoop(m_Jump.RegSet, m_RecompilerOps->GetCurrentPC());
        }
    }
    if (TargetSection[0] != TargetSection[1] || TargetSection[0] == NULL)
    {
        for (i = 0; i < 2; i++)
        {
            if (JumpInfo[i]->LinkLocation == NULL && JumpInfo[i]->FallThrough == false)
            {
                if (TargetSection[i])
                {
                    TargetSection[i]->UnlinkParent(this, i == 0);
                    TargetSection[i] = NULL;
                }
            }
            else if (TargetSection[i] == NULL && JumpInfo[i]->FallThrough)
            {
                m_RecompilerOps->LinkJump(*JumpInfo[i], (uint32_t)-1);
                m_RecompilerOps->CompileExit(JumpInfo[i]->JumpPC, JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet, JumpInfo[i]->ExitReason);
                JumpInfo[i]->FallThrough = false;
            }
            else if (TargetSection[i] != NULL && JumpInfo[i] != NULL)
            {
                if (!JumpInfo[i]->FallThrough) { continue; }
                if (JumpInfo[i]->TargetPC == TargetSection[i]->m_EnterPC) { continue; }
                m_RecompilerOps->LinkJump(*JumpInfo[i], (uint32_t)-1);
                m_RecompilerOps->CompileExit(JumpInfo[i]->JumpPC, JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet, JumpInfo[i]->ExitReason);
                //FreeSection(TargetSection[i],Section);
            }
        }
    }
    else
    {
        if (m_Cont.LinkLocation == NULL && m_Cont.FallThrough == false) { m_ContinueSection = NULL; }
        if (m_Jump.LinkLocation == NULL && m_Jump.FallThrough == false) { m_JumpSection = NULL; }
        if (m_JumpSection == NULL &&  m_ContinueSection == NULL)
        {
            //FreeSection(TargetSection[0],Section);
        }
    }

    TargetSection[0] = m_ContinueSection;
    TargetSection[1] = m_JumpSection;

    for (i = 0; i < 2; i++) {
        if (TargetSection[i] == NULL) { continue; }
        if (!JumpInfo[i]->FallThrough) { continue; }

        if (TargetSection[i]->m_CompiledLocation != NULL)
        {
            JumpInfo[i]->FallThrough = false;
            m_RecompilerOps->LinkJump(*JumpInfo[i], TargetSection[i]->m_SectionID);
            if (JumpInfo[i]->TargetPC <= m_RecompilerOps->GetCurrentPC())
            {
                if (JumpInfo[i]->PermLoop)
                {
                    CPU_Message("PermLoop *** 1");
                    m_RecompilerOps->CompileInPermLoop(JumpInfo[i]->RegSet, JumpInfo[i]->TargetPC);
                }
                else
                {
                    m_RecompilerOps->UpdateCounters(JumpInfo[i]->RegSet, true, true);
                    CPU_Message("CompileSystemCheck 5");
                    m_RecompilerOps->CompileSystemCheck(JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet);
                }
            }
            else
            {
                m_RecompilerOps->UpdateCounters(JumpInfo[i]->RegSet, false, true);
            }

            JumpInfo[i]->RegSet.SetBlockCycleCount(0);
            m_RecompilerOps->SetRegWorkingSet(JumpInfo[i]->RegSet);
            m_RecompilerOps->SyncRegState(TargetSection[i]->m_RegEnter);
            m_RecompilerOps->JumpToSection(TargetSection[i]);
        }
    }

    for (i = 0; i < 2; i++)
    {
        if (TargetSection[i] == NULL) { continue; }
        if (TargetSection[i]->m_ParentSection.empty()) { continue; }
        for (SECTION_LIST::iterator iter = TargetSection[i]->m_ParentSection.begin(); iter != TargetSection[i]->m_ParentSection.end(); iter++)
        {
            CCodeSection * Parent = *iter;

            if (Parent->m_CompiledLocation != NULL) { continue; }
            if (Parent->m_InLoop) { continue; }
            if (JumpInfo[i]->PermLoop)
            {
                CPU_Message("PermLoop *** 2");
                m_RecompilerOps->CompileInPermLoop(JumpInfo[i]->RegSet, JumpInfo[i]->TargetPC);
            }
            if (JumpInfo[i]->FallThrough)
            {
                JumpInfo[i]->FallThrough = false;
                m_RecompilerOps->JumpToUnknown(JumpInfo[i]);
            }
        }
    }

    for (i = 0; i < 2; i++)
    {
        if (JumpInfo[i]->FallThrough)
        {
            if (JumpInfo[i]->TargetPC < m_RecompilerOps->GetCurrentPC())
            {
                m_RecompilerOps->UpdateCounters(JumpInfo[i]->RegSet, true, true);
                CPU_Message("CompileSystemCheck 7");
                m_RecompilerOps->CompileSystemCheck(JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet);
            }
        }
    }

    CPU_Message("====== End of Section %d ======", m_SectionID);

    for (i = 0; i < 2; i++)
    {
        if (JumpInfo[i]->FallThrough && !TargetSection[i]->GenerateNativeCode(m_BlockInfo->NextTest()))
        {
            JumpInfo[i]->FallThrough = false;
            m_RecompilerOps->JumpToUnknown(JumpInfo[i]);
        }
    }

    //CPU_Message("Section %d",m_SectionID);
    for (i = 0; i < 2; i++)
    {
        if (JumpInfo[i]->LinkLocation == NULL) { continue; }
        if (TargetSection[i] == NULL)
        {
            CPU_Message("ExitBlock (from %d):", m_SectionID);
            m_RecompilerOps->LinkJump(*JumpInfo[i], (uint32_t)-1);
            m_RecompilerOps->CompileExit(JumpInfo[i]->JumpPC, JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet, JumpInfo[i]->ExitReason);
            continue;
        }
        if (JumpInfo[i]->TargetPC != TargetSection[i]->m_EnterPC)
        {
            g_Notify->BreakPoint(__FILE__, __LINE__);
        }
        if (TargetSection[i]->m_CompiledLocation == NULL)
        {
            TargetSection[i]->GenerateNativeCode(m_BlockInfo->NextTest());
        }
        else
        {
            stdstr_f Label("Section_%d (from %d):", TargetSection[i]->m_SectionID, m_SectionID);

            CPU_Message(Label.c_str());
            m_RecompilerOps->LinkJump(*JumpInfo[i], (uint32_t)-1);
            m_RecompilerOps->SetRegWorkingSet(JumpInfo[i]->RegSet);
            if (JumpInfo[i]->TargetPC <= JumpInfo[i]->JumpPC)
            {
                m_RecompilerOps->UpdateCounters(JumpInfo[i]->RegSet, true, true);
                if (JumpInfo[i]->PermLoop)
                {
                    CPU_Message("PermLoop *** 3");
                    m_RecompilerOps->CompileInPermLoop(JumpInfo[i]->RegSet, JumpInfo[i]->TargetPC);
                }
                else
                {
                    CPU_Message("CompileSystemCheck 9");
                    m_RecompilerOps->CompileSystemCheck(JumpInfo[i]->TargetPC, JumpInfo[i]->RegSet);
                }
            }
            else
            {
                m_RecompilerOps->UpdateCounters(m_RecompilerOps->GetRegWorkingSet(), false, true);
            }
            m_RecompilerOps->SetRegWorkingSet(JumpInfo[i]->RegSet);
            m_RecompilerOps->SyncRegState(TargetSection[i]->m_RegEnter);
            m_RecompilerOps->JumpToSection(TargetSection[i]);
        }
    }
}