-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1708 from bugsnag/PLAT-8582/encode-opaque-metadata
Encode opaque metadata
- Loading branch information
Showing
15 changed files
with
224 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
-keepattributes LineNumberTable,SourceFile | ||
-keep class com.bugsnag.android.ndk.OpaqueValue { | ||
java.lang.String getJson(); | ||
} | ||
-keep class com.bugsnag.android.ndk.NativeBridge { *; } | ||
-keep class com.bugsnag.android.NdkPlugin { *; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
bugsnag-plugin-android-ndk/src/main/java/com/bugsnag/android/ndk/OpaqueValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bugsnag.android.ndk | ||
|
||
import com.bugsnag.android.JsonStream | ||
import java.io.StringWriter | ||
|
||
/** | ||
* Marker class for values that are `BSG_METADATA_OPAQUE_VALUE` in the C layer | ||
*/ | ||
internal class OpaqueValue(val json: String) { | ||
companion object { | ||
private const val MAX_NDK_STRING_LENGTH = 64 | ||
private const val US_ASCII_MAX_CODEPOINT = 127 | ||
|
||
private fun isStringNDKSupported(value: String): Boolean { | ||
// anything over 63 characters is definitely not supported | ||
if (value.length >= MAX_NDK_STRING_LENGTH) return false | ||
|
||
// all chars are US-ASCII valid (0-127)? | ||
if (value.all { ch: Char -> ch.toInt() <= US_ASCII_MAX_CODEPOINT }) { | ||
// US-ASCII values shorter than 64 characters are supported directly | ||
return true | ||
} | ||
|
||
// easiest way to figure it out at this stage is to UTF-8 encode the string and check | ||
// it's length as a byte-array | ||
return value.toByteArray().size < MAX_NDK_STRING_LENGTH | ||
} | ||
|
||
private fun encode(value: Any): String = | ||
StringWriter().use { JsonStream(it).value(value, true) }.toString() | ||
|
||
/** | ||
* Ensure that the given `value` is compatible with the Bugsnag C layer by ensuring it | ||
* is both a compatible type and fits into the available space. This method can return | ||
* any one of: `Boolean`, `Number`, `String`, `OpaqueValue` or `null`. | ||
*/ | ||
fun makeSafe(value: Any?): Any? = when { | ||
value is Boolean -> value | ||
value is Number -> value | ||
value is String && isStringNDKSupported(value) -> value | ||
value is String || value is Map<*, *> || value is List<*> -> OpaqueValue(encode(value)) | ||
else -> null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <malloc.h> | ||
|
||
#include "bugsnag_ndk.h" | ||
#include "memory.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* Global shared context for Bugsnag reports | ||
*/ | ||
static bsg_environment *bsg_global_env; | ||
|
||
void bsg_free(void *ptr) { | ||
/* | ||
* free is only "active" when there are no signal handlers active, if there is | ||
* a crash in progress the memory cannot be safely released within the process | ||
* (free is not async safe), and the heap will be released along with the rest | ||
* of the process when the crash is over. | ||
*/ | ||
if (bsg_global_env->handling_crash) { | ||
return; | ||
} | ||
|
||
free(ptr); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef BUGSNAG_ANDROID_MEMORY_H | ||
#define BUGSNAG_ANDROID_MEMORY_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* Crash-safe 'free' that won't cause deadlocks if a signal handler is active. | ||
* @param ptr | ||
*/ | ||
void bsg_free(void *ptr); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // BUGSNAG_ANDROID_MEMORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters