From: Paul Floyd Date: Tue, 17 Mar 2026 07:46:34 +0000 (+0100) Subject: Darwin stack creation: hack to keep apple pointer conrrect on macOS 12+ X-Git-Tag: VALGRIND_3_27_0~91 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07661c56ee1457d23bcffe037fa7f5230bb30832;p=thirdparty%2Fvalgrind.git Darwin stack creation: hack to keep apple pointer conrrect on macOS 12+ From Louis Brunner's port. --- diff --git a/coregrind/vg_preloaded.c b/coregrind/vg_preloaded.c index 0b77e0537..baf4cc11c 100644 --- a/coregrind/vg_preloaded.c +++ b/coregrind/vg_preloaded.c @@ -188,9 +188,60 @@ static void env_unsetenv ( HChar **env, const HChar *varname ) } } *(to++) = *(from++); + /* fix the 4th "char* apple" pointer (aka. executable path pointer) */ *(to++) = *(from++); + +#if DARWIN_VERS < DARWIN_12_00 + /* We only do this on older versions of darwin because dyld changed + and by the point we do this changes, the apple env ptr is already set, + so if we move values around, we'll end up with a pointer pointing inside + (and even potentially after) applep. + + Instead we copy the first value of the applelp over and over again + so that envp and applep are still separated by NULL, + applep is continuous and points to a correct value. + + See the following example, using the following envp and applep: + + ``` + 0xXXXX00: PATH=/bin (envp) + 0xXXXX08: DYLD_INSERT_LIBRARIES=/lib + 0xXXXX10: USER=foo + 0xXXXX18: NULL + 0xXXXX20: executable_path=/bin/ls (applep) + 0xXXXX28: NULL + ``` + + # With this line + + ``` + 0xXXXX00: PATH=/bin (envp) + 0xXXXX08: USER=foo + 0xXXXX10: NULL + 0xXXXX18: executable_path=/bin/ls + 0xXXXX20: NULL (applep) + 0xXXXX28: NULL + ``` + + Notice that the applep is now invalid. + + # Without this line + + ``` + 0xXXXX00: PATH=/bin (envp) + 0xXXXX08: USER=foo + 0xXXXX10: NULL + 0xXXXX18: executable_path=/bin/ls + 0xXXXX20: executable_path=/bin/ls (applep) + 0xXXXX28: NULL + ``` + + Notice that while values in applep are duplicated, this is only the case if browsing frop envp[len+1] + but not from applep which is always valid. Duplicated values are also harmless in this case. + */ *to = NULL; +#endif } static void vg_cleanup_env(void) __attribute__((constructor));