Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75755f9d authored by Andre Eisenbach's avatar Andre Eisenbach
Browse files

Guard btsnooz ringbuffer access from multiple threads

Since moving to HIDL, the btsnooz packet ringbuffer can be accessed from
two separate threads. Thus it should be guarded from concurrent access
to avoid pointer corruption.

Bug: 35182804
Test: manual
Change-Id: I3e6e1a869887a7ad5d87d8bb09ed78a22b3383ae
parent f091cf51
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
 *
 ******************************************************************************/

#include <mutex>

#include <base/logging.h>
#include <resolv.h>
#include <zlib.h>
@@ -39,6 +41,7 @@ static const size_t BLOCK_SIZE = 16384;
// Maximum line length in bugreport (should be multiple of 4 for base64 output)
static const uint8_t MAX_LINE_LENGTH = 128;

static std::mutex buffer_mutex;
static ringbuffer_t* buffer = NULL;
static uint64_t last_timestamp_ms = 0;

@@ -53,6 +56,8 @@ static void btsnoop_cb(const uint16_t type, const uint8_t* data,
  size_t included_length = btsnoop_calculate_packet_length(type, data, length);
  if (included_length == 0) return;

  std::lock_guard<std::mutex> lock(buffer_mutex);

  // Make room in the ring buffer

  while (ringbuffer_available(buffer) <
@@ -174,9 +179,6 @@ void btif_debug_btsnoop_init(void) {
}

void btif_debug_btsnoop_dump(int fd) {
  dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n",
          ringbuffer_size(buffer));

  ringbuffer_t* ringbuffer = ringbuffer_init(BTSNOOP_MEM_BUFFER_SIZE);
  if (ringbuffer == NULL) {
    dprintf(fd, "%s Unable to allocate memory for compression", __func__);
@@ -198,7 +200,14 @@ void btif_debug_btsnoop_dump(int fd) {

  size_t line_length = 0;

  bool rc = btsnoop_compress(ringbuffer, buffer);
  bool rc;
  {
    std::lock_guard<std::mutex> lock(buffer_mutex);
    dprintf(fd, "--- BEGIN:BTSNOOP_LOG_SUMMARY (%zu bytes in) ---\n",
            ringbuffer_size(buffer));
    rc = btsnoop_compress(ringbuffer, buffer);
  }

  if (rc == false) {
    dprintf(fd, "%s Log compression failed", __func__);
    goto error;