@@ -46,6 +46,16 @@ class WriteProtected {
46
46
47
47
WriteProtectedContents<T> contents;
48
48
49
+ int set_protection (int prot) {
50
+ auto addr = reinterpret_cast <uintptr_t >(&contents);
51
+ #if __has_feature(hwaddress_sanitizer)
52
+ // The mprotect system call does not currently untag pointers, so do it
53
+ // ourselves.
54
+ addr &= (1ULL << 56 ) - 1 ;
55
+ #endif
56
+ return mprotect (reinterpret_cast <void *>(addr), PAGE_SIZE, prot);
57
+ }
58
+
49
59
public:
50
60
WriteProtected () = default ;
51
61
BIONIC_DISALLOW_COPY_AND_ASSIGN (WriteProtected);
@@ -55,7 +65,7 @@ class WriteProtected {
55
65
// multiple times by accident.
56
66
memset (&contents, 0 , sizeof (contents));
57
67
58
- if (mprotect (&contents, PAGE_SIZE, PROT_READ)) {
68
+ if (set_protection ( PROT_READ)) {
59
69
async_safe_fatal (" failed to make WriteProtected nonwritable in initialize" );
60
70
}
61
71
}
@@ -70,12 +80,12 @@ class WriteProtected {
70
80
71
81
template <typename Mutator>
72
82
void mutate (Mutator mutator) {
73
- if (mprotect (&contents, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0 ) {
83
+ if (set_protection ( PROT_READ | PROT_WRITE) != 0 ) {
74
84
async_safe_fatal (" failed to make WriteProtected writable in mutate: %s" ,
75
85
strerror (errno));
76
86
}
77
87
mutator (&contents.value );
78
- if (mprotect (&contents, PAGE_SIZE, PROT_READ) != 0 ) {
88
+ if (set_protection ( PROT_READ) != 0 ) {
79
89
async_safe_fatal (" failed to make WriteProtected nonwritable in mutate: %s" ,
80
90
strerror (errno));
81
91
}
0 commit comments