commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1100333 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr/jniapi.h include/acr/jnimacros.h shared/array.c shared/clazz.c shared/netaddr.c shared/string.c shared/unsafe.c
Date Fri, 06 May 2011 18:59:16 GMT
Author: mturk
Date: Fri May  6 18:59:16 2011
New Revision: 1100333

URL: http://svn.apache.org/viewvc?rev=1100333&view=rev
Log:
Use safer version of JNI critical arrays

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h
    commons/sandbox/runtime/trunk/src/main/native/shared/array.c
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
    commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c
    commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jniapi.h Fri May  6 18:59:16
2011
@@ -30,6 +30,20 @@ JNIEnv *AcrGetJNIEnv(void);
 void    AcrSetThreadError(int);
 int     AcrGetThreadError(void);
 
+ACR_INLINE(void *) AcrGetArrayCritical(JNI_STDENV, jarray array)
+{
+    if (array == 0)
+        return 0;
+    else
+        return (*env)->GetPrimitiveArrayCritical(env, array, 0);
+}
+
+ACR_INLINE(void)   AcrReleaseArrayCritical(JNI_STDENV, jarray array, void *carray)
+{
+    if (array != 0)
+        (*env)->ReleasePrimitiveArrayCritical(env, array, carray, 0);
+}
+
 #ifdef __cplusplus
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jnimacros.h Fri May  6 18:59:16
2011
@@ -220,4 +220,10 @@
         (*env)->DeleteLocalRef(env, _str);                  \
     } else (void)(0)
 
+#define JARRAY_CRITICAL(T, A) \
+    (T *)AcrGetArrayCritical(env, (A))
+
+#define RELEASE_CRITICAL(A, C) \
+    AcrReleaseArrayCritical(env, (A), (C))
+
 #endif /* _ACR_JNIDEFS_H_ */

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/array.c?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Fri May  6 18:59:16 2011
@@ -15,6 +15,7 @@
  */
 
 #include "acr/memory.h"
+#include "acr/jniapi.h"
 #include "acr/clazz.h"
 
 /**
@@ -45,18 +46,17 @@ ACR_UTIL_EXPORT(jboolean, Array, memcpy0
         (nbytes > ((dstLen * dstSiz) - dstOff)))
         return JNI_FALSE;
 
-    scp = (*env)->GetPrimitiveArrayCritical(env, src, 0);
-    if (scp == 0)
-        return JNI_FALSE;
-    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
-    if (dcp == 0) {
-        (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+    scp = JARRAY_CRITICAL(jbyte, src);
+    dcp = JARRAY_CRITICAL(jbyte, dst);
+    if (dcp == 0 || scp == 0) {
+        RELEASE_CRITICAL(src, scp);
+        RELEASE_CRITICAL(dst, dcp);
         return JNI_FALSE;
     }
     memmove(dcp + (size_t)dstOff,
             scp + (size_t)srcOff, (size_t)nbytes);
-    (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
-    (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+    RELEASE_CRITICAL(src, scp);
+    RELEASE_CRITICAL(dst, dcp);
     return JNI_TRUE;
 }
 
@@ -105,10 +105,10 @@ ACR_UTIL_EXPORT(jboolean, Array, memset0
     if (nbytes > ((dstLen * dstSiz) - dstOff))
         return JNI_FALSE;
 
-    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
+    dcp = JARRAY_CRITICAL(jbyte, dst);
     if (dcp != 0) {
         memset(dcp + (size_t)dstOff, number, (size_t)nbytes);
-        (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+        RELEASE_CRITICAL(dst, dcp);
         return JNI_TRUE;
     }
     else
@@ -142,17 +142,16 @@ ACR_UTIL_EXPORT(jint, Array, memcmp0)(JN
         return -1;
     }
 #endif
-    scp = (*env)->GetPrimitiveArrayCritical(env, src, 0);
-    if (scp == 0)
-        return -1;
-    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
-    if (dcp == 0) {
-        (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+    scp = JARRAY_CRITICAL(jbyte, src);
+    dcp = JARRAY_CRITICAL(jbyte, dst);
+    if (dcp == 0 || scp == 0) {
+        RELEASE_CRITICAL(src, scp);
+        RELEASE_CRITICAL(dst, dcp);
         return -1;
     }
     rv = memcmp(dcp + (size_t)dstOff,
                 scp + (size_t)srcOff, (size_t)nbytes);
-    (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
-    (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+    RELEASE_CRITICAL(src, scp);
+    RELEASE_CRITICAL(dst, dcp);
     return rv;
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Fri May  6 18:59:16 2011
@@ -402,3 +402,4 @@ AcrClassGetName(JNI_STDARGS, int simple)
     }
     return name;
 }
+

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/netaddr.c Fri May  6 18:59:16 2011
@@ -20,6 +20,7 @@
  */
 
 #include "acr/netapi.h"
