Bug 406349

Summary: Android runtime linker ignores DF_1_INTERPOSE in vgpreload_core-*
Product: [Developer tools] valgrind Reporter: John Reiser <jreiser>
Component: memcheckAssignee: Julian Seward <jseward>
Status: REPORTED ---    
Severity: normal CC: mark, naesten, tom, wuweijia
Priority: NOR    
Version First Reported In: 3.14.0   
Target Milestone: ---   
Platform: Android   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description John Reiser 2019-04-09 05:50:23 UTC
SUMMARY
The Android runtime linker /bin/linker64 does not understand the DF_1_INTERPOSE and DF_1_INITFIRST flags in DF_FLAGS_1 as used by vgpreload_core_* of valgrind-3.14.  As a result no re-directions or interceptions of calls to malloc/free/etc are performed.  Thus memcheck does not track allocations, so there is no leak checking.


STEPS TO REPRODUCE
1. Build a simple leak-producing test program as an ET_DYN executable file for Android.
2. Run valgrind (memcheck) on the test program under Android
3. 

OBSERVED RESULT
No leak reported because malloc is not tracked.


EXPECTED RESULT
malloc is tracked, and leak is reported.


SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Originally posted in [valgrind-users] by wuweijia@h###.com on 2019-Apr-08
Terminal transcript:
localhost:/system/bin # ./valgrind -v --undef-value-errors=no  ./test
==30806== Memcheck, a memory error detector
==30806== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30806== Using Valgrind-3.14.0-353a3587bb-20181007X and LibVEX; rerun with -h for copyright info
==30806== Command: ./test
==30806==
--30806-- Valgrind options:
--30806--    -v
--30806--    --undef-value-errors=no
--30806-- Contents of /proc/version:
--30806--   Linux version 4.4.7+ (root@baixin-HP-Compaq-8200-Elite-MT-PC) (gcc version 4.9.3 20151223 (prerelease) (SDK V100R005C00SPC030B080) ) #1 SMP PREEMPT Fri Sep 9 14:57:05 CST 2016
--30806--
--30806-- Arch and hwcaps: ARM64, LittleEndian, baseline
--30806-- Page sizes: currently 4096, max supported 65536
--30806-- Valgrind library directory: /system/lib64/valgrind
--30806-- Reading syms from /system_Q_EA3/bin/test
--30806-- Reading syms from /system_Q_EA3/bin/linker64
--30806-- Scheduler: using generic scheduler lock implementation.
--30806-- Reading suppressions file: /system/lib64/valgrind/default.supp
--30806-- Reading syms from /system_Q_EA3/lib64/libm.so
linker: Warning: "/system_Q_EA3/lib64/valgrind/vgpreload_core-arm64-linux.so" has unsupported flags DT_FLAGS_1=0x421 (ignoring unsupported flags)
WARNING: linker: Warning: "/system_Q_EA3/lib64/valgrind/vgpreload_core-arm64-linux.so" has unsupported flags DT_FLAGS_1=0x421 (ignoring unsupported flags)
linker: Warning: "/system_Q_EA3/lib64/valgrind/vgpreload_memcheck-arm64-linux.so" has unsupported flags DT_FLAGS_1=0x421 (ignoring unsupported flags)
WARNING: linker: Warning: "/system_Q_EA3/lib64/valgrind/vgpreload_memcheck-arm64-linux.so" has unsupported flags DT_FLAGS_1=0x421 (ignoring unsupported flags)
new lld  p=0x5613000
==30806==
==30806== HEAP SUMMARY:
==30806==     in use at exit: 0 bytes in 0 blocks
==30806==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==30806==
==30806== All heap blocks were freed -- no leaks are possible
==30806==
==30806== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==30806== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


----- /usr/include/elf.h
#define DF_1_INTERPOSE  0x00000400      /* Object is used to interpose.  */
#define DF_1_INITFIRST  0x00000020      /* Set RTLD_INITFIRST for this object*/
#define DF_1_NOW        0x00000001      /* Set RTLD_NOW for this object.  */
Comment 1 Tom Hughes 2019-04-09 06:50:00 UTC
I believe that comes from -Wl,-z,interpose,-z,initfirst being included in PRELOAD_LDFLAGS_COMMON_LINUX in Makefile.all.am.

Now obviously we can avoid doing that for Android - obviously that might mean we are less successfully at interposing the functions we want to replace though.

