Skip to content

clundin25/zero-copy-panic-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repo documents a scenario that prevents the compiler from optimizing away zerocopy::layout::DstLayout::validate_cast_and_convert_metadata which contains a panic.

This code will include a panic in the "release" binary:

use core::mem::size_of;
use zerocopy::FromBytes;

fn main() {
    let q = [0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0xA];
    let count = q.len() / size_of::<u32>();
    let (word_buf, _suffix) = <[u32]>::ref_from_prefix_with_elems(&q, count).unwrap();

    let mut p = [0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0xA];
    let p_count = p.len() / size_of::<u32>();
    let (mut writeable_word_buf, _suffix) = <[u32]>::mut_from_prefix_with_elems(&mut p, p_count).unwrap();
}

The following code does not include a panic. It does not matter which segment is commented.

use core::mem::size_of;
use zerocopy::FromBytes;

fn main() {
    let q = [0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0xA];
    let count = q.len() / size_of::<u32>();
    let (word_buf, _suffix) = <[u32]>::ref_from_prefix_with_elems(&q, count).unwrap();
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages