src/share/vm/prims/jniCheck.cpp

Print this page
rev 1876 : 6539281 -Xcheck:jni should validate char* argument to ReleaseStringUTFChars


1271                         const jchar *unicode,
1272                         jsize len))
1273     functionEnter(thr);
1274     jstring result = UNCHECKED()->NewString(env,unicode,len);
1275     functionExit(env);
1276     return result;
1277 JNI_END
1278 
1279 JNI_ENTRY_CHECKED(jsize,
1280   checked_jni_GetStringLength(JNIEnv *env,
1281                               jstring str))
1282     functionEnter(thr);
1283     IN_VM(
1284       checkString(thr, str);
1285     )
1286     jsize result = UNCHECKED()->GetStringLength(env,str);
1287     functionExit(env);
1288     return result;
1289 JNI_END
1290 



1291 JNI_ENTRY_CHECKED(const jchar *,
1292   checked_jni_GetStringChars(JNIEnv *env,
1293                              jstring str,
1294                              jboolean *isCopy))
1295     functionEnter(thr);
1296     IN_VM(
1297       checkString(thr, str);
1298     )
1299     const jchar *result = UNCHECKED()->GetStringChars(env,str,isCopy);









1300     functionExit(env);
1301     return result;
1302 JNI_END
1303 
1304 JNI_ENTRY_CHECKED(void,
1305   checked_jni_ReleaseStringChars(JNIEnv *env,
1306                                  jstring str,
1307                                  const jchar *chars))
1308     functionEnterExceptionAllowed(thr);
1309     IN_VM(
1310       checkString(thr, str);
1311     )
1312     /* cannot check validity of copy, unless every request is logged by
1313      * checking code.  Implementation of this check is deferred until a
1314      * subsequent release.
1315      */
1316     UNCHECKED()->ReleaseStringChars(env,str,chars);
1317     functionExit(env);
1318 JNI_END
1319 
1320 JNI_ENTRY_CHECKED(jstring,
1321   checked_jni_NewStringUTF(JNIEnv *env,
1322                            const char *utf))
1323     functionEnter(thr);
1324     jstring result = UNCHECKED()->NewStringUTF(env,utf);
1325     functionExit(env);
1326     return result;
1327 JNI_END
1328 
1329 JNI_ENTRY_CHECKED(jsize,
1330   checked_jni_GetStringUTFLength(JNIEnv *env,
1331                                  jstring str))
1332     functionEnter(thr);
1333     IN_VM(
1334       checkString(thr, str);
1335     )
1336     jsize result = UNCHECKED()->GetStringUTFLength(env,str);
1337     functionExit(env);
1338     return result;
1339 JNI_END
1340 



1341 JNI_ENTRY_CHECKED(const char *,
1342   checked_jni_GetStringUTFChars(JNIEnv *env,
1343                                 jstring str,
1344                                 jboolean *isCopy))
1345     functionEnter(thr);
1346     IN_VM(
1347       checkString(thr, str);
1348     )
1349     const char *result = UNCHECKED()->GetStringUTFChars(env,str,isCopy);









1350     functionExit(env);
1351     return result;
1352 JNI_END
1353 
1354 JNI_ENTRY_CHECKED(void,
1355   checked_jni_ReleaseStringUTFChars(JNIEnv *env,
1356                                     jstring str,
1357                                     const char* chars))
1358     functionEnterExceptionAllowed(thr);
1359     IN_VM(
1360       checkString(thr, str);
1361     )
1362     /* cannot check validity of copy, unless every request is logged by
1363      * checking code.  Implementation of this check is deferred until a
1364      * subsequent release.
1365      */
1366     UNCHECKED()->ReleaseStringUTFChars(env,str,chars);
1367     functionExit(env);
1368 JNI_END
1369 
1370 JNI_ENTRY_CHECKED(jsize,
1371   checked_jni_GetArrayLength(JNIEnv *env,
1372                              jarray array))
1373     functionEnter(thr);
1374     IN_VM(
1375       checkArray(thr, array, -1);
1376     )
1377     jsize result = UNCHECKED()->GetArrayLength(env,array);
1378     functionExit(env);
1379     return result;
1380 JNI_END
1381 
1382 JNI_ENTRY_CHECKED(jobjectArray,
1383   checked_jni_NewObjectArray(JNIEnv *env,
1384                              jsize len,
1385                              jclass clazz,
1386                              jobject init))




