/*******************************************************************************1* Copyright (c) 2002, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122/**23* @brief This file contains implementations of the public JVM interface (JVM_ functions)24* which simply forward to a concrete implementation located either in the JCL library25* or proxy forwarder.26*/2728#include <stdlib.h>29#include <assert.h>3031#include "j9.h"32#include "omrgcconsts.h"33#include "j9modifiers_api.h"34#include "j9vm_internal.h"35#include "j9vmconstantpool.h"36#include "rommeth.h"37#include "sunvmi_api.h"38#include "util_api.h"39#include "ut_j9scar.h"40#include "j9vmnls.h"4142jbyteArray JNICALL43JVM_GetClassTypeAnnotations(JNIEnv *env, jclass jlClass) {44ENSURE_VMI();45return g_VMI->JVM_GetClassTypeAnnotations(env, jlClass);46}4748jbyteArray JNICALL49JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject jlrField) {50ENSURE_VMI();51return g_VMI->JVM_GetFieldTypeAnnotations(env, jlrField);52}5354jobjectArray JNICALL55JVM_GetMethodParameters(JNIEnv *env, jobject jlrExecutable) {56ENSURE_VMI();57return g_VMI->JVM_GetMethodParameters(env, jlrExecutable);58}5960jbyteArray JNICALL61JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject jlrMethod) {62ENSURE_VMI();63return g_VMI->JVM_GetMethodTypeAnnotations(env, jlrMethod);64}6566jboolean JNICALL67JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cb, jint index) {68assert(!"JVM_IsVMGeneratedMethodIx unimplemented"); /* Jazz 63527: Stub in APIs for Java 8 */69return FALSE;70}7172/**73* Returns platform specific temporary directory used by the system.74* Same as getTmpDir() defined in jcl/unix/syshelp.c and jcl/win32/syshelp.c.75*76* @param [in] env Pointer to JNI environment.77*78* @return String object representing the platform specific temporary directory.79*/80jstring JNICALL81JVM_GetTemporaryDirectory(JNIEnv *env)82{83PORT_ACCESS_FROM_ENV(env);84jstring result = NULL;85IDATA size = j9sysinfo_get_tmp(NULL, 0, TRUE);86if (0 <= size) {87char *buffer = (char *)j9mem_allocate_memory(size, OMRMEM_CATEGORY_VM);88if (NULL == buffer) {89return NULL;90}91if (0 == j9sysinfo_get_tmp(buffer, size, TRUE)) {92result = (*env)->NewStringUTF(env, buffer);93}9495j9mem_free_memory(buffer);96}9798return result;99}100101102/**103* Copies memory from one place to another, endian flipping the data.104*105* Implementation of native java.nio.Bits.copySwapMemory0(). The single java caller106* has ensured all of the parameters are valid.107*108* @param [in] env Pointer to JNI environment109* @param [in] srcObj Source primitive array (NULL means srcOffset represents native memory)110* @param [in] srcOffset Offset in source array / address in native memory111* @param [in] dstObj Destination primitive array (NULL means dstOffset represents native memory)112* @param [in] dstOffset Offset in destination array / address in native memory113* @param [in] size Number of bytes to copy114* @param [in] elemSize Size of elements to copy and flip115*116* elemSize = 2 means byte order 1,2 becomes 2,1117* elemSize = 4 means byte order 1,2,3,4 becomes 4,3,2,1118* elemSize = 8 means byte order 1,2,3,4,5,6,7,8 becomes 8,7,6,5,4,3,2,1119*/120void JNICALL121JVM_CopySwapMemory(JNIEnv *env, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)122{123U_8 *srcBytes = NULL;124U_8 *dstBytes = NULL;125U_8 *dstAddr = NULL;126if (NULL != srcObj) {127srcBytes = (*env)->GetPrimitiveArrayCritical(env, srcObj, NULL);128/* The java caller has added Unsafe.arrayBaseOffset() to the offset. Remove it129* here as GetPrimitiveArrayCritical returns a pointer to the first element.130*/131srcOffset -= J9VMTHREAD_CONTIGUOUS_HEADER_SIZE((J9VMThread*)env);132}133if (NULL != dstObj) {134dstBytes = (*env)->GetPrimitiveArrayCritical(env, dstObj, NULL);135dstAddr = dstBytes;136/* The java caller has added Unsafe.arrayBaseOffset() to the offset. Remove it137* here as GetPrimitiveArrayCritical returns a pointer to the first element.138*/139dstOffset -= J9VMTHREAD_CONTIGUOUS_HEADER_SIZE((J9VMThread*)env);140}141dstAddr += (UDATA)dstOffset;142/* First copy the bytes unmodified to the new location (memmove handles the overlap case) */143memmove(dstAddr, srcBytes + (UDATA)srcOffset, (size_t)size);144/* Now flip each element in the destination */145switch(elemSize) {146case 2: {147jlong elemCount = size / 2;148while (0 != elemCount) {149U_8 temp = dstAddr[0];150dstAddr[0] = dstAddr[1];151dstAddr[1] = temp;152dstAddr += 2;153elemCount -= 1;154}155break;156}157case 4: {158jlong elemCount = size / 4;159while (0 != elemCount) {160U_8 temp = dstAddr[0];161dstAddr[0] = dstAddr[3];162dstAddr[3] = temp;163temp = dstAddr[1];164dstAddr[1] = dstAddr[2];165dstAddr[2] = temp;166dstAddr += 4;167elemCount -= 1;168}169break;170}171default /* 8 */: {172jlong elemCount = size / 8;173while (0 != elemCount) {174U_8 temp = dstAddr[0];175dstAddr[0] = dstAddr[7];176dstAddr[7] = temp;177temp = dstAddr[1];178dstAddr[1] = dstAddr[6];179dstAddr[6] = temp;180temp = dstAddr[2];181dstAddr[2] = dstAddr[5];182dstAddr[5] = temp;183temp = dstAddr[3];184dstAddr[3] = dstAddr[4];185dstAddr[4] = temp;186dstAddr += 8;187elemCount -= 1;188}189break;190}191}192if (NULL != srcObj) {193(*env)->ReleasePrimitiveArrayCritical(env, srcObj, srcBytes, JNI_ABORT);194}195if (NULL != dstObj) {196(*env)->ReleasePrimitiveArrayCritical(env, dstObj, dstBytes, 0);197}198}199200201