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_sparc
  38 # include "jniTypes_sparc.hpp"
  39 #endif
  40 #ifdef TARGET_ARCH_zero
  41 # include "jniTypes_zero.hpp"
  42 #endif
  43 #ifdef TARGET_ARCH_arm
  44 # include "jniTypes_arm.hpp"
  45 #endif
  46 #ifdef TARGET_ARCH_ppc
  47 # include "jniTypes_ppc.hpp"
  48 #endif
  49 
  50 // A JavaCallWrapper is constructed before each JavaCall and destructed after the call.
  51 // Its purpose is to allocate/deallocate a new handle block and to save/restore the last
  52 // Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack.
  53 
  54 class JavaCallWrapper: StackObj {
  55   friend class VMStructs;
  56  private:
  57   JavaThread*      _thread;                 // the thread to which this call belongs
  58   JNIHandleBlock*  _handles;                // the saved handle block
  59   Method*          _callee_method;          // to be able to collect arguments if entry frame is top frame
  60   oop              _receiver;               // the receiver of the call (if a non-static call)
  61 
  62   JavaFrameAnchor  _anchor;                 // last thread anchor state that we must restore
  63 
  64   JavaValue*       _result;                 // result value
  65 
  66  public:
  67   // Construction/destruction
  68    JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS);
  69   ~JavaCallWrapper();
  70 
  71   // Accessors
  72   JavaThread*      thread() const           { return _thread; }
  73   JNIHandleBlock*  handles() const          { return _handles; }
  74 
  75   JavaFrameAnchor* anchor(void)             { return &_anchor; }
  76 
  77   JavaValue*       result() const           { return _result; }
  78   // GC support
  79   Method*          callee_method()          { return _callee_method; }
  80   oop              receiver()               { return _receiver; }
  81   void             oops_do(OopClosure* f);
  82 
  83   bool             is_first_frame() const   { return _anchor.last_Java_sp() == NULL; }
  84 
  85 };
  86 
  87 
  88 // Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args)
  89 class JavaCallArguments : public StackObj {
  90  private:
  91   enum Constants {
  92    _default_size = 8    // Must be at least # of arguments in JavaCalls methods
  93   };
  94 
  95   intptr_t    _value_buffer      [_default_size + 1];
  96   u_char      _value_state_buffer[_default_size + 1];
  97 
  98   intptr_t*   _value;
  99   u_char*     _value_state;
 100   int         _size;
 101   int         _max_size;
 102   bool        _start_at_zero;      // Support late setting of receiver
 103 
 104   void initialize() {
 105     // Starts at first element to support set_receiver.
 106     _value       = &_value_buffer[1];
 107     _value_state = &_value_state_buffer[1];
 108 
 109     _max_size = _default_size;
 110     _size = 0;
 111     _start_at_zero = false;
 112   }
 113 
 114   // Helper for push_oop and the like.  The value argument is a
 115   // "handle" that refers to an oop.  We record the address of the
 116   // handle rather than the designated oop.  The handle is later
 117   // resolved to the oop by parameters().  This delays the exposure of
 118   // naked oops until it is GC-safe.
 119   template<typename T>
 120   inline int push_oop_impl(T handle, int size) {
 121     // JNITypes::put_obj expects an oop value, so we play fast and
 122     // loose with the type system.  The cast from handle type to oop
 123     // *must* use a C-style cast.  In a product build it performs a
 124     // reinterpret_cast. In a debug build (more accurately, in a
 125     // CHECK_UNHANDLED_OOPS build) it performs a static_cast, invoking
 126     // the debug-only oop class's conversion from void* constructor.
 127     JNITypes::put_obj((oop)handle, _value, size); // Updates size.
 128     return size;                // Return the updated size.
 129   }
 130 
 131  public:
 132   JavaCallArguments() { initialize(); }
 133 
 134   JavaCallArguments(Handle receiver) {
 135     initialize();
 136     push_oop(receiver);
 137   }
 138 
 139   JavaCallArguments(int max_size) {
 140     if (max_size > _default_size) {
 141       _value = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1);
 142       _value_state = NEW_RESOURCE_ARRAY(u_char, max_size + 1);
 143 
 144       // Reserve room for potential receiver in value and state
 145       _value++;
 146       _value_state++;
 147 
 148       _max_size = max_size;
 149       _size = 0;
 150       _start_at_zero = false;
 151     } else {
 152       initialize();
 153     }
 154   }
 155 
 156   // The possible values for _value_state elements.
 157   enum {
 158     value_state_primitive,
 159     value_state_oop,
 160     value_state_handle,
 161     value_state_jobject,
 162     value_state_limit
 163   };
 164 
 165   inline void push_oop(Handle h) {
 166     _value_state[_size] = value_state_handle;
 167     _size = push_oop_impl(h.raw_value(), _size);
 168   }
 169 
 170   inline void push_jobject(jobject h) {
 171     _value_state[_size] = value_state_jobject;
 172     _size = push_oop_impl(h, _size);
 173   }
 174 
 175   inline void push_int(int i) {
 176     _value_state[_size] = value_state_primitive;
 177     JNITypes::put_int(i, _value, _size);
 178   }
 179 
 180   inline void push_double(double d) {
 181     _value_state[_size] = value_state_primitive;
 182     _value_state[_size + 1] = value_state_primitive;
 183     JNITypes::put_double(d, _value, _size);
 184   }
 185 
 186   inline void push_long(jlong l) {
 187     _value_state[_size] = value_state_primitive;
 188     _value_state[_size + 1] = value_state_primitive;
 189     JNITypes::put_long(l, _value, _size);
 190   }
 191 
 192   inline void push_float(float f) {
 193     _value_state[_size] = value_state_primitive;
 194     JNITypes::put_float(f, _value, _size);
 195   }
 196 
 197   // receiver
 198   Handle receiver() {
 199     assert(_size > 0, "must at least be one argument");
 200     assert(_value_state[0] == value_state_handle,
 201            "first argument must be an oop");
 202     assert(_value[0] != 0, "receiver must be not-null");
 203     return Handle((oop*)_value[0], false);
 204   }
 205 
 206   void set_receiver(Handle h) {
 207     assert(_start_at_zero == false, "can only be called once");
 208     _start_at_zero = true;
 209     _value_state--;
 210     _value--;
 211     _size++;
 212     _value_state[0] = value_state_handle;
 213     push_oop_impl(h.raw_value(), 0);
 214   }
 215 
 216   // Converts all Handles to oops, and returns a reference to parameter vector
 217   intptr_t* parameters() ;
 218   int   size_of_parameters() const { return _size; }
 219 
 220   // Verify that pushed arguments fits a given method
 221   void verify(methodHandle method, BasicType return_type);
 222 };
 223 
 224 // All calls to Java have to go via JavaCalls. Sets up the stack frame
 225 // and makes sure that the last_Java_frame pointers are chained correctly.
 226 //
 227 
 228 class JavaCalls: AllStatic {
 229   static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS);
 230  public:
 231   // Optimized Constuctor call
 232   static void call_default_constructor(JavaThread* thread, methodHandle method, Handle receiver, TRAPS);
 233 
 234   // call_special
 235   // ------------
 236   // The receiver must be first oop in argument list
 237   static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 238 
 239   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args
 240   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 241   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 242 
 243   // virtual call
 244   // ------------
 245 
 246   // The receiver must be first oop in argument list
 247   static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 248 
 249   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args
 250   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 251   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 252 
 253   // Static call
 254   // -----------
 255   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 256 
 257   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS);
 258   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 259   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 260 
 261   // Low-level interface
 262   static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS);
 263 };
 264 
 265 #endif // SHARE_VM_RUNTIME_JAVACALLS_HPP