From 20840a40f9925ef30c8bde2dccbc888558e10097 Mon Sep 17 00:00:00 2001 From: Tianlei Wu Date: Wed, 8 Jul 2026 20:52:40 +0000 Subject: [PATCH 1/2] Spectre-mitigated libs for binskim compliant --- tools/ci_build/build.py | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 4061e4fafbe7d..fb11bc49afba8 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -320,6 +320,40 @@ def generate_vcpkg_install_options(build_dir, args): return vcpkg_install_options +def get_msvc_spectre_lib_dir(args): + """Return the directory that holds the MSVC Spectre-mitigated CRT/STL static libraries for the + target architecture, or None if it cannot be located. + + The /Qspectre compile flag only mitigates ONNX Runtime's own object files. The prebuilt MSVC + CRT/STL static libraries (libcmt.lib, libcpmt.lib, libvcruntime.lib) that get linked into the + binaries also need to be the Spectre-mitigated variants, otherwise BinSkim BA2024 + (EnableSpectreMitigations) still fails. Those variants ship in the "C++ Spectre-mitigated libs" + Visual Studio component under %VCToolsInstallDir%\\lib\\spectre\\. + """ + vctools_dir = os.environ.get("VCToolsInstallDir") # noqa: SIM112 + if not vctools_dir: + return None + if args.arm: + arch = "arm" + elif args.arm64: + arch = "arm64" + elif args.arm64ec: + arch = "arm64ec" + else: + # Default to the target architecture selected by vcvarsall.bat (x86, x64, arm, arm64), + # falling back to x64 which is what the official Windows release packages use. + arch = os.environ.get("VSCMD_ARG_TGT_ARCH", "x64") + spectre_dir = Path(vctools_dir) / "lib" / "spectre" / arch + if spectre_dir.is_dir(): + return str(spectre_dir) + # Some toolsets do not ship a dedicated arm64ec folder; those reuse the arm64 Spectre libraries. + if args.arm64ec: + fallback = Path(vctools_dir) / "lib" / "spectre" / "arm64" + if fallback.is_dir(): + return str(fallback) + return None + + def generate_build_tree( cmake_path, source_dir, @@ -1137,6 +1171,22 @@ def generate_build_tree( # Address Sanitizer libs do not have a Qspectre version. So they two cannot be both enabled. if not args.enable_address_sanitizer: cflags += ["/Qspectre"] + # /Qspectre only mitigates ONNX Runtime's own object files. The prebuilt MSVC + # CRT/STL static libraries (libcmt.lib, libcpmt.lib, libvcruntime.lib) that are + # linked into the binaries also have to be the Spectre-mitigated variants, + # otherwise BinSkim BA2024 (EnableSpectreMitigations) still fails. Prepend the + # Spectre lib directory to the linker search path so those libraries are + # resolved ahead of the default (non-mitigated) CRT libraries. + spectre_lib_dir = get_msvc_spectre_lib_dir(args) + if spectre_lib_dir is not None: + ldflags += [f'/LIBPATH:"{spectre_lib_dir}"'] + else: + log.warning( + "Could not locate the MSVC Spectre-mitigated CRT/STL libraries. The " + "resulting binaries may fail BinSkim BA2024 (EnableSpectreMitigations). " + "Install the 'C++ Spectre-mitigated libs' component from the Visual " + "Studio installer and build from a Developer Command Prompt." + ) if config == "Release": cflags += ["/O2", "/Ob2", "/DNDEBUG"] elif config == "RelWithDebInfo": From ca1e7b046fa75d559c385c0abe9a7005ec3d6c02 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Jul 2026 06:47:41 +0000 Subject: [PATCH 2/2] fix: check args.x86 in get_msvc_spectre_lib_dir and prepend LIBPATH --- tools/ci_build/build.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index fb11bc49afba8..3d501c16079a8 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -339,6 +339,8 @@ def get_msvc_spectre_lib_dir(args): arch = "arm64" elif args.arm64ec: arch = "arm64ec" + elif args.x86: + arch = "x86" else: # Default to the target architecture selected by vcvarsall.bat (x86, x64, arm, arm64), # falling back to x64 which is what the official Windows release packages use. @@ -1179,7 +1181,7 @@ def generate_build_tree( # resolved ahead of the default (non-mitigated) CRT libraries. spectre_lib_dir = get_msvc_spectre_lib_dir(args) if spectre_lib_dir is not None: - ldflags += [f'/LIBPATH:"{spectre_lib_dir}"'] + ldflags = [f'/LIBPATH:"{spectre_lib_dir}"', *ldflags] else: log.warning( "Could not locate the MSVC Spectre-mitigated CRT/STL libraries. The "