Skip to content

Commit

Permalink
Extend booleans to the int type in the native
Browse files Browse the repository at this point in the history
The change is to ensure booleans can be handled
appropriately as the int type when calling the
JNI natives.

Close: eclipse-openj9#4193

Signed-off-by: CHENGJin <jincheng@ca.ibm.com>
  • Loading branch information
CHENGJin committed Mar 21, 2019
1 parent 27eb090 commit c28b97c
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 11 deletions.
10 changes: 9 additions & 1 deletion runtime/oti/VMHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,15 @@ class VM_VMHelpers
}
break;
case J9NtcBoolean:
*returnStorage = ((UDATA)(U_8)(0 != *returnStorage));
{
U_32 returnValue = (U_32)*returnStorage;
U_8 * returnAddress = (U_8 *)&returnValue;
#ifdef J9VM_ENV_LITTLE_ENDIAN
*returnStorage = (UDATA)(0 != returnAddress[0]);
#else
*returnStorage = (UDATA)(0 != returnAddress[3]);
#endif /*J9VM_ENV_LITTLE_ENDIAN */
}
break;
case J9NtcByte:
*returnStorage = (UDATA)(IDATA)(I_8)*returnStorage;
Expand Down
3 changes: 2 additions & 1 deletion runtime/tests/jniarg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
################################################################################
# Copyright (c) 2017, 2018 IBM Corp. and others
# Copyright (c) 2017, 2019 IBM Corp. and others
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -30,6 +30,7 @@ add_library(jniargtests SHARED
args_07.c
args_08.c
args_09.c
args_10.c
cdefs.c
)

Expand Down
207 changes: 207 additions & 0 deletions runtime/tests/jniarg/args_10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@

/*******************************************************************************
* Copyright (c) 2019, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

#include "jniargtests.h"

jboolean JNICALL Java_JniArgTests_nativeIZrZ(JNIEnv *p_env, jobject p_this, jint value, jbooleanArray backChannel)
{
J9JavaVM *javaVM = getJ9JavaVM(p_env);
jboolean converted = (jboolean)value;
jboolean elements[] = {JNI_FALSE};
PORT_ACCESS_FROM_JAVAVM(javaVM);

jniTests++;

if (converted) {
/* if C could would see this as true, set the backchannel to true */
elements[0] = JNI_TRUE;
}

(*p_env)->SetBooleanArrayRegion(p_env, backChannel, 0, 1, elements);

return value; /* return the value as a boolean */
}

jboolean JNICALL Java_JniArgTests_nativeZIZIZIZIZIrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jint arg6, jint arg7, jint arg8, jint arg9, jint arg10)
{
J9JavaVM *javaVM = getJ9JavaVM(p_env);
PORT_ACCESS_FROM_JAVAVM(javaVM);

jniTests++;

if (arg1 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 1, arg1, test_jboolean[0]);
}
if (arg2 != test_jint[1]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 2, arg2, test_jint[1]);
}
if (arg3 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 3, arg3, test_jboolean[1]);
}
if (arg4 != test_jint[2]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 4, arg4, test_jint[2]);
}
if (arg5 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 5, arg5, test_jboolean[0]);
}
if (arg6 != test_jint[3]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 6, arg6, test_jint[3]);
}
if (arg7 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 7, arg7, test_jboolean[1]);
}
if (arg8 != test_jint[4]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 8, arg8, test_jint[4]);
}
if (arg9 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 9, arg9, test_jboolean[0]);
}
if (arg10 != test_jint[5]) {
cFailure_jint(PORTLIB, "nativeZIZIZIZIZIrZ", 10, arg10, test_jint[5]);
}

return (JNI_TRUE == test_jboolean[0]);
}

