تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتوافق Android NDK مع تطبيق Address Sanitizer (المعروف أيضًا باسم ASan) الذي يبدأ
مع المستوى 27 من واجهة برمجة التطبيقات (Android O MR 1)
ASan هي أداة سريعة تستند إلى برنامج التجميع لرصد أخطاء الذاكرة في الرموز البرمجية الأصلية.
يكتشف ASan ما يلي:
تجاوز/تدفق المخزن المؤقت للتكديس وأجزاء من الذاكرة
استخدام لقطات لأجزاء من الذاكرة بعد الفترة المجانية
استخدام التكديس خارج النطاق
موقف سيارات مزدوج/بدون تكلفة
يبلغ الزيادة في وحدة المعالجة المركزية في ASan حوالي الضعف، ويتراوح حجم الرمز البرمجي بين 50% و2x،
وحجم الذاكرة كبيرًا (يعتمد على أنماط التخصيص، ولكن
بالترتيب 2x).
لإنشاء الرمز الأصلي (JNI) لتطبيقك باستخدام مصحِّح العنوان، عليك تنفيذ
التالي:
لعبة ndk-build
في Application.mk:
APP_STL:=c++_shared# Or system, or none.APP_CFLAGS:=-fsanitize=address-fno-omit-frame-pointer
APP_LDFLAGS:=-fsanitize=address
لكل وحدة في Android.mk:
LOCAL_ARM_MODE:=arm
إنشاء فيديوهات Shorts
في Build.gradle:
android {
defaultConfig {
externalNativeBuild {
cmake {
// Can also use system or none as ANDROID_STL.
arguments "-DANDROID_ARM_MODE=arm", "-DANDROID_STL=c++_shared"
}
}
}
}
بدءًا من Android O MR1 (المستوى 27 من واجهة برمجة التطبيقات)، يمكن أن يوفر التطبيق
التفاف النص البرمجي Shell الذي يمكن أن يلتف حول عملية التطبيق أو يحل محلها. هذا يسمح
تطبيق يمكن تصحيح الأخطاء به لتخصيص بدء تشغيل التطبيق، وهو ما يمكّن
باستخدام ASan على أجهزة الإنتاج.
على معقّم العنوان إلغاء الحزمة في كل malloc/realloc/free.
الاتصال. هناك خياران هنا:
"سرعة" أداة إلغاء التمرير بالإطار المستند إلى المؤشر. وهذا ما يتم استخدامه من خلال اتباع
التعليمات في قسم المبنى.
"بطيئة" إلغاء ربط CFI. في هذا الوضع، يستخدم ASan _Unwind_Backtrace. أُنشأها جون هنتر، الذي كان متخصصًا
تتطلب فقط -funwind-tables، والتي تكون مفعّلة عادةً بشكل تلقائي.
وتكون أداة التراجع السريع هي الخيار التلقائي لـ mailoc/realloc/free. تُعد عملية التفكيك البطيئة
هو الإعداد الافتراضي لعمليات تتبع تسلسل استدعاء الدوال البرمجية الفادحة. يمكن تفعيل ميزة إلغاء التمرير البطيء للجميع
عمليات تتبُّع تسلسل استدعاء الدوال البرمجية من خلال إضافة fast_unwind_on_malloc=0 إلى المتغيّر ASAN_OPTIONS
في wrap.sh.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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."]]