Rust Undefined Behavior Sanitizer (UBSan) Implementation - Minimal Example
This challenge asks you to implement a simplified version of Undefined Behavior Sanitizer (UBSan) in Rust. UBSan is a powerful tool that detects various forms of undefined behavior at runtime, helping developers identify and fix subtle bugs that can lead to crashes or incorrect results. This exercise focuses on detecting integer overflow, a common source of undefined behavior.
Problem Description
You are tasked with creating a module that intercepts integer arithmetic operations and checks for overflow. The module should provide a wrapper around standard integer operations (add, sub, mul) for i32 that detects overflow and panics if it occurs. The goal is to demonstrate the core concept of intercepting operations and checking for UB, not to create a full-fledged UBSan.
What needs to be achieved:
- Create a module named
ubsan. - Within the
ubsanmodule, define three functions:add,sub, andmul. These functions will be wrappers around the standardi32addition, subtraction, and multiplication operations, respectively. - Each wrapper function should perform an overflow check before executing the underlying operation.
- If overflow is detected, the function should panic with a descriptive message indicating the type of operation and that overflow occurred.
- If no overflow is detected, the function should return the result of the underlying operation.
Key Requirements:
- The overflow check must be performed before the operation is executed.
- The panic message should be clear and informative.
- The module should be usable as a drop-in replacement for standard
i32arithmetic operations (within the scope of addition, subtraction, and multiplication).
Expected Behavior:
- When an
ubsanfunction is called with inputs that result in overflow, the program should panic. - When an
ubsanfunction is called with inputs that do not result in overflow, the program should return the correct result.
Edge Cases to Consider:
- Maximum and minimum values of
i32. - Zero values.
- Negative values.
- Operations that result in no change (e.g.,
x + 0,x - x).
Examples
Example 1:
Input: ubsan::add(i32::MAX, 1)
Output: Panic: "Integer overflow detected during addition!"
Explanation: Adding 1 to `i32::MAX` results in overflow. The `ubsan::add` function detects this and panics.
Example 2:
Input: ubsan::add(5, 3)
Output: 8
Explanation: Adding 5 and 3 does not result in overflow. The `ubsan::add` function returns the correct result.
Example 3:
Input: ubsan::mul(2, i32::MAX)
Output: Panic: "Integer overflow detected during multiplication!"
Explanation: Multiplying 2 by `i32::MAX` results in overflow. The `ubsan::mul` function detects this and panics.
Constraints
- The implementation must use only standard Rust libraries.
- The code should be reasonably efficient (avoiding unnecessary allocations or computations).
- The module should be self-contained and easy to integrate into other Rust projects.
- The panic message should be a
&'static str.
Notes
- Rust's built-in checked arithmetic methods (
checked_add,checked_sub,checked_mul) are your friends! Use them to detect overflow. - Consider using
matchorif letto handle the results of the checked arithmetic methods. - This is a simplified example. A real UBSan would detect many more forms of undefined behavior and provide more detailed diagnostics. The focus here is on the core concept of interception and overflow detection.
- The
ubsanmodule should be designed to be easily extensible to support other types of undefined behavior checks in the future.