Nonzero pointer address spaces are silently erased in generated bindings and static wrappers

Đang mở
#3,404 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
c, cpp, rust
Lĩnh vực
compilers, tooling

Hướng nghiên cứu

Tái hiện vấn đề với input.h, lệnh gọi bindgen được hiển thị, cùng các đầu ra được tạo của bindings.rs và wrapper. Theo dõi cách các không gian địa chỉ khác không được biểu diễn trong các khai báo Rust được tạo và các wrapper C cho hàm static. Hoàn tất nghĩa là đầu ra giữ lại không gian địa chỉ ở nơi được hỗ trợ, hoặc chẩn đoán và bỏ qua một khai báo Rust không thể biểu diễn, đồng thời Clang chấp nhận wrapper.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Input C/C++ Header
static inline int __attribute__((address_space(1))) *
phase8_as1_identity(
    int __attribute__((address_space(1))) *value
) {
    return value;
}

Clang accepts this declaration on the target used below. The function takes
and returns an address space 1 pointer.

Bindgen Invocation
$ bindgen input.h \
    --wrap-static-fns \
    --wrap-static-fns-path wrapper \
    --rust-target 1.75 \
    --no-layout-tests \
    --no-rustfmt-bindings \
    --output bindings.rs
Actual Results

The generated Rust declaration silently uses ordinary pointers:

extern "C" {
    #[link_name = "phase8_as1_identity__extern"]
    pub fn phase8_as1_identity(
        value: *mut ::std::os::raw::c_int,
    ) -> *mut ::std::os::raw::c_int;
}

The generated C wrapper drops the address space too:

int *phase8_as1_identity__extern(int *value) {
    return phase8_as1_identity(value);
}

Clang rejects that wrapper:

error: passing 'int *' to parameter of type
'__attribute__((address_space(1))) int *' changes address space of pointer

The two declarations also lower to different LLVM function types. These are
the signatures from the tools listed below:

; clang-15, correct C wrapper
define dso_local i32 addrspace(1)* @phase8_as1_identity__extern(
    i32 addrspace(1)* %value)

; rustc 1.75, caller using the generated binding
declare noundef ptr @phase8_as1_identity__extern(ptr noundef)
Expected Results

Bindgen should not emit an ordinary pointer for a nonzero address space.

The C wrapper can preserve the source declaration:

int __attribute__((address_space(1))) *
phase8_as1_identity__extern(
    int __attribute__((address_space(1))) *value
) {
    return phase8_as1_identity(value);
}

Stable Rust does not have a general source-level pointer type for arbitrary
LLVM address spaces. If the selected Rust output cannot represent this type,
I would expect bindgen to diagnose it and omit the misleading declaration
instead of changing it to *mut c_int.

Environment
bindgen current main: 9d26c6eddeff9192ddedb563192abe3128fc5aae
bindgen release:      0.72.1
clang:                15.0.7
rustc:                1.75.0
target:               x86_64-unknown-linux-gnu

Current main and 0.72.1 both generate the same invalid wrapper at O0 and O2.

Ngôn ngữ chính
Rust
Star
5.3k
Fork
846
Merge trung bình
2 ngày 6 giờ
Pull request đã merge (30 ngày)
11

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rust-bindgen

Tất cả issue của rust-lang/rust-bindgen

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.