//===-- protected_pages.h -------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // This file provides protected pages that fault when accessing prior or past // it. This is useful to check memory functions that must not access outside of // the provided size limited buffer. //===----------------------------------------------------------------------===// #ifndef LIBC_TEST_SRC_STRING_MEMORY_UTILS_PROTECTED_PAGES_H #define LIBC_TEST_SRC_STRING_MEMORY_UTILS_PROTECTED_PAGES_H #include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX #if defined(LIBC_FULL_BUILD) || !defined(LIBC_TARGET_OS_IS_LINUX) #error "Protected pages requires mmap and cannot be used in full build mode." #endif // defined(LIBC_FULL_BUILD) || !defined(LIBC_TARGET_OS_IS_LINUX) #include "src/__support/macros/attributes.h" // LIBC_INLINE #include <stddef.h> // size_t #include <stdint.h> // uint8_t #include <sys/mman.h> // mmap, munmap #include <unistd.h> // sysconf, _SC_PAGESIZE // Returns mmap page size. LIBC_INLINE size_t GetPageSize() { … } // Represents a page of memory whose access can be configured throught the // 'WithAccess' function. Accessing data above or below this page will trap as // it is sandwiched between two pages with no read / write access. struct Page { … }; // Allocates 5 consecutive pages that will trap if accessed. // | page layout | access | page name | // |-------------|--------|:---------:| // | 0 | trap | | // | 1 | custom | A | // | 2 | trap | | // | 3 | custom | B | // | 4 | trap | | // // The pages A and B can be retrieved as with 'GetPageA' / 'GetPageB' and their // accesses can be customized through the 'WithAccess' function. struct ProtectedPages { … }; #endif // LIBC_TEST_SRC_STRING_MEMORY_UTILS_PROTECTED_PAGES_H