Skip to content

Commit 8857488

Browse files
committed
Avoid crash on old Windows with AVX2-capable CPU for VS2013 builds
The Visual Studio 2013 CRT generates invalid code when it makes a 64-bit build that is later used on a CPU that supports AVX2 instructions using a version of Windows before 7SP1/2008R2SP1. Detect this combination, and in those cases turn off the generation of FMA3, per recommendation from the Visual Studio team. The bug is actually in the CRT shipping with Visual Studio 2013, but Microsoft have stated they're only fixing it in newer major versions. The fix is therefor conditioned specifically on being built with this version of Visual Studio, and not previous or later versions. Author: Christian Ullrich
1 parent 4f37d09 commit 8857488

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/backend/main/main.c

+21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
#include <sys/param.h>
3535
#endif
3636

37+
#if defined(_M_AMD64) && _MSC_VER == 1800
38+
#include <math.h>
39+
#include <versionhelpers.h>
40+
#endif
41+
3742
#include "bootstrap/bootstrap.h"
3843
#include "common/username.h"
3944
#include "postmaster/postmaster.h"
@@ -289,6 +294,22 @@ startup_hacks(const char *progname)
289294

290295
/* In case of general protection fault, don't show GUI popup box */
291296
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
297+
298+
#if defined(_M_AMD64) && _MSC_VER == 1800
299+
/*
300+
* Avoid crashing in certain floating-point operations if
301+
* we were compiled for x64 with MS Visual Studio 2013 and
302+
* are running on Windows prior to 7/2008R2 SP1 on an
303+
* AVX2-capable CPU.
304+
*
305+
* Ref: https://connect.microsoft.com/VisualStudio/feedback/details/811093/visual-studio-2013-rtm-c-x64-code-generation-bug-for-avx2-instructions
306+
*/
307+
if (!IsWindows7SP1OrGreater())
308+
{
309+
_set_FMA3_enable(0);
310+
}
311+
#endif /* defined(_M_AMD64) && _MSC_VER == 1800 */
312+
292313
}
293314
#endif /* WIN32 */
294315

0 commit comments

Comments
 (0)