Skip to content

va_start() is mishandled on Windows #12534

@timurrrr

Description

@timurrrr
Bugzilla Link 12162
Resolution DUPLICATE
Resolved on Sep 21, 2015 22:00
Version trunk
OS Windows NT
Blocks llvm/llvm-bugzilla-archive#13707
CC @AaronBallman,@asl,@majnemer,@efriedma-quic,@tritao,@kcc,@nico,@rnk

Extended Description

$ more va_args.c # Reproducer
#include <stdarg.h>
#include <stdio.h>

void foo(int A, ...) {
int B;
va_list L;
va_start(L, A);
B = va_arg(L, int);
printf("foo(%d, %d)\n", A, B);
va_end(L);
}

int main(void) { foo(1, 2); }

$ cl /nologo va_args.c && va_args.exe # Expected result
foo(1, 2)

$ clang.exe --version
clang version 3.1 (trunk 150957)
Target: i686-pc-win32
Thread model: posix

$ clang.exe va_args.c && ./a.out # Actual result
foo(1, 1638208)

$ clang.exe va_args.c -S -emit-llvm

$ more va_args.s
...
define void @​foo(i32 %A, ...) nounwind {
%1 = alloca i32, align 4
%B = alloca i32, align 4
%L = alloca i8*, align 4
store i32 %A, i32* %1, align 4
%2 = bitcast i32* %1 to i8*
%3 = getelementptr inbounds i8* %2, i32 4 # %3 = &A + 1
store i8* %3, i8** %L, align 4 # L = &A + 1
%4 = load i8** %L, align 4
%5 = getelementptr inbounds i8* %4, i32 4 # %5 = (&A + 1) + 1
store i8* %5, i8** %L, align 4 # L = &A + 2
%6 = getelementptr inbounds i8* %5, i32 -4 # load ((&A + 2) - 1) == (&A + 1) ?
%7 = bitcast i8* %6 to i32*
%8 = load i32* %7, align 4
store i32 %8, i32* %B, align 4
%9 = load i32* %1, align 4
%10 = load i32* %B, align 4
%11 = call i32 (i8*, ...)* @​printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0), i32 %9, i32 %10)
store i8* null, i8** %L, align 4
ret void
}
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclangClang issues not falling into any other categoryduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions