Skip to content

Commit

Permalink
Merge branch 'master' into vaivaswatha/cse
Browse files Browse the repository at this point in the history
  • Loading branch information
IGI-111 authored Sep 2, 2024
2 parents 9f26596 + d781832 commit 89704bb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
42 changes: 42 additions & 0 deletions sway-lib-std/src/flags.sw
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,45 @@ pub fn enable_panic_on_unsafe_math() {
flag flag_val;
}
}

/// Checks if the `panic-on-overflow` flag is set in the FuelVM.
///
/// # Returns
///
/// * [bool] - `true` if `panic-on-overflow` is enabled. `false` otherwise.
///
/// # Examples
///
/// ```sway
/// use std::flags::panic_on_overflow_enabled;
///
/// fn main() {
/// let is_enabled = panic_on_overflow_enabled();
/// // Panic on overflow is enabled by default.
/// assert(is_enabled);
/// }
/// ```
pub fn panic_on_overflow_enabled() -> bool {
(flags() & F_WRAPPING_DISABLE_MASK) == 0
}

/// Checks if the `panic-on-unsafe-math` flag is set in the FuelVM.
///
/// # Returns
///
/// * [bool] - `true` if `panic-on-unsafe-math` is enabled. `false` otherwise.
///
/// # Examples
///
/// ```sway
/// use std::flags::panic_on_unsafe_math_enabled;
///
/// fn main() {
/// let is_enabled = panic_on_unsafe_math_enabled();
/// // Panic on unsafe math is enabled by default.
/// assert(is_enabled);
/// }
/// ```
pub fn panic_on_unsafe_math_enabled() -> bool {
(flags() & F_UNSAFEMATH_DISABLE_MASK) == 0
}
14 changes: 7 additions & 7 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ library;
use ::assert::*;
use ::flags::{
disable_panic_on_overflow,
F_UNSAFEMATH_DISABLE_MASK,
F_WRAPPING_DISABLE_MASK,
panic_on_overflow_enabled,
panic_on_unsafe_math_enabled,
set_flags,
};
use ::registers::{flags, overflow};
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Power for u32 {
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
if panic_on_overflow_enabled() {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
Expand All @@ -139,7 +139,7 @@ impl Power for u16 {
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
if panic_on_overflow_enabled() {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
Expand All @@ -155,7 +155,7 @@ impl Power for u8 {
r3: u64
};
// If panic on wrapping math is enabled, only then revert
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
if panic_on_overflow_enabled() {
assert(res <= Self::max().as_u64());
}
asm(r1: res) {
Expand Down Expand Up @@ -244,7 +244,7 @@ impl BinaryLogarithm for u8 {
impl BinaryLogarithm for u256 {
fn log2(self) -> Self {
// If panic on unsafe math is enabled, only then revert
if flags() & F_UNSAFEMATH_DISABLE_MASK == 0 {
if panic_on_unsafe_math_enabled() {
// Logarithm is undefined for 0
assert(self != 0);
}
Expand All @@ -270,7 +270,7 @@ impl Logarithm for u256 {
let flags = disable_panic_on_overflow();

// If panic on unsafe math is enabled, only then revert
if flags & F_UNSAFEMATH_DISABLE_MASK == 0 {
if panic_on_unsafe_math_enabled() {
// Logarithm is undefined for bases less than 2
assert(base >= 2);
// Logarithm is undefined for 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
disable_panic_on_unsafe_math,
enable_panic_on_overflow,
enable_panic_on_unsafe_math,
panic_on_overflow_enabled,
panic_on_unsafe_math_enabled,
set_flags,
},
registers::error,
Expand Down Expand Up @@ -65,3 +67,27 @@ fn flags_disable_panic_on_unsafe_math_preserving() {

enable_panic_on_unsafe_math();
}

#[test]
fn test_panic_on_overflow_enabled() {
// Enabled by default
assert(panic_on_overflow_enabled());

disable_panic_on_overflow();
assert(!panic_on_overflow_enabled());

enable_panic_on_overflow();
assert(panic_on_overflow_enabled());
}

#[test]
fn test_panic_on_unsafe_math_enabled() {
// Enabled by default
assert(panic_on_unsafe_math_enabled());

disable_panic_on_unsafe_math();
assert(!panic_on_unsafe_math_enabled());

enable_panic_on_unsafe_math();
assert(panic_on_unsafe_math_enabled());
}

0 comments on commit 89704bb

Please sign in to comment.