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

MSVC: Add –d2UndefIntOverflow– #1396

Open
jedisct1 opened this issue Jul 31, 2024 · 3 comments
Open

MSVC: Add –d2UndefIntOverflow– #1396

jedisct1 opened this issue Jul 31, 2024 · 3 comments

Comments

@jedisct1
Copy link
Owner

When using zig/clang/gcc, we unconditionally enable -fwrapv in order make overflowing signed operations well defined.

But this is not the case when using MSVC.

–d2UndefIntOverflow– is a compiler flag equivalent to -fwrapv: https://devblogs.microsoft.com/cppblog/new-code-optimizer/

We should figure out how to enable it in the Visual Studio solutions.

@jedisct1
Copy link
Owner Author

Does anybody know how to do this?

@GPUex
Copy link

GPUex commented Oct 22, 2024

Well tried using latest VS 2022: Project properties -> C/C++ -> Command line -> added /d2UndefIntOverflow-
But on got compile error: Error C1007 unrecognized flag '-UndefIntOverflow-' in 'p2'
So it seems it's no longer valid flag, unless I'm missing something .

@ReenigneCA
Copy link
Contributor

I bit the bullet and booted my dual boot machine into windows to do some experimentation this morning. Using the same method of adding the flag to Commandline like GPUex /d2UndefIntOverflow- does not work but /UndefIntOverflow- does. I assume it does the same thing. if I search for UndefIntOverflow in the msvc docs I get no results with or without d2. https://lectem.github.io/msvc/reverse-engineering/build/2019/01/21/MSVC-hidden-flags.html lists /UndefIntOverflow+ and /UndefIntOverflow- as hidden flags but not versions with d2 in them.

My understanding is that d2 indicates which stage of compilation the flag effects and as far as I can see it seems this is generally or always only used for hidden flags not meant for public consumption. So my guess is that they decided to make UndefIntOverflow publicly available at some point and renamed the internal flag but haven't finalized things and actually put the flag in the documentation yet (and that they may or may not have just forgotten about the whole thing).

So really old versions of visual studio prior to that devlog from 2016 likely don't support the option at all. The first version likely supports it with the d2 prefix and then at some point the prefix is dropped which is the state for the latest version.

@jedisct1 So as I see it we could:

  1. Choose not to bother with the flag until it is officially documented,
  2. Implement it without the d2 prefix for visual studio 2022 and future versions as they are created,
  3. Have someone install all supported versions of visual studio and document which flag works and add in the correct flag for each version or
  4. Similar to 3 but have someone determine when the d2 prefix was dropped and put the flag in for all versions that drop the prefix;

I recommend 1 or 2. I believe 2 would just be a minor change to the libsodium.vcxproj file:

...
    <ClCompile>
      <AdditionalOptions>/UndefIntOverflow- %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants