Skip to content

Potential errors(e.g., deadlock) due to the incorrect lock usage #207

@jenny-cheung

Description

@jenny-cheung

Hi, developers, thank you for your checking. The lock &gdb->client_mutex may be acquired twice, which can lead to deadlock? Also, it seems the lock &gdb->client_mutex is not released in the thread correctly because there is no cen64_mutex_unlock(&gdb->client_mutex); statement.

cen64_mutex_lock(&gdb->client_mutex);

cen64_mutex_lock(&gdb->client_mutex);

The relevant code is shown:

CEN64_THREAD_RETURN_TYPE gdb_thread(void *opaque) {
  cen64_thread_setname(NULL, "gdb");
  struct gdb *gdb = (struct gdb *) opaque;

  cen64_mutex_lock(&gdb->client_mutex); // acquire the lock first time

  // wait until first breakpoint is hit before entering loop
  if (gdb->flags & GDB_FLAGS_INITIAL) {
    cen64_cv_wait(&gdb->client_semaphore, &gdb->client_mutex);
  } else {
    cen64_mutex_lock(&gdb->client_mutex); // acquire the lock second time
  }

 ...;
  return CEN64_THREAD_RETURN_VAL;
}
cen64_cold bool gdb_init(struct gdb* gdb, struct cen64_device* device, const char* host) {
  ...;

  if (cen64_thread_create(&gdb->thread, gdb_thread, gdb)) { // thread call site
    cen64_mutex_destroy(&gdb->client_mutex);
    cen64_cv_destroy(&gdb->client_semaphore);
    printf("Failed to create gdb thread.\n");
    return false;
  }

  return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions