ATLAS Offline Software
Loading...
Searching...
No Matches
PageAccessControl Class Reference

FIXME Access to the underlying protect/restore page access. More...

#include <PageAccessControl.h>

Collaboration diagram for PageAccessControl:

Classes

class  Entry
 protection of a memory region (see mprotect(2)) More...

Public Types

typedef std::vector< Entryprotected_t
 the list of protected pages
typedef protected_t::const_iterator const_iterator

Public Member Functions

 PageAccessControl (size_t reservedSize=65535)
 PageAccessControl (procmaps &pmaps, size_t reservedSize=65535)
template<typename T>
bool protectPage (T *addr, int prot)
 protect the page containing addr, record the amount of memory we protected NOTE To avoid SEGV, if PROT_WRITE is requested the remainder of the page containing the object at addr will be leaked with malloc before being write-locked
template<typename T>
bool forbidPage (const T *addr)
 forbid access to the page containing addr, setting its prot to PROT_NONE
bool forbidPage (const void *addr, size_t objSize)
 forbid access to the page containing addr, setting its prot to PROT_NONE
bool forbidPage (const void *addr)
 FIXME this will not work well for objects spanning across pages.
bool protectPage (const void *addr, size_t objSize, int prot)
 void* version of protectPage. Used to implement all others
bool restorePageProt (const void *addr)
void sort ()
const protected_tprotectedPtrs () const
const_iterator beginProtectedPtrs () const
const_iterator endProtectedPtrs () const
void reset ()
bool accessed (const void *address) const
 was the page containing address accessed?

Private Attributes

procmaps m_pmaps
 the content of /proc/self/maps
protected_t m_protected
bool m_protectedIsSorted

Detailed Description

FIXME Access to the underlying protect/restore page access.

Using the underlying access to page containing address.

pages containing selected addresses and to restore their original protection.

When startMonitoring/initialize is called a

Member Typedef Documentation

◆ const_iterator

typedef protected_t::const_iterator PageAccessControl::const_iterator

Definition at line 92 of file PageAccessControl.h.

◆ protected_t

typedef std::vector<Entry> PageAccessControl::protected_t

the list of protected pages

Definition at line 91 of file PageAccessControl.h.

Constructor & Destructor Documentation

◆ PageAccessControl() [1/2]

PageAccessControl::PageAccessControl ( size_t reservedSize = 65535)
inline

Definition at line 29 of file PageAccessControl.h.

29 :
30 m_pmaps(),
32 {
33 //we must reserve enough elements, or we risk vector allocating in a protected page during handle()...
34 m_protected.reserve(reservedSize);
35 }
procmaps m_pmaps
the content of /proc/self/maps

◆ PageAccessControl() [2/2]

PageAccessControl::PageAccessControl ( procmaps & pmaps,
size_t reservedSize = 65535 )
inline

Definition at line 36 of file PageAccessControl.h.

36 :
37 m_pmaps(pmaps),
39 {
40 //we must reserve enough elements, or we risk vector allocating in a protected page during handle()...
41 m_protected.reserve(reservedSize);
42 }

Member Function Documentation

◆ accessed()

bool PageAccessControl::accessed ( const void * address) const

was the page containing address accessed?

Definition at line 118 of file PageAccessControl.cxx.

118 {
119 bool acc(false);
120 //fixme: poor man implementation
121 Entry eaxx(address,0,0,0);
123 ea(endProtectedPtrs());
124 while (!acc && ia != ea) {
125 // std::cout << address << "page addr " << eaxx.addr << " ia " << ia->addr << " res " << ia->restored << std::endl;
126 acc = (eaxx.addr == ia->addr && 0 != ia->restored);
127 ++ia;
128 }
129 return acc;
130}
const_iterator beginProtectedPtrs() const
protected_t::const_iterator const_iterator
const_iterator endProtectedPtrs() const
protection of a memory region (see mprotect(2))

◆ beginProtectedPtrs()

const_iterator PageAccessControl::beginProtectedPtrs ( ) const
inline

Definition at line 94 of file PageAccessControl.h.

94{ return m_protected.begin(); }

◆ endProtectedPtrs()

const_iterator PageAccessControl::endProtectedPtrs ( ) const
inline

Definition at line 95 of file PageAccessControl.h.

95{ return m_protected.end(); }

◆ forbidPage() [1/3]

template<typename T>
bool PageAccessControl::forbidPage ( const T * addr)
inline

forbid access to the page containing addr, setting its prot to PROT_NONE

Definition at line 55 of file PageAccessControl.h.

55 {
56 return forbidPage(addr, sizeof(T));
57 }
bool forbidPage(const T *addr)
forbid access to the page containing addr, setting its prot to PROT_NONE

◆ forbidPage() [2/3]

bool PageAccessControl::forbidPage ( const void * addr)
inline

FIXME this will not work well for objects spanning across pages.

Definition at line 65 of file PageAccessControl.h.

65 {
66 return protectPage(addr, 4, PROT_NONE);
67 }
bool protectPage(T *addr, int prot)
protect the page containing addr, record the amount of memory we protected NOTE To avoid SEGV,...

◆ forbidPage() [3/3]

bool PageAccessControl::forbidPage ( const void * addr,
size_t objSize )
inline

forbid access to the page containing addr, setting its prot to PROT_NONE

Definition at line 60 of file PageAccessControl.h.

60 {
61 return protectPage(addr, objSize, PROT_NONE);
62 }

◆ protectedPtrs()

const protected_t & PageAccessControl::protectedPtrs ( ) const
inline

Definition at line 93 of file PageAccessControl.h.

93{ return m_protected; }

◆ protectPage() [1/2]

