Debian Codegraph @

src/atlas/library/FloatingPointExceptions.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "atlas/library/FloatingPointExceptions.h"
12
13 #include <cfenv>
14 #include <cstring>
15 #include <iomanip>
16 #include <bitset>
17
18 #include "eckit/config/LibEcKit.h"
19 #include "eckit/config/Resource.h"
20 #include "eckit/runtime/Main.h"
21 #include "eckit/utils/StringTools.h"
22 #include "eckit/utils/Translator.h"
23
24 #include "atlas/library/config.h"
25 #include "atlas/parallel/mpi/mpi.h"
26 #include "atlas/runtime/Exception.h"
27 #include "atlas/runtime/Log.h"
28 #include "atlas/runtime/Trace.h"
29
30 namespace {
31 int getEnv(const std::string& env, int default_value) {
32 const char* cenv = ::getenv(env.c_str());
33 if (cenv != nullptr) {
34 return eckit::Translator<std::string, int>()(cenv);
35 }
36 return default_value;
37 }
38 } // namespace
39
40 #if ATLAS_HAVE_FEENABLEEXCEPT
41 static int atlas_feenableexcept(unsigned int excepts) {
42 return ::feenableexcept(excepts);
43 }
44 static int atlas_fedisableexcept(unsigned int excepts) {
45 return ::fedisableexcept(excepts);
46 }
47 #elif defined(__APPLE__)
48 static int atlas_feenableexcept(unsigned int excepts) {
49 static fenv_t fenv;
50 unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
51 unsigned int old_excepts; // previous masks
52
53 if (::fegetenv(&fenv)) {
54 return -1;
55 }
56
57 #if defined(__arm64__)
58 old_excepts = fenv.__fpsr & FE_ALL_EXCEPT;
59
60 fenv.__fpsr |= new_excepts;
61 fenv.__fpcr |= (new_excepts << 8);
62 #else
63 old_excepts = fenv.__control & FE_ALL_EXCEPT;
64
65 fenv.__control &= ~new_excepts;
66 fenv.__mxcsr &= ~(new_excepts << 7);
67 #endif
68
69 return ::fesetenv(&fenv) ? -1 : old_excepts;
70 }
71 static int atlas_fedisableexcept(unsigned int excepts) {
72 static fenv_t fenv;
73 unsigned int new_excepts = excepts & FE_ALL_EXCEPT;
74 unsigned int old_excepts; // all previous masks
75
76 if (::fegetenv(&fenv)) {
77 return -1;
78 }
79
80 #if defined(__arm64__)
81 old_excepts = fenv.__fpsr & FE_ALL_EXCEPT;
82
83 fenv.__fpsr &= ~new_excepts;
84 fenv.__fpcr &= ~(new_excepts << 8);
85 #else
86 old_excepts = fenv.__control & FE_ALL_EXCEPT;
87
88 fenv.__control |= new_excepts;
89 fenv.__mxcsr |= (new_excepts << 7);
90 #endif
91
92 return ::fesetenv(&fenv) ? -1 : old_excepts;
93 }
94 #else
95 static int atlas_feenableexcept(unsigned int excepts) {
96 return 0;
97 }
98 static int atlas_fedisableexcept(unsigned int excepts) {
99 return 0;
100 }
101 #endif
102
103 namespace atlas {
104 namespace library {
105
106 static std::map<std::string, int> str_to_except = {{"FE_INVALID", FE_INVALID}, {"FE_INEXACT", FE_INEXACT},
107 {"FE_DIVBYZERO", FE_DIVBYZERO}, {"FE_OVERFLOW", FE_OVERFLOW},
108 {"FE_UNDERFLOW", FE_UNDERFLOW}, {"FE_ALL_EXCEPT", FE_ALL_EXCEPT}};
109 static std::map<int, std::string> except_to_str = {{FE_INVALID, "FE_INVALID"}, {FE_INEXACT, "FE_INEXACT"},
110 {FE_DIVBYZERO, "FE_DIVBYZERO"}, {FE_OVERFLOW, "FE_OVERFLOW"},
111 {FE_UNDERFLOW, "FE_UNDERFLOW"}, {FE_ALL_EXCEPT, "FE_ALL_EXCEPT"}};
112
113 static std::map<std::string, int> str_to_signal = {{"SIGINT", SIGINT}, {"SIGILL", SIGILL}, {"SIGABRT", SIGABRT},
114 {"SIGFPE", SIGFPE}, {"SIGKILL", SIGKILL}, {"SIGSEGV", SIGSEGV},
115 {"SIGTERM", SIGTERM}};
116 static std::map<int, std::string> signal_to_str = {{SIGINT, "SIGINT"}, {SIGILL, "SIGILL"}, {SIGABRT, "SIGABRT"},
117 {SIGFPE, "SIGFPE"}, {SIGKILL, "SIGKILL"}, {SIGSEGV, "SIGSEGV"},
118 {SIGTERM, "SIGTERM"}};
119
120 // ------------------------------------------------------------------------------------
121
122 class Signal {
123 // Not sure if this should be made public (in header file) just yet
124
125 public:
126 Signal();
127
128 Signal(int signum);
129
130 Signal(int signum, signal_action_t);
131
132 Signal(int signum, signal_handler_t signal_handler);
133
134 operator int() const { return signum_; }
135 int signum() const { return signum_; }
136
137 const std::string& code() const { return signal_to_str[signum_]; }
138
139 std::string str() const { return str_; }
140 const signal_handler_t& handler() const { return signal_action_.sa_handler; }
141 const struct sigaction* action() const { return &signal_action_; }
142
143 private:
144 friend std::ostream& operator<<(std::ostream&, const Signal&);
145
146 int signum_;
147 std::string str_;
148 struct sigaction signal_action_;
149 };
150
151 // ------------------------------------------------------------------------------------
152
153 class Signals {
154 // Not sure if this should be made public (in header file) just yet
155
156 private:
157 Signals();
158
159 public:
160 static Signals& instance();
161 void setSignalHandlers();
162 void setSignalHandler(const Signal&);
163 void restoreSignalHandler(int signum);
164 void restoreAllSignalHandlers();
165 const Signal& signal(int signum) const;
166
167 private:
168 using registered_signals_t = std::map<int, Signal>;
169 registered_signals_t registered_signals_;
170 eckit::Channel& out_;
171 };
172
173 // ------------------------------------------------------------------------------------
174
175
176 // ------------------------------------------------------------------------------------
177
178 [[noreturn]] void atlas_signal_handler(int signum, siginfo_t* si, [[maybe_unused]] void* ucontext) {
179 Signal signal = Signals::instance().signal(signum);
180
181 std::string signal_code;
182 if (signum == SIGFPE) {
183 switch (si->si_code) {
184 case FPE_FLTDIV:
185 signal_code = " [FE_DIVBYZERO]";
186 break;
187 case FPE_FLTINV:
188 signal_code = " [FE_INVALID]";
189 break;
190 case FPE_FLTOVF:
191 signal_code = " [FE_OVERFLOW]";
192 break;
193 case FPE_FLTUND:
194 signal_code = " [FE_UNDERFLOW]";
195 break;
196 case FPE_FLTRES:
197 signal_code = " [FE_INEXACT]";
198 break;
199 }
200 }
201 #if defined(__APPLE__) && defined(__arm64__)
202 if (signum == SIGILL) {
203 // On Apple Silicon a SIGFPE may be posing as a SIGILL
204 // See:
205 // https://developer.apple.com/forums/thread/689159?answerId=733736022
206 // https://developer.arm.com/documentation/ddi0595/2020-12/AArch64-Registers/ESR-EL1--Exception-Syndrome-Register--EL1-?lang=en#fieldset_0-24_0_16-1_1
207 auto esr = reinterpret_cast<ucontext_t*>(ucontext)->uc_mcontext->__es.__esr;
208 auto is_floating_point_exception = [&esr]() {
209 constexpr unsigned long fpe_mask = 2952790016; // bits: 10110000000000000000000000000000
210 constexpr std::bitset<32> fpe_mask_bits(fpe_mask);
211 return((fpe_mask_bits & std::bitset<32>(esr)) == fpe_mask_bits);
212 };
213 auto test_esr = [&esr](auto pos) -> bool {
214 return std::bitset<32>(esr).test(pos);
215 };
216 if (is_floating_point_exception()) {
217 // SIGILL is posing as a SIGFPE
218 signal = Signals::instance().signal(SIGFPE);
219 constexpr size_t IOF = 0; // invalid operation
220 constexpr size_t DZF = 1; // divide-by-zero
221 constexpr size_t OFF = 2; // overflow
222 constexpr size_t UFF = 3; // undeflow
223 constexpr size_t IXF = 4; // inexact
224 constexpr size_t IDF = 7; // denormal
225 if (test_esr(IOF)) {
226 signal_code = " [FE_INVALID]";
227 }
228 else if(test_esr(DZF)) {
229 signal_code = " [FE_DIVBYZERO]";
230 }
231 else if(test_esr(OFF)) {
232 signal_code = " [FE_OVERFLOW]";
233 }
234 else if(test_esr(UFF)) {
235 signal_code = " [FE_UNDERFLOW]";
236 }
237 else if(test_esr(IXF)) {
238 signal_code = " [FE_INEXACT]";
239 }
240 else if(test_esr(IDF)) {
241 signal_code = " [FE_DENORMAL]";
242 }
243 }
244 }
245 #endif
246
247 std::ostream& out = Log::error();
248 out << "\n"
249 << "=========================================\n"
250 << signal << signal_code << " (signal intercepted by atlas)\n";
251 out << "-----------------------------------------\n"
252 << "BACKTRACE\n"
253 << "-----------------------------------------\n"
254 << backtrace() << "\n"
255 << "=========================================\n"
256 << std::endl;
257
258 Signals::instance().restoreSignalHandler(signum);
259 eckit::LibEcKit::instance().abort();
260
261 // Just in case we end up here, which normally we shouldn't.
262 std::_Exit(EXIT_FAILURE);
263 }
264
265 //----------------------------------------------------------------------------------------------------------------------
266
267
268 Signals::Signals():
269 out_([&]() -> eckit::Channel& {
270 if (getEnv("ATLAS_LOG_RANK", 0) == int(mpi::rank())) {
271 return Log::debug();
272 }
273 static eckit::Channel sink;
274 return sink;
275 }()) {}
276
277 Signals& Signals::instance() {
278 static Signals signals;
279 return signals;
280 }
281
282 void Signals::restoreSignalHandler(int signum) {
283 if (registered_signals_.find(signum) != registered_signals_.end()) {
284 out_ << "\n";
285 std::signal(signum, SIG_DFL);
286 out_ << "Atlas restored default signal handler for signal " << std::setw(7) << std::left
287 << registered_signals_[signum].code() << " [" << registered_signals_[signum] << "]\n";
288 out_ << std::endl;
289 registered_signals_.erase(signum);
290 }
291 }
292
293 void Signals::restoreAllSignalHandlers() {
294 out_ << "\n";
295 for (registered_signals_t::const_iterator it = registered_signals_.begin(); it != registered_signals_.end(); ++it) {
296 std::signal(it->first, SIG_DFL);
297 out_ << "Atlas restored default signal handler for signal " << std::setw(7) << std::left << it->second.code()
298 << " [" << it->second.str() << "]\n";
299 }
300 out_ << std::endl;
301 registered_signals_.clear();
302 }
303
304 const Signal& Signals::signal(int signum) const {
305 return registered_signals_.at(signum);
306 }
307
308 std::ostream& operator<<(std::ostream& out, const Signal& signal) {
309 out << signal.str();
310 return out;
311 }
312
313 void Signals::setSignalHandlers() {
314 setSignalHandler(SIGINT);
315 setSignalHandler(SIGILL);
316 setSignalHandler(SIGABRT);
317 setSignalHandler(SIGFPE);
318 setSignalHandler(SIGSEGV);
319 setSignalHandler(SIGTERM);
320 }
321
322 void Signals::setSignalHandler(const Signal& signal) {
323 registered_signals_[signal] = signal;
324 sigaction(signal, signal.action(), nullptr);
325 out_ << "Atlas registered signal handler for signal " << std::setw(7) << std::left << signal.code() << " ["
326 << signal << "]" << std::endl;
327 }
328
329
330 Signal::Signal(): signum_(0), str_() {
331 signal_action_.sa_handler = SIG_DFL;
332 }
333
334 Signal::Signal(int signum): Signal(signum, atlas_signal_handler) {}
335
336 Signal::Signal(int signum, signal_handler_t signal_handler): signum_(signum), str_(strsignal(signum)) {
337 memset(&signal_action_, 0, sizeof(signal_action_));
338 sigemptyset(&signal_action_.sa_mask);
339 signal_action_.sa_handler = signal_handler;
340 signal_action_.sa_flags = 0;
341 }
342
343 Signal::Signal(int signum, signal_action_t signal_action): signum_(signum), str_(strsignal(signum)) {
344 memset(&signal_action_, 0, sizeof(signal_action_));
345 sigemptyset(&signal_action_.sa_mask);
346 signal_action_.sa_sigaction = signal_action;
347 signal_action_.sa_flags = SA_SIGINFO;
348 }
349
350
351 void enable_floating_point_exceptions() {
352 auto& out = [&]() -> eckit::Channel& {
353 if (getEnv("ATLAS_LOG_RANK", 0) == int(mpi::rank())) {
354 return Log::debug();
355 }
356 static eckit::Channel sink;
357 return sink;
358 }();
359
360 // Following line gives runtime errors with Cray 8.6 due to compiler bug (but works with Cray 8.5 and Cray 8.7)
361 // std::vector<std::string> floating_point_exceptions = eckit::Resource<std::vector<std::string>>( "atlasFPE;$ATLAS_FPE", {"false"} );
362 // Instead, manually access environment
363 std::vector<std::string> floating_point_exceptions{"false"};
364 const char* ATLAS_FPE = ::getenv("ATLAS_FPE");
365 if (ATLAS_FPE != nullptr) {
366 std::vector<std::string> tmp = eckit::Translator<std::string, std::vector<std::string>>()(ATLAS_FPE);
367 floating_point_exceptions = tmp;
368 // Above trick with "tmp" is what avoids the Cray 8.6 compiler bug
369 }
370 else {
371 if (eckit::Main::ready()) {
372 floating_point_exceptions = eckit::Resource<std::vector<std::string>>("atlasFPE", {"false"});
373 }
374 }
375 {
376 bool _enable = false;
377 int _excepts = 0;
378 auto enable = [&](int except) {
379 _excepts |= except;
380 _enable = true;
381 out << "Atlas enabled floating point exception " << except_to_str[except] << std::endl;
382 };
383 bool skip_map = false;
384 if (floating_point_exceptions.size() == 1) {
385 std::string s = eckit::StringTools::lower(floating_point_exceptions[0]);
386 if (s == "no" || s == "off" || s == "false" || s == "0") {
387 _enable = false;
388 skip_map = true;
389 }
390 else if (s == "yes" || s == "on" || s == "true" || s == "1") {
391 enable(FE_INVALID);
392 enable(FE_DIVBYZERO);
393 enable(FE_OVERFLOW);
394 skip_map = true;
395 }
396 }
397 if (not skip_map) {
398 for (auto& s : floating_point_exceptions) {
399 if (str_to_except.find(s) == str_to_except.end()) {
400 throw eckit::UserError(
401 s + " is not a valid floating point exception code. "
402 "Valid codes: [FE_INVALID,FE_INEXACT,FE_DIVBYZERO,FE_OVERFLOW,FE_ALL_EXCEPT]",
403 Here());
404 }
405 enable(str_to_except[s]);
406 }
407 }
408 if (_enable) {
409 atlas_feenableexcept(_excepts);
410 }
411 }
412 }
413
414 bool enable_floating_point_exception(int except) {
415 auto check_flag = [](int flags, int bits) -> bool { return (flags & bits) == bits; };
416 int previous = atlas_feenableexcept(except);
417 return !check_flag(previous,except);
418 }
419
420 bool disable_floating_point_exception(int except) {
421 auto check_flag = [](int flags, int bits) -> bool { return (flags & bits) == bits; };
422 int previous = atlas_fedisableexcept(except);
423 return !check_flag(previous,except);
424 }
425
426 bool enable_floating_point_exception(const std::string& floating_point_exception) {
427 auto it = str_to_except.find(floating_point_exception);
428 if (it == str_to_except.end()) {
429 throw eckit::UserError(
430 floating_point_exception + " is not a valid floating point exception code. "
431 "Valid codes: [FE_INVALID,FE_INEXACT,FE_DIVBYZERO,FE_OVERFLOW,FE_ALL_EXCEPT]",
432 Here());
433 }
434 return enable_floating_point_exception(it->second);
435 }
436
437 bool disable_floating_point_exception(const std::string& floating_point_exception) {
438 auto it = str_to_except.find(floating_point_exception);
439 if (it == str_to_except.end()) {
440 throw eckit::UserError(
441 floating_point_exception + " is not a valid floating point exception code. "
442 "Valid codes: [FE_INVALID,FE_INEXACT,FE_DIVBYZERO,FE_OVERFLOW,FE_ALL_EXCEPT]",
443 Here());
444 }
445 return disable_floating_point_exception(it->second);
446 }
447
448
449 void enable_atlas_signal_handler() {
450 bool enable = false;
451 const char* ATLAS_SIGNAL_HANDLER = ::getenv("ATLAS_SIGNAL_HANDLER");
452 if (ATLAS_SIGNAL_HANDLER != nullptr) {
453 bool tmp = eckit::Translator<std::string, bool>()(ATLAS_SIGNAL_HANDLER);
454 enable = tmp;
455 // Above trick with "tmp" is what avoids the Cray 8.6 compiler bug
456 }
457 else {
458 if (eckit::Main::ready()) {
459 enable = eckit::Resource<bool>("atlasSignalHandler", false);
460 }
461 }
462
463 if (enable) {
464 Signals::instance().setSignalHandlers();
465 }
466 }
467
468 } // namespace library
469 } // namespace atlas