Actually thinking about it we don't really rely on interposition these days, so do we really need those at all?
Comment 2 Tom Hughes 2019-04-09 06:51:40 UTC
I think those flags were originally introduced in 918c3a7b7e01abedf840c6fa8786df41192bf737 by Jeremy way back in 2003!
Comment 3 Owen wo 2019-04-10 03:46:30 UTC
These Text "linker: Warning: "/system_Q_EA3/lib64/valgrind/vgpreload_core-arm64-linux.so" has unsupported flags DT_FLAGS_1=0x421 (ignoring unsupported flags)" has been existed for several version from valgrind 3.12 and android 7 . and these version are all worked (valgrind can detect the memory leak). But only android Q  is failed.
Comment 4 Samuel Bronson 2025-07-14 23:02:59 UTC
Just to clarify: this warning always prints the full set of flags found in the DT_FLAGS_1 entry, including the supported ones, from its introduction at <https://cs.android.com/android/_/android/platform/bionic/+/6cdeb5234d7f4523fe9d83974f265d80f10512a6:linker/linker.cpp;drc=6cdeb5234d7f4523fe9d83974f265d80f10512a6;l=2067> to its current version at <https://cs.android.com/android/_/android/platform/bionic/+/7ea4c99263fb09c4590135e78bc26fa4e5786083:linker/linker.cpp;drc=7ea4c99263fb09c4590135e78bc26fa4e5786083;l=3186>.

The actual set of supported flags seems to range from `DF_1_NOW | DF_1_GLOBAL` to `DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE | DF_1_ORIGIN`.

In any case, since `DF_1_INTERPOSE` and `DF_1_INITFIRST` never "worked" on Android, the warning is presumably unrelated to any regressions you're encountering with leak tracking.

Actually, there's an FAQ entry titled "Memcheck doesn't report any errors and I know my program has errors." at <https://valgrind.org/docs/manual/faq.html#faq.hiddenbug>; it seems likely that your issue was caused by statically linking libc or libc++ into your test program?
Comment 5 Mark Wielaard 2025-10-17 13:03:08 UTC
So this patch (totally untested because I don't have access to android) might get rid of the warnings:

diff --git a/Makefile.all.am b/Makefile.all.am
index d4f6b3fb7985..17427b48e97c 100644
--- a/Makefile.all.am
+++ b/Makefile.all.am
@@ -327,7 +327,15 @@ endif
 
 # Baseline link flags for making vgpreload shared objects.
 #
+if VGCONF_PLATVARIANT_IS_ANDROID
+# The Android toolchain includes all kinds of stdlib helpers present in
+# bionic which is bad because we are not linking with it and the Android
+# linker will panic. Also the Android runtime linker doesn't recognize
+# DF_1_INTERPOSE and DF_1_INITFIRST.
+PRELOAD_LDFLAGS_COMMON_LINUX  = -nodefaultlibs -shared -nostdlib
+else
 PRELOAD_LDFLAGS_COMMON_LINUX  = -nodefaultlibs -shared -Wl,-z,interpose,-z,initfirst
+endif
 PRELOAD_LDFLAGS_COMMON_FREEBSD  = -nodefaultlibs -shared -Wl,-z,interpose,-z,initfirst
 PRELOAD_LDFLAGS_COMMON_DARWIN = -dynamic -dynamiclib -all_load
 PRELOAD_LDFLAGS_COMMON_SOLARIS = -nodefaultlibs -shared -Wl,-z,interpose,-z,initfirst
@@ -335,13 +343,6 @@ if SOLARIS_XPG_SYMBOLS_PRESENT
 PRELOAD_LDFLAGS_COMMON_SOLARIS += -Wl,-M,$(top_srcdir)/solaris/vgpreload-solaris.mapfile
 endif
 
-if VGCONF_PLATVARIANT_IS_ANDROID
-# The Android toolchain includes all kinds of stdlib helpers present in
-# bionic which is bad because we are not linking with it and the Android
-# linker will panic.
-PRELOAD_LDFLAGS_COMMON_LINUX += -nostdlib
-endif
-
 PRELOAD_LDFLAGS_X86_LINUX      = $(PRELOAD_LDFLAGS_COMMON_LINUX) @FLAG_M32@
 PRELOAD_LDFLAGS_AMD64_LINUX    = $(PRELOAD_LDFLAGS_COMMON_LINUX) @FLAG_M64@
 PRELOAD_LDFLAGS_PPC32_LINUX    = $(PRELOAD_LDFLAGS_COMMON_LINUX) @FLAG_M32@

If someone could try that?

Still leaves the question why the original reporter doesn't seem to get any leakchecking.