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/guest_function_wrapper_impl.h"
18
19 #include <mutex>
20 #include <string>
21 #include <tuple>
22 #include <utility>
23
24 #include "berberis/assembler/machine_code.h"
25 #include "berberis/base/forever_alloc.h"
26 #include "berberis/base/forever_map.h"
27 #include "berberis/base/logging.h"
28 #include "berberis/code_gen_lib/gen_wrapper.h"
29 #include "berberis/guest_abi/guest_type.h"
30 #include "berberis/guest_state/guest_addr.h"
31 #include "berberis/runtime_primitives/code_pool.h"
32 #include "berberis/runtime_primitives/host_code.h"
33 #include "berberis/runtime_primitives/translation_cache.h" // LookupGuestPC
34
35 namespace berberis {
36
37 namespace {
38
39 // Guest function wrappers are identified by guest function address, signature
40 // string and guest runner. The guest function address alone is not enough.
41 //
42 // Example: on RISC-V soft float guest, these functions are binary equal:
43 // float foo(float x, float y) { return y; }
44 // int bar(int x, int y) { return y; }
45 // And it is possible that guest compiler generates code only once and sets
46 // both foo and bar to it. However, on x86 hosts and RISC-V hard float guest,
47 // foo and bar need different wrappers, as floats and ints are passed and
48 // returned in differently.
49 //
50 // Example: imagine we wrap thread_func to run from pthread_create.
51 // In addition to running thread_func, the guest runner we provide also cleans
52 // up guest thread on exit. If we also want to call thread_func in regular way,
53 // we need another guest runner, otherwise we'll get an unexpected thread
54 // cleanup.
55 //
56 // TODO(b/232598137): implementation is inefficient, check if that matters!
57 class WrapperCache {
58 public:
GetInstance()59 static WrapperCache* GetInstance() {
60 static auto* g_wrapper_cache = NewForever<WrapperCache>();
61 return g_wrapper_cache;
62 }
63
Find(GuestAddr pc,const char * signature,HostCode guest_runner) const64 HostCode Find(GuestAddr pc, const char* signature, HostCode guest_runner) const {
65 std::lock_guard<std::mutex> lock(mutex_);
66 auto it = map_.find(std::make_tuple(pc, signature, guest_runner));
67 if (it != map_.end()) {
68 return it->second;
69 }
70 return nullptr;
71 }
72
73 // Another thread might have already inserted a wrapper for this key.
74 // In this case, discard the new wrapper and return the existing one.
Insert(GuestAddr pc,const char * signature,HostCode guest_runner,MachineCode * mc)75 HostCode Insert(GuestAddr pc, const char* signature, HostCode guest_runner, MachineCode* mc) {
76 std::lock_guard<std::mutex> lock(mutex_);
77 std::pair<WrapperMap::iterator, bool> res = map_.insert(
78 std::make_pair(std::make_tuple(pc, std::string(signature), guest_runner), nullptr));
79 if (res.second) {
80 res.first->second = AsHostCode(GetFunctionWrapperCodePoolInstance()->Add(mc));
81 }
82 return res.first->second;
83 }
84
SlowFindGuestAddrByWrapperAddr(void * wrapper_addr)85 GuestAddr SlowFindGuestAddrByWrapperAddr(void* wrapper_addr) {
86 std::lock_guard<std::mutex> lock(mutex_);
87 for (auto& entry : map_) {
88 if (entry.second == wrapper_addr) {
89 // Return pc.
90 return std::get<0>(entry.first);
91 }
92 }
93 return 0;
94 }
95
96 private:
97 using WrapperKey = std::tuple<GuestAddr, std::string, HostCode>;
98 using WrapperMap = ForeverMap<WrapperKey, HostCode>;
99
100 WrapperCache() = default;
101
102 WrapperMap map_;
103 mutable std::mutex mutex_;
104
105 friend WrapperCache* NewForever<WrapperCache>();
106 };
107
108 IsAddressGuestExecutableFunc g_is_address_guest_executable_func = nullptr;
109
110 } // namespace
111
InitGuestFunctionWrapper(IsAddressGuestExecutableFunc func)112 void InitGuestFunctionWrapper(IsAddressGuestExecutableFunc func) {
113 g_is_address_guest_executable_func = func;
114 }
115
WrapGuestFunctionImpl(GuestAddr pc,const char * signature,GuestRunnerFunc runner,const char * name)116 HostCode WrapGuestFunctionImpl(GuestAddr pc,
117 const char* signature,
118 GuestRunnerFunc runner,
119 const char* name) {
120 if (!pc) {
121 return nullptr;
122 }
123
124 HostCode guest_runner = AsHostCode(runner);
125 WrapperCache* wrapper_cache = WrapperCache::GetInstance();
126 HostCode wrapper = wrapper_cache->Find(pc, signature, guest_runner);
127 if (!wrapper) {
128 // We can only wrap executable guest address! Even though execution will still fail, an early
129 // check here helps a lot when debugging.
130 // One special case is wrapped host function (trampoline) that is passed back to the host.
131 // It should still go through the guest function wrapper and call trampoline code.
132 CHECK(g_is_address_guest_executable_func);
133 if (!g_is_address_guest_executable_func(pc) &&
134 !TranslationCache::GetInstance()->IsHostFunctionWrapped(pc)) {
135 LOG_ALWAYS_FATAL("Trying to wrap non-executable guest address 0x%zx", pc);
136 }
137 MachineCode mc;
138 GenWrapGuestFunction(&mc, pc, signature, guest_runner, name);
139 wrapper = wrapper_cache->Insert(pc, signature, guest_runner, &mc);
140 }
141 return wrapper;
142 }
143
SlowFindGuestAddrByWrapperAddr(void * wrapper_addr)144 GuestAddr SlowFindGuestAddrByWrapperAddr(void* wrapper_addr) {
145 return WrapperCache::GetInstance()->SlowFindGuestAddrByWrapperAddr(wrapper_addr);
146 }
147
148 } // namespace berberis
149