Hi, I find a aggressive optimization by clang.
#include <stdio.h>
#include <stdlib.h>
int main()
{
long size = 1024 * 535975264L;
printf("malloc size:%lu KB\n", size/1024);
char *p = (char *)malloc(size);
if (p == NULL) {
printf("malloc failed!\n");
} else {
printf("malloc success!\n");
}
free(p);
return 0;
}
PS:Assume that CommitLimit < 531853152 kB and malloc fails.
-
Clang
-O0
test.c, the output is malloc failed! -
Clang
-O2
test.c, the output is malloc success!
I disassemble it and find that the malloc instruction is Disappeared!
main: # @main
push rax
lea rdi, [rip + .L.str]
mov esi, 535975264
xor eax, eax
call printf@PLT
lea rdi, [rip + .Lstr]
call puts@PLT
xor eax, eax
pop rcx
ret
.L.str:
.asciz "malloc size:%lu KB\n"
.Lstr:
.asciz "malloc success!"
However, the behaviour of GCC is the same whether it’s O0 or O2. (look at this Compiler Explorer)
Is the behavior of clang -O2 reasonable?