GWP-ASan是否需要Android 11设备?

问题描述

DocumentationGWP-ASan is available on apps that target Android 11 (API level 30) or higher,但未提及设备的任何要求。

类似地,gwpAsanMode的{​​{3}}表示Added in API level 30,但未提及设备的任何要求。

我的目标是API级别30,并启用了GWP-Asan,我试图触发它只是为了证明它正在工作。我正在遵循文档中示例中的模式,该模式中进行了成千上万次的使用后使用,但并没有触发。我想知道是否是因为我正在Android 7设备上进行测试(我没有可使用的Android 11设备)。

解决方法

需要使用Android 11设备:source

GWP-ASan仅在Android 11设备上可用。因此,要使一个应用具有GWP-ASan,您需要:(1)在清单中指定具有Android 11的设备,(2)android:gwpAsanMode="always"。此外,您需要使用Android 11 SDK来构建应用,因为(2)中的标记未在旧版SDK中定义,并且将无法构建。

,

对我也不起作用,我正在尝试在Android 11的Google Pixel 2 XL上测试GWP-Asan。

更新:由于某种原因,当Android Studio调试器设置为“双重”时,它可以正常运行,并且应用正常启动。在没有附加调试器的情况下运行,使GWP Asan可以正常运行,此外,您可能希望使用标签“ DEBUG”过滤logcat,因为它是在其中打印日志的地方

使用Android Studio中的JNI创建的Android应用,build.gradle如下:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.gwpadresssanitizer"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

清单看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gwpadresssanitizer">

    <application
        android:gwpAsanMode="always"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GWPAdressSanitizer">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

使用代码:

jstring native_get_string(JNIEnv* env) {
    std::string s = "Hellooooooooooooooo ";
    std::string_view sv = s + "World\n";

    // BUG: Use-after-free. `sv` holds a dangling reference to the ephemeral
    // string created by `s + "World\n"`. Accessing the data here is a
    // use-after-free.
    return env->NewStringUTF(sv.data());
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_gwpadresssanitizer_MainActivity_stringFromJNI(
        JNIEnv* env,jobject /* this */) {

    jstring return_string;
    for (unsigned i = 0; i < 0x10000; ++i) {
        return_string = native_get_string(env);
    }

    return reinterpret_cast<jstring>(env->NewGlobalRef(return_string));
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...