← Back to Pull requests

Add the needed bits for compiling with link-time optimization. #109

Open
besser82 opened this pull request with 131 lines diff across 12 files
View on GitHub
besser82 besser82 commented on Aug 13, 2020 17:05 UTC
Author

GCC 10.2 and LLVM/Clang 10 offer initial support for building libraries, that are using symbol versioning features, with LTO.

To make use of this with GCC 10.2, the exported versioned symbols need to be declared explicitly with __attribute__((symver (...))).

LLVM/Clang 10 supports symbol versioning with LTO out of the box without any changes needed.

Fixes #24.

Diff: 131 lines (+106 / -25) across 12 files Reviewer: zackw Review duration: 3239530m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.55) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.55 · 131 lines diff across 12 files
0.55
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
88%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.292
Real PR Conversation Timeline
22 items
codecov[bot] codecov[bot] commented on Aug 13, 2020 18:05 UTC
Bot

Codecov Report


Merging #109 into develop will decrease coverage by 0.13%.

The diff coverage is n/a.

![Impacted file tree graph](https://codecov.io/gh/besser82/libxcrypt/pull/109?src=pr&el=tree)

@@             Coverage Diff             @@
##           develop     #109      +/-   ##
===========================================
- Coverage    94.48%   94.34%   -0.14%     
===========================================
  Files           32       32              
  Lines         3750     3764      +14     
===========================================
+ Hits          3543     3551       +8     
- Misses         207      213       +6     

| Impacted Files | Coverage Δ | |
|---|---|---|
| lib/crypt-yescrypt.c | 82.00% <0.00%> (-6.64%) | :arrow_down: |
| lib/crypt-des.c | 91.13% <0.00%> (-0.79%) | :arrow_down: |
| lib/crypt.c | 95.16% <0.00%> (+0.11%) | :arrow_up: |

------

Continue to review full report at Codecov.

Legend - Click here to learn more

Δ = absolute <relative> (impact), ø = not affected, ? = missing data

Powered by Codecov. Last update 328563b...2e443c5. Read the comment docs.

zackw zackw commented on Aug 13, 2020 19:41 UTC
Reviewer

Is this really enough to make it work? I was under the impression we needed __attribute__ ((symver)) as well (see GCC bug 48200).

Why is it necessary to use -flto-partition=none?

I was under the impression one ought to set AR, CC, NM, RANLIB, and CFLAGS as arguments to configure, e.g.

./configure CC="gcc" AR="gcc-ar" NM="gcc-nm" RANLIB="gcc-ranlib" CFLAGS="-g -O2 -flto -flto-partition=none" ...

The change to use ${NM-nm} in test/symbols-*.sh is approved independent of the rest of the patch, please go ahead and push it separately.

besser82 besser82 commented on Aug 13, 2020 21:41 UTC
Author

@zackw I've fixed your concerns in the rebased commits.

Is this really enough to make it work? I was under the impression we needed __attribute__ ((symver)) as well (see GCC bug 48200).

For LLVM/Clang it simply works that way. For GCC 10.2 one either needs to implement __attribute__ ((symver)) for symbol versioning, or pass -flto -flto-partition=none to skip automatic partitioning of the symbol table, resolved by the linker, which would drop all exported versioned symbols not used by the library itself.

Why is it necessary to use -flto-partition=none?

This is necessary, if one does NOT use __attribute__ ((symver)) with GCC and LTO enabled; see above.
Anyways, my rebased commits use the new symver attribute with GCC 10.2. LLVM/Clang doesn't support it nor needs it.

besser82 besser82 commented on Aug 15, 2020 11:24 UTC
Author

@zackw Tested and works with GCC 10.2, LTO enabled: https://koji.fedoraproject.org/koji/buildinfo?buildID=1593132

besser82 besser82 commented on Aug 18, 2020 14:23 UTC
Author

@zackw Would you mind to have another look here?

solardiz solardiz commented on Aug 18, 2020 14:31 UTC
Contributor

Before merging this, would anyone please comment on the concerns I brought up in https://github.com/besser82/libxcrypt/issues/24#issuecomment-674139907

zackw zackw commented on code on Aug 18, 2020 15:14 UTC
Code Review
lib/crypt-port.h:1
@@ -179,11 +179,29 @@ _crypt_strcpy_or_abort (void *, const size_t, const void *);
 # define _strong_alias(name, aliasname) \
   extern __typeof (name) aliasname __THROW __attribute__ ((alias (#name)))
 
+/* Starting with GCC 10.2, we can use the symver attribute, which also works
+   with link-time optimization enabled.  */
+# if __GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 2)
+
+/* Referencing specific _compatibility_ symbols still needs inline asm.  */
+# define _symver_ref(extstr, intname, version) \
+  __asm__ (".symver " #intname "," extstr "@" #version)
+
+/* Set the symbol version for EXTNAME, which uses INTNAME as its
+   implementation.  */
+# define symver_set(extstr, intname, version, mode) \
+  extern __typeof (intname) intname __THROW \
+    __attribute__((symver (extstr mode #version)))

The order of operations here briefly confused me into thinking the #if conditional was backward. Can you please rearrange it like this?

#if __GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 2)
/* Starting with GCC 10.2, we can use the symver attribute to define symbol versions.
   This has the advantage of working correctly with link-time optimization.  */

/* Set the symbol version for EXTSTR, which uses INTNAME as its
   implementation. */
# define symver_set(extstr, intname, version, mode) // ...

/* Referencing specific _compatibility_ symbols still needs inline asm.  */
# define _symver_ref(extstr, intname, version) // ...

zackw zackw commented on code on Aug 18, 2020 15:16 UTC
Code Review
libxcrypt.spec.rpkg:1
@@ -151,11 +151,6 @@ fi                                          \
 %global _ld_strict_symbol_defs 1
 
 
-# The library uses symbol versioning in way
-# that is not compatible with LTO currently.
-%undefine _lto_cflags

Per @solardiz's concerns, let's _not_ do this yet. Instead maybe just change the comment to "The library can be built with LTO using GCC >=10.2, but this mode is still experimental and not recommended for production."

zackw zackw requested changes on Aug 18, 2020 15:20 UTC
Changes Requested Reviewer

I have to say I share @solardiz's concerns about exposing latent UB. I would be okay with merging the changes to crypt-port.h, documenting that an LTO-using build is now possible but _experimental_ and discouraged for production use, ~~and not changing libxcrypt.spec.rpkg yet.~~ [never mind that last bit, I thought it was the spec for what actually goes into Fedora]

If we made the configure script detect LTO and force --disable-obsolete-api when it did, do you think that would be sufficient to "ensure that build wouldn't be inadvertently usable in production", @solardiz ?

zackw zackw commented on Aug 18, 2020 15:21 UTC
Contributor

Also, just to verify, the xcrypt_symbol annotations are no longer necessary with this version of the patch?

besser82 besser82 commented on code on Aug 18, 2020 15:30 UTC
Code Review
libxcrypt.spec.rpkg:1
@@ -151,11 +151,6 @@ fi                                          \
 %global _ld_strict_symbol_defs 1
 
 
-# The library uses symbol versioning in way
-# that is not compatible with LTO currently.
-%undefine _lto_cflags

Well, the purpose of this file is to build a new rpm from each commit to develop explicitly for testing. See: https://copr.fedorainfracloud.org/coprs/besser82/libxcrypt_CI/

Those builds need an explicit activation by the user and are otherwise NOT installed nor propagated to production enviroments.

besser82 besser82 commented on Aug 18, 2020 15:32 UTC
Author

Also, just to verify, the xcrypt_symbol annotations are no longer necessary with this version of the patch?

Yes, that's correct.

zackw zackw commented on code on Aug 18, 2020 15:43 UTC
Code Review
libxcrypt.spec.rpkg:1
@@ -151,11 +151,6 @@ fi                                          \
 %global _ld_strict_symbol_defs 1
 
 
-# The library uses symbol versioning in way
-# that is not compatible with LTO currently.
-%undefine _lto_cflags

Oh, OK. I thought this was the spec file used for the builds actually going into Fedora. I retract this comment.

besser82 besser82 commented on code on Aug 18, 2020 15:45 UTC
Code Review
lib/crypt-port.h:1
@@ -179,11 +179,29 @@ _crypt_strcpy_or_abort (void *, const size_t, const void *);
 # define _strong_alias(name, aliasname) \
   extern __typeof (name) aliasname __THROW __attribute__ ((alias (#name)))
 
+/* Starting with GCC 10.2, we can use the symver attribute, which also works
+   with link-time optimization enabled.  */
+# if __GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 2)
+
+/* Referencing specific _compatibility_ symbols still needs inline asm.  */
+# define _symver_ref(extstr, intname, version) \
+  __asm__ (".symver " #intname "," extstr "@" #version)
+
+/* Set the symbol version for EXTNAME, which uses INTNAME as its
+   implementation.  */
+# define symver_set(extstr, intname, version, mode) \
+  extern __typeof (intname) intname __THROW \
+    __attribute__((symver (extstr mode #version)))

Will update this with the next rebase.

zackw zackw commented on Aug 18, 2020 15:45 UTC
Contributor

Can we get a CI build with all hashes enabled, LTO on, and ASan+UBSan also on? That would be a quick way to get _some_ more confidence that the code is UB-sound.

besser82 besser82 commented on Aug 18, 2020 15:49 UTC
Author

Can we get a CI build with all hashes enabled, LTO on, and ASan+UBSan also on? That would be a quick way to get _some_ more confidence that the code is UB-sound.

I will check if that's possible. We LLVM / Clang >= 7 on Travis for this. Or is this possible with GCC as well? We can use Fedora >= 32 to do it, if GCC supports that as well.

solardiz solardiz commented on Aug 18, 2020 17:25 UTC
Contributor

If we made the configure script detect LTO and force --disable-obsolete-api when it did, do you think that would be sufficient to "ensure that build wouldn't be inadvertently usable in production", @solardiz ?

I think that would be weird - mixing unrelated things together, and excluding the obsolete APIs from our own LTO stress-testing. If we do use LTO for stress-testing, let's use the opportunity fully.

besser82 besser82 commented on Aug 18, 2020 19:31 UTC
Author

!Codacy Here is an overview of what got changed by this pull request:


Issues
======
- Added 1
           

See the complete overview on Codacy

besser82 besser82 commented on code on Aug 18, 2020 19:31 UTC
Code Review
README.md:1
@@ -93,13 +93,29 @@ not always be able to retrieve cryptographically-sound random numbers
 from the operating system; if you call these functions with a null
 pointer for the “rbytes” argument, be prepared for them to fail.
 
-As of mid-2018, GCC and LLVM don’t support link-time optimization of
-libraries that use symbol versioning.  If you build libxcrypt with
-either of these compilers, do not use `-flto`.  See [GCC bug 48200][1]
-for specifics; the problem is very similar for LLVM.  Because this is,
-at its root, a set of missing compiler features, we expect link-time
-optimization won’t work in other C compilers either, but we haven’t
-tested it ourselves.
+As of August 2020, link-time optimization (`-flto`) of libraries that use
+symbol versioning is initially supported by GCC 10 and LLVM/Clang 10, if
+their compiler specific toolchain is used during compilation.
+
+For GCC the needed tools are called with:
+```

Codacy found an issue: [[fenced-code-flag] Missing code language flag](https://app.codacy.com/manual/besser82/libxcrypt/pullRequest?prid=6010975)

zackw zackw commented on Aug 18, 2020 20:10 UTC
Contributor

Every time you force-push this branch, I get multiple emails from Codacy about the missing syntax-highlighting annotation in the README. It's making it hard for me to tell what actually changed :-(

besser82 besser82 commented on Aug 18, 2020 21:39 UTC
Author

@zackw For some funny reason I can even reproduce the errors shown on TravisCI with GCC 10.2 + ASan,UBSan without LTO enabled… :S

The other funny thing is: If I replace crypt_r with crypt_rn in test/special-char-salt.c the tests succeeds…

Reviewers
Assignees
besser82 besser82
Labels
risk: low confidence: 88.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 55.3% (MEDIUM) · Higher = larger blast radius
55.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 88.8% · Lower = rushed or shallow review
88.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.2% · Safe to merge
6.2%