Blame SOURCES/0015-Check-error-codes-of-jvmti-functions.patch

b35d98
From 4507e990bfaada71ebeeec800a1e22e869317b8b Mon Sep 17 00:00:00 2001
b35d98
From: Jakub Filak <jfilak@redhat.com>
b35d98
Date: Sat, 4 Jan 2014 21:10:00 +0100
b35d98
Subject: [PATCH 15/39] Check error codes of jvmti functions
b35d98
b35d98
Closes #24
b35d98
Related to rhbz#1051483
b35d98
---
b35d98
 src/abrt-checker.c | 134 +++++++++++++++++++++++++++++++++++------------------
b35d98
 1 file changed, 90 insertions(+), 44 deletions(-)
b35d98
b35d98
diff --git a/src/abrt-checker.c b/src/abrt-checker.c
b35d98
index 26665d6..23101bd 100644
b35d98
--- a/src/abrt-checker.c
b35d98
+++ b/src/abrt-checker.c
b35d98
@@ -573,6 +573,9 @@ static void print_jvmti_error(
b35d98
     (void)(*jvmti_env)->GetErrorName(jvmti_env, error_code, &errnum_str);
b35d98
     msg_err = errnum_str == NULL ? "Unknown" : errnum_str;
b35d98
     fprintf(stderr, "ERROR: JVMTI: %d(%s): %s\n", error_code, msg_err, msg_str);
b35d98
+
b35d98
+    if (NULL != errnum_str)
b35d98
+        (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)errnum_str);
b35d98
 }
b35d98
 
b35d98
 
b35d98
@@ -1547,6 +1550,7 @@ static jclass find_class_in_loaded_class(
b35d98
             JNIEnv     *jni_env,
b35d98
             const char *searched_class_name)
