Giter Club home page Giter Club logo

chunky's People

Contributors

aeplay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

chunky's Issues

Chunk API does not respect align requirement

chunky/src/value.rs

Lines 29 to 41 in ef8533a

impl<V> ::std::ops::Deref for Value<V> {
type Target = V;
fn deref(&self) -> &V {
unsafe { (self.chunk.as_ptr() as *const V).as_ref().unwrap() }
}
}
impl<V> ::std::ops::DerefMut for Value<V> {
fn deref_mut(&mut self) -> &mut V {
unsafe { (self.chunk.as_mut_ptr() as *mut V).as_mut().unwrap() }
}
}

Description

Chunk API does not respect the align requirement of types. Unaligned reference can be created with the API, which is an undefined behavior.

Demonstration

  • Crate: chunky
  • Version: 0.3.7
  • OS: Ubuntu 18.04.5 LTS
  • Rust: rustc 1.47.0-nightly (bf4342114 2020-08-25)
#![forbid(unsafe_code)]

use chunky::{HeapStorage, Ident, Value};
use std::rc::Rc;

#[repr(align(256))]
struct LargeAlign(u8);

impl Drop for LargeAlign {
    fn drop(&mut self) {
        println!("Dropped");
    }
}

fn main() {
    let ident = Ident(String::from("ident"));
    let storage = Rc::new(HeapStorage);
    let value = Value::load_or_default(ident, LargeAlign(0), storage.clone());

    // Value reference does not have a correct alignment
    let v = &*value as *const _ as usize;
    println!("{:x}", v);
    assert!(v % std::mem::align_of::<LargeAlign>() == 0);

    // https://github.com/aeplay/chunky/blob/ef8533aec961eb5f415414dcd81ec4b395bae177/src/value.rs#L43-L49
    // Another bug that is not UB: `LargeAlign::Drop` is not called due to incorrect `drop_in_place()` in `Value::drop()`.
    // "Dropped" should be printed otherwise.
}

Output:

555e921bea80
thread 'main' panicked at 'assertion failed: v % std::mem::align_of::<LargeAlign>() == 0', src/main.rs:42:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Return Code: 101

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.