Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS_USE_SPECTRE_MITIGATION_RUNTIME should prefer the Spectre library path, not use it exclusively #73

Merged
merged 1 commit into from
Jan 20, 2024

Conversation

MarkSchofield
Copy link
Owner

VS_USE_SPECTRE_MITIGATION_RUNTIME instructs the toolchain to switch the library include path for the compiler runtime to a spectre sub-directory. When compiling with MSVC's ASAN support (i.e. /fsanitize=address) the compilation depends on .lib files that aren't present in the spectre sub-directory, but are present in the default library include path, breaking ASAN builds if VS_USE_SPECTRE_MITIGATION_RUNTIME is used. If VS_USE_SPECTRE_MITIGATION_RUNTIME were to prefer the spectre subdirectory - rather than using it exclusively - then the ASAN libraries would still be found.

@MarkSchofield MarkSchofield marked this pull request as ready for review July 22, 2023 22:58
@MarkSchofield
Copy link
Owner Author

The problem here is that the fallback would happen for the Spectre runtime, too. So if someone asked for the Spectre runtime and VS didn't have it installed, then the non-Spectre runtime would be picked-up silently. Which is a problem.

#40 tracks error'ing if the Spectre runtime is asked for but not installed. Including that fix here would probably help.

@MarkSchofield
Copy link
Owner Author

This PR includes the work to FATAL_ERROR if the Spectre runtimes aren't installed. But in order to do that, there - realistically - needs to be one CMake option for each 'unit' of Spectre runtime. The VS installer separates the C/C++ runtime from the ATL/MFC runtime, so I'm using VS_USE_SPECTRE_MITIGATION_RUNTIME to ensure that the C/C++ runtime is installed, and VS_USE_SPECTRE_MITIGATION_ATLMFC_RUNTIME to ensure that the ATL/MFC runtime is installed.

@MarkSchofield MarkSchofield merged commit 067977d into main Jan 20, 2024
17 checks passed
@MarkSchofield MarkSchofield deleted the mschofie/spectre-asan branch January 20, 2024 18:49
geraldcombs pushed a commit to wireshark/wireshark that referenced this pull request May 18, 2024
Problem:
- SPECTRE-mitigation (/Qspectre) is enabled by default but this does not allow
ASAN (-fsanitize=address) to be enabled because of failing compiler checks for
ASAN_FLAG. This problem can also be reproduced with a C++ console application
sample using Visual Studio 2019/2022 where it will report linking error related
to ASAN library if both the options are enabled.

Cause:
- This happens because the ASAN libraries are not present in the path that is
used by the linker when SPECTRE-mitigation is enabled.
- ASAN libraries are present in VS_TOOLSET_PATH/lib/<arch> but are not present
in VS_TOOLSET_PATH/lib/spectre/<arch> which is the path used by
SPECTRE-mitigation libraries.
- Following links also talk about the same problem.
MarkSchofield/WindowsToolchain#72
MarkSchofield/WindowsToolchain#73

Fixes:
- First fix is to disable SPECTRE-mitigation when ASAN is requested and is
considered a default fix by this change.
- Second fix is to add path where ASAN libraries are present and is activated
only when ENABLE_ASAN_WITH_SPECTRE is provided on the CMake command line along
with ENABLE_ASAN.

Note:
- Both the fixes are applicable for Debug and RelWithDebInfo build types only
because they provide the necessary debug information required for ASAN to be
functional. If ASAN is enabled on Release and MinSizeRel build types too, then
warning C5072 gets generated causing build failure (because this warning is
treated as an error).

Example:
- error C2220: the following warning is treated as an error
- warning C5072: ASAN enabled without debug information emission.
                 Enable debug info for better ASAN error reporting.

Important:
- Checking if ASAN is supported requires SPECTRE-mitigation to be disabled
globally impacting all build types. If ASAN is supported, then it is re-enabled
selectively depending on the build types.
- As per /libpath documentation, path provided to it is used before all the
other paths. This causes SPECTRE-mitigated libraries to be skipped if
non-mitigated versions are present in the ASAN library path. For example,
comsuppw.lib is present in both the paths. If SPECTRE-mitigation is enabled
(along with ASAN), and only ASAN library path is added, then comsuppw.lib from
ASAN library path is used!

Summary:
- For First or Default Fix
  - Debug and RelWithDebInfo: SPECTRE-mitigation stays disabled
                              ASAN is enabled
  - Release and MinSizeRel: SPECTRE-mitigation is re-enabled
                            ASAN is skipped
- For Second Fix
  - Debug and RelWithDebInfo: SPECTRE-mitigation is re-enabled
                              ASAN is enabled
  - Release and MinSizeRel: SPECTRE-mitigation is re-enabled
                            ASAN is skipped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant