lkml.org 
[lkml]   [2017]   [Sep]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: [PATCH 09/14] arm64: kexec_file: add sha256 digest check in purgatory
Date

AKASHI Takahiro <takahiro.akashi@linaro.org> 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

\
 
 \ /
  Last update: 2017-09-08 18:00    [W:0.066 / U:7.648 seconds]
©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site