jboolean JNICALL Java_JniArgTests_nativeIZIZIZIZIZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jboolean arg2, jint arg3, jboolean arg4, jint arg5, jboolean arg6, jint arg7, jboolean arg8, jint arg9, jboolean arg10)
{
J9JavaVM *javaVM = getJ9JavaVM(p_env);
PORT_ACCESS_FROM_JAVAVM(javaVM);

jniTests++;

if (arg1 != test_jint[1]) {
cFailure_jint(PORTLIB, "nativeIZIZIZIZIZrZ", 1, arg1, test_jint[1]);
}
if (arg2 != test_jboolean[1]) {
cFailure_jboolean(PORTLIB, "nativeIZIZIZIZIZrZ", 2, arg2, test_jboolean[1]);
}
if (arg3 != test_jint[2]) {
cFailure_jint(PORTLIB, "nativeIZIZIZIZIZrZ", 3, arg3, test_jint[2]);
}
if (arg4 != test_jboolean[0]) {
cFailure_jboolean(PORTLIB, "nativeIZIZIZIZIZrZ", 4, arg4, test_jboolean[0]);
}
if (arg5 != test_jint[3]) {
cFailure_jint(PORTLIB, "nativeIZIZIZIZIZrZ", 5, arg5, test_jint[3]);
}
if (arg6 != test_jboolean[1]) {
cFailure_jboolean(PORTLIB, "nativeIZIZIZIZIZrZ", 6, arg6, test_jboolean[1]);
}
if (arg7 != test_jint[4]) {
cFailure_jint(PORTLIB, "nativeIZIZIZIZIZrZ", 7, arg7, test_jint[4]);
}
if (arg8 != test_jboolean[0]) {
cFailure_jboolean(PORTLIB, "nativeIZIZIZIZIZrZ", 8, arg8, test_jboolean[0]);
}
if (arg9 != test_jint[5]) {
cFailure_jint(PORTLIB, "nativeIZIZIZIZIZrZ", 9, arg9, test_jint[5]);
}
if (arg10 != test_jboolean[1]) {
cFailure_jboolean(PORTLIB, "nativeIZIZIZIZIZrZ", 10, arg10, test_jboolean[1]);
}

return (JNI_FALSE == test_jboolean[0]);
}

jboolean JNICALL Java_JniArgTests_nativeZZZZZZZZZZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jint arg6, jint arg7, jint arg8, jint arg9, jint arg10)
{
J9JavaVM *javaVM = getJ9JavaVM(p_env);
PORT_ACCESS_FROM_JAVAVM(javaVM);

jniTests++;

if (arg1 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 1, arg1, test_jboolean[1]);
}
if (arg2 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 2, arg2, test_jboolean[0]);
}
if (arg3 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 3, arg3, test_jboolean[1]);
}
if (arg4 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 4, arg4, test_jboolean[0]);
}
if (arg5 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 5, arg5, test_jboolean[1]);
}
if (arg6 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 6, arg6, test_jboolean[0]);
}
if (arg7 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 7, arg7, test_jboolean[1]);
}
if (arg8 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 8, arg8, test_jboolean[0]);
}
if (arg9 != test_jboolean[1]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 9, arg9, test_jboolean[1]);
}
if (arg10 != test_jboolean[0]) {
cFailure_jint(PORTLIB, "nativeZZZZZZZZZZrZ", 10, arg10, test_jboolean[0]);
}

return test_jboolean[0];
}

jboolean JNICALL Java_JniArgTests_nativeIIIIIZZZZZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jboolean arg6, jboolean arg7, jboolean arg8, jboolean arg9, jboolean arg10)
{
J9JavaVM *javaVM = getJ9JavaVM(p_env);
PORT_ACCESS_FROM_JAVAVM(javaVM);

jniTests++;

if (arg1 != test_jint[1]) {
cFailure_jint(PORTLIB, "nativeIIIIIZZZZZrZ", 1, arg1, test_jint[1]);
}
if (arg2 != test_jint[2]) {
cFailure_jint(PORTLIB, "nativeIIIIIZZZZZrZ", 2, arg2, test_jint[2]);
}
if (arg3 != test_jint[3]) {
cFailure_jint(PORTLIB, "nativeIIIIIZZZZZrZ", 3, arg3, test_jint[3]);
}
if (arg4 != test_jint[4]) {
cFailure_jint(PORTLIB, "nativeIIIIIZZZZZrZ", 4, arg4, test_jint[4]);
}
if (arg5 != test_jint[5]) {
cFailure_jint(PORTLIB, "nativeIIIIIZZZZZrZ", 5, arg5, test_jint[5]);
}
if (arg6 != test_jboolean[0]) {
cFailure_jboolean(PORTLIB, "nativeIIIIIZZZZZrZ", 6, arg6, test_jboolean[0]);
}
if (arg7 != test_jboolean[1]) {
cFailure_jboolean(PORTLIB, "nativeIIIIIZZZZZrZ", 7, arg7, test_jboolean[1]);
}
if (arg8 != test_jboolean[0]) {
cFailure_jboolean(PORTLIB, "nativeIIIIIZZZZZrZ", 8, arg8, test_jboolean[0]);
}
if (arg9 != test_jboolean[1]) {
cFailure_jboolean(PORTLIB, "nativeIIIIIZZZZZrZ", 9, arg9, test_jboolean[1]);
}
if (arg10 != test_jboolean[0]) {
cFailure_jboolean(PORTLIB, "nativeIIIIIZZZZZrZ", 10, arg10, test_jboolean[0]);
}

return test_jboolean[1];
}
10 changes: 9 additions & 1 deletion runtime/tests/jniarg/cdefs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1998, 2017 IBM Corp. and others
* Copyright (c) 1998, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -51,6 +51,8 @@ const jfloat test_jfloat[] = { 11.1f, 12.2f, 13.3f, 14.4f, 15.5f, 16.6f, 17.7f,

const jdouble test_jdouble[] = { 11.1, 12.2, 13.3, 14.4, 15.5, 16.6, 17.7, 18.8, 19.9, 20.10, 21.11, 22.12, 23.13, 24.14, 25.15, 26.16, };

const jboolean test_jboolean[] = { JNI_TRUE, JNI_FALSE };

int argFailures = 0;
int retValFailures = 0;
int jniTests = 0;
Expand All @@ -71,6 +73,12 @@ J9JavaVM *getJ9JavaVM(JNIEnv * env)
}


