OSDN Git Service

Disable the OSI allocation tracker.
authorPavlin Radoslavov <pavlin@google.com>
Mon, 1 Jun 2015 18:42:39 +0000 (11:42 -0700)
committerPavlin Radoslavov <pavlin@google.com>
Mon, 1 Jun 2015 22:39:48 +0000 (22:39 +0000)
Temporary disable the allocation tracker initialization
(even for BLUEDROID_DEBUG) when initializing Bluetooth.
This is a short-term workaround solution for several issues related to
the usage of the allocation tracker.

* Inconsistent usage of osi_malloc()/osi_calloc() and osi_free()
  - Within some of the Bluetooth-related unit tests we have two copies
    of the same libosi library: one copy statically linked against the
    unit test, and another dlopen() at runtime as part of bluetooth.default.so
    Each of those copy has its own static variables.
  - For the dlopen() copy we do call allocation_tracker_init(), while for the
    static copy within the unit test we don't call allocation_tracker_init()
  - Occasionally, there is a  memory allocation via osi_calloc()
    within the dlopen()-ed library, and then it is osi_free()-ed
    within the statically linked library.
 Such (mis)usage creates issues in two ways: (1) free()-ing incorrect
 pointer, and (2) the osi hash_map() in the dlopen()-ed library still considers
 the osi-tracked memory as allocated.
 NOTE: (1) could trigger random crashes, while (2) is the reason the unit
 test triggers an assert

* Avoid potential issues that could result from the fact that calling
  allocation_tracker_init() is not mandatory; i.e., the same
  issue described above could be triggered if osi_malloc()/osi_callod()
  was called before the call to allocation_tracker_init()

* There is still code that uses malloc(3)/calloc(3) and free(3) instead
  of osi_malloc()/osi_calloc() and osi_free()

Also, add missing pthread_mutex_lock()/pthread_mutex_unlock() guards,
and fix the allocation_tracker_uninit() implementation so it works
properly even if void allocation_tracker_init() wasn't called.

Bug: 21561735
Change-Id: Ic83d6cd40af1189c4ee9c1dbfd0ad8e4666e1502

btif/src/bluetooth.c
osi/src/allocation_tracker.c

index f842e5c..35d7501 100644 (file)
@@ -124,9 +124,17 @@ static int init(bt_callbacks_t *callbacks) {
   if (interface_ready())
     return BT_STATUS_DONE;
 
+  /*
+   * TODO: Temporary disable the allocation tracker initialization, and
+   * effectively the allocation tracker itself.
+   * This is a short-term workaround solution for several issues related to
+   * the usage of the allocation tracker.
+   */
+/*
 #ifdef BLUEDROID_DEBUG
   allocation_tracker_init();
 #endif
+*/
 
   bt_hal_cbacks = callbacks;
   stack_manager_get_interface()->init_stack();
index 7732d10..761e58d 100644 (file)
@@ -69,6 +69,8 @@ void allocation_tracker_init(void) {
   canary_size = strlen(canary);
 
   pthread_mutex_init(&lock, NULL);
+
+  pthread_mutex_lock(&lock);
   allocations = hash_map_new_internal(
     allocation_hash_map_size,
     hash_function_pointer,
@@ -76,19 +78,27 @@ void allocation_tracker_init(void) {
     free,
     NULL,
     &untracked_calloc_allocator);
+  pthread_mutex_unlock(&lock);
 }
 
 // Test function only. Do not call in the normal course of operations.
 void allocation_tracker_uninit(void) {
+  if (!allocations)
+    return;
+
+  pthread_mutex_lock(&lock);
   hash_map_free(allocations);
   allocations = NULL;
+  pthread_mutex_unlock(&lock);
 }
 
 void allocation_tracker_reset(void) {
   if (!allocations)
     return;
 
+  pthread_mutex_lock(&lock);
   hash_map_clear(allocations);
+  pthread_mutex_unlock(&lock);
 }
 
 size_t allocation_tracker_expect_no_allocations(void) {