+#include "acr/jniapi.h"
 #include "acr/memory.h"
 #include "acr/string.h"
 #include "acr/port.h"
@@ -754,10 +755,10 @@ cleanup:
     return ba;
 }
 
-#define JBA2SA(BA) \
-    (acr_sockaddr_t *)(*env)->GetPrimitiveArrayCritical(env, (BA), 0)
-#define JSA_RP(BA, SA) \
-    (*env)->ReleasePrimitiveArrayCritical(env, (BA), (SA), 0)
+#define SOCKADDR_CAST(BA) \
+    (acr_sockaddr_t *)AcrGetArrayCritical(env, (BA))
+#define SOCKADDR_RELEASE(BA, SA) \
+    AcrReleaseArrayCritical(env, (BA), (SA))
 
 ACR_NET_EXPORT(jint, EndpointAddress, init0)(JNI_STDARGS)
 {
@@ -767,7 +768,7 @@ ACR_NET_EXPORT(jint, EndpointAddress, in
 ACR_NET_EXPORT(jint, EndpointAddress, family0)(JNI_STDARGS, jbyteArray a)
 {
     jint rv = -1;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         switch (sa->family) {
@@ -782,7 +783,7 @@ ACR_NET_EXPORT(jint, EndpointAddress, fa
             break;
         }
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
@@ -790,7 +791,7 @@ ACR_NET_EXPORT(jstring, EndpointAddress,
 {
     char buf[256];
     const char *name = "(null)";
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         if (sa->hostname[0] == '\0') {
@@ -802,7 +803,7 @@ ACR_NET_EXPORT(jstring, EndpointAddress,
         else
             name = sa->hostname;
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return AcrNewJavaStringA(env, name);
 }
 
@@ -811,8 +812,8 @@ ACR_NET_EXPORT(jboolean, EndpointAddress
                                                    jbyteArray sa2)
 {
     jboolean rv = JNI_FALSE;
-    acr_sockaddr_t *addr1 = JBA2SA(sa1);
-    acr_sockaddr_t *addr2 = JBA2SA(sa2);
+    acr_sockaddr_t *addr1 = SOCKADDR_CAST(sa1);
+    acr_sockaddr_t *addr2 = SOCKADDR_CAST(sa2);
 
     if (addr1 != 0 && addr2 != 0) {
         if (addr1->iplen == addr2->iplen &&
@@ -823,8 +824,8 @@ ACR_NET_EXPORT(jboolean, EndpointAddress
         else if (V4MAPPED_EQUAL(addr2, addr1))
             rv = JNI_TRUE;
     }
-    JSA_RP(sa1, addr1);
-    JSA_RP(sa2, addr2);
+    SOCKADDR_RELEASE(sa1, addr1);
+    SOCKADDR_RELEASE(sa2, addr2);
     return rv;
 }
 
@@ -833,7 +834,7 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
     jbyteArray     ba;
     acr_sockaddr_t sa;
     char *np;
-    int   rc = 0;
+    int   rc = ACR_EINVAL;
 
     memset(&sa, 0, sizeof(acr_sockaddr_t));
     WITH_CSTR(hostname) {
@@ -857,10 +858,8 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
         sa.addrlen = ISIZEOF(sa.sa.unx.sun_path);
         sa.ipaddr  = &(sa.sa.unx.sun_path);
         sa.iplen   = sa.addrlen;
+        rc = 0;
 #elif defined(WINDOWS)
-        sa = calloc(1, sizeof(acr_sockaddr_t));
-        if (sa == 0)
-            return ACR_ENOMEM;
         strlcpy(sa.hostname, "\\\\.\\pipe\\", sizeof(sa.hostname));
         strlcat(sa.hostname, J2S(hostname), sizeof(sa.hostname));
         np = sa.hostname + 9;
@@ -873,6 +872,7 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
         sa.addrlen = ISIZEOF(sa.hostname);
         sa.ipaddr  = &(sa.hostname);
         sa.iplen   = sa.addrlen;
+        rc = 0;
 #else
         rc = ACR_ENOTIMPL;
 #endif
@@ -891,7 +891,7 @@ ACR_NET_EXPORT(jbyteArray, LocalEndpoint
 ACR_NET_EXPORT(jstring, SocketAddress, hostname0)(JNI_STDARGS, jbyteArray a)
 {
     jstring rv = 0;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         if (sa->hostname != '\0')
@@ -902,7 +902,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, h
                 rv = AcrNewJavaStringA(env, buf);
         }
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
@@ -911,7 +911,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, h
     const char *host = 0;
     int   rc;
     jstring rv = 0;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         rc = AcrGetNameInfo(&host, sa, 0);
@@ -923,7 +923,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, h
                 rv = AcrNewJavaStringA(env, buf);
         }
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
@@ -932,7 +932,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, s
     const char *serv = 0;
     int   rc;
     jstring rv = 0;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         if (sa->servname != '\0')
@@ -943,7 +943,7 @@ ACR_NET_EXPORT(jstring, SocketAddress, s
                 rv = AcrNewJavaStringA(env, serv);
         }
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
@@ -951,24 +951,24 @@ ACR_NET_EXPORT(jstring, SocketAddress, i
 {
     char buf[256];
     jstring rv = 0;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0) {
         if (AcrGetSockaddrIp(buf, 256, sa) == 0)
             rv = AcrNewJavaStringA(env, buf);
     }
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
 ACR_NET_EXPORT(jint, SocketAddress, port0)(JNI_STDARGS,jbyteArray a)
 {
     jint rv = 0;
-    acr_sockaddr_t *sa = JBA2SA(a);
+    acr_sockaddr_t *sa = SOCKADDR_CAST(a);
 
     if (sa != 0)
         rv = sa->port;
-    JSA_RP(a, sa);
+    SOCKADDR_RELEASE(a, sa);
     return rv;
 }
 
@@ -988,19 +988,20 @@ ACR_NET_EXPORT(jbyteArray, SocketAddress
             ffamily = AF_INET6;
         break;
         case 3:
-            ffamily = AF_LOCAL;
+            rc = ACR_EINVAL;
         break;
     }
     /* TODO: Move the param checks to java code
      */
     if ((masked = flags & (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK))) {
         if (hostname == 0 || family != AF_UNSPEC ||
-            masked == (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK)) {
-            ACR_THROW(ACR_EX_EINVAL, 0);
-            return 0;
-        }
+            masked == (ACR_IPV4_ADDR_OK | ACR_IPV6_ADDR_OK))
+            rc = ACR_EINVAL;
+    }
+    if (rc != 0) {
+        ACR_THROW_NET_ERROR(rc);
+        return 0;
     }
-    
     WITH_CSTR(hostname) {
         rc = AcrFindAddresses(&sa, J2S(hostname), ffamily, port, flags);
     } DONE_WITH_STR(hostname);

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Fri May  6 18:59:16 2011
@@ -16,6 +16,7 @@
 
 #include "acr/string.h"
 #include "acr/clazz.h"
+#include "acr/jniapi.h"
 #include "acr/unsafe.h"
 
 #define NCONVSETN()                         \
@@ -1053,7 +1054,7 @@ AcrGetJavaStringC(JNI_STDENV, jcharArray
         return 0;
     if ((len = (*env)->GetArrayLength(env, str)) < 1)
         return 0;
-    chr = (jchar *)(*env)->GetPrimitiveArrayCritical(env, str, 0);
+    chr = JARRAY_CRITICAL(jchar, str);
     if (chr == 0)
         return 0;
     if (len > ACR_MBUFF_LEN) {
@@ -1073,7 +1074,7 @@ AcrGetJavaStringC(JNI_STDENV, jcharArray
     }
 #endif
 cleanup:
-    (*env)->ReleasePrimitiveArrayCritical(env, str, chr, 0);
+    RELEASE_CRITICAL(str, chr);
     return rv;
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c?rev=1100333&r1=1100332&r2=1100333&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c Fri May  6 18:59:16 2011
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#include "acr/clazz.h"
+#include "acr/jniapi.h"
 #include "acr/unsafe.h"
+#include "acr/clazz.h"
 #include "acr/string.h"
 
 J_DECLARE_CLAZZ = {
@@ -308,13 +309,11 @@ ACR_JNI_EXPORT(void, Unsafe, poke7)(JNI_
 ACR_JNI_EXPORT(jlong, Unsafe, addr0)(JNI_STDARGS, jbyteArray ba)
 {
     jlong adr;
-    void *ptr;
+    char *ptr;
 
-    if (IS_JOBJECT_NULL(ba))
-        return 0;
-    ptr = (*env)->GetPrimitiveArrayCritical(env, ba, 0);
+    ptr = JARRAY_CRITICAL(char, ba);
     adr = P2J(ptr);
-    (*env)->ReleasePrimitiveArrayCritical(env, ba, ptr, 0);
+    RELEASE_CRITICAL(ba, ptr);
     return adr;
 }
 



Mime
View raw message