Skip to content

Commit f5c9214

Browse files
authored
Try #252:
2 parents aa500d0 + baef46b commit f5c9214

File tree

13 files changed

+204
-323
lines changed

13 files changed

+204
-323
lines changed

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
[workspace]
22
members = ["mbedtls", "mbedtls-sys"]
3+
4+
[patch.crates-io]
5+
# This patch resolves a local dependency conflict between `mbedtls` and `mbedtls-selftest`.
6+
# Both of these crates require the native library `mbedtls-sys-auto`.
7+
# By default, Cargo chooses the `mbedtls-sys-auto` version from crates.io for `mbedtls-selftest`,
8+
# which conflicts with the local `mbedtls-sys-auto` used by `mbedtls`.
9+
# This patch ensures the local `mbedtls-sys-auto` is used for both crates.
10+
mbedtls-sys-auto = { path = "./mbedtls-sys" }

mbedtls/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mbedtls"
3-
version = "0.8.2"
3+
version = "0.8.3"
44
authors = ["Jethro Beekman <[email protected]>"]
55
build = "build.rs"
66
edition = "2018"
@@ -38,9 +38,12 @@ chrono = "0.4"
3838
[dependencies.mbedtls-sys-auto]
3939
version = "2.25.0"
4040
default-features = false
41-
features = ["custom_printf", "trusted_cert_callback", "threading"]
41+
features = ["trusted_cert_callback", "threading"]
4242
path = "../mbedtls-sys"
4343

44+
[dependencies.mbedtls-selftest]
45+
version = "0.1"
46+
4447
[dev-dependencies]
4548
libc = "0.2.0"
4649
rand = "0.4.0"
@@ -55,7 +58,7 @@ cc = "1.0"
5558
[features]
5659
# Features are documented in the README
5760
default = ["std", "aesni", "time", "padlock"]
58-
std = ["mbedtls-sys-auto/std", "serde/std", "yasna"]
61+
std = ["mbedtls-sys-auto/std", "serde/std", "yasna", "mbedtls-selftest/std"]
5962
debug = ["mbedtls-sys-auto/debug"]
6063
no_std_deps = ["core_io", "spin"]
6164
force_aesni_support = ["mbedtls-sys-auto/custom_has_support", "mbedtls-sys-auto/aes_alt", "aesni"]

mbedtls/build.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,23 @@
99
use std::collections::{HashMap, HashSet};
1010
use std::env;
1111

