Skip to content

Commit

Permalink
8275344: -Xcheck:jni produces some warnings in the LCMS.c
Browse files Browse the repository at this point in the history
Reviewed-by: azvegint, prr, kizune
  • Loading branch information
mrserb committed Oct 22, 2021
1 parent 6a466fe commit c978ca8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
69 changes: 50 additions & 19 deletions src/java.desktop/share/native/liblcms/LCMS.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ void errorHandler(cmsContext ContextID, cmsUInt32Number errorCode,
errMsg[count] = 0;

(*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg);
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it before
JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg);
}
}

JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *jvm, void *reserved) {
Expand Down Expand Up @@ -113,6 +115,26 @@ void LCMS_freeTransform(JNIEnv *env, jlong ID)
cmsDeleteTransform(sTrans);
}

/*
* Throw an IllegalArgumentException and init the cause.
*/
static void ThrowIllegalArgumentException(JNIEnv *env, const char *msg) {
jthrowable cause = (*env)->ExceptionOccurred(env);
if (cause != NULL) {
(*env)->ExceptionClear(env);
}
jstring str = JNU_NewStringPlatform(env, msg);
if (str != NULL) {
jobject iae = JNU_NewObjectByName(env,
"java/lang/IllegalArgumentException",
"(Ljava/lang/String;Ljava/lang/Throwable;)V",
str, cause);
if (iae != NULL) {
(*env)->Throw(env, iae);
}
}
}

/*
* Class: sun_java2d_cmm_lcms_LCMS
* Method: createNativeTransform
Expand Down Expand Up @@ -185,7 +207,7 @@ JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_createNativeTransform
if (sTrans == NULL) {
J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_createNativeTransform: "
"sTrans == NULL");
if ((*env)->ExceptionOccurred(env) == NULL) {
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Cannot get color transform");
}
Expand Down Expand Up @@ -214,7 +236,7 @@ JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfileNative
cmsHPROFILE pf;

if (JNU_IsNull(env, data)) {
JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
ThrowIllegalArgumentException(env, "Invalid profile data");
return 0L;
}

Expand All @@ -232,7 +254,7 @@ JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfileNative
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0);

if (pf == NULL) {
JNU_ThrowIllegalArgumentException(env, "Invalid profile data");
ThrowIllegalArgumentException(env, "Invalid profile data");
} else {
/* Sanity check: try to save the profile in order
* to force basic validation.
Expand All @@ -241,8 +263,7 @@ JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfileNative
if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
pfSize < sizeof(cmsICCHeader))
{
JNU_ThrowIllegalArgumentException(env, "Invalid profile data");

ThrowIllegalArgumentException(env, "Invalid profile data");
cmsCloseProfile(pf);
pf = NULL;
}
Expand Down Expand Up @@ -276,8 +297,10 @@ JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileDataNative

// determine actual profile size
if (!cmsSaveProfileToMem(sProf->pf, NULL, &pfSize)) {
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not access specified profile.");
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not access specified profile.");
}
return NULL;
}

Expand All @@ -298,8 +321,10 @@ JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileDataNative
(*env)->ReleaseByteArrayElements(env, data, dataArray, 0);

if (!status) {
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not access specified profile.");
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not access specified profile.");
}
return NULL;
}
return data;
Expand Down Expand Up @@ -354,8 +379,10 @@ JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0);

if (!status) {
JNU_ThrowByName(env, "java/awt/color/CMMException",
"ICC Profile header not found");
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"ICC Profile header not found");
}
return NULL;
}

Expand All @@ -365,8 +392,10 @@ JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
if (cmsIsTag(sProf->pf, sig.cms)) {
tagSize = cmsReadRawTag(sProf->pf, sig.cms, NULL, 0);
} else {
JNU_ThrowByName(env, "java/awt/color/CMMException",
"ICC profile tag not found");
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"ICC profile tag not found");
}
return NULL;
}

Expand All @@ -389,8 +418,10 @@ JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
(*env)->ReleaseByteArrayElements (env, data, dataArray, 0);

if (bufSize != tagSize) {
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not get tag data.");
if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
JNU_ThrowByName(env, "java/awt/color/CMMException",
"Can not get tag data.");
}
return NULL;
}
return data;
Expand All @@ -415,7 +446,7 @@ JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagDataNative
sig.j = tagSig;

if (JNU_IsNull(env, data)) {
JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
ThrowIllegalArgumentException(env, "Can not write tag data.");
return;
}

Expand Down Expand Up @@ -443,7 +474,7 @@ JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagDataNative
(*env)->ReleaseByteArrayElements(env, data, dataArray, 0);

if (!status) {
JNU_ThrowIllegalArgumentException(env, "Can not write tag data.");
ThrowIllegalArgumentException(env, "Can not write tag data.");
} else if (pfReplace != NULL) {
cmsCloseProfile(sProf->pf);
sProf->pf = pfReplace;
Expand Down Expand Up @@ -554,7 +585,7 @@ JNIEXPORT jobject JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileID
return NULL;
}
jobject cmmProfile = (*env)->CallObjectMethod(env, pf, mid);
if ((*env)->ExceptionOccurred(env)) {
if ((*env)->ExceptionCheck(env)) {
return NULL;
}
jclass lcmsPCls = (*env)->FindClass(env, "sun/java2d/cmm/lcms/LCMSProfile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,31 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

/**
* @test
* @bug 8271718 8273135
* @bug 8271718 8273135 8275344
* @summary Verifies MT safety of color transformation while profile is changed
* @library /test/lib
* @run main/othervm MTTransformReplacedProfile
* @run main/othervm MTTransformReplacedProfile checkJNI
*/
public final class MTTransformReplacedProfile {

private static volatile long endtime;

public static void main(String[] args) throws Exception {
if (args.length > 0 && args[0].equals("checkJNI")) {
ProcessBuilder pb = ProcessTools.createTestJvm(
"-Xcheck:jni", MTTransformReplacedProfile.class.getName());
OutputAnalyzer oa = ProcessTools.executeProcess(pb);
oa.stderrShouldBeEmpty();
oa.stdoutShouldBeEmpty();
oa.shouldHaveExitValue(0);
return;
}
ICC_Profile[] profiles = {
ICC_Profile.getInstance(ColorSpace.CS_sRGB),
ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB),
Expand Down

1 comment on commit c978ca8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.