diff options
Diffstat (limited to 'libc/private/WriteProtected.h')
-rw-r--r-- | libc/private/WriteProtected.h | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/libc/private/WriteProtected.h b/libc/private/WriteProtected.h index 69a68229b..26c239ca1 100644 --- a/libc/private/WriteProtected.h +++ b/libc/private/WriteProtected.h @@ -46,6 +46,16 @@ class WriteProtected { WriteProtectedContents<T> contents; + int set_protection(int prot) { + auto addr = &contents; +#if __has_feature(hwaddress_sanitizer) + // The mprotect system call does not currently untag pointers, so do it + // ourselves. + addr = untag_address(addr); +#endif + return mprotect(reinterpret_cast<void*>(addr), PAGE_SIZE, prot); + } + public: WriteProtected() = default; BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtected); @@ -55,7 +65,7 @@ class WriteProtected { // multiple times by accident. memset(&contents, 0, sizeof(contents)); - if (mprotect(&contents, PAGE_SIZE, PROT_READ)) { + if (set_protection(PROT_READ)) { async_safe_fatal("failed to make WriteProtected nonwritable in initialize"); } } @@ -70,12 +80,12 @@ class WriteProtected { template <typename Mutator> void mutate(Mutator mutator) { - if (mprotect(&contents, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0) { + if (set_protection(PROT_READ | PROT_WRITE) != 0) { async_safe_fatal("failed to make WriteProtected writable in mutate: %s", strerror(errno)); } mutator(&contents.value); - if (mprotect(&contents, PAGE_SIZE, PROT_READ) != 0) { + if (set_protection(PROT_READ) != 0) { async_safe_fatal("failed to make WriteProtected nonwritable in mutate: %s", strerror(errno)); } |