| 25 | } |
| 26 | |
| 27 | int main(void) { |
| 28 | char exe[PATH_MAX]; |
| 29 | ssize_t exe_len = readlink("/proc/self/exe", exe, sizeof(exe) - 1); |
| 30 | if (exe_len < 0) { |
| 31 | return 1; |
| 32 | } |
| 33 | exe[exe_len] = '\0'; |
| 34 | |
| 35 | FILE *maps = fopen("/proc/self/maps", "r"); |
| 36 | if (!maps) { |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | uintptr_t target = (uintptr_t)&allocate; |
| 41 | uintptr_t start = 0, end = 0; |
| 42 | unsigned long long offset = 0; |
| 43 | char line[PATH_MAX + 128]; |
| 44 | while (fgets(line, sizeof(line), maps)) { |
| 45 | uintptr_t s, e; |
| 46 | char perms[5]; |
| 47 | unsigned long long off; |
| 48 | if (sscanf(line, "%lx-%lx %4s %llx", &s, &e, perms, &off) == 4 && |
| 49 | perms[2] == 'x' && s <= target && target < e) { |
| 50 | start = s; |
| 51 | end = e; |
| 52 | offset = off; |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | fclose(maps); |
| 57 | if (!start) { |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | int fd = open(exe, O_RDONLY); |
| 62 | if (fd < 0) { |
| 63 | return 1; |
| 64 | } |
| 65 | uint8_t *copy = mmap(NULL, end - start, PROT_READ | PROT_EXEC, MAP_PRIVATE, |
| 66 | fd, (off_t)offset); |
| 67 | close(fd); |
| 68 | if (copy == MAP_FAILED) { |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | allocate_fn remapped = (allocate_fn)(void *)(copy + (target - start)); |
| 73 | free(allocate(malloc, 1111)); |
| 74 | free(remapped(malloc, 2222)); |
| 75 | maps = fopen("/proc/self/maps", "r"); |
| 76 | if (!maps) { |
| 77 | return 1; |
| 78 | } |
| 79 | while (fgets(line, sizeof(line), maps)) { |
| 80 | char perms[5]; |
| 81 | if (sscanf(line, "%*x-%*x %4s", perms) == 1 && perms[2] == 'x' && |
| 82 | strstr(line, exe)) { |
| 83 | fputs(line, stdout); |
| 84 | } |
nothing calls this directly
no test coverage detected