Skip to content

Commit ad5a899

Browse files
committed
Universal bindings
1 parent d120394 commit ad5a899

File tree

9 files changed

+28702
-90
lines changed

9 files changed

+28702
-90
lines changed

aws-lc-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fips = ["dep:aws-lc-fips-sys"]
4747

4848
[dependencies]
4949
untrusted = { workspace = true, optional = true }
50-
aws-lc-sys = { version = "0.32.0", path = "../aws-lc-sys", optional = true }
50+
aws-lc-sys = { version = "0.32.0", path = "../aws-lc-sys", default-features = false, optional = true }
5151
aws-lc-fips-sys = { version = "0.13.1", path = "../aws-lc-fips-sys", optional = true }
5252
zeroize.workspace = true
5353

aws-lc-sys/.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[profile.dev.build-override]
2+
debug = true

aws-lc-sys/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ build = "builder/main.rs"
6060

6161
[features]
6262
asan = []
63-
ssl = ['bindgen']
63+
ssl = ['bindgen', 'all-bindings']
6464
bindgen = ["dep:bindgen"] # Generate the bindings on the targeted platform as a fallback mechanism.
6565
prebuilt-nasm = []
66+
all-bindings = []
67+
default = ['all-bindings']
6668

6769
[build-dependencies]
6870
cmake.workspace = true

aws-lc-sys/builder/main.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,20 @@ fn prefix_string() -> String {
283283
format!("aws_lc_{}", VERSION.to_string().replace('.', "_"))
284284
}
285285

286-
#[cfg(feature = "bindgen")]
286+
#[cfg(all(feature = "bindgen", feature = "all-bindings"))]
287287
fn target_platform_prefix(name: &str) -> String {
288288
format!("{}_{}", effective_target().replace('-', "_"), name)
289289
}
290290

