aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Hagander <magnus@hagander.net>2016-03-10 13:48:58 +0100
committerMagnus Hagander <magnus@hagander.net>2016-03-10 14:11:01 +0100
commit3e6385e27105a95ccc00ae475a37f83d431fc730 (patch)
tree163729e7efc3530b3514945e7814b80deb3612e9
parenta62714fae23d2056242bb2ac872111cab8f9fbd1 (diff)
downloadpostgresql-3e6385e27105a95ccc00ae475a37f83d431fc730.tar.gz
postgresql-3e6385e27105a95ccc00ae475a37f83d431fc730.zip
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
-rw-r--r--src/backend/main/main.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/backend/main/main.c b/src/backend/main/main.c
index b764f552f83..97d2c512dfd 100644
--- a/src/backend/main/main.c
+++ b/src/backend/main/main.c
@@ -35,6 +35,11 @@
#include <sys/param.h>
#endif
+#if defined(_M_AMD64) && _MSC_VER == 1800
+#include <math.h>
+#include <versionhelpers.h>
+#endif
+
#include "bootstrap/bootstrap.h"
#include "postmaster/postmaster.h"
#include "tcop/tcopprot.h"
@@ -274,6 +279,22 @@ startup_hacks(const char *progname)
/* In case of general protection fault, don't show GUI popup box */
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
+
+#if defined(_M_AMD64) && _MSC_VER == 1800
+ /*
+ * Avoid crashing in certain floating-point operations if
+ * we were compiled for x64 with MS Visual Studio 2013 and
+ * are running on Windows prior to 7/2008R2 SP1 on an
+ * AVX2-capable CPU.
+ *
+ * Ref: https://connect.microsoft.com/VisualStudio/feedback/details/811093/visual-studio-2013-rtm-c-x64-code-generation-bug-for-avx2-instructions
+ */
+ if (!IsWindows7SP1OrGreater())
+ {
+ _set_FMA3_enable(0);
+ }
+#endif /* defined(_M_AMD64) && _MSC_VER == 1800 */
+
}
#endif /* WIN32 */
}