1 /* 2 * Copyright (c) 2025 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 COMMON_COMPONENTS_MUTATOR_SAFEPOINT_PAGE_MANAGER_H 17 #define COMMON_COMPONENTS_MUTATOR_SAFEPOINT_PAGE_MANAGER_H 18 19 #include <sys/mman.h> 20 21 #include "common_components/base/globals.h" 22 #include "common_components/base/sys_call.h" 23 #include "securec.h" 24 25 namespace common { 26 class SafepointPageManager { 27 public: SafepointPageManager()28 SafepointPageManager() {} 29 Init()30 void Init() 31 { 32 readablePage_ = reinterpret_cast<uint8_t*>( 33 mmap(nullptr, COMMON_PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); 34 unreadablePage_ = reinterpret_cast<uint8_t*>( 35 mmap(nullptr, COMMON_PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); 36 unreadablePageForRawData_ = reinterpret_cast<uint8_t*>( 37 mmap(nullptr, COMMON_PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); 38 LOGF_CHECK((readablePage_ != MAP_FAILED && unreadablePage_ != MAP_FAILED && 39 unreadablePageForRawData_ != MAP_FAILED)) << "allocate safepoint page failed!"; 40 } 41 ~SafepointPageManager()42 ~SafepointPageManager() 43 { 44 LOGE_IF(UNLIKELY_CC(munmap(readablePage_, COMMON_PAGE_SIZE) != EOK)) << 45 "munmap failed in SafepointPageManager readablePage destruction, errno: " << errno; 46 LOGE_IF(UNLIKELY_CC(munmap(unreadablePage_, COMMON_PAGE_SIZE) != EOK)) << 47 "munmap failed in SafepointPageManager unreadablePage destruction, errno: " << errno; 48 LOGE_IF(UNLIKELY_CC(munmap(unreadablePageForRawData_, COMMON_PAGE_SIZE) != EOK)) << 49 "munmap failed in SafepointPageManager unreadablePageForRawData destruction, errno: " << errno; 50 } 51 GetSafepointReadablePage()52 uint8_t* GetSafepointReadablePage() const { return readablePage_; } 53 GetSafepointUnreadablePage()54 uint8_t* GetSafepointUnreadablePage() const { return unreadablePage_; } 55 56 // refactor this code: move to where it is used. GetUnreadablePage()57 uint8_t* GetUnreadablePage() const { return unreadablePageForRawData_; } 58 59 private: 60 uint8_t* readablePage_ = nullptr; 61 uint8_t* unreadablePage_ = nullptr; 62 uint8_t* unreadablePageForRawData_ = nullptr; 63 }; 64 } // namespace common 65 66 #endif // COMMON_COMPONENTS_MUTATOR_SAFEPOINT_PAGE_MANAGER_H 67