Skip to content

Incorrect code generated for fastcall functions on i686-pc-windows-gnu #91167

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
ricobbe opened this issue Nov 24, 2021 · 3 comments
Open

Incorrect code generated for fastcall functions on i686-pc-windows-gnu #91167

ricobbe opened this issue Nov 24, 2021 · 3 comments
Labels
C-external-bug Category: issue that is caused by bugs in software beyond our control O-x86_32 Target: x86 processors, 32 bit (like i686-*) (IA-32)

Comments

@ricobbe
Copy link
Contributor

ricobbe commented Nov 24, 2021

I tried this code:

#[repr(C)]
#[derive(Clone)]
struct S {
    x: u8,
    y: i32,
}

#[link(name = "extern", kind = "dylib")]
extern "fastcall" {
    fn fastcall_fn(a: S, b: i32);
}

fn main() {
    unsafe {
        fastcall_fn(S { x: 1, y: 2 }, 16);
    }
}

linked against the following C code (compiled to extern.dll):

#include <stdio.h>
#include <stdint.h>

struct S {
    uint8_t x;
    int32_t y;
};

__declspec(dllexport) void __fastcall fastcall_fn(struct S s, int i) {
    printf("fastcall_fn(S { x: %d, y: %d }, %d)\n", s.x, s.y, i);
    fflush(stdout);
}

I expected to see this output:

fastcall_fn(S { x: 1, y: 2 }, 16)

Instead, I got this output:

fastcall_fn(S { x: 1, y: 2 }, 18873345)
fastcall_fn(S { x: 1, y: 2 }, 29491201)

Meta

rustc --version --verbose:

rustc 1.58.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: i686-pc-windows-gnu
release: 1.58.0-dev
LLVM version: 13.0.0

Reproed against commit 7b3cd07 of master.

To reproduce, check out the fastcall-bug branch on my fork of rustc; the reproduction case is in src/test/run-make/fastcall-bug.

@ricobbe ricobbe added the C-bug Category: This is a bug. label Nov 24, 2021
@ricobbe
Copy link
Contributor Author

ricobbe commented Nov 24, 2021

Oh, I forgot: it was suggested that I include my config.toml:

# Includes one of the default files in src/bootstrap/defaults
profile = "compiler"
changelog-seen = 2

[build]
build = "i686-pc-windows-gnu"

[rust]
debug-logging = true
verbose-tests = true

[llvm]
download-ci-llvm = true

Also please note that rustc generates correct code for this case on i686-pc-windows-msvc (with the obvious change to config.toml).

@mati865
Copy link
Contributor

mati865 commented Nov 24, 2021

I don't think it's Rustc issue but either LLVM or GCC. If I'd bet I'd say it's GCC.
I haven't tested your configuration but when I was working on raw-dylib I've got the same issue with GCC but with Clang it worked perfectly.

@ricobbe
Copy link
Contributor Author

ricobbe commented Nov 30, 2021

That makes sense. To be honest, I mostly wanted to make sure this issue is documented. I've got a test case that is currently disabled for unrelated reasons that trips over this bug, so I want to have an issue number that I can put into the comments when I disable the affected functions calls as part of re-enabling the larger test.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 20, 2022
…-fix, r=Mark-Simulacrum

Fix src/test/run-make/raw-dylib-alt-calling-convention

Fix the test headers so that the test now runs on all intended platforms; it is currently ignored on all platforms because the headers are incorrect.  Also comment out a couple of function calls that fail because of an unrelated problem, described in issue rust-lang#91167.
@Noratrieb Noratrieb added O-x86_32 Target: x86 processors, 32 bit (like i686-*) (IA-32) and removed O-x86-all labels Oct 25, 2023
@jieyouxu jieyouxu added C-external-bug Category: issue that is caused by bugs in software beyond our control and removed C-bug Category: This is a bug. needs-triage-legacy labels Nov 12, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-external-bug Category: issue that is caused by bugs in software beyond our control O-x86_32 Target: x86 processors, 32 bit (like i686-*) (IA-32)
Projects
None yet
Development

No branches or pull requests

6 participants