Skip to content

Commit

Permalink
Remove the libc dependency for most platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 18, 2019
1 parent d94b6b5 commit 4c392ad
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,28 @@ name = "ring"

[dependencies]
untrusted = { version = "0.7.0" }
libc = { version = "0.2.48", default-features = false }

[target.'cfg(all(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86", target_arch = "x86_64"), not(target_os = "ios")))'.dependencies]
spin = { version = "0.5.0", default-features = false }


[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
libc = { version = "0.2.48", default-features = false }

[target.'cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd", target_os = "solaris"))'.dependencies]
lazy_static = { version = "1.3", default-features = false, optional = true }

[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown", target_env = ""))'.dependencies]
web-sys = { version = "0.3.25", default-features = false, features = ["Crypto", "Window"] }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.7", default-features = false, features = ["ntsecapi", "wtypesbase"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = { version = "0.2.48", default-features = false }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.7", default-features = false, features = ["ntsecapi", "wtypesbase"] }
[target.'cfg(any(unix, windows))'.dev-dependencies]
libc = { version = "0.2.48", default-features = false }

# Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml.
[build-dependencies]
Expand Down
2 changes: 0 additions & 2 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@
#define ASSERT(x) ((void)0)
#endif

#include <GFp/type_check.h>

#if defined(__GNUC__) && \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
// |alignas| and |alignof| were added in C11. GCC added support in version 4.8.
Expand Down
9 changes: 9 additions & 0 deletions include/GFp/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

// This file should be the first included by all BoringSSL headers.

#include <GFp/type_check.h>

#if defined(_MSC_VER)
#pragma warning(push, 3)
#endif
Expand Down Expand Up @@ -106,5 +108,12 @@
// items.
#define OPENSSL_EXPORT

// `ring::c` would need to be customized on any platform where these assertions
// fail. Keep in sync with `ring::c`.
OPENSSL_STATIC_ASSERT(sizeof(int32_t) == sizeof(int), "int isn't 32 bits.");
OPENSSL_STATIC_ASSERT(sizeof(uint32_t) == sizeof(unsigned int), "unsigned int isn't 32 bits.");
OPENSSL_STATIC_ASSERT(sizeof(size_t) == sizeof(uintptr_t), "uintptr_t and size_t differ.");
OPENSSL_STATIC_ASSERT(sizeof(size_t) <= sizeof(uint64_t), "size_t is larger than uint64_t.");
OPENSSL_STATIC_ASSERT(sizeof(size_t) >= sizeof(uint32_t), "size_t is smaller than uint32_t.");

#endif // OPENSSL_HEADER_BASE_H
47 changes: 30 additions & 17 deletions src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,38 @@

//! C types.
//!
//! The libc crate provide the C types for most, but not all, targets that
//! *ring* supports.
//! Avoid using the `libc` crate to get C types since `libc` doesn't support
//! all the targets we need to support. It turns out that the few types we need
//! are all uniformly defined on the platforms we care about. This will
//! probably change if/when we support 16-bit platforms or platforms where
//! `usize` and `uintptr_t` are different sizes.

#[cfg(not(all(target_arch = "wasm32", target_env = "",)))]
use ::libc;
// Keep in sync with the checks in base.h that verify these assumptions.

#[cfg(all(target_arch = "wasm32", target_env = ""))]
mod libc {
//! The WASM32 ABI is described at
//! https://github.com/WebAssembly/tool-conventions/blob/master/BasicCABI.md#data-representation
pub(crate) type int = i32;
pub(crate) type uint = u32;
pub(crate) type size_t = usize;

pub(crate) type c_int = i32;
pub(crate) type c_uint = u32;
#[cfg(all(test, any(unix, windows)))]
mod tests {
use crate::c;

// "The size_t type is defined as unsigned long."
// However, we must define it as an alias of `usize` for compatibility with `libc::size_t`.
pub(crate) type size_t = usize;
}
#[test]
fn test_libc_compatible() {
{
let x: c::int = 1;
let _x: libc::c_int = x;
}

{
let x: c::uint = 1;
let _x: libc::c_uint = x;
}

pub(crate) type size_t = self::libc::size_t;
pub(crate) type int = self::libc::c_int;
pub(crate) type uint = self::libc::c_uint;
{
let x: c::size_t = 1;
let _x: libc::size_t = x;
let _x: usize = x;
}
}
}

0 comments on commit 4c392ad

Please sign in to comment.