Skip to content

Commit e949d34

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 cc87515 commit e949d34

File tree

14 files changed

+245
-156
lines changed

14 files changed

+245
-156
lines changed

build.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
262262
const MACOS: &str = "macos";
263263
const WINDOWS: &str = "windows";
264264

265+
fn find_asm_target(target: &Target) -> Option<&'static AsmTarget> {
266+
ASM_TARGETS.iter().find(|asm_target| {
267+
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
268+
})
269+
}
270+
265271
fn main() {
266272
// Avoid assuming the working directory is the same is the $CARGO_MANIFEST_DIR so that toolchains
267273
// which may assume other working directories can still build this code.
@@ -324,9 +330,7 @@ fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
324330
force_warnings_into_errors,
325331
};
326332

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-
});
333+
let asm_target = find_asm_target(&target);
330334

331335
// If `.git` exists then assume this is the "local hacking" case where
332336
// we want to make it easy to build *ring* using `cargo build`/`cargo test`
@@ -419,6 +423,8 @@ fn build_c_code(
419423
core_name_and_version: &str,
420424
) {
421425
let (asm_srcs, obj_srcs) = if let Some(asm_target) = asm_target {
426+
println!("cargo:rustc-cfg=perlasm");
427+
422428
let perlasm_src_dsts = perlasm_src_dsts(generated_dir, asm_target);
423429

424430
let asm_srcs = asm_srcs(perlasm_src_dsts);
@@ -435,6 +441,8 @@ fn build_c_code(
435441
(asm_srcs, vec![])
436442
}
437443
} else {
444+
println!("cargo:rustc-cfg=no_perlasm");
445+
438446
(vec![], vec![])
439447
};
440448

@@ -591,6 +599,10 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d
591599
if target.force_warnings_into_errors {
592600
c.warnings_into_errors(true);
593601
}
602+
603+
if find_asm_target(target).is_none() {
604+
let _ = c.define("OPENSSL_NO_ASM", "1");
605+
}
594606
}
595607

596608
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
@@ -141,21 +141,27 @@ impl Key {
141141
};
142142

143143
match detect_implementation(cpu_features) {
144-
#[cfg(any(
145-
target_arch = "aarch64",
146-
target_arch = "arm",
147-
target_arch = "x86_64",
148-
target_arch = "x86"
144+
#[cfg(all(
145+
perlasm,
146+
any(
147+
target_arch = "aarch64",
148+
target_arch = "arm",
149+
target_arch = "x86_64",
150+
target_arch = "x86"
151+
)
149152
))]
150153
Implementation::HWAES => {
151154
set_encrypt_key!(aes_hw_set_encrypt_key, bytes, key_bits, &mut key)?
152155
}
153156

154-
#[cfg(any(
155-
target_arch = "aarch64",
156-
target_arch = "arm",
157-
target_arch = "x86_64",
158-
target_arch = "x86"
157+
#[cfg(all(
158+
perlasm,
159+
any(
160+
target_arch = "aarch64",
161+
target_arch = "arm",
162+
target_arch = "x86_64",
163+
target_arch = "x86"
164+
)
159165
))]
160166
Implementation::VPAES_BSAES => {
161167
set_encrypt_key!(vpaes_set_encrypt_key, bytes, key_bits, &mut key)?
@@ -172,19 +178,25 @@ impl Key {
172178
#[inline]
173179
pub fn encrypt_block(&self, a: Block, cpu_features: cpu::Features) -> Block {
174180
match detect_implementation(cpu_features) {
175-
#[cfg(any(
176-
target_arch = "aarch64",
177-
target_arch = "arm",
178-
target_arch = "x86_64",
179-
target_arch = "x86"
181+
#[cfg(all(
182+
perlasm,
183+
any(
184+
target_arch = "aarch64",
185+
target_arch = "arm",
186+
target_arch = "x86_64",
187+
target_arch = "x86"
188+
)
180189
))]
181190
Implementation::HWAES => encrypt_block!(aes_hw_encrypt, a, self),
182191

183-
#[cfg(any(
184-
target_arch = "aarch64",
185-
target_arch = "arm",
186-
target_arch = "x86_64",
187-
target_arch = "x86"
192+
#[cfg(all(
193+
perlasm,
194+
any(
195+
target_arch = "aarch64",
196+
target_arch = "arm",
197+
target_arch = "x86_64",
198+
target_arch = "x86"
199+
)
188200
))]
189201
Implementation::VPAES_BSAES => encrypt_block!(vpaes_encrypt, a, self),
190202

@@ -211,17 +223,23 @@ impl Key {
211223
assert_eq!(in_out_len % BLOCK_LEN, 0);
212224

213225
match detect_implementation(cpu_features) {
214-
#[cfg(any(
215-
target_arch = "aarch64",
216-
target_arch = "arm",
217-
target_arch = "x86_64",
218-
target_arch = "x86"
226+
#[cfg(all(
227+
perlasm,
228+
any(
229+
target_arch = "aarch64",
230+
target_arch = "arm",
231+
target_arch = "x86_64",
232+
target_arch = "x86"
233+
)
219234
))]
220235
Implementation::HWAES => {
221236
ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, src, &self.inner, ctr)
222237
}
223238

