Giter VIP home page Giter VIP logo

Comments (7)

RalfJung avatar RalfJung commented on June 30, 2024 1

ICE message (which as cut off from the original backtrace -- please pots the full error message, not just a pat of it :)

error: internal compiler error: compiler/rustc_const_eval/src/interpret/validity.rs:1039:17: Unexpected error during validation: `extern type` does not have a known offset

We should really reject extern types being used like that, they violate fundamental language invariants...

from rust.

lolbinarycat avatar lolbinarycat commented on June 30, 2024

it looks like the problem arises when transmuting in as_counted_str, i'll try to do a minimal repo.

from rust.

lolbinarycat avatar lolbinarycat commented on June 30, 2024

minimal repro found

#![feature(extern_types)]
#![allow(dead_code)]

extern {
    type Opaque;
}

struct ThinDst {
    x: u8,
    tail: Opaque,
}

const fn t<const N: usize>(x: &[u8; N]) -> &ThinDst {
    unsafe { std::mem::transmute(x.as_ptr()) }
}

const C1: &ThinDst = t(b"d");

fn main() {} // required because "miri build" does not exist

from rust.

saethlin avatar saethlin commented on June 30, 2024

I've transferred this issue because it's not a bug in Miri, it's a bug in the compiler overall. Miri is the compiler, until main starts. But in your repro, there's nothing in main. So this is just compiler.

from rust.

saethlin avatar saethlin commented on June 30, 2024

Smaller:

#![feature(extern_types)]

extern {
    type Opaque;
}

struct ThinDst {
    x: u8,
    tail: Opaque,
}

const C1: &ThinDst = std::mem::transmute(b"d".as_ptr());

from rust.

RalfJung avatar RalfJung commented on June 30, 2024

Note that this is not a good idea IMO:

// use extern types to satisfy stacked borrow rules.
#[cfg(miri)]
extern {
	type TrailingData;
}

Using extern types basically intimidates Miri into not checking your code, but that means Miri may miss bugs.

A better fix is to check your code with -Zmiri-tree-borrows, which has proper support for references that only cover the "header" of a struct. Tree Borrows should still find all UB bugs that can affect your code today, but in the future it's likely we are going to pick aliasing rules that are stricter than Tree Borrows (and weaker than Stacked Borrows).

from rust.

lolbinarycat avatar lolbinarycat commented on June 30, 2024

Note that this is not a good idea IMO:

// use extern types to satisfy stacked borrow rules.
#[cfg(miri)]
extern {
	type TrailingData;
}

Using extern types basically intimidates Miri into not checking your code, but that means Miri may miss bugs.

A better fix is to check your code with -Zmiri-tree-borrows, which has proper support for references that only cover the "header" of a struct. Tree Borrows should still find all UB bugs that can affect your code today, but in the future it's likely we are going to pick aliasing rules that are stricter than Tree Borrows (and weaker than Stacked Borrows).

I used extern types because it's what URLO suggested to accurately represent my semantics. it's a DST that exists behind a thin pointer. unfortunatly the "unknown alignment" part isn't really what i wanted. apparently tree borrows are weak enough that it allows writing to a zero-sized field past the end of a struct, so long as its backed by a larger allocation.

from rust.

Related Issues (20)

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.