Permalink
Browse files

Implement mprotect function for Windows.

Fixes #79.
  • Loading branch information...
1 parent 364da65 commit 5bad8a57a656ee2514b0672894304931f0284413 @niemeyer niemeyer committed Sep 11, 2014
Showing with 41 additions and 3 deletions.
  1. +4 −0 all.cpp
  2. +27 −0 cpp/mmemwin.cpp
  3. +10 −3 testing.go
View
@@ -6,3 +6,7 @@
#include "cpp/connector.cpp"
#include "cpp/moc_all.cpp"
+
+#ifdef _WIN32
+#include "cpp/mmemwin.cpp"
+#endif
View
@@ -0,0 +1,27 @@
+#include <windows.h>
+
+#define protREAD 1
+#define protWRITE 2
+#define protEXEC 4
+
+extern "C" {
+
+int mprotect(void *addr, size_t len, int prot)
+{
+ DWORD wprot = 0;
+ if (prot & protWRITE) {
+ wprot = PAGE_READWRITE;
+ } else if (prot & protREAD) {
+ wprot = PAGE_READ;
+ }
+ if (prot & protEXEC) {
+ wprot <<= 4;
+ }
+ DWORD oldwprot;
+ if (!VirtualProtect(addr, len, wprot, &oldwprot)) {
+ return -1;
+ }
+ return 0;
+}
+
+} // extern "C"
View
@@ -1,6 +1,7 @@
package qml
-// #include <sys/mman.h>
+// #include <stdlib.h>
+// int mprotect(void *addr, size_t len, int prot);
import "C"
import (
@@ -27,6 +28,12 @@ func SetupTesting() {
fset(ptr(tmain), ptr(tstub), mmain)
}
+const (
+ protREAD = 1
+ protWRITE = 2
+ protEXEC = 4
+)
+
func fset(target, old, new uintptr) {
pageOffset := target % pageSize
pageAddr := target - pageOffset
@@ -44,8 +51,8 @@ func fset(target, old, new uintptr) {
binary.LittleEndian.PutUint64(newAddr, uint64(new))
// BSD's syscall package misses Mprotect. Use cgo instead.
- C.mprotect(unsafe.Pointer(pageAddr), C.size_t(len(mem)), C.PROT_EXEC|C.PROT_READ|C.PROT_WRITE)
- defer C.mprotect(unsafe.Pointer(pageAddr), C.size_t(len(mem)), C.PROT_EXEC|C.PROT_READ)
+ C.mprotect(unsafe.Pointer(pageAddr), C.size_t(len(mem)), protEXEC|protREAD|protWRITE)
+ defer C.mprotect(unsafe.Pointer(pageAddr), C.size_t(len(mem)), protEXEC|protREAD)
delta := make([]byte, 4)
for i, c := range mem[pageOffset:] {

0 comments on commit 5bad8a5

Please sign in to comment.