Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Android NDK hỗ trợ Address Sanitizer (còn gọi là ASan) kể từ API cấp 27 (Android O MR 1).
ASan là một công cụ dựa trên trình biên dịch giúp phát hiện các lỗi bộ nhớ trong mã gốc.
ASan phát hiện:
Chặn tràn (overflow)/chặn trống (underflow) cho ngăn xếp (stack) và bộ nhớ khối xếp (heap)
Sử dụng bộ nhớ khối xếp sau khi giải phóng
Sử dụng ngăn xếp bên ngoài phạm vi
Giải phóng hai lần/giải phóng bộ nhớ chưa được cấp phát trước đó
Mức hao tổn CPU của ASan là khoảng 2x, mức hao tổn kích thước mã trong khoảng từ 50% đến 2x và mức hao tổn bộ nhớ khá lớn (tuỳ theo mô hình phân bổ, nhưng vào khoảng 2x).
Để tạo mã gốc (JNI) của ứng dụng bằng Address Sanitizer, hãy làm như sau:
ndk-build
Trong Application.mk:
APP_STL:=c++_shared# Or system, or none.APP_CFLAGS:=-fsanitize=address-fno-omit-frame-pointer
APP_LDFLAGS:=-fsanitize=address
Đối với mỗi mô-đun trong Android.mk:
LOCAL_ARM_MODE:=arm
CMake
Trong tệp build.gradle của mô-đun:
android {
defaultConfig {
externalNativeBuild {
cmake {
// Can also use system or none as ANDROID_STL.
arguments "-DANDROID_ARM_MODE=arm", "-DANDROID_STL=c++_shared"
}
}
}
}
Kể từ Android O MR1 (API cấp 27), một ứng dụng có thể cung cấp tập lệnh bao bọc môi trường shell có khả năng bao bọc hoặc thay thế quy trình của ứng dụng. Nhờ đó, một ứng dụng có thể gỡ lỗi có khả năng tuỳ chỉnh hoạt động khởi động ứng dụng, cho phép sử dụng ASan trên các thiết bị phát hành chính thức.
Address Sanitizer cần gỡ bỏ ngăn xếp trong mọi lệnh gọi malloc/realloc/free. Có hai cách:
Bộ gỡ bỏ dựa trên con trỏ khung "fast" (nhanh). Bạn sẽ sử dụng công cụ này theo hướng dẫn trong phần xây dựng.
Bộ gỡ bỏ CFI "slow" (chậm). Ở chế độ này, ASan sử dụng _Unwind_Backtrace. Phương thức này chỉ yêu cầu -funwind-tables (thường được bật theo mặc định).
Bộ gỡ bỏ nhanh là tuỳ chọn mặc định cho malloc/realloc/free. Bộ gỡ bỏ chậm là tuỳ chọn mặc định cho dấu vết ngăn xếp quan trọng. Bạn có thể bật bộ gỡ bỏ chậm cho tất cả dấu vết ngăn xếp bằng cách thêm fast_unwind_on_malloc=0 vào biến ASAN_OPTIONS trong wrap.sh.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Address Sanitizer\n\n| **Note:** This document covers running Android applications built with the NDK under [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer). For information about using [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) on Android platform components, see the [AOSP documentation](https://source.android.com/devices/tech/debug/asan.html).\n| **Deprecated:** As of 2023, ASan is unsupported. It is recommended to use [HWASan](/ndk/guides/hwasan) instead. HWASan can be used on ARM64 devices running Android 14 (API level 34) or newer; or on Pixel devices running Android 10 (API level 29) by flashing a [special system image](/ndk/guides/hwasan#setup). ASan may still be used but might have bugs.\n| **Important:** Asan is one of many tools available for memory debugging and mitigation. See [Memory error debugging and mitigation](/ndk/guides/memory-debug) for an overview of all the tools.\n\nThe Android NDK supports [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) (also known as ASan) beginning\nwith API level 27 (Android O MR 1).\n\nASan is a fast compiler-based tool for detecting memory bugs in native code.\nASan detects:\n\n- Stack and heap buffer overflow/underflow\n- Heap use after free\n- Stack use outside scope\n- Double free/wild free\n\nASan's CPU overhead is roughly 2x, code size overhead is between 50% and 2x,\nand the memory overhead is large (dependent on your allocation patterns, but on\nthe order of 2x).\n\nSample App\n----------\n\nA [sample app](https://github.com/android/ndk-samples/tree/main/sanitizers)\nshows how to configure a [build variant](/studio/build/build-variants) for asan.\n\nBuild\n-----\n\nTo build your app's native (JNI) code with [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer), do the\nfollowing: \n\n### ndk-build\n\nIn your Application.mk: \n\n APP_STL := c++_shared # Or system, or none.\n APP_CFLAGS := -fsanitize=address -fno-omit-frame-pointer\n APP_LDFLAGS := -fsanitize=address\n\nFor each module in your Android.mk: \n\n LOCAL_ARM_MODE := arm\n\n### CMake\n\nIn your module's build.gradle: \n\n android {\n defaultConfig {\n externalNativeBuild {\n cmake {\n // Can also use system or none as ANDROID_STL.\n arguments \"-DANDROID_ARM_MODE=arm\", \"-DANDROID_STL=c++_shared\"\n }\n }\n }\n }\n\nFor each target in your CMakeLists.txt: \n\n target_compile_options(${TARGET} PUBLIC -fsanitize=address -fno-omit-frame-pointer)\n set_target_properties(${TARGET} PROPERTIES LINK_FLAGS -fsanitize=address)\n\n| **Caution:** ASan is currently incompatible with C++ exception handling when using `libc++_static`. Apps using `libc++_shared` or not using exception handling are either unaffected or have workarounds available. See [Issue 988](https://github.com/android-ndk/ndk/issues/988) for more details.\n\nRun\n---\n\nBeginning with Android O MR1 (API level 27) an application can provide a\n[wrap shell script](/ndk/guides/wrap-script) that can wrap or replace the application process. This allows\na debuggable application to customize their application startup, which enables\nusing ASan on production devices.\n| **Note:** The following instructions describe how to use ASan with an Android Studio project. For a non-Android Studio project, refer to the [wrap shell script](/ndk/guides/wrap-script) documentation.\n\n1. Add `android:debuggable` to the application manifest.\n2. Set [`useLegacyPackaging`](/reference/tools/gradle-api/7.1/com/android/build/api/dsl/JniLibsPackagingOptions#uselegacypackaging) to `true` in your app's `build.gradle` file. See the [wrap shell script](/ndk/guides/wrap-script) guide for more information.\n3. Add the ASan runtime library to your app module's `jniLibs`.\n4. Add `wrap.sh` files with the following contents to each directory in your\n `src/main/resources/lib` directory.\n\n #!/system/bin/sh\n HERE=\"$(cd \"$(dirname \"$0\")\" && pwd)\"\n export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1\n ASAN_LIB=$(ls $HERE/libclang_rt.asan-*-android.so)\n if [ -f \"$HERE/libc++_shared.so\" ]; then\n # Workaround for https://github.com/android-ndk/ndk/issues/988.\n export LD_PRELOAD=\"$ASAN_LIB $HERE/libc++_shared.so\"\n else\n export LD_PRELOAD=\"$ASAN_LIB\"\n fi\n \"$@\"\n\n| **Note:** The NDK contains a recommended wrap.sh file for ASan [here](https://android.googlesource.com/platform/ndk/+/refs/heads/master/wrap.sh/asan.sh).\n\nAssuming your project's application module is named `app`, your final directory\nstructure should include the following: \n\n \u003cproject root\u003e\n └── app\n └── src\n └── main\n ├── jniLibs\n │ ├── arm64-v8a\n │ │ └── libclang_rt.asan-aarch64-android.so\n │ ├── armeabi-v7a\n │ │ └── libclang_rt.asan-arm-android.so\n │ ├── x86\n │ │ └── libclang_rt.asan-i686-android.so\n │ └── x86_64\n │ └── libclang_rt.asan-x86_64-android.so\n └── resources\n └── lib\n ├── arm64-v8a\n │ └── wrap.sh\n ├── armeabi-v7a\n │ └── wrap.sh\n ├── x86\n │ └── wrap.sh\n └── x86_64\n └── wrap.sh\n\nStack traces\n------------\n\n[Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) needs to unwind the stack on every `malloc`/`realloc`/`free`\ncall. There are two options here:\n\n1. A \"fast\" frame pointer-based unwinder. This is what is used by following the\n instructions in the [building section](#building).\n\n2. A \"slow\" CFI unwinder. In this mode ASan uses `_Unwind_Backtrace`. It\n requires only `-funwind-tables`, which is normally enabled by default.\n\n | **Caution:** the \"slow\" unwinder is **slow** (10x or more, depending on how often you call `malloc`/`free`).\n\nThe fast unwinder is the default for malloc/realloc/free. The slow unwinder is\nthe default for fatal stack traces. The slow unwinder can be enabled for all\nstack traces by adding `fast_unwind_on_malloc=0` to the `ASAN_OPTIONS` variable\nin your wrap.sh."]]