resolved conflicts for merge of c0133a73 to lmp-mr1-dev-plus-aosp
				
					
				
			Change-Id: I31d81e91d796fa27f9c88a7edc96c97320ade72c
This commit is contained in:
		@@ -1,140 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
 * Copyright (C) 2010 The Android Open Source Project
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 | 
				
			||||||
 * you may not use this file except in compliance with the License.
 | 
					 | 
				
			||||||
 * You may obtain a copy of the License at
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Unless required by applicable law or agreed to in writing, software
 | 
					 | 
				
			||||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
					 | 
				
			||||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					 | 
				
			||||||
 * See the License for the specific language governing permissions and
 | 
					 | 
				
			||||||
 * limitations under the License.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifndef UNIQUE_PTR_H_included
 | 
					 | 
				
			||||||
#define UNIQUE_PTR_H_included
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Default deleter for pointer types.
 | 
					 | 
				
			||||||
template <typename T>
 | 
					 | 
				
			||||||
struct DefaultDelete {
 | 
					 | 
				
			||||||
    enum { type_must_be_complete = sizeof(T) };
 | 
					 | 
				
			||||||
    DefaultDelete() {}
 | 
					 | 
				
			||||||
    void operator()(T* p) const {
 | 
					 | 
				
			||||||
        delete p;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Default deleter for array types.
 | 
					 | 
				
			||||||
template <typename T>
 | 
					 | 
				
			||||||
struct DefaultDelete<T[]> {
 | 
					 | 
				
			||||||
    enum { type_must_be_complete = sizeof(T) };
 | 
					 | 
				
			||||||
    void operator()(T* p) const {
 | 
					 | 
				
			||||||
        delete[] p;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// A smart pointer that deletes the given pointer on destruction.
 | 
					 | 
				
			||||||
// Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr
 | 
					 | 
				
			||||||
// and boost::scoped_array).
 | 
					 | 
				
			||||||
// Named to be in keeping with Android style but also to avoid
 | 
					 | 
				
			||||||
// collision with any other implementation, until we can switch over
 | 
					 | 
				
			||||||
// to unique_ptr.
 | 
					 | 
				
			||||||
// Use thus:
 | 
					 | 
				
			||||||
//   UniquePtr<C> c(new C);
 | 
					 | 
				
			||||||
template <typename T, typename D = DefaultDelete<T> >
 | 
					 | 
				
			||||||
class UniquePtr {
 | 
					 | 
				
			||||||
public:
 | 
					 | 
				
			||||||
    // Construct a new UniquePtr, taking ownership of the given raw pointer.
 | 
					 | 
				
			||||||
    explicit UniquePtr(T* ptr = nullptr) : mPtr(ptr) { }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    UniquePtr(UniquePtr<T, D>&& that) {
 | 
					 | 
				
			||||||
      mPtr = that.mPtr;
 | 
					 | 
				
			||||||
      that.mPtr = nullptr;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ~UniquePtr() {
 | 
					 | 
				
			||||||
        reset();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Accessors.
 | 
					 | 
				
			||||||
    T& operator*() const { return *mPtr; }
 | 
					 | 
				
			||||||
    T* operator->() const { return mPtr; }
 | 
					 | 
				
			||||||
    T* get() const { return mPtr; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Returns the raw pointer and hands over ownership to the caller.
 | 
					 | 
				
			||||||
    // The pointer will not be deleted by UniquePtr.
 | 
					 | 
				
			||||||
    T* release() __attribute__((warn_unused_result)) {
 | 
					 | 
				
			||||||
        T* result = mPtr;
 | 
					 | 
				
			||||||
        mPtr = nullptr;
 | 
					 | 
				
			||||||
        return result;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Takes ownership of the given raw pointer.
 | 
					 | 
				
			||||||
    // If this smart pointer previously owned a different raw pointer, that
 | 
					 | 
				
			||||||
    // raw pointer will be freed.
 | 
					 | 
				
			||||||
    void reset(T* ptr = nullptr) {
 | 
					 | 
				
			||||||
        if (ptr != mPtr) {
 | 
					 | 
				
			||||||
            D()(mPtr);
 | 
					 | 
				
			||||||
            mPtr = ptr;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
private:
 | 
					 | 
				
			||||||
    // The raw pointer.
 | 
					 | 
				
			||||||
    T* mPtr;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Comparing unique pointers is probably a mistake, since they're unique.
 | 
					 | 
				
			||||||
    template <typename T2> bool operator==(const UniquePtr<T2>& p) const = delete;
 | 
					 | 
				
			||||||
    template <typename T2> bool operator!=(const UniquePtr<T2>& p) const = delete;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Disallow copy and assignment.
 | 
					 | 
				
			||||||
    UniquePtr(const UniquePtr&) = delete;
 | 
					 | 
				
			||||||
    void operator=(const UniquePtr&) = delete;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Partial specialization for array types. Like std::unique_ptr, this removes
 | 
					 | 
				
			||||||
// operator* and operator-> but adds operator[].
 | 
					 | 
				
			||||||
template <typename T, typename D>
 | 
					 | 
				
			||||||
class UniquePtr<T[], D> {
 | 
					 | 
				
			||||||
public:
 | 
					 | 
				
			||||||
    explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    UniquePtr(UniquePtr<T, D>&& that) {
 | 
					 | 
				
			||||||
      mPtr = that.mPtr;
 | 
					 | 
				
			||||||
      that.mPtr = nullptr;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ~UniquePtr() {
 | 
					 | 
				
			||||||
        reset();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    T& operator[](size_t i) const {
 | 
					 | 
				
			||||||
        return mPtr[i];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    T* get() const { return mPtr; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    T* release() __attribute__((warn_unused_result)) {
 | 
					 | 
				
			||||||
        T* result = mPtr;
 | 
					 | 
				
			||||||
        mPtr = NULL;
 | 
					 | 
				
			||||||
        return result;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    void reset(T* ptr = NULL) {
 | 
					 | 
				
			||||||
        if (ptr != mPtr) {
 | 
					 | 
				
			||||||
            D()(mPtr);
 | 
					 | 
				
			||||||
            mPtr = ptr;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
private:
 | 
					 | 
				
			||||||
    T* mPtr;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Disallow copy and assignment.
 | 
					 | 
				
			||||||
    UniquePtr(const UniquePtr&) = delete;
 | 
					 | 
				
			||||||
    void operator=(const UniquePtr&) = delete;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif  // UNIQUE_PTR_H_included
 | 
					 | 
				
			||||||
@@ -44,8 +44,6 @@
 | 
				
			|||||||
#include "private/KernelArgumentBlock.h"
 | 
					#include "private/KernelArgumentBlock.h"
 | 
				
			||||||
#include "private/ScopedPthreadMutexLocker.h"
 | 
					#include "private/ScopedPthreadMutexLocker.h"
 | 
				
			||||||
#include "private/ScopedFd.h"
 | 
					#include "private/ScopedFd.h"
 | 
				
			||||||
#include "private/ScopeGuard.h"
 | 
					 | 
				
			||||||
#include "private/UniquePtr.h"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "linker.h"
 | 
					#include "linker.h"
 | 
				
			||||||
#include "linker_debug.h"
 | 
					#include "linker_debug.h"
 | 
				
			||||||
@@ -172,6 +170,7 @@ DISALLOW_ALLOCATION(void, free, (void* u __unused));
 | 
				
			|||||||
DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
 | 
					DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
 | 
				
			||||||
DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
 | 
					DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static char tmp_err_buf[768];
 | 
				
			||||||
static char __linker_dl_err_buf[768];
 | 
					static char __linker_dl_err_buf[768];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
char* linker_get_error_buffer() {
 | 
					char* linker_get_error_buffer() {
 | 
				
			||||||
@@ -796,6 +795,8 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld
 | 
				
			|||||||
    file_guard.reset(fd);
 | 
					    file_guard.reset(fd);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ElfReader elf_reader(name, fd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    struct stat file_stat;
 | 
					    struct stat file_stat;
 | 
				
			||||||
    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
 | 
					    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
 | 
				
			||||||
      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
 | 
					      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
 | 
				
			||||||
@@ -820,7 +821,6 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Read the ELF header and load the segments.
 | 
					    // Read the ELF header and load the segments.
 | 
				
			||||||
  ElfReader elf_reader(name, fd);
 | 
					 | 
				
			||||||
    if (!elf_reader.Load(extinfo)) {
 | 
					    if (!elf_reader.Load(extinfo)) {
 | 
				
			||||||
        return nullptr;
 | 
					        return nullptr;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -835,15 +835,16 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld
 | 
				
			|||||||
  si->phnum = elf_reader.phdr_count();
 | 
					  si->phnum = elf_reader.phdr_count();
 | 
				
			||||||
  si->phdr = elf_reader.loaded_phdr();
 | 
					  si->phdr = elf_reader.loaded_phdr();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!si->PrelinkImage()) {
 | 
					    // At this point we know that whatever is loaded @ base is a valid ELF
 | 
				
			||||||
 | 
					    // shared library whose segments are properly mapped in.
 | 
				
			||||||
 | 
					    TRACE("[ load_library base=%p size=%zu name='%s' ]",
 | 
				
			||||||
 | 
					          reinterpret_cast<void*>(si->base), si->size, si->name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!si->LinkImage(extinfo)) {
 | 
				
			||||||
      soinfo_free(si);
 | 
					      soinfo_free(si);
 | 
				
			||||||
      return nullptr;
 | 
					      return nullptr;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for_each_dt_needed(si, [&] (const char* name) {
 | 
					 | 
				
			||||||
    load_tasks.push_back(LoadTask::create(name, si));
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return si;
 | 
					    return si;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -862,23 +863,13 @@ static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name,
 | 
				
			|||||||
  soinfo* si = find_loaded_library_by_name(name);
 | 
					  soinfo* si = find_loaded_library_by_name(name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Library might still be loaded, the accurate detection
 | 
					  // Library might still be loaded, the accurate detection
 | 
				
			||||||
  // of this fact is done by load_library.
 | 
					  // of this fact is done by load_library
 | 
				
			||||||
  if (si == nullptr) {
 | 
					  if (si == nullptr) {
 | 
				
			||||||
    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
 | 
					    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
 | 
				
			||||||
    si = load_library(load_tasks, name, rtld_flags, extinfo);
 | 
					    si = load_library(load_tasks, name, rtld_flags, extinfo);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return si;
 | 
					  if (si != nullptr && (si->flags & FLAG_LINKED) == 0) {
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void soinfo_unload(soinfo* si);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static bool is_recursive(soinfo* si, soinfo* parent) {
 | 
					 | 
				
			||||||
  if (parent == nullptr) {
 | 
					 | 
				
			||||||
    return false;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (si == parent) {
 | 
					 | 
				
			||||||
    DL_ERR("recursive link to \"%s\"", si->name);
 | 
					    DL_ERR("recursive link to \"%s\"", si->name);
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -954,10 +945,7 @@ static bool find_libraries(const char* const library_names[], size_t library_nam
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // All is well - found_libs and load_tasks are empty at this point
 | 
					  return si;
 | 
				
			||||||
  // and all libs are successfully linked.
 | 
					 | 
				
			||||||
  failure_guard.disable();
 | 
					 | 
				
			||||||
  return true;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
 | 
					static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
 | 
				
			||||||
@@ -971,7 +959,6 @@ static soinfo* find_library(const char* name, int rtld_flags, const android_dlex
 | 
				
			|||||||
  if (!find_libraries(&name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
 | 
					  if (!find_libraries(&name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
 | 
				
			||||||
    return nullptr;
 | 
					    return nullptr;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  return si;
 | 
					  return si;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -987,8 +974,10 @@ static void soinfo_unload(soinfo* si) {
 | 
				
			|||||||
        soinfo_unload(child);
 | 
					        soinfo_unload(child);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      for_each_dt_needed(si, [&] (const char* library_name) {
 | 
					      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
 | 
				
			||||||
        TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
 | 
					        if (d->d_tag == DT_NEEDED) {
 | 
				
			||||||
 | 
					          const char* library_name = si->strtab + d->d_un.d_val;
 | 
				
			||||||
 | 
					          TRACE("%s needs to unload %s", si->name, library_name);
 | 
				
			||||||
          soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
 | 
					          soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
 | 
				
			||||||
          if (needed != nullptr) {
 | 
					          if (needed != nullptr) {
 | 
				
			||||||
            soinfo_unload(needed);
 | 
					            soinfo_unload(needed);
 | 
				
			||||||
@@ -997,7 +986,8 @@ static void soinfo_unload(soinfo* si) {
 | 
				
			|||||||
            // Since we cannot really handle errors at this point - print and continue.
 | 
					            // Since we cannot really handle errors at this point - print and continue.
 | 
				
			||||||
            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
 | 
					            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
      });
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    notify_gdb_of_unload(si);
 | 
					    notify_gdb_of_unload(si);
 | 
				
			||||||
@@ -1068,6 +1058,9 @@ static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#if defined(USE_RELA)
 | 
					#if defined(USE_RELA)
 | 
				
			||||||
int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
					int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
				
			||||||
 | 
					  ElfW(Sym)* s;
 | 
				
			||||||
 | 
					  soinfo* lsi;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for (size_t idx = 0; idx < count; ++idx, ++rela) {
 | 
					  for (size_t idx = 0; idx < count; ++idx, ++rela) {
 | 
				
			||||||
    unsigned type = ELFW(R_TYPE)(rela->r_info);
 | 
					    unsigned type = ELFW(R_TYPE)(rela->r_info);
 | 
				
			||||||
    unsigned sym = ELFW(R_SYM)(rela->r_info);
 | 
					    unsigned sym = ELFW(R_SYM)(rela->r_info);
 | 
				
			||||||
@@ -1079,10 +1072,6 @@ int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
				
			|||||||
    if (type == 0) { // R_*_NONE
 | 
					    if (type == 0) { // R_*_NONE
 | 
				
			||||||
      continue;
 | 
					      continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    ElfW(Sym)* s = nullptr;
 | 
					 | 
				
			||||||
    soinfo* lsi = nullptr;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (sym != 0) {
 | 
					    if (sym != 0) {
 | 
				
			||||||
      sym_name = get_string(symtab[sym].st_name);
 | 
					      sym_name = get_string(symtab[sym].st_name);
 | 
				
			||||||
      s = soinfo_do_lookup(this, sym_name, &lsi);
 | 
					      s = soinfo_do_lookup(this, sym_name, &lsi);
 | 
				
			||||||
@@ -1143,6 +1132,8 @@ int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
				
			|||||||
        sym_addr = lsi->resolve_symbol_address(s);
 | 
					        sym_addr = lsi->resolve_symbol_address(s);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      count_relocation(kRelocSymbol);
 | 
					      count_relocation(kRelocSymbol);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      s = nullptr;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (type) {
 | 
					    switch (type) {
 | 
				
			||||||
@@ -1678,9 +1669,6 @@ void soinfo::CallConstructors() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void soinfo::CallDestructors() {
 | 
					void soinfo::CallDestructors() {
 | 
				
			||||||
  if (!constructors_called) {
 | 
					 | 
				
			||||||
    return;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  TRACE("\"%s\": calling destructors", name);
 | 
					  TRACE("\"%s\": calling destructors", name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // DT_FINI_ARRAY must be parsed in reverse order.
 | 
					  // DT_FINI_ARRAY must be parsed in reverse order.
 | 
				
			||||||
@@ -1766,7 +1754,7 @@ int soinfo::get_rtld_flags() {
 | 
				
			|||||||
  return 0;
 | 
					  return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This is a return on get_children()/get_parents() if
 | 
					// This is a return on get_children() in case
 | 
				
			||||||
// 'this->flags' does not have FLAG_NEW_SOINFO set.
 | 
					// 'this->flags' does not have FLAG_NEW_SOINFO set.
 | 
				
			||||||
static soinfo::soinfo_list_t g_empty_list;
 | 
					static soinfo::soinfo_list_t g_empty_list;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1956,7 +1944,7 @@ bool soinfo::PrelinkImage() {
 | 
				
			|||||||
        // if the dynamic table is writable
 | 
					        // if the dynamic table is writable
 | 
				
			||||||
// FIXME: not working currently for N64
 | 
					// FIXME: not working currently for N64
 | 
				
			||||||
// The flags for the LOAD and DYNAMIC program headers do not agree.
 | 
					// The flags for the LOAD and DYNAMIC program headers do not agree.
 | 
				
			||||||
// The LOAD section containing the dynamic table has been mapped as
 | 
					// The LOAD section containng the dynamic table has been mapped as
 | 
				
			||||||
// read-only, but the DYNAMIC header claims it is writable.
 | 
					// read-only, but the DYNAMIC header claims it is writable.
 | 
				
			||||||
#if !(defined(__mips__) && defined(__LP64__))
 | 
					#if !(defined(__mips__) && defined(__LP64__))
 | 
				
			||||||
        if ((dynamic_flags & PF_W) != 0) {
 | 
					        if ((dynamic_flags & PF_W) != 0) {
 | 
				
			||||||
@@ -2135,7 +2123,37 @@ bool soinfo::PrelinkImage() {
 | 
				
			|||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool soinfo::LinkImage(const android_dlextinfo* extinfo) {
 | 
					    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
 | 
				
			||||||
 | 
					    if (flags & FLAG_EXE) {
 | 
				
			||||||
 | 
					        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
 | 
				
			||||||
 | 
					        size_t preload_count = 0;
 | 
				
			||||||
 | 
					        for (size_t i = 0; g_ld_preload_names[i] != nullptr; i++) {
 | 
				
			||||||
 | 
					            soinfo* lsi = find_library(g_ld_preload_names[i], 0, nullptr);
 | 
				
			||||||
 | 
					            if (lsi != nullptr) {
 | 
				
			||||||
 | 
					                g_ld_preloads[preload_count++] = lsi;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
 | 
				
			||||||
 | 
					                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
 | 
				
			||||||
 | 
					                        g_ld_preload_names[i], name, linker_get_error_buffer());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
 | 
				
			||||||
 | 
					        if (d->d_tag == DT_NEEDED) {
 | 
				
			||||||
 | 
					            const char* library_name = strtab + d->d_un.d_val;
 | 
				
			||||||
 | 
					            DEBUG("%s needs %s", name, library_name);
 | 
				
			||||||
 | 
					            soinfo* lsi = find_library(library_name, 0, nullptr);
 | 
				
			||||||
 | 
					            if (lsi == nullptr) {
 | 
				
			||||||
 | 
					                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
 | 
				
			||||||
 | 
					                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
 | 
				
			||||||
 | 
					                       library_name, name, tmp_err_buf);
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            add_child(lsi);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if !defined(__LP64__)
 | 
					#if !defined(__LP64__)
 | 
				
			||||||
  if (has_text_relocations) {
 | 
					  if (has_text_relocations) {
 | 
				
			||||||
@@ -2258,7 +2276,6 @@ static void add_vdso(KernelArgumentBlock& args __unused) {
 | 
				
			|||||||
  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
 | 
					  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
 | 
				
			||||||
  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 | 
					  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  si->PrelinkImage();
 | 
					 | 
				
			||||||
  si->LinkImage(nullptr);
 | 
					  si->LinkImage(nullptr);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -2543,7 +2560,7 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) {
 | 
				
			|||||||
  linker_so.phnum = elf_hdr->e_phnum;
 | 
					  linker_so.phnum = elf_hdr->e_phnum;
 | 
				
			||||||
  linker_so.flags |= FLAG_LINKER;
 | 
					  linker_so.flags |= FLAG_LINKER;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!(linker_so.PrelinkImage() && linker_so.LinkImage(nullptr))) {
 | 
					  if (!linker_so.LinkImage(nullptr)) {
 | 
				
			||||||
    // It would be nice to print an error message, but if the linker
 | 
					    // It would be nice to print an error message, but if the linker
 | 
				
			||||||
    // can't link itself, there's no guarantee that we'll be able to
 | 
					    // can't link itself, there's no guarantee that we'll be able to
 | 
				
			||||||
    // call write() (because it involves a GOT reference). We may as
 | 
					    // call write() (because it involves a GOT reference). We may as
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -206,7 +206,6 @@ struct soinfo {
 | 
				
			|||||||
  void CallConstructors();
 | 
					  void CallConstructors();
 | 
				
			||||||
  void CallDestructors();
 | 
					  void CallDestructors();
 | 
				
			||||||
  void CallPreInitConstructors();
 | 
					  void CallPreInitConstructors();
 | 
				
			||||||
  bool PrelinkImage();
 | 
					 | 
				
			||||||
  bool LinkImage(const android_dlextinfo* extinfo);
 | 
					  bool LinkImage(const android_dlextinfo* extinfo);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void add_child(soinfo* child);
 | 
					  void add_child(soinfo* child);
 | 
				
			||||||
@@ -224,7 +223,6 @@ struct soinfo {
 | 
				
			|||||||
  int get_rtld_flags();
 | 
					  int get_rtld_flags();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  soinfo_list_t& get_children();
 | 
					  soinfo_list_t& get_children();
 | 
				
			||||||
  soinfo_list_t& get_parents();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
 | 
					  ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -718,6 +718,19 @@ void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_co
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    *dynamic = reinterpret_cast<ElfW(Dyn)*>(load_bias + phdr->p_vaddr);
 | 
				
			||||||
 | 
					    if (dynamic_count) {
 | 
				
			||||||
 | 
					      *dynamic_count = (unsigned)(phdr->p_memsz / 8);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (dynamic_flags) {
 | 
				
			||||||
 | 
					      *dynamic_flags = phdr->p_flags;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  *dynamic = nullptr;
 | 
				
			||||||
 | 
					  if (dynamic_count) {
 | 
				
			||||||
 | 
					    *dynamic_count = 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -114,7 +114,6 @@ libBionicStandardTests_src_files := \
 | 
				
			|||||||
    system_properties_test.cpp \
 | 
					    system_properties_test.cpp \
 | 
				
			||||||
    time_test.cpp \
 | 
					    time_test.cpp \
 | 
				
			||||||
    uchar_test.cpp \
 | 
					    uchar_test.cpp \
 | 
				
			||||||
    uniqueptr_test.cpp \
 | 
					 | 
				
			||||||
    unistd_test.cpp \
 | 
					    unistd_test.cpp \
 | 
				
			||||||
    wchar_test.cpp \
 | 
					    wchar_test.cpp \
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -254,160 +254,6 @@ include $(LOCAL_PATH)/Android.build.testlib.mk
 | 
				
			|||||||
module := libtest_relo_check_dt_needed_order_2
 | 
					module := libtest_relo_check_dt_needed_order_2
 | 
				
			||||||
include $(LOCAL_PATH)/Android.build.testlib.mk
 | 
					include $(LOCAL_PATH)/Android.build.testlib.mk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# Libraries used by dlfcn tests to verify correct load order:
 | 
					 | 
				
			||||||
# libtest_check_order_2_right.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_2_right_src_files := \
 | 
					 | 
				
			||||||
    dlopen_testlib_answer.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_2_right_cflags := -D__ANSWER=42
 | 
					 | 
				
			||||||
module := libtest_check_order_2_right
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order_a.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_a_src_files := \
 | 
					 | 
				
			||||||
    dlopen_testlib_answer.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_a_cflags := -D__ANSWER=1
 | 
					 | 
				
			||||||
module := libtest_check_order_a
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order_b.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_b_src_files := \
 | 
					 | 
				
			||||||
    dlopen_testlib_answer.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_b_cflags := -D__ANSWER=2 -D__ANSWER2=43
 | 
					 | 
				
			||||||
module := libtest_check_order_b
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order_c.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_3_c_src_files := \
 | 
					 | 
				
			||||||
    dlopen_testlib_answer.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_3_c_cflags := -D__ANSWER=3
 | 
					 | 
				
			||||||
module := libtest_check_order_3_c
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order_d.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_d_src_files := \
 | 
					 | 
				
			||||||
   dlopen_testlib_answer.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_d_shared_libraries := libtest_check_order_b
 | 
					 | 
				
			||||||
libtest_check_order_d_cflags := -D__ANSWER=4 -D__ANSWER2=4
 | 
					 | 
				
			||||||
module := libtest_check_order_d
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order_left.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_1_left_src_files := \
 | 
					 | 
				
			||||||
    empty.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_1_left_shared_libraries := libtest_check_order_a libtest_check_order_b
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_check_order_1_left
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_check_order.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_check_order_src_files := \
 | 
					 | 
				
			||||||
    empty.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_check_order_shared_libraries := libtest_check_order_1_left \
 | 
					 | 
				
			||||||
  libtest_check_order_2_right libtest_check_order_3_c
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_check_order
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# Library with dependency loop used by dlfcn tests
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# libtest_with_dependency_loop -> a -> b -> c -> a
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_src_files := empty.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_shared_libraries := \
 | 
					 | 
				
			||||||
    libtest_with_dependency_loop_a
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_with_dependency_loop
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_with_dependency_loop_a.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_a_src_files := empty.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_a_shared_libraries := \
 | 
					 | 
				
			||||||
    libtest_with_dependency_loop_b_tmp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_with_dependency_loop_a
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_with_dependency_loop_b.so
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# this is temporary placeholder - will be removed
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_b_tmp_src_files := empty.cpp
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_b_tmp_ldflags := -Wl,-soname=libtest_with_dependency_loop_b.so
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_with_dependency_loop_b_tmp
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_with_dependency_loop_b.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_b_src_files := empty.cpp
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_b_shared_libraries := libtest_with_dependency_loop_c
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_with_dependency_loop_b
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
# libtest_with_dependency_loop_c.so
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_c_src_files := empty.cpp
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
libtest_with_dependency_loop_c_shared_libraries := \
 | 
					 | 
				
			||||||
    libtest_with_dependency_loop_a
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module := libtest_with_dependency_loop_c
 | 
					 | 
				
			||||||
build_type := target
 | 
					 | 
				
			||||||
build_target := SHARED_LIBRARY
 | 
					 | 
				
			||||||
include $(TEST_PATH)/Android.build.mk
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# -----------------------------------------------------------------------------
 | 
					# -----------------------------------------------------------------------------
 | 
				
			||||||
# libtest_relo_check_dt_needed_order.so
 | 
					# libtest_relo_check_dt_needed_order.so
 | 
				
			||||||
# |
 | 
					# |
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,25 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
 * Copyright (C) 2014 The Android Open Source Project
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 | 
				
			||||||
 * you may not use this file except in compliance with the License.
 | 
					 | 
				
			||||||
 * You may obtain a copy of the License at
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Unless required by applicable law or agreed to in writing, software
 | 
					 | 
				
			||||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
					 | 
				
			||||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					 | 
				
			||||||
 * See the License for the specific language governing permissions and
 | 
					 | 
				
			||||||
 * limitations under the License.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
extern "C" int dlopen_test_get_answer() {
 | 
					 | 
				
			||||||
  return __ANSWER;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifdef __ANSWER2
 | 
					 | 
				
			||||||
extern "C" int dlopen_test_get_answer2() {
 | 
					 | 
				
			||||||
  return __ANSWER2;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
@@ -1,101 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
 * Copyright (C) 2014 The Android Open Source Project
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 | 
				
			||||||
 * you may not use this file except in compliance with the License.
 | 
					 | 
				
			||||||
 * You may obtain a copy of the License at
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * Unless required by applicable law or agreed to in writing, software
 | 
					 | 
				
			||||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
					 | 
				
			||||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					 | 
				
			||||||
 * See the License for the specific language governing permissions and
 | 
					 | 
				
			||||||
 * limitations under the License.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include <gtest/gtest.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#include <private/UniquePtr.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static int cCount = 0;
 | 
					 | 
				
			||||||
struct C {
 | 
					 | 
				
			||||||
  C() { ++cCount; }
 | 
					 | 
				
			||||||
  ~C() { --cCount; }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static bool freed = false;
 | 
					 | 
				
			||||||
struct Freer {
 | 
					 | 
				
			||||||
  void operator() (int* p) {
 | 
					 | 
				
			||||||
    ASSERT_EQ(123, *p);
 | 
					 | 
				
			||||||
    free(p);
 | 
					 | 
				
			||||||
    freed = true;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
TEST(UniquePtr, smoke) {
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
  // UniquePtr<T> tests...
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Can we free a single object?
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
    UniquePtr<C> c(new C);
 | 
					 | 
				
			||||||
    ASSERT_TRUE(cCount == 1);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 0);
 | 
					 | 
				
			||||||
  // Does release work?
 | 
					 | 
				
			||||||
  C* rawC;
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<C> c(new C);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 1);
 | 
					 | 
				
			||||||
      rawC = c.release();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 1);
 | 
					 | 
				
			||||||
  delete rawC;
 | 
					 | 
				
			||||||
  // Does reset work?
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<C> c(new C);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 1);
 | 
					 | 
				
			||||||
      c.reset(new C);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 1);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
  // UniquePtr<T[]> tests...
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  // Can we free an array?
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<C[]> cs(new C[4]);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 4);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 0);
 | 
					 | 
				
			||||||
  // Does release work?
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<C[]> c(new C[4]);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 4);
 | 
					 | 
				
			||||||
      rawC = c.release();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 4);
 | 
					 | 
				
			||||||
  delete[] rawC;
 | 
					 | 
				
			||||||
  // Does reset work?
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<C[]> c(new C[4]);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 4);
 | 
					 | 
				
			||||||
      c.reset(new C[2]);
 | 
					 | 
				
			||||||
      ASSERT_TRUE(cCount == 2);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(cCount == 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
  // Custom deleter tests...
 | 
					 | 
				
			||||||
  //
 | 
					 | 
				
			||||||
  ASSERT_TRUE(!freed);
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
      UniquePtr<int, Freer> i(reinterpret_cast<int*>(malloc(sizeof(int))));
 | 
					 | 
				
			||||||
      *i = 123;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  ASSERT_TRUE(freed);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user