1
1
/*
2
- * Copyright (c) 2001, 2020 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2001, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
43
43
#include " runtime/jfieldIDWorkaround.hpp"
44
44
#include " runtime/jniHandles.inline.hpp"
45
45
#include " runtime/thread.inline.hpp"
46
+ #include " utilities/formatBuffer.hpp"
46
47
#include " utilities/utf8.hpp"
47
48
48
49
// Complain every extra number of unplanned local refs
@@ -399,19 +400,16 @@ static void* check_wrapped_array(JavaThread* thr, const char* fn_name,
399
400
GuardedMemory guarded (carray);
400
401
void * orig_result = guarded.get_tag ();
401
402
if (!guarded.verify_guards ()) {
402
- tty->print_cr (" ReleasePrimitiveArrayCritical: release array failed bounds "
403
- " check, incorrect pointer returned ? array: " PTR_FORMAT " carray: "
404
- PTR_FORMAT, p2i (obj), p2i (carray));
405
- guarded.print_on (tty);
406
- NativeReportJNIFatalError (thr, " ReleasePrimitiveArrayCritical: "
407
- " failed bounds check" );
403
+ tty->print_cr (" %s: release array failed bounds check, incorrect pointer returned ? array: "
404
+ PTR_FORMAT " carray: " PTR_FORMAT, fn_name, p2i (obj), p2i (carray));
405
+ DEBUG_ONLY (guarded.print_on (tty);) // This may crash.
406
+ NativeReportJNIFatalError (thr, err_msg (" %s: failed bounds check" , fn_name));
408
407
}
409
408
if (orig_result == NULL ) {
410
- tty->print_cr (" ReleasePrimitiveArrayCritical: unrecognized elements. array: "
411
- PTR_FORMAT " carray: " PTR_FORMAT, p2i (obj), p2i (carray));
412
- guarded.print_on (tty);
413
- NativeReportJNIFatalError (thr, " ReleasePrimitiveArrayCritical: "
414
- " unrecognized elements" );
409
+ tty->print_cr (" %s: unrecognized elements. array: " PTR_FORMAT " carray: " PTR_FORMAT,
410
+ fn_name, p2i (obj), p2i (carray));
411
+ DEBUG_ONLY (guarded.print_on (tty);) // This may crash.
412
+ NativeReportJNIFatalError (thr, err_msg (" %s: unrecognized elements" , fn_name));
415
413
}
416
414
if (rsz != NULL ) {
417
415
*rsz = guarded.get_user_size ();
@@ -420,24 +418,30 @@ static void* check_wrapped_array(JavaThread* thr, const char* fn_name,
420
418
}
421
419
422
420
static void * check_wrapped_array_release (JavaThread* thr, const char * fn_name,
423
- void * obj, void * carray, jint mode) {
421
+ void * obj, void * carray, jint mode, jboolean is_critical ) {
424
422
size_t sz;
425
423
void * orig_result = check_wrapped_array (thr, fn_name, obj, carray, &sz);
426
424
switch (mode) {
427
- // As we never make copies, mode 0 and JNI_COMMIT are the same.
428
425
case 0 :
426
+ memcpy (orig_result, carray, sz);
427
+ GuardedMemory::free_copy (carray);
428
+ break ;
429
429
case JNI_COMMIT:
430
430
memcpy (orig_result, carray, sz);
431
+ if (is_critical) {
432
+ // For ReleasePrimitiveArrayCritical we must free the internal buffer
433
+ // allocated through GuardedMemory.
434
+ GuardedMemory::free_copy (carray);
435
+ }
431
436
break ;
432
437
case JNI_ABORT:
438
+ GuardedMemory::free_copy (carray);
433
439
break ;
434
440
default :
435
441
tty->print_cr (" %s: Unrecognized mode %i releasing array "
436
- PTR_FORMAT " elements " PTR_FORMAT, fn_name, mode, p2i (obj), p2i (carray));
442
+ PTR_FORMAT " elements " PTR_FORMAT, fn_name, mode, p2i (obj), p2i (carray));
437
443
NativeReportJNIFatalError (thr, " Unrecognized array release mode" );
438
444
}
439
- // We always need to release the copy we made with GuardedMemory
440
- GuardedMemory::free_copy (carray);
441
445
return orig_result;
442
446
}
443
447
@@ -1715,7 +1719,7 @@ JNI_ENTRY_CHECKED(void, \
1715
1719
typeArrayOop a = typeArrayOop (JNIHandles::resolve_non_null (array)); \
1716
1720
) \
1717
1721
ElementType* orig_result = (ElementType *) check_wrapped_array_release ( \
1718
- thr, " checked_jni_Release" #Result" ArrayElements" , array, elems, mode); \
1722
+ thr, " checked_jni_Release" #Result" ArrayElements" , array, elems, mode, JNI_FALSE ); \
1719
1723
UNCHECKED ()->Release ##Result##ArrayElements (env, array, orig_result, mode); \
1720
1724
functionExit (thr); \
1721
1725
JNI_END
@@ -1884,7 +1888,8 @@ JNI_ENTRY_CHECKED(void,
1884
1888
check_is_primitive_array (thr, array);
1885
1889
)
1886
1890
// Check the element array...
1887
- void * orig_result = check_wrapped_array_release (thr, " ReleasePrimitiveArrayCritical" , array, carray, mode);
1891
+ void * orig_result = check_wrapped_array_release (thr, " ReleasePrimitiveArrayCritical" ,
1892
+ array, carray, mode, JNI_TRUE);
1888
1893
UNCHECKED ()->ReleasePrimitiveArrayCritical (env, array, orig_result, mode);
1889
1894
functionExit (thr);
1890
1895
JNI_END
0 commit comments