自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
GWP-ASan 和 KFENCE
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
GWP-ASan 和 KFENCE 分别是面向用户空间和内核的概率性内存检测工具。启用后,系统会用额外的内存页围绕分配的内存,从而为少量分配提供防护。
这样能以忽略不计的性能开销来检测缓冲区溢出和释放后使用。即使为受保护分配采用较低的采样率,在大规模部署时,这些工具也能有效检测内存安全 bug。
自推出后,这些工具帮助识别了许多平台 bug,并且已被证明是提高稳定性和安全性的重要工具。
我们鼓励供应商启用这些工具并密切监控在最终用户设备上检测到的 bug。
开发者应通过检查 /data/tombstones
、logcat
或监控供应商 DropboxManager
管线是否存在最终用户 bug 来检查是否发生了系统崩溃。
如需了解详情,请参阅有关调试 Android 原生代码的文档。
调试原生 Android 平台代码arrow_forward
用于检测用户空间内存安全 bug 的 GWP-ASan
Android 11 中为用户空间堆分配引入了 GWP-ASan。当检测到 bug 时,GWP-ASan 会记录包含 Cause: [GWP-ASan]:
的崩溃报告并终止进程。bug 报告中包含更多分配和取消分配信息,可帮助开发者更轻松地确定根本原因。
在系统进程或应用启动时,随机会有 1% 的系统进程或应用会启用 GWP-ASan。此外,在 AndroidManifest.xml
文件中设置 android:gwpAsanMode
的应用也会启用 GWP-ASan。
GWP-ASanarrow_forward
用于检测内核内存安全 bug 的 KFENCE
Android 12 引入了 KFENCE 来进行内核堆分配。当检测到 bug 时,KFENCE 会记录一个以 BUG: KFENCE,
开头的崩溃报告,停用防护页面并继续执行。
这样可以实现对用户体验影响尽可能小的内核 bug 检测机制。
如需详细了解 KFENCE,请参阅 https://www.kernel.org 上的文档
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# GWP-ASan and KFENCE are probabilistic memory detection tools for userspace\nand the kernel, respectively. When enabled, a small number of allocations are\nguarded with additional memory pages that surround the allocated memory.\nThis allows detecting buffer overflows and use-after-frees at a negligible\nperformance overhead. Even with a small sample rate for the guarded allocations,\nwhen deployed at scale they can effectively detect memory safety bugs.\nSince their introduction these tools have helped identify numerous platform\nbugs and have proved to be a valuable tool for improving stability and security.\nWe encourage vendors to enable them and to closely monitor the bugs detected on\nend user devices.\n\n\nDevelopers should check the presence of crashes by checking\n`/data/tombstones`, `logcat` or by monitoring the vendor\n[DropboxManager](https://developer.android.com/reference/android/os/DropBoxManager)\npipeline for end user bugs.\n\n\nFor more information see our docs on debugging Android native\ncode.\n[Debugging Native Android Platform Codearrow_forward](/docs/core/tests/debug)\n\nGWP-ASan for detecting userspace memory safety bugs\n---------------------------------------------------\n\n\nGWP-ASan was introduced in Android 11 for userspace\nheap allocations. When GWP-ASan detects a bug, it records a crash report with\n`Cause: [GWP-ASan]:` and terminates the process. The bug reports\ncontain additional allocation and deallocation information that make it easier\nto identify the root cause.\n\n\nGWP-ASan is randomly enabled, at start-up, for 1% of system processes or\napps and for applications that set the `android:gwpAsanMode` in their\n`AndroidManifest.xml` file.\n[GWP-ASanarrow_forward](https://developer.android.com/ndk/guides/gwp-asan)\n\nKFENCE for detecting kernel memory safety bugs\n----------------------------------------------\n\n\nAndroid 12 introduced KFENCE for kernel heap\nallocations. When KFENCE detects a bug, it records a crash report starting\nwith `BUG: KFENCE,` disables the guard pages and continues execution.\nThis enables a kernel bug detection mechanism that minimally impacts the user\nexperience.\n\n\nTo learn more about KFENCE see the documentation available at [https://www.kernel.org](https://www.kernel.org/doc/html/latest/dev-tools/kfence.html)"]]