File: memprot.c

package info (click to toggle)
fis-gtm 6.3-007-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,284 kB
  • sloc: ansic: 328,861; asm: 5,182; csh: 5,102; sh: 1,918; awk: 291; makefile: 69; sed: 13
file content (47 lines) | stat: -rw-r--r-- 1,328 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/****************************************************************
 *								*
 *	Copyright 2014 Fidelity Information Services, Inc	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/
#include <sys/mman.h>
#include "mdef.h"
#include "memprot.h"

OS_PAGE_SIZE_DECLARE

/*
 * Ensure base address has at least <size> bytes of accessible space, followed by an inaccessible (PROT_NONE) page.
 */
void memprot(mstr *base, uint4 size)
{
	int	status;

	if (base->len < size)
	{
		if (NULL != base->addr)
			munmap(base->addr, base->len + OS_PAGE_SIZE);	/* clear previous allocation */
		base->len = (mstr_len_t)ROUND_UP(size, OS_PAGE_SIZE);
		base->addr = (char *)mmap(NULL, base->len + OS_PAGE_SIZE, PROT_READ | PROT_WRITE,
											MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		if (MAP_FAILED == base->addr)
		{
			base->len = 0;
			base->addr = NULL;
		} else
		{
			status = mprotect(base->addr + base->len, OS_PAGE_SIZE, PROT_NONE);
			if (-1 == status)
			{
				munmap(base->addr, base->len + OS_PAGE_SIZE);
				base->len = 0;
				base->addr = NULL;
			}
		}
	}
	return;
}