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