Skip to content

Commit 554ee59

Browse files
committed
Migrate sanitizers to RegisterNatives.
1 parent 32ae6dd commit 554ee59

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

sanitizers/app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ android {
5555
"x86_64",
5656
)
5757
}
58+
59+
// Allows buildTypes which inherit from debug to match dependencies
60+
// with the debug buildType.
61+
matchingFallbacks = ["debug"]
5862
}
5963

6064
release {
@@ -136,6 +140,7 @@ android {
136140
}
137141

138142
buildFeatures {
143+
prefab true
139144
viewBinding true
140145
}
141146

@@ -147,6 +152,7 @@ android {
147152
}
148153

149154
dependencies {
155+
implementation project(":base")
150156
implementation libs.appcompat
151157
implementation libs.material
152158
implementation libs.androidx.constraintlayout

sanitizers/app/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ cmake_minimum_required(VERSION 3.22.1)
22
project(sanitizers LANGUAGES CXX)
33

44
include(AppLibrary)
5+
find_package(base CONFIG REQUIRED)
56

67
add_app_library(sanitizers SHARED native-lib.cpp)
7-
target_link_libraries(sanitizers PRIVATE log)
8+
target_link_libraries(sanitizers PRIVATE base::base log)
89

910
if(SANITIZE)
1011
# For asan and ubsan, we need to copy some files from the NDK into our APK.

sanitizers/app/src/main/cpp/native-lib.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
#include <base/macros.h>
12
#include <jni.h>
23

34
#include <string>
45

5-
extern "C" JNIEXPORT jstring JNICALL
6-
Java_com_example_sanitizers_MainActivity_stringFromJNI(JNIEnv* env,
7-
jobject /* this */) {
6+
jstring StringFromJni(JNIEnv* env, jobject /* this */) {
87
// Use-after-free error, caught by asan and hwasan.
98
int* foo = new int;
109
*foo = 3;
@@ -18,3 +17,23 @@ Java_com_example_sanitizers_MainActivity_stringFromJNI(JNIEnv* env,
1817
std::string hello = "Hello from C++";
1918
return env->NewStringUTF(hello.c_str());
2019
}
20+
21+
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _Nonnull vm,
22+
void* _Nullable) {
23+
JNIEnv* env;
24+
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
25+
return JNI_ERR;
26+
}
27+
28+
jclass c = env->FindClass("com/example/sanitizers/MainActivity");
29+
if (c == nullptr) return JNI_ERR;
30+
31+
static const JNINativeMethod methods[] = {
32+
{"stringFromJNI", "()Ljava/lang/String;",
33+
reinterpret_cast<void*>(StringFromJni)},
34+
};
35+
int rc = env->RegisterNatives(c, methods, arraysize(methods));
36+
if (rc != JNI_OK) return rc;
37+
38+
return JNI_VERSION_1_6;
39+
}

0 commit comments

Comments
 (0)