Merge pull request #3161 from shuffle2/ucrt-fread-workaround

exchange XSaveWorkaround for ucrtFreadWorkaround
This commit is contained in:
flacs 2015-10-17 05:41:40 +02:00
commit e289cb87e5
5 changed files with 89 additions and 65 deletions

View File

@ -162,13 +162,13 @@
<ClCompile Include="Thread.cpp" />
<ClCompile Include="Timer.cpp" />
<ClCompile Include="TraversalClient.cpp" />
<ClCompile Include="ucrtFreadWorkaround.cpp" />
<ClCompile Include="Version.cpp" />
<ClCompile Include="x64ABI.cpp" />
<ClCompile Include="x64Analyzer.cpp" />
<ClCompile Include="x64CPUDetect.cpp" />
<ClCompile Include="x64Emitter.cpp" />
<ClCompile Include="x64FPURoundMode.cpp" />
<ClCompile Include="XSaveWorkaround.cpp" />
<ClCompile Include="Crypto\bn.cpp" />
<ClCompile Include="Crypto\ec.cpp" />
<ClCompile Include="Logging\LogManager.cpp" />

View File

@ -230,7 +230,6 @@
<ClCompile Include="Logging\LogManager.cpp">
<Filter>Logging</Filter>
</ClCompile>
<ClCompile Include="XSaveWorkaround.cpp" />
<ClCompile Include="GekkoDisassembler.cpp" />
<ClCompile Include="JitRegister.cpp" />
<ClCompile Include="TraversalClient.cpp" />
@ -249,6 +248,7 @@
<ClCompile Include="GL\GLInterface\GLInterface.cpp">
<Filter>GL\GLInterface</Filter>
</ClCompile>
<ClCompile Include="ucrtFreadWorkaround.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />

View File

@ -1,61 +0,0 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#if defined(_WIN32)
#include <math.h>
#include <Windows.h>
typedef decltype(&GetEnabledXStateFeatures) GetEnabledXStateFeatures_t;
int __cdecl EnableXSaveWorkaround()
{
// Some Windows environments may have hardware support for AVX/FMA,
// but the OS does not support it. The CRT math library does not support
// this scenario, so we have to manually tell it not to use FMA3
// instructions.
// The API name is somewhat misleading - we're testing for OS support
// here.
if (!IsProcessorFeaturePresent(PF_XSAVE_ENABLED))
{
_set_FMA3_enable(0);
return 0;
}
// Even if XSAVE feature is enabled, we have to see if
// GetEnabledXStateFeatures function is present, and see what it says about
// AVX state.
auto kernel32Handle = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32Handle == nullptr)
{
std::abort();
}
auto pGetEnabledXStateFeatures = (GetEnabledXStateFeatures_t)GetProcAddress(
kernel32Handle, "GetEnabledXStateFeatures");
if (pGetEnabledXStateFeatures == nullptr ||
(pGetEnabledXStateFeatures() & XSTATE_MASK_AVX) == 0)
{
_set_FMA3_enable(0);
}
return 0;
}
// Create a segment which is recognized by the linker to be part of the CRT
// initialization. XI* = C startup, XC* = C++ startup. "A" placement is reserved
// for system use. Thus, the earliest we can get is XIB (C startup is before
// C++).
#pragma section(".CRT$XIB", read)
// Place a symbol in the special segment, make it have C linkage so that
// referencing it doesn't require ugly decorated names.
// Use /include:XSaveWorkaround linker flag to enable this.
extern "C" {
__declspec(allocate(".CRT$XIB"))
decltype(&EnableXSaveWorkaround) XSaveWorkaround = EnableXSaveWorkaround;
};
#endif

View File

@ -0,0 +1,85 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#if defined(_WIN32)
#include "CommonTypes.h"
#include <Windows.h>
struct PatchInfo {
const wchar_t *module_name;
u32 checksum;
u32 rva;
u32 length;
} static const s_patches[] = {
{ L"ucrtbase.dll", 0xF61ED, 0x6AE7B, 5 },
{ L"ucrtbased.dll", 0x1C1915 , 0x91905, 5 },
};
bool ApplyPatch(const PatchInfo &patch) {
auto module = GetModuleHandleW(patch.module_name);
if (module == nullptr)
{
return false;
}
auto ucrtbase_pe = (PIMAGE_NT_HEADERS)((uintptr_t)module + ((PIMAGE_DOS_HEADER)module)->e_lfanew);
if (ucrtbase_pe->OptionalHeader.CheckSum != patch.checksum) {
return false;
}
void *patch_addr = (void *)((uintptr_t)module + patch.rva);
size_t patch_size = patch.length;
DWORD old_protect;
if (!VirtualProtect(patch_addr, patch_size, PAGE_EXECUTE_READWRITE, &old_protect))
{
return false;
}
memset(patch_addr, 0x90, patch_size);
VirtualProtect(patch_addr, patch_size, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), patch_addr, patch_size);
return true;
}
int __cdecl EnableucrtFreadWorkaround()
{
// This patches ucrtbase such that fseek will always
// synchronize the file object's internal buffer.
bool applied_at_least_one = false;
for (const auto &patch : s_patches) {
if (ApplyPatch(patch)) {
applied_at_least_one = true;
}
}
/* For forward compat, do not fail if patches don't apply (e.g. version mismatch)
if (!applied_at_least_one) {
std::abort();
}
//*/
return 0;
}
// Create a segment which is recognized by the linker to be part of the CRT
// initialization. XI* = C startup, XC* = C++ startup. "A" placement is reserved
// for system use. Thus, the earliest we can get is XIB (C startup is before
// C++).
#pragma section(".CRT$XIB", read)
// Place a symbol in the special segment, make it have C linkage so that
// referencing it doesn't require ugly decorated names.
// Use /include:EnableucrtFreadWorkaround linker flag to enable this.
extern "C" {
__declspec(allocate(".CRT$XIB"))
decltype(&EnableucrtFreadWorkaround) ucrtFreadWorkaround = EnableucrtFreadWorkaround;
};
#endif

View File

@ -122,8 +122,8 @@
</Link>
<!--Link Base:Application-->
<Link Condition="'$(ConfigurationType)'=='Application'">
<!--See Common/EnableXSaveWorkaround.cpp-->
<ForceSymbolReferences>XSaveWorkaround</ForceSymbolReferences>
<!--See Common/ucrtFreadWorkaround.cpp-->
<ForceSymbolReferences>ucrtFreadWorkaround</ForceSymbolReferences>
</Link>
<Lib>
<TreatLibWarningAsErrors>true</TreatLibWarningAsErrors>