From: Philippe Waroquiers Date: Wed, 4 Jul 2012 21:59:29 +0000 (+0000) Subject: fix 302709 valgrind for ARM needs extra tls support for android emulator X-Git-Tag: svn/VALGRIND_3_8_0~158 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93f019ff737fee9fb9a1d6e7abc77d36e2e5fb36;p=thirdparty%2Fvalgrind.git fix 302709 valgrind for ARM needs extra tls support for android emulator Allow Valgrind to run on android emulator. + added README.android_emulator giving some details about versions used. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@12710 --- diff --git a/NEWS b/NEWS index a5825ab752..3a212de497 100644 --- a/NEWS +++ b/NEWS @@ -213,6 +213,7 @@ n-i-bz Fix assert in gdbserver for watchpoints watching the same address 302536 Fix for the POWER Valgrind regression test: memcheck-ISA2.0. 302656 Unhandled instruction (vcvtsi2ssq) == 273475 (canonical avx) +302709 valgrind for ARM needs extra tls support for android emulator under Linux Release 3.7.0 (5 November 2011) diff --git a/README.android b/README.android index 3c2eba09a7..9c43d7ee79 100644 --- a/README.android +++ b/README.android @@ -2,15 +2,23 @@ How to cross-compile for Android. These notes were last updated on 17 Feb 2012, for Valgrind SVN revision 12390/2257. -This is known to work at least for Android 4.0.3 running on a (rooted, -AOSP build) Nexus S and also on the same on Motorola Xoom. Android -2.3.4 on Nexus S worked at some time in the past. Other -configurations and toolchains might work, but haven't been tested. +This is known to work at least for : + Android 4.0.3 running on a (rooted, AOSP build) Nexus S. + Android 4.0.3 running on Motorola Xoom. + Android 4.0.3 running on android emulator. + +Android 2.3.4 on Nexus S worked at some time in the past. +It is known not to work on Android 4.1 running on android emulator. + +Other configurations and toolchains might work, but haven't been tested. Feedback is welcome. + You need the android-ndk-r6 native development kit. r6b and r7 give a non-completely-working build; see http://code.google.com/p/android/issues/detail?id=23203 +For the android emulator, the versions needed and how to +install them are described in README.android_emulator. Install it somewhere. Doesn't matter where. Then do this: @@ -33,6 +41,7 @@ export NDKROOT=/path/to/android-ndk-r6 # export HWKIND=nexus_s # Samsung Nexus S; also Xoom (for now) export HWKIND=pandaboard # Pandaboard running Linaro Android +export HWKIND=emulator # Android emulator # Then cd to the root of your Valgrind source tree. # @@ -63,7 +72,8 @@ CPPFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm -DANDROID_HARDWARE_$HW ./configure --prefix=/data/local/Inst \ --host=armv7-unknown-linux --target=armv7-unknown-linux \ --with-tmpdir=/sdcard - +# note: on android emulator, android-14 platform was also tested and works. +# It is not clear what this platform nr really is. # At the end of the configure run, a few lines of details # are printed. Make sure that you see these two lines: diff --git a/README.android_emulator b/README.android_emulator new file mode 100644 index 0000000000..4808c484b5 --- /dev/null +++ b/README.android_emulator @@ -0,0 +1,75 @@ + +How to install and run an android emulator. + +mkdir android # or any other place you prefer +cd android + +# download java JDK +# http://www.oracle.com/technetwork/java/javase/downloads/index.html +# download android SDK +# http://developer.android.com/sdk/index.html +# download android NDK +# http://developer.android.com/sdk/ndk/index.html + +# versions I used: +# jdk-7u4-linux-i586.tar.gz +# android-ndk-r8-linux-x86.tar.bz2 +# android-sdk_r18-linux.tgz + +# install jdk +tar xzf jdk-7u4-linux-i586.tar.gz + +# install sdk +tar xzf android-sdk_r18-linux.tgz + +# install ndk +tar xjf android-ndk-r8-linux-x86.tar.bz2 + + +# setup PATH to use the installed software: +export SDKROOT=$HOME/android/android-sdk-linux +export PATH=$PATH:$SDKROOT/tools:$SDKROOT/platform-tools +export NDKROOT=$HOME/android/android-ndk-r8 + +# install android platforms you want by starting: +android +# (from $SDKROOT/tools) + +# select the platforms you need +# I selected and installed: +# Android 4.0.3 (API 15) +# Upgraded then to the newer version available: +# Android sdk 20 +# Android platform tools 12 + +# Android 4.1 (API 16) does not work. + +# then define a virtual device: +Tools -> Manage AVDs... +# I define an AVD Name with 64 Mb SD Card, (4.0.3, api 15) +# rest is default + + +# compile and make install Valgrind, following README.android + + +# Start your android emulator (it takes some time). +# You can use adb shell to get a shell on the device +# and see it is working. Note that I usually get +# one or two time out from adb shell before it works +adb shell + +# Once the emulator is ready, push your Valgrind to the emulator: +adb push Inst / + + +# if you need to debug: +# You have on the android side a gdbserver +# on the device side: +gdbserver :1234 your_exe + +# on the host side: +adb forward tcp:1234 tcp:1234 +$HOME/android/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gdb your_exe +target remote :1234 + diff --git a/coregrind/m_initimg/initimg-linux.c b/coregrind/m_initimg/initimg-linux.c index 8acc7b0f51..691b381a94 100644 --- a/coregrind/m_initimg/initimg-linux.c +++ b/coregrind/m_initimg/initimg-linux.c @@ -690,6 +690,15 @@ Addr setup_client_stack( void* init_sp, "ARM has-neon from-auxv: %s\n", has_neon ? "YES" : "NO"); VG_(machine_arm_set_has_NEON)( has_neon ); + #define VKI_HWCAP_TLS 32768 + Bool has_tls = (auxv->u.a_val & VKI_HWCAP_TLS) > 0; + VG_(debugLog)(2, "initimg", + "ARM has-tls from-auxv: %s\n", + has_tls ? "YES" : "NO"); + /* If real hw sets properly HWCAP_TLS, we might + use this info to decide to really execute set_tls syscall + in syswrap-arm-linux.c rather than to base this on + conditional compilation. */ } # endif break; diff --git a/coregrind/m_syswrap/syswrap-arm-linux.c b/coregrind/m_syswrap/syswrap-arm-linux.c index e01bbaf57a..3792a8c0fb 100644 --- a/coregrind/m_syswrap/syswrap-arm-linux.c +++ b/coregrind/m_syswrap/syswrap-arm-linux.c @@ -151,6 +151,7 @@ asm( // forward declarations static void setup_child ( ThreadArchState*, ThreadArchState* ); +static void assign_guest_tls(ThreadId ctid, Addr tlsptr); static SysRes sys_set_tls ( ThreadId tid, Addr tlsptr ); /* @@ -233,9 +234,8 @@ static SysRes do_clone ( ThreadId ptid, VG_TRACK ( pre_thread_ll_create, ptid, ctid ); if (flags & VKI_CLONE_SETTLS) { - res = sys_set_tls(ctid, child_tls); - if (sr_isError(res)) - goto out; + /* Just assign the tls pointer in the guest TPIDRURO. */ + assign_guest_tls(ctid, child_tls); } flags &= ~VKI_CLONE_SETTLS; @@ -282,10 +282,53 @@ void setup_child ( /*OUT*/ ThreadArchState *child, child->vex_shadow2 = parent->vex_shadow2; } -static SysRes sys_set_tls ( ThreadId tid, Addr tlsptr ) +static void assign_guest_tls(ThreadId tid, Addr tlsptr) { VG_(threads)[tid].arch.vex.guest_TPIDRURO = tlsptr; +} + +/* Assigns tlsptr to the guest TPIDRURO. + If needed for the specific hardware, really executes + the set_tls syscall. +*/ +static SysRes sys_set_tls ( ThreadId tid, Addr tlsptr ) +{ + assign_guest_tls(tid, tlsptr); +#if defined(ANDROID_HARDWARE_emulator) + /* Android emulator does not provide an hw tls register. + So, the tls register is emulated by the kernel. + This emulated value is set by the __NR_ARM_set_tls syscall. + The emulated value must be read by the kernel helper function + located at 0xffff0fe0. + + The emulated tlsptr is located at 0xffff0ff0 + (so slightly after the kernel helper function). + Note that applications are not supposed to read this directly. + + For compatibility : if there is a hw tls register, the kernel + will put at 0xffff0fe0 the instructions to read it, so + as to have old applications calling the kernel helper + working properly. + + For having emulated guest TLS working correctly with + Valgrind, it is needed to execute the syscall to set + the emulated TLS value in addition to the assignment + of TPIDRURO. + + Note: the below means that if we need thread local storage + for Valgrind host, then there will be a conflict between + the need of the guest tls and of the host tls. + If all the guest code would cleanly call 0xffff0fe0, + then we might maybe intercept this. However, at least + __libc_preinit reads directly 0xffff0ff0. + */ + /* ??? might call the below if auxv->u.a_val & VKI_HWCAP_TLS ??? + Unclear if real hardware having tls hw register sets + VKI_HWCAP_TLS. */ + return VG_(do_syscall1) (__NR_ARM_set_tls, tlsptr); +#else return VG_(mk_SysRes_Success)( 0 ); +#endif } /* --------------------------------------------------------------------- @@ -1242,6 +1285,7 @@ PRE(sys_sigsuspend) PRE(sys_set_tls) { + PRINT("set_tls (%lx)",ARG1); PRE_REG_READ1(long, "set_tls", unsigned long, addr); SET_STATUS_from_SysRes( sys_set_tls( tid, ARG1 ) ); diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c index ce716782d5..ba84641b8e 100644 --- a/coregrind/m_syswrap/syswrap-linux.c +++ b/coregrind/m_syswrap/syswrap-linux.c @@ -5536,6 +5536,13 @@ POST(sys_ioctl) /* END Pandaboard specific ioctls */ +# elif defined(ANDROID_HARDWARE_emulator) + + /* BEGIN emulator specific ioctls */ + /* currently none are known */ + /* END emulator specific ioctls */ + + # else /* no ANDROID_HARDWARE_anything defined */ # warning "" diff --git a/coregrind/vg_preloaded.c b/coregrind/vg_preloaded.c index 24a8f71674..03038acb73 100644 --- a/coregrind/vg_preloaded.c +++ b/coregrind/vg_preloaded.c @@ -89,6 +89,27 @@ void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void) return (void*)result; } +#if defined(ANDROID_HARDWARE_emulator) +/* When running on android emulator, we get the following error when doing + ./valgrind date + + link_image[1921]: 494 could not load needed library + '/data/local/Inst/lib/valgrind/vgpreload_core-arm-linux.so' for 'date' + (reloc_library[1285]: 494 cannot locate '__cxa_finalize'... + )CANNOT LINK EXECUTABLE + + This problem is bypassed by adding the below function. + Do not ask me to explain neither the problem nor the solution. +*/ +extern void __cxa_finalize(void); +void __cxa_finalize(void) +{ + // ??? what should we do here ? Silently do nothing looks not sane. + // So, try to crash: + *(volatile int *)0 = 'x'; +} +#endif + #elif defined(VGO_darwin) #include "config.h" /* VERSION */ diff --git a/docs/internals/3_7_BUGSTATUS.txt b/docs/internals/3_7_BUGSTATUS.txt index df55c7f730..bfad790d36 100644 --- a/docs/internals/3_7_BUGSTATUS.txt +++ b/docs/internals/3_7_BUGSTATUS.txt @@ -251,7 +251,6 @@ get fixed. 302578] New: Unrecognized isntruction 0xc5 0x32 0xc2 0xca 0x09 vcmpngess 302630] New: Memcheck on ultithreaded program fails with Assertion 'sizeof(UWord) == sizeof(UInt)' failed in m_syscall.c 302700] New: Assertion 'srcix >= 0 && srcix < VG_(sizeXA)(srcxa)' failed. -302709] New: valgrind for ARM needs extra tls support for android emulator under Linux 302800] New: valgrind change the operator new's alloctor into system's -- Sat 30 June 2012, near midnight. diff --git a/docs/xml/dist-docs.xml b/docs/xml/dist-docs.xml index ad2c9a28cd..083a86040e 100644 --- a/docs/xml/dist-docs.xml +++ b/docs/xml/dist-docs.xml @@ -95,6 +95,16 @@ + + README.android_emulator + + + + + README.mips