12+
/// Return the crate hash that Cargo will be passing to `rustc -C metadata=`.
13+
// If there's a panic in this code block, that means Cargo's way of running the
14+
// build script has changed, and this code should be updated to handle the new
15+
// case.
16+
fn get_compilation_metadata_hash() -> String {
17+
let out_dir: std::path::PathBuf = std::env::var_os("OUT_DIR").unwrap().into();
18+
let mut out_dir_it = out_dir.iter().rev();
19+
assert_eq!(out_dir_it.next().unwrap(), "out");
20+
let crate_ = out_dir_it.next().unwrap().to_string_lossy();
21+
assert!(crate_.starts_with("mbedtls-"));
22+
crate_[8..].to_owned()
23+
}
24+
1225
fn main() {
26+
let metadata_hash = get_compilation_metadata_hash();
27+
println!("cargo:rustc-env=RUST_MBEDTLS_METADATA_HASH={}", metadata_hash);
28+
1329
let env_components = env::var("DEP_MBEDTLS_PLATFORM_COMPONENTS").unwrap();
1430
let mut sys_platform_components = HashMap::<_, HashSet<_>>::new();
1531
for mut kv in env_components.split(",").map(|component| component.splitn(2, "=")) {
@@ -24,17 +40,13 @@ fn main() {
2440
let config_file = format!(r#""{}""#, env::var("DEP_MBEDTLS_CONFIG_H").unwrap());
2541
b.define("MBEDTLS_CONFIG_FILE",
2642
Some(config_file.as_str()));
43+
b.define("RUST_MBEDTLS_METADATA_HASH", Some(metadata_hash.as_str()));
2744

2845
b.file("src/mbedtls_malloc.c");
29-
b.file("src/rust_printf.c");
3046
if sys_platform_components.get("c_compiler").map_or(false, |comps| comps.contains("freestanding")) {
3147
b.flag("-U_FORTIFY_SOURCE")
3248
.define("_FORTIFY_SOURCE", Some("0"))
3349
.flag("-ffreestanding");
3450
}
3551
b.compile("librust-mbedtls.a");
36-
// Force correct link order for mbedtls_printf
37-
println!("cargo:rustc-link-lib=static=mbedtls");
38-
println!("cargo:rustc-link-lib=static=mbedx509");
39-
println!("cargo:rustc-link-lib=static=mbedcrypto");
4052
}

mbedtls/src/alloc.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use core::mem::ManuallyDrop;
1515
use mbedtls_sys::types::raw_types::c_void;
1616

1717
extern "C" {
18-
pub(crate) fn forward_mbedtls_free(n: *mut mbedtls_sys::types::raw_types::c_void);
18+
#[link_name = concat!("\u{1}forward_mbedtls_free_", env!("RUST_MBEDTLS_METADATA_HASH"))]
19+
pub(crate) fn mbedtls_free(n: *mut mbedtls_sys::types::raw_types::c_void);
20+
#[link_name = concat!("\u{1}forward_mbedtls_calloc_", env!("RUST_MBEDTLS_METADATA_HASH"))]
21+
pub(crate) fn mbedtls_calloc(n: mbedtls_sys::types::size_t, size: mbedtls_sys::types::size_t) -> *mut mbedtls_sys::types::raw_types::c_void;
1922
}
2023

2124
#[repr(transparent)]
@@ -53,13 +56,15 @@ impl<T> Drop for Box<T> {
5356
fn drop(&mut self) {
5457
unsafe {
5558
drop_in_place(self.inner.as_ptr());
56-
forward_mbedtls_free(self.inner.as_ptr() as *mut c_void)
59+
mbedtls_free(self.inner.as_ptr() as *mut c_void)
5760
}
5861
}
5962
}
6063

64+
unsafe impl<T: Send> Send for Box<T> {}
65+
unsafe impl<T: Sync> Sync for Box<T> {}
66+
6167
#[repr(transparent)]
6268
pub struct List<T> {
6369
pub(crate) inner: Option<Box<T>>
6470
}
65-

mbedtls/src/lib.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub mod ecp;
3838
pub mod hash;
3939
pub mod pk;
4040
pub mod rng;
41-
pub mod self_test;
41+
pub use mbedtls_selftest as self_test;
4242
pub mod ssl;
4343
pub mod x509;
4444
pub mod alloc;
@@ -158,3 +158,69 @@ cfg_if::cfg_if! {
158158
pub unsafe fn set_global_debug_threshold(threshold: i32) {
159159
mbedtls_sys::debug_set_threshold(threshold);
160160
}
161+
162+
#[cfg(test)]
163+
mod tests {
164+
#[allow(dead_code)]
165+
/// Utilities for testing whether types implement certain traits.
166+
///
167+
/// For each trait `Trait` that you want to be able to test, you should
168+
/// implement:
169+
/// ```ignore
170+
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
171+
/// ```
172+
///
173+
/// Then, to test whether a type `Type` implements `Trait`, call:
174+
/// ```ignore
175+
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
176+
/// ```
177+
/// This returns a `bool` indicating whether the trait is implemented.
178+
// This relies on auto-deref to distinguish between types that do and don't
179+
// implement the trait.
180+
mod testtrait {
181+
use core::marker::PhantomData;
182+
183+
pub struct NonImplTrait<T> {
184+
inner: PhantomData<T>
185+
}
186+
187+
pub struct TestTrait<TraitObj: ?Sized, Type> {
188+
non_impl: NonImplTrait<Type>,
189+
phantom: PhantomData<*const TraitObj>,
190+
}
191+
192+
pub trait Testable<T: ?Sized> {}
193+
194+
impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
195+
pub fn new() -> Self {
196+
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
197+
}
198+
}
199+
200+
impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
201+
pub fn impls_trait(&self) -> bool {
202+
true
203+
}
204+
}
205+
206+
impl<T> NonImplTrait<T> {
207+
pub fn impls_trait(&self) -> bool {
208+
false
209+
}
210+
}
211+
212+
impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
213+
type Target = NonImplTrait<Type>;
214+
215+
fn deref(&self) -> &NonImplTrait<Type> {
216+
&self.non_impl
217+
}
218+
}
219+
}
220+
221+
pub use testtrait::{TestTrait, Testable};
222+
223+
impl<T: Send> Testable<dyn Send> for T {}
224+
impl<T: Sync> Testable<dyn Sync> for T {}
225+
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
226+
}

mbedtls/src/mbedtls_malloc.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
#define mbedtls_free free
2222
#endif
2323

24-
extern void *forward_mbedtls_calloc( size_t n, size_t size ) {
24+
// Use several macros to get the preprocessor to actually replace RUST_MBEDTLS_METADATA_HASH
25+
#define append_macro_inner(a, b) a##_##b
26+
#define append_macro(a, b) append_macro_inner(a, b)
27+
#define APPEND_METADATA_HASH(f) append_macro(f, RUST_MBEDTLS_METADATA_HASH)
28+
29+
extern void *APPEND_METADATA_HASH(forward_mbedtls_calloc)( size_t n, size_t size ) {
2530
return mbedtls_calloc(n, size);
2631
}
2732

28-
extern void forward_mbedtls_free( void *ptr ) {
33+
extern void APPEND_METADATA_HASH(forward_mbedtls_free)( void *ptr ) {
2934
mbedtls_free(ptr);
3035
}
31-

mbedtls/src/pk/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extern "C" fn alloc_custom_pk_ctx() -> *mut c_void {
113113
}
114114

115115
unsafe extern "C" fn free_custom_pk_ctx(p: *mut c_void) {
116-
Box::from_raw(p as *mut CustomPkContext);
116+
let _ = Box::from_raw(p as *mut CustomPkContext);
117117
}
118118

119119
extern "C" fn custom_pk_can_do(_t: u32) -> i32 {

mbedtls/src/rust_printf.c

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)