-
Notifications
You must be signed in to change notification settings - Fork 1k
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 symbol visibility issues, add test for it #1359
Open
hebasto
wants to merge
4
commits into
bitcoin-core:master
Choose a base branch
from
hebasto:230626-visibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef SECP256K1_LOCAL_VISIBILITY_H | ||
#define SECP256K1_LOCAL_VISIBILITY_H | ||
|
||
/* Global variable visibility */ | ||
/* See: https://github.com/bitcoin-core/secp256k1/issues/1181 */ | ||
#if !defined(_WIN32) && defined(__GNUC__) && (__GNUC__ >= 4) | ||
# define SECP256K1_LOCAL_VAR extern __attribute__ ((visibility ("hidden"))) | ||
#else | ||
# define SECP256K1_LOCAL_VAR extern | ||
#endif | ||
|
||
#endif /* SECP256K1_LOCAL_VISIBILITY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python3 | ||
"""Check that a libsecp256k1 shared library exports only expected symbols. | ||
|
||
Usage examples: | ||
- When building with Autotools: | ||
./tools/symbol-check.py .libs/libsecp256k1.so | ||
./tools/symbol-check.py .libs/libsecp256k1-<V>.dll | ||
./tools/symbol-check.py .libs/libsecp256k1.dylib | ||
|
||
- When building with CMake: | ||
./tools/symbol-check.py build/lib/libsecp256k1.so | ||
./tools/symbol-check.py build/bin/libsecp256k1-<V>.dll | ||
./tools/symbol-check.py build/lib/libsecp256k1.dylib""" | ||
|
||
import re | ||
import sys | ||
import subprocess | ||
|
||
import lief | ||
|
||
|
||
class UnexpectedExport(RuntimeError): | ||
pass | ||
|
||
|
||
def get_exported_exports(library) -> list[str]: | ||
"""Adapter function to get exported symbols based on the library format.""" | ||
if library.format == lief.Binary.FORMATS.ELF: | ||
return [symbol.name for symbol in library.exported_symbols] | ||
elif library.format == lief.Binary.FORMATS.PE: | ||
return [entry.name for entry in library.get_export().entries] | ||
elif library.format == lief.Binary.FORMATS.MACHO: | ||
return [symbol.name[1:] for symbol in library.exported_symbols] | ||
raise NotImplementedError(f"Unsupported format: {library.format}") | ||
|
||
|
||
def grep_expected_symbols() -> list[str]: | ||
"""Guess the list of expected exported symbols from the source code.""" | ||
grep_output = subprocess.check_output( | ||
["git", "grep", r"^\s*SECP256K1_API", "--", "include"], | ||
universal_newlines=True, | ||
encoding="utf-8" | ||
) | ||
lines = grep_output.split("\n") | ||
pattern = re.compile(r'\bsecp256k1_\w+') | ||
exported: list[str] = [pattern.findall(line)[-1] for line in lines if line.strip()] | ||
return exported | ||
|
||
|
||
def check_symbols(library, expected_exports) -> None: | ||
"""Check that the library exports only the expected symbols.""" | ||
actual_exports = list(get_exported_exports(library)) | ||
unexpected_exports = set(actual_exports) - set(expected_exports) | ||
if unexpected_exports != set(): | ||
raise UnexpectedExport(f"Unexpected exported symbols: {unexpected_exports}") | ||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
print(__doc__) | ||
return 1 | ||
library = lief.parse(sys.argv[1]) | ||
expected_exports = grep_expected_symbols() | ||
try: | ||
check_symbols(library, expected_exports) | ||
except UnexpectedExport as e: | ||
print(f"{sys.argv[0]}: In {sys.argv[1]}: {e}") | ||
return 1 | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case it's not obvious to other reviewers, this is no longer needed because every non-static function should be externally visible.
If we ever need to share private symbols between compilation units this won't work. But I agree that it makes sense to take advantage of that if we can.