Skip to content

Commit fcd2ba0

Browse files
authored
Guard NEON+SHA3 s2n-bignum assembly (#878)
* Guard NEON+SHA3 s2n-bignum assembly * Test w/ older cross-rs * Cleanup * CI cleanup
1 parent 88c7f50 commit fcd2ba0

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed

.github/workflows/cross.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ jobs:
136136

137137
aws-lc-rs-cross-0_2_5-test:
138138
if: github.repository_owner == 'aws'
139-
name: cross tests ${{ matrix.target }}
139+
name: cross v0.2.5 tests ${{ matrix.target }}
140140
runs-on: ubuntu-latest
141141
env:
142142
CROSS_CONFIG: "./Cross.toml.x86_64-unknown-linux-gnu"
@@ -145,6 +145,7 @@ jobs:
145145
matrix:
146146
target:
147147
- x86_64-unknown-linux-gnu
148+
- aarch64-unknown-linux-gnu
148149
steps:
149150
- uses: actions/checkout@v3
150151
with:
@@ -161,6 +162,9 @@ jobs:
161162
target: ${{ matrix.target }}
162163
- name: Set Rust toolchain override
163164
run: rustup override set ${{ steps.toolchain.outputs.name }}
165+
- if: ${{ !startsWith(matrix.target[0], 'x86_64') }}
166+
run: |
167+
echo 'AWS_LC_RS_DISABLE_SLOW_TESTS=1' >> "$GITHUB_ENV"
164168
- name: Cross-compilation (build debug)
165169
run: cross build -p aws-lc-rs --features unstable --target ${{ matrix.target }}
166170
- name: Cross-compilation (test release)

aws-lc-sys/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ test:
77
cargo test --all-targets
88
cargo test --release --all-targets
99

10+
clippy-fix:
11+
cargo +nightly clippy --all-targets --features bindgen --fix --allow-dirty -- -W clippy::all -W clippy::pedantic
12+
1013
ci: format clippy test api-diff-main
1114

12-
.PHONY: test ci
15+
.PHONY: test ci clippy-fix

aws-lc-sys/builder/cc_builder.rs

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@ use crate::{
1919
is_no_asm, optional_env_optional_crate_target, out_dir, requested_c_std, set_env_for_target,
2020
target, target_arch, target_env, target_os, CStdRequested, OutputLibType,
2121
};
22+
use std::cell::Cell;
23+
use std::collections::HashMap;
2224
use std::path::PathBuf;
2325

26+
#[non_exhaustive]
27+
#[derive(PartialEq, Eq)]
28+
pub(crate) enum CompilerFeature {
29+
NeonSha3,
30+
}
31+
2432
pub(crate) struct CcBuilder {
2533
manifest_dir: PathBuf,
2634
out_dir: PathBuf,
2735
build_prefix: Option<String>,
2836
output_lib_type: OutputLibType,
37+
compiler_features: Cell<Vec<CompilerFeature>>,
2938
}
3039

3140
use std::fs;
@@ -168,6 +177,7 @@ impl CcBuilder {
168177
out_dir,
169178
build_prefix,
170179
output_lib_type,
180+
compiler_features: Cell::new(vec![]),
171181
}
172182
}
173183

