Skip to content

Commit d781832

Browse files
authored
Add flag check functions (#6445)
## Description Adds functions to check the current state of the flags for panic on overflow, and panic on unsafe math ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 1bda0e8 commit d781832

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

sway-lib-std/src/flags.sw

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,45 @@ pub fn enable_panic_on_unsafe_math() {
210210
flag flag_val;
211211
}
212212
}
213+
214+
/// Checks if the `panic-on-overflow` flag is set in the FuelVM.
215+
///
216+
/// # Returns
217+
///
218+
/// * [bool] - `true` if `panic-on-overflow` is enabled. `false` otherwise.
219+
///
220+
/// # Examples
221+
///
222+
/// ```sway
223+
/// use std::flags::panic_on_overflow_enabled;
224+
///
225+
/// fn main() {
226+
/// let is_enabled = panic_on_overflow_enabled();
227+
/// // Panic on overflow is enabled by default.
228+
/// assert(is_enabled);
229+
/// }
230+
/// ```
231+
pub fn panic_on_overflow_enabled() -> bool {
232+
(flags() & F_WRAPPING_DISABLE_MASK) == 0
233+
}
234+
235+
/// Checks if the `panic-on-unsafe-math` flag is set in the FuelVM.
236+
///
237+
/// # Returns
238+
///
239+
/// * [bool] - `true` if `panic-on-unsafe-math` is enabled. `false` otherwise.
240+
///
241+
/// # Examples
242+
///
243+
/// ```sway
244+
/// use std::flags::panic_on_unsafe_math_enabled;
245+
///
246+
/// fn main() {
247+
/// let is_enabled = panic_on_unsafe_math_enabled();
248+
/// // Panic on unsafe math is enabled by default.
249+
/// assert(is_enabled);
250+
/// }
251+
/// ```
252+
pub fn panic_on_unsafe_math_enabled() -> bool {
253+
(flags() & F_UNSAFEMATH_DISABLE_MASK) == 0
254+
}

sway-lib-std/src/math.sw

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ library;
44
use ::assert::*;
55
use ::flags::{
66
disable_panic_on_overflow,
7-
F_UNSAFEMATH_DISABLE_MASK,
8-
F_WRAPPING_DISABLE_MASK,
7+
panic_on_overflow_enabled,
8+
panic_on_unsafe_math_enabled,
99
set_flags,
1010
};
1111
use ::registers::{flags, overflow};
@@ -123,7 +123,7 @@ impl Power for u32 {
123123
r3: u64
124124
};
125125
// If panic on wrapping math is enabled, only then revert
126-
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
126+
if panic_on_overflow_enabled() {
127127
assert(res <= Self::max().as_u64());
128128
}
129129
asm(r1: res) {
@@ -139,7 +139,7 @@ impl Power for u16 {
139139
r3: u64
140140
};
141141
// If panic on wrapping math is enabled, only then revert
142-
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
142+
if panic_on_overflow_enabled() {
143143
assert(res <= Self::max().as_u64());
144144
}
145145
asm(r1: res) {
@@ -155,7 +155,7 @@ impl Power for u8 {
155155
r3: u64
156156
};
157157
// If panic on wrapping math is enabled, only then revert
158-
if flags() & F_WRAPPING_DISABLE_MASK == 0 {
158+
if panic_on_overflow_enabled() {
159159
assert(res <= Self::max().as_u64());
160160
}
161161
asm(r1: res) {
@@ -244,7 +244,7 @@ impl BinaryLogarithm for u8 {
244244
impl BinaryLogarithm for u256 {
245245
fn log2(self) -> Self {
246246
// If panic on unsafe math is enabled, only then revert
247-
if flags() & F_UNSAFEMATH_DISABLE_MASK == 0 {
247+
if panic_on_unsafe_math_enabled() {
248248
// Logarithm is undefined for 0
249249
assert(self != 0);
250250
}
@@ -270,7 +270,7 @@ impl Logarithm for u256 {
270270
let flags = disable_panic_on_overflow();
271271

272272
// If panic on unsafe math is enabled, only then revert
273-
if flags & F_UNSAFEMATH_DISABLE_MASK == 0 {
273+
if panic_on_unsafe_math_enabled() {
274274
// Logarithm is undefined for bases less than 2
275275
assert(base >= 2);
276276
// Logarithm is undefined for 0

test/src/in_language_tests/test_programs/flags_inline_tests/src/main.sw

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use std::{
66
disable_panic_on_unsafe_math,
77
enable_panic_on_overflow,
88
enable_panic_on_unsafe_math,
9+
panic_on_overflow_enabled,
10+
panic_on_unsafe_math_enabled,
911
set_flags,
1012
},
1113
registers::error,
@@ -65,3 +67,27 @@ fn flags_disable_panic_on_unsafe_math_preserving() {
6567

6668
enable_panic_on_unsafe_math();
6769
}
70+
71+
#[test]
72+
fn test_panic_on_overflow_enabled() {
73+
// Enabled by default
74+
assert(panic_on_overflow_enabled());
75+
76+
disable_panic_on_overflow();
77+
assert(!panic_on_overflow_enabled());
78+
79+
enable_panic_on_overflow();
80+
assert(panic_on_overflow_enabled());
81+
}
82+
83+
#[test]
84+
fn test_panic_on_unsafe_math_enabled() {
85+
// Enabled by default
86+
assert(panic_on_unsafe_math_enabled());
87+
88+
disable_panic_on_unsafe_math();
89+
assert(!panic_on_unsafe_math_enabled());
90+
91+
enable_panic_on_unsafe_math();
92+
assert(panic_on_unsafe_math_enabled());
93+
}

0 commit comments

Comments
 (0)