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
|
#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <sys/mman.h>
#include <unistd.h>
struct node { struct node *next; uint64_t value; };
struct node head, middle, tail;
volatile uint64_t counter;
uint64_t *stack_pointer;
__attribute__((noinline)) void checkpoint(void) { __asm__ volatile("" ::: "memory"); }
__attribute__((naked, noinline)) void address_probe(uint64_t *stack, struct node *data, size_t index) {
__asm__ volatile(
".globl operand_stack\noperand_stack: lea 8(%rdi), %rax\n"
".globl operand_indexed\noperand_indexed: lea (%rsi,%rdx,8), %rax\n"
".globl operand_relative\noperand_relative: mov head(%rip), %rax\n"
"ret\n");
}
int main(void) {
uint64_t stack_value = 42;
stack_pointer = &stack_value;
const size_t size = (size_t)1 << 32;
int fd = memfd_create("ztrace-test-segment", 0);
assert(fd >= 0 && ftruncate(fd, size) == 0);
unsigned char *bytes = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert(bytes != MAP_FAILED);
close(fd);
bytes[0] = 'A';
bytes[size - 1] = 'Z';
head = (struct node){&middle, 1};
middle = (struct node){&tail, 2};
tail = (struct node){&head, 3};
for (;;) { ++counter; checkpoint(); address_probe(&stack_value, &head, 2); }
}
|