rust-lang / rust-lang/rust-bindgen

Functions that use vtables get `improper_ctypes` warning

Open
#2,699 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
struct TABLE
{
  TABLE() = default;

  TABLE_SHARE	*s;
  handler	*file;
  TABLE *next, *prev;
  // ... it's a very long class, I can provide more information if needed
}
Bindgen Invocation
    bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("src/wrapper.h")
        // Fix math.h double defines
        .parse_callbacks(Box::new(BuildCallbacks))
        .clang_args(incl_args)
        .clang_arg("-xc++")
        .clang_arg("-std=c++17")
        // Don't derive copy for structs
        .derive_copy(false)
        // Use `core::ffi` instead of `std::os::raw`
        .use_core()
        // Will be required in a future version of `rustc`
        .wrap_unsafe_ops(true)
        // Use rust-style enums labeled with non_exhaustive to represent C enums
        .default_enum_style(EnumVariation::Rust {
            non_exhaustive: true,
        })
        .vtable_generation(true)
        .allowlist_item(...)
        .generate()
        .map_err(Into::into)

Actual Results
warning: `extern` block uses type `Handler_share__bindgen_vtable`, which is not FFI-safe
     --> /Users/tmgross/Documents/projects/mdb/mariadb-server/rust/target/debug/build/mariadb-sys-b864f0925472491e/out/bindings.rs:27016:16
      |
27016 |         table: *mut TABLE,
      |                ^^^^^^^^^^ not FFI-safe
      |
      = help: consider adding a member to this struct
      = note: this struct has no fields
note: the type is defined here
     --> /Users/tmgross/Documents/projects/mdb/mariadb-server/rust/target/debug/build/mariadb-sys-b864f0925472491e/out/bindings.rs:19773:1
      |
19773 | pub struct Handler_share__bindgen_vtable {}

This comes from the following:

#[repr(C)]
pub struct Handler_share__bindgen_vtable {}
#[doc = " Base class to be used by handlers different shares"]
#[repr(C)]
#[derive(Debug)]
pub struct Handler_share {
    pub vtable_: *const Handler_share__bindgen_vtable,
}

// Omitting a few levels of indirection through which a `struct TABLE` contains
// a `*mut *mut Handler_share`, can provide if needed.

extern "C" {
    #[link_name = "\u{1}__ZN19TABLE_STATISTICS_CB21update_stats_in_tableEP5TABLE"]
    pub fn TABLE_STATISTICS_CB_update_stats_in_table(
        this: *mut TABLE_STATISTICS_CB,
        table: *mut TABLE,
    );
}
Expected Results

There shouldn't be a warning.

I think this could be worked around by adding the [u8; 0] value to the struct, or maybe c_void. Otherwise, adding #[allow(improper_ctypes)] to the calling function would work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning from src/wrapper.h using the shown bindgen::Builder invocation, especially vtable_generation(true), and inspect the generated bindings.rs definitions for Handler_share__bindgen_vtable and Handler_share. Compare the empty vtable representation with the extern function signature. Done means vtable-enabled output no longer emits the improper_ctypes warning without requiring an allow attribute.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.