You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/07/03 09:35:30 UTC

svn commit: r790815 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ native/include/ native/shared/

Author: mturk
Date: Fri Jul  3 07:35:30 2009
New Revision: 790815

URL: http://svn.apache.org/viewvc?rev=790815&view=rev
Log:
Guard agains realloc on non allocated memory

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
    commons/sandbox/runtime/trunk/src/main/native/shared/error.c
    commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
    commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java Fri Jul  3 07:35:30 2009
@@ -23,7 +23,8 @@
  *
  * @since Runtime 1.0
  */
-public interface DirectBuffer extends Pointer {
+public interface DirectBuffer extends Pointer
+{
 
 
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Fri Jul  3 07:35:30 2009
@@ -84,18 +84,17 @@
      * a {code Pointer} can be explained as this:
      * <pre>
      *
-     * void *pointer;
-     *
      * int method(void **pointer, int size)
      * {
      *     *pointer = malloc(size + something);
      *     return something;
      * }
      *
-     *     ...
-     *
-     *     free(pointer);
-     *
+     * void *pointer;
+     * method(&pointer, 16);
+     * ...
+     * free(pointer);
+     * 
      * </pre>
      * The example code uses {@code by-reference} concept to fill in
      * the provided {@code pointer} with the value unknown at the time
@@ -160,7 +159,7 @@
         throws OutOfMemoryError, IllegalArgumentException;
 
     private static native void realloc0(Pointer ptr, long size)
-        throws OutOfMemoryError;
+        throws OutOfMemoryError, RuntimeException
     /**
      * Change the size of memory block pointed by {@code ptr}.
      *
@@ -169,9 +168,10 @@
      *
      * @throws OutOfMemoryError if memory cannot be allocated.
      * @throws IllegalArgumentException if the size is less then {@code 1}.
+     * @throws RuntimeException if the {@src} point to an invalid address.
      */
     public static void realloc(Pointer ptr, long size)
-        throws OutOfMemoryError, IllegalArgumentException
+        throws OutOfMemoryError, IllegalArgumentException, RuntimeException
     {
         if (size < 1L)
             throw new IllegalArgumentException();

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_error.h Fri Jul  3 07:35:30 2009
@@ -274,7 +274,7 @@
  *    into one of the error/status ranges below -- except for
  *    ACR_OS_START_USERERR, which see.
  */
-#define ACR_OS_ERRSPACE_SIZE     50000
+#define ACR_OS_ERRSPACE_SIZE       50000
 /**
  * ACR_UTIL_ERRSPACE_SIZE is the size of the space that is reserved for
  * use within apr-util. This space is reserved above that used by ACR
@@ -497,6 +497,7 @@
 #define ACR_EINVALSIZ       (ACR_OS_START_ERROR + 101)
 #define ACR_ERANGE          (ACR_OS_START_ERROR + 102)
 #define ACR_ECLASSNOTFOUND  (ACR_OS_START_ERROR + 103)
+#define ACR_EFAULT          (ACR_OS_START_ERROR + 104)
 
 /**
  * @defgroup ACR_STATUS_IS Status Value Tests
@@ -1221,3 +1222,4 @@
 #endif
 
 #endif /* _ACR_ERROR_H */
+

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h Fri Jul  3 07:35:30 2009
@@ -85,13 +85,22 @@
                                 size_t len);
 
 /**
+ * Get the native pointer callback handler function pointer.
+ * @param env Current JNI environment
+ * @param ptr Java Pointer object use.
+ * @return Callback pointer.
+ */
+ACR_DECLARE(acr_pointer_cleanup_fn_t *) ACR_PointerCallbackGet(JNIEnv *env,
+ 															   jobject ptr);
+
+/**
  * Create new DirectBuffer class instance
  * @param env Current JNI environment
  * @param p Native pointer to wrap into Pointer class
  * @param len Length of the pointer.
  * @param cb callback function to use on Pointer.finalize()
  */
-ACR_DECLARE(jobject) ACR_DirectBufferCreate(JNIEnv *_E, void *p, size_t len,
+ACR_DECLARE(jobject) ACR_DirectBufferCreate(JNIEnv *env, void *p, size_t len,
                                             acr_pointer_cleanup_fn_t *cb);
 
 #ifdef __cplusplus

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Fri Jul  3 07:35:30 2009
@@ -255,6 +255,7 @@
         return "The process is not recognized.";
     case ACR_EGENERAL:
         return "Internal error";
+    /* ACR specific error codes */
     case ACR_EISNULL:
         return "The given argument is null";
     case ACR_EINVALSIZ:
@@ -263,6 +264,8 @@
         return "The given argument is out of range";
     case ACR_ECLASSNOTFOUND:
         return "The specified Java Class was not found";
+    case ACR_EFAULT:
+        return "Bad address";
     default:
         return "Error string not specified yet";
     }
@@ -535,3 +538,4 @@
 
     (*_E)->SetIntArrayRegion(_E, ra, 0, 32, &i[0]);
 }
+

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Fri Jul  3 07:35:30 2009
@@ -302,12 +302,19 @@
                                                jobject src,
                                                jlong siz)
 {
+	acr_pointer_cleanup_fn_t *cb;
     void   *np;
     size_t  ss = (size_t)siz;
     void   *op = ACR_PointerGet(_E, src, NULL);
 
     UNREFERENCED_O;
-
+	cb = ACR_PointerCallbackGet(_E, src);
+	if (cb != memory_pointer_cleanup) {
+	    /* Trying to realloc on something we didn't alloc first.
+	     */
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
+		return;	
+	}
     np = ACR_Realloc(_E, THROW_NMARK, op, ss);
     if (!np) {
         return;
@@ -373,7 +380,7 @@
         memcpy(dp, sp + so, ss);
         po = ACR_PointerCreate(_E, dp, ss, memory_pointer_cleanup);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
     if (!po) {
         /* Destroy the the memory we failed to attach
@@ -413,7 +420,7 @@
     ACR_TRY {
         memcpy(d + dn, s + sn, cs);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 
@@ -446,7 +453,7 @@
     ACR_TRY {
         memmove(d + dn, s + sn, cs);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 
@@ -474,7 +481,7 @@
     ACR_TRY {
         memset(d + dn, 0, cs);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 
@@ -502,7 +509,7 @@
     ACR_TRY {
         memset(d + dn, c, cs);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=790815&r1=790814&r2=790815&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Fri Jul  3 07:35:30 2009
@@ -196,7 +196,7 @@
     ACR_TRY {
         memcpy(N2P(d, void *), N2P(s, const void *), (size_t)l);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 
@@ -207,7 +207,7 @@
     ACR_TRY {
         memmove(N2P(d, void *), N2P(s, const void *), (size_t)l);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
     }
 }
 
@@ -218,7 +218,7 @@
     ACR_TRY {
         return memcmp(N2P(a, void *), N2P(b, const void *), (size_t)n);
     } ACR_CATCH() {
-        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, ACR_EFAULT);
         return -1;
     }
 }
@@ -302,3 +302,14 @@
     }
 }
 
+ACR_DECLARE(acr_pointer_cleanup_fn_t *) ACR_PointerCallbackGet(ACR_JNISTDARGS)
+{
+    if (_clazzn.i && J4MID(0000) && _O) {
+        return GET_IFIELD_V(0001, _O, acr_pointer_cleanup_fn_t *);
+    }
+    else {
+        ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND);
+        return NULL;
+    }
+}
+