224-
#[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))]
239+
#[cfg(all(
240+
perlasm,
241+
any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")
242+
))]
225243
Implementation::VPAES_BSAES => {
226244
// 8 blocks is the cut-off point where it's faster to use BSAES.
227245
#[cfg(target_arch = "arm")]
@@ -277,7 +295,7 @@ impl Key {
277295
[b0, b1, b2, b3, b4]
278296
}
279297

280-
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
298+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "aarch64")))]
281299
#[must_use]
282300
pub fn is_aes_hw(&self, cpu_features: cpu::Features) -> bool {
283301
matches!(detect_implementation(cpu_features), Implementation::HWAES)
@@ -357,20 +375,26 @@ pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
357375
#[derive(Clone, Copy)]
358376
#[allow(clippy::upper_case_acronyms)]
359377
pub enum Implementation {
360-
#[cfg(any(
361-
target_arch = "aarch64",
362-
target_arch = "arm",
363-
target_arch = "x86_64",
364-
target_arch = "x86"
378+
#[cfg(all(
379+
perlasm,
380+
any(
381+
target_arch = "aarch64",
382+
target_arch = "arm",
383+
target_arch = "x86_64",
384+
target_arch = "x86"
385+
)
365386
))]
366387
HWAES = 1,
367388

368389
// On "arm" only, this indicates that the bsaes implementation may be used.
369-
#[cfg(any(
370-
target_arch = "aarch64",
371-
target_arch = "arm",
372-
target_arch = "x86_64",
373-
target_arch = "x86"
390+
#[cfg(all(
391+
perlasm,
392+
any(
393+
target_arch = "aarch64",
394+
target_arch = "arm",
395+
target_arch = "x86_64",
396+
target_arch = "x86"
397+
)
374398
))]
375399
VPAES_BSAES = 2,
376400

@@ -379,36 +403,39 @@ pub enum Implementation {
379403

380404
fn detect_implementation(cpu_features: cpu::Features) -> Implementation {
381405
// `cpu_features` is only used for specific platforms.
382-
#[cfg(not(any(
383-
target_arch = "aarch64",
384-
target_arch = "arm",
385-
target_arch = "x86_64",
386-
target_arch = "x86"
406+
#[cfg(not(all(
407+
perlasm,
408+
any(
409+
target_arch = "aarch64",
410+
target_arch = "arm",
411+
target_arch = "x86_64",
412+
target_arch = "x86"
413+
)
387414
)))]
388415
let _cpu_features = cpu_features;
389416

390-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
417+
#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
391418
{
392419
if cpu::arm::AES.available(cpu_features) {
393420
return Implementation::HWAES;
394421
}
395422
}
396423

397-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
424+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
398425
{
399426
if cpu::intel::AES.available(cpu_features) {
400427
return Implementation::HWAES;
401428
}
402429
}
403430

404-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
431+
#[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
405432
{
406433
if cpu::intel::SSSE3.available(cpu_features) {
407434
return Implementation::VPAES_BSAES;
408435
}
409436
}
410437

411-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
438+
#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
412439
{
413440
if cpu::arm::NEON.available(cpu_features) {
414441
return Implementation::VPAES_BSAES;

src/aead/aes_gcm.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn aes_gcm_seal(
8686
let mut ctr = Counter::one(nonce);
8787
let tag_iv = ctr.increment();
8888

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

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

207207
let in_prefix_len = src.start;
208208

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

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

src/aead/chacha.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ use super::{quic::Sample, Nonce};
1717

1818
#[cfg(any(
1919
test,
20-
not(any(
21-
target_arch = "aarch64",
22-
target_arch = "arm",
23-
target_arch = "x86",
24-
target_arch = "x86_64"
20+
not(all(
21+
perlasm,
22+
any(
23+
target_arch = "aarch64",
24+
target_arch = "arm",
25+
target_arch = "x86",
26+
target_arch = "x86_64"
27+
)
2528
))
2629
))]
2730
mod fallback;
@@ -88,11 +91,14 @@ impl Key {
8891
/// Only call this with `src` equal to `0..` or from `encrypt_within`.
8992
#[inline]
9093
fn encrypt_less_safe(&self, counter: Counter, in_out: &mut [u8], src: RangeFrom<usize>) {
91-
#[cfg(any(
92-
target_arch = "aarch64",
93-
target_arch = "arm",
94-
target_arch = "x86",
95-
target_arch = "x86_64"
94+
#[cfg(all(
95+
perlasm,
96+
any(
97+
target_arch = "aarch64",
98+
target_arch = "arm",
99+
target_arch = "x86",
100+
target_arch = "x86_64"
101+
)
96102
))]
97103
#[inline(always)]
98104
pub(super) fn ChaCha20_ctr32(
@@ -125,11 +131,14 @@ impl Key {
125131
}
126132
}
127133

128-
#[cfg(not(any(
129-
target_arch = "aarch64",
130-
target_arch = "arm",
131-
target_arch = "x86",
132-
target_arch = "x86_64"
134+
#[cfg(not(all(
135+
perlasm,
136+
any(
137+
target_arch = "aarch64",
138+
target_arch = "arm",
139+
target_arch = "x86",
140+
target_arch = "x86_64"
141+
)
133142
)))]
134143
use fallback::ChaCha20_ctr32;
135144

@@ -166,11 +175,14 @@ impl Counter {
166175
/// the caller.
167176
#[cfg(any(
168177
test,
169-
not(any(
170-
target_arch = "aarch64",
171-
target_arch = "arm",
172-
target_arch = "x86",
173-
target_arch = "x86_64"
178+
not(all(
179+
perlasm,
180+
any(
181+
target_arch = "aarch64",
182+
target_arch = "arm",
183+
target_arch = "x86",
184+
target_arch = "x86_64"
185+
)
174186
))
175187
))]
176188
fn into_words_less_safe(self) -> [u32; 4] {

0 commit comments

Comments
 (0)