在 openEuler 上为 LLVM/ASan 增强 wchar_t 字符串函数支持的开源贡献实践
在 openEuler 上为 LLVM/ASan 增强 wchar_t 字符串函数支持的开源贡献实践
- 本机环境(openEuler)
- 动机:从 Tech-ASan 研究到社区贡献
- 贡献流程:从 fork 到发起 PR
- 实战案例:wchar_t 相关 PR 时间线
- 审稿人或社区其他成员指出问题该怎么办?
- 开源社区 CI bot 检出问题怎么办?
- 开发过程中的注意事项(流程与提交组织)
- 在 openEuler 上开发的小贴士
- 参考与延伸阅读
关键词:openEuler、LLVM、compiler-rt、ASan、wchar_t、wcscpy/wcsncpy、wcscat/wcsncat、Revert-Reland
本文介绍在 openEuler 操作系统(aarch64,Kunpeng-920)环境下,向 LLVM 项目(llvm-project)的 AddressSanitizer(ASan)提交关于 wchar_t
字符串函数支持的开源贡献全流程,并结合审稿反馈讲解“未合并时追加 commit、已合并后 Revert-Reland”的实战策略。
本机环境(openEuler)
以下信息来自系统检测,便于读者在近似环境复现:
OS: openEuler 22.03 LTS
Kernel: Linux 5.10.0-60.18.0.50.oe2203.aarch64
CPU: Kunpeng-920 (aarch64)
Clang: 12.0.1 (openEuler)
GCC: 10.3.1
CMake: 3.22.0
Ninja: 1.13.0
Python: 3.9.9
动机:从 Tech-ASan 研究到社区贡献
在 2025 年的 Internetware 会议上,我们发表了论文《Tech-ASan: Two-stage check for Address Sanitizer》,设计、实现并评估了 ASan 的两阶段检查,并在过程中注意到 ASan 对 wchar_t
字符串函数的支持不完善,尤其是 wcscpy
、wcsncpy
等。论文预印本见:https://arxiv.org/abs/2506.05022。为了提升在宽字符场景下的内存错误捕获能力,我们基于此研究向 LLVM 的 ASan 运行时(compiler-rt
)提交了相关改进。
贡献流程:从 fork 到发起 PR
整个流程遵循 LLVM 社区推荐的贡献路径与代码评审规范:
- LLVM GitHub 使用指南:
LLVM GitHub User Guide
- LLVM 开发者政策:
LLVM Developer Policy
- 提交流程与补丁指南:
Contributing to LLVM
- 代码风格:
LLVM Coding Standards
基于上述文档,我们在 openEuler 上的实际步骤如下:
- Fork 官方仓库到个人账号
- 上游仓库:
llvm/llvm-project
(https://github.com/llvm/llvm-project
) - 在 GitHub 页面点击 Fork,得到个人派生仓库。
- 本地克隆与创建分支
git clone https://github.com/<your-username>/llvm-project.git
cd llvm-project
git checkout -b feature/asan-wchar-support
- 本地构建与测试
以最小化工程为例,仅启用 clang
与 compiler-rt
,并使用 Ninja 构建:
cmake -S llvm -B build \-G Ninja \-DCMAKE_BUILD_TYPE=Release \-DLLVM_TARGETS_TO_BUILD="AArch64" \-DLLVM_ENABLE_PROJECTS="clang;compiler-rt"ninja -C build check-asan
- 实现与提交改动
在 compiler-rt
的 ASan 运行时中添加/注册对 wchar_t
字符串函数的拦截与检查逻辑,并补充测试用例(例如 wcscpy
、wcsncpy
、wcscat
、wcsncat
),以及在 Windows 平台上启用 wcscat
、wcsncat
。
git add -A
git commit -m "[compiler-rt][asan] Add wchar interceptors (wcscpy/wcsncpy); enable wcscat/wcsncat on Windows"
git push origin feature/asan-wchar-support
- 在 GitHub 发起 Pull Request(PR)
- 选择分支,填写变更动机、实现要点、测试覆盖及平台验证情况。
- 与审稿人保持积极但克制的沟通,遵循社区礼仪(例如,如果审稿人没有及时回复,每周一次友好 ping 即可)。
实战案例:wchar_t 相关 PR 时间线
以下 PR 展示了该特性的提交、调整与回滚重提的真实历程,读者可按需跳转细读:
-
初始尝试:添加
wcscpy/wcsncpy
拦截器,并在 Windows 上启用wcscat/wcsncat
(发现存在错误,遂主动关闭,认真审阅后重新提交)- PR #158231:
[compiler-rt][asan] Enable wchar checks: wcscpy/wcsncpy; enable wcscat/wcsncat on Windows
- PR #158231:
-
后续提交与增强:
- PR #160493:
[compiler-rt][asan] Add wcscpy/wcsncpy; enable wcscat/wcsncat on Windows
- PR #161624:
[compiler-rt][asan][tests] Stabilize wchar tests on Darwin/Android
- PR #160493:
-
回滚与重提(Revert - Reland)示例:
- PR #162021(Revert):
Revert "[compiler-rt][asan] Add wcscpy/wcsncpy; enable wcscat/wcsncat on Windows"
- PR #162001(Revert):
Revert "[compiler-rt][asan][tests] Stabilize wchar tests on Darwin/Android"
- PR #162002(草稿/已关闭,test-only reland):
[compiler-rt][asan][tests] Reland: Stabilize wchar tests on Darwin/Android (test-only)
- PR #162028(完整 Reland:运行时 + 测试):
[compiler-rt][asan] Reland: wcscpy/wcsncpy interceptors and stabilize wchar tests on Darwin/Android
- PR #162021(Revert):
说明:不同平台(如 Darwin/Android/Windows)行为差异、构建配置及测试基础设施差异,常常会导致“上游环境一切通过、下游 CI 暴露问题”的情形。通过小步提交、回滚与重提,可以在保证主分支稳定性的同时,快速迭代完善实现与测试。
审稿人或社区其他成员指出问题该怎么办?
基于是否已合并,处理策略有所不同:
-
未被合并(open PR):
- 直接在当前 PR 分支上继续提交修复 commit,保证评审人员只需要关注新增的改动;
- 及时告知相关人员修改的思路和方法,确保积极的沟通。
-
已被合并(merged PR):
- 推荐采用“Revert - Reland”策略。
- Revert:回滚问题变更,迅速恢复主分支稳定;
- Reland:在新分支修复问题后重新提交,补足测试与平台覆盖。
- 注:在构建/测试持续失败时,审稿人可能会直接建议或发起 Revert(例如 PR #162021),优先恢复 bots 绿灯,再推进修复与重提。
社区流程与礼仪参见:LLVM GitHub User Guide
、LLVM Developer Policy
。
开源社区 CI bot 检出问题怎么办?
当 PR 被 CI bot(如 GitHub Actions、llvmbot
触发的流水线等)标红时,建议按以下步骤处理:
- 快速定位、复现和解决失败任务
- 沟通与迭代:
- PR 未合并:在同一分支直接追加 commit,并在 PR 描述或评论中解释根因与修复范围。
- PR 已合并:采用 Revert-Reland,先回滚恢复主分支稳定,再修复后重提 PR。
参考流程与礼仪可见:LLVM GitHub User Guide
|LLVM Developer Policy
。
开发过程中的注意事项(流程与提交组织)
由于不熟悉社区的工作流程,社区审稿人善意地提醒我们开发过程的注意事项。以下2个实战经历可以说明何种工作流程是社区所欢迎的。
- PR #161624(增强测试的稳定性):该 PR 的作用是修补前一个 PR 的遗留问题,却未严格遵循 Revert-Reland 的推荐流程。但社区基于“首次贡献者”的背景,礼貌沟通后予以接纳与合并。需要说明的是,尽管该 PR 被接纳了,但是更推荐 Revert-Reland 保持主干稳定。审稿人给出了如下回复。
Thanks for the fix!
For future reference, please note that LLVM encourages reverting first (https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy):
‘Any time you learn of a serious problem with a change, you should revert it. We strongly encourage “revert to green” as opposed to “fixing forward”. We encourage reverting first, investigating offline, and then reapplying the fixed patch - possibly after another round of review if warranted.’
- PR #162028(完整 Reland):我们吸取先前的教训,Revert 有问题的 PR,并 Reland 最终方案。基于已有的工作,为了便于审查、回滚,并清晰地看到最新的修改,审稿人建议将改动组织为同一个 PR 中的 a stack of 3 commits。
Could you please make this pull request a stack of three commits (in one pull request):
- reland [compiler-rt][asan] Add wcscpy/wcsncpy; enable wcscat/wcsncat on Windows #160493
- reland [compiler-rt][asan][tests] Stabilize wchar tests on Darwin/Android #161624 (fix Fixing Rust build #1)
- new fix (DAG group?)
It would make this much easier to see what’s been fixed. (The commits will be automatically squashed before merging, so it won’t negataively affect the LLVM commit history.)
在 openEuler 上开发的小贴士
- 工具链版本:openEuler LTS 自带的
clang
/gcc
/cmake
/ninja
通常足以完成compiler-rt
开发与单元测试;如需更高版本 Clang,也可以定制化安装或从源码构建。 - 构建建议:小范围启用项目(如仅
clang;compiler-rt
),加快编译与check-asan
反馈回路。 - 跨平台:在 aarch64 环境实现功能后,务必关注其他平台(包括但不限于 x86_64、Windows、Android、Darwin)的行为差异,必要时引入平台守卫或条件编译,并补充跨平台测试用例。本案例之所以存在多次修改,正是因为要根据社区测试机器人的反馈,以解决在 Android/Darwin 系统上的兼容性问题。
openEuler 文档:docs.openeuler.org
参考与延伸阅读
- 论文:
Tech-ASan: Two-stage check for Address Sanitizer
- PR #158231:
[compiler-rt][asan] Enable wchar checks: wcscpy/wcsncpy; enable wcscat/wcsncat on Windows
- PR #160493:
[compiler-rt][asan] Add wcscpy/wcsncpy; enable wcscat/wcsncat on Windows
- PR #161624:
[compiler-rt][asan][tests] Stabilize wchar tests on Darwin/Android
- PR #162001:
Revert "[compiler-rt][asan][tests] Stabilize wchar tests on Darwin/Android"
- PR #162002:
[compiler-rt][asan][tests] Reland: Stabilize wchar tests on Darwin/Android (test-only)
- PR #162028:
[compiler-rt][asan] Reland: wcscpy/wcsncpy interceptors and stabilize wchar tests on Darwin/Android
- LLVM 指南:
LLVM GitHub User Guide
|Developer Policy
|Contributing
|Coding Standards
- openEuler:
docs.openeuler.org