Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cpuid for win32 #8339

Merged

Conversation

lpy4105
Copy link
Contributor

@lpy4105 lpy4105 commented Oct 10, 2023

Description

Fix: #8334 and #8332

cpuid.h and intrin.h define __cpuid with different signatures. We include intrin.h for WIN32 but we only use the correct signature for MSVC. This cause problem when build WIN32 target with non-MSVC compilers (i.g clang or i686-w64-mingw32).

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

`__cpuid` has two kinds of signatures in different
headers depending on the target OS. We make it
consistent between the usages ang the included header.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Change the type of array that stores the cpuinfo
data to int[4] to match the signature of `__cpuinfo`
in `intrin.h` header file.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@lpy4105 lpy4105 added bug needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most) labels Oct 10, 2023
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

library/aesni.c Outdated
static unsigned info[4] = { 0, 0, 0, 0 };
#if defined(_MSC_VER)
static int info[4] = { 0, 0, 0, 0 };
#if defined(_WIN32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's difference between GCC and MSC. I prefer change ln 36 to #if !defined(_MSC_VER)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not work when using clang compiler driver under Visual Studio, which is becoming more common.

(When clang is run under Linux, it sets __GNUC__, but when run as compiler driver under Visual Studio, it sets _MSC_VER. This definitely causes a problem for inline assembly (since clang format is not the same as MSVC format) - I need to test the intrinsics)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tom-cosgrove-arm , Yes, that might be a problem. Does that mean both macro will be defined in this case ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not work when using clang compiler driver under Visual Studio, which is becoming more common.

I've tested it, and no related issue. When clang is running in MSVC compatibility mode, it sets _MSC_VER and provide a compatible intrin.h.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking

@yuhaoth yuhaoth removed the needs-reviewer This PR needs someone to pick it up for review label Oct 11, 2023
MinGW provides both kinds of implementations of `__cpuid`,
but since `cpuid.h` is provided by GNUC, so we should choose
the implementation by the compiler type instead of OS type.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@@ -4626,8 +4626,7 @@ component_test_m32_o0 () {
# build) and not the i386-specific inline assembly.
msg "build: i386, make, gcc -O0 (ASan build)" # ~ 30s
scripts/config.py full
scripts/config.py unset MBEDTLS_AESNI_C # AESNI depends on cpu modifiers
make CC=gcc CFLAGS="$ASAN_CFLAGS -m32 -O0" LDFLAGS="-m32 $ASAN_CFLAGS"
make CC=gcc CFLAGS="$ASAN_CFLAGS -m32 -O0 -maes -msse2 -mpclmul" LDFLAGS="-m32 $ASAN_CFLAGS"
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change being made?

The comment at line 4625 is explicit: this component is intended to test the portable C code in a 32-bit build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to revert the workarrounds added in #7384, that's why I removed the previous line 4629, and the additional CFLAGS were just for a successful build.
I have just realize that all AESNI m32 test are in a separate component, so only reverting the work arround in component_build_mingw should be sufficient?

BTW, I don't quite understand the comments in line 4625 and line 4642. Why different optimization levels make a difference on using or not using i386-specific inline assembly?

@beni-sandu
Copy link
Contributor

At the moment, 3.5.0 compilation fails on 32-bit x86 for both clang and gcc, on Linux. Not sure if this is related in any way to this issue, but I will try to have a closer look next week.

Error is the same on both gcc/clang:

In file included from /buildarea/bsandu/poky-container/poky/build/tmp/work/core2-32-poky-linux/mbedtls/3.5.0/git/library/aesni.c:29:
| /buildarea/bsandu/poky-container/poky/build/tmp/work/core2-32-poky-linux/mbedtls/3.5.0/git/library/aesni.h:81:5: error: #error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C"
|    81 | #   error "Must use `-mpclmul -msse2 -maes` for MBEDTLS_AESNI_C"
|       |     ^~~~~

AESNI for x86 (32-bit) have been tested in
a seperate component, we don't need to test
twice.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
yuhaoth
yuhaoth previously approved these changes Oct 16, 2023
Copy link
Contributor

@yuhaoth yuhaoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


# note Make tests only builds the tests, but doesn't run them
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror' WINDOWS_BUILD=1 tests
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -maes -msse2 -mpclmul' WINDOWS_BUILD=1 tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be replace with Pragma or function attribute in future.

Copy link
Contributor

@yuhaoth yuhaoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

yuhaoth
yuhaoth previously approved these changes Oct 17, 2023
Copy link
Contributor

@yuhaoth yuhaoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Please verify the VS compiler driver case raised by @tom-cosgrove-arm. I guess this change can cover the case

@@ -5114,16 +5114,15 @@ component_test_tls13_only_record_size_limit () {

component_build_mingw () {
msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
scripts/config.py unset MBEDTLS_AESNI_C # AESNI depends on cpu modifiers
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib programs
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra -maes -msse2 -mpclmul' WINDOWS_BUILD=1 lib programs
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to make lib without MBEDTLS_AESNI_C to check that it works on mingw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to test the build of pure C impl of AES? I think they have been covered because MBEDTLS_AES_USE_HARDWARE_ONLY is unset in defaul config.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I mean to check that the C code builds under mingw, which is quite a "strange" environment - just lib, not programs, which shouldn't take long

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't get the point. We do build lib and programs and later tests for both static and dynamic linked libraries in this mingw test...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with this PR, under mingw we are now building only with MBEDTLS_AESNI_C set, and not without it? So we are no longer checking that we build correctly under mingw without MBEDTLS_AESNI_C. If that's right, I think we should add a build of lib on its own without MBEDTLS_AESNI_C

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
msg "build: Windows cross build - mingw64, make (Link Library, default config without MBEDTLS_AESNI_C)" # ~ 30s
./scripts/config.py unset MBEDTLS_AESNI_C #
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib programs
make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror' WINDOWS_BUILD=1 tests
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the config without MBEDTLS_AESNI_C we only need to make lib - we don't need to build programs or tests. The main reason for doing this is to ensure that the strange mingw environment (not quite *nix, not quite Windows) doesn't cause something related to AES-NI to get mis-compiled in. Outside of the aes*.c modules this is very unlikely, and will add unnecessary time to CI runs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to test DLL either, I think?

@tom-cosgrove-arm
Copy link
Contributor

Also: do we need a ChangeLog entry for this?

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Bugfix
* Fix an inconsistency between implementations and usages of `__cpuid`,
which mainly causes failures when building Windows target using
mingw or clang. Fix #8334 & #8332.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) We normally says "Fixes #nnnn and #nnn2"

$ fgrep 'Fixes #' ChangeLog | wc -l
     184
$ fgrep 'Fix #' ChangeLog | wc -l
       9

Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tom-cosgrove-arm
Copy link
Contributor

Can you update the "backport done, or not required" text with "backport not required because ..." at the top, please?

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@tom-cosgrove-arm tom-cosgrove-arm added the needs-backports Backports are missing or are pending review and approval. label Oct 19, 2023
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tom-cosgrove-arm
Copy link
Contributor

@yuhaoth can you re-review please? Also, do you have bandwidth to look at the backport?

Copy link
Contributor

@yuhaoth yuhaoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@tom-cosgrove-arm tom-cosgrove-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Oct 23, 2023
@tom-cosgrove-arm tom-cosgrove-arm added this pull request to the merge queue Oct 23, 2023
Merged via the queue into Mbed-TLS:development with commit 235e361 Oct 23, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug component-crypto Crypto primitives and low-level interfaces needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to compile with 32 bits using Mingw on mbedTLS v 3.5.0
4 participants