Skip to content

Commit

Permalink
feat: add new NDK crash scenarios to example app (#1139)
Browse files Browse the repository at this point in the history
* add: crashes examples to simple app

* feat(example): enable NDK crash
  • Loading branch information
ahmedAlaaInstabug authored and abdelhamid-f-nasser committed Feb 28, 2024
1 parent efc0dd2 commit 1b2e01d
Show file tree
Hide file tree
Showing 18 changed files with 522 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/default/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ android {

}
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.22.1'
}
}
}

dependencies {
Expand Down
52 changes: 52 additions & 0 deletions examples/default/android/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("native-lib")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
native-lib.cpp
crasher.c
crasher_2.c
crasher_3.c
crasher_4.cpp
)
find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log)


# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log
${log-lib}
)
50 changes: 50 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#include <jni.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdlib.h>
#include "crasher_2.h"

/************* SIGSEGV *******************************/
JNIEXPORT void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGSEGVCrash(JNIEnv *env, jobject thiz) {
causeSIGSEGVCrashF1();
}

/*****************************************************/

/************* SIGABRT *******************************/
void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGABRTCrash(JNIEnv *env, jobject thiz) {
causeSIGABRTCrashF1();
}
/****************************************************/

/************* SIGFPE *******************************/
void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGFPECrash(JNIEnv *env, jobject thiz) {
causeSIGFPECrashF1();
}
/***************************************************/

/************* SIGILL *******************************/

void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGILLCrash(JNIEnv *env, jobject thiz) {
causeSIGILLCrashF1();
}
/***************************************************/

/************* SIGBUS *******************************/
void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGBUSCrash(JNIEnv *env, jobject thiz) {
causeSIGBUSCrashF1();
}
/***************************************************/

/************* SIGTRAP *******************************/
void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_causeSIGTRAPCrash(JNIEnv *env, jobject thiz) {
causeSIGTRAPCrashF1();
}
/***************************************************/
47 changes: 47 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


#include <jni.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdlib.h>
#include "crasher_3.h"

/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF1() {
causeSIGSEGVCrashF2();
}
/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF1() {
causeSIGABRTCrashF2();
}
/****************************************************/

/************* SIGFPE *******************************/


void causeSIGFPECrashF1() {
causeSIGFPECrashF2();
}
/***************************************************/

/************* SIGILL *******************************/

void causeSIGILLCrashF1() {
causeSIGILLCrashF2();
}
/***************************************************/

/************* SIGBUS *******************************/

void causeSIGBUSCrashF1() {
causeSIGBUSCrashF2();
}
/***************************************************/

/************* SIGTRAP *******************************/
void causeSIGTRAPCrashF1() {
causeSIGTRAPCrashF2();
}
/***************************************************/
58 changes: 58 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF3();

void causeSIGSEGVCrashF2();

void causeSIGSEGVCrashF1();

/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF3();

void causeSIGABRTCrashF2();

void causeSIGABRTCrashF1();

/****************************************************/

/************* SIGFPE *******************************/

int causeSIGFPECrashF3();

void causeSIGFPECrashF2();

void causeSIGFPECrashF1();

/***************************************************/

/************* SIGILL *******************************/

void causeSIGILLCrashF3();

void causeSIGILLCrashF2();

void causeSIGILLCrashF1();

/***************************************************/

/************* SIGBUS *******************************/

void causeSIGBUSCrashF3();

void causeSIGBUSCrashF2();

void causeSIGBUSCrashF1();

/***************************************************/

/************* SIGTRAP *******************************/

void causeSIGTRAPCrashF3();

void causeSIGTRAPCrashF2();

void causeSIGTRAPCrashF1();
/***************************************************/
44 changes: 44 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#pragma GCC optimize ("O0")
#include <jni.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdlib.h>
#include "crasher_4.h"

/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF2() {
causeSIGSEGVCrashF3(NULL);
}
/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF2() {
causeSIGABRTCrashF3();
}
/****************************************************/

/************* SIGFPE *******************************/
void causeSIGFPECrashF2() {
unsigned int *bad_pointer = (unsigned int *)(0xdeadbeef);
*bad_pointer=0xfeedface;
}
/***************************************************/

/************* SIGILL *******************************/
void causeSIGILLCrashF2() {
causeSIGILLCrashF3();
}
/***************************************************/

/************* SIGBUS *******************************/
void causeSIGBUSCrashF2() {
causeSIGBUSCrashF3();
}
/***************************************************/

/************* SIGTRAP *******************************/
void causeSIGTRAPCrashF2() {
causeSIGTRAPCrashF3();
}
/***************************************************/
24 changes: 24 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF2();
/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF2();
/****************************************************/

/************* SIGFPE *******************************/
void causeSIGFPECrashF2();
/***************************************************/

/************* SIGILL *******************************/
void causeSIGILLCrashF2();
/***************************************************/

/************* SIGBUS *******************************/
void causeSIGBUSCrashF2();
/***************************************************/

/************* SIGTRAP *******************************/
void causeSIGTRAPCrashF2();
/***************************************************/
57 changes: 57 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@


#include <jni.h>
#include <sys/user.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdexcept>

extern "C" {
/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF3(volatile int *i) {
//SIGSEGV
volatile int j = 34 / *i;
}
/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF3() {
//SIGABRT
throw std::invalid_argument("received invalid value");
}
/****************************************************/

/************* SIGFPE *******************************/
void causeSIGFPECrashF3() {
//SIGFPE
raise(SIGFPE);
pthread_kill(getpid(), SIGFPE);
}
/***************************************************/

/************* SIGILL *******************************/

int causeSIGILLCrashF3() {
//SIGILL
raise(SIGILL);
pthread_kill(getpid(), SIGILL);
}
/***************************************************/

/************* SIGBUS *******************************/

void causeSIGBUSCrashF3() {
//SIGBUS
raise(SIGBUS);
pthread_kill(getpid(), SIGBUS);
}
/***************************************************/

/************* SIGTRAP *******************************/

void causeSIGTRAPCrashF3() {
//SIGBUS
__builtin_trap();
}
/***************************************************/
}
24 changes: 24 additions & 0 deletions examples/default/android/app/src/main/cpp/crasher_4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/************* SIGSEGV *******************************/
void causeSIGSEGVCrashF3(int* i);
/*****************************************************/

/************* SIGABRT *******************************/
void causeSIGABRTCrashF3();
/****************************************************/

/************* SIGFPE *******************************/
void causeSIGFPECrashF3();
/***************************************************/

/************* SIGILL *******************************/
int causeSIGILLCrashF3();
/***************************************************/

/************* SIGBUS *******************************/
void causeSIGBUSCrashF3();
/***************************************************/

/************* SIGTRAP *******************************/
void causeSIGTRAPCrashF3();
/***************************************************/
19 changes: 19 additions & 0 deletions examples/default/android/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <jni.h>
#include <string>
#include <android/log.h>




/*
* Throws invalid argument exception
*/
extern "C"
JNIEXPORT void JNICALL
Java_com_instabug_react_example_nativeLibs_CppNativeLib_crashNDK(JNIEnv *env,
jobject object) {
__android_log_print(ANDROID_LOG_DEBUG, "NativeC++", "%s", "received invalid value");

// in Android SDK it's equivalent to causeSIGABRTCrash()
throw std::invalid_argument("received invalid value");
}
Loading

0 comments on commit 1b2e01d

Please sign in to comment.