]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Darwin: add VG_(resolved_exename)
authorPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 24 Aug 2025 14:11:25 +0000 (16:11 +0200)
committerPaul Floyd <pjfloyd@wanadoo.fr>
Sun, 24 Aug 2025 14:11:25 +0000 (16:11 +0200)
Possibly not needed. On Linux it's used to check that the guest exe
doen't get opend with a write flag. macOS seems happy to allow that.

coregrind/m_initimg/initimg-darwin.c

index 57fbd0bf75cd88cd1deb4f0e79a50cb12f7790fa..ca71295f2d12a6495336d2f85e0dbf9b36fffd81 100644 (file)
@@ -259,6 +259,57 @@ static HChar *copy_str(HChar **tab, const HChar *str)
    return orig;
 }
 
+/*
+ * @todo PJF Make this multi-platform
+ */
+static Bool try_get_interp(const HChar* args_exe, HChar* interp_out)
+{
+   HChar  hdr[4096];
+   Int    len = sizeof hdr;
+   SysRes res;
+   Int fd;
+   HChar* end;
+   HChar* cp;
+   HChar* interp;
+
+   res = VG_(open)(args_exe, VKI_O_RDONLY, 0);
+   if (sr_isError(res)) {
+      return False;
+   } else {
+      fd = sr_Res(res);
+   }
+
+   res = VG_(pread)(fd, hdr, len, 0);
+
+   if (sr_isError(res)) {
+      VG_(close)(fd);
+      return False;
+   } else {
+      len = sr_Res(res);
+   }
+
+   if (0 != VG_(memcmp)(hdr, "#!", 2)) {
+      VG_(close)(fd);
+      return False;
+   }
+
+   end    = hdr + len;
+   interp = hdr + 2;
+   while (interp < end && (*interp == ' ' || *interp == '\t'))
+      interp++;
+
+   for (cp = interp; cp < end && !VG_(isspace)(*cp); cp++)
+      ;
+
+   *cp = '\0';
+
+   VG_(sprintf)(interp_out, "%s", interp);
+
+   VG_(close)(fd);
+   return True;
+}
+
+
 
 /* ----------------------------------------------------------------
  
@@ -449,6 +500,17 @@ Addr setup_client_stack( void*  init_sp,
 
    vg_assert((strtab-stringbase) == stringsize);
 
+   if (VG_(resolved_exename) == NULL) {
+      const HChar *exe_name = VG_(find_executable)(VG_(args_the_exename));
+      HChar interp_name[VKI_PATH_MAX];
+      if (try_get_interp(exe_name, interp_name)) {
+         exe_name = interp_name;
+      }
+      HChar resolved_name[VKI_PATH_MAX];
+      VG_(realpath)(exe_name, resolved_name);
+      VG_(resolved_exename) = VG_(strdup)("initimg-darwin.sre.1", resolved_name);
+   }
+
    /* client_SP is pointing at client's argc/argv */
 
    if (0) VG_(printf)("startup SP = %#lx\n", client_SP);