-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
baaa4a3
commit 20fda2f
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
test-cases/unsafe-block/unsafe-block-1/remediated-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "unsafe-block-1-remediated" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
soroban-sdk = { version = "20.0.0-rc2" } | ||
|
||
[dev_dependencies] | ||
soroban-sdk = { version = "20.0.0-rc2", features = ["testutils"] } | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] | ||
|
||
[profile.release] | ||
opt-level = "z" | ||
overflow-checks = true | ||
debug = 0 | ||
strip = "symbols" | ||
debug-assertions = false | ||
panic = "abort" | ||
codegen-units = 1 | ||
lto = true | ||
|
||
[profile.release-with-logs] | ||
inherits = "release" | ||
debug-assertions = true |
35 changes: 35 additions & 0 deletions
35
test-cases/unsafe-block/unsafe-block-1/remediated-example/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl}; | ||
#[contract] | ||
pub struct UnsafeBlock; | ||
#[contractimpl] | ||
impl UnsafeBlock { | ||
pub fn unsafe_function(n: u64) -> u64 { | ||
let mut i = n as f64; | ||
let mut y = i.to_bits(); | ||
y = 0x5fe6ec85e7de30da - (y >> 1); | ||
i = f64::from_bits(y); | ||
i *= 1.5 - 0.5 * n as f64 * i * i; | ||
i *= 1.5 - 0.5 * n as f64 * i * i; | ||
i.to_bits() | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::UnsafeBlock; | ||
#[test] | ||
fn test_unsafe_block() { | ||
let test_value = 8; | ||
let result = UnsafeBlock::unsafe_function(test_value); | ||
let inverse = inverse_square_root_without_unsafe(test_value); | ||
|
||
assert_eq!((inverse - result) / inverse, 0); | ||
assert_eq!((inverse - result) / result, 0); | ||
|
||
} | ||
|
||
fn inverse_square_root_without_unsafe(n: u64) -> u64 { | ||
(1.0 / (n as f64).sqrt()).to_bits() | ||
|
||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
test-cases/unsafe-block/unsafe-block-1/vulnerable-example/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "unsafe-block-1-vulnerable" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
soroban-sdk = { version = "20.0.0-rc2" } | ||
|
||
[dev_dependencies] | ||
soroban-sdk = { version = "20.0.0-rc2", features = ["testutils"] } | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] | ||
|
||
[profile.release] | ||
opt-level = "z" | ||
overflow-checks = true | ||
debug = 0 | ||
strip = "symbols" | ||
debug-assertions = false | ||
panic = "abort" | ||
codegen-units = 1 | ||
lto = true | ||
|
||
[profile.release-with-logs] | ||
inherits = "release" | ||
debug-assertions = true |
42 changes: 42 additions & 0 deletions
42
test-cases/unsafe-block/unsafe-block-1/vulnerable-example/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl}; | ||
#[contract] | ||
pub struct UnsafeBlock; | ||
#[contractimpl] | ||
impl UnsafeBlock { | ||
pub fn unsafe_function(n: u64) -> u64 { | ||
unsafe { | ||
let mut i = n as f64; | ||
let mut y = i.to_bits(); | ||
y = 0x5fe6ec85e7de30da - (y >> 1); | ||
i = f64::from_bits(y); | ||
i *= 1.5 - 0.5 * n as f64 * i * i; | ||
i *= 1.5 - 0.5 * n as f64 * i * i; | ||
|
||
let result_ptr: *mut f64 = &mut i; | ||
let result = *result_ptr; | ||
|
||
result.to_bits() | ||
} | ||
} | ||
|
||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::UnsafeBlock; | ||
#[test] | ||
fn test_unsafe_block() { | ||
let test_value = 8; | ||
let result = UnsafeBlock::unsafe_function(test_value); | ||
let inverse = inverse_square_root_without_unsafe(test_value); | ||
|
||
assert_eq!((inverse - result) / inverse, 0); | ||
assert_eq!((inverse - result) / result, 0); | ||
|
||
} | ||
|
||
fn inverse_square_root_without_unsafe(n: u64) -> u64 { | ||
(1.0 / (n as f64).sqrt()).to_bits() | ||
|
||
} | ||
} |