osdep: introduce qemu_mprotect_rwx/none

Backports commit 5fa64b3130af9a45e7e2a904bde1f8cfb72be5c9 from qemu
This commit is contained in:
Emilio G. Cota 2018-03-14 10:37:35 -04:00 committed by Lioncash
parent 5ad6116f20
commit 3fe9866ffe
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
8 changed files with 124 additions and 0 deletions

View file

@ -230,6 +230,7 @@ copy $(SolutionDir)..\include\unicorn\*.h $(SolutionDir)distro\include\unicorn\
<ClCompile Include="..\..\..\qemu\util\log.c" />
<ClCompile Include="..\..\..\qemu\util\qht.c" />
<ClCompile Include="..\..\..\qemu\util\module.c" />
<ClCompile Include="..\..\..\qemu\util\osdep.c" />
<ClCompile Include="..\..\..\qemu\util\oslib-win32.c" />
<ClCompile Include="..\..\..\qemu\util\qemu-error.c" />
<ClCompile Include="..\..\..\qemu\util\qemu-thread-win32.c" />

View file

@ -47,6 +47,9 @@
<ClCompile Include="..\..\..\qemu\util\module.c">
<Filter>qemu\util</Filter>
</ClCompile>
<ClCompile Include="..\..\..\qemu\util\osdep.c">
<Filter>qemu\util</Filter>
</ClCompile>
<ClCompile Include="..\..\..\qemu\util\oslib-win32.c">
<Filter>qemu\util</Filter>
</ClCompile>

View file

@ -58,6 +58,7 @@
<ClCompile Include="..\..\..\qemu\util\log.c" />
<ClCompile Include="..\..\..\qemu\util\qht.c" />
<ClCompile Include="..\..\..\qemu\util\module.c" />
<ClCompile Include="..\..\..\qemu\util\osdep.c" />
<ClCompile Include="..\..\..\qemu\util\oslib-win32.c" />
<ClCompile Include="..\..\..\qemu\util\qemu-error.c" />
<ClCompile Include="..\..\..\qemu\util\qemu-thread-win32.c" />

View file

@ -183,6 +183,9 @@
<ClCompile Include="..\..\..\qemu\util\module.c">
<Filter>qemu\util</Filter>
</ClCompile>
<ClCompile Include="..\..\..\qemu\util\osdep.c">
<Filter>qemu\util</Filter>
</ClCompile>
<ClCompile Include="..\..\..\qemu\util\oslib-win32.c">
<Filter>qemu\util</Filter>
</ClCompile>

View file

@ -86,10 +86,16 @@
#include "sysemu/os-win32.h"
#endif
#ifdef CONFIG_POSIX
#include "sysemu/os-posix.h"
#endif
#include "glib_compat.h"
#include "qemu/typedefs.h"
struct uc_struct;
/*
* We have a lot of unaudited code that may fail in strange ways, or
* even be a security risk during migration, if you disable assertions
@ -242,6 +248,9 @@
QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
#endif
int qemu_mprotect_rwx(struct uc_struct *uc, void *addr, size_t size);
int qemu_mprotect_none(struct uc_struct *uc, void *addr, size_t size);
void *qemu_try_memalign(size_t alignment, size_t size);
void *qemu_memalign(size_t alignment, size_t size);
void *qemu_anon_ram_alloc(size_t size, uint64_t *align);

View file

@ -0,0 +1,37 @@
/*
* win32 specific declarations
*
* Copyright (c) 2003-2008 Fabrice Bellard
* Copyright (c) 2010 Jes Sorensen <Jes.Sorensen@redhat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef QEMU_OS_POSIX_H
#define QEMU_OS_POSIX_H
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/un.h>
#endif

View file

@ -10,5 +10,6 @@ util-obj-y += crc32c.o
util-obj-y += host-utils.o
util-obj-y += getauxval.o
util-obj-y += log.o
util-obj-y += osdep.o
util-obj-y += qht.o
util-obj-y += range.o

69
qemu/util/osdep.c Normal file
View file

@ -0,0 +1,69 @@
/*
* QEMU low level functions
*
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/cutils.h"
#include "uc_priv.h"
static int qemu_mprotect__osdep(struct uc_struct *uc, void *addr, size_t size, int prot)
{
g_assert(!((uintptr_t)addr & ~uc->qemu_real_host_page_mask));
g_assert(!(size & ~uc->qemu_real_host_page_mask));
#ifdef _WIN32
DWORD old_protect;
if (!VirtualProtect(addr, size, prot, &old_protect)) {
//error_report("%s: VirtualProtect failed with error code %ld",
// __func__, GetLastError());
return -1;
}
return 0;
#else
if (mprotect(addr, size, prot)) {
//error_report("%s: mprotect failed: %s", __func__, strerror(errno));
return -1;
}
return 0;
#endif
}
int qemu_mprotect_rwx(struct uc_struct *uc, void *addr, size_t size)
{
#ifdef _WIN32
return qemu_mprotect__osdep(uc, addr, size, PAGE_EXECUTE_READWRITE);
#else
return qemu_mprotect__osdep(uc, addr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
#endif
}
int qemu_mprotect_none(struct uc_struct *uc, void *addr, size_t size)
{
#ifdef _WIN32
return qemu_mprotect__osdep(uc, addr, size, PAGE_NOACCESS);
#else
return qemu_mprotect__osdep(uc, addr, size, PROT_NONE);
#endif
}