b35d98
 {
b35d98
+    jclass result = NULL;
b35d98
     jint num_classes = 0;
b35d98
     jclass *loaded_classes;
b35d98
     jvmtiError error = (*jvmti_env)->GetLoadedClasses(jvmti_env, &num_classes, &loaded_classes);
b35d98
@@ -1559,7 +1563,7 @@ static jclass find_class_in_loaded_class(
b35d98
     if (NULL == class_class)
b35d98
     {
b35d98
         VERBOSE_PRINT("Cannot find java/lang/Class class");
b35d98
-        return NULL;
b35d98
+        goto find_class_in_loaded_class_cleanup;
b35d98
     }
b35d98
 
b35d98
     jmethodID get_name_method = (*jni_env)->GetMethodID(jni_env, class_class, "getName", "()Ljava/lang/String;");
b35d98
@@ -1567,10 +1571,9 @@ static jclass find_class_in_loaded_class(
b35d98
     {
b35d98
         VERBOSE_PRINT("Cannot find java.lang.Class.getName.()Ljava/lang/String;");
b35d98
         (*jni_env)->DeleteLocalRef(jni_env, class_class);
b35d98
-        return NULL;
b35d98
+        goto find_class_in_loaded_class_cleanup;
b35d98
     }
b35d98
 
b35d98
-    jclass result = NULL;
b35d98
     for (jint i = 0; NULL == result && i < num_classes; ++i)
b35d98
     {
b35d98
         jobject class_name = (*jni_env)->CallObjectMethod(jni_env, loaded_classes[i], get_name_method);
b35d98
@@ -1590,7 +1593,10 @@ static jclass find_class_in_loaded_class(
b35d98
         (*jni_env)->DeleteLocalRef(jni_env, class_name);
b35d98
     }
b35d98
 
b35d98
-    /* Not calling DeleteLocalRef() on items in loaded_classes. Hopefully they will be deleted automatically */
b35d98
+find_class_in_loaded_class_cleanup:
b35d98
+    if (NULL != loaded_classes)
b35d98
+        (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)loaded_classes);
b35d98
+
b35d98
     return result;
b35d98
 }
b35d98
 
b35d98
@@ -1866,8 +1872,9 @@ static void print_one_method_from_stack(
b35d98
 {
b35d98
     jvmtiError  error_code;
b35d98
     jclass      declaring_class;
b35d98
-    char       *method_name = "";
b35d98
-    char       *declaring_class_name = "";
b35d98
+    char       *method_name = NULL;
b35d98
+    char       *declaring_class_name = NULL;
b35d98
+    char       *source_file_name = NULL;
b35d98
 
b35d98
     error_code = (*jvmti_env)->GetMethodName(jvmti_env, stack_frame.method, &method_name, NULL, NULL);
b35d98
     if (error_code != JVMTI_ERROR_NONE)
b35d98
@@ -1875,9 +1882,12 @@ static void print_one_method_from_stack(
b35d98
         return;
b35d98
     }
b35d98
     error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, stack_frame.method, &declaring_class);
b35d98
-    check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto print_one_method_from_stack_cleanup;
b35d98
+
b35d98
     error_code = (*jvmti_env)->GetClassSignature(jvmti_env, declaring_class, &declaring_class_name, NULL);
b35d98
-    check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto print_one_method_from_stack_cleanup;
b35d98
 
b35d98
     if (error_code != JVMTI_ERROR_NONE)
b35d98
     {
b35d98
@@ -1885,11 +1895,11 @@ static void print_one_method_from_stack(
b35d98
     }
b35d98
     char *updated_class_name = format_class_name_for_JNI_call(declaring_class_name);
b35d98
     int line_number = get_line_number(jvmti_env, stack_frame.method, stack_frame.location);
b35d98
-    char *source_file_name;
b35d98
     if (declaring_class != NULL)
b35d98
     {
b35d98
         error_code = (*jvmti_env)->GetSourceFileName(jvmti_env, declaring_class, &source_file_name);
b35d98
-        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
+        if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+            goto print_one_method_from_stack_cleanup;
b35d98
     }
b35d98
 
b35d98
     char buf[1000];
b35d98
@@ -1919,17 +1929,23 @@ static void print_one_method_from_stack(
b35d98
     }
b35d98
 #endif
b35d98
 
b35d98
+print_one_method_from_stack_cleanup:
b35d98
     /* cleanup */
b35d98
-    if (method_name != NULL)
b35d98
+    if (NULL != method_name)
b35d98
     {
b35d98
         error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)method_name);
b35d98
         check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
     }
b35d98
-    if (declaring_class_name != NULL)
b35d98
+    if (NULL != declaring_class_name)
b35d98
     {
b35d98
         error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)declaring_class_name);
b35d98
         check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
     }
b35d98
+    if (NULL != source_file_name)
b35d98
+    {
b35d98
+        error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)source_file_name);
b35d98
+        check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
+    }
b35d98
 }
b35d98
 #endif /* GENERATE_JVMTI_STACK_TRACE */
b35d98
 
b35d98
@@ -1950,7 +1966,7 @@ static char *generate_stack_trace(
b35d98
 
b35d98
     char  *stack_trace_str;
b35d98
     char  buf[1000];
b35d98
-    int count;
b35d98
+    int count = -1;
b35d98
     int i;
b35d98
 
b35d98
     /* allocate string which will contain stack trace */
b35d98
@@ -1963,12 +1979,10 @@ static char *generate_stack_trace(
b35d98
 
b35d98
     /* get stack trace */
b35d98
     error_code = (*jvmti_env)->GetStackTrace(jvmti_env, thread, 0, MAX_STACK_TRACE_DEPTH, stack_frames, &count);
b35d98
-    check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__));
b35d98
-
b35d98
     VERBOSE_PRINT("Number of records filled: %d\n", count);