@@ -190,7 +200,7 @@ impl CcBuilder {
190200
build_options.push(BuildOption::std("c11"));
191201
}
192202
CStdRequested::None => {
193-
if self.compiler_check("c11") {
203+
if self.compiler_check("c11", Vec::<String>::new()) {
194204
build_options.push(BuildOption::std("c11"));
195205
} else {
196206
build_options.push(BuildOption::std("c99"));
@@ -341,6 +351,18 @@ impl CcBuilder {
341351
cc_build
342352
}
343353

354+
#[allow(clippy::zero_sized_map_values)]
355+
fn build_s2n_bignum_source_feature_map() -> HashMap<String, CompilerFeature> {
356+
let mut source_feature_map: HashMap<String, CompilerFeature> = HashMap::new();
357+
source_feature_map.insert("sha3_keccak_f1600_alt.S".into(), CompilerFeature::NeonSha3);
358+
source_feature_map.insert("sha3_keccak2_f1600.S".into(), CompilerFeature::NeonSha3);
359+
source_feature_map.insert(
360+
"sha3_keccak4_f1600_alt2.S".into(),
361+
CompilerFeature::NeonSha3,
362+
);
363+
source_feature_map
364+
}
365+
344366
fn add_all_files(&self, lib: &Library, cc_build: &mut cc::Build) {
345367
use core::str::FromStr;
346368

@@ -371,20 +393,45 @@ impl CcBuilder {
371393
// conditioned on the target OS.
372394
jitter_entropy_builder.flag("-DAWSLC -fwrapv --param ssp-buffer-size=4 -fvisibility=hidden -Wcast-align -Wmissing-field-initializers -Wshadow -Wswitch-enum -Wextra -Wall -pedantic -O0 -fwrapv -Wconversion");
373395

396+
let s2n_bignum_source_feature_map = Self::build_s2n_bignum_source_feature_map();
397+
let compiler_features = self.compiler_features.take();
374398
for source in lib.sources {
375399
let source_path = self.manifest_dir.join("aws-lc").join(source);
376400
let is_s2n_bignum = std::path::Path::new(source).starts_with("third_party/s2n-bignum");
377401
let is_jitter_entropy =
378402
std::path::Path::new(source).starts_with("third_party/jitterentropy");
379403

404+
if !source_path.is_file() {
405+
emit_warning(&format!("Not a file: {:?}", source_path.as_os_str()));
406+
continue;
407+
}
380408
if is_s2n_bignum {
381-
s2n_bignum_builder.file(source_path);
409+
let filename: String = source_path
410+
.file_name()
411+
.unwrap()
412+
.to_str()
413+
.unwrap()
414+
.to_string();
415+
416+
if let Some(compiler_feature) = s2n_bignum_source_feature_map.get(&filename) {
417+
if compiler_features.contains(compiler_feature) {
418+
s2n_bignum_builder.file(source_path);
419+
} else {
420+
emit_warning(&format!(
421+
"Skipping due to missing compiler features: {:?}",
422+
source_path.as_os_str()
423+
));
424+
}
425+
} else {
426+
s2n_bignum_builder.file(source_path);
427+
}
382428
} else if is_jitter_entropy {
383429
jitter_entropy_builder.file(source_path);
384430
} else {
385431
cc_build.file(source_path);
386432
}
387433
}
434+
self.compiler_features.set(compiler_features);
388435
let s2n_bignum_object_files = s2n_bignum_builder.compile_intermediates();
389436
for object in s2n_bignum_object_files {
390437
cc_build.object(object);
@@ -414,7 +461,11 @@ impl CcBuilder {
414461
// This performs basic checks of compiler capabilities and sets an appropriate flag on success.
415462
// This should be kept in alignment with the checks performed by AWS-LC's CMake build.
416463
// See: https://github.com/search?q=repo%3Aaws%2Faws-lc%20check_compiler&type=code
417-
fn compiler_check(&self, basename: &str) -> bool {
464+
fn compiler_check<T, S>(&self, basename: &str, extra_flags: T) -> bool
465+
where
466+
T: IntoIterator<Item = S>,
467+
S: AsRef<str>,
468+
{
418469
let mut ret_val = false;
419470
let output_dir = self.out_dir.join(format!("out-{basename}"));
420471
let source_file = self
@@ -439,6 +490,10 @@ impl CcBuilder {
439490
.file(source_file)
440491
.warnings_into_errors(true)
441492
.out_dir(&output_dir);
493+
for flag in extra_flags {
494+
let flag = flag.as_ref();
495+
cc_build.flag(flag);
496+
}
442497

443498
let compiler = cc_build.get_compiler();
444499
if compiler.is_like_gnu() || compiler.is_like_clang() {
@@ -529,12 +584,20 @@ impl CcBuilder {
529584
let _ = fs::remove_file(exec_path);
530585
}
531586
fn run_compiler_checks(&self, cc_build: &mut cc::Build) {
532-
if self.compiler_check("stdalign_check") {
587+
if self.compiler_check("stdalign_check", Vec::<&'static str>::new()) {
533588
cc_build.define("AWS_LC_STDALIGN_AVAILABLE", Some("1"));
534589
}
535-
if self.compiler_check("builtin_swap_check") {
590+
if self.compiler_check("builtin_swap_check", Vec::<&'static str>::new()) {
536591
cc_build.define("AWS_LC_BUILTIN_SWAP_SUPPORTED", Some("1"));
537592
}
593+
if target_arch() == "aarch64"
594+
&& self.compiler_check("neon_sha3_check", vec!["-march=armv8.4-a+sha3"])
595+
{
596+
let mut compiler_features = self.compiler_features.take();
597+
compiler_features.push(CompilerFeature::NeonSha3);
598+
self.compiler_features.set(compiler_features);
599+
cc_build.define("MY_ASSEMBLER_SUPPORTS_NEON_SHA3_EXTENSION", Some("1"));
600+
}
538601
self.memcmp_check();
539602
}
540603
}

docker/linux-cross/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ FROM $CROSS_BASE_IMAGE
44
ARG DEBIAN_FRONTEND=noninteractive
55

66
RUN apt-get update && \
7-
apt-get install --assume-yes --no-install-recommends gpg-agent software-properties-common && \
7+
apt-get install --assume-yes --no-install-recommends gpg-agent software-properties-common dirmngr && \
8+
gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys F6BC817356A3D45E C631127F87FA12D1 && \
89
add-apt-repository --yes ppa:longsleep/golang-backports && \
910
apt-get update && \
1011
apt-get install --assume-yes --no-install-recommends build-essential cmake golang-go clang && \

docker/ubuntu-18.04/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ ARG GOPROXY=direct
1010
RUN apt-get update && \
1111
apt-get install -y ca-certificates && \
1212
apt-get install -y cmake curl sudo && \
13-
apt-get install -y --no-install-recommends gpg-agent software-properties-common && \
13+
apt-get install -y --no-install-recommends gpg-agent software-properties-common dirmngr && \
14+
gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys F6BC817356A3D45E C631127F87FA12D1 && \
1415
add-apt-repository --yes ppa:longsleep/golang-backports && \
1516
add-apt-repository --yes --update ppa:ubuntu-toolchain-r/test && \
1617
apt-add-repository --yes ppa:git-core/ppa && \

0 commit comments

Comments
 (0)