1 /*
2  * Copyright (C) 2023 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 "berberis/runtime_primitives/recovery_code.h"
18 
19 #include <cstdint>
20 #include <initializer_list>
21 #include <utility>
22 
23 #include "berberis/base/checks.h"
24 #include "berberis/base/forever_map.h"
25 #include "berberis/base/tracing.h"
26 #include "berberis/guest_state/guest_state_opaque.h"
27 #include "berberis/runtime_primitives/code_pool.h"
28 
29 namespace berberis {
30 
31 namespace {
32 
33 ForeverMap<uintptr_t, uintptr_t> g_recovery_map;
34 bool g_recovery_map_initialized_ = false;
35 
FindExtraRecoveryCodeUnsafe(uintptr_t fault_addr)36 uintptr_t FindExtraRecoveryCodeUnsafe(uintptr_t fault_addr) {
37   CHECK(g_recovery_map_initialized_);
38   auto it = g_recovery_map.find(fault_addr);
39   if (it != g_recovery_map.end()) {
40     return it->second;
41   }
42   return 0;
43 }
44 
45 }  // namespace
46 
InitExtraRecoveryCodeUnsafe(std::initializer_list<std::pair<uintptr_t,uintptr_t>> fault_recovery_pairs)47 void InitExtraRecoveryCodeUnsafe(
48     std::initializer_list<std::pair<uintptr_t, uintptr_t>> fault_recovery_pairs) {
49   CHECK(!g_recovery_map_initialized_);
50   for (auto pair : fault_recovery_pairs) {
51     g_recovery_map[pair.first] = pair.second;
52   }
53   g_recovery_map_initialized_ = true;
54 }
55 
FindRecoveryCode(uintptr_t fault_addr,ThreadState * state)56 uintptr_t FindRecoveryCode(uintptr_t fault_addr, ThreadState* state) {
57   uintptr_t recovery_addr;
58   CHECK(state);
59   // Only look up in CodePool if we are inside generated code (interrupted by a
60   // signal). If a signal interrupts CodePool::Add then calling FindRecoveryCode
61   // in this state can cause deadlock.
62   if (GetResidence(*state) == kInsideGeneratedCode) {
63     // TODO(b/228188293): we might need to traverse all code pool instances.
64     recovery_addr = GetDefaultCodePoolInstance()->FindRecoveryCode(fault_addr);
65     if (recovery_addr) {
66       return recovery_addr;
67     }
68   }
69   // Extra recovery code is in read-only mode after the init, so we don't need mutexes.
70   // Note, that we cannot simply add extra recovery code to CodePool, since these
71   // fault addresses may be outside of generated code (e.g. interpreter).
72   recovery_addr = FindExtraRecoveryCodeUnsafe(fault_addr);
73   if (recovery_addr) {
74     TRACE("found recovery address outside of code pool");
75   }
76   return recovery_addr;
77 }
78 
79 }  // namespace berberis
80