Skip to content

Commit 60a2951

Browse files
committed
build.rs: Improve conditional compilation around PerlAsm.
build.rs determines whether the target platform is supported by PerlAsm using both target_arch and target_os. Instances of conditional compilation in both src/ and crypto/ were using just target_arch to determine whether PerlAsm symbols are present, resulting in link-time build failures for certain targets, including, for example, aarch64-unknown-none. This commit fixes those instances of conditional compilation to align with the build script. I agree to license my contributions to each file under the terms given at the top of each file I changed.
1 parent 6814023 commit 60a2951

File tree

14 files changed

+274
-180
lines changed

14 files changed

+274
-180
lines changed

build.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,44 @@ const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
262262
const MACOS: &str = "macos";
263263
const WINDOWS: &str = "windows";
264264

265+
fn get_target(is_git: bool) -> Target {
266+
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
267+
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
268+
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
269+
270+
// Published builds are always built in release mode.
271+
let is_debug = is_git && env::var("DEBUG").unwrap() != "false";
272+
273+
// During local development, force warnings in non-Rust code to be treated
274+
// as errors. Since warnings are highly compiler-dependent and compilers
275+
// don't maintain backward compatibility w.r.t. which warnings they issue,
276+
// don't do this for packaged builds.
277+
let force_warnings_into_errors = is_git;
278+
279+
Target {
280+
arch,
281+
os,
282+
env,
283+
is_debug,
284+
force_warnings_into_errors,
285+
}
286+
}
287+
288+
fn find_asm_target(target: &Target) -> Option<&'static AsmTarget> {
289+
ASM_TARGETS.iter().find(|asm_target| {
290+
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
291+
})
292+
}
293+
265294
fn main() {
266295
// Avoid assuming the working directory is the same is the $CARGO_MANIFEST_DIR so that toolchains
267296
// which may assume other working directories can still build this code.
268297
let c_root_dir = PathBuf::from(
269298
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should always be set"),
270299
);
271300

301+
let is_git = std::fs::metadata(c_root_dir.join(".git")).is_ok();
302+
272303
// Keep in sync with `core_name_and_version!` in prefixed.rs.
273304
let core_name_and_version = [
274305
&env::var("CARGO_PKG_NAME").unwrap(),
@@ -285,48 +316,30 @@ fn main() {
285316
&core_name_and_version
286317
);
287318

319+
match find_asm_target(&get_target(is_git)) {
320+
Some(_) => println!("cargo:rustc-cfg=perlasm"),
321+
None => println!("cargo:rustc-cfg=no_perlasm"),
322+
}
323+
288324
const RING_PREGENERATE_ASM: &str = "RING_PREGENERATE_ASM";
289325
match env::var_os(RING_PREGENERATE_ASM).as_deref() {
290326
Some(s) if s == "1" => {
291327
pregenerate_asm_main(&c_root_dir, &core_name_and_version);
292328
}
293-
None => ring_build_rs_main(&c_root_dir, &core_name_and_version),
329+
None => ring_build_rs_main(&c_root_dir, &core_name_and_version, is_git),
294330
_ => {
295331
panic!("${} has an invalid value", RING_PREGENERATE_ASM);
296332
}
297333
}
298334
}
299335

300-
fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
336+
fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str, is_git: bool) {
301337
let out_dir = env::var_os("OUT_DIR").unwrap();
302338
let out_dir = PathBuf::from(out_dir);
303339

304-
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
305-
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
306-
let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
307-
308-
let is_git = std::fs::metadata(c_root_dir.join(".git")).is_ok();
309-
310-
// Published builds are always built in release mode.
311-
let is_debug = is_git && env::var("DEBUG").unwrap() != "false";
312-
313-
// During local development, force warnings in non-Rust code to be treated
314-
// as errors. Since warnings are highly compiler-dependent and compilers
315-
// don't maintain backward compatibility w.r.t. which warnings they issue,
316-
// don't do this for packaged builds.
317-
let force_warnings_into_errors = is_git;
318-
319-
let target = Target {
320-
arch,
321-
os,
322-
env,
323-
is_debug,
324-
force_warnings_into_errors,
325-
};
340+
let target = get_target(is_git);
326341

327-
let asm_target = ASM_TARGETS.iter().find(|asm_target| {
328-
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
329-
});
342+
let asm_target = find_asm_target(&target);
330343

331344
// If `.git` exists then assume this is the "local hacking" case where
332345
// we want to make it easy to build *ring* using `cargo build`/`cargo test`
@@ -591,6 +604,10 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d
591604
if target.force_warnings_into_errors {
592605
c.warnings_into_errors(true);
593606
}
607+
608+
if find_asm_target(target).is_none() {
609+
let _ = c.define("OPENSSL_NO_ASM", "1");
610+
}
594611
}
595612

