Commit graph

177 commits

Author SHA1 Message Date
Bjorn Andersson
0753dc653e pthread: Invalidate stale stack pointers on pthread_exit()
A call to pthread_key_delete() after pthread_exit() have unmapped the stack of a thread
but before the ongoing pthread_join() have finished executing will result in an access
to unmapped memory.
Avoid this by invalidating the stack_base and tls pointers during pthread_exit().

This is based on the investigation and proprosed solution by
Srinavasa Nagaraju <srinavasa.x.nagaraju@sonyericsson.com>

Change-Id: I145fb5d57930e91b00f1609d7b2cd16a55d5b3a9
2012-05-08 17:43:57 -07:00
Pierre Peiffer
d0c884d359 Let pthread_create fail if schedparam can't be set
The creation of a thread succeeds even if the requested scheduling
parameters can not be set. This is not POSIX compliant, and even
worse, it leads to a wrong behavior. Let pthread_create() fail in this
case.

Change-Id: Ice66e2a720975c6bde9fe86c2cf8f649533a169c
Signed-off-by: Christian Bejram <christian.bejram@stericsson.com>
2012-05-08 10:54:51 -07:00
Xi Wang
7f5aa4f35e bionic: fix integer overflows in chk_malloc(), leak_malloc(), and leak_memalign()
The allocation size in chk_malloc(), leak_malloc(), and leak_memalign()
functions may be rounded up to a small value, leading to buffer overflows.
The code only runs in debugging mode.

This patch complements commit 6f04a0f4 (CVE-2009-0607).

Change-Id: Id899bcd2bcd2ea2205e5753c433390710032dc83
Signed-off-by: Xi Wang <xi.wang@gmail.com>
2012-05-07 10:50:21 -07:00
Ken Sumrall
6baffed252 Add the posix_memalign(3) function to bionic
The posix_memalign(3) function is very similar to the traditional
memalign(3) function, but with better error reporting and a guarantee
that the memory it allocates can be freed.  In bionic, memalign(3)
allocated memory can be freed, so posix_memalign(3) is just a wrapper
around memalign(3).

Change-Id: I62ee908aa5ba6b887d8446a00d8298d080a6a299
2012-04-27 09:34:53 -07:00
Jack Ren
34e89c232d bionic: fix atfork hanlder_mutex deadlock
After applying the kernel_id fix, the system refused to boot up and we
got following crash log:
I/DEBUG   (  113): pid: 618, tid: 618  >>> org.simalliance.openmobileapi.service:remote <<<
I/DEBUG   (  113): signal 16 (SIGSTKFLT), code -6 (?), fault addr --------
I/DEBUG   (  113):  eax fffffe00  ebx b77de994  ecx 00000080  edx 00724002
I/DEBUG   (  113):  esi 00000000  edi 00004000
I/DEBUG   (  113):  xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000000 xss 0000007b
I/DEBUG   (  113):  eip b7761351  ebp bfdf3de8  esp bfdf3dc4  flags 00000202
I/DEBUG   (  113):     #00  eip: 00015351  /system/lib/libc.so
I/DEBUG   (  113):     #01  eip: 0000d13c  /system/lib/libc.so (pthread_mutex_lock)
I/DEBUG   (  113):     #02  eip: 00077b48  /system/lib/libc.so (__bionic_atfork_run_prepare)
I/DEBUG   (  113):     #03  eip: 00052cdb  /system/lib/libc.so (fork)
I/DEBUG   (  113):     #04  eip: 0009ae91  /system/lib/libdvm.so (_Z18dvmOptimizeDexFileillPKcjjb)
I/DEBUG   (  113):     #05  eip: 000819d6  /system/lib/libdvm.so (_Z14dvmJarFileOpenPKcS0_PP7JarFileb)
I/DEBUG   (  113):     #06  eip: 000b175e  /system/lib/libdvm.so (_ZL40Dalvik_dalvik_system_DexFile_openDexFilePKjP6JValue)
I/DEBUG   (  113):     #07  eip: 0011fb94  /system/lib/libdvm.so

