microsoft / microsoft/STL

<atomic>: std::atomic<float>::fetch_add doesn't use safe FP environment

Open
#3,988 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

LWG issue needed
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug

std::atomic<FloatType>::fetch_add is supposed to always work, without undefined behavior. If you have floating-point exceptions unmasked, and fetch_add causes a floating-point exception, your program will crash.

This is apparently non-standard. Quoting the current draft:

Remarks: If the result is not a representable value for its type ([expr.pre]) the result is unspecified, but the operations otherwise have no undefined behavior. Atomic arithmetic operations on floating-point-type should conform to the std​::​numeric_limits<floating-point-type> traits associated with the floating-point type ([limits.syn]). The floating-point environment ([cfenv]) for atomic arithmetic operations on floating-point-type may be different than the calling thread's floating-point environment.

Command-line test case

C:\Temp>type repro.cpp
#include <atomic>
#include <float.h>

#pragma fenv_access(on)

std::atomic<float> g_test = 3.40282346639e+38f;

int main()
{
    unsigned original;
    _controlfp_s(&original, 0, _EM_OVERFLOW);

    float temp = g_test.load(std::memory_order_seq_cst);
    g_test.fetch_add(temp, std::memory_order_seq_cst);

    unsigned dummy;
    _controlfp_s(&dummy, original, _EM_OVERFLOW);
    return 0;
}

#pragma fenv_access(off)

C:\Temp>cl /Ox /std:c++20 .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32538 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.36.32538.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe

C:\Temp>echo %ERRORLEVEL%
-1073741679

Expected behavior

fetch_add's addition should use a safe floating-point environment.

STL version

Microsoft Visual Studio Professional 2022 (64-bit) - LTSC 17.6
Version 17.6.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling and running the repro.cpp command-line test case with MSVC, focusing on std::atomic::fetch_add and its floating-point environment behavior. Trace that entry point and verify that an overflowing addition with floating-point exceptions unmasked completes without crashing while preserving the required atomic operation semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.