// 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 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'; }