lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241107020831.1561063-1-alistair.francis@wdc.com>
Date: Thu,  7 Nov 2024 12:08:18 +1000
From: Alistair Francis <alistair23@...il.com>
To: alex.gaynor@...il.com,
	benno.lossin@...ton.me,
	gary@...yguo.net,
	linux-kernel@...r.kernel.org,
	ojeda@...nel.org,
	rust-for-linux@...r.kernel.org,
	bjorn3_gh@...tonmail.com,
	alistair.francis@....com,
	me@...enk.dev,
	a.hindborg@...nel.org,
	tmgross@...ch.edu,
	boqun.feng@...il.com,
	aliceryhl@...gle.com
Cc: alistair23@...il.com
Subject: [PATCH 00/13] rust: bindings: Auto-generate inline static functions

The kernel includes a large number of static inline functions that are
defined in header files. One example is the crypto_shash_descsize()
function which is defined in hash.h as

```
static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
{
        return tfm->descsize;
}
```

bindgen is currently unable to generate bindings to these functions as
they are not publically exposed (they are static after all).

The Rust code currently uses rust_helper_* functions, such as
rust_helper_alloc_pages() for example to call the static inline
functions. But this is a hassle as someone needs to write a C helper
function.

Instead we can use the bindgen wrap-static-fns feature. The feature
is marked as experimental, but has recently been promoted to
non-experimental (dependig on your version of bindgen).

By supporting wrap-static-fns we automatically generate a C file called
extern.c that exposes the static inline functions, for example like this

```
unsigned int crypto_shash_descsize__extern(struct crypto_shash *tfm) { return crypto_shash_descsize(tfm); }
```

The nice part is that this is auto-generated.

We then also get a bindings_generate_static.rs file with the Rust
binding, like this

```
extern "C" {
    #[link_name = "crypto_shash_descsize__extern"]
    pub fn crypto_shash_descsize(tfm: *mut crypto_shash) -> core::ffi::c_uint;
}
```

So now we can use the static inline functions just like normal
functions.

There are a bunch of static inline functions that don't work though, because
the C compiler fails to build extern.c:
 * functions with inline asm generate "operand probably does not match constraints"
   errors (rip_rel_ptr() for example)
 * functions with bit masks (u32_encode_bits() and friends) result in
   "call to ‘__bad_mask’ declared with attribute error: bad bitfield mask"
   errors

As well as that any static inline function that calls a function that has been
kconfig-ed out will fail to link as the function being called isn't built
(mdio45_ethtool_gset_npage for example)

Due to these failures we use a allow-list system (where functions must
be manually enabled).

This series adds support for bindgen generating wrappers for inline statics and
then converts the existing helper functions to this new method. This doesn't
work for C macros, so we can't reamove all of the helper functions, but we
can remove most.

Alistair Francis (13):
  rust: bindings: Support some inline static functions
  rust: helpers: Remove blk helper
  rust: helpers: Remove build_bug helper
  rust: helpers: Remove err helper
  rust: helpers: Remove kunit helper
  rust: helpers: Remove mutex helper
  rust: helpers: Remove some page helpers
  rust: helpers: Remove rbtree helper
  rust: helpers: Remove some refcount helpers
  rust: helpers: Remove signal helper
  rust: helpers: Remove some spinlock helpers
  rust: helpers: Remove some task helpers
  rust: helpers: Remove uaccess helpers

 rust/.gitignore                 |  2 ++
 rust/Makefile                   | 36 ++++++++++++++++++++++++++++++---
 rust/bindgen_static_functions   | 31 ++++++++++++++++++++++++++++
 rust/bindings/bindings_helper.h |  5 +++++
 rust/bindings/lib.rs            |  4 ++++
 rust/exports.c                  |  1 +
 rust/helpers/blk.c              | 14 -------------
 rust/helpers/build_bug.c        |  9 ---------
 rust/helpers/err.c              | 19 -----------------
 rust/helpers/helpers.c          | 13 ++----------
 rust/helpers/kunit.c            |  9 ---------
 rust/helpers/mutex.c            | 15 --------------
 rust/helpers/page.c             |  5 -----
 rust/helpers/rbtree.c           |  9 ---------
 rust/helpers/refcount.c         | 10 ---------
 rust/helpers/signal.c           |  9 ---------
 rust/helpers/spinlock.c         | 10 ---------
 rust/helpers/task.c             | 10 ---------
 rust/helpers/uaccess.c          | 15 --------------
 19 files changed, 78 insertions(+), 148 deletions(-)
 create mode 100644 rust/bindgen_static_functions
 delete mode 100644 rust/helpers/blk.c
 delete mode 100644 rust/helpers/build_bug.c
 delete mode 100644 rust/helpers/err.c
 delete mode 100644 rust/helpers/kunit.c
 delete mode 100644 rust/helpers/mutex.c
 delete mode 100644 rust/helpers/rbtree.c
 delete mode 100644 rust/helpers/signal.c
 delete mode 100644 rust/helpers/uaccess.c

-- 
2.47.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