Skip to content

Commit 0e644b7

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 7bc7672 commit 0e644b7

File tree

14 files changed

+274
-180
lines changed

14 files changed

+274
-180
lines changed

build.rs

+44-27
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

-1
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

+73-46
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)
@@ -436,20 +454,26 @@ pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
436454
#[derive(Clone, Copy)]
437455
#[allow(clippy::upper_case_acronyms)]
438456
pub enum Implementation {
439-
#[cfg(any(
440-
target_arch = "aarch64",
441-
target_arch = "arm",
442-
target_arch = "x86_64",
443-
target_arch = "x86"
457+
#[cfg(all(
458+
perlasm,
459+
any(
460+
target_arch = "aarch64",
461+
target_arch = "arm",
462+
target_arch = "x86_64",
463+
target_arch = "x86"
464+
)
444465
))]
445466
HWAES,
446467

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

@@ -458,36 +482,39 @@ pub enum Implementation {
458482

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

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

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

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

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

src/aead/aes_gcm.rs

+4-4
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
@@ -124,7 +124,7 @@ fn aes_gcm_seal(
124124

125125
let (whole, remainder) = slice::as_chunks_mut(in_out);
126126

127-
#[cfg(target_arch = "aarch64")]
127+
#[cfg(all(perlasm, target_arch = "aarch64"))]
128128
let whole = {
129129
if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() {
130130
whole
@@ -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)