| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
from collections import defaultdict |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import pretend |
| 8 |
import pytest |
| 9 |
|
| 10 |
from auditwheel import wheel_abi |
| 11 |
from auditwheel.architecture import Architecture |
| 12 |
from auditwheel.lddtree import DynamicExecutable, DynamicLibrary, Platform |
| 13 |
from auditwheel.libc import Libc |
| 14 |
from auditwheel.policy import ExternalReference, WheelPolicies |
| 15 |
|
| 16 |
|
| 17 |
class TestGetWheelElfdata: |
| 18 |
@pytest.mark.parametrize( |
| 19 |
("filenames", "message"), |
| 20 |
[ |
| 21 |
( |
| 22 |
# A single invalid file |
| 23 |
[Path("purelib") / "foo"], |
| 24 |
"Invalid binary wheel, found the following shared library/libraries in" |
| 25 |
" purelib folder:\n\tfoo\nThe wheel has to be platlib compliant in " |
| 26 |
"order to be repaired by auditwheel.", |
| 27 |
), |
| 28 |
( |
| 29 |
# Multiple invalid files |
| 30 |
[Path("purelib") / "foo", Path("purelib") / "bar"], |
| 31 |
"Invalid binary wheel, found the following shared library/libraries in" |
| 32 |
" purelib folder:\n\tfoo\n\tbar\nThe wheel has to be platlib compliant" |
| 33 |
" in order to be repaired by auditwheel.", |
| 34 |
), |
| 35 |
], |
| 36 |
) |
| 37 |
def test_finds_shared_library_in_purelib( |
| 38 |
self, |
| 39 |
filenames: list[Path], |
| 40 |
message: str, |
| 41 |
monkeypatch: pytest.MonkeyPatch, |
| 42 |
) -> None: |
| 43 |
entered_context = pretend.stub(iter_files=lambda: filenames) |
| 44 |
context = pretend.stub( |
| 45 |
__enter__=lambda: entered_context, |
| 46 |
__exit__=lambda *_: None, |
| 47 |
) |
| 48 |
|
| 49 |
monkeypatch.setattr(wheel_abi, "InGenericPkgCtx", pretend.stub(__call__=lambda _: context)) |
| 50 |
monkeypatch.setattr( |
| 51 |
wheel_abi, |
| 52 |
"elf_is_python_extension", |
| 53 |
lambda fn, elf: (fn, elf), |
| 54 |
) |
| 55 |
monkeypatch.setattr( |
| 56 |
wheel_abi, |
| 57 |
"elf_file_filter", |
| 58 |
lambda fns: [(fn, pretend.stub()) for fn in fns], |
| 59 |
) |
| 60 |
|
| 61 |
with pytest.raises(RuntimeError) as exec_info: |
| 62 |
wheel_abi.get_wheel_elfdata( |
| 63 |
Libc.GLIBC, |
| 64 |
Architecture.x86_64, |
| 65 |
Path("/fakepath"), |
| 66 |
exclude=frozenset(), |
| 67 |
args_ldpaths="", |
| 68 |
) |
| 69 |
|
| 70 |
assert exec_info.value.args == (message,) |
| 71 |
|
| 72 |
|
| 73 |
def test_get_symbol_policies() -> None: |
| 74 |
policies = WheelPolicies(libc=Libc.GLIBC, arch=Architecture.x86_64) |
| 75 |
versioned_symbols = defaultdict(set, {"libc.so.6": {"GLIBC_2.2.5"}}) |
| 76 |
external_versioned_symbols = { |
| 77 |
"libmvec.so.1": { |
| 78 |
"libc.so.6": {"GLIBC_2.2.5"}, |
| 79 |
"libm.so.6": {"GLIBC_2.15", "GLIBC_2.2.5"}, |
| 80 |
}, |
| 81 |
} |
| 82 |
external_refs = {} |
| 83 |
for policy in policies: |
| 84 |
if policy.name in { |
| 85 |
"manylinux_2_5_x86_64", |
| 86 |
"manylinux_2_12_x86_64", |
| 87 |
"manylinux_2_17_x86_64", |
| 88 |
}: |
| 89 |
libs = {"libmvec.so.1": Path("/lib64/libmvec-2.28.so")} |
| 90 |
else: |
| 91 |
libs = {} |
| 92 |
blacklist = {} |
| 93 |
external_refs[policy.name] = ExternalReference(libs, blacklist, policy) |
| 94 |
symbol_policies = wheel_abi.get_symbol_policies( |
| 95 |
policies, |
| 96 |
versioned_symbols, |
| 97 |
external_versioned_symbols, |
| 98 |
external_refs, |
| 99 |
) |
| 100 |
max_policy = max(symbol_policy[0] for symbol_policy in symbol_policies) |
| 101 |
assert max_policy.name == "manylinux_2_17_x86_64" |
| 102 |
|
| 103 |
|
| 104 |
@pytest.mark.parametrize("kind", ["resolved", "unresolved", "warning"]) |
| 105 |
def test_nonpy_elf_resolution(kind: str, tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None: |
| 106 |
caplog.set_level(logging.WARNING) |
| 107 |
liba_path = tmp_path / "liba.so" |
| 108 |
libb_path = tmp_path / "libb.so" |
| 109 |
liba_path.touch() |
| 110 |
libb_path.touch() |
| 111 |
platform = Platform("", 64, True, "EM_X86_64", Architecture.x86_64, None, None) |
| 112 |
libb = DynamicExecutable( |
| 113 |
interpreter=None, |
| 114 |
libc=None, |
| 115 |
path="libb.so", |
| 116 |
realpath=libb_path, |
| 117 |
platform=platform, |
| 118 |
needed=(), |
| 119 |
rpath=(), |
| 120 |
runpath=(), |
| 121 |
libraries={}, |
| 122 |
) |
| 123 |
liba = DynamicExecutable( |
| 124 |
interpreter=None, |
| 125 |
libc=None, |
| 126 |
path="liba.so", |
| 127 |
realpath=liba_path, |
| 128 |
platform=platform, |
| 129 |
needed=("libb.so",), |
| 130 |
rpath=(), |
| 131 |
runpath=(), |
| 132 |
libraries={ |
| 133 |
"libb.so": DynamicLibrary("libb.so", None, None), |
| 134 |
}, |
| 135 |
) |
| 136 |
libraries = { |
| 137 |
"liba.so": DynamicLibrary("liba.so", liba.path, liba.realpath, liba.platform, liba.needed), |
| 138 |
} |
| 139 |
if kind == "resolved": |
| 140 |
libraries["libb.so"] = DynamicLibrary( |
| 141 |
"libb.so", |
| 142 |
libb.path, |
| 143 |
libb.realpath, |
| 144 |
libb.platform, |
| 145 |
libb.needed, |
| 146 |
) |
| 147 |
elif kind == "unresolved": |
| 148 |
libraries["libb.so"] = DynamicLibrary("libb.so", None, None) |
| 149 |
extension = DynamicExecutable( |
| 150 |
interpreter=None, |
| 151 |
libc=None, |
| 152 |
path="extension.so", |
| 153 |
realpath=Path("extension.so"), |
| 154 |
platform=platform, |
| 155 |
needed=("liba.so",), |
| 156 |
rpath=(), |
| 157 |
runpath=(), |
| 158 |
libraries=libraries, |
| 159 |
) |
| 160 |
pyelf_trees = {extension.realpath: extension} |
| 161 |
nonpy_elftrees = {liba.realpath: liba, libb.realpath: libb} |
| 162 |
wheel_abi._fixup_elf_trees(pyelf_trees, nonpy_elftrees) |
| 163 |
assert pyelf_trees == {extension.realpath: extension} |
| 164 |
if kind == "resolved": |
| 165 |
assert nonpy_elftrees != {liba.realpath: liba, libb.realpath: libb} |
| 166 |
assert nonpy_elftrees[liba.realpath].libraries["libb.so"].realpath == libb.realpath |
| 167 |
else: |
| 168 |
assert nonpy_elftrees == {liba.realpath: liba, libb.realpath: libb} |
| 169 |
assert nonpy_elftrees[liba.realpath].libraries["libb.so"].realpath is None |
| 170 |
warnings = [ |
| 171 |
record |
| 172 |
for record in caplog.records |
| 173 |
if "ELF tree" in record.message and record.levelno == logging.WARNING |
| 174 |
] |
| 175 |
if kind == "warning": |
| 176 |
assert len(warnings) == 1 |
| 177 |
else: |
| 178 |
assert len(warnings) == 0 |
| 179 |
|
| 180 |
|
| 181 |
@pytest.mark.parametrize("kind", ["resolved", "unresolved", "warning"]) |
| 182 |
def test_nonpy_elf_resolution_transitive_needed( |
| 183 |
kind: str, |
| 184 |
tmp_path: Path, |
| 185 |
caplog: pytest.LogCaptureFixture, |
| 186 |
) -> None: |
| 187 |
caplog.set_level(logging.WARNING) |
| 188 |
liba_path = tmp_path / "liba.so" |
| 189 |
libb_path = tmp_path / "libb.so" |
| 190 |
libc_path = tmp_path / "libc.so" |
| 191 |
liba_path.touch() |
| 192 |
libb_path.touch() |
| 193 |
libc_path.touch() |
| 194 |
platform = Platform("", 64, True, "EM_X86_64", Architecture.x86_64, None, None) |
| 195 |
libc = DynamicExecutable( |
| 196 |
interpreter=None, |
| 197 |
libc=None, |
| 198 |
path="libc.so", |
| 199 |
realpath=libc_path, |
| 200 |
platform=platform, |
| 201 |
needed=(), |
| 202 |
rpath=(), |
| 203 |
runpath=(), |
| 204 |
libraries={}, |
| 205 |
) |
| 206 |
libb = DynamicExecutable( |
| 207 |
interpreter=None, |
| 208 |
libc=None, |
| 209 |
path="libb.so", |
| 210 |
realpath=libb_path, |
| 211 |
platform=platform, |
| 212 |
needed=("libc.so",), |
| 213 |
rpath=(), |
| 214 |
runpath=(), |
| 215 |
libraries={ |
| 216 |
"libc.so": DynamicLibrary("libc.so", None, None), |
| 217 |
}, |
| 218 |
) |
| 219 |
liba = DynamicExecutable( |
| 220 |
interpreter=None, |
| 221 |
libc=None, |
| 222 |
path="liba.so", |
| 223 |
realpath=liba_path, |
| 224 |
platform=platform, |
| 225 |
needed=("libb.so",), |
| 226 |
rpath=(), |
| 227 |
runpath=(), |
| 228 |
libraries={ |
| 229 |
"libb.so": DynamicLibrary("libb.so", None, None), |
| 230 |
}, |
| 231 |
) |
| 232 |
libraries = { |
| 233 |
"liba.so": DynamicLibrary("liba.so", liba.path, liba.realpath, liba.platform, liba.needed), |
| 234 |
} |
| 235 |
if kind in {"resolved", "warning"}: |
| 236 |
libraries["libb.so"] = DynamicLibrary( |
| 237 |
"libb.so", |
| 238 |
libb.path, |
| 239 |
libb.realpath, |
| 240 |
libb.platform, |
| 241 |
libb.needed, |
| 242 |
) |
| 243 |
if kind == "resolved": |
| 244 |
libraries["libc.so"] = DynamicLibrary( |
| 245 |
"libc.so", |
| 246 |
libc.path, |
| 247 |
libc.realpath, |
| 248 |
libc.platform, |
| 249 |
libc.needed, |
| 250 |
) |
| 251 |
elif kind == "unresolved": |
| 252 |
libraries["libb.so"] = DynamicLibrary("libb.so", None, None) |
| 253 |
|
| 254 |
extension = DynamicExecutable( |
| 255 |
interpreter=None, |
| 256 |
libc=None, |
| 257 |
path="extension.so", |
| 258 |
realpath=Path("extension.so"), |
| 259 |
platform=platform, |
| 260 |
needed=("liba.so",), |
| 261 |
rpath=(), |
| 262 |
runpath=(), |
| 263 |
libraries=libraries, |
| 264 |
) |
| 265 |
pyelf_trees = {extension.realpath: extension} |
| 266 |
nonpy_elftrees = {liba.realpath: liba, libb.realpath: libb, libc.realpath: libc} |
| 267 |
wheel_abi._fixup_elf_trees(pyelf_trees, nonpy_elftrees) |
| 268 |
assert pyelf_trees == {extension.realpath: extension} |
| 269 |
if kind in {"resolved", "warning"}: |
| 270 |
assert nonpy_elftrees != {liba.realpath: liba, libb.realpath: libb, libc.realpath: libc} |
| 271 |
assert nonpy_elftrees[liba.realpath].libraries["libb.so"].realpath == libb.realpath |
| 272 |
if kind == "resolved": |
| 273 |
assert nonpy_elftrees[liba.realpath].libraries["libc.so"].realpath == libc.realpath |
| 274 |
assert nonpy_elftrees[libb.realpath].libraries["libc.so"].realpath == libc.realpath |
| 275 |
else: |
| 276 |
assert "libc.so" not in nonpy_elftrees[liba.realpath].libraries |
| 277 |
assert nonpy_elftrees[libb.realpath].libraries["libc.so"].realpath is None |
| 278 |
else: |
| 279 |
assert nonpy_elftrees == {liba.realpath: liba, libb.realpath: libb, libc.realpath: libc} |
| 280 |
assert nonpy_elftrees[liba.realpath].libraries["libb.so"].realpath is None |
| 281 |
assert "libc.so" not in nonpy_elftrees[liba.realpath].libraries |
| 282 |
assert nonpy_elftrees[libb.realpath].libraries["libc.so"].realpath is None |
| 283 |
|
| 284 |
warnings = [ |
| 285 |
record |
| 286 |
for record in caplog.records |
| 287 |
if "ELF tree" in record.message and record.levelno == logging.WARNING |
| 288 |
] |
| 289 |
if kind == "warning": |
| 290 |
assert len(warnings) == 2 |
| 291 |
else: |
| 292 |
assert len(warnings) == 0 |