Merge "SecurityLog: Apply 7b25bb8 to SecurityLog" am: a98866a6b6 am: a25528b085

am: 1802fc7882

Change-Id: Ieaa64b9a5f92ccaed1ede82811ceab1f1224229c
This commit is contained in:
Andreas Gampe
2017-10-09 23:13:59 +00:00
committed by android-build-merger

View File

@@ -16,13 +16,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <log/log_event_list.h>
#include <log/log_id.h>
#include <private/android_logger.h>
#include <nativehelper/JNIHelp.h> #include <nativehelper/JNIHelp.h>
#include "core_jni_helpers.h" #include "core_jni_helpers.h"
#include "jni.h" #include "jni.h"
#include <private/android_logger.h>
// The size of the tag number comes out of the payload size.
#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
namespace android { namespace android {
@@ -52,65 +52,44 @@ static jboolean android_app_admin_SecurityLog_isLoggingEnabled(JNIEnv* env,
static jint android_app_admin_SecurityLog_writeEvent_String(JNIEnv* env, static jint android_app_admin_SecurityLog_writeEvent_String(JNIEnv* env,
jobject /* clazz */, jobject /* clazz */,
jint tag, jstring value) { jint tag, jstring value) {
uint8_t buf[MAX_EVENT_PAYLOAD]; android_log_event_list ctx(tag);
// Don't throw NPE -- I feel like it's sort of mean for a logging function // Don't throw NPE -- I feel like it's sort of mean for a logging function
// to be all crashy if you pass in NULL -- but make the NULL value explicit. // to be all crashy if you pass in NULL -- but make the NULL value explicit.
const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL"; if (value != NULL) {
uint32_t len = strlen(str); const char *str = env->GetStringUTFChars(value, NULL);
size_t max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline ctx << str;
if (len > max) len = max; env->ReleaseStringUTFChars(value, str);
} else {
buf[0] = EVENT_TYPE_STRING; ctx << "NULL";
memcpy(&buf[1], &len, sizeof(len)); }
memcpy(&buf[1 + sizeof(len)], str, len); return ctx.write(log_id_t::LOG_ID_SECURITY);
buf[1 + sizeof(len) + len] = '\n';
if (value != NULL) env->ReleaseStringUTFChars(value, str);
return __android_log_security_bwrite(tag, buf, 2 + sizeof(len) + len);
} }
static jint android_app_admin_SecurityLog_writeEvent_Array(JNIEnv* env, jobject clazz, static jint android_app_admin_SecurityLog_writeEvent_Array(JNIEnv* env, jobject clazz,
jint tag, jobjectArray value) { jint tag, jobjectArray value) {
if (value == NULL) { android_log_event_list ctx(tag);
return android_app_admin_SecurityLog_writeEvent_String(env, clazz, tag, NULL);
}
uint8_t buf[MAX_EVENT_PAYLOAD]; if (value == NULL) {
const size_t max = sizeof(buf) - 1; // leave room for final newline ctx << "[NULL]";
size_t pos = 2; // Save room for type tag & array count return ctx.write(log_id_t::LOG_ID_SECURITY);
}
jsize copied = 0, num = env->GetArrayLength(value); jsize copied = 0, num = env->GetArrayLength(value);
for (; copied < num && copied < 255; ++copied) { for (; copied < num && copied < 255; ++copied) {
if (ctx.status()) break;
jobject item = env->GetObjectArrayElement(value, copied); jobject item = env->GetObjectArrayElement(value, copied);
if (item == NULL || env->IsInstanceOf(item, gStringClass)) { if (item == NULL) {
if (pos + 1 + sizeof(jint) > max) break; ctx << "NULL";
const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL"; } else if (env->IsInstanceOf(item, gStringClass)) {
jint len = strlen(str); const char *str = env->GetStringUTFChars((jstring) item, NULL);
if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len); ctx << str;
buf[pos++] = EVENT_TYPE_STRING; env->ReleaseStringUTFChars((jstring) item, str);
memcpy(&buf[pos], &len, sizeof(len));
memcpy(&buf[pos + sizeof(len)], str, len);
pos += sizeof(len) + len;
if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str);
} else if (env->IsInstanceOf(item, gIntegerClass)) { } else if (env->IsInstanceOf(item, gIntegerClass)) {
jint intVal = env->GetIntField(item, gIntegerValueID); ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
if (pos + 1 + sizeof(intVal) > max) break;
buf[pos++] = EVENT_TYPE_INT;
memcpy(&buf[pos], &intVal, sizeof(intVal));
pos += sizeof(intVal);
} else if (env->IsInstanceOf(item, gLongClass)) { } else if (env->IsInstanceOf(item, gLongClass)) {
jlong longVal = env->GetLongField(item, gLongValueID); ctx << (int64_t)env->GetLongField(item, gLongValueID);
if (pos + 1 + sizeof(longVal) > max) break;
buf[pos++] = EVENT_TYPE_LONG;
memcpy(&buf[pos], &longVal, sizeof(longVal));
pos += sizeof(longVal);
} else if (env->IsInstanceOf(item, gFloatClass)) { } else if (env->IsInstanceOf(item, gFloatClass)) {
jfloat floatVal = env->GetFloatField(item, gFloatValueID); ctx << (float)env->GetFloatField(item, gFloatValueID);
if (pos + 1 + sizeof(floatVal) > max) break;
buf[pos++] = EVENT_TYPE_FLOAT;
memcpy(&buf[pos], &floatVal, sizeof(floatVal));
pos += sizeof(floatVal);
} else { } else {
jniThrowException(env, jniThrowException(env,
"java/lang/IllegalArgumentException", "java/lang/IllegalArgumentException",
@@ -119,11 +98,7 @@ static jint android_app_admin_SecurityLog_writeEvent_Array(JNIEnv* env, jobject
} }
env->DeleteLocalRef(item); env->DeleteLocalRef(item);
} }
return ctx.write(log_id_t::LOG_ID_SECURITY);
buf[0] = EVENT_TYPE_LIST;
buf[1] = copied;
buf[pos++] = '\n';
return __android_log_security_bwrite(tag, buf, pos);
} }
static void readEvents(JNIEnv* env, int loggerMode, jlong startTime, jobject out) { static void readEvents(JNIEnv* env, int loggerMode, jlong startTime, jobject out) {