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/runtime_library.h"
18
19 #include <sys/syscall.h>
20
21 #include "berberis/base/checks.h"
22 #include "berberis/base/tracing.h"
23 #include "berberis/guest_os_primitives/scoped_pending_signals.h"
24 #include "berberis/guest_state/guest_addr.h"
25 #include "berberis/guest_state/guest_state_opaque.h"
26
27 namespace berberis {
28
29 // ATTENTION: this symbol gets called directly, without PLT. To keep text
30 // sharable we should prevent preemption of this symbol, so do not export it!
31 // TODO(b/232598137): may be set default visibility to protected instead?
berberis_HandleNoExec(ThreadState * state)32 extern "C" __attribute__((used, __visibility__("hidden"))) void berberis_HandleNoExec(
33 ThreadState* state) {
34 CHECK(state);
35 // We are about to raise SIGSEGV. Let guest handler (if any) run immediately.
36 // It's safe since guest state is synchronized here. More context at b/143786256.
37 ScopedPendingSignalsDisabler disable_pending_signals(GetGuestThread(*state));
38 // LR register is usually useful even if we came here via jump instead of
39 // call because compilers rarely use LR for general-purpose calculations.
40 CPUState& cpu = GetCPUState(*state);
41 TRACE("Trying to execute non-executable code at %p called from %p",
42 ToHostAddr<void>(GetInsnAddr(cpu)),
43 ToHostAddr<void>(GetLinkRegister(cpu)));
44 siginfo_t info{};
45 info.si_signo = SIGSEGV;
46 info.si_code = SEGV_ACCERR;
47 info.si_addr = ToHostAddr<void>(GetInsnAddr(cpu));
48 syscall(SYS_rt_tgsigqueueinfo, GetpidSyscall(), GettidSyscall(), SIGSEGV, &info);
49 }
50
51 } // namespace berberis
52