1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_JAVACALLS_HPP
  26 #define SHARE_VM_RUNTIME_JAVACALLS_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "oops/method.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "runtime/javaFrameAnchor.hpp"
  32 #include "runtime/thread.inline.hpp"
  33 #include "runtime/vmThread.hpp"
  34 #ifdef TARGET_ARCH_x86
  35 # include "jniTypes_x86.hpp"
  36 #endif
  37 #ifdef TARGET_ARCH_aarch64
  38 # include "jniTypes_aarch64.hpp"
  39 #endif
  40 #ifdef TARGET_ARCH_sparc
  41 # include "jniTypes_sparc.hpp"
  42 #endif
  43 #ifdef TARGET_ARCH_zero
  44 # include "jniTypes_zero.hpp"
  45 #endif
  46 #ifdef TARGET_ARCH_arm
  47 # include "jniTypes_arm.hpp"
  48 #endif
  49 #ifdef TARGET_ARCH_ppc
  50 # include "jniTypes_ppc.hpp"
  51 #endif
  52 
  53 // A JavaCallWrapper is constructed before each JavaCall and destructed after the call.
  54 // Its purpose is to allocate/deallocate a new handle block and to save/restore the last
  55 // Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack.
  56 
  57 class JavaCallWrapper: StackObj {
  58   friend class VMStructs;
  59  private:
  60   JavaThread*      _thread;                 // the thread to which this call belongs
  61   JNIHandleBlock*  _handles;                // the saved handle block
  62   Method*          _callee_method;          // to be able to collect arguments if entry frame is top frame
  63   oop              _receiver;               // the receiver of the call (if a non-static call)
  64 
  65   JavaFrameAnchor  _anchor;                 // last thread anchor state that we must restore
  66 
  67   JavaValue*       _result;                 // result value
  68 
  69  public:
  70   // Construction/destruction
  71    JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS);
  72   ~JavaCallWrapper();
  73 
  74   // Accessors
  75   JavaThread*      thread() const           { return _thread; }
  76   JNIHandleBlock*  handles() const          { return _handles; }
  77 
  78   JavaFrameAnchor* anchor(void)             { return &_anchor; }
  79 
  80   JavaValue*       result() const           { return _result; }
  81   // GC support
  82   Method*          callee_method()          { return _callee_method; }
  83   oop              receiver()               { return _receiver; }
  84   void             oops_do(OopClosure* f);
  85 
  86   bool             is_first_frame() const   { return _anchor.last_Java_sp() == NULL; }
  87 
  88 };
  89 
  90 
  91 // Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args)
  92 class JavaCallArguments : public StackObj {
  93  private:
  94   enum Constants {
  95    _default_size = 8    // Must be at least # of arguments in JavaCalls methods
  96   };
  97 
  98   intptr_t    _value_buffer      [_default_size + 1];
  99   u_char      _value_state_buffer[_default_size + 1];
 100 
 101   intptr_t*   _value;
 102   u_char*     _value_state;
 103   int         _size;
 104   int         _max_size;
 105   bool        _start_at_zero;      // Support late setting of receiver
 106 
 107   void initialize() {
 108     // Starts at first element to support set_receiver.
 109     _value       = &_value_buffer[1];
 110     _value_state = &_value_state_buffer[1];
 111 
 112     _max_size = _default_size;
 113     _size = 0;
 114     _start_at_zero = false;
 115   }
 116 
 117   // Helper for push_oop and the like.  The value argument is a
 118   // "handle" that refers to an oop.  We record the address of the
 119   // handle rather than the designated oop.  The handle is later
 120   // resolved to the oop by parameters().  This delays the exposure of
 121   // naked oops until it is GC-safe.
 122   template<typename T>
 123   inline int push_oop_impl(T handle, int size) {
 124     // JNITypes::put_obj expects an oop value, so we play fast and
 125     // loose with the type system.  The cast from handle type to oop
 126     // *must* use a C-style cast.  In a product build it performs a
 127     // reinterpret_cast. In a debug build (more accurately, in a
 128     // CHECK_UNHANDLED_OOPS build) it performs a static_cast, invoking
 129     // the debug-only oop class's conversion from void* constructor.
 130     JNITypes::put_obj((oop)handle, _value, size); // Updates size.
 131     return size;                // Return the updated size.
 132   }
 133 
 134  public:
 135   JavaCallArguments() { initialize(); }
 136 
 137   JavaCallArguments(Handle receiver) {
 138     initialize();
 139     push_oop(receiver);
 140   }
 141 
 142   JavaCallArguments(int max_size) {
 143     if (max_size > _default_size) {
 144       _value = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1);
 145       _value_state = NEW_RESOURCE_ARRAY(u_char, max_size + 1);
 146 
 147       // Reserve room for potential receiver in value and state
 148       _value++;
 149       _value_state++;
 150 
 151       _max_size = max_size;
 152       _size = 0;
 153       _start_at_zero = false;
 154     } else {
 155       initialize();
 156     }
 157   }
 158 
 159   // The possible values for _value_state elements.
 160   enum {
 161     value_state_primitive,
 162     value_state_oop,
 163     value_state_handle,
 164     value_state_jobject,
 165     value_state_limit
 166   };
 167 
 168   inline void push_oop(Handle h) {
 169     _value_state[_size] = value_state_handle;
 170     _size = push_oop_impl(h.raw_value(), _size);
 171   }
 172 
 173   inline void push_jobject(jobject h) {
 174     _value_state[_size] = value_state_jobject;
 175     _size = push_oop_impl(h, _size);
 176   }
 177 
 178   inline void push_int(int i) {
 179     _value_state[_size] = value_state_primitive;
 180     JNITypes::put_int(i, _value, _size);
 181   }
 182 
 183   inline void push_double(double d) {
 184     _value_state[_size] = value_state_primitive;
 185     _value_state[_size + 1] = value_state_primitive;
 186     JNITypes::put_double(d, _value, _size);
 187   }
 188 
 189   inline void push_long(jlong l) {
 190     _value_state[_size] = value_state_primitive;
 191     _value_state[_size + 1] = value_state_primitive;
 192     JNITypes::put_long(l, _value, _size);
 193   }
 194 
 195   inline void push_float(float f) {
 196     _value_state[_size] = value_state_primitive;
 197     JNITypes::put_float(f, _value, _size);
 198   }
 199 
 200   // receiver
 201   Handle receiver() {
 202     assert(_size > 0, "must at least be one argument");
 203     assert(_value_state[0] == value_state_handle,
 204            "first argument must be an oop");
 205     assert(_value[0] != 0, "receiver must be not-null");
 206     return Handle((oop*)_value[0], false);
 207   }
 208 
 209   void set_receiver(Handle h) {
 210     assert(_start_at_zero == false, "can only be called once");
 211     _start_at_zero = true;
 212     _value_state--;
 213     _value--;
 214     _size++;
 215     _value_state[0] = value_state_handle;
 216     push_oop_impl(h.raw_value(), 0);
 217   }
 218 
 219   // Converts all Handles to oops, and returns a reference to parameter vector
 220   intptr_t* parameters() ;
 221   int   size_of_parameters() const { return _size; }
 222 
 223   // Verify that pushed arguments fits a given method
 224   void verify(methodHandle method, BasicType return_type);
 225 };
 226 
 227 // All calls to Java have to go via JavaCalls. Sets up the stack frame
 228 // and makes sure that the last_Java_frame pointers are chained correctly.
 229 //
 230 
 231 class JavaCalls: AllStatic {
 232   static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS);
 233  public:
 234   // Optimized Constuctor call
 235   static void call_default_constructor(JavaThread* thread, methodHandle method, Handle receiver, TRAPS);
 236 
 237   // call_special
 238   // ------------
 239   // The receiver must be first oop in argument list
 240   static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 241 
 242   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args
 243   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 244   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 245 
 246   // virtual call
 247   // ------------
 248 
 249   // The receiver must be first oop in argument list
 250   static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 251 
 252   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args
 253   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 254   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 255 
 256   // Static call
 257   // -----------
 258   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 259 
 260   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS);
 261   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 262   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 263 
 264   // Low-level interface
 265   static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS);
 266 };
 267 
 268 #endif // SHARE_VM_RUNTIME_JAVACALLS_HPP