Skip to content

Commit

Permalink
fix bug in memcopyopt related to killing defined symbols (#6820)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha authored Jan 9, 2025
1 parent bb855e8 commit e1c3fa3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sway-ir/src/optimize/memcpyopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ fn local_copy_prop(
available_copies.remove(copy);
}
}
copies.retain(|copy| available_copies.contains(copy));
}
if let Some(copies) = dest_to_copies.get_mut(&sym) {
for copy in &*copies {
Expand Down Expand Up @@ -333,9 +332,17 @@ fn local_copy_prop(
available_copies.remove(copy);
}
}
copies.retain(|copy| available_copies.contains(copy));
}
}
// Update src_to_copies and dest_to_copies to remove every copy not in available_copies.
src_to_copies.retain(|_, copies| {
copies.retain(|copy| available_copies.contains(copy));
!copies.is_empty()
});
dest_to_copies.retain(|_, copies| {
copies.retain(|copy| available_copies.contains(copy));
!copies.is_empty()
});
}
ReferredSymbols::Incomplete(_) => {
// The only safe thing we can do is to clear all information.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-1F6D5E67DD060824"

[[package]]
name = "mutability_of_references_memcpy_bug"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "path+from-root-1F6D5E67DD060824"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "mutability_of_references_memcpy_bug"

[dependencies]
std = { path = "../../../../../reduced_std_libs/sway-lib-std-assert" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library;

#[inline(never)]
pub fn unit(b: u256) -> u256 {
b
}

#[inline(never)]
pub fn weird(_b: u256) {
let mut b = _b; // b = _b = 2

log(b);

let mut b_tmp = b; // b_tmp = b = 2

b = 0x03u256; // b = a = 1

assert(unit(b_tmp) == 0x02u256);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
contract;

pub mod lib;

use ::lib::weird;

abi MyContract {
fn test_function(b: u256) -> u256;
}

impl MyContract for Contract {

fn test_function(_b: u256) -> u256 {
weird(_b);
0x00u256
}
}



#[test]
fn test() {
let caller = abi(MyContract, CONTRACT_ID);
let b = 0x02u256;

let _ = caller.test_function(b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "unit_tests_pass"
validate_abi = false
expected_warnings = 0

0 comments on commit e1c3fa3

Please sign in to comment.