1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_RUNTIME_SIGNAL_HANDLER_H_ 17 #define PANDA_RUNTIME_SIGNAL_HANDLER_H_ 18 19 #include <signal.h> // NOLINTNEXTLINE(modernize-deprecated-headers) 20 #include <cstdint> 21 #include <iostream> 22 #include <vector> 23 #include <libpandabase/macros.h> 24 #include "runtime/include/mem/panda_containers.h" 25 26 namespace panda { 27 28 class Method; 29 class SignalHandler; 30 31 class SignalManager { 32 public: 33 void InitSignals(); 34 IsInitialized()35 bool IsInitialized() 36 { 37 return is_init_; 38 } 39 40 bool SignalActionHandler(int sig, siginfo_t *info, void *context); 41 bool InOatCode(const siginfo_t *siginfo, const void *context, bool check_bytecode_pc); 42 bool InOtherCode(int sig, siginfo_t *info, void *context); 43 44 void AddHandler(SignalHandler *handler, bool oat_code); 45 46 void RemoveHandler(SignalHandler *handler); 47 void GetMethodAndReturnPcAndSp(const siginfo_t *siginfo, const void *context, const Method **out_method, 48 const uintptr_t *out_return_pc, const uintptr_t *out_sp); 49 GetAllocator()50 mem::InternalAllocatorPtr GetAllocator() 51 { 52 return allocator_; 53 } 54 55 void DeleteHandlersArray(); 56 SignalManager(mem::InternalAllocatorPtr allocator)57 explicit SignalManager(mem::InternalAllocatorPtr allocator) : allocator_(allocator) {} 58 SignalManager(SignalManager &&) = delete; 59 SignalManager &operator=(SignalManager &&) = delete; 60 virtual ~SignalManager() = default; 61 62 private: 63 bool is_init_ {false}; 64 mem::InternalAllocatorPtr allocator_; 65 PandaVector<SignalHandler *> oat_code_handler_; 66 PandaVector<SignalHandler *> other_handlers_; 67 NO_COPY_SEMANTIC(SignalManager); 68 }; 69 70 class SignalHandler { 71 public: 72 SignalHandler() = default; 73 74 virtual bool Action(int sig, siginfo_t *siginfo, void *context) = 0; 75 76 SignalHandler(SignalHandler &&) = delete; 77 SignalHandler &operator=(SignalHandler &&) = delete; 78 virtual ~SignalHandler() = default; 79 80 private: 81 NO_COPY_SEMANTIC(SignalHandler); 82 }; 83 84 class NullPointerHandler final : public SignalHandler { 85 public: 86 NullPointerHandler() = default; 87 88 bool Action(int sig, siginfo_t *siginfo, void *context) override; 89 90 NullPointerHandler(NullPointerHandler &&) = delete; 91 NullPointerHandler &operator=(NullPointerHandler &&) = delete; 92 ~NullPointerHandler() override; 93 94 private: 95 NO_COPY_SEMANTIC(NullPointerHandler); 96 }; 97 98 } // namespace panda 99 100 #endif // PANDA_RUNTIME_SIGNAL_HANDLER_H_ 101