[null,null,["最后更新时间 (UTC):2023-09-25。"],[],[],null,["# Memory error debugging and mitigation\n\nAndroid supports multiple tools for debugging memory errors. There are tradeoffs\nto each, so read below to decide which is best for your use case. This doc gives\nyou an overview of the tools available so you can decide which to investigate\nfurther, but it aims to be succinct, so read the tool-specific docs for details.\n\ntl;dr\n-----\n\n- Use a [memory safe language](#memory-safe-languages) whenever possible to make memory errors impossible\n- Always use [PAC/BTI](#pacbti) to mitigate ROP/JOP attacks\n- Always use [GWP-ASan](#gwp-asan) for detecting rare memory errors in production\n- Use [HWASan](#hwasan) to detect memory errors during testing\n- [MTE](#mte) is available as an option on [select devices](/ndk/guides/arm-mte#hwsupport).\n- Use [ASan](#asan) during testing only as a last resort\n\nMemory safe languages\n---------------------\n\nA memory safe language is the only way to entirely avoid and mitigate memory\nerrors. The other tools on this page can help you make your memory unsafe code\nsafer and more reliable, but using a memory safe language eliminates the entire\nclass of problems.\n\nThe officially supported memory safe languages for Android are Java and Kotlin.\nMost Android applications are easier to develop in one of those languages.\n\nThat said, there are app developers shipping code written in Rust, and if you're\nreading this page you probably do have a good reason to need native code\n(portability, performance, or both). Rust is the best choice for memory safe\nnative code on Android. The NDK team is not necessarily able to help you with\nthe problems you face if you go that route, but we are interested in\n[hearing about them](https://github.com/android/ndk/discussions)!\n\nPAC/BTI\n-------\n\n[Pointer Authentication and Branch Target Identification](/ndk/guides/abis#armv9_enabling_pac_and_bti_for_cc),\nalso known as PAC/BTI, are mitigation tools suitable for use in production.\nThough separate technologies, they are controlled by the same compiler flag so\nare always used together.\n\nThese features are backward compatible with devices that don't support them\nbecause the new instructions used are no-ops on earlier devices. It's also\nnecessary to have a new enough kernel and a new enough version of the OS.\nLooking for `paca` and `bti` in `/proc/cpuinfo` shows you whether you have new\nenough hardware and a new enough kernel. Android 12 (API 31) has the necessary\nuserspace support.\n| **Key Point:** Good mitigation requires defense in depth. Consider combining PAC/BTI with [MTE](#mte).\n\nPros:\n\n- Can be enabled in all builds without causing problems on older devices or kernels (but make sure you've actually tested on a device/kernel/OS combination that *does* support it!)\n\nCons:\n\n- Only available for 64-bit apps\n- Doesn't mitigate errors on devices that don't support it\n- 1% code size overhead\n\nGWP-Asan\n--------\n\n[GWP-ASan](/ndk/guides/gwp-asan) can be used to detect memory errors in the\nfield, but the sampling rate is too low to be an effective mitigation.\n\nPros:\n\n- No significant CPU or memory overhead\n- Trivial to deploy: does not require rebuilding native code\n- Works for 32-bit apps\n\nCons:\n\n- Low sampling rate requires a large number of users to find bugs effectively\n- Only detects heap errors, not stack errors\n\nHWASan\n------\n\n[Hardware address sanitizer](/ndk/guides/hwasan), also known as HWASan, is the\nbest fit for catching memory errors during testing. It's most useful when used\nwith automated testing, especially if you're running fuzz tests, but depending\non your app's performance needs it may also be usable on high end phones in a\ndogfood setting.\n\nPros:\n\n- No false positives\n- Detects additional classes of errors that ASan cannot (stack use after return)\n- Lower rate of false negatives than MTE (1 in 256 vs 1 in 16)\n- Lower memory overhead than ASan, its closest alternative\n\nCons:\n\n- Significant CPU (\\~100%), code size (\\~50%), and memory (10% - 35%) overhead\n- Until API 34 and NDK r26, requires flashing a HWASan compatible image\n- Only works on 64-bit apps\n\nMTE\n---\n\n[Memory tagging extension](/ndk/guides/arm-mte), also known as MTE, is a lower-\ncost alternative to HWASan. In addition to debugging and testing capabilities,\nit can be used to detect and mitigate memory corruption in production. If you\nhave [the hardware to test MTE builds](/ndk/guides/arm-mte#check), you should\nenable it.\n\nPros:\n\n- Low enough overhead to be tolerable in production for many apps\n- No false positives\n- Does not require rebuilding code to detect heap errors (but does to detect stack errors)\n\nCons:\n\n- There are no commercially available devices with MTE enabled by default in 2024, but Arm's documentation explains [how to enable MTE for testing on Pixel 8/Pixel 8 Pro](https://learn.arm.com/learning-paths/smartphones-and-mobile/mte_on_pixel8/).\n- False negative rate of 1 in 16 vs HWASan's 1 in 256\n- Only available for 64-bit apps\n- Requires building separate libraries for targeting both MTE-enabled and non- MTE enabled devices\n\nASan\n----\n\n[Address sanitizer](/ndk/guides/asan), also known as ASan, is the oldest and\nmost widely available of the tools available. It is useful for catching memory\nerrors during testing and debugging issues that only affect old devices where\nnone of the other tools are available. **Whenever possible, prefer HWASan.**\n| **Deprecated:** ASan has been superseded by the other tools on this page, so is no longer receiving active development or support. ASan should only be used when HWASan is not an option.\n\nPros:\n\n- Widely available. May work on devices as old as KitKat\n- No false positives or negatives when used correctly\n\nCons:\n\n- Difficult to build and package correctly\n- Highest overhead of all options: \\~100% CPU, \\~50% code size, \\~100% memory use\n- No longer supported\n- Has known bugs that won't be fixed"]]