Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754397AbdIHP7z (ORCPT ); Fri, 8 Sep 2017 11:59:55 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:52272 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752132AbdIHP7w (ORCPT ); Fri, 8 Sep 2017 11:59:52 -0400 References: <20170824081811.19299-1-takahiro.akashi@linaro.org> <20170824081811.19299-10-takahiro.akashi@linaro.org> <20170824170440.GD29665@leverpostej> <20170825012103.GB7245@akashi-kouhiroshi-no-MacBook-Air.local> <20170825104133.GB3127@leverpostej> <20170908025044.GD17186@linaro.org> From: Thiago Jung Bauermann To: AKASHI Takahiro Cc: Mark Rutland , catalin.marinas@arm.com, will.deacon@arm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, akpm@linux-foundation.org, mpe@ellerman.id.au, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, ard.biesheuvel@linaro.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH 09/14] arm64: kexec_file: add sha256 digest check in purgatory In-reply-to: <20170908025044.GD17186@linaro.org> Date: Fri, 08 Sep 2017 12:59:38 -0300 MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 x-cbid: 17090815-0028-0000-0000-000008549407 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007689; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000227; SDB=6.00914097; UDB=6.00458848; IPR=6.00694355; BA=6.00005576; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017070; XFM=3.00000015; UTC=2017-09-08 15:59:51 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17090815-0029-0000-0000-000037799411 Message-Id: <87lglpp4et.fsf@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-09-08_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1709080238 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2180 Lines: 73 AKASHI Takahiro writes: > On Fri, Aug 25, 2017 at 11:41:33AM +0100, Mark Rutland wrote: >> On Fri, Aug 25, 2017 at 10:21:06AM +0900, AKASHI Takahiro wrote: >> > On Thu, Aug 24, 2017 at 06:04:40PM +0100, Mark Rutland wrote: >> > > On Thu, Aug 24, 2017 at 05:18:06PM +0900, AKASHI Takahiro wrote: >> > > > +void *memcpy(void *dst, const void *src, size_t len) >> > > > +{ >> > > > + int i; >> > > > + >> > > > + for (i = 0; i < len; i++) >> > > > + ((u8 *)dst)[i] = ((u8 *)src)[i]; >> > > > + >> > > > + return NULL; >> > > > +} >> > > > + >> > > > +void *memset(void *dst, int c, size_t len) >> > > > +{ >> > > > + int i; >> > > > + >> > > > + for (i = 0; i < len; i++) >> > > > + ((u8 *)dst)[i] = (u8)c; >> > > > + >> > > > + return NULL; >> > > > +} >> > > > + >> > > > +int memcmp(const void *src, const void *dst, size_t len) >> > > > +{ >> > > > + int i; >> > > > + >> > > > + for (i = 0; i < len; i++) >> > > > + if (*(char *)src != *(char *)dst) >> > > > + return 1; >> > > > + >> > > > + return 0; >> > > > +} >> > > >> > > How is the compiler prevented from "optimising" these into calls to >> > > themselves? >> > >> > I don't get what you mean by "calls to themselves." >> >> There are compiler optimizations that recognise sequences like: >> >> for (i = 0; i < len; i++) >> dst[i] = src[i]; >> >> ... and turn those into: >> >> memcpy(dst, src, len); >> >> ... these have been known to "optimize" memcpy implementations into >> calls to themselves. Likewise for other string operations. >> >> One way we avoid that today is by writing our memcpy in assembly. > > I see, thanks. > >> Do we have a guarnatee that this will not happen here? e.g. do we pass >> some compiler flag that prevents this? > > I don't know any options to do this. > (maybe -nostdlib?) kexec-tools calls gcc with -fno-builtin -ffreestanding (though according to the man page, the former is implied in the latter), which tells the compiler that the standard library may not exist. I don't know specifically that this options turns off the memcpy optimization, but it seems logical that it does. -- Thiago Jung Bauermann IBM Linux Technology Center