Created
November 17, 2015 15:36
-
-
Save jonathan-beard/cb1c261c1d2487d20ffc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <cstdlib> | |
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <iomanip> | |
int | |
main( int argc, char **argv ) | |
{ | |
int *a( nullptr ); | |
const auto page_size( sysconf( _SC_PAGESIZE ) ); | |
static const auto length( 100 ); | |
const auto size( length * sizeof( int ) ); | |
posix_memalign( reinterpret_cast< void** >( &a ), page_size, page_size * 2 ); | |
std::cout << "start address: " << std::hex | |
<< reinterpret_cast< std::uintptr_t >( a ) << "\n"; | |
/** make sure it's writeable **/ | |
for( auto index( 0 ); index < length; index ++ ) | |
{ | |
a[ index ] = 5; | |
} | |
auto *last_page( | |
reinterpret_cast< void* >( | |
reinterpret_cast< std::uintptr_t >( a ) + page_size ) ); | |
std::cout << "last page start: " << std::hex << | |
reinterpret_cast< std::uintptr_t >( last_page ) << "\n"; | |
mprotect( last_page, page_size , PROT_NONE ); | |
std::cout << "protected page starting at: " << | |
std::hex << reinterpret_cast< std::uintptr_t >( last_page ) << "\n"; | |
std::cout << "calling free on: " << std::hex << | |
reinterpret_cast< std::uintptr_t >( a ) << "\n"; | |
std::free( a ); | |
return( EXIT_SUCCESS ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment