Should immutable statics be allowed to overlap?

Abierto
#629 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust
Área
compilers

Línea de trabajo

Read the linked Rust Reference PR #2334 and its discussion, then review the linked LLVM discussion about immutable static overlap and the Zulip thread on linker section slice iteration. Determine what LLVM changes would permit this pattern while preserving the relevant provenance rules; done means the overlap behavior is resolved well enough to address the linkme undefined behavior.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

This came up in https://github.com/rust-lang/reference/pull/2334 which in the end went forward without blessing this pattern:

// Access a "linker section slice" spanned between `whatever.start` and `whatever.end`.
// Assumption: both are usize-aligned and there's an implicit list of usizes stored between them.
unsafe extern "C" {
  #[link_name = "whatever.start"]
  static START: [usize; 0];
  #[link_name = "whatever.end"]
  static END: [usize; 0];
}

/// Call the callback on each element of the "linker section slice".
#[allow(static_mut_refs)] // we immediately decay them to raw pointers
pub fn iter(mut callback: impl FnMut(usize)) { unsafe {
  let mut ptr = START.as_ptr();
  while ptr.addr() != END.as_ptr().addr() {
    callback(ptr.read());
    ptr = ptr.add(1);
  }
} }

The problem is that the statics which are being put into this section overlap with START:

#[link_section = ".whatever"]
#[used]
static MY_PART: usize = 15; // can also be an array of `usize`

That's UB according to the rule that distinct statics must not overlap.
https://github.com/dtolnay/linkme currently has UB due to this. Cc @dtolnay (https://github.com/dtolnay/linkme/issues/133)

To fix this we'd have to get LLVM to allow immutable statics to overlap. There were concerns that this would require changes to LLVM's logic that go beyond what is needed to fix provenance in general, see this example and discussion. Cc @nikic

Also see Zulip: #t-opsem > linker section slice iteration

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

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/unsafe-code-guidelines

Todos los issues de rust-lang/unsafe-code-guidelines

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.