1 /*
2  * Copyright (C) 2012 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 "entrypoints/entrypoint_utils-inl.h"
18 #include "mirror/art_method.h"
19 #include "mirror/object-inl.h"
20 #include "verifier/dex_gc_map.h"
21 #include "stack.h"
22 
23 namespace art {
24 
25 class ShadowFrameCopyVisitor : public StackVisitor {
26  public:
ShadowFrameCopyVisitor(Thread * self)27   explicit ShadowFrameCopyVisitor(Thread* self) : StackVisitor(self, NULL), prev_frame_(NULL),
28       top_frame_(NULL) {}
29 
VisitFrame()30   bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
31     if (IsShadowFrame()) {
32       ShadowFrame* cur_frame = GetCurrentShadowFrame();
33       size_t num_regs = cur_frame->NumberOfVRegs();
34       mirror::ArtMethod* method = cur_frame->GetMethod();
35       uint32_t dex_pc = cur_frame->GetDexPC();
36       ShadowFrame* new_frame = ShadowFrame::Create(num_regs, NULL, method, dex_pc);
37 
38       const uint8_t* gc_map = method->GetNativeGcMap(sizeof(void*));
39       verifier::DexPcToReferenceMap dex_gc_map(gc_map);
40       const uint8_t* reg_bitmap = dex_gc_map.FindBitMap(dex_pc);
41       for (size_t reg = 0; reg < num_regs; ++reg) {
42         if (TestBitmap(reg, reg_bitmap)) {
43           new_frame->SetVRegReference(reg, cur_frame->GetVRegReference(reg));
44         } else {
45           new_frame->SetVReg(reg, cur_frame->GetVReg(reg));
46         }
47       }
48 
49       if (prev_frame_ != NULL) {
50         prev_frame_->SetLink(new_frame);
51       } else {
52         top_frame_ = new_frame;
53       }
54       prev_frame_ = new_frame;
55     }
56     return true;
57   }
58 
GetShadowFrameCopy()59   ShadowFrame* GetShadowFrameCopy() {
60     return top_frame_;
61   }
62 
63  private:
TestBitmap(int reg,const uint8_t * reg_vector)64   static bool TestBitmap(int reg, const uint8_t* reg_vector) {
65     return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0;
66   }
67 
68   ShadowFrame* prev_frame_;
69   ShadowFrame* top_frame_;
70 };
71 
art_portable_test_suspend_from_code(Thread * self)72 extern "C" void art_portable_test_suspend_from_code(Thread* self)
73     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74   CheckSuspend(self);
75   if (Runtime::Current()->GetInstrumentation()->ShouldPortableCodeDeoptimize()) {
76     // Save out the shadow frame to the heap
77     ShadowFrameCopyVisitor visitor(self);
78     visitor.WalkStack(true);
79     self->SetDeoptimizationShadowFrame(visitor.GetShadowFrameCopy());
80     self->SetDeoptimizationReturnValue(JValue());
81     self->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
82   }
83 }
84 
art_portable_push_shadow_frame_from_code(Thread * thread,ShadowFrame * new_shadow_frame,mirror::ArtMethod * method,uint32_t num_vregs)85 extern "C" ShadowFrame* art_portable_push_shadow_frame_from_code(Thread* thread,
86                                                                  ShadowFrame* new_shadow_frame,
87                                                                  mirror::ArtMethod* method,
88                                                                  uint32_t num_vregs) {
89   ShadowFrame* old_frame = thread->PushShadowFrame(new_shadow_frame);
90   new_shadow_frame->SetMethod(method);
91   new_shadow_frame->SetNumberOfVRegs(num_vregs);
92   return old_frame;
93 }
94 
95 }  // namespace art
96