Root cause:
The atfork uses the mutex handler_mutex to protect the atfork_head. The
parent will call __bionic_atfork_run_prepare() to lock the handler_mutex,
and need both the parent and child to unlock their own copy of handler_mutex
after fork. At that time, the owner of hanlder_mutex is set as the parent.
If we apply the kernel_id fix, then the child's kernel_id will be set as
child's tid.
The handler_mutex is a recursive lock, and pthread_mutex_unlock(&hander_mutex)
will fail because the mutex owner is the parent, while the current tid
(__get_thread()->kernel_id) is child, not matched with the mutex owner.
At that time, the handler_mutex is left in lock state.If the child wants to
fork other process after than, then it will try to lock handler_mutex, and
then be deadlocked.

Fix:
Since the child has its own copy of vm space from the the parent, the
child space's handler_mutex should be reset to the initialized state.

Change-Id: I3907dd9a153418fb78862f2aa6d0302c375d9e27
Signed-off-by: Jack Ren <jack.ren@intel.com>
Signed-off-by: Chenyang Du <chenyang.du@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2012-03-17 17:08:54 +08:00
Jack Ren
d8bc6e7119 bionic: Fix wrong kernel_id in pthread descriptor after fork()
After forking, the kernel_id field in the phtread_internal_t returned by pthread_self()
is incorrect --- it's the tid from the parent, not the new tid of the
child.

The root cause is that: currently the kernel_id is set by
_init_thread(), which is called in 2 cases:
(1) called by __libc_init_common(). That happens when the execv( ) is
called after fork( ). But when the zygote tries to fork the android
application, the child application doesn't call execv( ), instread, it
tries to call the Java main method directly.
(2) called by pthread_create(). That happens when a new thread is
created.

For the lead thread which is the thread created by fork(), it should
call execv() but it doesn't, as described in (1) above. So its kernel_id
will inherit the parent's kernel_id.

Fixed it in this patch.

Change-Id: I63513e82af40ec5fe51fbb69456b1843e4bc0fc7
Signed-off-by: Chenyang Du <chenyang.du@intel.com>
Signed-off-by: Jack Ren <jack.ren@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2012-03-12 23:14:56 +08:00
Rabin Vincent
177ba8cb42 Prevent deadlock when using fork
When forking of a new process in bionic, it is critical that it
does not allocate any memory according to the comment in
java_lang_ProcessManager.c:
"Note: We cannot malloc() or free() after this point!
A no-longer-running thread may be holding on to the heap lock, and
an attempt to malloc() or free() would result in deadlock."
However, as fork is using standard lib calls when tracing it a bit,
they might allocate memory, and thus causing the deadlock.
This is a rewrite so that the function cpuacct_add, that fork calls,
will use system calls instead of standard lib calls.

Signed-off-by: christian bejram <christian.bejram@stericsson.com>

Change-Id: Iff22ea6b424ce9f9bf0ac8e9c76593f689e0cc86
2011-12-06 08:39:18 -08:00
Bruce Beare
e4a21c89a8 signal: Align the sigset_t size passed to from user space to kernel.
Pass kernel space sigset_t size to __rt_sigprocmask to workaround
the miss-match of NSIG/sigset_t definition between kernel and bionic.

Note: Patch originally from Google...
Change-Id: I4840fdc56d0b90d7ce2334250f04a84caffcba2a
Signed-off-by: Chenyang Du <chenyang.du@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-12-05 18:37:33 -08:00
Bruce Beare
89d3fdcae2 MALLOC_DEBUG: enable the option libc.debug.malloc = 10
Fix the compile warning to let the libc.debug.malloc=10 works well
Due to unsuitable value comparison, which cause compiler optimize the
code of comparing two digits.

