summaryrefslogtreecommitdiff
path: root/libc/private/WriteProtected.h
diff options
context:
space:
mode:
authorScott Lobdell <slobdell@google.com>2019-08-25 12:20:54 -0700
committerScott Lobdell <slobdell@google.com>2019-08-25 12:20:54 -0700
commit4f9bfdcaca2414c8959986f0a4d73f16cb15e1c4 (patch)
tree540bab5498d276cbbfad24c48a7ff989ee8b920a /libc/private/WriteProtected.h
parentbfda022dd6fbbcea60e9f52496d90ece514b32da (diff)
parentf77cc9b224c35fa7d1d71e7c374ef19e47b5f6a5 (diff)
Merge RP1A.190822.001
Change-Id: Iaf90835a99d87f6246798efd2cea6fe9f750ea18
Diffstat (limited to 'libc/private/WriteProtected.h')
-rw-r--r--libc/private/WriteProtected.h16
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));
}