diff options
author | Fangrui Song <maskray@google.com> | 2020-03-10 08:05:00 -0700 |
---|---|---|
committer | Fangrui Song <maskray@google.com> | 2020-03-10 11:11:58 -0700 |
commit | a0c0389ffbb51d5f4417e926a5eb59d89fd2af05 (patch) | |
tree | 9c060fe74c03f209ec127dac5234c398a5abf114 /llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | |
parent | 9624beb38a46037f69362650a52e06d8be4fd006 (diff) | |
download | llvm-a0c0389ffbb51d5f4417e926a5eb59d89fd2af05.zip llvm-a0c0389ffbb51d5f4417e926a5eb59d89fd2af05.tar.gz llvm-a0c0389ffbb51d5f4417e926a5eb59d89fd2af05.tar.bz2 |
[SimplifyLibcalls] Don't replace locked IO (fgetc/fgets/fputc/fputs/fread/fwrite) with unlocked IO (*_unlocked)
This essentially reverts some of the SimplifyLibcalls part changes of D45736 [SimplifyLibcalls] Replace locked IO with unlocked IO.
C11 7.21.5.2 The fflush function
> If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above.
i.e. fopen'ed FILE* is inherently captured.
POSIX.1-2017 getc_unlocked, getchar_unlocked, putc_unlocked, putchar_unlocked - stdio with explicit client locking
> These functions can safely be used in a multi-threaded program if and only if they are called while the invoking thread owns the ( FILE *) object, as is the case after a successful call to the flockfile() or ftrylockfile() functions.
After a thread fopen'ed a FILE*, when it is calling foobar() which is now replaced by foobar_unlocked(),
if another thread is concurrently calling fflush(0), the behavior is undefined.
C11 7.22.4.4 The exit function
> Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.
The replacement is only feasible if the program is single threaded, or exit or fflush(0) is never called.
See also http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20180528/556615.html
for how the replacement makes libc interceptors difficult to implement.
dalias: in a worst case, it's unbounded data corruption because of concurrent access to pointers
without synchronization. f->wpos or rpos could get outside of the buffer, thread A could do
f->wpos += j after knowing j is in bounds, while thread B also changes it concurrently.
This can produce exploitable conditions depending on libc internals.
Revert the SimplifyLibcalls part change because the cons obviously
overweigh the pros. Even when the replacement is feasible, the benefit
is indemonstrable, more so in an application instead of an artificial
glibc benchmark. Theoretically the replacement could be beneficial when
calling getc_unlocked/putc_unlocked in a loop, but then it is better
using a blocked IO operation and the user is likely aware of that.
The function attribute inference is still useful and thus kept.
Reviewed By: xbolva00
Differential Revision: https://reviews.llvm.org/D75933
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 81 |
1 files changed, 3 insertions, 78 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 4443c391..77fe6c1 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -138,28 +138,6 @@ static Value *convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base) { return ConstantInt::get(CI->getType(), Result); } -static bool isLocallyOpenedFile(Value *File, CallInst *CI, - const TargetLibraryInfo *TLI) { - CallInst *FOpen = dyn_cast<CallInst>(File); - if (!FOpen) - return false; - - Function *InnerCallee = FOpen->getCalledFunction(); - if (!InnerCallee) - return false; - - LibFunc Func; - if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) || - Func != LibFunc_fopen) - return false; - - inferLibFuncAttributes(*CI->getCalledFunction(), *TLI); - if (PointerMayBeCaptured(File, true, true)) - return false; - - return true; -} - static bool isOnlyUsedInComparisonWithZero(Value *V) { for (User *U : V->users()) { if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) @@ -2754,11 +2732,6 @@ Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) { } } - if (isLocallyOpenedFile(CI->getArgOperand(3), CI, TLI)) - return emitFWriteUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), CI->getArgOperand(3), B, DL, - TLI); - return nullptr; } @@ -2773,15 +2746,9 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) { if (OptForSize) return nullptr; - // Check if has any use - if (!CI->use_empty()) { - if (isLocallyOpenedFile(CI->getArgOperand(1), CI, TLI)) - return emitFPutSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B, - TLI); - else - // We can't optimize if return value is used. - return nullptr; - } + // We can't optimize if return value is used. + if (!CI->use_empty()) + return nullptr; // fputs(s,F) --> fwrite(s,strlen(s),1,F) uint64_t Len = GetStringLength(CI->getArgOperand(0)); @@ -2795,40 +2762,6 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) { CI->getArgOperand(1), B, DL, TLI); } -Value *LibCallSimplifier::optimizeFPutc(CallInst *CI, IRBuilderBase &B) { - optimizeErrorReporting(CI, B, 1); - - if (isLocallyOpenedFile(CI->getArgOperand(1), CI, TLI)) - return emitFPutCUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B, - TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFGetc(CallInst *CI, IRBuilderBase &B) { - if (isLocallyOpenedFile(CI->getArgOperand(0), CI, TLI)) - return emitFGetCUnlocked(CI->getArgOperand(0), B, TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFGets(CallInst *CI, IRBuilderBase &B) { - if (isLocallyOpenedFile(CI->getArgOperand(2), CI, TLI)) - return emitFGetSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), B, TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFRead(CallInst *CI, IRBuilderBase &B) { - if (isLocallyOpenedFile(CI->getArgOperand(3), CI, TLI)) - return emitFReadUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), CI->getArgOperand(3), B, DL, - TLI); - - return nullptr; -} - Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) { annotateNonNullBasedOnAccess(CI, 0); if (!CI->use_empty()) @@ -3162,16 +3095,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) { return optimizeFPrintF(CI, Builder); case LibFunc_fwrite: return optimizeFWrite(CI, Builder); - case LibFunc_fread: - return optimizeFRead(CI, Builder); case LibFunc_fputs: return optimizeFPuts(CI, Builder); - case LibFunc_fgets: - return optimizeFGets(CI, Builder); - case LibFunc_fputc: - return optimizeFPutc(CI, Builder); - case LibFunc_fgetc: - return optimizeFGetc(CI, Builder); case LibFunc_puts: return optimizePuts(CI, Builder); case LibFunc_perror: |