Change-Id: I0bedd596c9ca2ba308fb008da20ecb328d8548f5
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
Author: liu chuansheng <chuansheng.liu@intel.com>
2011-12-05 14:58:55 -08:00
Jack Ren
e480fc83b2 bionic: fix pthread_{create, exit}/signal race condition
(1) in pthread_create:
    If the one signal is received before esp is subtracted by 16 and
    __thread_entry( ) is called, the stack will be cleared by kernel
    when it tries to contruct the signal stack frame. That will cause
    that __thread_entry will get a wrong tls pointer from the stack
    which leads to the segment fault when trying to access tls content.

(2) in pthread_exit
    After pthread_exit called system call unmap(), its stack will be
    freed.  If one signal is received at that time, there is no stack
    available for it.

Fixed by subtracting the child's esp by 16 before the clone system
call and by blocking signal handling before pthread_exit is started.

Author: Jack Ren <jack.ren@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-11-29 17:09:51 -08:00
Glenn Kasten
d53cae0e45 Add non-NDK internal API __pthread_gettid
Tracking bugs 5267571 and 5090073 (for deadlock detection logs).

Change-Id: Icb90f91ec1525607551c2234ef921bf88296484f
2011-09-16 12:38:28 -07:00
David 'Digit' Turner
30e30acf10 am 6b6ebeca: am 3a131026: resolved conflicts for merge of 50a83255 to gingerbread-plus-aosp
* commit '6b6ebeca985fb3843b56b507ac4ac1be44080a9c':
  enable support for large files (> 2G)
  Enable functional DSO object destruction
  x86: Enable -fstack-protector
  Update X86 Bionic CRT files for unwind/exceptions
  bionic, libthread_db x86 fixes
  Updated gcc 4.4.3 IA toolchain doesn't require the .ctors list
  Remove an extra register move.
  Replace __atomic_XXX with GCC __sync_XXX intrinsics.
  move some typedefs to procfs.h required by gdbserver build
  use consistent guards for off_t and size_t defines for IA
  Simplify variable typing for IA builds
  sigsetmask.c was not processing the "mask" argument.
  Add defines for CAIF support
  Remove extra/unneeded copy of fenv.h
  Use proper variable typing
  Update ATOM string routines to latest
  Fix undefined reference to dl_iterate_phdr for x86
  Fix missing NL
  ptrace.c Fix source file format to unix from dos
2011-07-13 14:53:56 -07:00
David 'Digit' Turner
6b6ebeca98 am 3a131026: resolved conflicts for merge of 50a83255 to gingerbread-plus-aosp
* commit '3a13102637c8be53edf28f96598ac11aaa3e14df':
  enable support for large files (> 2G)
  Enable functional DSO object destruction
  x86: Enable -fstack-protector
  Update X86 Bionic CRT files for unwind/exceptions
  bionic, libthread_db x86 fixes
  Updated gcc 4.4.3 IA toolchain doesn't require the .ctors list
  Remove an extra register move.
  Replace __atomic_XXX with GCC __sync_XXX intrinsics.
  move some typedefs to procfs.h required by gdbserver build
  use consistent guards for off_t and size_t defines for IA
  Simplify variable typing for IA builds
  sigsetmask.c was not processing the "mask" argument.
  Add defines for CAIF support
  Remove extra/unneeded copy of fenv.h
  Use proper variable typing
  Update ATOM string routines to latest
  Fix undefined reference to dl_iterate_phdr for x86
  Fix missing NL
  ptrace.c Fix source file format to unix from dos