bool PageAccessControl::protectPage ( const void * addr,
size_t objSize,
int prot )

void* version of protectPage. Used to implement all others

Definition at line 56 of file PageAccessControl.cxx.

56 {
57 int rc(-1);
58 const procmaps::Entry *e=m_pmaps.getEntry(addr,true);
59 //this is the length of the range we are going to protect
60 if (0 != e) {
61 const void *pageAddr = page_address(addr);
62 size_t lenProt = (size_t)addr - (size_t)(pageAddr) + objSize;
63 size_t nextProt = (size_t)addr + objSize;
64 size_t nextUnprot = (size_t)(next_page_address((void*)(nextProt-1)));
65 int pageProt(PROT_NONE);
66 if (e->readable) pageProt |= PROT_READ;
67 if (e->writable) pageProt |= PROT_WRITE;
68 if (e->executable) pageProt |= PROT_EXEC;
69 if (pageProt != prot) {
70 //fill up the space from nextProt to nextUnprot to avoid allocations
71 //in the locked pages, and SEGVs...
72// void *leak(0);
73// if (0 == (prot & PROT_WRITE)) {
74// size_t lenLeak(nextUnprot-nextProt-1);
75// leak=malloc(lenLeak);
76// if ((size_t)leak<nextUnprot && (size_t)leak + lenLeak>=nextUnprot) {
77// //we do not want to allocate our buffer memory past the current
78// //page, so trim it down
79// free(leak);
80// lenLeak=nextUnprot - (size_t)leak -1;
81// leak=malloc(lenLeak);
82// }
83// if (leak < pageAddr ||
84// (size_t)leak >= nextUnprot) {
85// //leak has been allocated into previous/next page
86// //better get rid of it as it will likely
87// //be locked by another protectPage
88// free(leak);
89// leak=0;
90// } else {
91// #ifdef DEBUG
92// printf("PageAccessControl::protectPage DEBUG: fill up space from %p to 0x%x to avoid allocations in locked pages\n",
93// leak, (int)leak+lenLeak);
94// #endif
95// }
96// }
97
98 void* pageAddr_nc ATLAS_THREAD_SAFE = const_cast<void*> (pageAddr);
99 if (0 == (rc = mprotect( pageAddr_nc,
100 lenProt,
101 prot))) {
102 m_protected.push_back(Entry(pageAddr,lenProt, pageProt, 0));
103 m_protectedIsSorted=false; //FIXME we should use a mapvector
104#ifdef DEBUG
105 printf("PageAccessControl::protectPage DEBUG: set protection %i for page range %p - 0x%lx containing address range=%p - 0x%lx\n",
106 prot,
107 pageAddr,
108 (long unsigned int)(nextUnprot - 1),
109 addr,
110 (long unsigned int)(nextProt -1) );
111#endif
112 }
113 } else rc=0;
114 } else printf("PageAccessControl::protectPage WARNING: no entry in procmap for addr=%p, page protection not restored \n",addr);
115 return (rc == 0);
116}
static Double_t rc
const void * page_address(const void *addr)
void * next_page_address(void *addr)
#define ATLAS_THREAD_SAFE

◆ protectPage() [2/2]

template<typename T>
bool PageAccessControl::protectPage ( T * addr,
int prot )
inline

protect the page containing addr, record the amount of memory we protected NOTE To avoid SEGV, if PROT_WRITE is requested the remainder of the page containing the object at addr will be leaked with malloc before being write-locked

Definition at line 49 of file PageAccessControl.h.

49 {
50 return protectPage(addr, sizeof(T), prot);
51 }

◆ reset()

void PageAccessControl::reset ( )
inline

Definition at line 96 of file PageAccessControl.h.

96{ m_protected.clear(); }

◆ restorePageProt()

bool PageAccessControl::restorePageProt ( const void * addr)

Definition at line 27 of file PageAccessControl.cxx.

27 {
28 int rc(-1);
29 sort();
30 Entry ea(addr,0,0,0);
31 protected_t::iterator entry=
32 std::lower_bound(m_protected.begin(), m_protected.end(), ea);
33 if (entry != m_protected.end() &&
34 entry->addr == ea.addr) {
35 //found it. Restore page prot
36 const void* pageAddr = page_address(entry->addr);
37 void* pageAddr_nc ATLAS_THREAD_SAFE = const_cast<void*> (pageAddr);
38 rc=mprotect( pageAddr_nc, entry->lenProt, entry->prot);
39 if (rc==0) {
40#ifdef DEBUG
41 printf("PageAccessControl::restorePageProt DEBUG: restored protection %i for page %p containing address %p \n",
42 entry->prot,
43 page_address(entry->addr),
44 entry->addr);
45 printf(" FIXME NOT Freeing memory at %p \n", entry->leak );
46#endif
47 // free(entry->leak);
48 entry->leak=0;
49 ++(entry->restored);
50 }
51 } else printf("WARNING no entry in procmap for addr=%p, page protection not restored \n",addr);
52 return (rc == 0);
53}

◆ sort()

void PageAccessControl::sort ( )

Definition at line 20 of file PageAccessControl.cxx.

20 {
22 std::sort(m_protected.begin(), m_protected.end());
24 }
25}
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.

Member Data Documentation

◆ m_pmaps

procmaps PageAccessControl::m_pmaps
private

the content of /proc/self/maps

Definition at line 104 of file PageAccessControl.h.

◆ m_protected

protected_t PageAccessControl::m_protected
private

Definition at line 109 of file PageAccessControl.h.

◆ m_protectedIsSorted

bool PageAccessControl::m_protectedIsSorted
private

Definition at line 110 of file PageAccessControl.h.


The documentation for this class was generated from the following files: