Skip to content

fix(scrcpy): omit server-default options from scrcpy-server command line (Samsung ACodec 255-byte crash) - #8

Merged
Exmeaning merged 1 commit into
masterfrom
arena/9354e184-clickzen
Nov 18, 2026
Merged

Exmeaning merged 1 commit into
masterfrom
arena/9354e184-clickzen

Conversation

@Exmeaning

Copy link
Copy Markdown
Owner

问题(Issue #7)

三星 Galaxy S20+(SM-G9860,Android 13)投屏反复重连:connected on display 0 后约 0.3 秒即断开,手机端 scrcpy-server 发生 native 崩溃:

F libc    : stack corruption detected (-fstack-protector)
F DEBUG   : Cmdline: app_process / com.genymobile.scrcpy.Server 4.1 scid=4a0a27bc log_level=info video=true audio=false control=true tunnel_forward=false video_codec=h264 max_size=1080 video_bit_rate=4000000 max_fps=30 stay_awake=true show_touches=false power_off_on_close=false clipboard_autosync=false cleanup=true
F DEBUG   :   #02 ... /system/lib64/libstagefright.so (android::ACodec::reconfigEncoder4OtherApps(...)+768)

三星系统底层 ACodec::reconfigEncoder4OtherApps 会把 /proc/<pid>/cmdline 读入约 255 字节的栈缓冲区(Genymobile/scrcpy#7169)。ClickZen 原先把所有参数(含大量与服务端默认值相同的项)都显式拼进命令行,总长约 295 字节,直接触发栈保护导致 CodecLooper 被 SIGABRT 杀掉。

修复

对齐上游 scrcpy server.c 的做法:只发送与 scrcpy-server 默认值(Options.java v4.1)不同的参数,其余省略,服务端自动使用默认值。

参数 服务端默认值 新行为
video / control true 为 true 时省略
tunnel_forward / show_touches / power_off_on_close false 为 false 时省略
video_codec h264 恒为 h264,不再发送
cleanup true 恒为 true,不再发送
max_size / max_fps 0 为 0 时省略
video_bit_rate 8000000 等于默认值时省略
log_level debug 为 debug 时省略(默认 info 仍发送,保持原日志级别)
scid — 始终发送
audio / clipboard_autosync true ClickZen 恒为 false,仍发送
stay_awake false 为 true 时发送
display_id 0 > 0 时发送

效果(app_process argv 长度):

  • 修复前:约 295 字节(Issue 报告值)
  • 修复后默认:153 字节;最坏真实组合(forward 回退 + display_id + 非默认码率/分辨率/帧率 + 显示触摸):228 字节

CLASSPATH=... 是环境变量,不在 /proc/<pid>/cmdline 中,不计入 255 字节限制。

测试

tests/ClickZen.Device.Tests/Scrcpy/ProtocolTests.cs(ScrcpyServerOptionsTests):

  • 默认选项的精确命令行(锁定最小命令格式);
  • 默认值参数均不出现(video=true、control=true、video_codec、cleanup、tunnel_forward、show_touches、power_off_on_close、video_bit_rate、display_id);
  • 非默认选项均正确发送(video_bit_rate=4000000、show_touches=true、power_off_on_close=true、control=false,以及 stay_awake/log_level/max_size/max_fps 的省略);
  • 三组参数组合的 argv 长度 < 255 断言(三星限制回归防护)。

Closes #7

…ine (Samsung ACodec 255-byte crash)

Samsung's ACodec::reconfigEncoder4OtherApps copies /proc/<pid>/cmdline into a
~255-byte stack buffer (Genymobile/scrcpy#7169). ClickZen sent every option
explicitly (~295 bytes), tripping -fstack-protector and killing scrcpy-server
on Galaxy devices (issue #7).

Mirror upstream scrcpy server.c: only send values that differ from the
scrcpy-server defaults (Options.java v4.1). Typical command drops from ~295
to ~153 bytes; worst realistic combination stays at 228 bytes.

- Omit when equal to server default: video/control=true, tunnel_forward/
  show_touches/power_off_on_close=false, video_codec=h264, cleanup=true,
  max_size/max_fps=0, video_bit_rate=8000000, log_level=debug.
- Keep sending non-defaults: scid, log_level=info, audio=false,
  clipboard_autosync=false, stay_awake=true, max_size/max_fps/video_bit_rate
  when non-default, tunnel_forward=true, display_id>0.
- Tests: exact default command, non-default emission, <255-byte argv guard.

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@Exmeaning
Exmeaning merged commit 41184ca into master Nov 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 三星设备(SM-G9860)投屏反复重连:scrcpy-server 命令行超过 255 字节触发 ACodec 栈溢出崩溃

1 participant