bionic: libc: WriteProtected use max_page_size()
In the 4k targets there is no fucntional difference since
max_page_size() == page_size() == 4096.
On a 16kb device max_page_size() == 65536 and page_size() == 16384.
However, aligning up does not incur any memory regressions
since the .bss section is still be backes in page-sizeed chunks.
See: go/16k-page-aligned-variables
Bug: 296275298
Test: mma
Change-Id: I41c3e410f3b84c24eeb969c9aeca4b33a8d6170a
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
diff --git a/libc/private/WriteProtected.h b/libc/private/WriteProtected.h
index 746f72a..2faaf77 100644
--- a/libc/private/WriteProtected.h
+++ b/libc/private/WriteProtected.h
@@ -25,15 +25,16 @@
#include <async_safe/log.h>
#include "platform/bionic/macros.h"
+#include "platform/bionic/page.h"
template <typename T>
union WriteProtectedContents {
T value;
- char padding[PAGE_SIZE];
+ char padding[max_page_size()];
WriteProtectedContents() = default;
BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtectedContents);
-} __attribute__((aligned(PAGE_SIZE)));
+} __attribute__((aligned(max_page_size())));
// Write protected wrapper class that aligns its contents to a page boundary,
// and sets the memory protection to be non-writable, except when being modified
@@ -41,8 +42,8 @@
template <typename T>
class WriteProtected {
public:
- static_assert(sizeof(T) < PAGE_SIZE,
- "WriteProtected only supports contents up to PAGE_SIZE");
+ static_assert(sizeof(T) < max_page_size(),
+ "WriteProtected only supports contents up to max_page_size()");
static_assert(__is_pod(T), "WriteProtected only supports POD contents");
WriteProtected() = default;
@@ -80,7 +81,7 @@
// ourselves.
addr = untag_address(addr);
#endif
- if (mprotect(reinterpret_cast<void*>(addr), PAGE_SIZE, prot) == -1) {
+ if (mprotect(reinterpret_cast<void*>(addr), max_page_size(), prot) == -1) {
async_safe_fatal("WriteProtected mprotect %x failed: %s", prot, strerror(errno));
}
}