Semantics of function pointers with equal addresses?

Abierto
#589 17 comentarios 2 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
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
rust
Área
compilers

Línea de trabajo

Start with the two Rust functions in the issue and reproduce their deduplication using the linked Godbolt example. Trace the implications of converting their equal addresses through usize and consider the analogous deduplicated-vtable case. Done means reaching and documenting a resolved language-semantics position for these cases.

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

Descripción

Consider the following code:

#[unsafe(no_mangle)]
fn foo(x: *mut i32, y: *mut i32) {
    unsafe {
        let a = &mut *x;
        let b = &mut *y;
        *a = *b;
    }
}

#[unsafe(no_mangle)]
fn bar(x: *mut i32, y: *mut i32) {
    unsafe {
        x.write(y.read());
    }
}

On rust version 1.91.0, the above code compiles to this assembly (Godbolt link):

bar:
        mov     eax, dword ptr [rsi]
        mov     dword ptr [rdi], eax
        ret

foo = bar

That is, the two functions are deduplicated, and therefore have the same address. This happened even though these two functions have different behavior in the abstract machine: the foo function has UB if x and y alias, while the bar function doesn't.

Suppose that some code casts these two functions into usize addresses (which are equal to each other), does some operation with those two usizes, and then transmutes an equal usize back into a function pointer. When calling that created function pointer, what is the behavior of that call? Does the call behave like calling foo or calling bar?

In theory, we probably also want (in the future) to potentially allow LLVM to deduplicate functions with incompatible ABIs, as long as their generated assembly are identical. This complicates the above question even further.

A similar question applies to deduplicated vtables.

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.