596613
fn nasm(file: &Path, arch: &str, include_dir: &Path, out_dir: &Path, c_root_dir: &Path) {

crypto/fipsmodule/ec/p256_shared.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "../bn/internal.h"
2525

2626
#if !defined(OPENSSL_NO_ASM) && \
27-
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
2827
!defined(OPENSSL_SMALL)
2928
# define OPENSSL_USE_NISTZ256
3029
#endif

src/aead/aes.rs

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,29 @@ impl Key {
176176
};
177177

178178
match detect_implementation(cpu_features) {
179-
#[cfg(any(
180-
target_arch = "aarch64",
181-
target_arch = "arm",
182-
target_arch = "x86_64",
183-
target_arch = "x86"
179+
#[cfg(all(
180+
perlasm,
181+
any(
182+
target_arch = "aarch64",
183+
target_arch = "arm",
184+
target_arch = "x86_64",
185+
target_arch = "x86"
186+
)
184187
))]
185188
// SAFETY: `aes_hw_set_encrypt_key` satisfies the `set_encrypt_key!`
186189
// contract for these target architectures.
187190
Implementation::HWAES => unsafe {
188191
set_encrypt_key!(aes_hw_set_encrypt_key, bytes, &mut key, cpu_features)?;
189192
},
190193

191-
#[cfg(any(
192-
target_arch = "aarch64",
193-
target_arch = "arm",
194-
target_arch = "x86_64",
195-
target_arch = "x86"
194+
#[cfg(all(
195+
perlasm,
196+
any(
197+
target_arch = "aarch64",
198+
target_arch = "arm",
199+
target_arch = "x86_64",
200+
target_arch = "x86"
201+
)
196202
))]
197203
// SAFETY: `vpaes_set_encrypt_key` satisfies the `set_encrypt_key!`
198204
// contract for these target architectures.
@@ -213,19 +219,25 @@ impl Key {
213219
#[inline]
214220
pub fn encrypt_block(&self, a: Block, cpu_features: cpu::Features) -> Block {
215221
match detect_implementation(cpu_features) {
216-
#[cfg(any(
217-
target_arch = "aarch64",
218-
target_arch = "arm",
219-
target_arch = "x86_64",
220-
target_arch = "x86"
222+
#[cfg(all(
223+
perlasm,
224+
any(
225+
target_arch = "aarch64",
226+
target_arch = "arm",
227+
target_arch = "x86_64",
228+
target_arch = "x86"
229+
)
221230
))]
222231
Implementation::HWAES => encrypt_block!(aes_hw_encrypt, a, self),
223232

224-
#[cfg(any(
225-
target_arch = "aarch64",
226-
target_arch = "arm",
227-
target_arch = "x86_64",
228-
target_arch = "x86"
233+
#[cfg(all(
234+
perlasm,
235+
any(
236+
target_arch = "aarch64",
237+
target_arch = "arm",
238+
target_arch = "x86_64",
239+
target_arch = "x86"
240+
)
229241
))]
230242
Implementation::VPAES_BSAES => encrypt_block!(vpaes_encrypt, a, self),
231243

@@ -248,11 +260,14 @@ impl Key {
248260
cpu_features: cpu::Features,
249261
) {
250262
match detect_implementation(cpu_features) {
251-
#[cfg(any(
252-
target_arch = "aarch64",
253-
target_arch = "arm",
254-
target_arch = "x86_64",
255-
target_arch = "x86"
263+
#[cfg(all(
264+
perlasm,
265+
any(
266+
target_arch = "aarch64",
267+
target_arch = "arm",
268+
target_arch = "x86_64",
269+
target_arch = "x86"
270+
)
256271
))]
257272
// SAFETY:
258273
// * self.inner was initialized with `aes_hw_set_encrypt_key` above,
@@ -270,7 +285,10 @@ impl Key {
270285
)
271286
},
272287

