-
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
5
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.
+164
−9
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e257138
Do not export `secp256k1_musig_nonce_gen_internal`
hebasto e92ec15
Introduce `SECP256K1_LOCAL_VAR` macro
hebasto 4b721ec
test: Add `tools/symbol-check.py`
hebasto 8a5c84a
ci: Run `tools/symbol-check.py`
hebasto 44a9392
build: Drop no longer needed `-fvisibility=hidden` compiler option
hebasto 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
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,98 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
A script to check that a secp256k1 shared library | ||
exports only expected symbols. | ||
|
||
Example usage: | ||
|
||
- when building with Autotools: | ||
|
||
./tools/symbol-check.py .libs/libsecp256k1.so | ||
or | ||
./tools/symbol-check.py .libs/libsecp256k1-<V>.dll | ||
or | ||
./tools/symbol-check.py .libs/libsecp256k1.dylib | ||
|
||
- when building with CMake: | ||
|
||
./tools/symbol-check.py build/lib/libsecp256k1.so | ||
or | ||
./tools/symbol-check.py build/bin/libsecp256k1-<V>.dll | ||
or | ||
./tools/symbol-check.py build/lib/libsecp256k1.dylib | ||
''' | ||
import re | ||
import sys | ||
import subprocess | ||
from typing import List | ||
|
||
import lief | ||
|
||
|
||
def grep_exported_symbols() -> List[str]: | ||
grep_output = subprocess.check_output(["git", "grep", "^SECP256K1_API", "--", "include"], universal_newlines=True, encoding="utf8") | ||
lines = grep_output.split("\n") | ||
exports: List[str] = [] | ||
pattern = re.compile(r'\bsecp256k1_\w+') | ||
for line in lines: | ||
if line.strip(): | ||
function_name = pattern.findall(line)[-1] | ||
exports.append(function_name) | ||
return exports | ||
|
||
|
||
def check_ELF_exported_symbols(library, expected_exports) -> bool: | ||
ok: bool = True | ||
elf_lib: lief.ELF.Binary = library.concrete | ||
|
||
for symbol in elf_lib.exported_symbols: | ||
name: str = symbol.name | ||
if name in expected_exports: | ||
continue | ||
print(f'{filename}: export of symbol {name} not expected') | ||
ok = False | ||
return ok | ||
|
||
|
||
def check_PE_exported_functions(library, expected_exports) -> bool: | ||
ok: bool = True | ||
pe_lib: lief.PE.Binary = library.concrete | ||
|
||
for function in pe_lib.exported_functions: | ||
name: str = function.name | ||
if name in expected_exports: | ||
continue | ||
print(f'{filename}: export of function {name} not expected') | ||
ok = False | ||
return ok | ||
|
||
|
||
def check_MACHO_exported_functions(library, expected_exports) -> bool: | ||
ok: bool = True | ||
macho_lib: lief.MACHO.Binary = library.concrete | ||
|
||
for function in macho_lib.exported_functions: | ||
name: str = function.name[1:] | ||
if name in expected_exports: | ||
continue | ||
print(f'{filename}: export of function {name} not expected') | ||
ok = False | ||
return ok | ||
|
||
|
||
if __name__ == '__main__': | ||
filename: str = sys.argv[1] | ||
library: lief.Binary = lief.parse(filename) | ||
exe_format: lief.Binary.FORMATS = library.format | ||
if exe_format == lief.Binary.FORMATS.ELF: | ||
success = check_ELF_exported_symbols(library, grep_exported_symbols()) | ||
elif exe_format == lief.Binary.FORMATS.PE: | ||
success = check_PE_exported_functions(library, grep_exported_symbols()) | ||
elif exe_format == lief.Binary.FORMATS.MACHO: | ||
success = check_MACHO_exported_functions(library, grep_exported_symbols()) | ||
else: | ||
print(f'{filename}: unknown executable format') | ||
sys.exit(1) | ||
|
||
if not success: | ||
sys.exit(1) |
Oops, something went wrong.
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.