Closes #1181.
Catches the actual symbol visibility issue.
Replaces #1135.
--env-file
drops ENV
?
0@@ -0,0 +1,12 @@
1+#ifndef SECP256K1_UTIL_DEFS_H
2+#define SECP256K1_UTIL_DEFS_H
3+
4+/* Global variable visibility */
5+/* See: https://github.com/bitcoin-core/secp256k1/issues/1181 */
6+#if !defined(_WIN32) && defined(__GNUC__) && (__GNUC__ >= 4)
7+# define SECP256K1_LOCAL_VAR extern __attribute__ ((visibility ("hidden")))
0@@ -0,0 +1,12 @@
1+#ifndef SECP256K1_UTIL_DEFS_H
2+#define SECP256K1_UTIL_DEFS_H
util_defs.h
is pretty ambiguous. How about util_local_visibility.h
or so?
266@@ -267,8 +267,6 @@ else()
267 try_append_c_flags(-Wundef)
268 endif()
269
270-set(CMAKE_C_VISIBILITY_PRESET hidden)
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.
Cool that this caught a real (new) symbol leak.
To clarify, the fix is in the first commit. The excerpt from the CI log with an error message from the previous iteration of this branch looks as follows:
0+ python3 ./tools/symbol-check.py .libs/libsecp256k1.so
1.libs/libsecp256k1.so: export of symbol secp256k1_musig_nonce_gen_internal not expected
2.libs/libsecp256k1.so: export of symbol secp256k1_musig_nonce_gen_internal not expected
3.libs/libsecp256k1.so: failed EXPORTED_SYMBOLS
4Error: Process completed with exit code 1.
@fanquake suggested that the test could potentially be simplified for c-i.
Thanks! The tools/symbol-check.py
script has been simplified.
781@@ -763,6 +782,12 @@ jobs:
782 run: |
783 cd build/bin/RelWithDebInfo && file *tests.exe bench*.exe libsecp256k1-*.dll || true
784
785+ - name: Symbol check
786+ if: ${{ matrix.configuration.cmake_options != '-A x64 -DBUILD_SHARED_LIBS=OFF' }}
symbol_check
in addition to cmake_options
? Or alternatively, a variable shared
that controls -DBUILD_SHARED_LIBS
.
0@@ -0,0 +1,98 @@
1+#!/usr/bin/env python3
2+'''
3+A script to check that a secp256k1 shared library
0A script to check that a libsecp256k1 shared library
:P
106@@ -107,6 +107,19 @@ file *tests* || true
107 file bench* || true
108 file .libs/* || true
109
110+if [ "$SYMBOL_CHECK" = "yes" ]
111+then
112+ case "$HOST" in
113+ *mingw*)
114+ ls -l .libs
115+ python3 ./tools/symbol-check.py .libs/libsecp256k1-2.dll
python3
here, I think it makes sense to add a python3 --version
at the top of the file, as we done for the other tools.
22+ ./tools/symbol-check.py build/lib/libsecp256k1.dylib
23+'''
24+import re
25+import sys
26+import subprocess
27+from typing import List
List[T]
has been deprecated since Python 3.9, which is the oldest supported Python version. So you can safely use list[T]
, and this import is not needed. See https://docs.python.org/3.9/library/typing.html?highlight=typing#typing.List (Kudos for adding types!)
30 # define WINDOW_G ECMULT_WINDOW_SIZE
31-extern const secp256k1_ge_storage secp256k1_pre_g[ECMULT_TABLE_SIZE(WINDOW_G)];
32-extern const secp256k1_ge_storage secp256k1_pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)];
33+SECP256K1_LOCAL_VAR const secp256k1_ge_storage secp256k1_pre_g[ECMULT_TABLE_SIZE(WINDOW_G)];
34+SECP256K1_LOCAL_VAR const secp256k1_ge_storage secp256k1_pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)];
35 #endif /* defined(EXHAUSTIVE_TEST_ORDER) */
#include
in a specific branch (instead of putting it at the top of file)?
28+
29+import lief
30+
31+
32+def grep_exported_symbols() -> List[str]:
33+ grep_output = subprocess.check_output(["git", "grep", "^SECP256K1_API", "--", "include"], universal_newlines=True, encoding="utf8")
0 grep_output = subprocess.check_output(["git", "grep", "^[\s*]SECP256K1_API", "--", "include"], universal_newlines=True, encoding="utf8")
That’s perhaps a bit more robust
util.h
. Or is there a reason not to keep it here?
../include/secp256k1.h
header into lower-level code via util.h
. However, at this point, util.h
is already being included indirectly through other headers.
66+ return ok
67+
68+
69+def check_MACHO_exported_functions(library, expected_exports) -> bool:
70+ ok: bool = True
71+ macho_lib: lief.MACHO.Binary = library.concrete
.concrete
for?
88+ elif exe_format == lief.Binary.FORMATS.PE:
89+ success = check_PE_exported_functions(library, grep_exported_symbols())
90+ elif exe_format == lief.Binary.FORMATS.MACHO:
91+ success = check_MACHO_exported_functions(library, grep_exported_symbols())
92+ else:
93+ print(f'{filename}: unknown executable format')
I don’t think this code can be reached, because if it is an unknown format, .parse()
will have errored (printing Unknown format
), and then lief.Binary.FORMATS = library.format
will fail:
0./tools/symbol-check.py not_a_file
1Unknown format
2Traceback (most recent call last):
3 File "./tools/symbol-check.py", line 81, in <module>
4 exe_format: lief.Binary.FORMATS = library.format
5 ^^^^^^^^^^^^^^
6AttributeError: 'NoneType' object has no attribute 'format'
I don’t think this code can be reached,
This will be reachable once LIEF adds support for a new binary format, so I think it was useful to have it.
51+ return ok
52+
53+
54+def check_PE_exported_functions(library, expected_exports) -> bool:
55+ ok: bool = True
56+ for function in library.exported_functions:
.exported_functions
is from the abstract Binary class, and [entry.name for entry in library.get_export()]
may give the better result for PE because it includes non-function exports? See https://lief.re/doc/latest/formats/pe/python.html#export-entry
62+ return ok
63+
64+
65+def check_MACHO_exported_functions(library, expected_exports) -> bool:
66+ ok: bool = True
67+ for function in library.exported_functions:
.exported_functions
is from the abstract Binary class, and .exported_symbols
may give a better result for Mach-O because it includes non-function exports?
Hm, just checking: You’re saying that this change didn’t make a difference, right? So it still finds only exported functions and not exported variables?
(And I have the same question for PE.)
Hm, just checking: You’re saying that this change didn’t make a difference, right?
Correct.
So it still finds only exported functions and not exported variables?
With the reverted second commit, both variants catch the same local variables:
0./tools/symbol-check.py: In .libs/libsecp256k1.dylib: Unexpected exported symbols: {'secp256k1_pre_g', 'secp256k1_pre_g_128', 'secp256k1_ecmult_gen_prec_table'}
(And I have the same question for PE.)
With the reverted second commit, both variants do not catch local variables.
For more details, please refer to:
Ok, that means that the LIEF API is a bit unexpected. Or we don’t understand it.
exported_functions
is the same as exported_symbols
. Both include variables, but I’d expect the former to include only functions.secp256k1_ellswift_xdh_hash_function_bip324
, so at least we test this in CI. And thus we can be sure that the variables are actually exported in PE, and it’s just LIEF that doesn’t show them.Does this match your understanding? Do you think it’s worth asking LIEF about the variables?
Relatedly, on platforms where LIEF gives us both variables and functions, would it make sense also check that all expected symbols are actually exported?
Here’s a refactored version, which is more readable IMHO (haven’t tested on Mac or Windows):
0#!/usr/bin/env python3
1"""Check that a libsecp256k1 shared library exports only expected symbols.
2
3Usage examples:
4 - When building with Autotools:
5 ./tools/symbol-check.py .libs/libsecp256k1.so
6 ./tools/symbol-check.py .libs/libsecp256k1-<V>.dll
7 ./tools/symbol-check.py .libs/libsecp256k1.dylib
8
9 - When building with CMake:
10 ./tools/symbol-check.py build/lib/libsecp256k1.so
11 ./tools/symbol-check.py build/bin/libsecp256k1-<V>.dll
12 ./tools/symbol-check.py build/lib/libsecp256k1.dylib"""
13
14import re
15import sys
16import subprocess
17
18import lief
19
20
21class UnexpectedExport(RuntimeError):
22 pass
23
24
25def get_exported_exports(library) -> list[str]:
26 """Adapter function to get exported symbols based on the library format."""
27 if library.format == lief.Binary.FORMATS.ELF:
28 return [symbol.name for symbol in library.exported_symbols]
29 elif library.format == lief.Binary.FORMATS.PE:
30 return [function.name for function in library.exported_functions]
31 elif library.format == lief.Binary.FORMATS.MACHO:
32 return [function.name[1:] for function in library.exported_functions]
33 raise NotImplementedError(f"Unsupported format: {library.format}")
34
35
36def grep_expected_symbols() -> list[str]:
37 """Guess the list of expected exported symbols from the source code."""
38 grep_output = subprocess.check_output(
39 ["git", "grep", "^SECP256K1_API", "--", "include"], # TODO WHITESPACE
40 universal_newlines=True,
41 encoding="utf-8"
42 )
43 lines = grep_output.split("\n")
44 pattern = re.compile(r'\bsecp256k1_\w+')
45 exported: list[str] = [pattern.findall(line)[-1] for line in lines if line.strip()]
46 return exported
47
48
49def check_symbols(library, expected_exports) -> None:
50 """Check that the library exports only the expected symbols."""
51 actual_exports = list(get_exported_exports(library))
52 unexpected_exports = set(actual_exports) - set(expected_exports)
53 if unexpected_exports != set():
54 raise UnexpectedExport(f"Unexpected exported symbols: {unexpected_exports}")
55
56def main():
57 if len(sys.argv) != 2:
58 print(__doc__)
59 return 1
60 library = lief.parse(sys.argv[1])
61 expected_exports = grep_expected_symbols()
62 try:
63 check_symbols(library, expected_exports)
64 except UnexpectedExport as e:
65 print(f"{sys.argv[0]}: In {sys.argv[1]}: {e}")
66 return 1
67 return 0
68
69
70if __name__ == "__main__":
71 sys.exit(main())
Here’s a refactored version, which is more readable IMHO (haven’t tested on Mac or Windows):
Tested on macOS and on Windows. Incorporated. Thank you!
35+
36+
37+def grep_expected_symbols() -> list[str]:
38+ """Guess the list of expected exported symbols from the source code."""
39+ grep_output = subprocess.check_output(
40+ ["git", "grep", "^SECP256K1_API", "--", "include"], # TODO WHITESPACE
0 ["git", "grep", "^\s*SECP256K1_API", "--", "include"],
Oops, I think I had this in mind here, to make the matching a bit more flexible.
35+
36+
37+def grep_expected_symbols() -> list[str]:
38+ """Guess the list of expected exported symbols from the source code."""
39+ grep_output = subprocess.check_output(
40+ ["git", "grep", "^\s*SECP256K1_API", "--", "include"],
This gives me error symbol-check.backup.py:40: SyntaxWarning: invalid escape sequence '\s'
. I think it should be
0r"^\s*SECP256K1_API"
or
0"^\\s*SECP256K1_API"
This change makes the `-fvisibility=hidden` compiler option unnecessary.
Co-authored-by: Tim Ruffing <crypto@timruffing.de>
Code-review reACK 7ac46cf93c322193e428effeac3f024f4ab6a05b.
Still no opinion on the .py/ci.
Being able to drop -fvisibility=hidden
is a nice improvement (and also a testament to the code structure!)