1271                         const jchar *unicode,
1272                         jsize len))
1273     functionEnter(thr);
1274     jstring result = UNCHECKED()->NewString(env,unicode,len);
1275     functionExit(env);
1276     return result;
1277 JNI_END
1278 
1279 JNI_ENTRY_CHECKED(jsize,
1280   checked_jni_GetStringLength(JNIEnv *env,
1281                               jstring str))
1282     functionEnter(thr);
1283     IN_VM(
1284       checkString(thr, str);
1285     )
1286     jsize result = UNCHECKED()->GetStringLength(env,str);
1287     functionExit(env);
1288     return result;
1289 JNI_END
1290 
1291 // Arbitrary (but well-known) tag
1292 #define STRING_TAG 0x47114711
1293 
1294 JNI_ENTRY_CHECKED(const jchar *,
1295   checked_jni_GetStringChars(JNIEnv *env,
1296                              jstring str,
1297                              jboolean *isCopy))
1298     functionEnter(thr);
1299     IN_VM(
1300       checkString(thr, str);
1301     )
1302     const jchar *result = UNCHECKED()->GetStringChars(env,str,isCopy);
1303 
1304     size_t len = UNCHECKED()->GetStringLength(env,str) + 1; // + 1 for NULL termination
1305     jint* tagLocation = (jint*) AllocateHeap(len * sizeof(jchar) + sizeof(jint), "checked_jni_GetStringChars"); 
1306     *tagLocation = STRING_TAG;
1307     jchar* newResult = (jchar*) (tagLocation + 1);
1308     memcpy(newResult, result, len * sizeof(jchar));
1309     // Avoiding call to UNCHECKED()->ReleaseStringChars() since that will fire unexpected dtrace probes
1310     FreeHeap((char*)result);
1311 
1312     functionExit(env);
1313     return newResult;
1314 JNI_END
1315 
1316 JNI_ENTRY_CHECKED(void,
1317   checked_jni_ReleaseStringChars(JNIEnv *env,
1318                                  jstring str,
1319                                  const jchar *chars))
1320     functionEnterExceptionAllowed(thr);
1321     IN_VM(
1322       checkString(thr, str);
1323     )
1324     jint *tagLocation = ((jint*) chars) - 1;
1325     if (*tagLocation != STRING_TAG) {
1326        NativeReportJNIFatalError(thr, "ReleaseStringChars called on something not allocated by GetStringChars");
1327     }
1328     UNCHECKED()->ReleaseStringChars(env,str,(const jchar*)tagLocation);
1329     functionExit(env);
1330 JNI_END
1331 
1332 JNI_ENTRY_CHECKED(jstring,
1333   checked_jni_NewStringUTF(JNIEnv *env,
1334                            const char *utf))
1335     functionEnter(thr);
1336     jstring result = UNCHECKED()->NewStringUTF(env,utf);
1337     functionExit(env);
1338     return result;
1339 JNI_END
1340 
1341 JNI_ENTRY_CHECKED(jsize,
1342   checked_jni_GetStringUTFLength(JNIEnv *env,
1343                                  jstring str))
1344     functionEnter(thr);
1345     IN_VM(
1346       checkString(thr, str);
1347     )
1348     jsize result = UNCHECKED()->GetStringUTFLength(env,str);
1349     functionExit(env);
1350     return result;
1351 JNI_END
1352 
1353 // Arbitrary (but well-known) tag - different than GetStringChars
1354 #define STRING_UTF_TAG 0x48124812
1355 
1356 JNI_ENTRY_CHECKED(const char *,
1357   checked_jni_GetStringUTFChars(JNIEnv *env,
1358                                 jstring str,
1359                                 jboolean *isCopy))
1360     functionEnter(thr);
1361     IN_VM(
1362       checkString(thr, str);
1363     )
1364     const char *result = UNCHECKED()->GetStringUTFChars(env,str,isCopy);
1365 
1366     size_t len = strlen(result) + 1; // + 1 for NULL termination
1367     jint* tagLocation = (jint*) AllocateHeap(len + sizeof(jint), "checked_jni_GetStringUTFChars");
1368     *tagLocation = STRING_UTF_TAG;
1369     char* newResult = (char*) (tagLocation + 1);
1370     strcpy(newResult, result);
1371     // Avoiding call to UNCHECKED()->ReleaseStringUTFChars() since that will fire unexpected dtrace probes
1372     FreeHeap((char*)result);
1373 
1374     functionExit(env);
1375     return newResult;
1376 JNI_END
1377 
1378 JNI_ENTRY_CHECKED(void,
1379   checked_jni_ReleaseStringUTFChars(JNIEnv *env,
1380                                     jstring str,
1381                                     const char* chars))
1382     functionEnterExceptionAllowed(thr);
1383     IN_VM(
1384       checkString(thr, str);
1385     )
1386     jint* tagLocation = ((jint*) chars) - 1;
1387     if (*tagLocation != STRING_UTF_TAG) {
1388        NativeReportJNIFatalError(thr, "ReleaseStringUTFChars called on something not allocated by GetStringUTFChars");
1389     }
1390     UNCHECKED()->ReleaseStringUTFChars(env,str,(const char*)tagLocation);
1391     functionExit(env);
1392 JNI_END
1393 
1394 JNI_ENTRY_CHECKED(jsize,
1395   checked_jni_GetArrayLength(JNIEnv *env,
1396                              jarray array))
1397     functionEnter(thr);
1398     IN_VM(
1399       checkArray(thr, array, -1);
1400     )
1401     jsize result = UNCHECKED()->GetArrayLength(env,array);
1402     functionExit(env);
1403     return result;
1404 JNI_END
1405 
1406 JNI_ENTRY_CHECKED(jobjectArray,
1407   checked_jni_NewObjectArray(JNIEnv *env,
1408                              jsize len,
1409                              jclass clazz,
1410                              jobject init))