Skip to content

Commit 86b74c3

Browse files
authored
chore: refuse to compile if FLT_EVAL_METHOD != 0 (#13872)
This PR is the first of several aiming to harden our runtime against floating point nondeterminism.
1 parent e8bf278 commit 86b74c3

4 files changed

Lines changed: 12 additions & 3 deletions

File tree

script/prepare-llvm-linux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $CP $GCC_LIB/lib/libatomic.so* stage1/lib/
3535

3636
find stage1 -type f -exec strip --strip-unneeded '{}' \; 2> /dev/null
3737
# lean.h dependencies
38-
$CP llvm/lib/clang/*/include/{std*,__std*,limits}.h stage1/include/clang
38+
$CP llvm/lib/clang/*/include/{std*,__std*,limits,float,__float*}.h stage1/include/clang
3939
# ELF dependencies, must be put there for `--sysroot`
4040
$CP $GLIBC/lib/*crt* llvm/lib/
4141
$CP $GLIBC/lib/*crt* stage1/lib/

script/prepare-llvm-macos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ gcp -L llvm/bin/llvm-ar stage1/bin/
3030
$CP llvm/lib/lib{clang-cpp,LLVM}.dylib stage1/lib/
3131
#find stage1 -type f -exec strip --strip-unneeded '{}' \; 2> /dev/null
3232
# lean.h dependencies
33-
$CP llvm/lib/clang/*/include/{std*,__std*,limits}.h stage1/include/clang
33+
$CP llvm/lib/clang/*/include/{std*,__std*,limits,float,__float*}.h stage1/include/clang
3434
# runtime
3535
(cd llvm; $CP --parents lib/clang/*/lib/*/libclang_rt.osx.a ../stage1)
3636
# libSystem stub, includes libc

script/prepare-llvm-mingw.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cp llvm/bin/llvm-ar stage1/bin/
2020
# dependencies of the above
2121
cp $(ldd llvm/bin/{clang,lld,llvm-ar}.exe | cut -f3 -d' ' --only-delimited | grep -E 'llvm|clang64') stage1/bin
2222
# lean.h dependencies
23-
cp llvm/lib/clang/*/include/{std*,__std*,limits}.h stage1/include/clang
23+
cp llvm/lib/clang/*/include/{std*,__std*,limits,float,__float*}.h stage1/include/clang
2424
# single Windows dependency
2525
echo '
2626
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode

src/include/lean/lean.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Leonardo de Moura
99
#include <stdbool.h>
1010
#include <stdint.h>
1111
#include <limits.h>
12+
#include <float.h>
1213

1314
#include <lean/config.h>
1415

@@ -78,6 +79,14 @@ void lean_notify_assert(const char * fileName, int line, const char * condition)
7879
#define LEAN_EXPORT
7980
#endif
8081

82+
// `FLT_EVAL_METHOD` is mandated to exist by the standard since C++11 and C99, but if we're in danger of triggering this
83+
// error, all bets are probably off and we should check whether it exists to avoid falling back to `0`.
84+
// We also perform this check in `lean.h` rather than in one of the runtime files because it is only valid per translation
85+
// unit, so we try to be safe and check it in all translation units.
86+
#if !defined(FLT_EVAL_METHOD) || (FLT_EVAL_METHOD != 0)
87+
#error Lean requires `FLT_EVAL_METHOD = 0` to ensure predictable semantics for floating-point operations.
88+
#endif
89+
8190
#define LEAN_BYTE(Var, Index) *(((uint8_t*)&Var)+Index)
8291

8392
#define LeanMaxCtorTag 243

0 commit comments

Comments
 (0)