git: eb54f261fce2 - stable/13 - compiler-rt: support ReExec() on FreeBSD

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Thu, 10 Feb 2022 18:18:20 UTC
The branch stable/13 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=eb54f261fce2b55e6859bc1416ee8d964aefa079

commit eb54f261fce2b55e6859bc1416ee8d964aefa079
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-01-19 18:08:18 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-02-10 18:17:39 +0000

    compiler-rt: support ReExec() on FreeBSD
    
    Based on getMainExecutable() in llvm/lib/Support/Unix/Path.inc.
    This will need a little more work for an upstream change as it must
    support older FreeBSD releases that lack elf_aux_info() / AT_EXEC_PATH.
    
    No objection:   dim
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D33934
    
    (cherry picked from commit 96fe7c8ab0f65cf829619abd74ae6c126b21e15f)
---
 .../lib/sanitizer_common/sanitizer_linux_libcdep.cpp          | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
index 7ce9e25da342..59f8d5d1fec2 100644
--- a/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
+++ b/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp
@@ -46,7 +46,9 @@
 
 #if SANITIZER_FREEBSD
 #include <pthread_np.h>
+#include <stdlib.h>
 #include <osreldate.h>
+#include <sys/auxv.h>
 #include <sys/sysctl.h>
 #define pthread_getattr_np pthread_attr_get_np
 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
@@ -865,7 +867,14 @@ u64 MonotonicNanoTime() {
 void ReExec() {
   const char *pathname = "/proc/self/exe";
 
-#if SANITIZER_NETBSD
+#if SANITIZER_FREEBSD
+  char exe_path[PATH_MAX];
+  if (elf_aux_info(AT_EXECPATH, exe_path, sizeof(exe_path)) == 0) {
+    char link_path[PATH_MAX];
+    if (realpath(exe_path, link_path))
+      pathname = link_path;
+  }
+#elif SANITIZER_NETBSD
   static const int name[] = {
       CTL_KERN,
       KERN_PROC_ARGS,