Skip to content

Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.

fix(fetch): load the Linux engine in a glibc or musl userspace on Android

HenryNdubuaku278eea197d6a
diff --git a/needle/agent/fetch.py b/needle/agent/fetch.py
index 21d0d1e..17d255a 100644
--- a/needle/agent/fetch.py
+++ b/needle/agent/fetch.py
@@ -73,6 +73,19 @@ def lib_path_env(generation=2):
return LIB_PATH_ENVS.get(generation) or f"NEEDLE{generation}_LIB_PATH"
+def _loaded_libc():
+ try:
+ with open("/proc/self/maps", "rb") as maps:
+ blob = maps.read()
+ except OSError:
+ return None
+ if b"musl" in blob:
+ return "musl"
+ if b"/libc.so.6" in blob or b"/ld-linux" in blob:
+ return "glibc"
+ return None
+
+
def _is_musl():
"""True on a musl system (Alpine and friends).
@@ -81,15 +94,9 @@ def _is_musl():
"""
if sys.platform != "linux":
return False
- try:
- with open("/proc/self/maps", "rb") as maps:
- blob = maps.read()
- if b"musl" in blob:
- return True
- if b"/libc.so.6" in blob or b"/ld-linux" in blob:
- return False
- except OSError:
- pass
+ libc = _loaded_libc()
+ if libc:
+ return libc == "musl"
import glob
if glob.glob("/lib/ld-musl-*.so.1"):
return True
@@ -113,6 +120,8 @@ def _is_android():
"""
if hasattr(sys, "getandroidapilevel"):
return True
+ if _loaded_libc():
+ return False
return bool(os.environ.get("ANDROID_ROOT") and os.environ.get("ANDROID_DATA"))
diff --git a/tests/test_fetch.py b/tests/test_fetch.py
index fe6cb7c..bfe2889 100644
--- a/tests/test_fetch.py
+++ b/tests/test_fetch.py
@@ -317,6 +317,7 @@ def test_android_is_refused_instead_of_being_handed_a_glibc_wheel(monkeypatch):
def test_android_is_detected_from_the_api_level_or_the_runtime_env(monkeypatch):
from needle.agent import fetch
+ monkeypatch.setattr(fetch, "_loaded_libc", lambda: None)
monkeypatch.delenv("ANDROID_ROOT", raising=False)
monkeypatch.delenv("ANDROID_DATA", raising=False)
monkeypatch.delattr(sys, "getandroidapilevel", raising=False)
@@ -333,6 +334,25 @@ def test_android_is_detected_from_the_api_level_or_the_runtime_env(monkeypatch):
assert fetch._is_android() is True
+def test_android_markers_inside_a_glibc_or_musl_userspace_are_linux(monkeypatch, tmp_path):
+ from needle.agent import fetch
+
+ monkeypatch.delattr(sys, "getandroidapilevel", raising=False)
+ monkeypatch.setenv("ANDROID_ROOT", "/system")
+ monkeypatch.setenv("ANDROID_DATA", "/data")
+ maps = tmp_path / "maps"
+ real_open = open
+ monkeypatch.setattr("builtins.open",
+ lambda p, *a, **k: real_open(maps, *a, **k) if p == "/proc/self/maps"
+ else real_open(p, *a, **k))
+ rootfs = b"/data/data/com.termux/files/usr/var/lib/proot-distro/installed-rootfs"
+ for loaded, android in ((rootfs + b"/debian/usr/lib/aarch64-linux-gnu/libc.so.6", False),
+ (rootfs + b"/alpine/lib/ld-musl-aarch64.so.1", False),
+ (b"/apex/com.android.runtime/lib64/bionic/libc.so", True)):
+ maps.write_bytes(b"7f0000000000-7f0000001000 r-xp 00000000 fd:00 1 " + loaded + b"\n")
+ assert fetch._is_android() is android, loaded
+
+
def test_building_the_published_archive_needs_no_training_stack(tmp_path, monkeypatch):
import needle._telemetry
from needle.agent import fetch