xref: /aosp_15_r20/external/cronet/third_party/jni_zero/jni_zero_internal.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 // Project-specific macros used by the header files generated by
15 // jni_generator.py. Different projects can then specify their own
16 // implementation for this file.
17 #define CHECK_NATIVE_PTR(env, jcaller, native_ptr, method_name, ...) \
18   JNI_ZERO_DCHECK(native_ptr);
19 
20 #define CHECK_CLAZZ(env, jcaller, clazz, ...) JNI_ZERO_DCHECK(clazz);
21 
22 #if defined(__clang__) && __has_attribute(noinline)
23 #define JNI_ZERO_NOINLINE [[clang::noinline]]
24 #elif __has_attribute(noinline)
25 #define JNI_ZERO_NOINLINE __attribute__((noinline))
26 #endif
27 
28 #if defined(__clang__) && defined(NDEBUG) && __has_attribute(always_inline)
29 #define JNI_ZERO_ALWAYS_INLINE [[clang::always_inline]] inline
30 #elif defined(NDEBUG) && __has_attribute(always_inline)
31 #define JNI_ZERO_ALWAYS_INLINE inline __attribute__((__always_inline__))
32 #else
33 #define JNI_ZERO_ALWAYS_INLINE inline
34 #endif
35 
36 namespace jni_zero::internal {
37 
HandleRegistrationError(JNIEnv * env,jclass clazz,const char * filename)38 inline void HandleRegistrationError(JNIEnv* env,
39                                     jclass clazz,
40                                     const char* filename) {
41   JNI_ZERO_ELOG("RegisterNatives failed in %s", filename);
42 }
43 
44 // A 32 bit number could be an address on stack. Random 64 bit marker on the
45 // stack is much less likely to be present on stack.
46 inline constexpr uint64_t kJniStackMarkerValue = 0xbdbdef1bebcade1b;
47 
48 // The method will initialize |atomic_class_id| to contain a global ref to the
49 // class. And will return that ref on subsequent calls.
50 JNI_ZERO_COMPONENT_BUILD_EXPORT jclass
51 LazyGetClass(JNIEnv* env,
52              const char* class_name,
53              const char* split_name,
54              std::atomic<jclass>* atomic_class_id);
55 
56 JNI_ZERO_COMPONENT_BUILD_EXPORT jclass
57 LazyGetClass(JNIEnv* env,
58              const char* class_name,
59              std::atomic<jclass>* atomic_class_id);
60 
61 // Context about the JNI call with exception checked to be stored in stack.
62 template <bool checked>
63 class JNI_ZERO_COMPONENT_BUILD_EXPORT JniJavaCallContext {
64  public:
JniJavaCallContext()65   JNI_ZERO_ALWAYS_INLINE JniJavaCallContext() {
66 // TODO(ssid): Implement for other architectures.
67 #if defined(__arm__) || defined(__aarch64__)
68     // This assumes that this method does not increment the stack pointer.
69     asm volatile("mov %0, sp" : "=r"(sp_));
70 #else
71     sp_ = 0;
72 #endif
73   }
74 
75   // Force no inline to reduce code size.
76   template <MethodID::Type type>
Init(JNIEnv * env,jclass clazz,const char * method_name,const char * jni_signature,std::atomic<jmethodID> * atomic_method_id)77   JNI_ZERO_NOINLINE void Init(JNIEnv* env,
78                               jclass clazz,
79                               const char* method_name,
80                               const char* jni_signature,
81                               std::atomic<jmethodID>* atomic_method_id) {
82     env_ = env;
83 
84     // Make sure compiler doesn't optimize out the assignment.
85     memcpy(&marker_, &kJniStackMarkerValue, sizeof(kJniStackMarkerValue));
86     // Gets PC of the calling function.
87     pc_ = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
88 
89     method_id_ = MethodID::LazyGet<type>(env, clazz, method_name, jni_signature,
90                                          atomic_method_id);
91   }
92 
~JniJavaCallContext()93   JNI_ZERO_NOINLINE ~JniJavaCallContext() {
94     // Reset so that spurious marker finds are avoided.
95     memset(&marker_, 0, sizeof(marker_));
96     if (checked) {
97       CheckException(env_);
98     }
99   }
100 
method_id()101   jmethodID method_id() { return method_id_; }
102 
103  private:
104   uint64_t marker_;
105   uintptr_t sp_;
106   uintptr_t pc_;
107   JNIEnv* env_;
108   jmethodID method_id_;
109 };
110 
111 }  // namespace jni_zero::internal
112 
113 #endif  // JNI_ZERO_JNI_ZERO_INTERNAL_H
114