Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve conditional compilation around PerlAsm #1937

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,44 @@ const APPLE_ABI: &[&str] = &["ios", "macos", "tvos", "visionos", "watchos"];

const WINDOWS: &str = "windows";

fn get_target(is_git: bool) -> Target {
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();

// Published builds are always built in release mode.
let is_debug = is_git && env::var("DEBUG").unwrap() != "false";

// During local development, force warnings in non-Rust code to be treated
// as errors. Since warnings are highly compiler-dependent and compilers
// don't maintain backward compatibility w.r.t. which warnings they issue,
// don't do this for packaged builds.
let force_warnings_into_errors = is_git;

Target {
arch,
os,
env,
is_debug,
force_warnings_into_errors,
}
}

fn find_asm_target(target: &Target) -> Option<&'static AsmTarget> {
ASM_TARGETS.iter().find(|asm_target| {
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
})
}

fn main() {
// Avoid assuming the working directory is the same is the $CARGO_MANIFEST_DIR so that toolchains
// which may assume other working directories can still build this code.
let c_root_dir = PathBuf::from(
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should always be set"),
);

let is_git = std::fs::metadata(c_root_dir.join(".git")).is_ok();

// Keep in sync with `core_name_and_version!` in prefixed.rs.
let core_name_and_version = [
&env::var("CARGO_PKG_NAME").unwrap(),
Expand All @@ -282,48 +313,33 @@ fn main() {
&core_name_and_version
);

println!("cargo:rustc-check-cfg=cfg(perlasm)");
println!("cargo:rustc-check-cfg=cfg(no_perlasm)");

match find_asm_target(&get_target(is_git)) {
Some(_) => println!("cargo:rustc-cfg=perlasm"),
None => println!("cargo:rustc-cfg=no_perlasm"),
}

const RING_PREGENERATE_ASM: &str = "RING_PREGENERATE_ASM";
match env::var_os(RING_PREGENERATE_ASM).as_deref() {
Some(s) if s == "1" => {
pregenerate_asm_main(&c_root_dir, &core_name_and_version);
}
None => ring_build_rs_main(&c_root_dir, &core_name_and_version),
None => ring_build_rs_main(&c_root_dir, &core_name_and_version, is_git),
_ => {
panic!("${} has an invalid value", RING_PREGENERATE_ASM);
}
}
}

fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str, is_git: bool) {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = PathBuf::from(out_dir);

let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();

let is_git = fs::metadata(c_root_dir.join(".git")).is_ok();

// Published builds are always built in release mode.
let is_debug = is_git && env::var("DEBUG").unwrap() != "false";

// During local development, force warnings in non-Rust code to be treated
// as errors. Since warnings are highly compiler-dependent and compilers
// don't maintain backward compatibility w.r.t. which warnings they issue,
// don't do this for packaged builds.
let force_warnings_into_errors = is_git;

let target = Target {
arch,
os,
env,
is_debug,
force_warnings_into_errors,
};
let target = get_target(is_git);

let asm_target = ASM_TARGETS.iter().find(|asm_target| {
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
});
let asm_target = find_asm_target(&target);

// If `.git` exists then assume this is the "local hacking" case where
// we want to make it easy to build *ring* using `cargo build`/`cargo test`
Expand Down Expand Up @@ -586,6 +602,10 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d
if target.force_warnings_into_errors {
c.warnings_into_errors(true);
}

if find_asm_target(target).is_none() {
let _ = c.define("OPENSSL_NO_ASM", "1");
}
}

fn nasm(file: &Path, arch: &str, include_dir: &Path, out_dir: &Path, c_root_dir: &Path) {
Expand Down
1 change: 0 additions & 1 deletion crypto/fipsmodule/ec/p256_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "../bn/internal.h"

#if !defined(OPENSSL_NO_ASM) && \
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
!defined(OPENSSL_SMALL)
# define OPENSSL_USE_NISTZ256
#endif
Expand Down
56 changes: 37 additions & 19 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(super) mod hw;
pub(super) mod vp;

cfg_if! {
if #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] {
if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] {
pub(super) use ffi::AES_KEY;
} else {
use ffi::AES_KEY;
Expand All @@ -41,14 +41,20 @@ cfg_if! {

#[derive(Clone)]
pub(super) enum Key {
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))]
#[cfg(all(
perlasm,
any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86")
))]
Hw(hw::Key),

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
#[cfg(all(
perlasm,
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
)
))]
Vp(vp::Key),

Expand All @@ -61,16 +67,22 @@ impl Key {
bytes: KeyBytes<'_>,
cpu_features: cpu::Features,
) -> Result<Self, error::Unspecified> {
#[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(
perlasm,
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
))]
if let Some(hw_features) = cpu_features.get_feature() {
return Ok(Self::Hw(hw::Key::new(bytes, hw_features)?));
}

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
#[cfg(all(
perlasm,
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86_64",
target_arch = "x86"
)
))]
if let Some(vp_features) = cpu_features.get_feature() {
return Ok(Self::Vp(vp::Key::new(bytes, vp_features)?));
Expand All @@ -84,14 +96,20 @@ impl Key {
#[inline]
fn encrypt_block(&self, a: Block) -> Block {
match self {
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))]
#[cfg(all(
perlasm,
any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86")
))]
Key::Hw(inner) => inner.encrypt_block(a),

#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
#[cfg(all(
perlasm,
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
)
))]
Key::Vp(inner) => inner.encrypt_block(a),

Expand Down
5 changes: 4 additions & 1 deletion src/aead/aes/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#![cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))]
#![cfg(all(
perlasm,
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
))]

use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, AES_KEY};
use crate::{cpu, error};
Expand Down
13 changes: 8 additions & 5 deletions src/aead/aes/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#![cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
#![cfg(all(
perlasm,
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "x86",
target_arch = "x86_64"
)
))]

use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, AES_KEY};
Expand Down
Loading