Are all referents contiguous?
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 20/100
Línea de trabajo
Start with the as_bytes example and its safety comment. Investigate whether Rust's reference model guarantees contiguous referents, including the non-contiguous case described and its implications for unsafe code. Done means a resolved, documented answer to the question, rather than an isolated code change.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Consider the following code:
use zerocopy::{Immutable, IntoBytes};
fn as_bytes<T: ?Sized + IntoBytes + Immutable>(t: &T) -> &[u8] {
let len = size_of_val(t);
let ptr: *const T = t;
// SAFETY:
// - By `T: IntoBytes`, all of the bytes in `*t` are initialized to valid `u8`s
// - By `T: Immutable`, `T` does not permit interior mutation, and neither does `[u8]`
// - The constructed pointer's referent has the same bytes as `*t`. By invariant on `&T`,
// this means that the referent is either zero-sized or lives in a single allocation which
// lives for at least as long as the lifetime of the input/output references to this function.
unsafe { core::ptr::slice_from_raw_parts(ptr.cast::<u8>(), len) }
}
This safety comment implicitly assumes that *t is contiguous. If it isn't (e.g., if it contains bytes at addresses 1 and 3 but not 2), then the safety comment is wrong, and the code is unsound since the returned reference refers to bytes which may not be allocated, may not be initialized, may be subject to interior mutation, etc.
This makes me think that we basically need to guarantee that all pointers refer to contiguous sets of bytes, or else tons of unsafe code is unsound, and it would be very difficult to write sound unsafe code. Every time you did a transmute, you'd need to reason about whether the referents of the source and destination types had the same "holes".
Should we just bite the bullet and guarantee that all referents are contiguous in memory? Have we already guaranteed this somewhere that I'm not aware of?
cc @jswrenn
- Lenguaje dominante
- Sin datos de lenguaje
- Estrellas
- 760
- Forks
- 68
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de rust-lang/unsafe-code-guidelines
-
I-opsem-nominated
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
rust-lang/unsafe-code-guidelines#634 · 5 comentarios ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 15/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 12/100
rust-lang/unsafe-code-guidelines#632 · 8 comentarios · 1 reacción ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 10/100
rust-lang/unsafe-code-guidelines#631 · 7 comentarios ·
-
I-opsem-nominated
Dificultad 5/5 Más de una semana Aptitud para principiantes 20/100
rust-lang/unsafe-code-guidelines#630 · 1 reacción ·
Todos los issues de rust-lang/unsafe-code-guidelines
Issues similares
-
bug core-lang
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentenceAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
mw00/project-maya#1368 ·
Los mantenedores suelen responder en 2 días
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
foundry-rs/foundry#18828 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día