Skip to content

安装后尝试运行显示Segmentation fault #1

Closed

Description

@tuokun

OpenCode Termux 打包问题报告

环境信息

设备: 荣耀20

已安装组件:

  • glibc: v2.42
  • glibc-runner: v2.0
  • opencode package: 1.2.23

项目路径:

  • Termux: ~/opencode/opencode-termux/

问题描述

尝试为 Termux 打包 OpenCode 1.2.23,使用 bun-termux-loader 包装官方的 opencode-linux-arm64 二进制。包装过程成功完成,但运行时出现 Segmentation fault。

构建过程

cd ~/opencode/opencode-termux
./tools/produce-local.sh 1.2.23

输出:

[produce-local] version=1.2.23
[produce-local] using npm package source for version 1.2.23
[produce-local] upstream fingerprint
/data/data/.../opencode: ELF 64-bit LSB executable, ARM aarch64, ... for GNU/Linux 3.7.0
[produce-local] wrapping upstream binary for Termux/Android
[+] Processing: opencode -> opencode-termux
[+] Found 3 native lib reference(s): libopentui-j9b4zwd2.so, librust_pty_arm64-kkpzdzzv.so, watcher-b681ffr4.node
[+] Success! Output size: 156.2 MB
[+]   Embedded 3 native lib(s) + shim
[produce-local] wrapped runtime verification
/data/data/.../opencode-termux: ELF 64-bit LSB pie executable, ARM aarch64, ... for Android 24, built by NDK r29
./tools/produce-local.sh: line 122:  5942 Segmentation fault      "$RUNTIME_OUT" --version

诊断结果

1. 包装结构验证 ✅

使用 xxd 检查输出文件,结构正确:

0x0000 - 0x2f40: Wrapper ELF
0x2f40 - 0x2f48: BUNWRAP1 标记
0x2f48+:      Bun ELF
文件末尾:      "---- Bun! ----" 标记

结论: bun-termux-loader 的包装过程本身没有问题,BUNWRAP1 标记正确添加。

2. strace 崩溃分析

strace -o ~/strace.log ~/opencode/opencode-termux/artifacts/opencode/runtime/opencode-termux --version

关键日志:

statx(AT_FDCWD, "/data/data/com.termux/files/home/opencode/opencode-termux/.work", AT_STATX_SYNC_AS_STAT, STATX_ALL, 0x747a5da720) = -1 ENETDOWN (Network is down)
--- SIGSYS {si_signo=SIGSYS, si_code=SYS_SECCOMP, si_call_addr=0x30511e4, si_syscall=__NR_statx, si_arch=AUDIT_ARCH_AARCH64} ---
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x2b8} ---
+++ killed by SIGSEGV +++

问题根因: statx() 系统调用被 Termux/Android 的 seccomp 沙箱拒绝。

3. statx() 系统调用

  • Linux 4.11+ 引入的新系统调用
  • OpenCode/Bun 针对现代 Linux 编译,使用了这个新系统调用
  • Termux/Android 可能不完全支持或受 seccomp 限制

当前 opencode 测试情况

~/opencode/opencode-termux $ opencode web
error loading current directory
error: An internal error occurred (CouldntReadCurrentDirectory)

~/opencode/opencode-termux/.work/opencode-1.2.23 $ opencode --version
1.3.10
~/opencode/opencode-termux/.work/opencode-1.2.23 $ opencode --help
Bun is a fast JavaScript runtime, package manager, bundler, and test runner. (1.3.10+30e609e08)

Usage: bun <command> [...flags] [...args]

Commands:
  run       ./my-script.ts       Execute a file with Bun
            lint                 Run a package.json script
  test                           Run unit tests with Bun
  x         nuxi                 Execute a package binary (CLI), installing if needed (bunx)
  repl                           Start a REPL session with Bun
  exec                           Run a shell script directly with Bun

  install                        Install dependencies for a package.json (bun i)
  add       tailwindcss          Add a dependency to package.json (bun a)
  remove    jquery               Remove a dependency from package.json (bun rm)
  update    elysia               Update outdated dependencies
  audit                          Check installed packages for vulnerabilities
 
.......... 

Learn more about Bun:            https://bun.com/docs
Join our Discord community:      https://bun.com/discord
~/opencode/opencode-termux/.work/opencode-1.2.23 $ 

已尝试的解决方向

  1. ✅ 修复 produce-local.sh 的工作目录问题
  2. ✅ 确认 wrapper 二进制编译成功
  3. ✅ 验证包装结构正确
  4. ❌ proot (系统调用转换) - 性能开销大,放弃
  5. ⏸️ 重新编译 - 复杂度高,未尝试
  6. ⏸️ LD_PRELOAD 拦截 - 待研究

文件状态

