1. binet
  2. mana-core-cxxutils

Commits

calaf  committed 8ba32fa

add support for counting number of times page was accessed, and for accessing list of protected ptrs

  • Participants
  • Parent commits 582e7dc
  • Branches default
  • Tags CxxUtils-00-00-18

Comments (0)

Files changed (3)

File ChangeLog

View file
  • Ignore whitespace
-2009-03-04  Paolo Calafiura  <calaf@localhost.localdomain>
+2009-03-04  Paolo Calafiura  
+	* Tagging CxxUtils-00-00-18
 	* class PageAccessControl: add support for counting number of times page was accessed, and for accessing list of protected ptrs
 	* src/procmaps.cxx (procmaps::procmaps): do not print maps content
 

File src/PageAccessControl.cxx

View file
  • Ignore whitespace
 #include <sys/mman.h> /* mprotect */
 #include "CxxUtils/PageAccessControl.h"
 #include "CxxUtils/page_access.h"  
+#include <iostream>
 using athena::page_address;
 using athena::next_page_address;
 
 PageAccessControl::Entry::Entry(void* a, size_t l, int p, void* pl): 
-  addr(page_address(a)), lenProt(l), prot(p), leak(pl) {}
+  addr(page_address(a)), lenProt(l), prot(p), leak(pl), restored(0) {}
 
 
 void
 #endif
       //      free(entry->leak);
       entry->leak=0;
+      ++(entry->restored);
     }
   } else printf("WARNING no entry in procmap for addr=%p, page protection not restored \n",addr);
   return (rc == 0);
 PageAccessControl::protectPage(const void* caddr, size_t objSize, int prot) {
   void* addr(const_cast<void*>(caddr));
   int  rc(-1);
-  const procmaps::Entry *e=m_pmaps.getEntry(addr,false);
+  const procmaps::Entry *e=m_pmaps.getEntry(addr,true);
   //this is the lenght of the range we are going to protect
   if (0 != e) {
     void *pageAddr = page_address(addr);
     if (pageProt != prot) {
       //fill up the space from nextProt to nextUnprot to avoid allocations
       //in the locked pages, and SEGVs...
-      void *leak(0);
-      if (0 == (prot & PROT_WRITE)) {
-	size_t lenLeak(nextUnprot-nextProt-1);
-	leak=malloc(lenLeak);
-	if ((size_t)leak<nextUnprot && (size_t)leak + lenLeak>=nextUnprot) {
-	  //we do not want to allocate our buffer memory past the current
-	  //page, so trim it down
-	  free(leak);
-	  lenLeak=nextUnprot - (size_t)leak -1;
-	  leak=malloc(lenLeak);
-	} 
-	if ((size_t)leak >= nextUnprot) {
-	  //leak has been allocated into next page
-	  //better get rid of it as it will likely
-	  //be locked by another protectPage
-	  free(leak);
-	  leak=0; 
-	} else {
-#ifdef DEBUG
-	  printf("PageAccessControl::protectPage DEBUG: fill up space from %p to 0x%x to avoid allocations in locked pages\n",
-		 leak, (int)leak+lenLeak);
-#endif
-	}
-      }
-      
+//       void *leak(0);
+//       if (0 == (prot & PROT_WRITE)) {
+// 	size_t lenLeak(nextUnprot-nextProt-1);
+// 	leak=malloc(lenLeak);
+// 	if ((size_t)leak<nextUnprot && (size_t)leak + lenLeak>=nextUnprot) {
+// 	  //we do not want to allocate our buffer memory past the current
+// 	  //page, so trim it down
+// 	  free(leak);
+// 	  lenLeak=nextUnprot - (size_t)leak -1;
+// 	  leak=malloc(lenLeak);
+// 	} 
+// 	if (leak < pageAddr ||
+// 	    (size_t)leak >= nextUnprot) {
+// 	  //leak has been allocated into previous/next page
+// 	  //better get rid of it as it will likely
+// 	  //be locked by another protectPage
+// 	  free(leak);
+// 	  leak=0; 
+// 	} else {
+// #ifdef DEBUG
+// 	  printf("PageAccessControl::protectPage DEBUG: fill up space from %p to 0x%x to avoid allocations in locked pages\n",
+// 		 leak, (int)leak+lenLeak);
+// #endif
+// 	}
+//       }
+          
       if (0 == (rc = mprotect( pageAddr,
 			       lenProt,
 			       prot))) {
-	m_protected.push_back(Entry(pageAddr,lenProt, pageProt, leak));
+	m_protected.push_back(Entry(pageAddr,lenProt, pageProt, 0));
 	m_protectedIsSorted=false; //FIXME we should use a mapvector
 #ifdef DEBUG
 	printf("PageAccessControl::protectPage DEBUG: set protection %i for page range %p - 0x%x containing address range=%p - 0x%x\n",
   } else printf("PageAccessControl::protectPage WARNING: no entry in procmap for addr=%p, page protection not restored \n",addr);
   return (rc == 0);
 }
+
+bool PageAccessControl::accessed(const void* address) const {
+  bool acc(false);
+  //fixme: poor man implementation
+  Entry eaxx(const_cast<void*>(address),0,0,0);
+  PageAccessControl::const_iterator ia(beginProtectedPtrs()),
+    ea(endProtectedPtrs());
+  while (!acc && ia != ea) {
+    //    std::cout << address << "page addr " << eaxx.addr << " ia " << ia->addr << " res " << ia->restored << std::endl;
+    acc = (eaxx.addr == ia->addr && 0 != ia->restored);
+    ++ia;
+  }
+  return acc;
+}

File src/procmaps.cxx

View file
  • Ignore whitespace
 procmaps::procmaps(size_t entries) {
   if (!s_pmapsLoaded) {
      procmaps::s_pmaps.reserve(entries);
-     loadMaps(true);
+     loadMaps(false);
      s_pmapsLoaded=true;
    }
 }