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

bind'ing a native function behaves incorrectly #1174

Closed
jwise opened this issue Nov 13, 2011 · 3 comments
Closed

bind'ing a native function behaves incorrectly #1174

jwise opened this issue Nov 13, 2011 · 3 comments

Comments

@jwise
Copy link
Contributor

jwise commented Nov 13, 2011

The following code is miscompiled on my OS X laptop:

use std;

native "cdecl" mod libc = "" {
    fn printf(s: *u8, a: int); /* A tenuous definition, indeed.  Herp, derp. */
}

fn main() {
    let b = std::str::bytes("%d\n");
    let b8 = unsafe { std::vec::unsafe::to_ptr(b) };
    libc::printf(b8, 4);
    let a = bind libc::printf(b8, 5);
    a(); /* core dump */
}

I also reproed on my Linux machine, in which I captured this GDB session:

(gdb) break printf
Breakpoint 1 at 0x8048b78
(gdb) run
Starting program: /storage/homes/joshua/rust/build/bindbad
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
[Thread debugging using libthread_db enabled]
[New Thread 0xf7917b70 (LWP 15444)]
[Switching to Thread 0xf7917b70 (LWP 15444)]

Breakpoint 1, __printf (format=0x804ca68 "%d\n") at printf.c:29
29      printf.c: No such file or directory.
        in printf.c
(gdb) c
Continuing.
4

Breakpoint 1, __printf (format=0x10 <Address 0x10 out of bounds>) at printf.c:29
29      in printf.c
(gdb)
@nikomatsakis
Copy link
Contributor

This is probably related to issues #1058 and #1059. I have a planned fix for these problems (basically, generate a stub function that follows rust conventions). If I find the time I may put it in today. In the short term, you can do the same: generate a Rust wrapper.

@nikomatsakis
Copy link
Contributor

Indeed, this works fine:

use std;

native "cdecl" mod libc = "" {
    fn printf(s: *u8, a: int); /* A tenuous definition, indeed.  Herp, derp. */
}

fn printf(s: *u8, a: int) {
    libc::printf(s, a);
}

fn main() {
    let b = std::str::bytes("%d\n");
    let b8 = unsafe { std::vec::unsafe::to_ptr(b) };
    printf(b8, 4);
    let a = bind printf(b8, 5);
    a(); /* core dump */
}

@nikomatsakis
Copy link
Contributor

Duplicate of #1058, #1059.

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Nov 19, 2011
nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Nov 19, 2011
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
Naming right now for wasm simd intrinsics takes the signededness of the
instruction into account, but some operations are the same regardless of
signededness, such as `i32x4_add`. This commit adds aliases for all of
these operations under unsigned names as well (such as `u32x4_add`)
which are just a `pub use` to rename the item as two names. The goal of
this is to assist in reading code (no need to switch back and forth
between `i` and `u`) as well as writing code (no need to always remember
which operations are the same for signed/unsigned but only available
under the signed names).
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

No branches or pull requests

2 participants