From 72078b1948e3b8aa12f0a598631218341a8b9970 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 2 Jan 2023 01:38:49 +0100 Subject: Bug 574: Java_org_jau_sys_Clock_get[Monotonic|WallClock]TimeImpl(): Avoid GetPrimitiveArrayCritical(), which occasionally hangs on system call ::clock_gettime() Resolves Bug 574, detected via Java application utilizing Cypherpack on RPI4 arm64. --- java_jni/jni/jau/jau_sys_Clock.cxx | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'java_jni/jni/jau') diff --git a/java_jni/jni/jau/jau_sys_Clock.cxx b/java_jni/jni/jau/jau_sys_Clock.cxx index 0e845f3..dde56de 100644 --- a/java_jni/jni/jau/jau_sys_Clock.cxx +++ b/java_jni/jni/jau/jau_sys_Clock.cxx @@ -44,19 +44,16 @@ void Java_org_jau_sys_Clock_getMonotonicTimeImpl(JNIEnv *env, jclass clazz, jlon if( nullptr == jval ) { throw jau::IllegalArgumentException("val null", E_FILE_LINE); } - const size_t in_size = env->GetArrayLength(jval); - if( 2 > in_size ) { - throw jau::IllegalArgumentException("val size "+std::to_string(in_size)+" < 2", E_FILE_LINE); - } - jau::jni::JNICriticalArray criticalArray(env); // RAII - release - int64_t * val_ptr = criticalArray.get(jval, criticalArray.Mode::UPDATE_AND_RELEASE); - if( nullptr == val_ptr ) { - throw jau::InternalError("GetPrimitiveArrayCritical(address val array) is null", E_FILE_LINE); + const size_t jval_size = env->GetArrayLength(jval); + if( 2 > jval_size ) { + throw jau::IllegalArgumentException("val size "+std::to_string(jval_size)+" < 2", E_FILE_LINE); } + // Avoid GetPrimitiveArrayCritical(), which occasionally hangs on system call ::clock_gettime() struct timespec t { 0, 0 }; ::clock_gettime(CLOCK_MONOTONIC, &t); - val_ptr[0] = (int64_t)t.tv_sec; - val_ptr[1] = (int64_t)t.tv_nsec; + const jlong val[] = { (jlong)t.tv_sec, (jlong)t.tv_nsec }; + env->SetLongArrayRegion(jval, 0, (jsize)jval_size, val); + jau::jni::java_exception_check_and_throw(env, E_FILE_LINE); } catch(...) { rethrow_and_raise_java_exception_jau(env); } @@ -68,19 +65,16 @@ void Java_org_jau_sys_Clock_getWallClockTimeImpl(JNIEnv *env, jclass clazz, jlon if( nullptr == jval ) { throw jau::IllegalArgumentException("val null", E_FILE_LINE); } - const size_t in_size = env->GetArrayLength(jval); - if( 2 > in_size ) { - throw jau::IllegalArgumentException("val size "+std::to_string(in_size)+" < 2", E_FILE_LINE); - } - jau::jni::JNICriticalArray criticalArray(env); // RAII - release - int64_t * val_ptr = criticalArray.get(jval, criticalArray.Mode::UPDATE_AND_RELEASE); - if( nullptr == val_ptr ) { - throw jau::InternalError("GetPrimitiveArrayCritical(address val array) is null", E_FILE_LINE); + const size_t jval_size = env->GetArrayLength(jval); + if( 2 > jval_size ) { + throw jau::IllegalArgumentException("val size "+std::to_string(jval_size)+" < 2", E_FILE_LINE); } + // Avoid GetPrimitiveArrayCritical(), which occasionally hangs on system call ::clock_gettime() struct timespec t { 0, 0 }; ::clock_gettime(CLOCK_REALTIME, &t); - val_ptr[0] = (int64_t)t.tv_sec; - val_ptr[1] = (int64_t)t.tv_nsec; + const jlong val[] = { (jlong)t.tv_sec, (jlong)t.tv_nsec }; + env->SetLongArrayRegion(jval, 0, (jsize)jval_size, val); + jau::jni::java_exception_check_and_throw(env, E_FILE_LINE); } catch(...) { rethrow_and_raise_java_exception_jau(env); } -- cgit v1.2.3