Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis

bnoordhuis/gccbug.c

Created May 19, 2013
Embed
What would you like to do?
gcc + asm + on-stack iovec = uninitialized memory
// Compile at -O0 and -O1 or higher and compare the output of
// `strace -e pwritev a.out`. At -O0:
//
// pwritev(1, [{"Hello ", 6}, {"world!", 6}, {"\n", 1}], 3, 0) = -1 ESPIPE
// (Illegal seek)
//
// At -O1 or higher:
//
// pwritev(1, [{"", 0}, {"H\211l$\330L\211d$\340H\215-\367\10 \0L\215%\350\10
// \0H\211\\$\320L\211l"..., 4195360}, {"", 0}], 3, 0) = -1 ESPIPE (Illegal
// seek)
//
// Adding "memory" to the clobber list fixes it. GCC 4.7.2 bug or not?
#include <sys/syscall.h>
#include <sys/uio.h>
int main(void)
{
const struct iovec vec[] = {
{ .iov_base="Hello ", .iov_len=sizeof("Hello ") - 1 },
{ .iov_base="world!", .iov_len=sizeof("world!") - 1 },
{ .iov_base="\n", .iov_len=1 }
};
long rval;
__asm__ __volatile__ (
"xor %%r10, %%r10;"
"xor %%r8, %%r8;"
"syscall;"
: "=a" (rval)
: "a" (SYS_pwritev), "D" (1), "S" (vec), "d" (3)
: "%rcx", "%r11", "%r10", "%r8"
);
return 0;
}
@indutny

This comment has been minimized.

Copy link

@indutny indutny commented May 20, 2013

Can't reproduce it on gcc 4.2.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment