1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef JNI_ZERO_JNI_ZERO_INTERNAL_H
6 #define JNI_ZERO_JNI_ZERO_INTERNAL_H
7
8 #include <jni.h>
9
10 #include "third_party/jni_zero/jni_export.h"
11 #include "third_party/jni_zero/jni_zero.h"
12 #include "third_party/jni_zero/logging.h"
13
14 #if JNI_ZERO_ENABLE_TYPE_CONVERSIONS
15 #include "third_party/jni_zero/default_conversions.h"
16 #endif
17
18 // Project-specific macros used by the header files generated by
19 // jni_generator.py. Different projects can then specify their own
20 // implementation for this file.
21
22 #define CHECK_CLAZZ(env, jcaller, clazz, ...) JNI_ZERO_DCHECK(clazz);
23
24 namespace jni_zero::internal {
25
HandleRegistrationError(JNIEnv * env,jclass clazz,const char * filename)26 inline void HandleRegistrationError(JNIEnv* env,
27 jclass clazz,
28 const char* filename) {
29 JNI_ZERO_ELOG("RegisterNatives failed in %s", filename);
30 }
31
32 // A 32 bit number could be an address on stack. Random 64 bit marker on the
33 // stack is much less likely to be present on stack.
34 inline constexpr uint64_t kJniStackMarkerValue = 0xbdbdef1bebcade1b;
35
36 // The method will initialize |atomic_class_id| to contain a global ref to the
37 // class. And will return that ref on subsequent calls.
38 JNI_ZERO_COMPONENT_BUILD_EXPORT jclass
39 LazyGetClass(JNIEnv* env,
40 const char* class_name,
41 const char* split_name,
42 std::atomic<jclass>* atomic_class_id);
43
44 JNI_ZERO_COMPONENT_BUILD_EXPORT jclass
45 LazyGetClass(JNIEnv* env,
46 const char* class_name,
47 std::atomic<jclass>* atomic_class_id);
48
49 // Context about the JNI call with exception checked to be stored in stack.
50 template <bool checked>
51 class JNI_ZERO_COMPONENT_BUILD_EXPORT JniJavaCallContext {
52 public:
JniJavaCallContext()53 JNI_ZERO_ALWAYS_INLINE JniJavaCallContext() {
54 // TODO(ssid): Implement for other architectures.
55 #if defined(__arm__) || defined(__aarch64__)
56 // This assumes that this method does not increment the stack pointer.
57 asm volatile("mov %0, sp" : "=r"(sp_));
58 #else
59 sp_ = 0;
60 #endif
61 }
62
63 // Force no inline to reduce code size.
64 template <MethodID::Type type>
Init(JNIEnv * env,jclass clazz,const char * method_name,const char * jni_signature,std::atomic<jmethodID> * atomic_method_id)65 JNI_ZERO_NEVER_INLINE void Init(JNIEnv* env,
66 jclass clazz,
67 const char* method_name,
68 const char* jni_signature,
69 std::atomic<jmethodID>* atomic_method_id) {
70 env_ = env;
71
72 // Make sure compiler doesn't optimize out the assignment.
73 memcpy(&marker_, &kJniStackMarkerValue, sizeof(kJniStackMarkerValue));
74 // Gets PC of the calling function.
75 pc_ = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
76
77 method_id_ = MethodID::LazyGet<type>(env, clazz, method_name, jni_signature,
78 atomic_method_id);
79 }
80
~JniJavaCallContext()81 JNI_ZERO_NEVER_INLINE ~JniJavaCallContext() {
82 // Reset so that spurious marker finds are avoided.
83 memset(&marker_, 0, sizeof(marker_));
84 if (checked) {
85 CheckException(env_);
86 }
87 }
88
method_id()89 jmethodID method_id() { return method_id_; }
90
91 private:
92 uint64_t marker_;
93 uintptr_t sp_;
94 uintptr_t pc_;
95 JNIEnv* env_;
96 jmethodID method_id_;
97 };
98
99 } // namespace jni_zero::internal
100
101 #endif // JNI_ZERO_JNI_ZERO_INTERNAL_H
102