-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslow.cpp
73 lines (57 loc) · 2.01 KB
/
slow.cpp
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
#include <sys/mman.h>
#include <cstring>
#include <unistd.h>
#include <cstdio>
#include <cassert>
#include <string>
#include "primes.h"
char* get_jmp_addr(char* jump_base, uint32_t i) {
return jump_base + (i * 7);
}
char* get_target_addr(char* target_base, uint32_t i) {
return target_base + (i * 8);
}
int main(int argc, char** argv) {
if (argc != 2) {
printf("Usage: %s <approx_shfit>\n", argv[0]);
return 1;
}
uint32_t shift = std::stoi(argv[1]);
printf("Generating permutation...\n");
auto perm = generate_cyclic_permutation(1 << 26, shift);
// Allocate the instruction and target buffers
const size_t size = 1ULL << 30;
const int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
const int flags = MAP_PRIVATE | MAP_ANONYMOUS;
const int fd = -1;
const off_t offset = 0;
assert(perm.size() < (size / (2 * 8))); // make sure that N fits
char* addr = (char*)mmap((void*)0x1000, size, prot, flags, fd, offset);
if (addr == MAP_FAILED) {
printf("mmap failed\n");
return 1;
}
std::memset(addr, 0, size);
// Write the instructions and target buffers
char* jump_instrs = addr;
char* jump_targets = addr + (size / 2);
printf("Preparing instrs, jump_instrs = %p, jump_targets = %p...\n", jump_instrs, jump_targets);
for (uint32_t i = 0; i < perm.size(); i++) {
char* J = get_jmp_addr(jump_instrs, i);
char* T = get_target_addr(jump_targets, i);
assert((uintptr_t)J <= (1u << 31));
uint32_t Ti = (uint32_t)(uintptr_t)T;
char* nextJ = get_jmp_addr(jump_instrs, perm.at(i));
assert((uintptr_t)nextJ <= (1u << 31));
*(uint32_t*)T = (uint32_t)(uintptr_t)nextJ;
// jmp *(Ti) = 0xff 0x24 0x25 (Ti)
J[0] = 0xff;
J[1] = 0x24;
J[2] = 0x25;
*(uint32_t*)(J + 3) = Ti;
}
printf("Jumping to start. PID = %d\n", (int)getpid());
asm volatile ("jmp *%0" : : "r"(jump_instrs));
assert(false); // Will never get here
return 0;
}