Author: mturk Date: Thu Jun 18 16:16:01 2009 New Revision: 786143 URL: http://svn.apache.org/viewvc?rev=786143&view=rev Log: Do argument check in Java code. Also synchronize access to Pointers when doing memory operations
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java commons/sandbox/runtime/trunk/src/main/native/shared/memory.c 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=786143&r1=786142&r2=786143&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 Thu Jun 18 16:16:01 2009 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.commons.runtime; /** @@ -95,7 +94,6 @@ return p; } - /** * Allocates {...@code size} bytes and returns a {...@link Pointer} * to the allocated memory. The memory is not cleared. @@ -122,6 +120,8 @@ public static native Pointer calloc(long size) throws OutOfMemoryError, IllegalArgumentException; + public static native void realloc0(Pointer ptr, long size) + throws OutOfMemoryError; /** * Change the size of memory block pointed by {...@code ptr}. * @@ -131,9 +131,18 @@ * @throws OutOfMemoryError if memory cannot be allocated. * @throws IllegalArgumentException if the size is less then {...@code 1}. */ - public static native void realloc(Pointer ptr, long size) - throws OutOfMemoryError, IllegalArgumentException; + public static void realloc(Pointer ptr, long size) + throws OutOfMemoryError, IllegalArgumentException + { + if (size < 1L) + throw new IllegalArgumentException(); + synchronized (ptr) { + realloc0(ptr, size); + } + } + public static native Pointer slice0(Pointer src, long offset, long size) + throws IndexOutOfBoundsException; /** * Creates a new {...@link Pointer} object whose content is a shared * subsequence of a {...@code src} memory address. @@ -149,10 +158,22 @@ * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code src} is {...@code null}. */ - public static native Pointer slice(Pointer src, long offset, long size) + public static Pointer slice(Pointer src, long offset, long size) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException + { + if (src == null || src.IsNull()) + throw new NullPointerException(); + if (offset < 0L || size < 1L) + throw new IllegalArgumentException(); + synchronized (src) { + return slice0(src, offset, size); + } + } + public static native Pointer dup0(Pointer src, long offset, long size) + throws IndexOutOfBoundsException, OutOfMemoryError, + RuntimeException; /** * Creates a new {...@link Pointer} object whose content is a duplicated * subsequence of a {...@code src} memory address. @@ -167,11 +188,27 @@ * @throws IndexOutOfBoundsException if the operation would cause * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code src} is {...@code null}. + * @throws RuntimeException if memory is corrupted. + * @throws OutOfMemoryError if memory cannot be dipicated. */ - public static native Pointer dup(Pointer src, long offset, long size) + public static Pointer dup(Pointer src, long offset, long size) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException, RuntimeException, OutOfMemoryError + { + if (src == null || src.IsNull()) + throw new NullPointerException(); + if (offset < 0L || size < 1L) + throw new IllegalArgumentException(); + + synchronized (src) { + return dup0(src, offset, size); + } + } + public static native void copy0(Pointer src, long srcPos, Pointer dst, + long dstPos, long length) + throws IndexOutOfBoundsException, IllegalArgumentException, + RuntimeException; /** * Copy the memory area from {...@code src} to {...@code dst}. * <p> @@ -193,12 +230,33 @@ * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code src} or {...@code dst} is * {...@code null}. + * @throws RuntimeException if memory is corrupted. */ - public static native void copy(Pointer src, long srcPos, Pointer dst, - long dstPos, long length) + public static void copy(Pointer src, long srcPos, Pointer dst, + long dstPos, long length) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException, RuntimeException + { + if (src == null || src.IsNull()) + throw new NullPointerException(); + if (dst == null || dst.IsNull()) + throw new NullPointerException(); + if (srcPos < 0L || dstPos < 0L) + throw new IllegalArgumentException(); + if (length < 1L) + throw new IllegalArgumentException(); + + synchronized (src) { + synchronized (dst) { + copy0(src, srcPos, dst, dstPos, length); + } + } + } + public static native void move0(Pointer src, long srcPos, Pointer dst, + long dstPos, long length) + throws IndexOutOfBoundsException, IllegalArgumentException, + RuntimeException; /** * Copy the memory area from {...@code src} to {...@code dst}. * <p> @@ -219,12 +277,31 @@ * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code src} or {...@code dst} is * {...@code null}. + * @throws RuntimeException if memory is corrupted. */ - public static native void move(Pointer src, long srcPos, Pointer dst, - long dstPos, long length) + public static void move(Pointer src, long srcPos, Pointer dst, + long dstPos, long length) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException, RuntimeException + { + if (src == null || src.IsNull()) + throw new NullPointerException(); + if (dst == null || dst.IsNull()) + throw new NullPointerException(); + if (srcPos < 0L || dstPos < 0L) + throw new IllegalArgumentException(); + if (length < 1L) + throw new IllegalArgumentException(); + + synchronized (src) { + synchronized (dst) { + move0(src, srcPos, dst, dstPos, length); + } + } + } + public static native void clear0(Pointer ptr, long offset, long length) + throws IndexOutOfBoundsException, RuntimeException; /** * Set the {...@code ptr} memory area from {...@code src} to {...@code zero}. * @@ -238,11 +315,24 @@ * @throws IndexOutOfBoundsException if the operation would cause * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws RuntimeException if memory is corrupted. */ - public static native void clear(Pointer ptr, long offset, long length) + public static void clear(Pointer ptr, long offset, long length) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException, RuntimeException + { + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1L) + throw new IllegalArgumentException(); + + synchronized (ptr) { + clear0(ptr, offset, length); + } + } + public static native void set0(Pointer ptr, long offset, long length, int val) + throws IndexOutOfBoundsException, RuntimeException; /** * Set the {...@code ptr} memory area from {...@code src} to {...@code val}. * @@ -257,14 +347,24 @@ * @throws IndexOutOfBoundsException if the operation would cause * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws RuntimeException if memory is corrupted. */ - public static native void set(Pointer ptr, long offset, long length, int val) + public static void set(Pointer ptr, long offset, long length, int val) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + NullPointerException, RuntimeException + { + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1L) + throw new IllegalArgumentException(); + + synchronized (ptr) { + set0(ptr, offset, length, val); + } + } private static native byte[] array0(Pointer ptr, long offset, int length) - throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException; + throws IndexOutOfBoundsException, OutOfMemoryError; /** * Return the {...@code ptr} memory area from {...@code src} to {...@code byte} array. * @@ -279,12 +379,19 @@ * @throws IndexOutOfBoundsException if the operation would cause * access of data outside allocated memory bounds. * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws OutOfMemoryError if memory cannot be dipicated. */ public static byte[] array(Pointer ptr, long offset, int length) throws IndexOutOfBoundsException, IllegalArgumentException, - NullPointerException + NullPointerException, OutOfMemoryError { - return array0(ptr, offset, length); + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1L) + throw new IllegalArgumentException(); + + synchronized (ptr) { + return array0(ptr, offset, length); + } } - } 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=786143&r1=786142&r2=786143&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Thu Jun 18 16:16:01 2009 @@ -298,9 +298,9 @@ return ptr; } -ACR_JNI_EXPORT_DECLARE(void, Memory, realloc)(ACR_JNISTDARGS, - jobject src, - jlong siz) +ACR_JNI_EXPORT_DECLARE(void, Memory, realloc0)(ACR_JNISTDARGS, + jobject src, + jlong siz) { void *np; size_t ss = (size_t)siz; @@ -308,10 +308,6 @@ UNREFERENCED_O; - if (siz < 1L) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); - return; - } np = ACR_Realloc(_E, THROW_NMARK, op, ss); if (!np) { return; @@ -319,9 +315,9 @@ ACR_PointerSet(_E, src, np, ss); } -ACR_JNI_EXPORT_DECLARE(jobject, Memory, slice)(ACR_JNISTDARGS, - jobject src, jlong off, - jlong siz) +ACR_JNI_EXPORT_DECLARE(jobject, Memory, slice0)(ACR_JNISTDARGS, + jobject src, jlong off, + jlong siz) { size_t so = (size_t)off; size_t ss = (size_t)siz; @@ -330,14 +326,6 @@ UNREFERENCED_O; - if (!sp) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); - return NULL; - } - if (off < 0L || siz < 1L) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); - return NULL; - } if ((so + ss) > sl) { ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return NULL; @@ -349,9 +337,9 @@ return ACR_PointerCreate(_E, sp + so, ss, NULL); } -ACR_JNI_EXPORT_DECLARE(jobject, Memory, dup)(ACR_JNISTDARGS, - jobject src, jlong off, - jlong siz) +ACR_JNI_EXPORT_DECLARE(jobject, Memory, dup0)(ACR_JNISTDARGS, + jobject src, jlong off, + jlong siz) { jobject po = NULL; size_t so = (size_t)off; @@ -362,14 +350,6 @@ UNREFERENCED_O; - if (!sp) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); - return NULL; - } - if (off < 0L || siz < 1L) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); - return NULL; - } if ((so + ss) > sl) { ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return NULL; @@ -396,9 +376,9 @@ return po; } -ACR_JNI_EXPORT_DECLARE(void, Memory, copy)(ACR_JNISTDARGS, jobject src, - jlong soff, jobject dst, - jlong doff, jlong size) +ACR_JNI_EXPORT_DECLARE(void, Memory, copy0)(ACR_JNISTDARGS, jobject src, + jlong soff, jobject dst, + jlong doff, jlong size) { size_t dl; size_t sl; @@ -407,14 +387,7 @@ size_t cs = (size_t)size; char *d = (char *)ACR_PointerGet(_E, dst, &dl); char *s = (char *)ACR_PointerGet(_E, src, &sl); - if (!d || !s) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); - return; - } - if (doff < 0L || soff < 0L || size < 1L) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); - return; - } + if ((dn + cs) > dl) { ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return; @@ -432,9 +405,9 @@ } } -ACR_JNI_EXPORT_DECLARE(void, Memory, move)(ACR_JNISTDARGS, jobject src, - jlong soff, jobject dst, - jlong doff, jlong size) +ACR_JNI_EXPORT_DECLARE(void, Memory, move0)(ACR_JNISTDARGS, jobject src, + jlong soff, jobject dst, + jlong doff, jlong size) { size_t dl; size_t sl; @@ -444,14 +417,6 @@ char *d = (char *)ACR_PointerGet(_E, dst, &dl); char *s = (char *)ACR_PointerGet(_E, src, &sl); - if (!d || !s) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); - return; - } - if (doff < 0L || soff < 0L || size < 1L) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); - return; - } if ((dn + cs) > dl) { ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return; @@ -469,8 +434,8 @@ } } -ACR_JNI_EXPORT_DECLARE(void, Memory, clear)(ACR_JNISTDARGS, jobject dst, - jlong doff, jlong size) +ACR_JNI_EXPORT_DECLARE(void, Memory, clear0)(ACR_JNISTDARGS, jobject dst, + jlong doff, jlong size) { size_t dl; size_t dn = (size_t)doff; @@ -497,8 +462,8 @@ } } -ACR_JNI_EXPORT_DECLARE(void, Memory, set)(ACR_JNISTDARGS, jobject dst, - jlong doff, jlong size, jint c) +ACR_JNI_EXPORT_DECLARE(void, Memory, set0)(ACR_JNISTDARGS, jobject dst, + jlong doff, jlong size, jint c) { size_t dl; size_t dn = (size_t)doff; @@ -534,10 +499,6 @@ size_t cs = (size_t)size; jbyte *p = (jbyte *)ACR_PointerGet(_E, ptr, &pl); - if (!p) { - ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); - return NULL; - } if ((poff + cs) > pl) { ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return NULL;