From e16ea3d93cb83332428fd36852523c867227c98c Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Mon, 4 Mar 2024 12:14:54 -0500 Subject: [PATCH] Add support for s390x architecture. (#1431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description of changes: Add support for s390x architecture. This is pretty trivial change (a few defines), since the big-endian support was previously added for PowerPC architectures. ### Call-outs: There is a false-positive error when compiling using `s390x-linux-gnu-gcc-11` with `-O3` (i.e. `Release` profile): ``` crypto/pem/pem_lib.c:705:11: error: ‘strncmp’ of strings of length 1 and 9 and bound of 9 evaluates to nonzero [-Werror=string-compare] 705 | if (strncmp(buf, "-----END ", 9) == 0) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors ``` but there are no issues when using `-O2` or when building with `s390x-linux-gnu-gcc-12`. ### Testing: All tests from `run_cross_tests.sh`, i.e. ``` crypto/crypto_test --gtest_also_run_disabled_tests crypto/urandom_test crypto/mem_test crypto/mem_set_test crypto/dynamic_loading_test ssl/ssl_test ssl/integration_test ``` passed, with the exception of `BIOTest.InvokeConnectCallback`, which fails for me on all platforms. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license. cc @andrewhop @justsmth --------- Signed-off-by: Piotr Sikora Co-authored-by: Justin Smith Co-authored-by: Justin W Smith <103147162+justsmth@users.noreply.github.com> --- .github/workflows/cross-test.yml | 17 ++++++++ CMakeLists.txt | 2 + crypto/fipsmodule/rand/getrandom_fillin.h | 2 + include/openssl/target.h | 4 ++ tests/ci/run_cross_tests.sh | 2 + .../linux-ppc/ubuntu-x-tools/ppc.cmake | 1 + .../linux-ppc64/ubuntu-x-tools/ppc64.cmake | 1 + .../docker_images/linux-s390x/build_images.sh | 41 +++++++++++++++++++ .../linux-s390x/ubuntu-test/Dockerfile | 23 +++++++++++ .../linux-s390x/ubuntu-x-tools/Dockerfile | 40 ++++++++++++++++++ .../linux-s390x/ubuntu-x-tools/s390x.cmake | 12 ++++++ 11 files changed, 145 insertions(+) create mode 100755 tests/docker_images/linux-s390x/build_images.sh create mode 100644 tests/docker_images/linux-s390x/ubuntu-test/Dockerfile create mode 100644 tests/docker_images/linux-s390x/ubuntu-x-tools/Dockerfile create mode 100644 tests/docker_images/linux-s390x/ubuntu-x-tools/s390x.cmake diff --git a/.github/workflows/cross-test.yml b/.github/workflows/cross-test.yml index fbaa9acbf1..4f28f36723 100644 --- a/.github/workflows/cross-test.yml +++ b/.github/workflows/cross-test.yml @@ -48,3 +48,20 @@ jobs: - uses: actions/checkout@v4 - name: PPC64LE Build/Test run: tests/ci/run_cross_tests.sh ppc64le powerpc64le-unknown-linux-gnu "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_BUILD_TYPE=Release -DFIPS=1 -DBUILD_SHARED_LIBS=1" + s390x-non-fips-build-test: + runs-on: ubuntu-latest + steps: + - name: Install qemu + run: | + sudo apt-get update + sudo apt-get -y install qemu-user qemu-user-binfmt + - uses: actions/checkout@v4 + - name: s390x Build/Test +# The flag below is set to avoid the following error with GCC 11.4.0: +# +# /home/runner/work/aws-lc/aws-lc/crypto/pem/pem_lib.c:705:11: error: 'strncmp' of strings of length 1 and 9 and bound of 9 evaluates to nonzero [-Werror=string-compare] +# 705 | if (strncmp(buf, "-----END ", 9) == 0) { +# | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ + env: + CFLAGS: "-Wno-string-compare" + run: tests/ci/run_cross_tests.sh s390x s390x-ibm-linux-gnu "-DCMAKE_BUILD_TYPE=Release" diff --git a/CMakeLists.txt b/CMakeLists.txt index 80dfbaa2e5..5d5e868527 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -764,6 +764,8 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "powerpc64le|ppc64le") set(ARCH "ppc64le") elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "riscv64") set(ARCH "riscv64") +elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "s390x") + set(ARCH "s390x") elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "loongarch64") set(ARCH "loongarch64") else() diff --git a/crypto/fipsmodule/rand/getrandom_fillin.h b/crypto/fipsmodule/rand/getrandom_fillin.h index af61f7c601..b3dcd8c918 100644 --- a/crypto/fipsmodule/rand/getrandom_fillin.h +++ b/crypto/fipsmodule/rand/getrandom_fillin.h @@ -34,6 +34,8 @@ #define EXPECTED_NR_getrandom 359 #elif defined(OPENSSL_RISCV64) #define EXPECTED_NR_getrandom 278 +#elif defined(OPENSSL_S390X) +#define EXPECTED_NR_getrandom 349 #elif defined(OPENSSL_LOONGARCH64) #define EXPECTED_NR_getrandom 278 #endif diff --git a/include/openssl/target.h b/include/openssl/target.h index f63f235595..8bb421cb48 100644 --- a/include/openssl/target.h +++ b/include/openssl/target.h @@ -45,6 +45,10 @@ #define OPENSSL_32_BIT #define OPENSSL_PPC32BE #define OPENSSL_BIG_ENDIAN +#elif defined(__s390x__) +#define OPENSSL_64_BIT +#define OPENSSL_S390X +#define OPENSSL_BIG_ENDIAN #elif defined(__MIPSEL__) && !defined(__LP64__) #define OPENSSL_32_BIT #define OPENSSL_MIPS diff --git a/tests/ci/run_cross_tests.sh b/tests/ci/run_cross_tests.sh index 164ac5912d..99066f3fed 100755 --- a/tests/ci/run_cross_tests.sh +++ b/tests/ci/run_cross_tests.sh @@ -51,6 +51,8 @@ set(ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT true) set(CMAKE_GENERATOR Ninja) EOF +cat ${TARGET_CPU}.cmake + export QEMU_LD_PREFIX="${SCRATCH_FOLDER}/${TARGET_PLATFORM}/${TARGET_PLATFORM}/sysroot" export LD_LIBRARY_PATH="${SCRATCH_FOLDER}/${TARGET_PLATFORM}/${TARGET_PLATFORM}/sysroot/lib" diff --git a/tests/docker_images/linux-ppc/ubuntu-x-tools/ppc.cmake b/tests/docker_images/linux-ppc/ubuntu-x-tools/ppc.cmake index f83e7ddcec..015db6c94c 100644 --- a/tests/docker_images/linux-ppc/ubuntu-x-tools/ppc.cmake +++ b/tests/docker_images/linux-ppc/ubuntu-x-tools/ppc.cmake @@ -9,3 +9,4 @@ set(CMAKE_CXX_COMPILER /powerpc-unknown-linux-gnu/bin/powerpc-unknown-linux-gnu- # Specify the sysroot for the target system set(CMAKE_SYSROOT /powerpc-unknown-linux-gnu/powerpc-unknown-linux-gnu/sysroot) set(CMAKE_GENERATOR Ninja) +set(ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT true) diff --git a/tests/docker_images/linux-ppc64/ubuntu-x-tools/ppc64.cmake b/tests/docker_images/linux-ppc64/ubuntu-x-tools/ppc64.cmake index 9cde392e8f..a0f1067aa9 100644 --- a/tests/docker_images/linux-ppc64/ubuntu-x-tools/ppc64.cmake +++ b/tests/docker_images/linux-ppc64/ubuntu-x-tools/ppc64.cmake @@ -9,3 +9,4 @@ set(CMAKE_CXX_COMPILER /powerpc64-unknown-linux-gnu/bin/powerpc64-unknown-linux- # Specify the sysroot for the target system set(CMAKE_SYSROOT /powerpc64-unknown-linux-gnu/powerpc64-unknown-linux-gnu/sysroot/) set(CMAKE_GENERATOR Ninja) +set(ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT true) diff --git a/tests/docker_images/linux-s390x/build_images.sh b/tests/docker_images/linux-s390x/build_images.sh new file mode 100755 index 0000000000..d52c83abba --- /dev/null +++ b/tests/docker_images/linux-s390x/build_images.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 OR ISC + +set -ex + +# Note: +# After host reboot, qemu registration may need to be performed. +# `docker run --rm --privileged multiarch/qemu-user-static --reset -p yes` + +# On Linux, you can see which architectures that qemu is registered for by looking +# under `/proc/sys/fs/binfmt_misc`. + +# If needed, you can clear these entries using the following command: +# `sudo find /proc/sys/fs/binfmt_misc -type f -name 'qemu-*' -exec sh -c 'echo -1 > {}' \;` + +# Log Docker hub limit https://docs.docker.com/docker-hub/download-rate-limit/#how-can-i-check-my-current-rate +TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token) +curl --head -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest + +SCRIPT_DIR=$(dirname "$(readlink -f "${0}")") + +docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + +ARCH_NAME=s390x + +X_TOOLS_FILE=${ARCH_NAME}-x-tools +if [ ! -f "./ubuntu-x-tools/${X_TOOLS_FILE}.tar.xz" ]; then + wget "https://aws-libcrypto.s3.us-west-2.amazonaws.com/cross-compile-toolchains/host-x86_64-pc-linux-gnu/${X_TOOLS_FILE}.tar.xz" + mv ${X_TOOLS_FILE}.tar.xz ./ubuntu-x-tools/ +fi + +BUILDER_NAME=${ARCH_NAME}-builder +if ! docker buildx inspect ${BUILDER_NAME}; then + docker buildx create --name ${BUILDER_NAME} --use +fi + +docker buildx build -t ubuntu-${ARCH_NAME}:test "${SCRIPT_DIR}"/ubuntu-test --load +docker buildx build -t ubuntu-${ARCH_NAME}:x-tools "${SCRIPT_DIR}"/ubuntu-x-tools --load + +docker buildx rm ${BUILDER_NAME} diff --git a/tests/docker_images/linux-s390x/ubuntu-test/Dockerfile b/tests/docker_images/linux-s390x/ubuntu-test/Dockerfile new file mode 100644 index 0000000000..87ba1868f8 --- /dev/null +++ b/tests/docker_images/linux-s390x/ubuntu-test/Dockerfile @@ -0,0 +1,23 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 OR ISC + +FROM --platform=$BUILDPLATFORM s390x/ubuntu + +SHELL ["/bin/bash", "-c"] + +# Note: valgind was not available on this platform +RUN apt-get update && apt-get install -y \ + git gcc g++ cmake golang gdb gdbserver \ + libclang-dev clang \ + build-essential \ + ssh \ + rsync \ + tar \ + python3 \ + && apt-get clean + +EXPOSE 7777 + +ENV GOCACHE=/tmp + +CMD ["/bin/bash"] diff --git a/tests/docker_images/linux-s390x/ubuntu-x-tools/Dockerfile b/tests/docker_images/linux-s390x/ubuntu-x-tools/Dockerfile new file mode 100644 index 0000000000..6efb51e364 --- /dev/null +++ b/tests/docker_images/linux-s390x/ubuntu-x-tools/Dockerfile @@ -0,0 +1,40 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 OR ISC + +FROM ubuntu:22.04 + +RUN apt-get update && apt-get install -y \ + git cmake golang gdb gdbserver valgrind \ + libclang1 \ + build-essential \ + ssh \ + rsync \ + tar \ + python3 \ + xz-utils \ + ninja-build + +RUN apt-get install -y \ + qemu-system-s390x \ + qemu-user \ + qemu-user-binfmt + +RUN apt-get clean + +COPY s390x-x-tools.tar.xz / +RUN tar Jxvf s390x-x-tools.tar.xz -C / && rm /s390x-x-tools.tar.xz +COPY s390x.cmake / + +EXPOSE 1234 + +ENV GOCACHE=/tmp \ + CMAKE_TOOLCHAIN_FILE=/s390x.cmake \ + CMAKE_SYSTEM_NAME=Linux \ + CMAKE_SYSTEM_PROCESSOR=s390x \ + PATH="${PATH}:/s390x-ibm-linux-gnu/bin/" \ + CMAKE_C_COMPILER=/s390x-ibm-linux-gnu/bin/s390x-ibm-linux-gnu-gcc \ + CMAKE_CXX_COMPILER=/s390x-ibm-linux-gnu/bin/s390x-ibm-linux-gnu-g++ \ + CMAKE_SYSROOT=/s390x-ibm-linux-gnu/s390x-ibm-linux-gnu/sysroot \ + CMAKE_GENERATOR=Ninja + +CMD ["/bin/bash"] diff --git a/tests/docker_images/linux-s390x/ubuntu-x-tools/s390x.cmake b/tests/docker_images/linux-s390x/ubuntu-x-tools/s390x.cmake new file mode 100644 index 0000000000..d112e55b41 --- /dev/null +++ b/tests/docker_images/linux-s390x/ubuntu-x-tools/s390x.cmake @@ -0,0 +1,12 @@ +# Specify the target system +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_PROCESSOR s390x) + +# Specify the cross-compiler +set(CMAKE_C_COMPILER /s390x-ibm-linux-gnu/bin/s390x-ibm-linux-gnu-gcc) +set(CMAKE_CXX_COMPILER /s390x-ibm-linux-gnu/bin/s390x-ibm-linux-gnu-g++) + +# Specify the sysroot for the target system +set(CMAKE_SYSROOT /s390x-ibm-linux-gnu/s390x-ibm-linux-gnu/sysroot) +set(CMAKE_GENERATOR Ninja) +set(ENABLE_EXPERIMENTAL_BIG_ENDIAN_SUPPORT true)