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

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