Protect filename_cache with lock in ota fault
Bug: 33173924 Test: Apply a successfully update on bullhead Change-Id: I28cc356e216a3e957b5533c338ee6bc3c0920222
This commit is contained in:
parent
b4b0c49c48
commit
4bb11c745b
2 changed files with 22 additions and 3 deletions
|
@ -23,7 +23,12 @@ otafault_static_libs := \
|
||||||
libbase \
|
libbase \
|
||||||
liblog
|
liblog
|
||||||
|
|
||||||
LOCAL_CFLAGS := -Werror
|
LOCAL_CFLAGS := \
|
||||||
|
-Werror \
|
||||||
|
-Wthread-safety \
|
||||||
|
-Wthread-safety-negative \
|
||||||
|
-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
|
||||||
|
|
||||||
LOCAL_SRC_FILES := config.cpp ota_io.cpp
|
LOCAL_SRC_FILES := config.cpp ota_io.cpp
|
||||||
LOCAL_MODULE_TAGS := eng
|
LOCAL_MODULE_TAGS := eng
|
||||||
LOCAL_MODULE := libotafault
|
LOCAL_MODULE := libotafault
|
||||||
|
|
|
@ -24,10 +24,13 @@
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
#include <android-base/thread_annotations.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
static std::map<intptr_t, const char*> filename_cache;
|
static std::mutex filename_mutex;
|
||||||
|
static std::map<intptr_t, const char*> filename_cache GUARDED_BY(filename_mutex);
|
||||||
static std::string read_fault_file_name = "";
|
static std::string read_fault_file_name = "";
|
||||||
static std::string write_fault_file_name = "";
|
static std::string write_fault_file_name = "";
|
||||||
static std::string fsync_fault_file_name = "";
|
static std::string fsync_fault_file_name = "";
|
||||||
|
@ -55,23 +58,28 @@ bool have_eio_error = false;
|
||||||
int ota_open(const char* path, int oflags) {
|
int ota_open(const char* path, int oflags) {
|
||||||
// Let the caller handle errors; we do not care if open succeeds or fails
|
// Let the caller handle errors; we do not care if open succeeds or fails
|
||||||
int fd = open(path, oflags);
|
int fd = open(path, oflags);
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
filename_cache[fd] = path;
|
filename_cache[fd] = path;
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ota_open(const char* path, int oflags, mode_t mode) {
|
int ota_open(const char* path, int oflags, mode_t mode) {
|
||||||
int fd = open(path, oflags, mode);
|
int fd = open(path, oflags, mode);
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
filename_cache[fd] = path;
|
filename_cache[fd] = path;
|
||||||
return fd; }
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
FILE* ota_fopen(const char* path, const char* mode) {
|
FILE* ota_fopen(const char* path, const char* mode) {
|
||||||
FILE* fh = fopen(path, mode);
|
FILE* fh = fopen(path, mode);
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
filename_cache[(intptr_t)fh] = path;
|
filename_cache[(intptr_t)fh] = path;
|
||||||
return fh;
|
return fh;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __ota_close(int fd) {
|
static int __ota_close(int fd) {
|
||||||
// descriptors can be reused, so make sure not to leave them in the cache
|
// descriptors can be reused, so make sure not to leave them in the cache
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
filename_cache.erase(fd);
|
filename_cache.erase(fd);
|
||||||
return close(fd);
|
return close(fd);
|
||||||
}
|
}
|
||||||
|
@ -85,6 +93,7 @@ int ota_close(unique_fd& fd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __ota_fclose(FILE* fh) {
|
static int __ota_fclose(FILE* fh) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
filename_cache.erase(reinterpret_cast<intptr_t>(fh));
|
filename_cache.erase(reinterpret_cast<intptr_t>(fh));
|
||||||
return fclose(fh);
|
return fclose(fh);
|
||||||
}
|
}
|
||||||
|
@ -99,6 +108,7 @@ int ota_fclose(unique_file& fh) {
|
||||||
|
|
||||||
size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
|
size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
|
||||||
if (should_fault_inject(OTAIO_READ)) {
|
if (should_fault_inject(OTAIO_READ)) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
auto cached = filename_cache.find((intptr_t)stream);
|
auto cached = filename_cache.find((intptr_t)stream);
|
||||||
const char* cached_path = cached->second;
|
const char* cached_path = cached->second;
|
||||||
if (cached != filename_cache.end() &&
|
if (cached != filename_cache.end() &&
|
||||||
|
@ -119,6 +129,7 @@ size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
|
||||||
|
|
||||||
ssize_t ota_read(int fd, void* buf, size_t nbyte) {
|
ssize_t ota_read(int fd, void* buf, size_t nbyte) {
|
||||||
if (should_fault_inject(OTAIO_READ)) {
|
if (should_fault_inject(OTAIO_READ)) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
auto cached = filename_cache.find(fd);
|
auto cached = filename_cache.find(fd);
|
||||||
const char* cached_path = cached->second;
|
const char* cached_path = cached->second;
|
||||||
if (cached != filename_cache.end()
|
if (cached != filename_cache.end()
|
||||||
|
@ -138,6 +149,7 @@ ssize_t ota_read(int fd, void* buf, size_t nbyte) {
|
||||||
|
|
||||||
size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
|
size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
|
||||||
if (should_fault_inject(OTAIO_WRITE)) {
|
if (should_fault_inject(OTAIO_WRITE)) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
auto cached = filename_cache.find((intptr_t)stream);
|
auto cached = filename_cache.find((intptr_t)stream);
|
||||||
const char* cached_path = cached->second;
|
const char* cached_path = cached->second;
|
||||||
if (cached != filename_cache.end() &&
|
if (cached != filename_cache.end() &&
|
||||||
|
@ -157,6 +169,7 @@ size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
|
||||||
|
|
||||||
ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
|
ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
|
||||||
if (should_fault_inject(OTAIO_WRITE)) {
|
if (should_fault_inject(OTAIO_WRITE)) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
auto cached = filename_cache.find(fd);
|
auto cached = filename_cache.find(fd);
|
||||||
const char* cached_path = cached->second;
|
const char* cached_path = cached->second;
|
||||||
if (cached != filename_cache.end() &&
|
if (cached != filename_cache.end() &&
|
||||||
|
@ -176,6 +189,7 @@ ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
|
||||||
|
|
||||||
int ota_fsync(int fd) {
|
int ota_fsync(int fd) {
|
||||||
if (should_fault_inject(OTAIO_FSYNC)) {
|
if (should_fault_inject(OTAIO_FSYNC)) {
|
||||||
|
std::lock_guard<std::mutex> lock(filename_mutex);
|
||||||
auto cached = filename_cache.find(fd);
|
auto cached = filename_cache.find(fd);
|
||||||
const char* cached_path = cached->second;
|
const char* cached_path = cached->second;
|
||||||
if (cached != filename_cache.end() &&
|
if (cached != filename_cache.end() &&
|
||||||
|
|
Loading…
Reference in a new issue