This is an archive of the discontinued LLVM Phabricator instance.

[BuildLibCalls] Add argmemonly to more lib calls.
ClosedPublic

Authored by fhahn on Aug 27 2020, 9:44 AM.

Details

Summary

strspn, strncmp, strcspn, strcasecmp, strncasecmp, memcmp, memchr,
memrchr, memcpy, memmove, memcpy, mempcpy should all only access memory
through their arguments.

I broke out strcoll, because the result depends on the locale, which
might get accessed through memory.

I also added nofree to memcmp, so we do not introduce another attribute
group for in the tests.

Diff Detail

Event Timeline

fhahn created this revision.Aug 27 2020, 9:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 27 2020, 9:44 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
fhahn requested review of this revision.Aug 27 2020, 9:44 AM
xbolva00 added inline comments.
llvm/lib/Transforms/Utils/BuildLibCalls.cpp
188–189

strchr too?

321–322

Same for bcmp?

329

We miss attrs for strnlen

xbolva00 added inline comments.Aug 27 2020, 10:04 AM
llvm/lib/Transforms/Utils/BuildLibCalls.cpp
325

Should be nofree opt out rather than opt in?

@jeoerfert

xbolva00 added inline comments.Aug 27 2020, 10:18 AM
llvm/lib/Transforms/Utils/BuildLibCalls.cpp
325

Ok, we have there:

if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))

Changed |= setDoesNotFreeMemory(F);

So why we need to set nofree for memcmp?

FWIW, I have a non-clean patch for this too that has some more things we could add: https://github.com/jdoerfert/llvm-project/commit/c405073b6f353b3bccb10f222185763c4c7e3457
I would recommend we sort the order in each case, arguments first, in order of their number, than return than function, or something similar. that makes it easier to compare and read IMHO.
We should maybe wait for D85932 and start using the script for this file, it can handle what we need it too nicely (I think/hope).

I'm fine with doing them in multiple steps, especially D85932 should make this even simpler.

llvm/lib/Transforms/Utils/BuildLibCalls.cpp
325

I think it was set before already. So no need to add it here if that was the case.

I broke out strcoll, because the result depends on the locale, which might get accessed through memory.

Aren't strcasecmp/strncasecmp also locale-sensitive?

I broke out strcoll, because the result depends on the locale, which might get accessed through memory.

Aren't strcasecmp/strncasecmp also locale-sensitive?

Hm, not clear to me:

The POSIX.1-2008 standard says of these functions:
 
       When the LC_CTYPE category of the locale being used is from  the
       POSIX locale, these functions shall behave as if the strings had
       been converted to lowercase and  then  a  byte  comparison  per‐
       formed.  Otherwise, the results are unspecified.

https://www.gnu.org/software/libc/manual/html_node/String_002fArray-Comparison.html#index-strcasecmp says "How uppercase and lowercase characters are related is determined by the currently selected locale." That seems pretty clear.

fhahn updated this revision to Diff 288438.Aug 27 2020, 1:02 PM

Thanks, updated to add argmemonly also for strchr,bcmp. Do not add it for strcasecmp/strncasecmp. Remove unnecessary setDoesNotFreeMemory.

FWIW, I have a non-clean patch for this too that has some more things we could add: https://github.com/jdoerfert/llvm-project/commit/c405073b6f353b3bccb10f222185763c4c7e3457
I would recommend we sort the order in each case, arguments first, in order of their number, than return than function, or something similar. that makes it easier to compare and read IMHO.
We should maybe wait for D85932 and start using the script for this file, it can handle what we need it too nicely (I think/hope).

That's great, there's definitely lots of additional stuff missing. I already adjusted the lines, I don't think there a need to await for a script update. We can just re-generate the lines once the script update is in.

I'm fine with doing them in multiple steps, especially D85932 should make this even simpler.

I think it is beneficial to do this in a few steps, so we can more easily bisect problems.

https://www.gnu.org/software/libc/manual/html_node/String_002fArray-Comparison.html#index-strcasecmp says "How uppercase and lowercase characters are related is determined by the currently selected locale." That seems pretty clear.

Thanks, this indeed seems clear. The first page I found was not as explicit https://linux.die.net/man/3/strcasecmp. I moved them to strcoll.

fhahn marked 5 inline comments as done.Aug 27 2020, 1:02 PM
fhahn added inline comments.
llvm/lib/Transforms/Utils/BuildLibCalls.cpp
325

Oh right, that's great, I removed it.

329

probably best to add in a separate patch, as it is completely missing?

This revision is now accepted and ready to land.Aug 27 2020, 1:12 PM
This revision was automatically updated to reflect the committed changes.
fhahn marked an inline comment as done.