1 /*
2  * Copyright (C) 2016 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 #ifndef ART_OPENJDKJVMTI_EVENTS_H_
18 #define ART_OPENJDKJVMTI_EVENTS_H_
19 
20 #include <bitset>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include <android-base/logging.h>
25 #include <android-base/thread_annotations.h>
26 
27 #include "android-base/thread_annotations.h"
28 #include "base/macros.h"
29 #include "base/mutex.h"
30 #include "jvmti.h"
31 #include "managed_stack.h"
32 #include "thread.h"
33 
34 namespace openjdkjvmti {
35 
36 struct ArtJvmTiEnv;
37 class JvmtiEventAllocationListener;
38 class JvmtiDdmChunkListener;
39 class JvmtiGcPauseListener;
40 class JvmtiMethodTraceListener;
41 class JvmtiMonitorListener;
42 class JvmtiParkListener;
43 
44 // an enum for ArtEvents. This differs from the JVMTI events only in that we distinguish between
45 // retransformation capable and incapable loading
46 enum class ArtJvmtiEvent : jint {
47     kMinEventTypeVal = JVMTI_MIN_EVENT_TYPE_VAL,
48     kVmInit = JVMTI_EVENT_VM_INIT,
49     kVmDeath = JVMTI_EVENT_VM_DEATH,
50     kThreadStart = JVMTI_EVENT_THREAD_START,
51     kThreadEnd = JVMTI_EVENT_THREAD_END,
52     kClassFileLoadHookNonRetransformable = JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
53     kClassLoad = JVMTI_EVENT_CLASS_LOAD,
54     kClassPrepare = JVMTI_EVENT_CLASS_PREPARE,
55     kVmStart = JVMTI_EVENT_VM_START,
56     kException = JVMTI_EVENT_EXCEPTION,
57     kExceptionCatch = JVMTI_EVENT_EXCEPTION_CATCH,
58     kSingleStep = JVMTI_EVENT_SINGLE_STEP,
59     kFramePop = JVMTI_EVENT_FRAME_POP,
60     kBreakpoint = JVMTI_EVENT_BREAKPOINT,
61     kFieldAccess = JVMTI_EVENT_FIELD_ACCESS,
62     kFieldModification = JVMTI_EVENT_FIELD_MODIFICATION,
63     kMethodEntry = JVMTI_EVENT_METHOD_ENTRY,
64     kMethodExit = JVMTI_EVENT_METHOD_EXIT,
65     kNativeMethodBind = JVMTI_EVENT_NATIVE_METHOD_BIND,
66     kCompiledMethodLoad = JVMTI_EVENT_COMPILED_METHOD_LOAD,
67     kCompiledMethodUnload = JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
68     kDynamicCodeGenerated = JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
69     kDataDumpRequest = JVMTI_EVENT_DATA_DUMP_REQUEST,
70     kMonitorWait = JVMTI_EVENT_MONITOR_WAIT,
71     kMonitorWaited = JVMTI_EVENT_MONITOR_WAITED,
72     kMonitorContendedEnter = JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
73     kMonitorContendedEntered = JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
74     kResourceExhausted = JVMTI_EVENT_RESOURCE_EXHAUSTED,
75     kGarbageCollectionStart = JVMTI_EVENT_GARBAGE_COLLECTION_START,
76     kGarbageCollectionFinish = JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
77     kObjectFree = JVMTI_EVENT_OBJECT_FREE,
78     kVmObjectAlloc = JVMTI_EVENT_VM_OBJECT_ALLOC,
79     // Internal event to mark a ClassFileLoadHook as one created with the can_retransform_classes
80     // capability.
81     kClassFileLoadHookRetransformable = JVMTI_MAX_EVENT_TYPE_VAL + 1,
82     kDdmPublishChunk = JVMTI_MAX_EVENT_TYPE_VAL + 2,
83     kObsoleteObjectCreated = JVMTI_MAX_EVENT_TYPE_VAL + 3,
84     kStructuralDexFileLoadHook = JVMTI_MAX_EVENT_TYPE_VAL + 4,
85     kMaxNormalEventTypeVal = kStructuralDexFileLoadHook,
86 
87     // All that follow are events used to implement internal JVMTI functions. They are not settable
88     // directly by agents.
89     kMinInternalEventTypeVal = kMaxNormalEventTypeVal + 1,
90 
91     // Internal event we use to implement the ForceEarlyReturn functions.
92     kForceEarlyReturnUpdateReturnValue = kMinInternalEventTypeVal,
93     kMaxInternalEventTypeVal = kForceEarlyReturnUpdateReturnValue,
94 
95     kMaxEventTypeVal = kMaxInternalEventTypeVal,
96 };
97 
98 constexpr jint kInternalEventCount = static_cast<jint>(ArtJvmtiEvent::kMaxInternalEventTypeVal) -
99                                      static_cast<jint>(ArtJvmtiEvent::kMinInternalEventTypeVal) + 1;
100 
101 using ArtJvmtiEventDdmPublishChunk = void (*)(jvmtiEnv *jvmti_env,
102                                               JNIEnv* jni_env,
103                                               jint data_type,
104                                               jint data_len,
105                                               const jbyte* data);
106 
107 using ArtJvmtiEventObsoleteObjectCreated = void (*)(jvmtiEnv *jvmti_env,
108                                                     jlong* obsolete_tag,
109                                                     jlong* new_tag);
110 
111 using ArtJvmtiEventStructuralDexFileLoadHook = void (*)(jvmtiEnv *jvmti_env,
112                                                         JNIEnv* jni_env,
113                                                         jclass class_being_redefined,
114                                                         jobject loader,
115                                                         const char* name,
116                                                         jobject protection_domain,
117                                                         jint dex_data_len,
118                                                         const unsigned char* dex_data,
119                                                         jint* new_dex_data_len,
120                                                         unsigned char** new_dex_data);
121 
122 // It is not enough to store a Thread pointer, as these may be reused. Use the pointer and the
123 // thread id.
124 // Note: We could just use the tid like tracing does.
125 using UniqueThread = std::pair<art::Thread*, uint32_t>;
126 
127 struct UniqueThreadHasher {
operatorUniqueThreadHasher128   std::size_t operator()(const UniqueThread& k) const {
129     return std::hash<uint32_t>{}(k.second) ^ (std::hash<void*>{}(k.first) << 1);
130   }
131 };
132 
133 struct ArtJvmtiEventCallbacks : jvmtiEventCallbacks {
ArtJvmtiEventCallbacksArtJvmtiEventCallbacks134   ArtJvmtiEventCallbacks()
135       : DdmPublishChunk(nullptr),
136         ObsoleteObjectCreated(nullptr),
137         StructuralDexFileLoadHook(nullptr) {
138     memset(this, 0, sizeof(jvmtiEventCallbacks));
139   }
140 
141   // Copies extension functions from other callback struct if it exists. There must not have been
142   // any modifications to this struct when it is called.
143   void CopyExtensionsFrom(const ArtJvmtiEventCallbacks* cb);
144 
145   jvmtiError Set(jint index, jvmtiExtensionEvent cb);
146 
147   ArtJvmtiEventDdmPublishChunk DdmPublishChunk;
148   ArtJvmtiEventObsoleteObjectCreated ObsoleteObjectCreated;
149   ArtJvmtiEventStructuralDexFileLoadHook StructuralDexFileLoadHook;
150 };
151 
152 bool IsExtensionEvent(jint e);
153 bool IsExtensionEvent(ArtJvmtiEvent e);
154 
155 // Convert a jvmtiEvent into a ArtJvmtiEvent
156 ALWAYS_INLINE static inline ArtJvmtiEvent GetArtJvmtiEvent(ArtJvmTiEnv* env, jvmtiEvent e);
157 
GetJvmtiEvent(ArtJvmtiEvent e)158 static inline jvmtiEvent GetJvmtiEvent(ArtJvmtiEvent e) {
159   if (UNLIKELY(e == ArtJvmtiEvent::kClassFileLoadHookRetransformable)) {
160     return JVMTI_EVENT_CLASS_FILE_LOAD_HOOK;
161   } else {
162     return static_cast<jvmtiEvent>(e);
163   }
164 }
165 
166 struct EventMask {
167   static constexpr size_t kEventsSize =
168       static_cast<size_t>(ArtJvmtiEvent::kMaxEventTypeVal) -
169       static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal) + 1;
170   std::bitset<kEventsSize> bit_set;
171 
EventIsInRangeEventMask172   static bool EventIsInRange(ArtJvmtiEvent event) {
173     return event >= ArtJvmtiEvent::kMinEventTypeVal && event <= ArtJvmtiEvent::kMaxEventTypeVal;
174   }
175 
176   void Set(ArtJvmtiEvent event, bool value = true) {
177     DCHECK(EventIsInRange(event));
178     bit_set.set(static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal),
179                 value);
180   }
181 
TestEventMask182   bool Test(ArtJvmtiEvent event) const {
183     DCHECK(EventIsInRange(event));
184     return bit_set.test(
185         static_cast<size_t>(event) - static_cast<size_t>(ArtJvmtiEvent::kMinEventTypeVal));
186   }
187 };
188 
189 struct EventMasks {
190   // The globally enabled events.
191   EventMask global_event_mask;
192 
193   // The per-thread enabled events.
194 
195   // TODO: Native thread objects are immovable, so we can use them as keys in an (unordered) map,
196   //       if necessary.
197   std::vector<std::pair<UniqueThread, EventMask>> thread_event_masks;
198 
199   // A union of the per-thread events, for fast-pathing.
200   EventMask unioned_thread_event_mask;
201 
202   EventMask& GetEventMask(art::Thread* thread);
203   EventMask* GetEventMaskOrNull(art::Thread* thread);
204   // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
205   // asserted in the function.
206   // Note that the 'env' passed in must be the same env this EventMasks is associated with.
207   void EnableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
208       // REQUIRES(env->event_info_mutex_);
209   // Circular dependencies mean we cannot see the definition of ArtJvmTiEnv so the mutex is simply
210   // asserted in the function.
211   // Note that the 'env' passed in must be the same env this EventMasks is associated with.
212   void DisableEvent(ArtJvmTiEnv* env, art::Thread* thread, ArtJvmtiEvent event);
213       // REQUIRES(env->event_info_mutex_);
214   bool IsEnabledAnywhere(ArtJvmtiEvent event);
215   // Make any changes to event masks needed for the given capability changes. If caps_added is true
216   // then caps is all the newly set capabilities of the jvmtiEnv. If it is false then caps is the
217   // set of all capabilities that were removed from the jvmtiEnv.
218   void HandleChangedCapabilities(const jvmtiCapabilities& caps, bool caps_added);
219 };
220 
221 namespace impl {
222 template <ArtJvmtiEvent kEvent> struct EventHandlerFunc { };
223 }  // namespace impl
224 
225 // Helper class for event handling.
226 class EventHandler {
227  public:
228   EventHandler();
229   ~EventHandler();
230 
231   // do cleanup for the event handler.
232   void Shutdown();
233 
234   // Register an env. It is assumed that this happens on env creation, that is, no events are
235   // enabled, yet.
236   void RegisterArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
237 
238   // Remove an env.
239   void RemoveArtJvmTiEnv(ArtJvmTiEnv* env) REQUIRES(!envs_lock_);
240 
IsEventEnabledAnywhere(ArtJvmtiEvent event)241   bool IsEventEnabledAnywhere(ArtJvmtiEvent event) const {
242     if (!EventMask::EventIsInRange(event)) {
243       return false;
244     }
245     return global_mask.Test(event);
246   }
247 
248   // Sets an internal event. Unlike normal JVMTI events internal events are not associated with any
249   // particular jvmtiEnv and are refcounted. This refcounting is done to allow us to easily enable
250   // events during functions and disable them during the requested event callback. Since these are
251   // used to implement various JVMTI functions these events always have a single target thread. If
252   // target is null the current thread is used.
253   jvmtiError SetInternalEvent(jthread target,
254                               ArtJvmtiEvent event,
255                               jvmtiEventMode mode)
256       REQUIRES(!envs_lock_, !art::Locks::mutator_lock_);
257 
258   jvmtiError SetEvent(ArtJvmTiEnv* env,
259                       jthread thread,
260                       ArtJvmtiEvent event,
261                       jvmtiEventMode mode)
262       REQUIRES(!envs_lock_);
263 
264   // Dispatch event to all registered environments. Since this one doesn't have a JNIEnv* it doesn't
265   // matter if it has the mutator_lock.
266   template <ArtJvmtiEvent kEvent, typename ...Args>
267   ALWAYS_INLINE
268   inline void DispatchEvent(art::Thread* thread, Args... args) const
269       REQUIRES(!envs_lock_);
270 
271   // Dispatch event to all registered environments stashing exceptions as needed. This works since
272   // JNIEnv* is always the second argument if it is passed to an event. Needed since C++ does not
273   // allow partial template function specialization.
274   //
275   // We need both of these since we want to make sure to push a stack frame when it is possible for
276   // the event to allocate local references.
277   template <ArtJvmtiEvent kEvent, typename ...Args>
278   ALWAYS_INLINE
279   inline void DispatchEvent(art::Thread* thread, JNIEnv* jnienv, Args... args) const
280       REQUIRES(!envs_lock_);
281 
282   // Tell the event handler capabilities were added/lost so it can adjust the sent events.If
283   // caps_added is true then caps is all the newly set capabilities of the jvmtiEnv. If it is false
284   // then caps is the set of all capabilities that were removed from the jvmtiEnv.
285   ALWAYS_INLINE
286   inline void HandleChangedCapabilities(ArtJvmTiEnv* env,
287                                         const jvmtiCapabilities& caps,
288                                         bool added)
289       REQUIRES(!envs_lock_);
290 
291   // Dispatch event to the given environment, only.
292   template <ArtJvmtiEvent kEvent, typename ...Args>
293   ALWAYS_INLINE
294   inline void DispatchEventOnEnv(ArtJvmTiEnv* env,
295                                  art::Thread* thread,
296                                  JNIEnv* jnienv,
297                                  Args... args) const
298       REQUIRES(!envs_lock_);
299 
300   // Dispatch event to the given environment, only.
301   template <ArtJvmtiEvent kEvent, typename ...Args>
302   ALWAYS_INLINE
303   inline void DispatchEventOnEnv(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const
304       REQUIRES(!envs_lock_);
305 
306   void AddDelayedNonStandardExitEvent(const art::ShadowFrame* frame, bool is_object, jvalue val)
307       REQUIRES_SHARED(art::Locks::mutator_lock_)
308       REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_);
309 
310   template<typename Visitor>
ForEachEnv(art::Thread * self,Visitor v)311   void ForEachEnv(art::Thread* self, Visitor v) REQUIRES(!envs_lock_) {
312     art::ReaderMutexLock mu(self, envs_lock_);
313     for (ArtJvmTiEnv* e : envs) {
314       if (e != nullptr) {
315         v(e);
316       }
317     }
318   }
319 
320  private:
321   void SetupTraceListener(JvmtiMethodTraceListener* listener, ArtJvmtiEvent event, bool enable);
322 
323   uint32_t GetInstrumentationEventsFor(ArtJvmtiEvent event);
324 
325   // Specifically handle the FramePop event which it might not always be possible to turn off.
326   void SetupFramePopTraceListener(bool enable);
327 
328   template <ArtJvmtiEvent kEvent, typename ...Args>
329   ALWAYS_INLINE
330   inline std::vector<impl::EventHandlerFunc<kEvent>> CollectEvents(art::Thread* thread,
331                                                                    Args... args) const
332       REQUIRES(!envs_lock_);
333 
334   template <ArtJvmtiEvent kEvent>
335   ALWAYS_INLINE
336   inline bool ShouldDispatchOnThread(ArtJvmTiEnv* env, art::Thread* thread) const;
337 
338   template <ArtJvmtiEvent kEvent, typename ...Args>
339   ALWAYS_INLINE
340   static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler,
341                                      JNIEnv* env,
342                                      Args... args)
343       REQUIRES(!envs_lock_);
344 
345   template <ArtJvmtiEvent kEvent, typename ...Args>
346   ALWAYS_INLINE
347   static inline void ExecuteCallback(impl::EventHandlerFunc<kEvent> handler, Args... args)
348       REQUIRES(!envs_lock_);
349 
350   // Public for use to collect dispatches
351   template <ArtJvmtiEvent kEvent, typename ...Args>
352   ALWAYS_INLINE
353   inline bool ShouldDispatch(ArtJvmTiEnv* env, art::Thread* thread, Args... args) const;
354 
355   ALWAYS_INLINE
356   inline bool NeedsEventUpdate(ArtJvmTiEnv* env,
357                                const jvmtiCapabilities& caps,
358                                bool added);
359 
360   // Recalculates the event mask for the given event.
361   ALWAYS_INLINE
362   inline void RecalculateGlobalEventMask(ArtJvmtiEvent event) REQUIRES(!envs_lock_);
363   ALWAYS_INLINE
364   inline void RecalculateGlobalEventMaskLocked(ArtJvmtiEvent event) REQUIRES_SHARED(envs_lock_);
365 
366   // Returns whether there are any active requests for the given event on the given thread. This
367   // should only be used while modifying the events for a thread.
368   bool GetThreadEventState(ArtJvmtiEvent event, art::Thread* thread)
369       REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
370 
371   template <ArtJvmtiEvent kEvent>
372   ALWAYS_INLINE inline void DispatchClassFileLoadHookEvent(art::Thread* thread,
373                                                            JNIEnv* jnienv,
374                                                            jclass class_being_redefined,
375                                                            jobject loader,
376                                                            const char* name,
377                                                            jobject protection_domain,
378                                                            jint class_data_len,
379                                                            const unsigned char* class_data,
380                                                            jint* new_class_data_len,
381                                                            unsigned char** new_class_data) const
382       REQUIRES(!envs_lock_);
383 
384   template <ArtJvmtiEvent kEvent>
385   ALWAYS_INLINE inline void DispatchClassLoadOrPrepareEvent(art::Thread* thread,
386                                                             JNIEnv* jnienv,
387                                                             jthread jni_thread,
388                                                             jclass klass) const
389       REQUIRES(!envs_lock_);
390 
391   // Sets up the global state needed for the first/last enable of an event across all threads
392   void HandleEventType(ArtJvmtiEvent event, bool enable);
393   // Perform deopts required for enabling the event on the given thread. Null thread indicates
394   // global event enabled.
395   jvmtiError HandleEventDeopt(ArtJvmtiEvent event, jthread thread, bool enable);
396   void HandleLocalAccessCapabilityAdded();
397   void HandleBreakpointEventsChanged(bool enable);
398 
399   bool OtherMonitorEventsEnabledAnywhere(ArtJvmtiEvent event);
400 
401   int32_t GetInternalEventRefcount(ArtJvmtiEvent event) const REQUIRES(envs_lock_);
402   // Increment internal event refcount for the given event and return the new count.
403   int32_t IncrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_);
404   // Decrement internal event refcount for the given event and return the new count.
405   int32_t DecrInternalEventRefcount(ArtJvmtiEvent event) REQUIRES(envs_lock_);
406 
407   int32_t& GetInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
408       REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
409   // Increment internal event refcount for the given event and return the new count.
410   int32_t IncrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
411       REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
412   // Decrement internal event refcount for the given event and return the new count.
413   int32_t DecrInternalEventThreadRefcount(ArtJvmtiEvent event, art::Thread* target)
414       REQUIRES(envs_lock_, art::Locks::thread_list_lock_);
415 
416   // List of all JvmTiEnv objects that have been created, in their creation order. It is a std::list
417   // since we mostly access it by iterating over the entire thing, only ever append to the end, and
418   // need to be able to remove arbitrary elements from it.
419   std::list<ArtJvmTiEnv*> envs GUARDED_BY(envs_lock_);
420 
421   // Close to top level lock. Nothing should be held when we lock this (except for mutator_lock_
422   // which is needed when setting new events).
423   mutable art::ReaderWriterMutex envs_lock_ ACQUIRED_AFTER(art::Locks::mutator_lock_);
424 
425   // A union of all enabled events, anywhere.
426   EventMask global_mask;
427 
428   std::unique_ptr<JvmtiEventAllocationListener> alloc_listener_;
429   std::unique_ptr<JvmtiDdmChunkListener> ddm_listener_;
430   std::unique_ptr<JvmtiGcPauseListener> gc_pause_listener_;
431   std::unique_ptr<JvmtiMethodTraceListener> method_trace_listener_;
432   std::unique_ptr<JvmtiMonitorListener> monitor_listener_;
433   std::unique_ptr<JvmtiParkListener> park_listener_;
434 
435   // True if frame pop has ever been enabled. Since we store pointers to stack frames we need to
436   // continue to listen to this event even if it has been disabled.
437   // TODO We could remove the listeners once all jvmtiEnvs have drained their shadow-frame vectors.
438   bool frame_pop_enabled;
439 
440   // The overall refcount for each internal event across all threads.
441   std::array<int32_t, kInternalEventCount> internal_event_refcount_ GUARDED_BY(envs_lock_);
442   // The refcount for each thread for each internal event.
443   // TODO We should clean both this and the normal EventMask lists up when threads end.
444   std::array<std::unordered_map<UniqueThread, int32_t, UniqueThreadHasher>, kInternalEventCount>
445       internal_event_thread_refcount_
446           GUARDED_BY(envs_lock_) GUARDED_BY(art::Locks::thread_list_lock_);
447 
448   friend class JvmtiMethodTraceListener;
449 };
450 
451 }  // namespace openjdkjvmti
452 
453 #endif  // ART_OPENJDKJVMTI_EVENTS_H_
454