2011-07-11 13:25:01 -07:00
David 'Digit' Turner
3a13102637 resolved conflicts for merge of 50a83255 to gingerbread-plus-aosp
Change-Id: Idf1971120bbdd52676f95aa3aa69f62342dc012e
2011-07-11 21:39:14 +02:00
James Rose
3435fc600d bionic, libthread_db x86 fixes
Orig-Change-Id: I3be997f5f1f6a894a3c200d4f325cf3bfd428c66
Author: James Rose <james.rose@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-07-07 22:46:16 +02:00
Bruce Beare
bf29647964 Updated gcc 4.4.3 IA toolchain doesn't require the .ctors list
Orig-Change-Id: Ia840a19a45257128eccdcf25d105f500f2d90741
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-07-07 22:46:16 +02:00
Bruce Beare
0f2dc2b60c ptrace.c Fix source file format to unix from dos
Orig-Change-Id: Ia771b457eec7f9575d4631fb1c12a7062ebcc7de
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-07-07 22:46:15 +02:00
Jean-Baptiste Queru
681c4bd782 Merge b3773e9c
Change-Id: I5787d9ac5e745a08e1c891e9ce9efc4e7f97ace8
2011-07-06 13:00:05 -07:00
Jean-Baptiste Queru
cb5529efd8 Merge 784515ad
Change-Id: I3e5b691d008e9548f43339635bafdd95bc2f56fe
2011-07-06 12:59:55 -07:00
Jean-Baptiste Queru
c993768360 Revert "Revert "libc: Add logcat error message for memory corruption""
This reverts commit 368ee1e4d6.
2011-07-06 12:58:56 -07:00
David 'Digit' Turner
b3773e9cc8 am 9c95cbf7: Merge "Really fix the build."
* commit '9c95cbf75094c15d251bb3d57a55ce47d7c6c2c8':
  Really fix the build.
2011-07-06 10:27:49 -07:00
David 'Digit' Turner
c51871d4b2 Really fix the build.
libcutils/mspace.c includes libc/bionic/dlmalloc.c, we need to
take care of the fact that any internal C library function cannot
be used from it.

Change-Id: I0bc81ae090b7ac2d464f26b97fc6b94a08cdad9c
2011-07-06 19:02:15 +02:00
David 'Digit' Turner
784515ad93 am 01eb7f72: Merge "Fix broken build"
* commit '01eb7f72434830bb14cec906d885d2b7ee40d53b':
  Fix broken build
2011-07-06 09:08:47 -07:00
David 'Digit' Turner
a4824467c3 Fix broken build
Change-Id: Ia46b50aec51a55434c8828a73e07f4732f8f6c1c
2011-07-06 17:54:35 +02:00
Mathew Inwood
368ee1e4d6 Revert "libc: Add logcat error message for memory corruption"
This fixes the build.

This reverts commit 7708a89c60.
2011-07-06 16:51:54 +01:00
David 'Digit' Turner
877923d369 am d25ea49e: am b73b6783: Merge "libc: Add logcat error message for memory corruption"
* commit 'd25ea49e632c00d57d893563dac3f6dae5493b7e':
  libc: Add logcat error message for memory corruption
2011-07-06 07:53:10 -07:00
David 'Digit' Turner
d25ea49e63 am b73b6783: Merge "libc: Add logcat error message for memory corruption"
* commit 'b73b6783a595cf4b1cef6463c23317a8b417f1e9':
  libc: Add logcat error message for memory corruption
2011-07-06 07:46:40 -07:00
David 'Digit' Turner
7708a89c60 libc: Add logcat error message for memory corruption
Our dlmalloc implementation currently calls abort() when it detects
that the heap is corrupted, or that an invalid pointer is passed to
one of its functions.

The only way to detect this is because abort() will force-fully
crash the current program with a magic fault address of '0xdeadbaad'.

However, this is not really well documented, and a frequent topic
on the android-ndk forum (among others).

This change makes our dlmalloc code dump a simple message to the
log just before the abort() call (and hence before the stack trace)
to better help identify the problem.

Change-Id: Iebf7eb7fe26463ecadfaca8f247d237edb441e3c
2011-07-05 19:53:53 +02:00
Brian Carlstrom
1f8e2672a8 Implement getpwnam_r(3) and getpwuid_r(3).
These functions were already declared in <pwd.h>, but hadn't been implemented
yet.

