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

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