Skip to content

Commit

Permalink
JIT cthelpers to query annotations
Browse files Browse the repository at this point in the history
Details are in the issues below.

Fixes: eclipse-openj9#11993
Fixes: eclipse-openj9#11990

Most of the work was done by Sharon Wang. Creating this PR on her
behalf.

Signed-off-by: Tobi Ajila <atobia@ca.ibm.com>
  • Loading branch information
tajila committed May 20, 2021
1 parent 82f540b commit ba6a26e
Show file tree
Hide file tree
Showing 17 changed files with 1,252 additions and 27 deletions.
57 changes: 56 additions & 1 deletion runtime/jit_vm/cthelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2019 IBM Corp. and others
* Copyright (c) 1991, 2021 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 @@ -29,6 +29,18 @@

extern "C" {

#if JAVA_SPEC_VERSION >= 16
J9_DECLARE_CONSTANT_UTF8(ojdk_intrinsicCandidate, "Ljdk/internal/vm/annotation/IntrinsicCandidate;");
#endif /* JAVA_SPEC_VERSION >= 16 */

#if JAVA_SPEC_VERSION >= 11
J9_DECLARE_CONSTANT_UTF8(ojdk_stable, "Ljdk/internal/vm/annotation/Stable;");
J9_DECLARE_CONSTANT_UTF8(ojdk_forceInline, "Ljdk/internal/vm/annotation/ForceInline;");
#else /* JAVA_SPEC_VERSION >= 11 */
J9_DECLARE_CONSTANT_UTF8(ojdk_stable, "Ljava/lang/invoke/Stable;");
J9_DECLARE_CONSTANT_UTF8(ojdk_forceInline, "Ljava/lang/invoke/ForceInline;");
#endif /* JAVA_SPEC_VERSION >= 11 */

void*
jitGetCountingSendTarget(J9VMThread *vmThread, J9Method *ramMethod)
{
Expand Down Expand Up @@ -181,4 +193,47 @@ jitGetConstantDynamicTypeFromCP(J9VMThread *currentThread, J9ConstantPool *const
return sigUTF;
}

/**
* Queries if the fieldref at the specified cpIndex contains the @Stable annotation
*
* @param clazz J9Class
* @param cpIndex fieldref cp index
* @return true if fieldref contains @Stable, false otherwise
*/
bool
jitIsFieldStable(J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, bool isStatic)
{
return FALSE != fieldContainsRuntimeAnnotation(currentThread, clazz, cpIndex, (J9UTF8 *)&ojdk_stable);
}

/**
* Queries if the methodref at the specified cpIndex contains the @ForceInline annotation
*
* @param clazz J9Class
* @param cpIndex methodref cp index
* @return true if methodref contains @ForceInline, false otherwise
*/
bool
jitIsMethodTaggedWithForceInline(J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, UDATA type)
{
return FALSE != methodContainsRuntimeAnnotation(currentThread, clazz, cpIndex, (J9UTF8 *)&ojdk_forceInline, type);
}

/**
* Queries if the methodref at the specified cpIndex contains the @IntrinsicCandidate annotation
*
* @param clazz J9Class
* @param cpIndex methodref cp index
* @return true if methodref contains @IntrinsicCandidate, false otherwise
*/
bool
jitIsMethodTaggedWithIntrinsicCandidate(J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, UDATA type)
{
#if JAVA_SPEC_VERSION >= 16
return FALSE != methodContainsRuntimeAnnotation(currentThread, clazz, cpIndex, (J9UTF8 *)&ojdk_intrinsicCandidate, type);
#else /* JAVA_SPEC_VERSION >= 16 */
return false;
#endif /* JAVA_SPEC_VERSION >= 16 */
}

}
26 changes: 13 additions & 13 deletions runtime/makelib/targets.mk.linux.inc.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $(UMA_DLLTARGET) : $(UMA_OBJECTS) $(UMA_TARGET_LIBRARIES)
$(UMA_DLL_LINK_POSTFLAGS)
ifdef j9vm_uma_gnuDebugSymbols
$(OBJCOPY) --only-keep-debug $@ $(@:$(UMA_DOT_DLL)=.debuginfo)
$(OBJCOPY) --strip-debug $@
$(OBJCOPY) $@
$(OBJCOPY) --add-gnu-debuglink=$(@:$(UMA_DOT_DLL)=.debuginfo) $@
endif
</#assign>
Expand All @@ -64,7 +64,7 @@ $(UMA_EXETARGET) : $(UMA_OBJECTS) $(UMA_TARGET_LIBRARIES)
-o $@ $(UMA_EXE_POSTFIX_FLAGS)
ifdef j9vm_uma_gnuDebugSymbols
$(OBJCOPY) --only-keep-debug $@ $(@:$(UMA_DOT_EXE)=.debuginfo)
$(OBJCOPY) --strip-debug $@
$(OBJCOPY) $@
$(OBJCOPY) --add-gnu-debuglink=$(@:$(UMA_DOT_EXE)=.debuginfo) $@
endif
</#assign>
Expand Down Expand Up @@ -111,13 +111,13 @@ ifndef UMA_DO_NOT_OPTIMIZE_CCODE
UMA_OPTIMIZATION_CFLAGS += ${uma.spec.properties.uma_optimization_cflags.value}
<#else>
<#if uma.spec.processor.amd64 || uma.spec.processor.riscv64>
UMA_OPTIMIZATION_CFLAGS += -O3 -fno-strict-aliasing
UMA_OPTIMIZATION_CFLAGS += -O0 -fno-strict-aliasing
<#elseif uma.spec.processor.x86>
UMA_OPTIMIZATION_CFLAGS += -O3 -fno-strict-aliasing -march=pentium4 -mtune=prescott -mpreferred-stack-boundary=4
UMA_OPTIMIZATION_CFLAGS += -O0 -fno-strict-aliasing -march=pentium4 -mtune=prescott -mpreferred-stack-boundary=4
<#elseif uma.spec.processor.arm>
UMA_OPTIMIZATION_CFLAGS += -g -O3 -fno-strict-aliasing $(ARM_ARCH_FLAGS) -Wno-unused-but-set-variable
UMA_OPTIMIZATION_CFLAGS += -g3 -O0 -fno-strict-aliasing $(ARM_ARCH_FLAGS) -Wno-unused-but-set-variable
<#elseif uma.spec.processor.ppc>
UMA_OPTIMIZATION_CFLAGS += -O3
UMA_OPTIMIZATION_CFLAGS += -O0
<#if uma.spec.flags.env_gcc.enabled>
UMA_OPTIMIZATION_CFLAGS += -fno-strict-aliasing
</#if>
Expand All @@ -128,7 +128,7 @@ ifndef UMA_DO_NOT_OPTIMIZE_CCODE
endif
</#if>
<#elseif uma.spec.processor.s390>
UMA_OPTIMIZATION_CFLAGS += -O3 -mtune=z10 -march=z9-109 -mzarch
UMA_OPTIMIZATION_CFLAGS += -O0 -mtune=z10 -march=z9-109 -mzarch
<#else>
UMA_OPTIMIZATION_CFLAGS += -O
</#if>
Expand All @@ -137,21 +137,21 @@ ifndef UMA_DO_NOT_OPTIMIZE_CCODE
UMA_OPTIMIZATION_CXXFLAGS += ${uma.spec.properties.uma_optimization_cxxflags.value}
<#else>
<#if uma.spec.processor.amd64 || uma.spec.processor.riscv64>
UMA_OPTIMIZATION_CXXFLAGS += -O3 -fno-strict-aliasing
UMA_OPTIMIZATION_CXXFLAGS += -O0 -fno-strict-aliasing
<#elseif uma.spec.processor.x86>
UMA_OPTIMIZATION_CXXFLAGS += -O3 -fno-strict-aliasing -march=pentium4 -mtune=prescott -mpreferred-stack-boundary=4
UMA_OPTIMIZATION_CXXFLAGS += -O0 -fno-strict-aliasing -march=pentium4 -mtune=prescott -mpreferred-stack-boundary=4
<#elseif uma.spec.processor.arm>
UMA_OPTIMIZATION_CXXFLAGS += -g -O3 -fno-strict-aliasing $(ARM_ARCH_FLAGS) -Wno-unused-but-set-variable
UMA_OPTIMIZATION_CXXFLAGS += -g3 -O0 -fno-strict-aliasing $(ARM_ARCH_FLAGS) -Wno-unused-but-set-variable
<#elseif uma.spec.processor.ppc>
UMA_OPTIMIZATION_CXXFLAGS += -O3
UMA_OPTIMIZATION_CXXFLAGS += -O0
<#if uma.spec.flags.env_gcc.enabled>
UMA_OPTIMIZATION_CXXFLAGS += -fno-strict-aliasing
</#if>
<#if uma.spec.flags.env_littleEndian.enabled && uma.spec.type.linux>
UMA_OPTIMIZATION_CXXFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
</#if>
<#elseif uma.spec.processor.s390>
UMA_OPTIMIZATION_CXXFLAGS += -O3 -mtune=z10 -march=z9-109 -mzarch
UMA_OPTIMIZATION_CXXFLAGS += -O0 -mtune=z10 -march=z9-109 -mzarch
<#else>
UMA_OPTIMIZATION_CXXFLAGS += -O
</#if>
Expand All @@ -165,7 +165,7 @@ CFLAGS += $(UMA_OPTIMIZATION_CFLAGS)
CXXFLAGS += $(UMA_OPTIMIZATION_CXXFLAGS)
<#if uma.spec.processor.ppc>
ifdef USE_PPC_GCC
PPC_GCC_CXXFLAGS += -O3 -fno-strict-aliasing
PPC_GCC_CXXFLAGS += -O0 -fno-strict-aliasing
endif
</#if>

Expand Down
3 changes: 3 additions & 0 deletions runtime/oti/j9nonbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4779,6 +4779,9 @@ typedef struct J9InternalVMFunctions {
UDATA ( *jniIsInternalClassRef)(struct J9JavaVM *vm, jobject ref);
BOOLEAN (*objectIsBeingWaitedOn)(struct J9VMThread *currentThread, struct J9VMThread *targetThread, j9object_t obj);
BOOLEAN (*areValueBasedMonitorChecksEnabled)(struct J9JavaVM *vm);
BOOLEAN (*fieldContainsRuntimeAnnotation)(struct J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, J9UTF8 *annotationName);
BOOLEAN (*methodContainsRuntimeAnnotation)(struct J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, J9UTF8 *annotationName, UDATA type);
J9ROMFieldShape* (*findFieldAndCheckVisibility)(struct J9VMThread *currentThread, J9Class *clazz, U_8 *fieldName, UDATA fieldNameLength, U_8 *signature, UDATA signatureLength, J9Class **definingClass, UDATA *offsetOrAddress, UDATA options, J9Class *sourceClass);
} J9InternalVMFunctions;

/* Jazz 99339: define a new structure to replace JavaVM so as to pass J9NativeLibrary to JVMTIEnv */
Expand Down
4 changes: 0 additions & 4 deletions runtime/oti/j9protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,10 +1382,6 @@ extern J9_CFUNC void jitAddPicToPatchOnClassUnload (void *classPointer, void *a
#endif /* J9VM_INTERP_NATIVE_SUPPORT */
#endif /* _J9VMNATIVEHELPERSLARGE_ */

/* Runtime annotation handling */
extern J9_CFUNC I_32 getAnnotationByType(J9ROMConstantPoolItem const *constantPool, J9UTF8 const *searchString,
U_32 const numAnnotations, U_8 const *data, U_8 const **pIndex, U_8 const *dataEnd);

#ifdef __cplusplus
}
#endif
Expand Down
33 changes: 33 additions & 0 deletions runtime/oti/util_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ alignedMemcpy(J9VMThread *vmStruct, void *dest, void *source, UDATA bytes, UDATA
void
alignedBackwardsMemcpy(J9VMThread *vmStruct, void *dest, void *source, UDATA bytes, UDATA alignment);

/* ---------------- annhelp.c ---------------- */

/**
* Check if a fieldref contains the specified Runtime Visible annotation. Fieldref
* must be resolved.
*
* @param currentThread Thread token
* @param clazz The class the field belongs to.
* @param cpIndex The constant pool index of the fieldref.
* @param annotationName The name of the annotation to check for.
* @return TRUE if the annotation is found, FALSE otherwise.
*/
BOOLEAN
fieldContainsRuntimeAnnotation(J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, J9UTF8 *annotationName);

#define J9VM_METHOD_CONTAINS_ANN_VIRTUAL 1
#define J9VM_METHOD_CONTAINS_ANN_STATIC 2
#define J9VM_METHOD_CONTAINS_ANN_SPECIAL 4
#define J9VM_METHOD_CONTAINS_ANN_INTERFACE 8

/**
* Check if a methodref contains the specified Runtime Visible annotation. Methodref
* must be resolved.
*
* @param currentThread Thread token
* @param clazz The class the method belongs to.
* @param cpIndex The constant pool index of the methodref.
* @param annotationName The name of the annotation to check for.
* @param type the type of method, static|virtual|special|interface
* @return TRUE if the annotation is found, FALSE otherwise.
*/
BOOLEAN
methodContainsRuntimeAnnotation(J9VMThread *currentThread, J9Class *clazz, UDATA cpIndex, J9UTF8 *annotationName, UDATA type);

/* ---------------- argbits.c ---------------- */

Expand Down
6 changes: 6 additions & 0 deletions runtime/oti/vm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,12 @@ putFlattenableField(J9VMThread *currentThread, J9RAMFieldRef *cpEntry, j9object_
void *
staticFieldAddress(J9VMThread *vmStruct, J9Class *clazz, U_8 *fieldName, UDATA fieldNameLength, U_8 *signature, UDATA signatureLength, J9Class **definingClass, UDATA *staticField, UDATA options, J9Class *sourceClass);

/**
*
*/
J9ROMFieldShape*
findFieldAndCheckVisibility (J9VMThread *currentThread, J9Class *clazz, U_8 *fieldName, UDATA fieldNameLength, U_8 *signature, UDATA signatureLength, J9Class **definingClass, UDATA *offsetOrAddress, UDATA options, J9Class *sourceClass);

/**
* @brief
* @param *vm: Reference to the VM, used to locate the table.
Expand Down
43 changes: 43 additions & 0 deletions runtime/tests/annotationtests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
################################################################################
# Copyright (c) 2021, 2021 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
################################################################################

set(OMR_ENHANCED_WARNINGS OFF)

j9vm_add_library(anntests SHARED
annhelp_tests.c
)

target_link_libraries(anntests
PRIVATE
j9vm_interface
jvm
)

omr_add_exports(annotationtests
Java_org_openj9_test_annotation_ContainsRuntimeAnnotationTest_containsRuntimeAnnotation
)

install(
TARGETS anntests
LIBRARY DESTINATION ${j9vm_SOURCE_DIR}
RUNTIME DESTINATION ${j9vm_SOURCE_DIR}
)
84 changes: 84 additions & 0 deletions runtime/tests/annotationtests/annhelp_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2021, 2021 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 "vmaccess.h"

/**
* Checks if a field or method contains the runtime annotation specified.
*
* @param env The JNI environment.
* @param jlClass The class that the fieldref or method belongs to.
* @param cpIndex The constant pool index of the fieldref or methodref.
* @param annotationNameString The name of the annotation to check for.
* @param isField True if checking for a field, false if checking a method.
* @return JNI_TRUE if the annotation is found, JNI_FALSE otherwise.
*/
jboolean JNICALL
Java_org_openj9_test_annotation_ContainsRuntimeAnnotationTest_containsRuntimeAnnotation(JNIEnv *env, jclass unused, jclass jlClass, jint cpIndex, jstring annotationNameString, jboolean isField, jint type)
{
jboolean result = JNI_FALSE;
J9VMThread *currentThread = (J9VMThread *)env;
J9JavaVM *vm = currentThread->javaVM;
J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;
PORT_ACCESS_FROM_JAVAVM(vm);

if (NULL == annotationNameString) {
vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, "annotation name is null");
} else {
vmFuncs->internalEnterVMFromJNI(currentThread);

j9object_t annotationNameObj = J9_JNI_UNWRAP_REFERENCE(annotationNameString);
char annotationNameStackBuffer[J9VM_PACKAGE_NAME_BUFFER_LENGTH] = {0};
J9UTF8 *annotationNameUTF8 = vmFuncs->copyStringToJ9UTF8WithMemAlloc(
currentThread,
annotationNameObj,
J9_STR_NULL_TERMINATE_RESULT,
"",
0,
annotationNameStackBuffer,
0);

if (NULL == annotationNameUTF8) {
vmFuncs->setNativeOutOfMemoryError(currentThread, 0, 0);
} else {
J9Class *clazz = J9VM_J9CLASS_FROM_HEAPCLASS(currentThread, J9_JNI_UNWRAP_REFERENCE(jlClass));

if (NULL == clazz) {
vmFuncs->setCurrentExceptionUTF(currentThread, J9VMCONSTANTPOOL_JAVALANGNULLPOINTEREXCEPTION, "class cannot be found");
} else {
if (isField) {
result = (jboolean)vmFuncs->fieldContainsRuntimeAnnotation(currentThread, clazz, (UDATA)cpIndex, annotationNameUTF8);
} else {
result = (jboolean)vmFuncs->methodContainsRuntimeAnnotation(currentThread, clazz, (UDATA)cpIndex, annotationNameUTF8, (UDATA)type);
}
}

if ((J9UTF8 *)annotationNameStackBuffer != annotationNameUTF8) {
j9mem_free_memory(annotationNameUTF8);
}
}

vmFuncs->internalExitVMToJNI(currentThread);
}

return result;
}
Loading

0 comments on commit ba6a26e

Please sign in to comment.