VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/RTMemProtect-posix.cpp@ 62514

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 2.9 KB
Line 
1/* $Id: RTMemProtect-posix.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/alloc.h>
32#include <iprt/assert.h>
33#include <iprt/param.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36
37#include <errno.h>
38#include <sys/mman.h>
39
40
41RTDECL(int) RTMemProtect(void *pv, size_t cb, unsigned fProtect) RT_NO_THROW_DEF
42{
43 /*
44 * Validate input.
45 */
46 if (cb == 0)
47 {
48 AssertMsgFailed(("!cb\n"));
49 return VERR_INVALID_PARAMETER;
50 }
51 if (fProtect & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC))
52 {
53 AssertMsgFailed(("fProtect=%#x\n", fProtect));
54 return VERR_INVALID_PARAMETER;
55 }
56
57 /*
58 * Convert the flags.
59 */
60 int fProt;
61#if RTMEM_PROT_NONE == PROT_NONE \
62 && RTMEM_PROT_READ == PROT_READ \
63 && RTMEM_PROT_WRITE == PROT_WRITE \
64 && RTMEM_PROT_EXEC == PROT_EXEC
65 fProt = fProtect;
66#else
67 Assert(!RTMEM_PROT_NONE);
68 if (!fProtect)
69 fProt = PROT_NONE;
70 else
71 {
72 fProt = 0;
73 if (fProtect & RTMEM_PROT_READ)
74 fProt |= PROT_READ;
75 if (fProtect & RTMEM_PROT_WRITE)
76 fProt |= PROT_WRITE;
77 if (fProtect & RTMEM_PROT_EXEC)
78 fProt |= PROT_EXEC;
79 }
80#endif
81
82 /*
83 * Align the request.
84 */
85 cb += (uintptr_t)pv & PAGE_OFFSET_MASK;
86 pv = (void *)((uintptr_t)pv & ~PAGE_OFFSET_MASK);
87
88 /*
89 * Change the page attributes.
90 */
91 int rc = mprotect(pv, cb, fProt);
92 if (!rc)
93 return rc;
94 return RTErrConvertFromErrno(errno);
95}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette