Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Bindgen produces incorrect bindings to functions with 80-bit long doubles (f80) #2378

Open
GKFX opened this issue Dec 31, 2022 · 2 comments · May be fixed by #2403
Open

Bindgen produces incorrect bindings to functions with 80-bit long doubles (f80) #2378

GKFX opened this issue Dec 31, 2022 · 2 comments · May be fixed by #2403

Comments

@GKFX
Copy link
Contributor

GKFX commented Dec 31, 2022

Input C

// float_to_ld.h
long double float_to_ld(float f);

// float_to_ld.c
void test1(void);

long double float_to_ld(float f) {
    return f;
}

int main() {
    test1();
    return 0;
}

Bindgen Invocation

bindgen float_to_ld.h

Actual Results

extern "C" {
    pub fn float_to_ld(f: f32) -> u128;
}

#[no_mangle]
extern "C" fn test1() {
    let mut buf = [0; 10];
    for (i, x) in buf.iter_mut().enumerate() {
        *x ^= unsafe {
            float_to_ld(i as f32)
        };
    }
    println!("{buf:X?}");
}
[7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824, 7FFDF7F781F800005568AD1E4824]

This code fills up and overflows the x87 floating point stack, which doesn't crash the application but results in the stack containing nonsense values rather than the values 8.0, 9.0 expected. (This can be observed with gdb -ex start -ex 'layout asm' -ex 'tui reg float' float_to_ld.) Also the return values in the buffer are not the actual return values of the function as the wrong registers are being used.

As a more minor annoyance, even if the functions aren't used, rustc emits the warning note: 128-bit integers don't currently have a known stable ABI.

Expected Results

Either no bindings should be generated, or inline wrapper functions using inline assembly should be generated which correctly implement the calling convention. Given that Rust has no support for f80, simply omitting the bindings seems like the most pragmatic solution.

@GKFX GKFX changed the title Bindgen should not produce bindings to functions with 80-bit long doubles (f80) Bindgen produces incorrect bindings to functions with 80-bit long doubles (f80) Dec 31, 2022
@pvdrz
Copy link
Contributor

pvdrz commented Jan 5, 2023

Hmmm... interesting. Could it be that we're assuming that float is f32 everywhere? I'll try to check this.

@pvdrz
Copy link
Contributor

pvdrz commented Feb 1, 2023

Related to #1851

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants