Skip to content

Commit 1b2e01d

Browse files
feat: add new NDK crash scenarios to example app (#1139)
* add: crashes examples to simple app * feat(example): enable NDK crash
1 parent efc0dd2 commit 1b2e01d

File tree

18 files changed

+522
-3
lines changed

18 files changed

+522
-3
lines changed

examples/default/android/app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ android {
156156

157157
}
158158
}
159+
externalNativeBuild {
160+
cmake {
161+
path file('src/main/cpp/CMakeLists.txt')
162+
version '3.22.1'
163+
}
164+
}
159165
}
160166

161167
dependencies {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# For more information about using CMake with Android Studio, read the
3+
# documentation: https://d.android.com/studio/projects/add-native-code.html.
4+
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
5+
6+
# Sets the minimum CMake version required for this project.
7+
cmake_minimum_required(VERSION 3.22.1)
8+
9+
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
10+
# Since this is the top level CMakeLists.txt, the project name is also accessible
11+
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
12+
# build script scope).
13+
project("native-lib")
14+
15+
# Creates and names a library, sets it as either STATIC
16+
# or SHARED, and provides the relative paths to its source code.
17+
# You can define multiple libraries, and CMake builds them for you.
18+
# Gradle automatically packages shared libraries with your APK.
19+
#
20+
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
21+
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
22+
# is preferred for the same purpose.
23+
#
24+
# In order to load a library into your app from Java/Kotlin, you must call
25+
# System.loadLibrary() and pass the name of the library defined here;
26+
# for GameActivity/NativeActivity derived applications, the same library name must be
27+
# used in the AndroidManifest.xml file.
28+
add_library(${CMAKE_PROJECT_NAME} SHARED
29+
# List C/C++ source files with relative paths to this CMakeLists.txt.
30+
native-lib.cpp
31+
crasher.c
32+
crasher_2.c
33+
crasher_3.c
34+
crasher_4.cpp
35+
)
36+
find_library( # Sets the name of the path variable.
37+
log-lib
38+
39+
# Specifies the name of the NDK library that
40+
# you want CMake to locate.
41+
log)
42+
43+
44+
# Specifies libraries CMake should link to your target library. You
45+
# can link libraries from various origins, such as libraries defined in this
46+
# build script, prebuilt third-party libraries, or Android system libraries.
47+
target_link_libraries(${CMAKE_PROJECT_NAME}
48+
# List libraries link to the target library
49+
android
50+
log
51+
${log-lib}
52+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#include <jni.h>
3+
#include <sys/user.h>
4+
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include "crasher_2.h"
7+
8+
/************* SIGSEGV *******************************/
9+
JNIEXPORT void JNICALL
10+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGSEGVCrash(JNIEnv *env, jobject thiz) {
11+
causeSIGSEGVCrashF1();
12+
}
13+
14+
/*****************************************************/
15+
16+
/************* SIGABRT *******************************/
17+
void JNICALL
18+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGABRTCrash(JNIEnv *env, jobject thiz) {
19+
causeSIGABRTCrashF1();
20+
}
21+
/****************************************************/
22+
23+
/************* SIGFPE *******************************/
24+
void JNICALL
25+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGFPECrash(JNIEnv *env, jobject thiz) {
26+
causeSIGFPECrashF1();
27+
}
28+
/***************************************************/
29+
30+
/************* SIGILL *******************************/
31+
32+
void JNICALL
33+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGILLCrash(JNIEnv *env, jobject thiz) {
34+
causeSIGILLCrashF1();
35+
}
36+
/***************************************************/
37+
38+
/************* SIGBUS *******************************/
39+
void JNICALL
40+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGBUSCrash(JNIEnv *env, jobject thiz) {
41+
causeSIGBUSCrashF1();
42+
}
43+
/***************************************************/
44+
45+
/************* SIGTRAP *******************************/
46+
void JNICALL
47+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGTRAPCrash(JNIEnv *env, jobject thiz) {
48+
causeSIGTRAPCrashF1();
49+
}
50+
/***************************************************/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
3+
#include <jni.h>
4+
#include <sys/user.h>
5+
#include <unistd.h>
6+
#include <stdlib.h>
7+
#include "crasher_3.h"
8+
9+
/************* SIGSEGV *******************************/
10+
void causeSIGSEGVCrashF1() {
11+
causeSIGSEGVCrashF2();
12+
}
13+
/*****************************************************/
14+
15+
/************* SIGABRT *******************************/
16+
void causeSIGABRTCrashF1() {
17+
causeSIGABRTCrashF2();
18+
}
19+
/****************************************************/
20+
21+
/************* SIGFPE *******************************/
22+
23+
24+
void causeSIGFPECrashF1() {
25+
causeSIGFPECrashF2();
26+
}
27+
/***************************************************/
28+
29+
/************* SIGILL *******************************/
30+
31+
void causeSIGILLCrashF1() {
32+
causeSIGILLCrashF2();
33+
}
34+
/***************************************************/
35+
36+
/************* SIGBUS *******************************/
37+
38+
void causeSIGBUSCrashF1() {
39+
causeSIGBUSCrashF2();
40+
}
41+
/***************************************************/
42+
43+
/************* SIGTRAP *******************************/
44+
void causeSIGTRAPCrashF1() {
45+
causeSIGTRAPCrashF2();
46+
}
47+
/***************************************************/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
3+
/************* SIGSEGV *******************************/
4+
void causeSIGSEGVCrashF3();
5+
6+
void causeSIGSEGVCrashF2();
7+
8+
void causeSIGSEGVCrashF1();
9+
10+
/*****************************************************/
11+
12+
/************* SIGABRT *******************************/
13+
void causeSIGABRTCrashF3();
14+
15+
void causeSIGABRTCrashF2();
16+
17+
void causeSIGABRTCrashF1();
18+
19+
/****************************************************/
20+
21+
/************* SIGFPE *******************************/
22+
23+
int causeSIGFPECrashF3();
24+
25+
void causeSIGFPECrashF2();
26+
27+
void causeSIGFPECrashF1();
28+
29+
/***************************************************/
30+
31+
/************* SIGILL *******************************/
32+
33+
void causeSIGILLCrashF3();
34+
35+
void causeSIGILLCrashF2();
36+
37+
void causeSIGILLCrashF1();
38+
39+
/***************************************************/
40+
41+
/************* SIGBUS *******************************/
42+
43+
void causeSIGBUSCrashF3();
44+
45+
void causeSIGBUSCrashF2();
46+
47+
void causeSIGBUSCrashF1();
48+
49+
/***************************************************/
50+
51+
/************* SIGTRAP *******************************/
52+
53+
void causeSIGTRAPCrashF3();
54+
55+
void causeSIGTRAPCrashF2();
56+
57+
void causeSIGTRAPCrashF1();
58+
/***************************************************/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#pragma GCC optimize ("O0")
3+
#include <jni.h>
4+
#include <sys/user.h>
5+
#include <unistd.h>
6+
#include <stdlib.h>
7+
#include "crasher_4.h"
8+
9+
/************* SIGSEGV *******************************/
10+
void causeSIGSEGVCrashF2() {
11+
causeSIGSEGVCrashF3(NULL);
12+
}
13+
/*****************************************************/
14+
15+
/************* SIGABRT *******************************/
16+
void causeSIGABRTCrashF2() {
17+
causeSIGABRTCrashF3();
18+
}
19+
/****************************************************/
20+
21+
/************* SIGFPE *******************************/
22+
void causeSIGFPECrashF2() {
23+
unsigned int *bad_pointer = (unsigned int *)(0xdeadbeef);
24+
*bad_pointer=0xfeedface;
25+
}
26+
/***************************************************/
27+
28+
/************* SIGILL *******************************/
29+
void causeSIGILLCrashF2() {
30+
causeSIGILLCrashF3();
31+
}
32+
/***************************************************/
33+
34+
/************* SIGBUS *******************************/
35+
void causeSIGBUSCrashF2() {
36+
causeSIGBUSCrashF3();
37+
}
38+
/***************************************************/
39+
40+
/************* SIGTRAP *******************************/
41+
void causeSIGTRAPCrashF2() {
42+
causeSIGTRAPCrashF3();
43+
}
44+
/***************************************************/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/************* SIGSEGV *******************************/
3+
void causeSIGSEGVCrashF2();
4+
/*****************************************************/
5+
6+
/************* SIGABRT *******************************/
7+
void causeSIGABRTCrashF2();
8+
/****************************************************/
9+
10+
/************* SIGFPE *******************************/
11+
void causeSIGFPECrashF2();
12+
/***************************************************/
13+
14+
/************* SIGILL *******************************/
15+
void causeSIGILLCrashF2();
16+
/***************************************************/
17+
18+
/************* SIGBUS *******************************/
19+
void causeSIGBUSCrashF2();
20+
/***************************************************/
21+
22+
/************* SIGTRAP *******************************/
23+
void causeSIGTRAPCrashF2();
24+
/***************************************************/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
#include <jni.h>
4+
#include <sys/user.h>
5+
#include <unistd.h>
6+
#include <stdlib.h>
7+
#include <stdexcept>
8+
9+
extern "C" {
10+
/************* SIGSEGV *******************************/
11+
void causeSIGSEGVCrashF3(volatile int *i) {
12+
//SIGSEGV
13+
volatile int j = 34 / *i;
14+
}
15+
/*****************************************************/
16+
17+
/************* SIGABRT *******************************/
18+
void causeSIGABRTCrashF3() {
19+
//SIGABRT
20+
throw std::invalid_argument("received invalid value");
21+
}
22+
/****************************************************/
23+
24+
/************* SIGFPE *******************************/
25+
void causeSIGFPECrashF3() {
26+
//SIGFPE
27+
raise(SIGFPE);
28+
pthread_kill(getpid(), SIGFPE);
29+
}
30+
/***************************************************/
31+
32+
/************* SIGILL *******************************/
33+
34+
int causeSIGILLCrashF3() {
35+
//SIGILL
36+
raise(SIGILL);
37+
pthread_kill(getpid(), SIGILL);
38+
}
39+
/***************************************************/
40+
41+
/************* SIGBUS *******************************/
42+
43+
void causeSIGBUSCrashF3() {
44+
//SIGBUS
45+
raise(SIGBUS);
46+
pthread_kill(getpid(), SIGBUS);
47+
}
48+
/***************************************************/
49+
50+
/************* SIGTRAP *******************************/
51+
52+
void causeSIGTRAPCrashF3() {
53+
//SIGBUS
54+
__builtin_trap();
55+
}
56+
/***************************************************/
57+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/************* SIGSEGV *******************************/
3+
void causeSIGSEGVCrashF3(int* i);
4+
/*****************************************************/
5+
6+
/************* SIGABRT *******************************/
7+
void causeSIGABRTCrashF3();
8+
/****************************************************/
9+
10+
/************* SIGFPE *******************************/
11+
void causeSIGFPECrashF3();
12+
/***************************************************/
13+
14+
/************* SIGILL *******************************/
15+
int causeSIGILLCrashF3();
16+
/***************************************************/
17+
18+
/************* SIGBUS *******************************/
19+
void causeSIGBUSCrashF3();
20+
/***************************************************/
21+
22+
/************* SIGTRAP *******************************/
23+
void causeSIGTRAPCrashF3();
24+
/***************************************************/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <jni.h>
2+
#include <string>
3+
#include <android/log.h>
4+
5+
6+
7+
8+
/*
9+
* Throws invalid argument exception
10+
*/
11+
extern "C"
12+
JNIEXPORT void JNICALL
13+
Java_com_instabug_react_example_nativeLibs_CppNativeLib_crashNDK(JNIEnv *env,
14+
jobject object) {
15+
__android_log_print(ANDROID_LOG_DEBUG, "NativeC++", "%s", "received invalid value");
16+
17+
// in Android SDK it's equivalent to causeSIGABRTCrash()
18+
throw std::invalid_argument("received invalid value");
19+
}

0 commit comments

Comments
 (0)