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

Pass c99 to compiler #3641

Merged
merged 5 commits into from
May 12, 2022
Merged

Pass c99 to compiler #3641

merged 5 commits into from
May 12, 2022

Conversation

okhowang
Copy link
Contributor

@okhowang okhowang commented Sep 3, 2020

Fixes #3631

Signed-off-by: okhowang(王沛文) okhowang@tencent.com

Notes:

  • Pull requests cannot be accepted until the PR follows the contributing guidelines. In particular, each commit must have at least one Signed-off-by: line from the committer to certify that the contribution is made under the terms of the Developer Certificate of Origin.
  • This is just a template, so feel free to use/remove the unnecessary things

Description

Pass standard c99 to compiler

Status

READY

Requires Backporting

No

Migrations

NO

Additional comments

Todos

  • Tests
  • Documentation
  • Changelog updated
  • Backported

Steps to test or reproduce

@daverodgman daverodgman added the needs-review Every commit must be reviewed by at least two team members, label Sep 3, 2020
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

I have a few questions.

CMakeLists.txt Outdated
@@ -154,6 +154,10 @@ string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")

include(CheckCCompilerFlag)

if (NOT CMAKE_VERSION VERSION_LESS 3.1.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CMAKE_C_STANDARD is available from cmake 3.1
ref: https://cmake.org/cmake/help/v3.1/release/3.1.0.html

CMakeLists.txt Outdated
@@ -175,6 +179,9 @@ if(CMAKE_COMPILER_IS_GNU)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness")
endif()
endif()
if (NOT CMAKE_C_STANDARD AND GCC_VERSION VERSION_LESS 5.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about to always set flag, whatever the compiler version. For later versions as it is their default it shouldn't matter and if at some point their default move to c11 we are still good.
Otherwise why not doing the same for the arm compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For later versions as it is their default it shouldn't matter and if at some point their default move to c11 we are still good.

agree.

Otherwise why not doing the same for the arm compiler?

if cmake version < 3.1, there is no portable approach to set -std=c99 for all compiler, we should do it by different compiler.
I add flags for gcc only currently.
BTY, what compiler should we support?

Copy link
Contributor

Choose a reason for hiding this comment

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

why not doing the same for the arm compiler?

I don't think Cmake has any support for Arm compilers. And it shouldn't try to invoke an Arm Compiler unless explicitly requested, because that could cause an unexpected network access to check the license. People who use a “specialist” compiler like Arm Compiler generally have their build environment set up to pass target-specific flags anyway, so Cmake defaults aren't particularly useful for them. The Cmake defaults are mostly useful for non-cross builds where the user just wants to build without fuss.

Makefile Outdated
@@ -1,5 +1,8 @@
DESTDIR=/usr/local
PREFIX=mbedtls_
override CFLAGS+=-std=gnu99
MAKEOVERRIDES += CFLAGS+=-std=gnu99
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this ok with ARM compiler?

Copy link
Contributor

Choose a reason for hiding this comment

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

No (CI confirms), and it's not ok with many other compilers either. Our makefile should be compiler-agnostic, at least if you pass a different WARNING_CFLAGS argument. I can't think of a case where an override in the makefile would be acceptable: when it comes to compiler flags, the makefile suggests, but the user decides.

Copy link
Contributor

Choose a reason for hiding this comment

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

If _GNU_SOURCE or some such is needed to support a library feature, the #define needs to go in the appropriate .c or .h file. If the code is using a GNU extension to the C language, it shouldn't, or if the feature is optional (e.g. extra safety or optimization), it should be guarded by conditional compilation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

-std=c99 and -std=gnu99 are made by C_EXTENSIONS in cmake.
and CMAKE_C_EXTENSIONS is ON default.
so we need set it to OFF when cmake?

we should set different flag for different compiler also.
but I have no idea about how do it in Makefile

Copy link
Contributor

Choose a reason for hiding this comment

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

Passing -std=c99 when you know that the compiler supports it (GCC, Clang, maybe other compilers with a gcc-compatible command line) is ok. But don't pass it if you don't know. And don't pass -std=gnu99: our code shouldn't require any GNU extension.

The makefiles are designed to be portable as long as you have GNU make. At least make lib should work on any OS with any compiler that accepts the options -o and -c. They don't even assume Unix utilities, and targets like make clean that assume Unix utilities (such as rm) have a Windows alternative (del). I don't think it's realistic to check the compiler in the makefiles. Note that cross-compilation is very common.

It's ok if some compilers require passing different flags when compiling, for example passing make WARNING_CFLAGS="-Wno-something" or make CFLAGS="-mfoo -fno-weird-optimization". We expect the default to work on the platforms that people use the most (at least when not cross-compiling), but we know we can't cover them all. It's not ok if the makefile passes -std=c99 to the compiler whether the user wanted it or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so we shouldn't add c99 flag in Makefile?

Copy link
Contributor

Choose a reason for hiding this comment

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

so we shouldn't add c99 flag in Makefile?

Right. Unless you find a way to do so that does the right thing for compilers that don't support -std=, is OS-agnostic, and doesn't make people who aren't GNU make experts want to gouge their eyes out (GNU make is a difficult language once you get past the basics).

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 have revert changes in Makefile

@okhowang
Copy link
Contributor Author

I current add c99 flag for gcc/clang in Makefile/CMake.
IAR and MSVC has no necessary to add c99 flag.
ref: https://github.com/Kitware/CMake/blob/master/Modules/Compiler/MSVC-C.cmake and https://github.com/Kitware/CMake/blob/master/Modules/Compiler/IAR-C.cmake

is there any other compiler that we need support?

@gilles-peskine-arm
Copy link
Contributor

gilles-peskine-arm commented Sep 25, 2020

is there any other compiler that we need support?

In CMake: no, it's fine. Other compilers should still be usable, but they don't need to be usable out of the box: it's ok if they require explicit CFLAGS.

CMakeLists.txt Outdated
@@ -154,6 +154,11 @@ string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")

include(CheckCCompilerFlag)

if (NOT CMAKE_VERSION VERSION_LESS 3.1.0)
set(CMAKE_C_STANDARD 99)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it a problem to not use this at all (remove those 4 lines) and to do "set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")" unconditionally for GNU and ARM compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add -std=c99 to CMAKE_C_FLAGS for GNU and ARM compiler is simple.
but CMAKE_C_STANDARD and CMAKE_C_EXTENSIONS is standard variables in CMake.
although they are exists after CMake 3.1.0
adding -std=c99 and some other compiler-specific script should be removed eventually after we drop support for old CMake version

Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't followed all the conversation so I'm not sure if this is relevant. For information, we try to support maintained versions of Ubuntu and CentOS, which currently means CMake back to 2.8.12.2. And armcc doesn't support -std.

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm Sep 30, 2020

Choose a reason for hiding this comment

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

ok, I am convinced with your arguments, let's keep the setting of CMAKE_C_STANDARD. Rather than:
if (NOT CMAKE_VERSION VERSION_LESS 3.1.0), I would prefer though:
if (CMAKE_VERSION VERSION_GREATER ... ).

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 need a expression cmake >= 3.1.0.
but VERSION_GREATER_EQUAL cmake introduced in cmake 3.7.
so we have to use not NOT CMAKE_VERSION VERSION_LESS 3.1.0

@ronald-cron-arm
Copy link
Contributor

Otherwise in the CI the all.sh test component "test_everest: build: Everest ECDH context (ASan build)" is falling. Here is the part of the CI logs reporting the error:

[ 14%] Building C object library/CMakeFiles/mbedcrypto.dir/sha1.c.o
[ 15%] Building C object library/CMakeFiles/mbedcrypto.dir/__/3rdparty/everest/library/Hacl_Curve25519_joined.c.o
[ 15%] Built target mbedtls_test
In file included from /var/lib/build/3rdparty/everest/library/Hacl_Curve25519_joined.c:31:
/var/lib/build/3rdparty/everest/library/Hacl_Curve25519.c:417:17: error: implicit declaration of function 'le64toh' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
uint64_t i0 = load64_le(input);
^
/var/lib/build/3rdparty/everest/include/everest/kremlin/c_endianness.h:199:23: note: expanded from macro 'load64_le'
#define load64_le(b) (le64toh(load64(b)))
^
In file included from /var/lib/build/3rdparty/everest/library/Hacl_Curve25519_joined.c:31:
/var/lib/build/3rdparty/everest/library/Hacl_Curve25519.c:544:3: error: implicit declaration of function 'htole64' is invalid in C99 -Werror,-Wimplicit-function-declaration]
store64_le(b0, o0);
^
/var/lib/build/3rdparty/everest/include/everest/kremlin/c_endianness.h:200:38: note: expanded from macro 'store64_le'
#define store64_le(b, i) (store64(b, htole64(i)))
^
/var/lib/build/3rdparty/everest/library/Hacl_Curve25519.c:544:3: note: did you mean 'store64'?
/var/lib/build/3rdparty/everest/include/everest/kremlin/c_endianness.h:200:38: note: expanded from macro 'store64_le'
#define store64_le(b, i) (store64(b, htole64(i)))
^
/var/lib/build/3rdparty/everest/include/everest/kremlin/c_endianness.h:185:20: note: 'store64' declared here
inline static void store64(uint8_t *b, uint64_t i) {
^
2 errors generated.

library/CMakeFiles/mbedcrypto.dir/build.make:1670: recipe for target 'library/CMakeFiles/mbedcrypto.dir//3rdparty/everest/library/Hacl_Curve25519_joined.c.o' failed
make[2]: *** [library/CMakeFiles/mbedcrypto.dir/
/3rdparty/everest/library/Hacl_Curve25519_joined.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:418: recipe for target 'library/CMakeFiles/mbedcrypto.dir/all' failed
make[1]: *** [library/CMakeFiles/mbedcrypto.dir/all] Error 2
make: *** [all] Error 2
^^^^test_everest: build: Everest ECDH context (ASan build): command make -> 2^^^^
Makefile:138: recipe for target 'all' failed

@okhowang
Copy link
Contributor Author

okhowang commented Oct 1, 2020

htole64 and le64toh was defined in <endian.h> with _BSD_SOURCE macro.
I couldn't found it in everest.
and Hacl_Curve25519.c looks like a generated code.
How we fix this problem?

@gilles-peskine-arm
Copy link
Contributor

The Everest code is less portable than the rest of the library. If it needs #define _BSD_SOURCE to compile under a strict mode, we can add that somewhere. But preferably in a header that was not automatically generated.

@okhowang
Copy link
Contributor Author

okhowang commented Oct 9, 2020

I added _BSD_SOURCE in Hacl_Curve25519_joined.c.
and _DEFAULT_SOURCE was added additionly for some compiler's warning #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE".

@jk-arm
Copy link

jk-arm commented Oct 20, 2020

may i know when this fix expected to be part of the master?

@ronald-cron-arm ronald-cron-arm self-requested a review November 20, 2020 16:00
@gilles-peskine-arm gilles-peskine-arm added the needs-reviewer This PR needs someone to pick it up for review label Apr 6, 2021
@daverodgman daverodgman added needs-reviewer This PR needs someone to pick it up for review and removed needs-reviewer This PR needs someone to pick it up for review labels Apr 6, 2021
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Apr 27, 2021
@@ -18,6 +18,12 @@
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef _BSD_SOURCE
Copy link
Contributor

Choose a reason for hiding this comment

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

For unusual things like this it is useful to have a comment that explains why they are needed

Copy link
Contributor

Choose a reason for hiding this comment

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

err, yes, but why? what happens if you don't have it?

okhowang and others added 2 commits May 11, 2022 16:26
Fixes Mbed-TLS#3631

Signed-off-by: okhowang(王沛文) <okhowang@tencent.com>
Disable compiler-specific extensions (e.g. use --std=c11 instead
of --std=gnu11).

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@daverodgman daverodgman added size-s Estimated task size: small (~2d) priority-medium Medium priority - this can be reviewed as time permits labels May 11, 2022
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@@ -73,9 +73,10 @@ int main( void )
#endif
#endif /* _MSC_VER */
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
#if defined(MBEDTLS_HAVE_TIME)
#if defined(MBEDTLS_HAVE_TIME) || (defined(MBEDTLS_TIMING_C) && !defined(MBEDTLS_TIMING_ALT))
Copy link
Contributor

Choose a reason for hiding this comment

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

@AndrzejKurek LOL :)

(@daverodgman don't worry, just flash-backs to a previous PR!)

@@ -18,6 +18,12 @@
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm May 11, 2022

Choose a reason for hiding this comment

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

So actually I think this should be

Suggested change
#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */
/* Required to get htole64() from gcc/glibc's endian.h (older systems)
* when we compile with -std=c99 */
#define _BSD_SOURCE

#define _BSD_SOURCE /* Required to enable compilation under --std=c99 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#define _DEFAULT_SOURCE
/* (modern version of _BSD_SOURCE) */
#define _DEFAULT_SOURCE

Copy link
Contributor

Choose a reason for hiding this comment

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

See https://stackoverflow.com/a/29201732/7761803 (not clear that _BSD_SOURCE is still required, but let's continue to support older systems)

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@daverodgman daverodgman added needs-review Every commit must be reviewed by at least two team members, and removed needs-ci Needs to pass CI tests labels May 12, 2022
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

@davidhorstmann-arm can you re-review please?

Copy link
Contributor

@davidhorstmann-arm davidhorstmann-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 - thanks!

@daverodgman daverodgman 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 May 12, 2022
@daverodgman
Copy link
Contributor

CI failure is spurious

@daverodgman daverodgman merged commit d87e46f into Mbed-TLS:development May 12, 2022
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 component-platform Portability layer and build scripts historical-reviewing Currently reviewing (for legacy PR/issues) priority-medium Medium priority - this can be reviewed as time permits size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build error (compiler not in C99 mode) The build fails out of the box with GCC 4
8 participants