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 <memory>
21 #include <vector>
22 #include <fstream>
23 
24 #include "art_method.h"
25 #include "base/logging.h"
26 #include "base/macros.h"
27 #include "calling_convention.h"
28 #include "class_linker.h"
29 #include "compiled_method.h"
30 #include "dex_file-inl.h"
31 #include "driver/compiler_driver.h"
32 #include "driver/compiler_options.h"
33 #include "entrypoints/quick/quick_entrypoints.h"
34 #include "jni_env_ext.h"
35 #include "utils/assembler.h"
36 #include "utils/managed_register.h"
37 #include "utils/arm/managed_register_arm.h"
38 #include "utils/arm64/managed_register_arm64.h"
39 #include "utils/mips/managed_register_mips.h"
40 #include "utils/mips64/managed_register_mips64.h"
41 #include "utils/x86/managed_register_x86.h"
42 #include "thread.h"
43 
44 #define __ jni_asm->
45 
46 namespace art {
47 
48 static void CopyParameter(Assembler* jni_asm,
49                           ManagedRuntimeCallingConvention* mr_conv,
50                           JniCallingConvention* jni_conv,
51                           size_t frame_size, size_t out_arg_size);
52 static void SetNativeParameter(Assembler* jni_asm,
53                                JniCallingConvention* jni_conv,
54                                ManagedRegister in_reg);
55 
56 // Generate the JNI bridge for the given method, general contract:
57 // - Arguments are in the managed runtime format, either on stack or in
58 //   registers, a reference to the method object is supplied as part of this
59 //   convention.
60 //
ArtJniCompileMethodInternal(CompilerDriver * driver,uint32_t access_flags,uint32_t method_idx,const DexFile & dex_file)61 CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
62                                             uint32_t access_flags, uint32_t method_idx,
63                                             const DexFile& dex_file) {
64   const bool is_native = (access_flags & kAccNative) != 0;
65   CHECK(is_native);
66   const bool is_static = (access_flags & kAccStatic) != 0;
67   const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
68   const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
69   InstructionSet instruction_set = driver->GetInstructionSet();
70   const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
71   // Calling conventions used to iterate over parameters to method
72   std::unique_ptr<JniCallingConvention> main_jni_conv(
73       JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
74   bool reference_return = main_jni_conv->IsReturnAReference();
75 
76   std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
77       ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
78 
79   // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
80   //     method and the current thread.
81   const char* jni_end_shorty;
82   if (reference_return && is_synchronized) {
83     jni_end_shorty = "ILL";
84   } else if (reference_return) {
85     jni_end_shorty = "IL";
86   } else if (is_synchronized) {
87     jni_end_shorty = "VL";
88   } else {
89     jni_end_shorty = "V";
90   }
91 
92   std::unique_ptr<JniCallingConvention> end_jni_conv(
93       JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
94 
95   // Assembler that holds generated instructions
96   std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
97   jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GetGenerateDebugInfo());
98 
99   // Offsets into data structures
100   // TODO: if cross compiling these offsets are for the host not the target
101   const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
102   const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
103   const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
104 
105   // 1. Build the frame saving all callee saves
106   const size_t frame_size(main_jni_conv->FrameSize());
107   const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
108   __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
109   DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
110 
111   // 2. Set up the HandleScope
112   mr_conv->ResetIterator(FrameOffset(frame_size));
113   main_jni_conv->ResetIterator(FrameOffset(0));
114   __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
115                            main_jni_conv->ReferenceCount(),
116                            mr_conv->InterproceduralScratchRegister());
117 
118   if (is_64_bit_target) {
119     __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
120                               Thread::TopHandleScopeOffset<8>(),
121                               mr_conv->InterproceduralScratchRegister());
122     __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
123                                   main_jni_conv->HandleScopeOffset(),
124                                   mr_conv->InterproceduralScratchRegister());
125   } else {
126     __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
127                               Thread::TopHandleScopeOffset<4>(),
128                               mr_conv->InterproceduralScratchRegister());
129     __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
130                                   main_jni_conv->HandleScopeOffset(),
131                                   mr_conv->InterproceduralScratchRegister());
132   }
133 
134   // 3. Place incoming reference arguments into handle scope
135   main_jni_conv->Next();  // Skip JNIEnv*
136   // 3.5. Create Class argument for static methods out of passed method
137   if (is_static) {
138     FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
139     // Check handle scope offset is within frame
140     CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
141     // Note this LoadRef() doesn't need heap poisoning since its from the ArtMethod.
142     // Note this LoadRef() does not include read barrier. It will be handled below.
143     __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
144                mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
145     __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
146     __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
147     main_jni_conv->Next();  // in handle scope so move to next argument
148   }
149   while (mr_conv->HasNext()) {
150     CHECK(main_jni_conv->HasNext());
151     bool ref_param = main_jni_conv->IsCurrentParamAReference();
152     CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
153     // References need placing in handle scope and the entry value passing
154     if (ref_param) {
155       // Compute handle scope entry, note null is placed in the handle scope but its boxed value
156       // must be null.
157       FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
158       // Check handle scope offset is within frame and doesn't run into the saved segment state.
159       CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
160       CHECK_NE(handle_scope_offset.Uint32Value(),
161                main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
162       bool input_in_reg = mr_conv->IsCurrentParamInRegister();
163       bool input_on_stack = mr_conv->IsCurrentParamOnStack();
164       CHECK(input_in_reg || input_on_stack);
165 
166       if (input_in_reg) {
167         ManagedRegister in_reg  =  mr_conv->CurrentParamRegister();
168         __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
169         __ StoreRef(handle_scope_offset, in_reg);
170       } else if (input_on_stack) {
171         FrameOffset in_off  = mr_conv->CurrentParamStackOffset();
172         __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
173         __ CopyRef(handle_scope_offset, in_off,
174                    mr_conv->InterproceduralScratchRegister());
175       }
176     }
177     mr_conv->Next();
178     main_jni_conv->Next();
179   }
180 
181   // 4. Write out the end of the quick frames.
182   if (is_64_bit_target) {
183     __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
184   } else {
185     __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
186   }
187 
188   // 5. Move frame down to allow space for out going args.
189   const size_t main_out_arg_size = main_jni_conv->OutArgSize();
190   size_t current_out_arg_size = main_out_arg_size;
191   __ IncreaseFrameSize(main_out_arg_size);
192 
193   // Call the read barrier for the declaring class loaded from the method for a static call.
194   // Note that we always have outgoing param space available for at least two params.
195   if (kUseReadBarrier && is_static) {
196     ThreadOffset<4> read_barrier32 = QUICK_ENTRYPOINT_OFFSET(4, pReadBarrierJni);
197     ThreadOffset<8> read_barrier64 = QUICK_ENTRYPOINT_OFFSET(8, pReadBarrierJni);
198     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
199     main_jni_conv->Next();  // Skip JNIEnv.
200     FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
201     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
202     // Pass the handle for the class as the first argument.
203     if (main_jni_conv->IsCurrentParamOnStack()) {
204       FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
205       __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
206                          mr_conv->InterproceduralScratchRegister(),
207                          false);
208     } else {
209       ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
210       __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
211                          ManagedRegister::NoRegister(), false);
212     }
213     main_jni_conv->Next();
214     // Pass the current thread as the second argument and call.
215     if (main_jni_conv->IsCurrentParamInRegister()) {
216       __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
217       if (is_64_bit_target) {
218         __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier64),
219                 main_jni_conv->InterproceduralScratchRegister());
220       } else {
221         __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier32),
222                 main_jni_conv->InterproceduralScratchRegister());
223       }
224     } else {
225       __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
226                           main_jni_conv->InterproceduralScratchRegister());
227       if (is_64_bit_target) {
228         __ CallFromThread64(read_barrier64, main_jni_conv->InterproceduralScratchRegister());
229       } else {
230         __ CallFromThread32(read_barrier32, main_jni_conv->InterproceduralScratchRegister());
231       }
232     }
233     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));  // Reset.
234   }
235 
236   // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
237   //    can occur. The result is the saved JNI local state that is restored by the exit call. We
238   //    abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
239   //    arguments.
240   ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
241                                                 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
242   ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
243                                                 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
244   main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
245   FrameOffset locked_object_handle_scope_offset(0);
246   if (is_synchronized) {
247     // Pass object for locking.
248     main_jni_conv->Next();  // Skip JNIEnv.
249     locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
250     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
251     if (main_jni_conv->IsCurrentParamOnStack()) {
252       FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
253       __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
254                                 mr_conv->InterproceduralScratchRegister(), false);
255     } else {
256       ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
257       __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
258                                 ManagedRegister::NoRegister(), false);
259     }
260     main_jni_conv->Next();
261   }
262   if (main_jni_conv->IsCurrentParamInRegister()) {
263     __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
264     if (is_64_bit_target) {
265       __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
266               main_jni_conv->InterproceduralScratchRegister());
267     } else {
268       __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
269               main_jni_conv->InterproceduralScratchRegister());
270     }
271   } else {
272     __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
273                         main_jni_conv->InterproceduralScratchRegister());
274     if (is_64_bit_target) {
275       __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
276     } else {
277       __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
278     }
279   }
280   if (is_synchronized) {  // Check for exceptions from monitor enter.
281     __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
282   }
283   FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
284   __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
285 
286   // 7. Iterate over arguments placing values from managed calling convention in
287   //    to the convention required for a native call (shuffling). For references
288   //    place an index/pointer to the reference after checking whether it is
289   //    null (which must be encoded as null).
290   //    Note: we do this prior to materializing the JNIEnv* and static's jclass to
291   //    give as many free registers for the shuffle as possible.
292   mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
293   uint32_t args_count = 0;
294   while (mr_conv->HasNext()) {
295     args_count++;
296     mr_conv->Next();
297   }
298 
299   // Do a backward pass over arguments, so that the generated code will be "mov
300   // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
301   // TODO: A reverse iterator to improve readability.
302   for (uint32_t i = 0; i < args_count; ++i) {
303     mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
304     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
305     main_jni_conv->Next();  // Skip JNIEnv*.
306     if (is_static) {
307       main_jni_conv->Next();  // Skip Class for now.
308     }
309     // Skip to the argument we're interested in.
310     for (uint32_t j = 0; j < args_count - i - 1; ++j) {
311       mr_conv->Next();
312       main_jni_conv->Next();
313     }
314     CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
315   }
316   if (is_static) {
317     // Create argument for Class
318     mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
319     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
320     main_jni_conv->Next();  // Skip JNIEnv*
321     FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
322     if (main_jni_conv->IsCurrentParamOnStack()) {
323       FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
324       __ CreateHandleScopeEntry(out_off, handle_scope_offset,
325                          mr_conv->InterproceduralScratchRegister(),
326                          false);
327     } else {
328       ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
329       __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
330                          ManagedRegister::NoRegister(), false);
331     }
332   }
333 
334   // 8. Create 1st argument, the JNI environment ptr.
335   main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
336   // Register that will hold local indirect reference table
337   if (main_jni_conv->IsCurrentParamInRegister()) {
338     ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
339     DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
340     if (is_64_bit_target) {
341       __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
342     } else {
343       __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
344     }
345   } else {
346     FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
347     if (is_64_bit_target) {
348       __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
349                                 main_jni_conv->InterproceduralScratchRegister());
350     } else {
351       __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
352                                 main_jni_conv->InterproceduralScratchRegister());
353     }
354   }
355 
356   // 9. Plant call to native code associated with method.
357   MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset(
358       InstructionSetPointerSize(instruction_set));
359   __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
360           mr_conv->InterproceduralScratchRegister());
361 
362   // 10. Fix differences in result widths.
363   if (main_jni_conv->RequiresSmallResultTypeExtension()) {
364     if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
365         main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
366       __ SignExtend(main_jni_conv->ReturnRegister(),
367                     Primitive::ComponentSize(main_jni_conv->GetReturnType()));
368     } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
369                main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
370       __ ZeroExtend(main_jni_conv->ReturnRegister(),
371                     Primitive::ComponentSize(main_jni_conv->GetReturnType()));
372     }
373   }
374 
375   // 11. Save return value
376   FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
377   if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
378     if ((instruction_set == kMips || instruction_set == kMips64) &&
379         main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
380         return_save_location.Uint32Value() % 8 != 0) {
381       // Ensure doubles are 8-byte aligned for MIPS
382       return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
383     }
384     CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
385     __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
386   }
387 
388   // Increase frame size for out args if needed by the end_jni_conv.
389   const size_t end_out_arg_size = end_jni_conv->OutArgSize();
390   if (end_out_arg_size > current_out_arg_size) {
391     size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
392     current_out_arg_size = end_out_arg_size;
393     __ IncreaseFrameSize(out_arg_size_diff);
394     saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
395     locked_object_handle_scope_offset =
396         FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
397     return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
398   }
399   //     thread.
400   end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
401   ThreadOffset<4> jni_end32(-1);
402   ThreadOffset<8> jni_end64(-1);
403   if (reference_return) {
404     // Pass result.
405     jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
406                                 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
407     jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
408                                 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
409     SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
410     end_jni_conv->Next();
411   } else {
412     jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
413                                 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
414     jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
415                                 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
416   }
417   // Pass saved local reference state.
418   if (end_jni_conv->IsCurrentParamOnStack()) {
419     FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
420     __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
421   } else {
422     ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
423     __ Load(out_reg, saved_cookie_offset, 4);
424   }
425   end_jni_conv->Next();
426   if (is_synchronized) {
427     // Pass object for unlocking.
428     if (end_jni_conv->IsCurrentParamOnStack()) {
429       FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
430       __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
431                          end_jni_conv->InterproceduralScratchRegister(),
432                          false);
433     } else {
434       ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
435       __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
436                          ManagedRegister::NoRegister(), false);
437     }
438     end_jni_conv->Next();
439   }
440   if (end_jni_conv->IsCurrentParamInRegister()) {
441     __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
442     if (is_64_bit_target) {
443       __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
444               end_jni_conv->InterproceduralScratchRegister());
445     } else {
446       __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
447               end_jni_conv->InterproceduralScratchRegister());
448     }
449   } else {
450     __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
451                         end_jni_conv->InterproceduralScratchRegister());
452     if (is_64_bit_target) {
453       __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
454     } else {
455       __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
456     }
457   }
458 
459   // 13. Reload return value
460   if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
461     __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
462   }
463 
464   // 14. Move frame up now we're done with the out arg space.
465   __ DecreaseFrameSize(current_out_arg_size);
466 
467   // 15. Process pending exceptions from JNI call or monitor exit.
468   __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
469 
470   // 16. Remove activation - need to restore callee save registers since the GC may have changed
471   //     them.
472   DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
473   __ RemoveFrame(frame_size, callee_save_regs);
474   DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
475 
476   // 17. Finalize code generation
477   __ EmitSlowPaths();
478   size_t cs = __ CodeSize();
479   std::vector<uint8_t> managed_code(cs);
480   MemoryRegion code(&managed_code[0], managed_code.size());
481   __ FinalizeInstructions(code);
482 
483   return CompiledMethod::SwapAllocCompiledMethod(driver,
484                                                  instruction_set,
485                                                  ArrayRef<const uint8_t>(managed_code),
486                                                  frame_size,
487                                                  main_jni_conv->CoreSpillMask(),
488                                                  main_jni_conv->FpSpillMask(),
489                                                  nullptr,  // src_mapping_table.
490                                                  ArrayRef<const uint8_t>(),  // mapping_table.
491                                                  ArrayRef<const uint8_t>(),  // vmap_table.
492                                                  ArrayRef<const uint8_t>(),  // native_gc_map.
493                                                  ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
494                                                  ArrayRef<const LinkerPatch>());
495 }
496 
497 // Copy a single parameter from the managed to the JNI calling convention.
CopyParameter(Assembler * jni_asm,ManagedRuntimeCallingConvention * mr_conv,JniCallingConvention * jni_conv,size_t frame_size,size_t out_arg_size)498 static void CopyParameter(Assembler* jni_asm,
499                           ManagedRuntimeCallingConvention* mr_conv,
500                           JniCallingConvention* jni_conv,
501                           size_t frame_size, size_t out_arg_size) {
502   bool input_in_reg = mr_conv->IsCurrentParamInRegister();
503   bool output_in_reg = jni_conv->IsCurrentParamInRegister();
504   FrameOffset handle_scope_offset(0);
505   bool null_allowed = false;
506   bool ref_param = jni_conv->IsCurrentParamAReference();
507   CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
508   // input may be in register, on stack or both - but not none!
509   CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
510   if (output_in_reg) {  // output shouldn't straddle registers and stack
511     CHECK(!jni_conv->IsCurrentParamOnStack());
512   } else {
513     CHECK(jni_conv->IsCurrentParamOnStack());
514   }
515   // References need placing in handle scope and the entry address passing.
516   if (ref_param) {
517     null_allowed = mr_conv->IsCurrentArgPossiblyNull();
518     // Compute handle scope offset. Note null is placed in the handle scope but the jobject
519     // passed to the native code must be null (not a pointer into the handle scope
520     // as with regular references).
521     handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
522     // Check handle scope offset is within frame.
523     CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
524   }
525   if (input_in_reg && output_in_reg) {
526     ManagedRegister in_reg = mr_conv->CurrentParamRegister();
527     ManagedRegister out_reg = jni_conv->CurrentParamRegister();
528     if (ref_param) {
529       __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
530     } else {
531       if (!mr_conv->IsCurrentParamOnStack()) {
532         // regular non-straddling move
533         __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
534       } else {
535         UNIMPLEMENTED(FATAL);  // we currently don't expect to see this case
536       }
537     }
538   } else if (!input_in_reg && !output_in_reg) {
539     FrameOffset out_off = jni_conv->CurrentParamStackOffset();
540     if (ref_param) {
541       __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
542                          null_allowed);
543     } else {
544       FrameOffset in_off = mr_conv->CurrentParamStackOffset();
545       size_t param_size = mr_conv->CurrentParamSize();
546       CHECK_EQ(param_size, jni_conv->CurrentParamSize());
547       __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
548     }
549   } else if (!input_in_reg && output_in_reg) {
550     FrameOffset in_off = mr_conv->CurrentParamStackOffset();
551     ManagedRegister out_reg = jni_conv->CurrentParamRegister();
552     // Check that incoming stack arguments are above the current stack frame.
553     CHECK_GT(in_off.Uint32Value(), frame_size);
554     if (ref_param) {
555       __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
556     } else {
557       size_t param_size = mr_conv->CurrentParamSize();
558       CHECK_EQ(param_size, jni_conv->CurrentParamSize());
559       __ Load(out_reg, in_off, param_size);
560     }
561   } else {
562     CHECK(input_in_reg && !output_in_reg);
563     ManagedRegister in_reg = mr_conv->CurrentParamRegister();
564     FrameOffset out_off = jni_conv->CurrentParamStackOffset();
565     // Check outgoing argument is within frame
566     CHECK_LT(out_off.Uint32Value(), frame_size);
567     if (ref_param) {
568       // TODO: recycle value in in_reg rather than reload from handle scope
569       __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
570                          null_allowed);
571     } else {
572       size_t param_size = mr_conv->CurrentParamSize();
573       CHECK_EQ(param_size, jni_conv->CurrentParamSize());
574       if (!mr_conv->IsCurrentParamOnStack()) {
575         // regular non-straddling store
576         __ Store(out_off, in_reg, param_size);
577       } else {
578         // store where input straddles registers and stack
579         CHECK_EQ(param_size, 8u);
580         FrameOffset in_off = mr_conv->CurrentParamStackOffset();
581         __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
582       }
583     }
584   }
585 }
586 
SetNativeParameter(Assembler * jni_asm,JniCallingConvention * jni_conv,ManagedRegister in_reg)587 static void SetNativeParameter(Assembler* jni_asm,
588                                JniCallingConvention* jni_conv,
589                                ManagedRegister in_reg) {
590   if (jni_conv->IsCurrentParamOnStack()) {
591     FrameOffset dest = jni_conv->CurrentParamStackOffset();
592     __ StoreRawPtr(dest, in_reg);
593   } else {
594     if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
595       __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
596     }
597   }
598 }
599 
ArtQuickJniCompileMethod(CompilerDriver * compiler,uint32_t access_flags,uint32_t method_idx,const DexFile & dex_file)600 CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
601                                          uint32_t method_idx, const DexFile& dex_file) {
602   return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
603 }
604 
605 }  // namespace art
606