From owner-freebsd-current@FreeBSD.ORG Fri Jun 11 21:34:08 2010 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B704E106568C for ; Fri, 11 Jun 2010 21:34:08 +0000 (UTC) (envelope-from des@des.no) Received: from smtp.des.no (smtp.des.no [194.63.250.102]) by mx1.freebsd.org (Postfix) with ESMTP id 4771D8FC08 for ; Fri, 11 Jun 2010 21:34:08 +0000 (UTC) Received: from ds4.des.no (des.no [84.49.246.2]) by smtp.des.no (Postfix) with ESMTP id 7DD891FFC34; Fri, 11 Jun 2010 21:34:07 +0000 (UTC) Received: by ds4.des.no (Postfix, from userid 1001) id 8C3ED84438; Fri, 11 Jun 2010 23:31:57 +0200 (CEST) From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= To: Tijl Coosemans References: <20100611162118.GR39829@acme.spoerlein.net> <867hm5tl6u.fsf@ds4.des.no> <201006112304.10952.tijl@coosemans.org> Date: Fri, 11 Jun 2010 23:31:57 +0200 In-Reply-To: <201006112304.10952.tijl@coosemans.org> (Tijl Coosemans's message of "Fri, 11 Jun 2010 23:04:10 +0200") Message-ID: <86y6els1bm.fsf@ds4.des.no> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.95 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Cc: freebsd-current@freebsd.org Subject: Re: Cleanup for cryptographic algorithms vs. compiler optimizations X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jun 2010 21:34:08 -0000 Tijl Coosemans writes: > Dag-Erling Sm=C3=B8rgrav writes: > > #define FORCE_ASSIGN(type, var, value) \ > > *(volatile type *)&(var) =3D (value) > memset can be optimised away as well. The only way is to declare those > variables volatile. Assigning through a volatile pointer, as in FORCE_ASSIGN(), also works, even if the variable itself is not volatile. Unfortunately, you can't trick the compiler into not optimizing away memset(), since you can't pass a volatile pointer to memset(). % cat >zero.c < int main(void) { char a, b, c; a =3D 1; memset(&b, 2, sizeof b); *(volatile char *)&c =3D 3; return 0; } EOF % for O in 0 1 2 ; do c99 -O$O -o zero-O$O zero.c ; done % for O in 0 1 2 ; do echo disassemble main | gdb -batch -x /dev/stdin -q z= ero-O$O ; done Dump of assembler code for function main: 0x0000000000400560 : push %rbp 0x0000000000400561 : mov %rsp,%rbp 0x0000000000400564 : sub $0x10,%rsp 0x0000000000400568 : movb $0x1,0xffffffffffffffff(%rbp) 0x000000000040056c : lea 0xfffffffffffffffe(%rbp),%rdi 0x0000000000400570 : mov $0x1,%edx 0x0000000000400575 : mov $0x2,%esi 0x000000000040057a : callq 0x40042c 0x000000000040057f : lea 0xfffffffffffffffd(%rbp),%rax 0x0000000000400583 : movb $0x3,(%rax) 0x0000000000400586 : mov $0x0,%eax 0x000000000040058b : leaveq=20 0x000000000040058c : retq=20=20=20 0x000000000040058d : nop=20=20=20=20 0x000000000040058e : nop=20=20=20=20 0x000000000040058f : nop=20=20=20=20 End of assembler dump. Dump of assembler code for function main: 0x0000000000400520 : movb $0x3,0xffffffffffffffff(%rsp) 0x0000000000400525 : mov $0x0,%eax 0x000000000040052a : retq=20=20=20 0x000000000040052b : nop=20=20=20=20 End of assembler dump. Dump of assembler code for function main: 0x0000000000400520 : xor %eax,%eax 0x0000000000400522 : movb $0x3,0xffffffffffffffff(%rsp) 0x0000000000400527 : retq=20=20=20 End of assembler dump. In the -O0 case, all three assignments are carried out. In the -O1 and -O2 cases, the first two assignments and the corresponding variables are optimized away, while the third (which uses the volatile pointer trick) is not. Clang produces significantly worse code than gcc in all cases: % for O in 0 1 2 ; do clang -O$O -o zero-O$O zero.c ; done % for O in 0 1 2 ; do echo disassemble main | gdb -batch -x /dev/stdin -q z= ero-O$O ; done Dump of assembler code for function main: 0x0000000000400550 : push %rbp 0x0000000000400551 : mov %rsp,%rbp 0x0000000000400554 : movl $0x0,0xfffffffffffffffc(%rbp) 0x000000000040055b : movb $0x1,0xfffffffffffffffb(%rbp) 0x000000000040055f : movb $0x2,0xfffffffffffffffa(%rbp) 0x0000000000400563 : movb $0x3,0xfffffffffffffff9(%rbp) 0x0000000000400567 : movl $0x0,0xfffffffffffffffc(%rbp) 0x000000000040056e : mov 0xfffffffffffffffc(%rbp),%eax 0x0000000000400571 : pop %rbp 0x0000000000400572 : retq=20=20=20 0x0000000000400573 : nop=20=20=20=20 End of assembler dump. Dump of assembler code for function main: 0x0000000000400550 : push %rbp 0x0000000000400551 : mov %rsp,%rbp 0x0000000000400554 : movb $0x3,0xffffffffffffffff(%rbp) 0x0000000000400558 : xor %eax,%eax 0x000000000040055a : pop %rbp 0x000000000040055b : retq=20=20=20 End of assembler dump. Dump of assembler code for function main: 0x0000000000400550 : push %rbp 0x0000000000400551 : mov %rsp,%rbp 0x0000000000400554 : movb $0x3,0xffffffffffffffff(%rbp) 0x0000000000400558 : xor %eax,%eax 0x000000000040055a : pop %rbp 0x000000000040055b : retq=20=20=20 End of assembler dump. DES --=20 Dag-Erling Sm=C3=B8rgrav - des@des.no