void cFailure_jboolean(J9PortLibrary *portLib, char *functionName, int index, jboolean arg, jboolean expected)
{
PORT_ACCESS_FROM_PORT(portLib);
j9tty_printf(portLib, "Argument error: %s::arg%i got: %08X expected: %08X\n", functionName, index, arg, expected);
argFailures++;
}
void cFailure_jbyte(J9PortLibrary *portLib, char *functionName, int index, jbyte arg, jbyte expected)
{
PORT_ACCESS_FROM_PORT(portLib);
Expand Down
7 changes: 6 additions & 1 deletion runtime/tests/jniarg/exports.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright (c) 1991, 2017 IBM Corp. and others
Copyright (c) 1991, 2019 IBM Corp. and others
This program and the accompanying materials are made available under
the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -464,4 +464,9 @@
<export name="Java_JniArgTests_nativeFDFDFDFDFDFDrJ" />
<export name="Java_JniArgTests_nativeDFDFDFDFDFDFrJ" />
<export name="Java_JniArgTests_nativeBBSSIJFDIFDFDFDBBSSIJFDrJ" />
<export name="Java_JniArgTests_nativeIZrZ" />
<export name="Java_JniArgTests_nativeZIZIZIZIZIrZ" />
<export name="Java_JniArgTests_nativeIZIZIZIZIZrZ" />
<export name="Java_JniArgTests_nativeZZZZZZZZZZrZ" />
<export name="Java_JniArgTests_nativeIIIIIZZZZZrZ" />
</exports>
9 changes: 8 additions & 1 deletion runtime/tests/jniarg/jniargtests.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*******************************************************************************
* Copyright (c) 1998, 2014 IBM Corp. and others
* Copyright (c) 1998, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -35,6 +35,7 @@ extern const jint test_jint[];
extern const jlong test_jlong[];
extern const jfloat test_jfloat[];
extern const jdouble test_jdouble[];
extern const jboolean test_jboolean[];
extern int jniTests;

extern void cFailure_jbyte(J9PortLibrary *portLib, char *functionName, int index, jbyte arg, jbyte expected);
Expand All @@ -43,6 +44,7 @@ extern void cFailure_jint(J9PortLibrary *portLib, char *functionName, int index,
extern void cFailure_jlong(J9PortLibrary *portLib, char *functionName, int index, jlong arg, jlong expected);
extern void cFailure_jfloat(J9PortLibrary *portLib, char *functionName, int index, jfloat arg, jfloat expected);
extern void cFailure_jdouble(J9PortLibrary *portLib, char *functionName, int index, jdouble arg, jdouble expected);
extern void cFailure_jboolean(J9PortLibrary *portLib, char *functionName, int index, jboolean arg, jboolean expected);
extern J9JavaVM *getJ9JavaVM(JNIEnv * env);

void JNICALL Java_JniArgTests_logRetValError( JNIEnv *p_env, jobject p_this, jstring error_message );
Expand Down Expand Up @@ -509,3 +511,8 @@ jlong JNICALL Java_JniArgTests_nativeFDFDFDFDFDFDrJ( JNIEnv *p_env, jobject p_th
jlong JNICALL Java_JniArgTests_nativeDFDFDFDFDFDFrJ( JNIEnv *p_env, jobject p_this, jdouble arg1, jfloat arg2, jdouble arg3, jfloat arg4, jdouble arg5, jfloat arg6, jdouble arg7, jfloat arg8, jdouble arg9, jfloat arg10, jdouble arg11, jfloat arg12 );
jlong JNICALL Java_JniArgTests_nativeBBSSIJFDIFDFDFDBBSSIJFDrJ( JNIEnv *p_env, jobject p_this, jbyte arg1, jbyte arg2, jshort arg3, jshort arg4, jint arg5, jlong arg6, jfloat arg7, jdouble arg8, jint arg9, jfloat arg10, jdouble arg11, jfloat arg12, jdouble arg13, jfloat arg14, jdouble arg15, jbyte arg16, jbyte arg17, jshort arg18, jshort arg19, jint arg20, jlong arg21, jfloat arg22, jdouble arg23 );

jboolean JNICALL Java_JniArgTests_nativeIZrZ(JNIEnv *p_env, jobject p_this, jint value, jbooleanArray backChannel);
jboolean JNICALL Java_JniArgTests_nativeZIZIZIZIZIrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jint arg6, jint arg7, jint arg8, jint arg9, jint arg10);
jboolean JNICALL Java_JniArgTests_nativeIZIZIZIZIZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jboolean arg2, jint arg3, jboolean arg4, jint arg5, jboolean arg6, jint arg7, jboolean arg8, jint arg9, jboolean arg10);
jboolean JNICALL Java_JniArgTests_nativeZZZZZZZZZZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jint arg6, jint arg7, jint arg8, jint arg9, jint arg10);
jboolean JNICALL Java_JniArgTests_nativeIIIIIZZZZZrZ(JNIEnv *p_env, jobject p_this, jint arg1, jint arg2, jint arg3, jint arg4, jint arg5, jboolean arg6, jboolean arg7, jboolean arg8, jboolean arg9, jboolean arg10);
4 changes: 2 additions & 2 deletions runtime/vm/BytecodeInterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,7 @@ done:;
getFFIType(U_8 j9ntc) {
static const ffi_type * const J9NtcToFFI[] = {
&ffi_type_void, /* J9NtcVoid */
&ffi_type_uint8, /* J9NtcBoolean */
&ffi_type_uint32, /* J9NtcBoolean */
&ffi_type_sint8, /* J9NtcByte */
&ffi_type_uint16, /* J9NtcChar */
&ffi_type_sint16, /* J9NtcShort */
Expand Down Expand Up @@ -2362,7 +2362,7 @@ done:;
#if !defined(J9VM_ENV_LITTLE_ENDIAN)
if ((J9NtcShort == argTypes[i]) || (J9NtcChar == argTypes[i])) {
values[i + extraArgs] = (void *)((UDATA)values[i + extraArgs] + extraBytesShortAndChar);
}else if ((J9NtcByte == argTypes[i]) || (J9NtcBoolean == argTypes[i])) {
}else if (J9NtcByte == argTypes[i]) {
values[i + extraArgs] = (void *)((UDATA)values[i + extraArgs] + extraBytesBoolAndByte);
}
#endif /*J9VM_ENV_LITTLE_ENDIAN */
Expand Down
10 changes: 9 additions & 1 deletion runtime/vm/MHInterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,15 @@ class VM_MHInterpreter
{
switch (returnType) {
case J9NtcBoolean:
*returnStorage = (UDATA)(U_8)*returnStorage;
{
U_32 returnValue = (U_32)*returnStorage;
U_8 * returnAddress = (U_8 *)&returnValue;
#ifdef J9VM_ENV_LITTLE_ENDIAN
*returnStorage = (UDATA)(0 != returnAddress[0]);
#else
*returnStorage = (UDATA)(0 != returnAddress[3]);
#endif /*J9VM_ENV_LITTLE_ENDIAN */
}
break;
case J9NtcByte:
*returnStorage = (UDATA)(IDATA)(I_8)*returnStorage;
Expand Down
Loading

0 comments on commit c28b97c

Please sign in to comment.