Skip to content

Commit fe2569c

Browse files
committed
Retry the musl build when the glibc build does not load
1 parent 99a4da7 commit fe2569c

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎aikido_zen/helpers/zen_internals.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ def _load():
5959
try:
6060
return ctypes.CDLL(binary_path), None
6161
except OSError as e:
62-
return None, f"Failed to load the zen-internals library for {_TARGET}: {e}"
62+
error = e
63+
# A musl Python built without HOST_GNU_TYPE picks the glibc build, which musl
64+
# cannot load.
65+
musl_path = binary_path.replace("-gnu.so", "-musl.so")
66+
if musl_path != binary_path and os.path.exists(musl_path):
67+
try:
68+
return ctypes.CDLL(musl_path), None
69+
except OSError:
70+
pass
71+
return None, f"Failed to load the zen-internals library for {_TARGET}: {error}"
6372

6473

6574
def _warn_unavailable(consequence, function_name):

‎aikido_zen/helpers/zen_internals_test.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ctypes
22
import os
3+
import shutil
34

45
import pytest
56

@@ -60,6 +61,23 @@ def test_load_returns_none_when_library_fails_to_load(monkeypatch):
6061
assert "Failed to load the zen-internals library" in error
6162

6263

64+
def test_load_falls_back_to_the_musl_build_when_the_glibc_build_fails(
65+
monkeypatch, tmp_path
66+
):
67+
gnu_path = tmp_path / "libzen_internals_x86_64-unknown-linux-gnu.so"
68+
gnu_path.write_text("not a library")
69+
shutil.copy(
70+
zen_internals._binary_path(), tmp_path / gnu_path.name.replace("-gnu", "-musl")
71+
)
72+
monkeypatch.setattr(
73+
"aikido_zen.helpers.zen_internals._binary_path", lambda: str(gnu_path)
74+
)
75+
76+
internals_lib, error = zen_internals._load()
77+
assert internals_lib is not None
78+
assert error is None
79+
80+
6381
def test_bind_returns_none_when_library_is_not_loaded():
6482
assert zen_internals._bind(None, "detect_sql_injection", [], ctypes.c_int) is None
6583

0 commit comments

Comments
 (0)