1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "jni_compiler.h"
18
19 #include <algorithm>
20 #include <fstream>
21 #include <ios>
22 #include <memory>
23 #include <vector>
24
25 #include "art_method.h"
26 #include "base/arena_allocator.h"
27 #include "base/arena_containers.h"
28 #include "base/enums.h"
29 #include "base/logging.h" // For VLOG.
30 #include "base/macros.h"
31 #include "base/malloc_arena_pool.h"
32 #include "base/memory_region.h"
33 #include "base/utils.h"
34 #include "calling_convention.h"
35 #include "class_linker.h"
36 #include "dwarf/debug_frame_opcode_writer.h"
37 #include "dex/dex_file-inl.h"
38 #include "driver/compiler_options.h"
39 #include "entrypoints/quick/quick_entrypoints.h"
40 #include "jni/jni_env_ext.h"
41 #include "thread.h"
42 #include "utils/arm/managed_register_arm.h"
43 #include "utils/arm64/managed_register_arm64.h"
44 #include "utils/assembler.h"
45 #include "utils/jni_macro_assembler.h"
46 #include "utils/managed_register.h"
47 #include "utils/x86/managed_register_x86.h"
48
49 #define __ jni_asm->
50
51 namespace art {
52
53 template <PointerSize kPointerSize>
54 static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
55 ManagedRuntimeCallingConvention* mr_conv,
56 JniCallingConvention* jni_conv);
57 template <PointerSize kPointerSize>
58 static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
59 JniCallingConvention* jni_conv,
60 ManagedRegister in_reg);
61
62 template <PointerSize kPointerSize>
GetMacroAssembler(ArenaAllocator * allocator,InstructionSet isa,const InstructionSetFeatures * features)63 static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
64 ArenaAllocator* allocator, InstructionSet isa, const InstructionSetFeatures* features) {
65 return JNIMacroAssembler<kPointerSize>::Create(allocator, isa, features);
66 }
67
68 enum class JniEntrypoint {
69 kStart,
70 kEnd
71 };
72
73 template <PointerSize kPointerSize>
GetJniEntrypointThreadOffset(JniEntrypoint which,bool reference_return,bool is_synchronized,bool is_fast_native)74 static ThreadOffset<kPointerSize> GetJniEntrypointThreadOffset(JniEntrypoint which,
75 bool reference_return,
76 bool is_synchronized,
77 bool is_fast_native) {
78 if (which == JniEntrypoint::kStart) { // JniMethodStart
79 ThreadOffset<kPointerSize> jni_start =
80 is_synchronized
81 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
82 : (is_fast_native
83 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart)
84 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
85
86 return jni_start;
87 } else { // JniMethodEnd
88 ThreadOffset<kPointerSize> jni_end(-1);
89 if (reference_return) {
90 // Pass result.
91 jni_end = is_synchronized
92 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
93 : (is_fast_native
94 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEndWithReference)
95 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference));
96 } else {
97 jni_end = is_synchronized
98 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
99 : (is_fast_native
100 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd)
101 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
102 }
103
104 return jni_end;
105 }
106 }
107
108
109 // Generate the JNI bridge for the given method, general contract:
110 // - Arguments are in the managed runtime format, either on stack or in
111 // registers, a reference to the method object is supplied as part of this
112 // convention.
113 //
114 template <PointerSize kPointerSize>
ArtJniCompileMethodInternal(const CompilerOptions & compiler_options,uint32_t access_flags,uint32_t method_idx,const DexFile & dex_file)115 static JniCompiledMethod ArtJniCompileMethodInternal(const CompilerOptions& compiler_options,
116 uint32_t access_flags,
117 uint32_t method_idx,
118 const DexFile& dex_file) {
119 const bool is_native = (access_flags & kAccNative) != 0;
120 CHECK(is_native);
121 const bool is_static = (access_flags & kAccStatic) != 0;
122 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
123 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
124 InstructionSet instruction_set = compiler_options.GetInstructionSet();
125 const InstructionSetFeatures* instruction_set_features =
126 compiler_options.GetInstructionSetFeatures();
127
128 // i.e. if the method was annotated with @FastNative
129 const bool is_fast_native = (access_flags & kAccFastNative) != 0u;
130
131 // i.e. if the method was annotated with @CriticalNative
132 const bool is_critical_native = (access_flags & kAccCriticalNative) != 0u;
133
134 VLOG(jni) << "JniCompile: Method :: "
135 << dex_file.PrettyMethod(method_idx, /* with signature */ true)
136 << " :: access_flags = " << std::hex << access_flags << std::dec;
137
138 if (UNLIKELY(is_fast_native)) {
139 VLOG(jni) << "JniCompile: Fast native method detected :: "
140 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
141 }
142
143 if (UNLIKELY(is_critical_native)) {
144 VLOG(jni) << "JniCompile: Critical native method detected :: "
145 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
146 }
147
148 if (kIsDebugBuild) {
149 // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive.
150 if (UNLIKELY(is_fast_native && is_critical_native)) {
151 LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative"
152 << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
153 }
154
155 // @CriticalNative - extra checks:
156 // -- Don't allow virtual criticals
157 // -- Don't allow synchronized criticals
158 // -- Don't allow any objects as parameter or return value
159 if (UNLIKELY(is_critical_native)) {
160 CHECK(is_static)
161 << "@CriticalNative functions cannot be virtual since that would"
162 << "require passing a reference parameter (this), which is illegal "
163 << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
164 CHECK(!is_synchronized)
165 << "@CriticalNative functions cannot be synchronized since that would"
166 << "require passing a (class and/or this) reference parameter, which is illegal "
167 << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
168 for (size_t i = 0; i < strlen(shorty); ++i) {
169 CHECK_NE(Primitive::kPrimNot, Primitive::GetType(shorty[i]))
170 << "@CriticalNative methods' shorty types must not have illegal references "
171 << dex_file.PrettyMethod(method_idx, /* with_signature= */ true);
172 }
173 }
174 }
175
176 MallocArenaPool pool;
177 ArenaAllocator allocator(&pool);
178
179 // Calling conventions used to iterate over parameters to method
180 std::unique_ptr<JniCallingConvention> main_jni_conv =
181 JniCallingConvention::Create(&allocator,
182 is_static,
183 is_synchronized,
184 is_critical_native,
185 shorty,
186 instruction_set);
187 bool reference_return = main_jni_conv->IsReturnAReference();
188
189 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
190 ManagedRuntimeCallingConvention::Create(
191 &allocator, is_static, is_synchronized, shorty, instruction_set));
192
193 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
194 // method and the current thread.
195 const char* jni_end_shorty;
196 if (reference_return && is_synchronized) {
197 jni_end_shorty = "ILL";
198 } else if (reference_return) {
199 jni_end_shorty = "IL";
200 } else if (is_synchronized) {
201 jni_end_shorty = "VL";
202 } else {
203 jni_end_shorty = "V";
204 }
205
206 std::unique_ptr<JniCallingConvention> end_jni_conv(
207 JniCallingConvention::Create(&allocator,
208 is_static,
209 is_synchronized,
210 is_critical_native,
211 jni_end_shorty,
212 instruction_set));
213
214 // Assembler that holds generated instructions
215 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
216 GetMacroAssembler<kPointerSize>(&allocator, instruction_set, instruction_set_features);
217 jni_asm->cfi().SetEnabled(compiler_options.GenerateAnyDebugInfo());
218 jni_asm->SetEmitRunTimeChecksInDebugMode(compiler_options.EmitRunTimeChecksInDebugMode());
219
220 // 1. Build the frame saving all callee saves, Method*, and PC return address.
221 // For @CriticalNative, this includes space for out args, otherwise just the managed frame.
222 const size_t managed_frame_size = main_jni_conv->FrameSize();
223 const size_t main_out_arg_size = main_jni_conv->OutFrameSize();
224 size_t current_frame_size = is_critical_native ? main_out_arg_size : managed_frame_size;
225 ManagedRegister method_register =
226 is_critical_native ? ManagedRegister::NoRegister() : mr_conv->MethodRegister();
227 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
228 __ BuildFrame(current_frame_size, method_register, callee_save_regs);
229 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
230
231 if (LIKELY(!is_critical_native)) {
232 // Spill all register arguments.
233 // TODO: Spill reference args directly to the HandleScope.
234 // TODO: Spill native stack args straight to their stack locations (adjust SP earlier).
235 mr_conv->ResetIterator(FrameOffset(current_frame_size));
236 for (; mr_conv->HasNext(); mr_conv->Next()) {
237 if (mr_conv->IsCurrentParamInRegister()) {
238 size_t size = mr_conv->IsCurrentParamALongOrDouble() ? 8u : 4u;
239 __ Store(mr_conv->CurrentParamStackOffset(), mr_conv->CurrentParamRegister(), size);
240 }
241 }
242
243 // NOTE: @CriticalNative methods don't have a HandleScope
244 // because they can't have any reference parameters or return values.
245
246 // 2. Set up the HandleScope
247 mr_conv->ResetIterator(FrameOffset(current_frame_size));
248 main_jni_conv->ResetIterator(FrameOffset(0));
249 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
250 main_jni_conv->ReferenceCount());
251
252 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
253 Thread::TopHandleScopeOffset<kPointerSize>());
254 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
255 main_jni_conv->HandleScopeOffset());
256
257 // 3. Place incoming reference arguments into handle scope
258 main_jni_conv->Next(); // Skip JNIEnv*
259 // 3.5. Create Class argument for static methods out of passed method
260 if (is_static) {
261 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
262 // Check handle scope offset is within frame
263 CHECK_LT(handle_scope_offset.Uint32Value(), current_frame_size);
264 // Note: This CopyRef() doesn't need heap unpoisoning since it's from the ArtMethod.
265 // Note: This CopyRef() does not include read barrier. It will be handled below.
266 __ CopyRef(handle_scope_offset,
267 mr_conv->MethodRegister(),
268 ArtMethod::DeclaringClassOffset(),
269 /* unpoison_reference= */ false);
270 main_jni_conv->Next(); // in handle scope so move to next argument
271 }
272 // Place every reference into the handle scope (ignore other parameters).
273 while (mr_conv->HasNext()) {
274 CHECK(main_jni_conv->HasNext());
275 bool ref_param = main_jni_conv->IsCurrentParamAReference();
276 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
277 // References need placing in handle scope and the entry value passing
278 if (ref_param) {
279 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
280 // must be null.
281 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
282 // Check handle scope offset is within frame and doesn't run into the saved segment state.
283 CHECK_LT(handle_scope_offset.Uint32Value(), current_frame_size);
284 CHECK_NE(handle_scope_offset.Uint32Value(),
285 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
286 // We spilled all registers above, so use stack locations.
287 // TODO: Spill refs straight to the HandleScope.
288 bool input_in_reg = false; // mr_conv->IsCurrentParamInRegister();
289 bool input_on_stack = true; // mr_conv->IsCurrentParamOnStack();
290 CHECK(input_in_reg || input_on_stack);
291
292 if (input_in_reg) {
293 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
294 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
295 __ StoreRef(handle_scope_offset, in_reg);
296 } else if (input_on_stack) {
297 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
298 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
299 __ CopyRef(handle_scope_offset, in_off);
300 }
301 }
302 mr_conv->Next();
303 main_jni_conv->Next();
304 }
305
306 // 4. Write out the end of the quick frames.
307 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
308
309 // NOTE: @CriticalNative does not need to store the stack pointer to the thread
310 // because garbage collections are disabled within the execution of a
311 // @CriticalNative method.
312 // (TODO: We could probably disable it for @FastNative too).
313 } // if (!is_critical_native)
314
315 // 5. Move frame down to allow space for out going args.
316 size_t current_out_arg_size = main_out_arg_size;
317 if (UNLIKELY(is_critical_native)) {
318 DCHECK_EQ(main_out_arg_size, current_frame_size);
319 } else {
320 __ IncreaseFrameSize(main_out_arg_size);
321 current_frame_size += main_out_arg_size;
322 }
323
324 // Call the read barrier for the declaring class loaded from the method for a static call.
325 // Skip this for @CriticalNative because we didn't build a HandleScope to begin with.
326 // Note that we always have outgoing param space available for at least two params.
327 if (kUseReadBarrier && is_static && !is_critical_native) {
328 const bool kReadBarrierFastPath = true; // Always true after Mips codegen was removed.
329 std::unique_ptr<JNIMacroLabel> skip_cold_path_label;
330 if (kReadBarrierFastPath) {
331 skip_cold_path_label = __ CreateLabel();
332 // Fast path for supported targets.
333 //
334 // Check if gc_is_marking is set -- if it's not, we don't need
335 // a read barrier so skip it.
336 // Jump over the slow path if gc is marking is false.
337 __ TestGcMarking(skip_cold_path_label.get(), JNIMacroUnaryCondition::kZero);
338 }
339
340 // Construct slow path for read barrier:
341 //
342 // Call into the runtime's ReadBarrierJni and have it fix up
343 // the object address if it was moved.
344
345 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
346 pReadBarrierJni);
347 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
348 main_jni_conv->Next(); // Skip JNIEnv.
349 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
350 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
351 // Pass the handle for the class as the first argument.
352 if (main_jni_conv->IsCurrentParamOnStack()) {
353 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
354 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset, /*null_allowed=*/ false);
355 } else {
356 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
357 __ CreateHandleScopeEntry(out_reg,
358 class_handle_scope_offset,
359 ManagedRegister::NoRegister(),
360 /*null_allowed=*/ false);
361 }
362 main_jni_conv->Next();
363 // Pass the current thread as the second argument and call.
364 if (main_jni_conv->IsCurrentParamInRegister()) {
365 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
366 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier));
367 } else {
368 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset());
369 __ CallFromThread(read_barrier);
370 }
371 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
372
373 if (kReadBarrierFastPath) {
374 __ Bind(skip_cold_path_label.get());
375 }
376 }
377
378 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
379 // can occur. The result is the saved JNI local state that is restored by the exit call. We
380 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
381 // arguments.
382 FrameOffset locked_object_handle_scope_offset(0xBEEFDEAD);
383 FrameOffset saved_cookie_offset(
384 FrameOffset(0xDEADBEEFu)); // @CriticalNative - use obviously bad value for debugging
385 if (LIKELY(!is_critical_native)) {
386 // Skip this for @CriticalNative methods. They do not call JniMethodStart.
387 ThreadOffset<kPointerSize> jni_start(
388 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kStart,
389 reference_return,
390 is_synchronized,
391 is_fast_native).SizeValue());
392 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
393 locked_object_handle_scope_offset = FrameOffset(0);
394 if (is_synchronized) {
395 // Pass object for locking.
396 main_jni_conv->Next(); // Skip JNIEnv.
397 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
398 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
399 if (main_jni_conv->IsCurrentParamOnStack()) {
400 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
401 __ CreateHandleScopeEntry(out_off,
402 locked_object_handle_scope_offset,
403 /*null_allowed=*/ false);
404 } else {
405 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
406 __ CreateHandleScopeEntry(out_reg,
407 locked_object_handle_scope_offset,
408 ManagedRegister::NoRegister(),
409 /*null_allowed=*/ false);
410 }
411 main_jni_conv->Next();
412 }
413 if (main_jni_conv->IsCurrentParamInRegister()) {
414 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
415 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start));
416 } else {
417 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset());
418 __ CallFromThread(jni_start);
419 }
420 if (is_synchronized) { // Check for exceptions from monitor enter.
421 __ ExceptionPoll(main_out_arg_size);
422 }
423
424 // Store into stack_frame[saved_cookie_offset] the return value of JniMethodStart.
425 saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
426 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4 /* sizeof cookie */);
427 }
428
429 // 7. Fill arguments.
430 if (UNLIKELY(is_critical_native)) {
431 ArenaVector<ArgumentLocation> src_args(allocator.Adapter());
432 ArenaVector<ArgumentLocation> dest_args(allocator.Adapter());
433 // Move the method pointer to the hidden argument register.
434 size_t pointer_size = static_cast<size_t>(kPointerSize);
435 dest_args.push_back(ArgumentLocation(main_jni_conv->HiddenArgumentRegister(), pointer_size));
436 src_args.push_back(ArgumentLocation(mr_conv->MethodRegister(), pointer_size));
437 // Move normal arguments to their locations.
438 mr_conv->ResetIterator(FrameOffset(current_frame_size));
439 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
440 for (; mr_conv->HasNext(); mr_conv->Next(), main_jni_conv->Next()) {
441 DCHECK(main_jni_conv->HasNext());
442 size_t size = mr_conv->IsCurrentParamALongOrDouble() ? 8u : 4u;
443 src_args.push_back(mr_conv->IsCurrentParamInRegister()
444 ? ArgumentLocation(mr_conv->CurrentParamRegister(), size)
445 : ArgumentLocation(mr_conv->CurrentParamStackOffset(), size));
446 dest_args.push_back(main_jni_conv->IsCurrentParamInRegister()
447 ? ArgumentLocation(main_jni_conv->CurrentParamRegister(), size)
448 : ArgumentLocation(main_jni_conv->CurrentParamStackOffset(), size));
449 }
450 DCHECK(!main_jni_conv->HasNext());
451 __ MoveArguments(ArrayRef<ArgumentLocation>(dest_args), ArrayRef<ArgumentLocation>(src_args));
452 } else {
453 // Iterate over arguments placing values from managed calling convention in
454 // to the convention required for a native call (shuffling). For references
455 // place an index/pointer to the reference after checking whether it is
456 // null (which must be encoded as null).
457 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
458 // give as many free registers for the shuffle as possible.
459 mr_conv->ResetIterator(FrameOffset(current_frame_size));
460 uint32_t args_count = 0;
461 while (mr_conv->HasNext()) {
462 args_count++;
463 mr_conv->Next();
464 }
465
466 // Do a backward pass over arguments, so that the generated code will be "mov
467 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
468 // TODO: A reverse iterator to improve readability.
469 // TODO: This is currently useless as all archs spill args when building the frame.
470 // To avoid the full spilling, we would have to do one pass before the BuildFrame()
471 // to determine which arg registers are clobbered before they are needed.
472 for (uint32_t i = 0; i < args_count; ++i) {
473 mr_conv->ResetIterator(FrameOffset(current_frame_size));
474 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
475
476 // Skip the extra JNI parameters for now.
477 main_jni_conv->Next(); // Skip JNIEnv*.
478 if (is_static) {
479 main_jni_conv->Next(); // Skip Class for now.
480 }
481 // Skip to the argument we're interested in.
482 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
483 mr_conv->Next();
484 main_jni_conv->Next();
485 }
486 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get());
487 }
488 if (is_static) {
489 // Create argument for Class
490 mr_conv->ResetIterator(FrameOffset(current_frame_size));
491 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
492 main_jni_conv->Next(); // Skip JNIEnv*
493 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
494 if (main_jni_conv->IsCurrentParamOnStack()) {
495 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
496 __ CreateHandleScopeEntry(out_off, handle_scope_offset, /*null_allowed=*/ false);
497 } else {
498 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
499 __ CreateHandleScopeEntry(out_reg,
500 handle_scope_offset,
501 ManagedRegister::NoRegister(),
502 /*null_allowed=*/ false);
503 }
504 }
505
506 // Set the iterator back to the incoming Method*.
507 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
508
509 // 8. Create 1st argument, the JNI environment ptr.
510 // Register that will hold local indirect reference table
511 if (main_jni_conv->IsCurrentParamInRegister()) {
512 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
513 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
514 } else {
515 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
516 __ CopyRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
517 }
518 }
519
520 // 9. Plant call to native code associated with method.
521 MemberOffset jni_entrypoint_offset =
522 ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set));
523 if (UNLIKELY(is_critical_native)) {
524 if (main_jni_conv->UseTailCall()) {
525 __ Jump(main_jni_conv->HiddenArgumentRegister(), jni_entrypoint_offset);
526 } else {
527 __ Call(main_jni_conv->HiddenArgumentRegister(), jni_entrypoint_offset);
528 }
529 } else {
530 __ Call(FrameOffset(main_out_arg_size + mr_conv->MethodStackOffset().SizeValue()),
531 jni_entrypoint_offset);
532 }
533
534 // 10. Fix differences in result widths.
535 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
536 DCHECK(main_jni_conv->HasSmallReturnType());
537 CHECK(!is_critical_native || !main_jni_conv->UseTailCall());
538 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
539 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
540 __ SignExtend(main_jni_conv->ReturnRegister(),
541 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
542 } else {
543 CHECK(main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
544 main_jni_conv->GetReturnType() == Primitive::kPrimChar);
545 __ ZeroExtend(main_jni_conv->ReturnRegister(),
546 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
547 }
548 }
549
550 // 11. Process return value
551 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
552 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
553 if (LIKELY(!is_critical_native)) {
554 // For normal JNI, store the return value on the stack because the call to
555 // JniMethodEnd will clobber the return value. It will be restored in (13).
556 CHECK_LT(return_save_location.Uint32Value(), current_frame_size);
557 __ Store(return_save_location,
558 main_jni_conv->ReturnRegister(),
559 main_jni_conv->SizeOfReturnValue());
560 } else {
561 // For @CriticalNative only,
562 // move the JNI return register into the managed return register (if they don't match).
563 ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister();
564 ManagedRegister mr_return_reg = mr_conv->ReturnRegister();
565
566 // Check if the JNI return register matches the managed return register.
567 // If they differ, only then do we have to do anything about it.
568 // Otherwise the return value is already in the right place when we return.
569 if (!jni_return_reg.Equals(mr_return_reg)) {
570 CHECK(!main_jni_conv->UseTailCall());
571 // This is typically only necessary on ARM32 due to native being softfloat
572 // while managed is hardfloat.
573 // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0.
574 __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue());
575 } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) {
576 // Check that if the return value is passed on the stack for some reason,
577 // that the size matches.
578 CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue());
579 }
580 }
581 }
582
583 if (LIKELY(!is_critical_native)) {
584 // Increase frame size for out args if needed by the end_jni_conv.
585 const size_t end_out_arg_size = end_jni_conv->OutFrameSize();
586 if (end_out_arg_size > current_out_arg_size) {
587 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
588 current_out_arg_size = end_out_arg_size;
589 __ IncreaseFrameSize(out_arg_size_diff);
590 current_frame_size += out_arg_size_diff;
591 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
592 locked_object_handle_scope_offset =
593 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
594 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
595 }
596 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
597
598 // 12. Call JniMethodEnd
599 ThreadOffset<kPointerSize> jni_end(
600 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kEnd,
601 reference_return,
602 is_synchronized,
603 is_fast_native).SizeValue());
604 if (reference_return) {
605 // Pass result.
606 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
607 end_jni_conv->Next();
608 }
609 // Pass saved local reference state.
610 if (end_jni_conv->IsCurrentParamOnStack()) {
611 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
612 __ Copy(out_off, saved_cookie_offset, 4);
613 } else {
614 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
615 __ Load(out_reg, saved_cookie_offset, 4);
616 }
617 end_jni_conv->Next();
618 if (is_synchronized) {
619 // Pass object for unlocking.
620 if (end_jni_conv->IsCurrentParamOnStack()) {
621 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
622 __ CreateHandleScopeEntry(out_off,
623 locked_object_handle_scope_offset,
624 /*null_allowed=*/ false);
625 } else {
626 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
627 __ CreateHandleScopeEntry(out_reg,
628 locked_object_handle_scope_offset,
629 ManagedRegister::NoRegister(),
630 /*null_allowed=*/ false);
631 }
632 end_jni_conv->Next();
633 }
634 if (end_jni_conv->IsCurrentParamInRegister()) {
635 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
636 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end));
637 } else {
638 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset());
639 __ CallFromThread(jni_end);
640 }
641
642 // 13. Reload return value
643 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
644 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
645 // NIT: If it's @CriticalNative then we actually only need to do this IF
646 // the calling convention's native return register doesn't match the managed convention's
647 // return register.
648 }
649 } // if (!is_critical_native)
650
651 // 14. Move frame up now we're done with the out arg space.
652 // @CriticalNative remove out args together with the frame in RemoveFrame().
653 if (LIKELY(!is_critical_native)) {
654 __ DecreaseFrameSize(current_out_arg_size);
655 current_frame_size -= current_out_arg_size;
656 }
657
658 // 15. Process pending exceptions from JNI call or monitor exit.
659 // @CriticalNative methods do not need exception poll in the stub.
660 if (LIKELY(!is_critical_native)) {
661 __ ExceptionPoll(/* stack_adjust= */ 0);
662 }
663
664 // 16. Remove activation - need to restore callee save registers since the GC may have changed
665 // them.
666 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
667 if (LIKELY(!is_critical_native) || !main_jni_conv->UseTailCall()) {
668 // We expect the compiled method to possibly be suspended during its
669 // execution, except in the case of a CriticalNative method.
670 bool may_suspend = !is_critical_native;
671 __ RemoveFrame(current_frame_size, callee_save_regs, may_suspend);
672 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
673 }
674
675 // 17. Finalize code generation
676 __ FinalizeCode();
677 size_t cs = __ CodeSize();
678 std::vector<uint8_t> managed_code(cs);
679 MemoryRegion code(&managed_code[0], managed_code.size());
680 __ FinalizeInstructions(code);
681
682 return JniCompiledMethod(instruction_set,
683 std::move(managed_code),
684 managed_frame_size,
685 main_jni_conv->CoreSpillMask(),
686 main_jni_conv->FpSpillMask(),
687 ArrayRef<const uint8_t>(*jni_asm->cfi().data()));
688 }
689
690 // Copy a single parameter from the managed to the JNI calling convention.
691 template <PointerSize kPointerSize>
CopyParameter(JNIMacroAssembler<kPointerSize> * jni_asm,ManagedRuntimeCallingConvention * mr_conv,JniCallingConvention * jni_conv)692 static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
693 ManagedRuntimeCallingConvention* mr_conv,
694 JniCallingConvention* jni_conv) {
695 // We spilled all registers, so use stack locations.
696 // TODO: Move args in registers for @CriticalNative.
697 bool input_in_reg = false; // mr_conv->IsCurrentParamInRegister();
698 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
699 FrameOffset handle_scope_offset(0);
700 bool null_allowed = false;
701 bool ref_param = jni_conv->IsCurrentParamAReference();
702 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
703 if (output_in_reg) { // output shouldn't straddle registers and stack
704 CHECK(!jni_conv->IsCurrentParamOnStack());
705 } else {
706 CHECK(jni_conv->IsCurrentParamOnStack());
707 }
708 // References need placing in handle scope and the entry address passing.
709 if (ref_param) {
710 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
711 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
712 // passed to the native code must be null (not a pointer into the handle scope
713 // as with regular references).
714 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
715 // Check handle scope offset is within frame.
716 CHECK_LT(handle_scope_offset.Uint32Value(), mr_conv->GetDisplacement().Uint32Value());
717 }
718 if (input_in_reg && output_in_reg) {
719 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
720 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
721 if (ref_param) {
722 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
723 } else {
724 if (!mr_conv->IsCurrentParamOnStack()) {
725 // regular non-straddling move
726 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
727 } else {
728 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
729 }
730 }
731 } else if (!input_in_reg && !output_in_reg) {
732 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
733 if (ref_param) {
734 __ CreateHandleScopeEntry(out_off, handle_scope_offset, null_allowed);
735 } else {
736 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
737 size_t param_size = mr_conv->CurrentParamSize();
738 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
739 __ Copy(out_off, in_off, param_size);
740 }
741 } else if (!input_in_reg && output_in_reg) {
742 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
743 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
744 // Check that incoming stack arguments are above the current stack frame.
745 CHECK_GT(in_off.Uint32Value(), mr_conv->GetDisplacement().Uint32Value());
746 if (ref_param) {
747 __ CreateHandleScopeEntry(out_reg,
748 handle_scope_offset,
749 ManagedRegister::NoRegister(),
750 null_allowed);
751 } else {
752 size_t param_size = mr_conv->CurrentParamSize();
753 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
754 __ Load(out_reg, in_off, param_size);
755 }
756 } else {
757 CHECK(input_in_reg && !output_in_reg);
758 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
759 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
760 // Check outgoing argument is within frame part dedicated to out args.
761 CHECK_LT(out_off.Uint32Value(), jni_conv->GetDisplacement().Uint32Value());
762 if (ref_param) {
763 // TODO: recycle value in in_reg rather than reload from handle scope
764 __ CreateHandleScopeEntry(out_off, handle_scope_offset, null_allowed);
765 } else {
766 size_t param_size = mr_conv->CurrentParamSize();
767 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
768 if (!mr_conv->IsCurrentParamOnStack()) {
769 // regular non-straddling store
770 __ Store(out_off, in_reg, param_size);
771 } else {
772 // store where input straddles registers and stack
773 CHECK_EQ(param_size, 8u);
774 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
775 __ StoreSpanning(out_off, in_reg, in_off);
776 }
777 }
778 }
779 }
780
781 template <PointerSize kPointerSize>
SetNativeParameter(JNIMacroAssembler<kPointerSize> * jni_asm,JniCallingConvention * jni_conv,ManagedRegister in_reg)782 static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
783 JniCallingConvention* jni_conv,
784 ManagedRegister in_reg) {
785 if (jni_conv->IsCurrentParamOnStack()) {
786 FrameOffset dest = jni_conv->CurrentParamStackOffset();
787 __ StoreRawPtr(dest, in_reg);
788 } else {
789 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
790 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
791 }
792 }
793 }
794
ArtQuickJniCompileMethod(const CompilerOptions & compiler_options,uint32_t access_flags,uint32_t method_idx,const DexFile & dex_file)795 JniCompiledMethod ArtQuickJniCompileMethod(const CompilerOptions& compiler_options,
796 uint32_t access_flags,
797 uint32_t method_idx,
798 const DexFile& dex_file) {
799 if (Is64BitInstructionSet(compiler_options.GetInstructionSet())) {
800 return ArtJniCompileMethodInternal<PointerSize::k64>(
801 compiler_options, access_flags, method_idx, dex_file);
802 } else {
803 return ArtJniCompileMethodInternal<PointerSize::k32>(
804 compiler_options, access_flags, method_idx, dex_file);
805 }
806 }
807
808 } // namespace art
809