291+
#[cfg(all(feature = "bindgen", not(feature = "all-bindings")))]
292+
fn target_platform_prefix(name: &str) -> String {
293+
if target_vendor() == "apple" {
294+
format!("universal_apple_{}", name.replace('-', "-"))
295+
} else {
296+
format!("universal_{}", name.replace('-', "-"))
297+
}
298+
}
299+
291300
pub(crate) struct TestCommandResult {
292301
#[allow(dead_code)]
293302
stderr: Box<str>,
@@ -355,11 +364,12 @@ fn generate_src_bindings(manifest_dir: &Path, prefix: &Option<String>, src_bindi
355364

356365
fn emit_rustc_cfg(cfg: &str) {
357366
let cfg = cfg.replace('-', "_");
367+
emit_warning(format!("Emitting configuration: cargo:rustc-cfg={cfg}"));
358368
println!("cargo:rustc-cfg={cfg}");
359369
}
360370

361-
fn emit_warning(message: &str) {
362-
println!("cargo:warning={message}");
371+
fn emit_warning<T: AsRef<str>>(message: T) {
372+
println!("cargo:warning={}", message.as_ref());
363373
}
364374

365375
#[allow(dead_code)]
@@ -527,25 +537,39 @@ fn initialize() {
527537
if !is_external_bindgen_requested().unwrap_or(false)
528538
&& (is_pregenerating_bindings() || !has_bindgen_feature())
529539
{
530-
let target = effective_target();
531-
let supported_platform = match target.as_str() {
532-
"aarch64-apple-darwin"
533-
| "aarch64-linux-android"
534-
| "aarch64-pc-windows-msvc"
535-
| "aarch64-unknown-linux-gnu"
536-
| "aarch64-unknown-linux-musl"
537-
| "i686-pc-windows-msvc"
538-
| "i686-unknown-linux-gnu"
539-
| "riscv64gc-unknown-linux-gnu"
540-
| "x86_64-apple-darwin"
541-
| "x86_64-pc-windows-gnu"
542-
| "x86_64-pc-windows-msvc"
543-
| "x86_64-unknown-linux-gnu"
544-
| "x86_64-unknown-linux-musl" => Some(target),
545-
_ => None,
546-
};
547-
if let Some(platform) = supported_platform {
548-
emit_rustc_cfg(platform.as_str());
540+
#[cfg(feature = "all-bindings")]
541+
{
542+
let target = effective_target();
543+
let supported_platform = match target.as_str() {
544+
"aarch64-apple-darwin"
545+
| "aarch64-linux-android"
546+
| "aarch64-pc-windows-msvc"
547+
| "aarch64-unknown-linux-gnu"
548+
| "aarch64-unknown-linux-musl"
549+
| "i686-pc-windows-msvc"
550+
| "i686-unknown-linux-gnu"
551+
| "riscv64gc-unknown-linux-gnu"
552+
| "x86_64-apple-darwin"
553+
| "x86_64-pc-windows-gnu"
554+
| "x86_64-pc-windows-msvc"
555+
| "x86_64-unknown-linux-gnu"
556+
| "x86_64-unknown-linux-musl" => Some(target),
557+
_ => None,
558+
};
559+
if let Some(platform) = supported_platform {
560+
emit_rustc_cfg(platform.as_str());
561+
unsafe {
562+
PREGENERATED = true;
563+
}
564+
}
565+
}
566+
#[cfg(not(feature = "all-bindings"))]
567+
{
568+
if target_vendor() == "apple" {
569+
emit_rustc_cfg("universal-apple");
570+
} else {
571+
emit_rustc_cfg("universal");
572+
}
549573
unsafe {
550574
PREGENERATED = true;
551575
}
@@ -712,6 +736,9 @@ fn main() {
712736
.join("src")
713737
.join(format!("{}.rs", target_platform_prefix("crypto")));
714738
if is_external_bindgen_requested().unwrap_or(false) {
739+
if is_pregenerating_bindings() {
740+
panic!("Pregenrated bindings not supported using external bindgen.")
741+
}
715742
invoke_external_bindgen(&manifest_dir, &prefix, &src_bindings_path).unwrap();
716743
} else {
717744
generate_src_bindings(&manifest_dir, &prefix, &src_bindings_path);

aws-lc-sys/builder/sys_bindgen.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

4-
use crate::{get_rust_include_path, BindingOptions, COPYRIGHT, PRELUDE};
4+
use crate::{emit_warning, get_rust_include_path, BindingOptions, COPYRIGHT, PRELUDE};
55
use bindgen::callbacks::{ItemInfo, ParseCallbacks};
66
use std::fmt::Debug;
77
use std::path::Path;
@@ -31,6 +31,48 @@ impl ParseCallbacks for StripPrefixCallback {
3131
}
3232
}
3333

34+
const ALLOWED_HEADERS: [&str; 29] = [
35+
"aes.h",
36+
"aead.h",
37+
"base.h",
38+
"bn.h",
39+
"boringssl_prefix_symbols.h",
40+
"boringssl_prefix_symbols_asm.h",
41+
"boringssl_prefix_symbols_nasm.inc",
42+
"bytestring.h",
43+
"chacha.h",
44+
"cipher.h",
45+
"crypto.h",
46+
"curve25519.h",
47+
"digest.h",
48+
"ec.h",
49+
"ec_key.h",
50+
"ecdh.h",
51+
"ecdsa.h",
52+
"err.h",
53+
"evp.h",
54+
"hkdf.h",
55+
"hmac.h",
56+
"is_awslc.h",
57+
"kdf.h",
58+
"mem.h",
59+
"nid.h",
60+
"poly1305.h",
61+
"rand.h",
62+
"rsa.h",
63+
"sha.h",
64+
];
65+
66+
const BLOCKED_FUNCTIONS: [&str; 5] = [
67+
"BN_print_fp",
68+
"CBS_parse_generalized_time",
69+
"CBS_parse_utc_time",
70+
"ERR_print_errors_fp",
71+
"RSA_print_fp",
72+
];
73+
74+
const BLOCKED_TYPES: [&str; 4] = ["FILE", "fpos_t", "tm", "__sFILE"];
75+
3476
fn prepare_bindings_builder(manifest_dir: &Path, options: &BindingOptions) -> bindgen::Builder {
3577
let clang_args = crate::prepare_clang_args(manifest_dir, options);
3678

@@ -39,7 +81,6 @@ fn prepare_bindings_builder(manifest_dir: &Path, options: &BindingOptions) -> bi
3981
.derive_debug(true)
4082
.derive_default(true)
4183
.derive_eq(true)
42-
.allowlist_file(r".*(/|\\)openssl((/|\\)[^/\\]+)+\.h")
4384
.allowlist_file(r".*(/|\\)rust_wrapper\.h")
4485
.rustified_enum(r"point_conversion_form_t")
4586
.rust_target(bindgen::RustTarget::stable(70, 0).unwrap())
@@ -59,6 +100,23 @@ fn prepare_bindings_builder(manifest_dir: &Path, options: &BindingOptions) -> bi
59100
.to_string(),
60101
);
61102

103+
if cfg!(feature = "all-bindings") {
104+
builder = builder.allowlist_file(r".*(/|\\)openssl((/|\\)[^/\\]+)+\.h");
105+
} else {
106+
for header in ALLOWED_HEADERS {
107+
emit_warning(format!("Allowed header: {header}").as_str());
108+
builder = builder.allowlist_file(format!("{}{}", r".*(/|\\)openssl(/|\\)", header));
109+
}
110+
for function in BLOCKED_FUNCTIONS {
111+
emit_warning(format!("Blocked function: {function}").as_str());
112+
builder = builder.blocklist_function(function);
113+
}
114+
for tipe in BLOCKED_TYPES {
115+
emit_warning(format!("Opaque type: {tipe}").as_str());
116+
builder = builder.blocklist_type(tipe);
117+
}
118+
}
119+
62120
if !options.disable_prelude {
63121
builder = builder.raw_line(PRELUDE);
64122
}

aws-lc-sys/src/lib.rs

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#![cfg_attr(not(clippy), allow(unexpected_cfgs))]
55
#![cfg_attr(not(clippy), allow(unknown_lints))]
66

7-
use std::os::raw::{c_char, c_long, c_void};
8-
97
#[allow(unused_macros)]
108
macro_rules! use_bindings {
119
($bindings:ident) => {
@@ -15,79 +13,34 @@ macro_rules! use_bindings {
1513
}
1614

1715
macro_rules! platform_binding {
18-
($platform:ident, $platform_crypto:ident, $platform_ssl:ident) => {
16+
($platform:ident, $platform_crypto:ident) => {
1917
#[cfg(all($platform, not(feature = "ssl"), not(use_bindgen_generated)))]
2018
use_bindings!($platform_crypto);
21-
#[cfg(all($platform, feature = "ssl", not(use_bindgen_generated)))]
22-
use_bindings!($platform_ssl);
2319
};
2420
}
2521

26-
platform_binding!(
27-
aarch64_linux_android,
28-
aarch64_linux_android_crypto,
29-
aarch64_linux_android_crypto_ssl
30-
);
31-
platform_binding!(
32-
aarch64_apple_darwin,
33-
aarch64_apple_darwin_crypto,
34-
aarch64_apple_darwin_crypto_ssl
35-
);
36-
platform_binding!(
37-
aarch64_pc_windows_msvc,
38-
aarch64_pc_windows_msvc_crypto,
39-
aarch64_pc_windows_msvc_crypto_ssl
40-
);
41-
platform_binding!(
42-
aarch64_unknown_linux_gnu,
43-
aarch64_unknown_linux_gnu_crypto,
44-
aarch64_unknown_linux_gnu_crypto_ssl
45-
);
22+
platform_binding!(universal_apple, universal_apple_crypto);
23+
platform_binding!(universal, universal_crypto);
24+
25+
platform_binding!(aarch64_linux_android, aarch64_linux_android_crypto);
26+
platform_binding!(aarch64_apple_darwin, aarch64_apple_darwin_crypto);
27+
platform_binding!(aarch64_pc_windows_msvc, aarch64_pc_windows_msvc_crypto);
28+
platform_binding!(aarch64_unknown_linux_gnu, aarch64_unknown_linux_gnu_crypto);
4629
platform_binding!(
4730
aarch64_unknown_linux_musl,
48-
aarch64_unknown_linux_musl_crypto,
49-
aarch64_unknown_linux_musl_crypto_ssl
50-
);
51-
platform_binding!(
52-
i686_pc_windows_msvc,
53-
i686_pc_windows_msvc_crypto,
54-
i686_pc_windows_msvc_crypto_ssl
55-
);
56-
platform_binding!(
57-
i686_unknown_linux_gnu,
58-
i686_unknown_linux_gnu_crypto,
59-
i686_unknown_linux_gnu_crypto_ssl
31+
aarch64_unknown_linux_musl_crypto
6032
);
33+
platform_binding!(i686_pc_windows_msvc, i686_pc_windows_msvc_crypto);
34+
platform_binding!(i686_unknown_linux_gnu, i686_unknown_linux_gnu_crypto);
6135
platform_binding!(
6236
riscv64gc_unknown_linux_gnu,
63-
riscv64gc_unknown_linux_gnu_crypto,
64-
riscv64gc_unknown_linux_gnu_crypto_ssl
65-
);
66-
platform_binding!(
67-
x86_64_apple_darwin,
68-
x86_64_apple_darwin_crypto,
69-
x86_64_apple_darwin_crypto_ssl
70-
);
71-
platform_binding!(
72-
x86_64_pc_windows_gnu,
73-
x86_64_pc_windows_gnu_crypto,
74-
x86_64_pc_windows_gnu_crypto_ssl
75-
);
76-
platform_binding!(
77-
x86_64_pc_windows_msvc,
78-
x86_64_pc_windows_msvc_crypto,
79-
x86_64_pc_windows_msvc_crypto_ssl
80-
);
81-
platform_binding!(
82-
x86_64_unknown_linux_gnu,
83-
x86_64_unknown_linux_gnu_crypto,
84-
x86_64_unknown_linux_gnu_crypto_ssl
85-
);
86-
platform_binding!(
87-
x86_64_unknown_linux_musl,
88-
x86_64_unknown_linux_musl_crypto,
89-
x86_64_unknown_linux_musl_crypto_ssl
37+
riscv64gc_unknown_linux_gnu_crypto
9038
);
39+
platform_binding!(x86_64_apple_darwin, x86_64_apple_darwin_crypto);
40+
platform_binding!(x86_64_pc_windows_gnu, x86_64_pc_windows_gnu_crypto);
41+
platform_binding!(x86_64_pc_windows_msvc, x86_64_pc_windows_msvc_crypto);
42+
platform_binding!(x86_64_unknown_linux_gnu, x86_64_unknown_linux_gnu_crypto);
43+
platform_binding!(x86_64_unknown_linux_musl, x86_64_unknown_linux_musl_crypto);
9144

9245
#[cfg(use_bindgen_generated)]
9346
#[allow(
@@ -138,6 +91,10 @@ pub fn ERR_GET_FUNC(packed_error: u32) -> i32 {
13891
unsafe { ERR_GET_FUNC_RUST(packed_error) }
13992
}
14093

94+
#[cfg(feature = "all-bindings")]
95+
use std::os::raw::{c_char, c_long, c_void};
96+
97+
#[cfg(feature = "all-bindings")]
14198
#[allow(non_snake_case, clippy::not_unsafe_ptr_arg_deref)]
14299
pub fn BIO_get_mem_data(b: *mut BIO, pp: *mut *mut c_char) -> c_long {
143100
unsafe { BIO_ctrl(b, BIO_CTRL_INFO, 0, pp.cast::<c_void>()) }

0 commit comments

Comments
 (0)