Skip to content

Commit fd36458

Browse files
atalmanmalfet
andauthored
[Cherry-Pick] Work Around exposing statically linked libstdc++ CXX11 ABI strong symbols (pytorch#163980) (pytorch#164508)
* Work Around exposing statically linked libstdc++ CXX11 ABI strong symbols (pytorch#163980) Work Around for: pytorch#133437 Test plan: 1. Build whl in CI 2. Download 3. Run ``nm -D libtorch_cpu.so | grep "recursive_directory_iterator"`` Test with check_binary_symbols.py: Success: ``` num_cxx11_symbols: 2326 num_pre_cxx11_symbols: 0 lib: /home/ec2-user/github/variant-repack/.venv/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so num_statically_linked_symbols (T): 0 ``` Fail when using "W" instead of "T" as type calling ``cxx11_statically_linked_symbols = grep_symbols( lib, STATICALLY_LINKED_CXX11_ABI, symbol_type="W" )`` : ``` num_cxx11_symbols: 2326 num_pre_cxx11_symbols: 0 lib: /home/ec2-user/github/variant-repack/.venv/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so num_statically_linked_symbols (T): 20 Traceback (most recent call last): File "/home/ec2-user/github/variant-repack/test/pytorch/.ci/pytorch/smoke_test/check_binary_symbolsc.py", line 130, in <module> main() File "/home/ec2-user/github/variant-repack/test/pytorch/.ci/pytorch/smoke_test/check_binary_symbolsc.py", line 126, in main check_lib_statically_linked_libstdc_cxx_abi_symbols(libtorch_cpu_path) File "/home/ec2-user/github/variant-repack/test/pytorch/.ci/pytorch/smoke_test/check_binary_symbolsc.py", line 95, in check_lib_statically_linked_libstdc_cxx_abi_symbols raise RuntimeError( RuntimeError: Found statically linked libstdc++ symbols (recursive_directory_iterator), but there shouldn't be any, see: ['std::filesystem::__cxx11::recursive_directory_iterator::recursion_pending() const', 'std::filesystem::__cxx11::recursive_directory_iterator::depth() const', 'std::filesystem::__cxx11::recursive_directory_iterator::options() const', 'std::filesystem::__cxx11::recursive_directory_iterator::operator*() const', 'std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>::operator bool() const', 'std::filesystem::__cxx11::recursive_directory_iterator::disable_recursion_pending()', 'std::filesystem::__cxx11::recursive_directory_iterator::pop(std::error_code&)', 'std::filesystem::__cxx11::recursive_directory_iterator::pop()', 'std::filesystem::__cxx11::recursive_directory_iterator::increment(std::error_code&)', 'std::filesystem::__cxx11::recursive_directory_iterator::recursive_directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)', 'std::filesystem::__cxx11::recursive_directory_iterator::recursive_directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)', 'std::filesystem::__cxx11::recursive_directory_iterator::~recursive_directory_iterator()', 'std::filesystem::__cxx11::recursive_directory_iterator::~recursive_directory_iterator()', 'std::filesystem::__cxx11::recursive_directory_iterator::operator=(std::filesystem::__cxx11::recursive_directory_iterator&&)', 'std::filesystem::__cxx11::recursive_directory_iterator::operator=(std::filesystem::__cxx11::recursive_directory_iterator const&)', 'std::filesystem::__cxx11::recursive_directory_iterator::operator++()', 'std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>::__shared_ptr(std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>&&)', 'std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>::__shared_ptr()', 'std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>::__shared_ptr(std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>&&)', 'std::__shared_ptr<std::filesystem::__cxx11::recursive_directory_iterator::_Dir_stack, (__gnu_cxx::_Lock_policy)2>::__shared_ptr()'] ``` Pull Request resolved: pytorch#163980 Approved by: https://github.com/isuruf, https://github.com/malfet Co-authored-by: Nikita Shulga <[email protected]> * fix --------- Co-authored-by: Nikita Shulga <[email protected]>
1 parent 2f6387e commit fd36458

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

.ci/docker/common/patch_libstdc.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
set -xe
3+
# Script used in Linux x86 and aarch64 CD pipeline
4+
5+
# Workaround for exposing statically linked libstdc++ CXX11 ABI symbols.
6+
# see: https://github.com/pytorch/pytorch/issues/133437
7+
LIBNONSHARED=$(gcc -print-file-name=libstdc++_nonshared.a)
8+
nm -g $LIBNONSHARED | grep " T " | grep recursive_directory_iterator | cut -c 20- > weaken-symbols.txt
9+
objcopy --weaken-symbols weaken-symbols.txt $LIBNONSHARED $LIBNONSHARED

.ci/docker/manywheel/Dockerfile_2_28

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ ENV LD_LIBRARY_PATH=/opt/rh/gcc-toolset-${DEVTOOLSET_VERSION}/root/usr/lib64:/op
130130
RUN for cpython_version in "cp312-cp312" "cp313-cp313" "cp313-cp313t"; do \
131131
/opt/python/${cpython_version}/bin/python -m pip install setuptools wheel; \
132132
done;
133-
133+
ADD ./common/patch_libstdc.sh patch_libstdc.sh
134+
RUN bash ./patch_libstdc.sh && rm patch_libstdc.sh
134135

135136
# cmake-3.18.4 from pip; force in case cmake3 already exists
136137
RUN yum install -y python3-pip && \

.ci/docker/manywheel/Dockerfile_2_28_aarch64

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ RUN rm -rf /opt/python/cp33-cp33m /opt/_internal/cpython-3.3.6
7171
RUN rm -rf /opt/python/cp34-cp34m /opt/_internal/cpython-3.4.6
7272
COPY --from=openblas /opt/OpenBLAS/ /opt/OpenBLAS/
7373
ENV LD_LIBRARY_PATH=/opt/OpenBLAS/lib:$LD_LIBRARY_PATH
74+
ADD ./common/patch_libstdc.sh patch_libstdc.sh
75+
RUN bash ./patch_libstdc.sh && rm patch_libstdc.sh

.ci/docker/manywheel/Dockerfile_cuda_aarch64

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,5 @@ COPY --from=nvpl /opt/nvpl/lib/ /usr/local/lib/
9595
COPY --from=nvpl /opt/nvpl/include/ /usr/local/include/
9696
RUN ln -sf /usr/local/cuda-${BASE_CUDA_VERSION} /usr/local/cuda
9797
ENV PATH=/usr/local/cuda/bin:$PATH
98+
ADD ./common/patch_libstdc.sh patch_libstdc.sh
99+
RUN bash ./patch_libstdc.sh && rm patch_libstdc.sh

.ci/pytorch/check_binary.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fi
6767
# wheels with cxx11-abi
6868

6969
echo "Checking that the gcc ABI is what we expect"
70-
if [[ "$(uname)" != 'Darwin' ]]; then
70+
if [[ "$(uname)" != 'Darwin' && "$(uname -m)" != "s390x" ]]; then
7171
# We also check that there are cxx11 symbols in libtorch
7272
#
7373
echo "Checking that symbols in libtorch.so have the right gcc abi"

.ci/pytorch/smoke_test/check_binary_symbols.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"torch::",
3333
)
3434

35+
# Patterns for detecting statically linked libstdc++ symbols
36+
STATICALLY_LINKED_CXX11_ABI = [re.compile(r".*recursive_directory_iterator.*")]
37+
3538

3639
def _apply_libtorch_symbols(symbols):
3740
return [
@@ -53,12 +56,17 @@ def get_symbols(lib: str) -> list[tuple[str, str, str]]:
5356
return [x.split(" ", 2) for x in lines.decode("latin1").split("\n")[:-1]]
5457

5558

56-
def grep_symbols(lib: str, patterns: list[Any]) -> list[str]:
59+
def grep_symbols(
60+
lib: str, patterns: list[Any], symbol_type: str | None = None
61+
) -> list[str]:
5762
def _grep_symbols(
5863
symbols: list[tuple[str, str, str]], patterns: list[Any]
5964
) -> list[str]:
6065
rc = []
6166
for _s_addr, _s_type, s_name in symbols:
67+
# Filter by symbol type if specified
68+
if symbol_type and _s_type != symbol_type:
69+
continue
6270
for pattern in patterns:
6371
if pattern.match(s_name):
6472
rc.append(s_name)
@@ -80,6 +88,18 @@ def _get_symbols_chunk(i):
8088
return functools.reduce(list.__add__, (x.result() for x in tasks), [])
8189

8290

91+
def check_lib_statically_linked_libstdc_cxx_abi_symbols(lib: str) -> None:
92+
cxx11_statically_linked_symbols = grep_symbols(
93+
lib, STATICALLY_LINKED_CXX11_ABI, symbol_type="T"
94+
)
95+
num_statically_linked_symbols = len(cxx11_statically_linked_symbols)
96+
print(f"num_statically_linked_symbols (T): {num_statically_linked_symbols}")
97+
if num_statically_linked_symbols > 0:
98+
raise RuntimeError(
99+
f"Found statically linked libstdc++ symbols (recursive_directory_iterator): {cxx11_statically_linked_symbols[:100]}"
100+
)
101+
102+
83103
def check_lib_symbols_for_abi_correctness(lib: str) -> None:
84104
print(f"lib: {lib}")
85105
cxx11_symbols = grep_symbols(lib, LIBTORCH_CXX11_PATTERNS)
@@ -107,6 +127,7 @@ def main() -> None:
107127

108128
libtorch_cpu_path = str(install_root / "lib" / "libtorch_cpu.so")
109129
check_lib_symbols_for_abi_correctness(libtorch_cpu_path)
130+
check_lib_statically_linked_libstdc_cxx_abi_symbols(libtorch_cpu_path)
110131

111132

112133
if __name__ == "__main__":

0 commit comments

Comments
 (0)