1 /*
2  * Copyright 2020 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 #include <atomic>
18 #include <string_view>
19 
20 #include "arch/context.h"
21 #include "art_method-inl.h"
22 #include "jni.h"
23 #include "scoped_thread_state_change.h"
24 #include "stack.h"
25 #include "thread.h"
26 
27 namespace art {
28 namespace StackWalkConcurrentInstrument {
29 
30 std::atomic<bool> instrument_waiting = false;
31 std::atomic<bool> instrumented = false;
32 
33 // Spin lock.
WaitForInstrument()34 static void WaitForInstrument() REQUIRES_SHARED(Locks::mutator_lock_) {
35   ScopedThreadSuspension sts(Thread::Current(), ThreadState::kWaitingForDeoptimization);
36   instrument_waiting = true;
37   while (!instrumented) {
38   }
39 }
40 
41 class SelfStackWalkVisitor : public StackVisitor {
42  public:
REQUIRES_SHARED(Locks::mutator_lock_)43   explicit SelfStackWalkVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
44       : StackVisitor(thread, Context::Create(), StackWalkKind::kIncludeInlinedFrames) {}
45 
VisitFrame()46   bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
47     if (GetMethod()->GetNameView() == "$noinline$f") {
48       CHECK(!found_f_);
49       found_f_ = true;
50     } else if (GetMethod()->GetNameView() == "$noinline$g") {
51       CHECK(!found_g_);
52       found_g_ = true;
53       WaitForInstrument();
54     } else if (GetMethod()->GetNameView() == "$noinline$h") {
55       CHECK(!found_h_);
56       found_h_ = true;
57     }
58     return true;
59   }
60 
61   bool found_f_ = false;
62   bool found_g_ = false;
63   bool found_h_ = false;
64 };
65 
Java_Main_resetTest(JNIEnv *,jobject)66 extern "C" JNIEXPORT void JNICALL Java_Main_resetTest(JNIEnv*, jobject) {
67   instrument_waiting = false;
68   instrumented = false;
69 }
70 
Java_Main_doSelfStackWalk(JNIEnv *,jobject)71 extern "C" JNIEXPORT void JNICALL Java_Main_doSelfStackWalk(JNIEnv*, jobject) {
72   ScopedObjectAccess soa(Thread::Current());
73   SelfStackWalkVisitor sswv(Thread::Current());
74   sswv.WalkStack();
75   CHECK(sswv.found_f_);
76   CHECK(sswv.found_g_);
77   CHECK(sswv.found_h_);
78 }
Java_Main_waitAndDeopt(JNIEnv *,jobject,jobject target)79 extern "C" JNIEXPORT void JNICALL Java_Main_waitAndDeopt(JNIEnv*, jobject, jobject target) {
80   while (!instrument_waiting) {
81   }
82   bool timed_out = false;
83   Thread* other = Runtime::Current()->GetThreadList()->SuspendThreadByPeer(
84       target, true, SuspendReason::kInternal, &timed_out);
85   CHECK(!timed_out);
86   CHECK(other != nullptr);
87   ScopedSuspendAll ssa(__FUNCTION__);
88   Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(other);
89   MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
90   bool updated = other->ModifySuspendCount(Thread::Current(), -1, nullptr, SuspendReason::kInternal);
91   CHECK(updated);
92   instrumented = true;
93   return;
94 }
95 
96 }  // namespace StackWalkConcurrentInstrument
97 }  // namespace art
98