git cherry-pick --no-commit 081504af74

Change-Id: I316acf4cffb9f2c6788e8e342aa620f9a00886d5
2011-05-27 00:52:21 -07:00
David Turner
54e494d909 am add0a45a: am e8f7e30b: Merge "bionic, libthread_db x86 fixes"
* commit 'add0a45a117f00553e79e7137d023416f9c0a54f':
  bionic, libthread_db x86 fixes
2011-05-11 13:11:56 -07:00
David Turner
add0a45a11 am e8f7e30b: Merge "bionic, libthread_db x86 fixes"
* commit 'e8f7e30b05c6ba103d64c1fb3cd5ef2b17be7aab':
  bionic, libthread_db x86 fixes
2011-05-10 17:19:13 -07:00
Bruce Beare
8e551a6319 bionic, libthread_db x86 fixes
Change-Id: I3be997f5f1f6a894a3c200d4f325cf3bfd428c66
Author: James Rose <james.rose@intel.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-04-27 09:12:58 -07:00
David Turner
3be409a38c am 70245736: am aad685ce: am 4c090434: Merge "removing shadowing redeclaration of result"
* commit '702457363c2b1b01f0d9cb0093a48ce28753e275':
  removing shadowing redeclaration of result
2011-04-11 14:50:33 -07:00
David Turner
702457363c am aad685ce: am 4c090434: Merge "removing shadowing redeclaration of result"
* commit 'aad685ce001495260a3ce0c9d9284a6ee3ca09c0':
  removing shadowing redeclaration of result
2011-04-11 14:46:48 -07:00
Brad Fitzpatrick
5f133f3c4d am 31e2feee: am 2dc801f9: am 8da75ab8: Ignore property set timeouts.
* commit '31e2feeef3dffb49ce742a6ae1c9280c792c7c49':
  Ignore property set timeouts.
2011-04-01 12:27:07 -07:00
Brad Fitzpatrick
2dc801f9fc am 8da75ab8: Ignore property set timeouts.
* commit '8da75ab8936b0b7fcf8dd9a3befeb696ee6aa39d':
  Ignore property set timeouts.
2011-04-01 12:24:15 -07:00
Brad Fitzpatrick
8da75ab893 Ignore property set timeouts.
Change-Id: Ic3f6119398368ba047736370336d0260905abd40
2011-04-01 10:53:12 -07:00
Brad Fitzpatrick
099423ba1c am 23bc3ff7: Don\'t futex_wait spin when setting properties. Wait for socket close.
* commit '23bc3ff71dffdfec208aee05938e544c7cb3bc37':
  Don't futex_wait spin when setting properties. Wait for socket close.
2011-03-30 15:48:11 -07:00
Brad Fitzpatrick
23bc3ff71d Don't futex_wait spin when setting properties. Wait for socket close.
Depends on init change I8dd685ea

Bug: 4185486
Change-Id: I5a2dbc3b7be1759212d4a3988d9033b9b947a1db
2011-03-30 15:28:11 -07:00
Brad Fitzpatrick
bf90b57b44 Don't futex_wait spin when setting properties. Wait for socket close.
Depends on init change I8dd685ea

Bug: 4185486
Change-Id: I3e80cecfad8e072973003ec6f93146c5cad369ac
2011-03-30 14:25:27 -07:00
Christopher Schwardt
0f5587097b removing shadowing redeclaration of result
Change-Id: Ic1f86962dfb8620cf5c63ff4913b2f0bf908abb5
2011-03-22 15:35:40 +01:00
Carl Shapiro
2cc2b2be69 Add some missing includes.
Change-Id: Ieec623c06bc32ec78334f628af25b00c2bccd2e7
2011-03-21 20:01:03 -07:00
satok
0b3c5c50f7 do not merge. Move property setting from libcutils to bionic.
Backport I110b653a58f3