273-
#[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))]
288+
#[cfg(all(
289+
perlasm,
290+
any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")
291+
))]
274292
Implementation::VPAES_BSAES => {
275293
#[cfg(target_arch = "arm")]
276294
let in_out = {
@@ -357,7 +375,7 @@ impl Key {
357375
[b0, b1, b2, b3, b4]
358376
}
359377

360-
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
378+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "aarch64")))]
361379
#[must_use]
362380
pub fn is_aes_hw(&self, cpu_features: cpu::Features) -> bool {
363381
matches!(detect_implementation(cpu_features), Implementation::HWAES)
@@ -437,20 +455,26 @@ pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
437455
#[derive(Clone, Copy)]
438456
#[allow(clippy::upper_case_acronyms)]
439457
pub enum Implementation {
440-
#[cfg(any(
441-
target_arch = "aarch64",
442-
target_arch = "arm",
443-
target_arch = "x86_64",
444-
target_arch = "x86"
458+
#[cfg(all(
459+
perlasm,
460+
any(
461+
target_arch = "aarch64",
462+
target_arch = "arm",
463+
target_arch = "x86_64",
464+
target_arch = "x86"
465+
)
445466
))]
446467
HWAES = 1,
447468

448469
// On "arm" only, this indicates that the bsaes implementation may be used.
449-
#[cfg(any(
450-
target_arch = "aarch64",
451-
target_arch = "arm",
452-
target_arch = "x86_64",
453-
target_arch = "x86"
470+
#[cfg(all(
471+
perlasm,
472+
any(
473+
target_arch = "aarch64",
474+
target_arch = "arm",
475+
target_arch = "x86_64",
476+
target_arch = "x86"
477+
)
454478
))]
455479
VPAES_BSAES = 2,
456480

@@ -459,36 +483,39 @@ pub enum Implementation {
459483

460484
fn detect_implementation(cpu_features: cpu::Features) -> Implementation {
461485
// `cpu_features` is only used for specific platforms.
462-
#[cfg(not(any(
463-
target_arch = "aarch64",
464-
target_arch = "arm",
465-
target_arch = "x86_64",
466-
target_arch = "x86"
486+
#[cfg(not(all(
487+
perlasm,
488+
any(
489+
target_arch = "aarch64",
490+
target_arch = "arm",
491+
target_arch = "x86_64",
492+
target_arch = "x86"
493+
)
467494
)))]
468495
let _cpu_features = cpu_features;
469496

470-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
497+
#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
471498
{
472499
if cpu::arm::AES.available(cpu_features) {
473500
return Implementation::HWAES;
474501
}
475502
}
476503

477-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
504+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
478505
{
479506
if cpu::intel::AES.available(cpu_features) {
480507
return Implementation::HWAES;
481508
}
482509
}
483510

484-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
511+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
485512
{
486513
if cpu::intel::SSSE3.available(cpu_features) {
487514
return Implementation::VPAES_BSAES;
488515
}
489516
}
490517

491-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
518+
#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
492519
{
493520
if cpu::arm::NEON.available(cpu_features) {
494521
return Implementation::VPAES_BSAES;

src/aead/aes_gcm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn aes_gcm_seal(
8787
let mut ctr = Counter::one(nonce);
8888
let tag_iv = ctr.increment();
8989

90-
#[cfg(target_arch = "x86_64")]
90+
#[cfg(all(perlasm, target_arch = "x86_64"))]
9191
let in_out = {
9292
if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() {
9393
in_out
@@ -122,7 +122,7 @@ fn aes_gcm_seal(
122122
}
123123
};
124124

125-
#[cfg(target_arch = "aarch64")]
125+
#[cfg(all(perlasm, target_arch = "aarch64"))]
126126
let in_out = {
127127
if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() {
128128
in_out
@@ -207,7 +207,7 @@ fn aes_gcm_open(
207207

208208
let in_prefix_len = src.start;
209209

210-
#[cfg(target_arch = "x86_64")]
210+
#[cfg(all(perlasm, target_arch = "x86_64"))]
211211
let in_out = {
212212
if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() {
213213
in_out
@@ -242,7 +242,7 @@ fn aes_gcm_open(
242242
}
243243
};
244244

245-
#[cfg(target_arch = "aarch64")]
245+
#[cfg(all(perlasm, target_arch = "aarch64"))]
246246
let in_out = {
247247
if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() {
248248
in_out

0 commit comments

Comments
 (0)