1
头图

Android Memory Safety Tool is a comprehensive toolkit to help you improve the quality and security of your applications. In this article, you can learn about the various memory safety tools we have introduced, their usage scenarios, and how to use them to find and fix problems.

If you prefer to see this through video, check it out here:

https://www.bilibili.com/video/BV1Ga41187oJ/?aid=212288106&cid=549840007&page=1

△ Use memory safety tools to improve application quality and security

what is memory safety error

Memory errors are errors that occur when working with memory in native languages such as C or C++.

void BufferOverflow() {
    char *p = new char[10];
    p[20] = 'x'; // 💣💥 在分配的数组之外写入
}

void UseAfterFree() {
    char *p = new char[10];
    delete[] p;
    p[0] = 'x'; // 💣💥 在数组已经被释放之后写入
}

△ Two common memory security mistakes

In this example, we can see the two most common forms of errors, Buffer Overflow and Use After Free.

End-user devices report more than 3,000 memory-related crashes per second, or 7.7 billion crashes per month, and these crashes are easily perceived by users, resulting in a bad impression of these applications. Using memory safety tools can help you reduce such errors, thereby improving the user experience.

Over 60% of Android bugs each year are caused by memory errors, and other large native codebases other than Android have reported similar issues. Fixing memory errors in apps is just as important as fixing memory errors in the system. Users don't have to care about how the OS protects their data, and your app shouldn't ignore this, and using memory safety tools can help provide users with greater security.

Over 50% of apps on the Play Store contain native code, and even if you don't use native code directly to implement functionality in your app, you may include native code indirectly through the use of third-party SDKs or libraries.

Use memory safety tool

Our mission is to help developers ensure memory safety and help you avoid bugs and vulnerabilities when using native code to handle memory. So we've developed a suite of tools to detect and help developers be more productive, making it easier than ever to detect and fix these kinds of bugs.

Over the years, we've worked to introduce new tools and enhance existing ones, and are now officially introducing you to these three tools:

  • HWASan: Compiler-based memory error detection tool
  • GWP-ASan: Allocator-Based Probabilistic Memory Error Detection Tool
  • Arm MTE: Hardware-Based Memory Error Detection Tool

HWASan

Available since Android 10, HWASan can detect all kinds of memory errors including stack, global and heap issues. Using the tool requires recompilation, as it needs to introduce extra code in all memory operations to run, so the tool may not be suitable for deployment in a production environment. Introducing HWASan approximately doubles the performance of your application, and we recommend that you use HWASan during development and testing.

There are a total of 3 steps to use HWASan:

  • Flash HWASan onto your test device
  • Rebuild your app with the -fsanitize=hwaddress parameter
  • run

We maintain HWASan builds for most Pixel devices, and while the performance of the tool is not suitable for deployment in production, it is adequate for testing. Internally we use HWASan builds to dogfood new Pixel devices. If you want to learn more about HWASan, see the documentation guide HWAddress Sanitizer .

GWP-ASan

GWP-ASan is a probabilistic memory error detection tool that we introduced in Android 11. Probabilistic refers to randomly guarding certain heap allocations, which balances performance and chance of catching errors. It's a bit like a lottery system, as the number of devices running the codebase increases, so does the chance of detecting a bug. GWP-ASan does not require recompilation, and its performance is very suitable for production environments. It is strongly recommended that you use GWP-ASan from early development all the way through testing and deployment to production.

Using GWP-ASan is very simple:

  • Add gwpAsanMode to Android manifest file
  • run

If you would like to know more details about GWP-ASan, please see the documentation guide GWP-ASan .

Arm MTE

Arm MTE is a hardware-based memory error detection tool we developed in partnership with Arm, and we will gradually provide this tool to developers in the future as new hardware becomes available. While this type of hardware technology is available on some Android devices, we strongly recommend that app developers familiarize themselves with HWASan and GWP-ASan for a smoother transition between compatible devices.

run the code and look for issues

Enabling memory safety tools should execute as many code paths as possible, memory errors generate Logcat and Tombstone traces that can be used for local debugging, and in production, reports will be exported from the device to the Play Developer Console. We introduced the new Tombostone API in Android 12 to enable developers to extract more crash information the next time the app is launched. We've been working with Firebase to provide support for memory safety tools in Crashlytics.

fixes issue

△ 内存安全工具提供了包含分配和取消分配回溯的错误报告

△ Memory safety tools provide error reports with allocation and deallocation tracebacks

When using memory safety tools, error reports contain more information to aid debugging, and in addition to unpleasant backtraces, also contain allocation and deallocation tracebacks, which can be helpful in finding the root cause of errors. We've been using these tools when developing the Android operating system in-house, which has helped us detect a large number of bugs that have been hiding in our codebase for years, these tools have greatly improved our ability to detect bugs, and enhanced bug reporting has helped us shorten repair time.

Summary

The Android Memory Safety Tool can detect memory errors in your codebase, and fixing such errors can help improve quality and security. The trick to ensuring memory safety is to run your code with a memory safety tool to find bugs, and then fix those bugs.

Thank you for reading this article and look forward to using the tools we provide to improve the quality and security of the Android ecosystem, if you encounter any issues or have any feedback, please contact us via Github Issues .

You are welcome here to submit feedback to us, or share your favorite content and found problems. Your feedback is very important to us, thank you for your support!


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。