summaryrefslogtreecommitdiff
path: root/targets/stack.c
blob: 6ba2735629f0f1d35630f4a2fa840553cfab0147 (plain) (blame)
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
// What a stack's own instructions and the string instructions touch, by
// instruction: pushes and pops, calls and returns, enter and leave, the
// flags pushed; copies and fills, repeated, both ways. Built at -O0 with
// the stack protector: `fill` keeps a canary, and its i and n share a word.
#include <stdint.h>

char dst[64], src[64] = "abcdefghijklmnopqrstuvwxyz";
// Past the file's last page: zeroes in a mapping of their own.
char big[1 << 16];

__attribute__((noinline)) static void strings(void) {
    void *d = dst, *s = src;
    uint64_t n = 3;
    __asm__ volatile("rep movsq" : "+D"(d), "+S"(s), "+c"(n) : : "memory");
    d = dst + 40;
    n = 5;
    __asm__ volatile("rep stosb" : "+D"(d), "+c"(n) : "a"(0x41) : "memory");
    // Down from the last byte, the direction flag set.
    d = dst + 63;
    s = src + 25;
    n = 4;
    __asm__ volatile("std; rep movsb; cld" : "+D"(d), "+S"(s), "+c"(n) : : "memory");
    __asm__ volatile("pushfq; popfq; push %%rbx; pop %%rbx; enter $16, $0; leave" : : : "memory");
}

__attribute__((noinline)) static int fill(int n) {
    char buf[16];
    for (int i = 0; i < n; i++) buf[i] = 'A' + i;
    return buf[n - 1];
}

int main(void) {
    strings();
    big[4096] = 1;
    return fill(16) + big[4096] + dst[0] - 'P' - 1 - 'a';
}