Author: Xing Xue
Date: 2022-08-08T17:21:30-04:00
New Revision: cc8edbea7a5f26906ae3d1f7ba0dc5da8fc5afb5

URL: 
https://github.com/llvm/llvm-project/commit/cc8edbea7a5f26906ae3d1f7ba0dc5da8fc5afb5
DIFF: 
https://github.com/llvm/llvm-project/commit/cc8edbea7a5f26906ae3d1f7ba0dc5da8fc5afb5.diff

LOG: [libunwind][AIX] Save/restore errno before/after system calls 
dlopen/dlsym/dlclose

Summary:
libunwind on AIX calls dlopen()/dlsym()/dlclose() to dynamically load libc++abi 
and get the personality for state table EH when it is running against the 
legacy xlcang++ compiler genereated applications. dlopen() sets errno to 0 when 
it is successful, which clobbers the value in errno from the user code. This 
seems to be an AIX bug that it should not set errno to 0 according to POSIX. We 
will open a bug report to AIX but in the mean time there won't be time line 
when AIX will have a fix and even AIX does fix it, it won't help earlier AIX 
releases in the field. This patch saves and restores errno before and after 
these calls so that user code can work as expected.

Reviewed by: compnerd, libunwind

Differential Revision: https://reviews.llvm.org/D131292

Added: 
    

Modified: 
    libunwind/src/UnwindCursor.hpp

Removed: 
    


################################################################################
diff  --git a/libunwind/src/UnwindCursor.hpp b/libunwind/src/UnwindCursor.hpp
index b8bd9bc59010..d325ed5f0eea 100644
--- a/libunwind/src/UnwindCursor.hpp
+++ b/libunwind/src/UnwindCursor.hpp
@@ -2106,6 +2106,11 @@ bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R 
&registers) {
           // using dlopen().
           const char libcxxabi[] = "libc++abi.a(libc++abi.so.1)";
           void *libHandle;
+          // The AIX dlopen() sets errno to 0 when it is successful, which
+          // clobbers the value of errno from the user code. This is an AIX
+          // bug because according to POSIX it should not set errno to 0. To
+          // workaround before AIX fixes the bug, errno is saved and restored.
+          int saveErrno = errno;
           libHandle = dlopen(libcxxabi, RTLD_MEMBER | RTLD_NOW);
           if (libHandle == NULL) {
             _LIBUNWIND_TRACE_UNWINDING("dlopen() failed with errno=%d\n",
@@ -2119,6 +2124,7 @@ bool UnwindCursor<A, R>::getInfoFromTBTable(pint_t pc, R 
&registers) {
             assert(0 && "dlsym() failed");
           }
           dlclose(libHandle);
+          errno = saveErrno;
         }
       }
       xlcPersonalityV0InitLock.unlock();


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to