All the other property stuff is already here.  Property setting was
only in libcutils previously to leverage a utility function / constant
or two.

Unfortunately in the process of fixing a race condition we would've
had to do break abstraction boundaries and put some libc-internal
details into libcutils so instead of that we'll just move this
into bionic.

Along with Iee1ca9b7, this now passes:

$ adb shell am instrument -w -e class android.os.SystemPropertiesTest \
  com.android.frameworks.coretests.systemproperties/android.test.InstrumentationTestRunner

  Bug: 3511230

Change-Id: I1b588db3344169621e1279ecc0b660cf4e1015d7
2011-03-15 23:06:58 -07:00
satok
ec7e8cc9dd do not merge. Move property setting from libcutils to bionic.
Backport I110b653a58f3

All the other property stuff is already here.  Property setting was
only in libcutils previously to leverage a utility function / constant
or two.

Unfortunately in the process of fixing a race condition we would've
had to do break abstraction boundaries and put some libc-internal
details into libcutils so instead of that we'll just move this
into bionic.

Along with Iee1ca9b7, this now passes:

$ adb shell am instrument -w -e class android.os.SystemPropertiesTest \
  com.android.frameworks.coretests.systemproperties/android.test.InstrumentationTestRunner

  Bug: 3511230

Change-Id: I1b588db3344169621e1279ecc0b660cf4e1015d7
2011-03-15 11:02:26 +09:00
Brad Fitzpatrick
4399df8f2e Move property setting from libcutils to bionic.
All the other property stuff is already here.  Property setting was
only in libcutils previously to leverage a utility function / constant
or two.

Unfortunately in the process of fixing a race condition we would've
had to do break abstraction boundaries and put some libc-internal
details into libcutils so instead of that we'll just move this
into bionic.

Along with Iee1ca9b7, this now passes:

$ adb shell am instrument -w -e class android.os.SystemPropertiesTest \
  com.android.frameworks.coretests.systemproperties/android.test.InstrumentationTestRunner

Bug: 3511230
Change-Id: I110b653a58f312fbe069dca59892a877ae9bc911
2011-03-14 16:53:18 -07:00
Brad Fitzpatrick
f9316d37b4 am 08a4ce98: am 0c398985: Merge "[ENDIAN] Build md5 code correctly for bigendian targets"
* commit '08a4ce983ba14cef394e5198c9561349ec7fef44':
  [ENDIAN] Build md5 code correctly for bigendian targets
2011-03-14 11:50:56 -07:00
Paul Lind
865e3789dd [ENDIAN] Build md5 code correctly for bigendian targets
Signed-off-by: Raghu Gandham <raghu@mips.com>
2011-03-10 11:13:50 -08:00
David 'Digit' Turner
81d79f9fb3 am 962dcb22: am fed58049: Merge "libc: Fix PTHREAD_RWLOCK_INITIALIZER"
* commit '962dcb22218a1a6d4ebd05e4fc4a69875d037234':
  libc: Fix PTHREAD_RWLOCK_INITIALIZER
  Proxy getnameinfo through netd
  Updated gcc 4.4.3 IA toolchain doesn't require the .ctors list
  Convert cname lenght before use
2011-03-10 01:55:35 -08:00
Bruce Beare
48e1feaa9d Updated gcc 4.4.3 IA toolchain doesn't require the .ctors list
Change-Id: Ia840a19a45257128eccdcf25d105f500f2d90741
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
2011-02-16 17:50:23 -08:00
Andy McFadden
4ce737f5df Added _memmove_words
Added a memmove() variant for Dalvik's System.arraycopy()
implementation.  It guarantees 16-bit or 32-bit atomicity depending
on the alignment of the arguments.

Bug 3398352

Change-Id: Ie7bd246305ef0ff8290513663327c5b81680368d
2011-02-04 14:49:49 -08:00