文件 大小 状态
.work/opencode-1.2.23/package/bin/opencode 158.2 MB 原始 glibc 二进制
.work/opencode-1.2.23/package/bin/opencode-termux 163.8 MB 包装后,运行崩溃
artifacts/opencode/runtime/opencode-termux 163.8 MB 复制自上述文件

附加信息

bun-termux-loader 信息:

  • 路径: ~/bun-termux-loader/
  • wrapper: 12064 字节,Android 24 NDK r29 ELF
  • build.py: 使用 --wrapper ./wrapper 参数

Activity

  1. Hope2333 commented on Apr 1, 2026

    @Hope2333
    Owner

    好的,我来检查一下

  2. Hope2333 commented on Apr 1, 2026

    @Hope2333
    Owner

    根因分析

    确认: statx() 系统调用 (aarch64 #291) 被 Android 的 seccomp 过滤器拦截。

    SIGSYS {si_signo=SIGSYS, si_code=SYS_SECCOMP, si_syscall=__NR_statx}
    → SIGSEGV (进程崩溃)
    

    glibc 的 stat()/fstatat() 内部使用直接 svc #0 系统调用指令(不是公开的 statx() 符号),所以普通的 LD_PRELOAD 符号拦截无法生效。seccomp 拦截后内核发送 SIGSYS 信号,默认处理是终止进程。

    这是 Termux 上已知问题,本仓库内已有多个项目采用相同方案:

    • OpenJDK 17/21/25: #if defined(__linux__) && !defined(__ANDROID__) 禁用 statx
    • libuv: static _Atomic int no_statx; — "The statx system call is prevented by seccomp on Android"
    • util-linux: ac_cv_func_statx=no
    • Qt6: 补丁禁用 Android 上的 statx

    修复方案

    tools/statx-shim.c — 一个通过 LD_PRELOAD 注入的 SIGSYS 信号处理器:

    1. 共享库加载时(__attribute__((constructor)))安装 SIGSYS handler
    2. 当 seccomp 拦截 statx 系统调用时,handler 捕获信号
    3. 设置返回值寄存器 x0 = -ENOSYS
    4. 将程序计数器推进 4 字节(跳过 svc #0 指令)
    5. glibc 看到 -ENOSYS,设置 errno=ENOSYS,回退到 fstatat/newfstatat(这些系统调用未被 seccomp 拦截)

    已提交

    提交 55101ce 包含:

    文件 变更
    tools/statx-shim.c 新增 — SIGSYS handler shim
    scripts/build.sh staging 时编译 shim → lib/libstatx-shim.so
    scripts/launcher.sh 启动 runtime 前 LD_PRELOAD shim
    README.md / docs/README.md 文档更新

    验证步骤

    重新构建并安装后:

    cd ~/opencode/opencode-termux
    make clean
    make all VER=1.2.23 PKG=both

    produce-local.sh 中的 "$RUNTIME_OUT" --version 验证步骤应不再 segfault。

    安装新包后:

    opencode --version
    opencode --help

    回退开关

    如遇问题可禁用 shim:

    export OPENCODE_DISABLE_STATX_SHIM=1
  3. Hope2333 commented on Apr 1, 2026

    @Hope2333
    Owner

    请尝试再次构建看看这个问题是否继续存在,如果已经无此问题则可以关闭这个issus.
    谢谢你的问题反馈!

  4. Hope2333 commented on Apr 1, 2026

    @Hope2333
    Owner

    问题已修复 ✅

    此 issue 报告的核心问题 — statx() 被 Android seccomp 拦截导致 SIGSEGV — 已修复。此外在排查过程中还顺带修复了两个构建流程问题。

    修复内容

    提交 修复内容
    55101ce statx seccomp shim — 核心修复。通过 LD_PRELOAD 注入 SIGSYS handler,拦截被 seccomp 阻断的 statx 系统调用,返回 -ENOSYS 使 glibc 回退到 fstatat
    26268a8 BUN_TERMUX_LOADER 环境变量覆盖 — 允许用户指定 loader 仓库路径,不再局限于硬编码位置
    e9227c7 third-party/ 自动克隆 — 第三方依赖收敛到项目内 third-party/bun-termux-loader/,小白用户零前置依赖即可 make all

    验证方式

    cd ~/opencode/opencode-termux
    git pull
    make clean
    make all VER=1.2.23 PKG=both

    produce-local.sh 中的 "$RUNTIME_OUT" --version 验证步骤应不再 segfault。

    回退开关

    # 禁用 statx shim
    export OPENCODE_DISABLE_STATX_SHIM=1
    
    # 指定 loader 仓库位置
    export BUN_TERMUX_LOADER=/path/to/your/bun-termux-loader

    感谢你的详细诊断报告,特别是 strace 分析直接定位到了根因!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions