Skip to content

Commit 5412e5f

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.
1 parent ee5db43 commit 5412e5f

File tree

13 files changed

+244
-155
lines changed

13 files changed

+244
-155
lines changed

build.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
269269
const MACOS: &str = "macos";
270270
const WINDOWS: &str = "windows";
271271

272+
fn find_asm_target(target: &Target) -> Option<&'static AsmTarget> {
273+
ASM_TARGETS.iter().find(|asm_target| {
274+
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
275+
})
276+
}
277+
272278
/// Read an environment variable and tell Cargo that we depend on it.
273279
///
274280
/// This needs to be used for any environment variable that isn't a standard
@@ -418,10 +424,6 @@ fn build_c_code(
418424
) {
419425
println!("cargo:rustc-env=RING_CORE_PREFIX={}", ring_core_prefix);
420426

421-
let asm_target = ASM_TARGETS.iter().find(|asm_target| {
422-
asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref())
423-
});
424-
425427
let asm_dir = if use_pregenerated {
426428
&pregenerated
427429
} else {
@@ -433,7 +435,9 @@ fn build_c_code(
433435

434436
generate_prefix_symbols_asm_headers(out_dir, ring_core_prefix).unwrap();
435437

436-
let (asm_srcs, obj_srcs) = if let Some(asm_target) = asm_target {
438+
let (asm_srcs, obj_srcs) = if let Some(asm_target) = find_asm_target(target) {
439+
println!("cargo:rustc-cfg=have_perlasm");
440+
437441
let perlasm_src_dsts = perlasm_src_dsts(asm_dir, asm_target);
438442

439443
if !use_pregenerated {
@@ -617,6 +621,10 @@ fn configure_cc(c: &mut cc::Build, target: &Target, include_dir: &Path) {
617621
if target.force_warnings_into_errors {
618622
c.warnings_into_errors(true);
619623
}
624+
625+
if find_asm_target(target).is_some() {
626+
let _ = c.define("RING_HAVE_PERLASM", "1");
627+
}
620628
}
621629

622630
/// Assembles the assemply language source `file` into the object file

crypto/fipsmodule/ec/p256_shared.h

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

2626
#if !defined(OPENSSL_NO_ASM) && \
27-
(defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
27+
defined(RING_HAVE_PERLASM) && \
2828
!defined(OPENSSL_SMALL)
2929
# define OPENSSL_USE_NISTZ256
3030
#endif

src/aead/aes.rs

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,27 @@ impl Key {
145145
};
146146

147147
match detect_implementation(cpu_features) {
148-
#[cfg(any(
149-
target_arch = "aarch64",
150-
target_arch = "arm",
151-
target_arch = "x86_64",
152-
target_arch = "x86"
148+
#[cfg(all(
149+
have_perlasm,
150+
any(
151+
target_arch = "aarch64",
152+
target_arch = "arm",
153+
target_arch = "x86_64",
154+
target_arch = "x86"
155+
)
153156
))]
154157
Implementation::HWAES => {
155158
set_encrypt_key!(aes_hw_set_encrypt_key, bytes, key_bits, &mut key)?
156159
}
157160

158-
#[cfg(any(
159-
target_arch = "aarch64",
160-
target_arch = "arm",
161-
target_arch = "x86_64",
162-
target_arch = "x86"
161+
#[cfg(all(
162+
have_perlasm,
163+
any(
164+
target_arch = "aarch64",
165+
target_arch = "arm",
166+
target_arch = "x86_64",
167+
target_arch = "x86"
168+
)
163169
))]
164170
Implementation::VPAES_BSAES => {
165171
set_encrypt_key!(vpaes_set_encrypt_key, bytes, key_bits, &mut key)?
@@ -176,19 +182,25 @@ impl Key {
176182
#[inline]
177183
pub fn encrypt_block(&self, a: Block, cpu_features: cpu::Features) -> Block {
178184
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"
185+
#[cfg(all(
186+
have_perlasm,
187+
any(
188+
target_arch = "aarch64",
189+
target_arch = "arm",
190+
target_arch = "x86_64",
191+
target_arch = "x86"
192+
)
184193
))]
185194
Implementation::HWAES => encrypt_block!(aes_hw_encrypt, a, self),
186195

187-
#[cfg(any(
188-
target_arch = "aarch64",
189-
target_arch = "arm",
190-
target_arch = "x86_64",
191-
target_arch = "x86"
196+
#[cfg(all(
197+
have_perlasm,
198+
any(
199+
target_arch = "aarch64",
200+
target_arch = "arm",
201+
target_arch = "x86_64",
202+
target_arch = "x86"
203+
)
192204
))]
193205
Implementation::VPAES_BSAES => encrypt_block!(vpaes_encrypt, a, self),
194206

@@ -215,17 +227,23 @@ impl Key {
215227
assert_eq!(in_out_len % BLOCK_LEN, 0);
216228

217229
match detect_implementation(cpu_features) {
218-
#[cfg(any(
219-
target_arch = "aarch64",
220-
target_arch = "arm",
221-
target_arch = "x86_64",
222-
target_arch = "x86"
230+
#[cfg(all(
231+
have_perlasm,
232+
any(
233+
target_arch = "aarch64",
234+
target_arch = "arm",
235+
target_arch = "x86_64",
236+
target_arch = "x86"
237+
)
223238
))]
224239
Implementation::HWAES => {
225240
ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, src, &self.inner, ctr)
226241
}
227242

228-
#[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))]
243+
#[cfg(all(
244+
have_perlasm,
245+
any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")
246+
))]
229247
Implementation::VPAES_BSAES => {
230248
// 8 blocks is the cut-off point where it's faster to use BSAES.
231249
#[cfg(target_arch = "arm")]
@@ -285,7 +303,7 @@ impl Key {
285303
out
286304
}
287305

288-
#[cfg(target_arch = "x86_64")]
306+
#[cfg(all(have_perlasm, target_arch = "x86_64"))]
289307
#[must_use]
290308
pub fn is_aes_hw(&self, cpu_features: cpu::Features) -> bool {
291309
matches!(detect_implementation(cpu_features), Implementation::HWAES)
@@ -361,20 +379,26 @@ impl Iv {
361379
#[derive(Clone, Copy)]
362380
#[allow(clippy::upper_case_acronyms)]
363381
pub enum Implementation {
364-
#[cfg(any(
365-
target_arch = "aarch64",
366-
target_arch = "arm",
367-
target_arch = "x86_64",
368-
target_arch = "x86"
382+
#[cfg(all(
383+
have_perlasm,
384+
any(
385+
target_arch = "aarch64",
386+
target_arch = "arm",
387+
target_arch = "x86_64",
388+
target_arch = "x86"
389+
)
369390
))]
370391
HWAES = 1,
371392

372393
// On "arm" only, this indicates that the bsaes implementation may be used.
373-
#[cfg(any(
374-
target_arch = "aarch64",
375-
target_arch = "arm",
376-
target_arch = "x86_64",
377-
target_arch = "x86"
394+
#[cfg(all(
395+
have_perlasm,
396+
any(
397+
target_arch = "aarch64",
398+
target_arch = "arm",
399+
target_arch = "x86_64",
400+
target_arch = "x86"
401+
)
378402
))]
379403
VPAES_BSAES = 2,
380404

@@ -383,36 +407,39 @@ pub enum Implementation {
383407

384408
fn detect_implementation(cpu_features: cpu::Features) -> Implementation {
385409
// `cpu_features` is only used for specific platforms.
386-
#[cfg(not(any(
387-
target_arch = "aarch64",
388-
target_arch = "arm",
389-
target_arch = "x86_64",
390-
target_arch = "x86"
410+
#[cfg(not(all(
411+
have_perlasm,
412+
any(
413+
target_arch = "aarch64",
414+
target_arch = "arm",
415+
target_arch = "x86_64",
416+
target_arch = "x86"
417+
)
391418
)))]
392419
let _cpu_features = cpu_features;
393420

394-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
421+
#[cfg(all(have_perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
395422
{
396423
if cpu::arm::AES.available(cpu_features) {
397424
return Implementation::HWAES;
398425
}
399426
}
400427

401-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
428+
#[cfg(all(have_perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
402429
{
403430
if cpu::intel::AES.available(cpu_features) {
404431
return Implementation::HWAES;
405432
}
406433
}
407434

408-
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
435+
#[cfg(all(have_perlasm, any(target_arch = "x86_64", target_arch = "x86")))]
409436
{
410437
if cpu::intel::SSSE3.available(cpu_features) {
411438
return Implementation::VPAES_BSAES;
412439
}
413440
}
414441

415-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
442+
#[cfg(all(have_perlasm, any(target_arch = "aarch64", target_arch = "arm")))]
416443
{
417444
if cpu::arm::NEON.available(cpu_features) {
418445
return Implementation::VPAES_BSAES;

src/aead/aes_gcm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn aes_gcm_seal(
9191
let aad_len = aad.0.len();
9292
let mut auth = gcm::Context::new(gcm_key, aad, cpu_features);
9393

94-
#[cfg(target_arch = "x86_64")]
94+
#[cfg(all(have_perlasm, target_arch = "x86_64"))]
9595
let in_out = {
9696
if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() {
9797
in_out
@@ -179,7 +179,7 @@ fn aes_gcm_open(
179179

180180
let total_in_out_len = in_out.len() - in_prefix_len;
181181

182-
#[cfg(target_arch = "x86_64")]
182+
#[cfg(all(have_perlasm, target_arch = "x86_64"))]
183183
let in_out = {
184184
if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() {
185185
in_out

src/aead/chacha.rs

Lines changed: 32 additions & 20 deletions
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+
have_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+
have_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+
have_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+
have_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] {

src/aead/chacha20_poly1305.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn chacha20_poly1305_seal(
5757
_ => unreachable!(),
5858
};
5959

60-
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
60+
#[cfg(all(have_perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))]
6161
if has_integrated(cpu_features) {
6262
// XXX: BoringSSL uses `alignas(16)` on `key` instead of on the
6363
// structure, but Rust can't do that yet; see
@@ -137,7 +137,7 @@ fn chacha20_poly1305_open(
137137
_ => unreachable!(),
138138
};
139139

140-
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
140+
#[cfg(all(have_perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))]
141141
if has_integrated(cpu_features) {
142142
// XXX: BoringSSL uses `alignas(16)` on `key` instead of on the
143143
// structure, but Rust can't do that yet; see
@@ -200,7 +200,7 @@ fn chacha20_poly1305_open(
200200
finish(auth, aad.as_ref().len(), in_out[src].len())
201201
}
202202

203-
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
203+
#[cfg(all(have_perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))]
204204
#[allow(clippy::needless_return)]
205205
#[inline(always)]
206206
fn has_integrated(cpu_features: cpu::Features) -> bool {

0 commit comments

Comments
 (0)