b35d98
-
b35d98
-    /* is stack trace empty? */
b35d98
-    if (count < 1)
b35d98
+    /* error or is stack trace empty? */
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__))
b35d98
+            || count < 1)
b35d98
     {
b35d98
         free(stack_trace_str);
b35d98
         return NULL;
b35d98
@@ -2011,12 +2025,12 @@ static void JNICALL callback_on_exception(
b35d98
 {
b35d98
     jvmtiError error_code;
b35d98
 
b35d98
-    char *method_name_ptr;
b35d98
-    char *method_signature_ptr;
b35d98
-    char *class_name_ptr;
b35d98
-    char *class_signature_ptr;
b35d98
-    char *exception_name_ptr;
b35d98
-    char *updated_exception_name_ptr;
b35d98
+    char *method_name_ptr = NULL;
b35d98
+    char *method_signature_ptr = NULL;
b35d98
+    char *class_name_ptr = NULL;
b35d98
+    char *class_signature_ptr = NULL;
b35d98
+    char *exception_name_ptr = NULL;
b35d98
+    char *updated_exception_name_ptr = NULL;
b35d98
 
b35d98
     jclass method_class;
b35d98
     jclass exception_class;
b35d98
@@ -2030,10 +2044,21 @@ static void JNICALL callback_on_exception(
b35d98
     exception_class = (*jni_env)->GetObjectClass(jni_env, exception_object);
b35d98
 
b35d98
     /* retrieve all required informations */
b35d98
-    (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
b35d98
-    (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &method_class);
b35d98
-    (*jvmti_env)->GetClassSignature(jvmti_env, method_class, &class_signature_ptr, NULL);
b35d98
-    (*jvmti_env)->GetClassSignature(jvmti_env, exception_class, &exception_name_ptr, NULL);
b35d98
+    error_code = (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &method_class);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, method_class, &class_signature_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, exception_class, &exception_name_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_cleanup;
b35d98
 
b35d98
     /* readable class names */
b35d98
     class_name_ptr = format_class_name(class_signature_ptr, '.');
b35d98
@@ -2090,6 +2115,7 @@ static void JNICALL callback_on_exception(
b35d98
         }
b35d98
     }
b35d98
 
b35d98
+callback_on_exception_cleanup:
b35d98
     /* cleapup */
b35d98
     if (method_name_ptr != NULL)
b35d98
     {
b35d98
@@ -2131,9 +2157,9 @@ static void JNICALL callback_on_exception_catch(
b35d98
 {
b35d98
     jvmtiError error_code;
b35d98
 
b35d98
-    char *method_name_ptr;
b35d98
-    char *method_signature_ptr;
b35d98
-    char *class_signature_ptr;
b35d98
+    char *method_name_ptr = NULL;
b35d98
+    char *method_signature_ptr = NULL;
b35d98
+    char *class_signature_ptr = NULL;
b35d98
 
b35d98
     jclass class;
b35d98
 
b35d98
@@ -2141,9 +2167,17 @@ static void JNICALL callback_on_exception_catch(
b35d98
     enter_critical_section(jvmti_env, shared_lock);
b35d98
 
b35d98
     /* retrieve all required informations */
b35d98
-    (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
b35d98
-    (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &class);
b35d98
-    (*jvmti_env)->GetClassSignature(jvmti_env, class, &class_signature_ptr, NULL);
b35d98
+    error_code = (*jvmti_env)->GetMethodName(jvmti_env, method, &method_name_ptr, &method_signature_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_catch_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &class);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_catch_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, class, &class_signature_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        goto callback_on_exception_catch_cleanup;
b35d98
 
b35d98
 #ifdef VERBOSE
b35d98
     /* readable class name */
b35d98
@@ -2152,6 +2186,7 @@ static void JNICALL callback_on_exception_catch(
b35d98
 
b35d98
     VERBOSE_PRINT("An exception was caught in a method %s%s() with signature %s\n", class_name_ptr, method_name_ptr, method_signature_ptr);
b35d98
 
b35d98
+callback_on_exception_catch_cleanup:
b35d98
     /* cleapup */
b35d98
     if (method_name_ptr != NULL)
b35d98
     {
b35d98
@@ -2187,15 +2222,18 @@ static void JNICALL callback_on_object_alloc(
b35d98
             jclass object_klass,
b35d98
             jlong size)
b35d98
 {
b35d98
-    char *signature_ptr;
b35d98
+    char *signature_ptr = NULL;
b35d98
 
b35d98
     enter_critical_section(jvmti_env, shared_lock);
b35d98
-    (*jvmti_env)->GetClassSignature(jvmti_env, object_klass, &signature_ptr, NULL);
b35d98
+    jvmtiError error_code = (*jvmti_env)->GetClassSignature(jvmti_env, object_klass, &signature_ptr, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+        return;
b35d98
 
b35d98
     if (size >= VM_MEMORY_ALLOCATION_THRESHOLD)
b35d98
     {
b35d98
         INFO_PRINT("object allocation: instance of class %s, allocated %ld bytes\n", signature_ptr, (long int)size);
b35d98
     }
b35d98
+
b35d98
     (*jvmti_env)->Deallocate(jvmti_env, (unsigned char *)signature_ptr);
b35d98
     exit_critical_section(jvmti_env, shared_lock);
b35d98
 }
b35d98
@@ -2281,17 +2319,23 @@ static void JNICALL callback_on_compiled_method_load(
b35d98
     enter_critical_section(jvmti_env, shared_lock);
b35d98
 
b35d98
     error_code = (*jvmti_env)->GetMethodName(jvmti_env, method, &name, &signature, &generic_ptr);
b35d98
-    check_jvmti_error(jvmti_env, error_code, "get method name");
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, "get method name"))
b35d98
+        goto callback_on_compiled_method_load_cleanup;
b35d98
 
b35d98
     error_code = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &class);
b35d98
-    check_jvmti_error(jvmti_env, error_code, "get method declaring class");
b35d98
-    (*jvmti_env)->GetClassSignature(jvmti_env, class, &class_signature, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, "get method declaring class"))
b35d98
+        goto callback_on_compiled_method_load_cleanup;
b35d98
+
b35d98
+    error_code = (*jvmti_env)->GetClassSignature(jvmti_env, class, &class_signature, NULL);
b35d98
+    if (check_jvmti_error(jvmti_env, error_code, "get method name"))
b35d98
+        goto callback_on_compiled_method_load_cleanup;
b35d98
 
b35d98
     INFO_PRINT("Compiling method: %s.%s with signature %s %s   Code size: %5d\n",
b35d98
         class_signature == NULL ? "" : class_signature,
b35d98
         name, signature,
b35d98
         generic_ptr == NULL ? "" : generic_ptr, (int)code_size);
b35d98
 
b35d98
+callback_on_compiled_method_load_cleanup:
b35d98
     if (name != NULL)
b35d98
     {
b35d98
         error_code = (*jvmti_env)->Deallocate(jvmti_env, (unsigned char*)name);
b35d98
@@ -2533,11 +2577,13 @@ jvmtiError print_jvmti_version(jvmtiEnv *jvmti_env __UNUSED_VAR)
b35d98
     jint cmajor, cminor, cmicro;
b35d98
 
b35d98
     error_code = (*jvmti_env)->GetVersionNumber(jvmti_env, &version);
b35d98
-
b35d98
-    cmajor = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
b35d98
-    cminor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
b35d98
-    cmicro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
b35d98
-    printf("Compile Time JVMTI Version: %d.%d.%d (0x%08x)\n", cmajor, cminor, cmicro, version);
b35d98
+    if (!check_jvmti_error(jvmti_env, error_code, __FILE__ ":" STRINGIZE(__LINE__)))
b35d98
+    {
b35d98
+        cmajor = (version & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
b35d98
+        cminor = (version & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
b35d98
+        cmicro = (version & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
b35d98
+        printf("Compile Time JVMTI Version: %d.%d.%d (0x%08x)\n", cmajor, cminor, cmicro, version);
b35d98
+    }
b35d98
 
b35d98
     return error_code;
b35d98
 #else
b35d98
-- 
b35d98
1.8.3.1
b35d98