Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions libr/include/r_util/r_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#define R_LOG_H

#include <r_userconf.h>
#include <r_list.h>
#include <r_types.h>
#include <stdarg.h>
// NOTE: avoid including <r_vec.h> here because it includes r_assert.h, which
// includes this header, causing a circular dependency.

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -39,6 +42,8 @@ typedef enum r_log_level {

typedef bool (*RLogCallback)(void *user, int type, const char *origin, const char *msg);

typedef struct r_vec_RVecRLogCallbackUser_t RVecRLogCallbackUser;

typedef struct r_log_t {
int level; // skip messages lower than this level
int traplevel; // skip messages lower than this level
Expand All @@ -50,7 +55,7 @@ typedef struct r_log_t {
bool show_origin;
bool show_source;
bool show_ts;
RList *cbs;
RVecRLogCallbackUser *cbs;
PrintfCallback cb_printf;
} RLog;

Expand Down
39 changes: 20 additions & 19 deletions libr/util/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typedef struct r_log_cbuser_t {
RLogCallback cb;
} RLogCallbackUser;

R_VEC_TYPE (RVecRLogCallbackUser, RLogCallbackUser);

static const char *level_tags[] = { // Log level to tag string lookup array
[R_LOG_LEVEL_FATAL] = "FATAL",
[R_LOG_LEVEL_ERROR] = "ERROR",
Expand Down Expand Up @@ -78,7 +80,7 @@ R_API void r_log_fini(void) {
if (rlog) {
RLog *log = rlog;
rlog = NULL;
r_list_free (log->cbs);
RVecRLogCallbackUser_free (log->cbs);
free (log->file);
free (log->filter);
free (log);
Expand Down Expand Up @@ -161,10 +163,9 @@ R_API bool r_log_match(int level, const char *origin) {
}
}
if (rlog->cbs) {
RListIter *iter;
RLogCallbackUser *cbu;
r_list_foreach (rlog->cbs, iter, cbu) {
if (cbu->cb (cbu->user, level, origin, NULL)) {
RLogCallbackUser *iter;
R_VEC_FOREACH (rlog->cbs, iter) {
if (iter->cb (iter->user, level, origin, NULL)) {
return true;
}
}
Expand All @@ -179,10 +180,9 @@ R_API void r_log_vmessage(RLogLevel level, const char *origin, const char *func,
}
vsnprintf (out, sizeof (out), fmt, ap);
if (rlog->cbs) {
RListIter *iter;
RLogCallbackUser *cbu;
r_list_foreach (rlog->cbs, iter, cbu) {
if (cbu->cb (cbu->user, level, origin, out)) {
RLogCallbackUser *iter;
R_VEC_FOREACH (rlog->cbs, iter) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Iterate callbacks over stable storage

This loop walks rlog->cbs with a raw element pointer, but callbacks are allowed to call r_log_add_callback/r_log_del_callback while handling a log message. With the new RVec backend, push_back can realloc at capacity boundaries (e.g. when growing from 8 to 9 callbacks), which invalidates iter mid-loop and can lead to use-after-free or corrupted iteration; removals can also shift elements and skip callbacks. A safer approach is to iterate by index over a captured length (or clone/snapshot the vector) before invoking user callbacks.

Useful? React with 👍 / 👎.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe good to fix what this comment mentions. even if its stupid to add and dlete log callbacks in the event of a logcallback execution it can happen and cause ugly crashes. i can think of two solutions:

  1. forbid that (easier, fster, more direct). just have a boolean inside rlog that tells addcallback and delcallback that we are iterating and we wont accept them to run, just return error if thats the case. this boolean can be set and unset before and after this R_VEC_FOREACH loop.

  2. add a second list of callbacks that is not iterated from here, fill it when we call add/dell a callback and sync it in an atomic way if there's any call. this will add more runtime code weight. and make log more complicated. but the resulting implementation will be more flexible.

i vote for the 1st option. what do you think? is there any relevant situation that im missing to be able to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with option 1 to keep it simple and avoid extra runtime weight.
One detail: r_log_add_callback/r_log_del_callback currently return void, so we can’t return an error without API changes.
I propose to guard callback iteration with a boolean lock and make add/del no-op while iterating.
That prevents iterator invalidation/crashes with minimal complexity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me, lets do that

if (iter->cb (iter->user, level, origin, out)) {
return;
}
}
Expand Down Expand Up @@ -251,21 +251,22 @@ R_API void r_log_add_callback(RLogCallback cb, void *user) {
return;
}
if (!rlog->cbs) {
rlog->cbs = r_list_newf (free);
rlog->cbs = RVecRLogCallbackUser_new ();
}
RLogCallbackUser *cbu = R_NEW (RLogCallbackUser);
cbu->cb = cb;
cbu->user = user;
r_list_append (rlog->cbs, cbu);
RLogCallbackUser cbu = {
.cb = cb,
.user = user,
};
RVecRLogCallbackUser_push_back (rlog->cbs, &cbu);
}

R_API void r_log_del_callback(RLogCallback cb) {
if (r_log_init ()) {
RLogCallbackUser *p;
RListIter *iter;
r_list_foreach (rlog->cbs, iter, p) {
if (cb == p->cb) {
r_list_delete (rlog->cbs, iter);
size_t i;
for (i = 0; rlog->cbs && i < RVecRLogCallbackUser_length (rlog->cbs); i++) {
RLogCallbackUser *p = RVecRLogCallbackUser_at (rlog->cbs, i);
if (p && cb == p->cb) {
RVecRLogCallbackUser_remove (rlog->cbs, i);
return;
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/unit/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ static Sdb *setup_sdb(void) {
return res;
}

typedef struct {
int calls;
} LogCbCtx;

static bool log_cb_counter(void *user, int type, const char *origin, const char *msg) {
(void)type;
(void)origin;
if (msg) {
LogCbCtx *ctx = (LogCbCtx *)user;
ctx->calls++;
}
return false;
}

bool test_dll_names(void) {
Sdb *TDB = setup_sdb ();
char *s;
Expand Down Expand Up @@ -237,6 +251,13 @@ bool test_log(void) {
// https://github.com/radareorg/radare2/issues/22468
R_LOG_INFO ("%s", "");

LogCbCtx ctx = { 0 };
r_log_add_callback (log_cb_counter, &ctx);
R_LOG_INFO ("hi");
r_log_del_callback (log_cb_counter);
R_LOG_INFO ("bye");
mu_assert_eq (ctx.calls, 1, "r_log_del_callback should unregister callback");

r_core_free (core);
mu_end;
}
Expand Down
Loading