Enable floating point exceptions with backtraces.

Related tags

Miscellaneous batman
Overview

Batman

Bat signal appears in the sky over Catwoman from the 1989 Batman film

Have you ever written a function like this that was hard to debug?

fn main() {
    let signal = [""; 16].join(&format!("{}", f64::sqrt(50.3 - 50.0 - 0.3)));

    println!("{signal} Batman!");
}

Well now you don't have to be confused any more! Just let batman configure FPU hardware exceptions and your days of debugging garbage floating point calculations will be over!

fn main() {
    // Here be dragons!
    unsafe { batman::signal() };

    let signal = [""; 16].join(&format!("{}", f64::sqrt(50.3 - 50.0 - 0.3)));

    println!("{signal} Batman!");
}

Let's see how it works:

$ cargo run --example batman
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/examples/batman`
Caught signal 8 (SIGFPE): Floating point exception
   0: batman::backtrace::Backtrace::new
             at ./src/backtrace.rs:14:20
   1: batman::unix::sigfpe_handler
             at ./src/unix.rs:25:14
   2: <unknown>
   3: std::f64::<impl f64>::sqrt
             at /rustc/9ad5d82f822b3cb67637f11be2e65c5662b66ec0/library/std/src/f64.rs:345
   4: batman::main
             at ./examples/batman.rs:5:47
   5: core::ops::function::FnOnce::call_once
             at /rustc/9ad5d82f822b3cb67637f11be2e65c5662b66ec0/library/core/src/ops/function.rs:227:5

Aborted

Signal SIGFPE is caught on line 5 in batman.rs in the call to f64::sqrt()! Multiply this by several thousand lines of various linear algebra calculations used in games and scientific computing, and batman will have your back.

But floating point exceptions are slow and they crash my program!

That's why batman only enables them when debug assertions are enabled.

$ cargo run --example batman --release
    Finished release [optimized] target(s) in 0.00s
     Running `target/release/examples/batman`
NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batman!

But setting signal and exception handlers mutates global state!

Yeah, configuring FPU exceptions was made unsafe because this can be a real problem. On the bright side, enabling exceptions themselves is thread-local state, so you can decide to raise exceptions on a per-thread basis. But be aware that threads inherit FPU exceptions from their parent, and there is no way to turn it off (for API simplicity).

Caveats

batman requires unstable features and only works on nightly compilers. Since you're obviously adventurous anyway, you can use it on stable with nightly-crimes.

Why is it named batman?

If the joke wasn't clear by now, I humbly refer you to https://www.destroyallsoftware.com/talks/wat. Combine this with the bat signal implication, and you'll agree it's the best name.

You might also like...
A floating, tag-based window manager written in Rust
A floating, tag-based window manager written in Rust

worm worm is a floating, tag-based window manager for X11. It is written in the Rust programming language, using the X11RB library. Install cargo buil

The nightly_crimes!{} macro commits horrible crimes to allow you to enable nightly features on the stable compiler.

The nightly_crimes!{} macro commits horrible crimes to allow you to enable nightly features on the stable compiler.

A library to access BGPKIT Broker API and enable searching for BGP data archive files over time from public available data sources.
A library to access BGPKIT Broker API and enable searching for BGP data archive files over time from public available data sources.

BGPKIT Broker BGPKIT Broker is a online data API service that allows users to search for publicly available BGP archive files by time, collector, proj

A set of utilities to better enable polymorphic behavior in Rust

Polymorph A set of utilities to better enable polymorphic behavior in Rust. Introduction Rust is a wonderful language, with a strong emphasis on fast,

Rust crate to enable ANSI escape code support on Windows.

enable-ansi-support: Enable ANSI escape code support on Windows 10 About This crate provides one function, enable_ansi_support, which allows ANSI esca

The Light Protocol program verifies zkSNARK proofs to enable anonymous transactions on Solana.

Light Protocol DISCLAIMER: THIS SOFTWARE IS NOT AUDITED. Do not use in production! Tests cd ./program && cargo test-bpf deposit_should_succeed cd ./pr

A template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it.

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

Smart Contract built in Rust to run inside Cosmos SDK module on all chains that enable it

CoinSwap is a Smart Contract that is built on the terra blockchain and can be used to swap cryptocurrencies such as LUNA, UST, TerraUSD, Anchor, Mirror Protocol, LUNI and other CW20 tokens. The Project also contains a smart contract which works as a analysis tool for the gas fees on the Terra Blockchain.

An investigation to enable ethernet with smoltcp on the SAM E70 XPLAINED ULTRA EVALUATION KIT

An investigation to enable ethernet with smoltcp on the SAM E70 XPLAINED ULTRA EVALUATION KIT

Outp0st is an open-source UI tool to enable next-level team collaboration on dApp development over Terra blockchain
Outp0st is an open-source UI tool to enable next-level team collaboration on dApp development over Terra blockchain

Outp0st is an open-source UI tool to enable next-level team collaboration on dApp development over Terra blockchain

A plugin to enable random number generation for the Bevy game engine.

bevy_turborand A plugin to enable random number generation for the Bevy game engine, built upon turborand. Implements ideas from Bevy's Deterministic

This is an example Nostr rust project to enable '402 Payment Required' responses for requests to paid content.

Nostr Paywall Example This is an example Nostr rust project to enable 402 Payment Required responses for requests to paid content. To prove payment, a

Work to enable a Classic Mac (24-bit 68000) with ~16MB of RAM.

Apple SE FDHD ROM analysis In order to build a Mac clone that doesn't fully emulate the hardware (which is possible because the ROM abstracts hardware

SlintDotnet is a C# bindings project to enable developers to use Slint UI with .NET C#

SlintDotnet (Alpha) Slint is a UI toolkit that supports different programming languages. SlintDotnet is the integration with .NET C#. ⚠️ This is exper

Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.
Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.

dlctix Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin. This project is part of the Backdrop Build

Nearest neighbor search algorithms including a ball tree and a vantage point tree.

petal-neighbors Nearest neighbor search algorithms including a ball tree and a vantage point tree. Examples The following example shows how to find tw

The point of this anchor project is to serve as a starter kit or example to compose with mango-v3 using anchor.

The point of this anchor project is to serve as a starter kit or example to compose with mango-v3 using anchor. It currently provides 2 examples and various inline todos on how to extend this.

Wena is a micro-framework that provides an elegant starting point for your console application.
Wena is a micro-framework that provides an elegant starting point for your console application.

Wena was created by Nuno Maduro, and is a Rust Lang micro-framework that provides an elegant starting point for your console application. This project

this is an initial working version, ALL code will be replaced and rewritten at SOME point

fracgen-gui this is an initial working version, ALL code will be replaced and rewritten at SOME point z | x less/more der bail alt: - | - adjust col

Owner
Jay Oster
Jay Oster
A library to access BGPKIT Broker API and enable searching for BGP data archive files over time from public available data sources.

BGPKIT Broker BGPKIT Broker is a online data API service that allows users to search for publicly available BGP archive files by time, collector, proj

BGPKIT 10 Nov 30, 2022
An investigation to enable ethernet with smoltcp on the SAM E70 XPLAINED ULTRA EVALUATION KIT

An investigation to enable ethernet with smoltcp on the SAM E70 XPLAINED ULTRA EVALUATION KIT

James Munns 2 Mar 10, 2022
Fixed point to floating point (and back) conversion utility

fixed2float Simple utility for fixed point to real number conversions, using the VisSim (Fxm.b) and Q (Qm.n) notations. Usage as a dependency of your

Francesco Urbani 2 Aug 5, 2022
SIMD Floating point and integer compressed vector library

compressed_vec Floating point and integer compressed vector library, SIMD-enabled for fast processing/iteration over compressed representations. This

Evan Chan 56 Nov 24, 2022
Optimize floating-point expressions for accuracy

Herbie automatically improves the error of floating point expressions. Visit our website for tutorials, documentation, and an online demo. Herbie has

Herbie Project 611 Dec 19, 2022
Multiple precision floating point numbers implemented purely in Rust.

Multiple precision floating point numbers implemented purely in Rust. Rationale There are several notable implementations of numbers with increased pr

null 11 Nov 23, 2022
Teleport is a simple application for sending files from Point A to Point B

Teleporter Teleporter is a small utility in the vein of netcat to send files quickly from point A to point B. It is more convenient than netcat in tha

geno 21 Dec 18, 2022
📡Proxy HTTP/1.1 requests over a sensitive point-to-point link

ptproxy Motivation What's this? Why do I need this? What's a sensitive network link? What's wrong with a VPN? What's wrong with HTTP[S]? What's wrong

Alba Mendez 5 Jul 26, 2023
Error context library with support for type-erased sources and backtraces, targeting full support of all features on stable Rust

Error context library with support for type-erased sources and backtraces, targeting full support of all features on stable Rust, and with an eye towards serializing runtime errors using serde.

Findora Foundation 1 Feb 12, 2022
Detect EDR's exceptions by inspecting processes' loaded modules

Description This tool looks for either the processes that have a certain binary loaded or the processes that don't. This is useful in the following sc

Kurosh Dabbagh Escalante 89 May 7, 2023