src/share/vm/shark/sharkNativeWrapper.cpp

Tue, 07 May 2019 20:38:26 +0000

author
phh
date
Tue, 07 May 2019 20:38:26 +0000
changeset 9669
32bc598624bd
parent 4314
2cd5e15048e6
child 9703
2fdf635bcf28
permissions
-rw-r--r--

8176100: [REDO][REDO] G1 Needs pre barrier on dereference of weak JNI handles
Summary: Add tag bit to all JNI weak handles
Reviewed-by: kbarrett, coleenp, tschatzl

twisti@2047 1 /*
phh@9669 2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
twisti@2047 3 * Copyright 2009, 2010 Red Hat, Inc.
twisti@2047 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
twisti@2047 5 *
twisti@2047 6 * This code is free software; you can redistribute it and/or modify it
twisti@2047 7 * under the terms of the GNU General Public License version 2 only, as
twisti@2047 8 * published by the Free Software Foundation.
twisti@2047 9 *
twisti@2047 10 * This code is distributed in the hope that it will be useful, but WITHOUT
twisti@2047 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
twisti@2047 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
twisti@2047 13 * version 2 for more details (a copy is included in the LICENSE file that
twisti@2047 14 * accompanied this code).
twisti@2047 15 *
twisti@2047 16 * You should have received a copy of the GNU General Public License version
twisti@2047 17 * 2 along with this work; if not, write to the Free Software Foundation,
twisti@2047 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
twisti@2047 19 *
twisti@2047 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
twisti@2047 21 * or visit www.oracle.com if you need additional information or have any
twisti@2047 22 * questions.
twisti@2047 23 *
twisti@2047 24 */
twisti@2047 25
stefank@2314 26 #include "precompiled.hpp"
stefank@2314 27 #include "shark/llvmHeaders.hpp"
stefank@2314 28 #include "shark/sharkNativeWrapper.hpp"
stefank@2314 29 #include "shark/sharkType.hpp"
twisti@2047 30
twisti@2047 31 using namespace llvm;
twisti@2047 32
twisti@2047 33 void SharkNativeWrapper::initialize(const char *name) {
twisti@2047 34 // Create the function
twisti@2047 35 _function = Function::Create(
twisti@2047 36 SharkType::entry_point_type(),
twisti@2047 37 GlobalVariable::InternalLinkage,
twisti@2047 38 name);
twisti@2047 39
twisti@2047 40 // Get our arguments
twisti@2047 41 Function::arg_iterator ai = function()->arg_begin();
twisti@2047 42 Argument *method = ai++;
twisti@2047 43 method->setName("method");
twisti@2047 44 Argument *base_pc = ai++;
twisti@2047 45 base_pc->setName("base_pc");
twisti@2047 46 code_buffer()->set_base_pc(base_pc);
twisti@2047 47 Argument *thread = ai++;
twisti@2047 48 thread->setName("thread");
twisti@2047 49 set_thread(thread);
twisti@2047 50
twisti@2047 51 // Create and push our stack frame
twisti@2047 52 builder()->SetInsertPoint(CreateBlock());
phh@9669 53 #error Needs to be updated for tagged jweak; see JNIHandles.
twisti@2047 54 _stack = SharkStack::CreateBuildAndPushFrame(this, method);
twisti@2047 55 NOT_PRODUCT(method = NULL);
twisti@2047 56
twisti@2047 57 // Create the oopmap. We use the one oopmap for every call site in
twisti@2047 58 // the wrapper, which results in the odd mild inefficiency but is a
twisti@2047 59 // damn sight easier to code.
twisti@2047 60 OopMap *oopmap = new OopMap(
twisti@2047 61 SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),
twisti@2047 62 SharkStack::oopmap_slot_munge(arg_size()));
twisti@2047 63
twisti@2047 64 // Set up the oop_tmp slot if required:
twisti@2047 65 // - For static methods we use it to handlize the class argument
twisti@2047 66 // for the call, and to protect the same during slow path locks
twisti@2047 67 // (if synchronized).
twisti@2047 68 // - For methods returning oops, we use it to protect the return
twisti@2047 69 // value across safepoints or slow path unlocking.
twisti@2047 70 if (is_static() || is_returning_oop()) {
twisti@2047 71 _oop_tmp_slot = stack()->slot_addr(
twisti@2047 72 stack()->oop_tmp_slot_offset(),
twisti@2047 73 SharkType::oop_type(),
twisti@2047 74 "oop_tmp_slot");
twisti@2047 75
twisti@2047 76 oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));
twisti@2047 77 }
twisti@2047 78
twisti@2047 79 // Set up the monitor slot, for synchronized methods
twisti@2047 80 if (is_synchronized()) {
twisti@2047 81 Unimplemented();
twisti@2047 82 _lock_slot_offset = 23;
twisti@2047 83 }
twisti@2047 84
twisti@2047 85 // Start building the argument list
twisti@4314 86 std::vector<Type*> param_types;
twisti@2047 87 std::vector<Value*> param_values;
twisti@4314 88 PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());
twisti@2047 89
twisti@2047 90 // First argument is the JNIEnv
twisti@2047 91 param_types.push_back(SharkType::jniEnv_type());
twisti@2047 92 param_values.push_back(
twisti@2047 93 builder()->CreateAddressOfStructEntry(
twisti@2047 94 thread,
twisti@2047 95 JavaThread::jni_environment_offset(),
twisti@2047 96 SharkType::jniEnv_type(),
twisti@2047 97 "jni_environment"));
twisti@2047 98
twisti@2047 99 // For static methods, the second argument is the class
twisti@2047 100 if (is_static()) {
twisti@2047 101 builder()->CreateStore(
twisti@2047 102 builder()->CreateInlineOop(
twisti@2047 103 JNIHandles::make_local(
never@2658 104 target()->method_holder()->java_mirror())),
twisti@2047 105 oop_tmp_slot());
twisti@2047 106
twisti@2047 107 param_types.push_back(box_type);
twisti@2047 108 param_values.push_back(oop_tmp_slot());
twisti@2047 109
twisti@2047 110 _receiver_slot_offset = stack()->oop_tmp_slot_offset();
twisti@2047 111 }
twisti@2047 112 else if (is_returning_oop()) {
twisti@2047 113 // The oop_tmp slot is registered in the oopmap,
twisti@2047 114 // so we need to clear it. This is one of the
twisti@2047 115 // mild inefficiencies I mentioned earlier.
twisti@2047 116 builder()->CreateStore(LLVMValue::null(), oop_tmp_slot());
twisti@2047 117 }
twisti@2047 118
twisti@2047 119 // Parse the arguments
twisti@2047 120 for (int i = 0; i < arg_size(); i++) {
twisti@2047 121 int slot_offset = stack()->locals_slots_offset() + arg_size() - 1 - i;
twisti@2047 122 int adjusted_offset = slot_offset;
twisti@2047 123 BasicBlock *null, *not_null, *merge;
twisti@2047 124 Value *box;
twisti@2047 125 PHINode *phi;
twisti@2047 126
twisti@2047 127 switch (arg_type(i)) {
twisti@2047 128 case T_VOID:
twisti@2047 129 break;
twisti@2047 130
twisti@2047 131 case T_OBJECT:
twisti@2047 132 case T_ARRAY:
twisti@2047 133 null = CreateBlock("null");
twisti@2047 134 not_null = CreateBlock("not_null");
twisti@2047 135 merge = CreateBlock("merge");
twisti@2047 136
twisti@2047 137 box = stack()->slot_addr(slot_offset, SharkType::oop_type());
twisti@2047 138 builder()->CreateCondBr(
twisti@2047 139 builder()->CreateICmp(
twisti@2047 140 ICmpInst::ICMP_EQ,
twisti@2047 141 builder()->CreateLoad(box),
twisti@2047 142 LLVMValue::null()),
twisti@2047 143 null, not_null);
twisti@2047 144
twisti@2047 145 builder()->SetInsertPoint(null);
twisti@2047 146 builder()->CreateBr(merge);
twisti@2047 147
twisti@2047 148 builder()->SetInsertPoint(not_null);
twisti@2047 149 builder()->CreateBr(merge);
twisti@2047 150
twisti@2047 151 builder()->SetInsertPoint(merge);
twisti@4314 152 phi = builder()->CreatePHI(box_type, 0, "boxed_object");
twisti@2047 153 phi->addIncoming(ConstantPointerNull::get(box_type), null);
twisti@2047 154 phi->addIncoming(box, not_null);
twisti@2047 155 box = phi;
twisti@2047 156
twisti@2047 157 param_types.push_back(box_type);
twisti@2047 158 param_values.push_back(box);
twisti@2047 159
twisti@2047 160 oopmap->set_oop(SharkStack::slot2reg(slot_offset));
twisti@2047 161
twisti@2047 162 if (i == 0 && !is_static())
twisti@2047 163 _receiver_slot_offset = slot_offset;
twisti@2047 164
twisti@2047 165 break;
twisti@2047 166
twisti@2047 167 case T_LONG:
twisti@2047 168 case T_DOUBLE:
twisti@2047 169 adjusted_offset--;
twisti@2047 170 // fall through
twisti@2047 171
twisti@2047 172 default:
twisti@4314 173 Type *param_type = SharkType::to_stackType(arg_type(i));
twisti@2047 174
twisti@2047 175 param_types.push_back(param_type);
twisti@2047 176 param_values.push_back(
twisti@2047 177 builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));
twisti@2047 178 }
twisti@2047 179 }
twisti@2047 180
twisti@2047 181 // The oopmap is now complete, and everything is written
twisti@2047 182 // into the frame except the PC.
twisti@2047 183 int pc_offset = code_buffer()->create_unique_offset();
twisti@2047 184
twisti@2047 185 _oop_maps = new OopMapSet();
twisti@2047 186 oop_maps()->add_gc_map(pc_offset, oopmap);
twisti@2047 187
twisti@2047 188 builder()->CreateStore(
twisti@2047 189 builder()->code_buffer_address(pc_offset),
twisti@2047 190 stack()->slot_addr(stack()->pc_slot_offset()));
twisti@2047 191
twisti@2047 192 // Set up the Java frame anchor
twisti@2047 193 stack()->CreateSetLastJavaFrame();
twisti@2047 194
twisti@2047 195 // Lock if necessary
twisti@2047 196 if (is_synchronized())
twisti@2047 197 Unimplemented();
twisti@2047 198
twisti@2047 199 // Change the thread state to _thread_in_native
twisti@2047 200 CreateSetThreadState(_thread_in_native);
twisti@2047 201
twisti@2047 202 // Make the call
twisti@2047 203 BasicType result_type = target()->result_type();
twisti@4314 204 Type* return_type;
twisti@2047 205 if (result_type == T_VOID)
twisti@2047 206 return_type = SharkType::void_type();
twisti@2047 207 else if (is_returning_oop())
twisti@2047 208 return_type = box_type;
twisti@2047 209 else
twisti@2047 210 return_type = SharkType::to_arrayType(result_type);
twisti@2047 211 Value* native_function = builder()->CreateIntToPtr(
twisti@2047 212 LLVMValue::intptr_constant((intptr_t) target()->native_function()),
twisti@2047 213 PointerType::getUnqual(
twisti@2047 214 FunctionType::get(return_type, param_types, false)));
twisti@2047 215 Value *result = builder()->CreateCall(
twisti@4314 216 native_function, llvm::makeArrayRef(param_values));
twisti@2047 217
twisti@2047 218 // Start the transition back to _thread_in_Java
twisti@2047 219 CreateSetThreadState(_thread_in_native_trans);
twisti@2047 220
twisti@2047 221 // Make sure new state is visible in the GC thread
twisti@2047 222 if (os::is_MP()) {
twisti@2047 223 if (UseMembar)
twisti@4314 224 builder()->CreateFence(llvm::SequentiallyConsistent, llvm::CrossThread);
twisti@2047 225 else
twisti@2047 226 CreateWriteMemorySerializePage();
twisti@2047 227 }
twisti@2047 228
twisti@2047 229 // Handle safepoint operations, pending suspend requests,
twisti@2047 230 // and pending asynchronous exceptions.
twisti@2047 231 BasicBlock *check_thread = CreateBlock("check_thread");
twisti@2047 232 BasicBlock *do_safepoint = CreateBlock("do_safepoint");
twisti@2047 233 BasicBlock *safepointed = CreateBlock("safepointed");
twisti@2047 234
twisti@2047 235 Value *global_state = builder()->CreateLoad(
twisti@2047 236 builder()->CreateIntToPtr(
twisti@2047 237 LLVMValue::intptr_constant(
twisti@2047 238 (intptr_t) SafepointSynchronize::address_of_state()),
twisti@2047 239 PointerType::getUnqual(SharkType::jint_type())),
twisti@2047 240 "global_state");
twisti@2047 241
twisti@2047 242 builder()->CreateCondBr(
twisti@2047 243 builder()->CreateICmpNE(
twisti@2047 244 global_state,
twisti@2047 245 LLVMValue::jint_constant(SafepointSynchronize::_not_synchronized)),
twisti@2047 246 do_safepoint, check_thread);
twisti@2047 247
twisti@2047 248 builder()->SetInsertPoint(check_thread);
twisti@2047 249 Value *thread_state = builder()->CreateValueOfStructEntry(
twisti@2047 250 thread,
twisti@2047 251 JavaThread::suspend_flags_offset(),
twisti@2047 252 SharkType::jint_type(),
twisti@2047 253 "thread_state");
twisti@2047 254
twisti@2047 255 builder()->CreateCondBr(
twisti@2047 256 builder()->CreateICmpNE(
twisti@2047 257 thread_state,
twisti@2047 258 LLVMValue::jint_constant(0)),
twisti@2047 259 do_safepoint, safepointed);
twisti@2047 260
twisti@2047 261 builder()->SetInsertPoint(do_safepoint);
twisti@2047 262 builder()->CreateCall(
twisti@2047 263 builder()->check_special_condition_for_native_trans(), thread);
twisti@2047 264 builder()->CreateBr(safepointed);
twisti@2047 265
twisti@2047 266 // Finally we can change the thread state to _thread_in_Java
twisti@2047 267 builder()->SetInsertPoint(safepointed);
twisti@2047 268 CreateSetThreadState(_thread_in_Java);
twisti@2047 269
twisti@2047 270 // Clear the frame anchor
twisti@2047 271 stack()->CreateResetLastJavaFrame();
twisti@2047 272
twisti@2047 273 // If there is a pending exception then we can just unwind and
twisti@2047 274 // return. It seems totally wrong that unlocking is skipped here
twisti@2047 275 // but apparently the template interpreter does this so we do too.
twisti@2047 276 BasicBlock *exception = CreateBlock("exception");
twisti@2047 277 BasicBlock *no_exception = CreateBlock("no_exception");
twisti@2047 278
twisti@2047 279 builder()->CreateCondBr(
twisti@2047 280 builder()->CreateICmpEQ(
twisti@2047 281 CreateLoadPendingException(),
twisti@2047 282 LLVMValue::null()),
twisti@2047 283 no_exception, exception);
twisti@2047 284
twisti@2047 285 builder()->SetInsertPoint(exception);
twisti@2047 286 CreateResetHandleBlock();
twisti@2047 287 stack()->CreatePopFrame(0);
twisti@2047 288 builder()->CreateRet(LLVMValue::jint_constant(0));
twisti@2047 289
twisti@2047 290 builder()->SetInsertPoint(no_exception);
twisti@2047 291
twisti@2047 292 // If the result was an oop then unbox it before
twisti@2047 293 // releasing the handle it might be protected by
twisti@2047 294 if (is_returning_oop()) {
twisti@2047 295 BasicBlock *null = builder()->GetInsertBlock();
twisti@2047 296 BasicBlock *not_null = CreateBlock("not_null");
twisti@2047 297 BasicBlock *merge = CreateBlock("merge");
twisti@2047 298
twisti@2047 299 builder()->CreateCondBr(
twisti@2047 300 builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),
twisti@2047 301 not_null, merge);
twisti@2047 302
twisti@2047 303 builder()->SetInsertPoint(not_null);
twisti@2047 304 Value *unboxed_result = builder()->CreateLoad(result);
twisti@2047 305 builder()->CreateBr(merge);
twisti@2047 306
twisti@2047 307 builder()->SetInsertPoint(merge);
twisti@4314 308 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "result");
twisti@2047 309 phi->addIncoming(LLVMValue::null(), null);
twisti@2047 310 phi->addIncoming(unboxed_result, not_null);
twisti@2047 311 result = phi;
twisti@2047 312 }
twisti@2047 313
twisti@2047 314 // Reset handle block
twisti@2047 315 CreateResetHandleBlock();
twisti@2047 316
twisti@2047 317 // Unlock if necessary.
twisti@2047 318 if (is_synchronized())
twisti@2047 319 Unimplemented();
twisti@2047 320
twisti@2047 321 // Unwind and return
twisti@2047 322 Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);
twisti@2047 323 if (result_type != T_VOID) {
twisti@2047 324 bool needs_cast = false;
twisti@2047 325 bool is_signed = false;
twisti@2047 326 switch (result_type) {
twisti@2047 327 case T_BOOLEAN:
twisti@2047 328 result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));
twisti@2047 329 needs_cast = true;
twisti@2047 330 break;
twisti@2047 331
twisti@2047 332 case T_CHAR:
twisti@2047 333 needs_cast = true;
twisti@2047 334 break;
twisti@2047 335
twisti@2047 336 case T_BYTE:
twisti@2047 337 case T_SHORT:
twisti@2047 338 needs_cast = true;
twisti@2047 339 is_signed = true;
twisti@2047 340 break;
twisti@2047 341 }
twisti@2047 342 if (needs_cast) {
twisti@2047 343 result = builder()->CreateIntCast(
twisti@2047 344 result, SharkType::to_stackType(result_type), is_signed);
twisti@2047 345 }
twisti@2047 346
twisti@2047 347 builder()->CreateStore(
twisti@2047 348 result,
twisti@2047 349 builder()->CreateIntToPtr(
twisti@2047 350 result_addr,
twisti@2047 351 PointerType::getUnqual(SharkType::to_stackType(result_type))));
twisti@2047 352 }
twisti@2047 353 builder()->CreateRet(LLVMValue::jint_constant(0));
twisti@2047 354 }

mercurial