Skip to content

sparc issue when calling C++ functions which returns >16bytes structure #46679

Closed
@psumbera

Description

@psumbera

I have an issue with rust (now version 1.22.1) on Solaris sparc (64bit). On intel I don't see it. I originally see it as part of Firefox build (https://bugzilla.mozilla.org/show_bug.cgi?id=1413887)

I now believe it's rustc issue (though it could be still issue with clang-sys). Any help would be greatly appreciated.

mkdir -p test-clang/src
cat > test-clang/Cargo.toml <<EOF
[package]
name = "test-clang"
version = "0.1.0"
authors = ["Petr Sumbera <petr.sumbera@oracle.com>"]

[dependencies]
clang-sys = "0.21.1"
libc = "0.2.14"
EOF

cat > test-clang/src/main.rs <<EOF
extern crate libc;
use libc::{c_char};
extern crate clang_sys;
use clang_sys::*;

fn main() {
    unsafe {
      let index = clang_createIndex(0, 0);
      let tu = clang_createTranslationUnit(index, "tests/header.h".as_ptr() as *const c_char);
      println!("tu is: {:p}", tu);
      clang_getTranslationUnitCursor(tu);
     }
}
EOF

cd test-clang
LIBCLANG_PATH=/usr/lib/64 cargo build
LIBCLANG_PATH=/usr/lib/64 cargo run

Run output looks like:

LIBCLANG_PATH=/usr/lib/64 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/test-clang`
tu is: 0x0
Segmentation Fault (core dumped)

Stack:

test-clang:core> $C
ffffffff7fffe0d1 libclang.so.5.0`clang_getTranslationUnitCursor+0x44(ffffffff7fffeb20, 0, 0, 1, 1001f4800, ffffffff7c604438)
ffffffff7fffe1e1 test_clang::main::h39d0f3581df1cb39+0xfc(ffffffff7fffeac8, 100071bc8, 10, 1001f4800, 0, 0)
ffffffff7fffe351 std::sys_common::backtrace::__rust_begin_short_backtrace::h6a22156fb88cf7c0+4(100071f34, 0, bc00, ffffffff7c227948, ffffffff7c226000, 0)
ffffffff7fffe401 std::panicking::try::do_call::h10d3852066fc0682+8(ffffffff7fffee50, 0, 1, 0, 0, 0)
ffffffff7fffe4b1 __rust_maybe_catch_panic+0x14(1000b93ec, ffffffff7fffee88, ffffffff7fffeef0, ffffffff7fffeea8, 1001f4800, 0)
ffffffff7fffe591 std::rt::lang_start::h6e897821881fc078+0x458(ffffffff7fffeea8, 10020dec8, 1, 1, 1, 10022e440)
ffffffff7fffe701 main+0x48(8, ffffffff7ffff088, 1001f4800, 1, ffffffff7ffff088, 1)
ffffffff7fffe7c1 _start+0x64(0, 0, 0, 0, 0, 0)

With tracing:

$ apptrace -o log ./target/debug/test-clang
tu is: 0x0

apptrace: ./target/debug/test-clang: Segmentation Fault(Core dump)

tu is: 0x0
Segmentation Fault (core dumped)

$ grep clang_ log
-> test-clang -> libclang.so.5:clang_createIndex(0x0, 0x0, 0x2) ** NR
-> test-clang -> libclang.so.5:clang_createTranslationUnit(0x100230f40, 0x1000577b4, 0xe) ** NR
-> test-clang -> libclang.so.5:clang_getTranslationUnitCursor(0xffffffff7fffeea0, 0x0, 0x0) ** NR

Note that clang_getTranslationUnitCursor isn't called with 'tu' set to 0.

I have find out that the issue seems to be with the size of CXCursor structure which is returned:

CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU);

CXCursor is 32 bytes long. But if I tell clang-sys it's 16 bytes everything is perfect (when it's 17 bytes or more it causes the trouble):

diff --git a/src/lib.rs b/src/lib.rs
index b750b9d..05aaa88 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1103,9 +1103,10 @@ default!(CXCompletionResult);
 #[derive(Copy, Clone, Debug)]
 #[repr(C)]
 pub struct CXCursor {
-    pub kind: CXCursorKind,
-    pub xdata: c_int,
-    pub data: [*const c_void; 3],
+    pub x1: [u8; 16],
+//    pub kind: CXCursorKind,
+//    pub xdata: c_int,
+//    pub data: [*const c_void; 3],
 }

Following is disassembly when CXCursor is pretended to be just 16 bytes:

_ZN10test_clang4main17heae3a929f5bb6356E+0x58:  stx       %o0, [%fp + 0x7b7]
_ZN10test_clang4main17heae3a929f5bb6356E+0x5c:  ldx       [%fp + 0x7bf], %o0
_ZN10test_clang4main17heae3a929f5bb6356E+0x60:  call      +0x18553c     <PLT=libclang.so.5.0`clang_createTranslationUnit>
_ZN10test_clang4main17heae3a929f5bb6356E+0x64:  ldx       [%fp + 0x7b7], %o1
_ZN10test_clang4main17heae3a929f5bb6356E+0x68:  ba        +0x8          <_ZN10test_clang4main17heae3a929f5bb6356E+0x70>
_ZN10test_clang4main17heae3a929f5bb6356E+0x6c:  stx       %o0, [%fp + 0x7d7]   // (*)
_ZN10test_clang4main17heae3a929f5bb6356E+0x70:  call      +0x18554c     <PLT=libclang.so.5.0`clang_getTranslationUnitCursor>
_ZN10test_clang4main17heae3a929f5bb6356E+0x74:  ldx       [%fp + 0x7d7], %o0   // corectly sets what was returned from clang_createTranslationUnit (*)
_ZN10test_clang4main17heae3a929f5bb6356E+0x78:  stx       %o1, [%fp + 0x7f7]
_ZN10test_clang4main17heae3a929f5bb6356E+0x7c:  stx       %o0, [%fp + 0x7ef]

And now with 17 bytes:

_ZN10test_clang4main17heae3a929f5bb6356E+0x58:  stx       %o0, [%fp + 0x7bf]
_ZN10test_clang4main17heae3a929f5bb6356E+0x5c:  ldx       [%fp + 0x7c7], %o0
_ZN10test_clang4main17heae3a929f5bb6356E+0x60:  call      +0x18553c     <PLT:clang_createTranslationUnit>
_ZN10test_clang4main17heae3a929f5bb6356E+0x64:  ldx       [%fp + 0x7bf], %o1
_ZN10test_clang4main17heae3a929f5bb6356E+0x68:  ba        +0x8          <_ZN10test_clang4main17heae3a929f5bb6356E+0x70>
_ZN10test_clang4main17heae3a929f5bb6356E+0x6c:  stx       %o0, [%fp + 0x7df]
_ZN10test_clang4main17heae3a929f5bb6356E+0x70:  ldx       [%fp + 0x7df], %o1
_ZN10test_clang4main17heae3a929f5bb6356E+0x74:  call      +0x185548     <PLT:clang_getTranslationUnitCursor>
_ZN10test_clang4main17heae3a929f5bb6356E+0x78:  add       %fp, 0x7e7, %o0
_ZN10test_clang4main17heae3a929f5bb6356E+0x7c:  ba        +0x8          <_ZN10test_clang4main17heae3a929f5bb6356E+0x84>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.O-SPARCTarget: SPARC processors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions