Git Product home page Git Product logo

Comments (9)

Konard avatar Konard commented on July 19, 2024 1

If Rust forces us to be not UB here, we should try to make code better.

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

To implement it, we need:

  • Fix Szb trait from trees-rs to remove self parameter from all methods. Add explicit data argument (currently &mut [T])
  • Remove all data fields from Szb trait implementors from doublets
  • Rework all Store's functional to use new trees.

from doublets-rs.

Konard avatar Konard commented on July 19, 2024

At the moment our tree implementations do not support thread safety. Can you make a benchmark of this proposal? Does it give any improvement in speed by itself?

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

Any code in Rust is always thread-safe, otherwise it's UB.
Also, intentional violation of Stacked Borrows is also UB.

This code is works, but actually it's UB (later)

let mut data = 10;
let ref1 = &mut data;
let ref2 = ref1 as *mut _;
unsafe {
    *ref1 += 1;
    *ref2 += 2;
}
error: Undefined Behavior: attempting a read access using <untagged> at alloc2284[0x0], but that tag does not exist in the borrow stack for this location
 --> src\main.rs:8:9
  |
8 |         *ref2 += 2;
  |         ^^^^^^^^^^
  |         |
  |
4 |     let ref2 = ref1 as *mut _;
  |                ^^^^
help: tag was later invalidated at offsets [0x0..0x4]
 --> src\main.rs:7:9
  |
7 |         *ref1 += 1;
  |         ^^^^^^^^^^
  = note: inside `main` at src\main.rs:8:9

Since the compiler uses two rules for optimization:

  • cannot borrow X as mutable more than once at a time
  • cannot borrow X as mutable because it is also borrowed as immutable

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

Does it give any improvement in speed by itself?

I can only promise compiler optimizations and no UB

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

It is my initiative, but it will help us to make the code better and without possible UB

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

Why is bad?
Try to write this code
It's actually the valid code, but the compiler isn't that smart:

let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let monitor = &data[..5];
data[..5].copy_from_slice(&[10, 9, 8, 7, 6]);
println!("hey, i immutable: {monitor:?}");
println!("{data:?}");
error[E0502]: cannot borrow `data` as mutable because it is also borrowed as immutable
 --> src\main.rs:5:10
  |
4 |     let monitor = &data[..5];
  |                    ---- immutable borrow occurs here
5 |     &mut data[..5].copy_from_slice(&[10, 9, 8, 7, 6]);
  |          ^^^^ mutable borrow occurs here
6 |
7 |     println!("hey, i immutable: {monitor:?}");
  |                                  ------- immutable borrow later used here

Ok, unsafe time:

let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let monitor = unsafe { from_raw_parts(data.as_ptr(), data.len()).get_unchecked(..5) };
data[..5].copy_from_slice(&[10, 9, 8, 7, 6]);
println!("hey, i immutable: {monitor:?}");
println!("{data:?}");

It works:

hey, i immutable: [10, 9, 8, 7, 6]
[10, 9, 8, 7, 6, 6, 7, 8, 9, 10]

But miri...

     |
2371 | fmt_refs! { Debug, Display, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     | |
     | trying to reborrow from <4390> for SharedReadOnly permission at alloc2110[0x0], but that tag does not exist in the borrow stack for this location
     | this error occurs as part of a reborrow at alloc2110[0x0..0x14]
     |

Hm. println! hinders us. Remove it:

let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let monitor = unsafe { from_raw_parts(data.as_ptr(), data.len()).get_unchecked(..5) };
data[..5].copy_from_slice(&[10, 9, 8, 7, 6]);
let _ = monitor;
error: Undefined Behavior: trying to reborrow from <4390> for SharedReadOnly permission at alloc2102[0x0], but that tag does not exist in the borrow stack for this location
 --> src\main.rs:9:19
  |
9 |     let monitor = monitor;
  |                   ^^^^^^^
  |                   |
  |                   trying to reborrow from <4390> for SharedReadOnly permission at alloc2102[0x0], but that tag does not exist in the borrow stack for this location
  |                   this error occurs as part of a reborrow at alloc2102[0x0..0x14]
  |

Why are you doing that?
from_raw_parts(data.as_ptr(), data.len()) borrow whole data. Miri think that we borrow data[0..10]. Let's rewrite the code:

let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let monitor = unsafe { from_raw_parts(data.as_ptr().add(5), data.len() - 5) };
data[..5].copy_from_slice(&[10, 9, 8, 7, 6]);

Nice. Miri silent.
I also recommend using a safe rust:

let mut data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let (monitor, hot) = data.split_at_mut(5);
hot.copy_from_slice(&[10, 9, 8, 7, 6]);

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

It's not exactly dangerous, but possible has potential bugs

from doublets-rs.

uselessgoddess avatar uselessgoddess commented on July 19, 2024

Develompent in stacked-borrows-initiative branch

from doublets-rs.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.