From 41f4133e1638629f03097fa20dae67eec8639ee1 Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Wed, 27 Apr 2022 14:18:37 -0700 Subject: Build Rust bindings Converts the CMake build for Rust into an Android.bp one. Resulting Rust bindings are only available to the `openssl` crate, as they are raw bindings and should have a safe wrapper before anyone else uses them. Bug: 177080016 Test: mm Change-Id: I81e85b663c6ac7539395b54dad7e224e2b3f5433 Merged-In: I81e85b663c6ac7539395b54dad7e224e2b3f5433 (cherry picked from commit aa347ab80ee55e0594c1988abf6fbcfb5cd80a11) --- Android.bp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rust/src/lib.rs | 4 ++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Android.bp b/Android.bp index 84c70c72..3629a11c 100644 --- a/Android.bp +++ b/Android.bp @@ -566,3 +566,63 @@ cc_binary { "src/util/fipstools/test_fips.c", ], } + +// Rust bindings +rust_bindgen { + name: "libbssl_sys_raw", + source_stem: "bindings", + crate_name: "bssl_sys_raw", + host_supported: true, + wrapper_src: "src/rust/wrapper.h", + bindgen_flags: [ + "--no-derive-default", + "--enable-function-attribute-detection", + "--use-core", + "--size_t-is-usize", + "--default-macro-constant-type=signed", + "--rustified-enum=point_conversion_form_t", + // These are not BoringSSL symbols, they are from glibc + // and are not relevant to the build besides throwing warnings + // about their 'long double' (aka u128) not being FFI safe. + // We block those functions so that the build doesn't + // spam warnings. + // + // https://github.com/rust-lang/rust-bindgen/issues/1549 describes the current problem + // and other folks' solutions. + "--blocklist-function=strtold", + "--blocklist-function=qecvt", + "--blocklist-function=qecvt_r", + "--blocklist-function=qgcvt", + "--blocklist-function=qfcvt", + "--blocklist-function=qfcvt_r", + ], + shared_libs: [ + "libcrypto", + "libssl", + ], +} + +cc_library_static { + name: "libbssl_rust_support", + host_supported: true, + defaults: ["boringssl_flags"], + srcs: ["src/rust/rust_wrapper.c"], + shared_libs: [ + "libcrypto", + "libssl", + ], +} + +rust_library { + name: "libbssl_ffi", + host_supported: true, + crate_name: "bssl_ffi", + visibility: ["//external/rust/crates/openssl"], + srcs: ["src/rust/src/lib.rs"], + // Since libbssl_sys_raw is not publically visible, we can't + // accidentally force a double-link by linking statically, so do so. + rlibs: ["libbssl_sys_raw"], + static_libs: [ + "libbssl_rust_support", + ], +} diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index d8c2c000..b691baba 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -2,8 +2,8 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] -// populated by cmake -${INCLUDES} +// ANDROID: Use Soong-generated bindings rather than CMake-generated +pub use bssl_sys_raw::*; pub fn ERR_GET_LIB(packed_error: u32) -> i32 { unsafe { ERR_GET_LIB_RUST(packed_error) } -- cgit v1.2.3 From b034aa0d4ed11513326083a3496add8a37ab987c Mon Sep 17 00:00:00 2001 From: Paul Crowley Date: Mon, 16 May 2022 21:46:39 +0000 Subject: Allow prng_seeder utility Give //system/security/prng_seeder access to ctrdrbg.h Bug: 243933553 Test: Started under init and verified correct operation using strace Change-Id: If18124d50d97d93541e5c040b506484a48ee40ac Merged-In: If18124d50d97d93541e5c040b506484a48ee40ac (cherry picked from commit 4ea336a10142019b185fffd2f800fb02e5f2d46e) --- Android.bp | 10 ++++++++-- src/rust/wrapper.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Android.bp b/Android.bp index 3629a11c..44e46e22 100644 --- a/Android.bp +++ b/Android.bp @@ -617,8 +617,14 @@ rust_library { name: "libbssl_ffi", host_supported: true, crate_name: "bssl_ffi", - visibility: ["//external/rust/crates/openssl"], - srcs: ["src/rust/src/lib.rs"], + visibility: [ + "//external/rust/crates/openssl", + "//system/keymint/boringssl", + "//system/security/prng_seeder", + ], + // Use the modified source with placeholder replaced. + srcs: [":libbssl_sys_src"], + vendor_available: true, // Since libbssl_sys_raw is not publically visible, we can't // accidentally force a double-link by linking statically, so do so. rlibs: ["libbssl_sys_raw"], diff --git a/src/rust/wrapper.h b/src/rust/wrapper.h index aa5aeedb..ff466423 100644 --- a/src/rust/wrapper.h +++ b/src/rust/wrapper.h @@ -18,6 +18,7 @@ #include "../include/openssl/conf.h" #include "../include/openssl/cpu.h" #include "../include/openssl/crypto.h" +#include "../include/openssl/ctrdrbg.h" #include "../include/openssl/curve25519.h" #include "../include/openssl/des.h" #include "../include/openssl/dh.h" -- cgit v1.2.3 From 94eca3d919e5d6adcabb71ef2016a95d3f49805c Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Mon, 9 May 2022 13:07:42 +0100 Subject: Make crate vendor_available Bug: 195310053 Test: build, TreeHugger Change-Id: I7dbfd285a11b7a08f84943276d9525782c33dd56 Merged-In: I7dbfd285a11b7a08f84943276d9525782c33dd56 (cherry picked from commit 7b2ddebcee185ecd7b5cb80d6c898797a6296492) --- Android.bp | 1 + 1 file changed, 1 insertion(+) diff --git a/Android.bp b/Android.bp index 44e46e22..99617760 100644 --- a/Android.bp +++ b/Android.bp @@ -574,6 +574,7 @@ rust_bindgen { crate_name: "bssl_sys_raw", host_supported: true, wrapper_src: "src/rust/wrapper.h", + vendor_available: true, bindgen_flags: [ "--no-derive-default", "--enable-function-attribute-detection", -- cgit v1.2.3 From 1364953c681d19461b5bf3f291cc336c1ec0f625 Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Wed, 2 Feb 2022 17:04:17 +0000 Subject: Use genrule to pull in bindgen-erated source Rather than having a local modification of the upstream Rust source, instead use a genrule to replace the `${INCLUDES}` placeholder with a re-export of everything from the bindgen-created code. This change means that the upstream BoringSSL source no longer has a local modification, making it easier to import new versions. Also add a rust_test for the bindgen layout tests along the way. Bug: 177080016 Test: build targets, atest libbssl_sys_raw_test Change-Id: Iabf1b6ac4ae7d3a391cc038b29f157347fb36f2f Merged-In: Iabf1b6ac4ae7d3a391cc038b29f157347fb36f2f (cherry picked from commit 71257a0a0347296eba49f1ca39c0e6b024243ffb) --- Android.bp | 24 ++++++++++++++++++++++++ TEST_MAPPING | 7 +++++++ src/rust/src/lib.rs | 4 ++-- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 TEST_MAPPING diff --git a/Android.bp b/Android.bp index 99617760..40acf086 100644 --- a/Android.bp +++ b/Android.bp @@ -576,6 +576,8 @@ rust_bindgen { wrapper_src: "src/rust/wrapper.h", vendor_available: true, bindgen_flags: [ + // Adapted from upstream the src/rust/CMakeLists.txt file at: + // https://boringssl.googlesource.com/boringssl/+/refs/heads/master/rust/CMakeLists.txt "--no-derive-default", "--enable-function-attribute-detection", "--use-core", @@ -603,6 +605,21 @@ rust_bindgen { ], } +// Encapsulate the bindgen-generated layout tests as a test target. +rust_test { + name: "libbssl_sys_raw_test", + srcs: [ + ":libbssl_sys_raw", + ], + crate_name: "bssl_sys_raw_test", + test_suites: ["general-tests"], + auto_gen_config: true, + clippy_lints: "none", + lints: "none", +} + +// Rust's bindgen doesn't cope with macros, so this target includes C functions that +// do the same thing as macros defined in BoringSSL header files. cc_library_static { name: "libbssl_rust_support", host_supported: true, @@ -614,6 +631,13 @@ cc_library_static { ], } +// Replace the upstream CMake placeholder with a re-export of all of the local bindgen output. +gensrcs { + name: "libbssl_sys_src", + srcs: ["src/rust/src/lib.rs"], + cmd: "sed 's@^.{INCLUDES}@pub use bssl_sys_raw::*;@' $(in) > $(out)", +} + rust_library { name: "libbssl_ffi", host_supported: true, diff --git a/TEST_MAPPING b/TEST_MAPPING new file mode 100644 index 00000000..ce976386 --- /dev/null +++ b/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "presubmit": [ + { + "name": "libbssl_sys_raw_test" + } + ] +} diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index b691baba..d8c2c000 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -2,8 +2,8 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] -// ANDROID: Use Soong-generated bindings rather than CMake-generated -pub use bssl_sys_raw::*; +// populated by cmake +${INCLUDES} pub fn ERR_GET_LIB(packed_error: u32) -> i32 { unsafe { ERR_GET_LIB_RUST(packed_error) } -- cgit v1.2.3