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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int testBlockExecute() {
size_t size;
unsigned char *buf;
unsigned char *block;
int i = 2;
unsigned char *done;
int (*pputs)(const char*)=&puts;
done = &&end;
size = (&&block_end - &&block_begin) + (&&block2_end - &&block2_begin);
size += (&&blockBis_end - &&blockBis_begin);
fprintf(stderr,"size buf\t%d\n",size);
buf = mmap(NULL, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|0x20, -1, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
block = buf;
memcpy(buf, &&block_begin, &&block_end - &&block_begin);
buf += &&block_end - &&block_begin;
memcpy(buf, &&blockBis_begin, &&blockBis_end - &&blockBis_begin);
buf += &&blockBis_end - &&blockBis_begin;
memcpy(buf, &&block2_begin, &&block2_end - &&block2_begin);
buf += &&block2_end - &&block2_begin;
if (msync(block, size, MS_INVALIDATE) != 0) {
perror("msync");
exit(EXIT_FAILURE);
}
fprintf(stderr,"début de l'exécution\n");
goto *block;
fprintf(stderr,"ce morceau n'est pas atteignable\n");
blockBis_begin:
i /= 5;
blockBis_end:
block_begin:
pputs("addition");
i *= 3;
block_end:
block2_begin: {
__asm__ volatile ("jmp *%0" : : "m" (done));
}
block2_end:
end:
return i;
}
int main (void) {
int i;
i = testBlockExecute();
printf("%d\n", i);
return EXIT_SUCCESS;
} |