// by vnull

#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <limits.h>

// i386
#define PAGESIZE 4096

int ble(){
	/* szatan powered */
	printf("fuck, no!\n");
	return 666;
}

int main()
{
	int r;
	const void *bleptr = (void *) &ble;
	const void *ptr = (void *)((int) (bleptr+PAGESIZE-1) & ~(PAGESIZE-1));
	const int len = ptr - bleptr;

	printf("code at %p\nble at %p (PS=%d)\nlen=%d\n", ptr, bleptr, PAGESIZE, len);

	/* czyli nie dostanie PROT_EXEC, ale na x86_32 PROT_READ = PROT_EXEC 
	   na x86_64 jak nie bedzie PRTO_EXEC to faktycznie powinno sie niewykoanc - SIGSEGV 
	   tzw. NX - non execute bit, procesory SPARC maja cos takiego np juz b. dlugo */
	if(mprotect(ptr, len, PROT_NONE)) {
		perror("mprotect");
		exit(-1);
	}
	r = ble();

	/* os zwroci % 255, o ile mozna wykonac */
	return r;
}


