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

Fix VS2022 build error #7502

Merged
merged 2 commits into from
Apr 28, 2023
Merged

Conversation

daverodgman
Copy link
Contributor

@daverodgman daverodgman commented Apr 26, 2023

Description

Fixes #7501 .

Gatekeeper checklist

  • changelog not needed
  • backport not in 2.28
  • tests already covered

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@daverodgman daverodgman added bug needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review labels Apr 26, 2023
@@ -109,7 +109,38 @@
* \return The number of leading zero bits in \p a, if \p a != 0.
* If \p a == 0, the result is undefined.
*/
size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
static inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's weird that arranging to have two copies of the function (since it's used in two different C files) makes the code smaller. How is this happening? Is this maybe only on platforms that have a CLZ instruction — what's the impact on platforms that don't?

Copy link
Contributor

Choose a reason for hiding this comment

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

Without clz there’s still a 16-byte saving in bignum_core.o, it's just that it is 20 bytes bigger without clz in both development and this PR (code size goes from 2446 to 2466 without clz)

Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm Apr 26, 2023

Choose a reason for hiding this comment

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

Ah - the clz function is only used in bignum.o if we don't have MBEDTLS_HAVE_UDBL

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 suspect a clz instruction is near-universal. E.g., Arm has had it since Armv5. So this function will almost always turn into a single instruction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to get inlined regardless of marking it static inline (at least on my machine where the clz instruction is present). So the effect of the static inline is really just to eliminate the not-inlined copy of the function, which is small and presumably eliminated during the final link stage. Do we want to simplify and drop the inlining?

Copy link
Contributor

Choose a reason for hiding this comment

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

static in a header gives you a copy of the function in every module that includes the header. Most compilers are smart enough to eliminate the function if the module doesn't use it at all, but that still leaves two copies in the two modules that do use this particular function (and possibly more later). So no, we shouldn't have static functions in a header.

Copy link
Contributor

Choose a reason for hiding this comment

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

Err, actually static restricts visibility. If nothing needs an actual not-inlined function, the compiler is free to not include it, and I would expect most modern compilers will do this. An out-of-line function implementation is usually only needed if the address of the function is taken at some point

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 meant: should we return it to bignum_core.c, as a non-inline, non-static function, and retain a declaration only in the header?

Copy link
Contributor

Choose a reason for hiding this comment

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

This PR saved bytes over the code in development. I think it is okay

Copy link
Contributor

Choose a reason for hiding this comment

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

should we return it to bignum_core.c, as a non-inline, non-static function, and retain a declaration only in the header?

Yes, we should do the simple thing (declare in header, define in .c) unless there's a compelling reason to do otherwise. And it seems that there's no compelling reason after all.

@tom-cosgrove-arm
Copy link
Contributor

I'd like the know why the CI didn't catch this problem. Is it just Visual Studio 2022? In which case, we really need to update the compiler versions we use...

@@ -33,39 +33,6 @@
#include "bn_mul.h"
#include "constant_time_internal.h"

Copy link
Contributor

Choose a reason for hiding this comment

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

What could we put in our CI to ensure that if someone tries this again, the CI will complain?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I'd like to know that too!

@tom-cosgrove-arm
Copy link
Contributor

I'd like the know why the CI didn't catch this problem. Is it just Visual Studio 2022? In which case, we really need to update the compiler versions we use...

Hmm, seen in the CI logs this morning

   319>test_suite_bignum_core.generated.obj : error LNK2019: unresolved external symbol _mbedtls_mpi_core_clz referenced in function _test_mpi_core_clz_wrapper [C:\builds\workspace\mbed-tls-nightly-tests\worktrees\tmpxsl0yyyt\cmake_solution\tests\test_suite_bignum_core.generated.vcxproj]
   319>C:\builds\workspace\mbed-tls-nightly-tests\worktrees\tmpxsl0yyyt\cmake_solution\tests\Release\test_suite_bignum_core.generated.exe : fatal error LNK1120: 1 unresolved externals [C:\builds\workspace\mbed-tls-nightly-tests\worktrees\tmpxsl0yyyt\cmake_solution\tests\test_suite_bignum_core.generated.vcxproj]
   319>Done Building Project "C:\builds\workspace\mbed-tls-nightly-tests\worktrees\tmpxsl0yyyt\cmake_solution\tests\test_suite_bignum_core.generated.vcxproj" (Rebuild target(s)) -- FAILED.
:
:
+---------+---------------+--------------+------------+--------------------+------------------+--------------------+----------------+-------------------+
| Version | Configuration | Architecture | Retargeted |   shipped build    | shipped selftest |    cmake build     | cmake selftest | cmake test suites |
+---------+---------------+--------------+------------+--------------------+------------------+--------------------+----------------+-------------------+
|   2015  |    Release    |    Win32     |   False    |        Pass        |       Pass       | Pass with warnings |      Pass      |        Pass       |
|   2015  |    Release    |    Win32     |    True    |        Pass        |       Pass       |        Fail        |    Not Run     |      Not Run      |
|   2015  |    Release    |     x64      |   False    |        Pass        |       Pass       | Pass with warnings |      Pass      |        Pass       |
|   2015  |    Release    |     x64      |    True    |        Pass        |       Pass       |        Fail        |    Not Run     |      Not Run      |
|   2015  |     Debug     |    Win32     |   False    | Pass with warnings |       Pass       | Pass with warnings |      Pass      |        Pass       |
|   2015  |     Debug     |    Win32     |    True    | Pass with warnings |       Pass       | Pass with warnings |      Pass      |        Pass       |
|   2015  |     Debug     |     x64      |   False    | Pass with warnings |       Pass       | Pass with warnings |      Pass      |        Pass       |
|   2015  |     Debug     |     x64      |    True    | Pass with warnings |       Pass       | Pass with warnings |      Pass      |        Pass       |
+---------+---------------+--------------+------------+--------------------+------------------+--------------------+----------------+-------------------+

@gilles-peskine-arm
Copy link
Contributor

Hmm, seen in the CI logs this morning

I remember we have a discrepancy in the set of Windows tests we run in nightlies vs in pull requests: PR jobs only test with VS2013, but nightlies also test VS2015 and VS2017. To my recollection this is the first time the VS2013 job passes but not the more recent versions (which is why we didn't really care about the discrepancy until now).

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
@daverodgman daverodgman removed the needs-ci Needs to pass CI tests label Apr 28, 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.

This needs to go in to fix the nightlies

@tom-cosgrove-arm tom-cosgrove-arm added the priority-very-high Highest priority - prioritise this over other review work label Apr 28, 2023
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

Keeping things simple, I like it.

@gilles-peskine-arm gilles-peskine-arm merged commit 7351101 into Mbed-TLS:development Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 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 priority-very-high Highest priority - prioritise this over other review work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Undefined mbedtls_mpi_core_clz
3 participants