diff mbox

[v2,40/45] osdep: introduce qemu_mprotect_rwx/none

Message ID 1500235468-15341-41-git-send-email-cota@braap.org (mailing list archive)
State New, archived
Headers show

Commit Message

Emilio Cota July 16, 2017, 8:04 p.m. UTC
Signed-off-by: Emilio G. Cota <cota@braap.org>
---
 include/qemu/osdep.h |  2 ++
 util/osdep.c         | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

Comments

Richard Henderson July 18, 2017, 4:26 a.m. UTC | #1
On 07/16/2017 10:04 AM, Emilio G. Cota wrote:
> +static int qemu_mprotect__osdep(void *addr, size_t size, int prot)
> +{
> +    void *start = QEMU_ALIGN_PTR_DOWN(addr, qemu_real_host_page_size);
> +    void *end = QEMU_ALIGN_PTR_UP(addr + size, qemu_real_host_page_size);

I'm not keen on this.  Any good reason for it as opposed to asserting that the 
inputs are already page aligned?


r~
Emilio Cota July 18, 2017, 4:57 a.m. UTC | #2
On Mon, Jul 17, 2017 at 18:26:09 -1000, Richard Henderson wrote:
> On 07/16/2017 10:04 AM, Emilio G. Cota wrote:
> >+static int qemu_mprotect__osdep(void *addr, size_t size, int prot)
> >+{
> >+    void *start = QEMU_ALIGN_PTR_DOWN(addr, qemu_real_host_page_size);
> >+    void *end = QEMU_ALIGN_PTR_UP(addr + size, qemu_real_host_page_size);
> 
> I'm not keen on this.  Any good reason for it as opposed to asserting that
> the inputs are already page aligned?

No particular reason other than "kept the same behaviour we had".

Let's go with asserts, I like that approach much better actually.

		E.
diff mbox

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 3cb36e6..dcecfbc 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -348,6 +348,8 @@  void sigaction_invoke(struct sigaction *action,
 #endif
 
 int qemu_madvise(void *addr, size_t len, int advice);
+int qemu_mprotect_rwx(void *addr, size_t size);
+int qemu_mprotect_none(void *addr, size_t size);
 
 int qemu_open(const char *name, int flags, ...);
 int qemu_close(int fd);
diff --git a/util/osdep.c b/util/osdep.c
index 90f4f11..85df97e 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -90,6 +90,46 @@  int qemu_madvise(void *addr, size_t len, int advice)
 #endif
 }
 
+static int qemu_mprotect__osdep(void *addr, size_t size, int prot)
+{
+    void *start = QEMU_ALIGN_PTR_DOWN(addr, qemu_real_host_page_size);
+    void *end = QEMU_ALIGN_PTR_UP(addr + size, qemu_real_host_page_size);
+#ifdef _WIN32
+    DWORD old_protect;
+
+    if (!VirtualProtect(start, end - start, prot, &old_protect)) {
+        error_report("%s: VirtualProtect failed with error code %d",
+                     __func__, GetLastError());
+        return -1;
+    }
+    return 0;
+#else
+    if (mprotect(start, end - start, prot)) {
+        error_report("%s: mprotect failed: %s", __func__, strerror(errno));
+        return -1;
+    }
+    return 0;
+#endif
+}
+
+int qemu_mprotect_rwx(void *addr, size_t size)
+{
+#ifdef _WIN32
+    return qemu_mprotect__osdep(addr, size, PAGE_EXECUTE_READWRITE);
+#else
+    return qemu_mprotect__osdep(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
+#endif
+}
+
+int qemu_mprotect_none(void *addr, size_t size)
+{
+#ifdef _WIN32
+    return qemu_mprotect__osdep(addr, size, PAGE_NOACCESS);
+#else
+    return qemu_mprotect__osdep(addr, size, PROT_NONE);
+#endif
+}
+
 #ifndef _WIN32
 /*
  * Dups an fd and sets the flags