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