libtalloc_debugging(3)

πŸ“¦
version 2.4.4 Β· 3 versions
, in centos-stream 10, fedora 43, opensuse-leap 16.0

✨ NAME ✨

okay so this is libtalloc_debugging(3) from the Library Functions Manual, and it's literally Chapter 6: Debugging πŸ’…. talloc makes memory management way easier than the C standard library, but bestie, developers are still only humans and can make mistakes 😭. So it's super handy to know some tools for inspecting talloc memory usage. main character energy for debugging, fr fr.

πŸ’… Talloc log and abort πŸ’…

We already met the abort function in section Dynamic type system. In that case it was used when a type mismatch was detected. But talloc calls this abort function in a bunch of other situations too:

  • when the provided pointer is not a valid talloc context,
  • when the meta data is invalid β€” probably due to memory corruption,
  • and when an access after free is detected.

The third one? literally the most interesting, iykyk. It can help us detect an attempt to double-free a context or any other manipulation with it via talloc functions (using it as a parent, stealing it, etc.).

Before the context is freed, talloc sets a flag in the meta data. This is then used to detect the access after free. It basically works on the assumption that the memory stays unchanged (at least for a while) even when it is properly deallocated. This will work even if the memory is filled with the value specified in the TALLOC_FREE_FILL environment variable, because it fills only the data part and leaves the meta data intact. no cap.

Apart from the abort function, talloc uses a log function to provide additional information to the aforementioned violations. To enable logging we shall set the log function with one of:

  • talloc_set_log_fn()
  • talloc_set_log_stderr()

The following code is a sample output of accessing a context after it has been freed:

talloc_set_log_stderr();
TALLOC_CTX *ctx = talloc_new(NULL);

talloc_free(ctx);
talloc_free(ctx);

results in:

talloc: access after free error - first free may be at ../src/main.c:55
Bad talloc magic value - access after free

Another example is an invalid context:

talloc_set_log_stderr();
TALLOC_CTX *ctx = talloc_new(NULL);
char *str = strdup("not a talloc context");
talloc_steal(ctx, str);

results in:

Bad talloc magic value - unknown value

🌸 Memory usage reports 🌸

talloc can print reports of memory usage of a specified talloc context to a file (to stdout or stderr). The report can be simple or full. The simple report provides information only about the context itself and its direct descendants. The full report goes recursively through the entire context tree. See:

  • talloc_report()
  • talloc_report_full()

We will use the following code to retrieve the sample report:

struct foo {
  char *str;
};

TALLOC_CTX *ctx = talloc_new(NULL);
char *str =  talloc_strdup(ctx, "my string");
struct foo *foo = talloc_zero(ctx, struct foo);
foo->str = talloc_strdup(foo, "I am Foo");
char *str2 = talloc_strdup(foo, "Foo is my parent");

/* print full report */
talloc_report_full(ctx, stdout);

It will print a full report of ctx to the standard output. The message should be similar to:

full talloc report on 'talloc_new: ../src/main.c:82' (total 46 bytes in 5 blocks)
  struct foo contains 34 bytes in 3 blocks (ref 0) 0x1495130
    Foo is my parent contains 17 bytes in 1 blocks (ref 0) 0x1495200
    I am Foo contains 9 bytes in 1 blocks (ref 0) 0x1495190
  my string contains 10 bytes in 1 blocks (ref 0) 0x14950c0

We can notice in this report that something is wrong with the context containing struct foo. We know that the structure has only one string element. However, we can see in the report that it has two children. This indicates that we have either violated the memory hierarchy or forgotten to free it as temporary data. Looking into the code, we can see that "Foo is my parent" should be attached to ctx.

πŸ‘€ See also: πŸ‘€

  • talloc_enable_null_tracking()
  • talloc_disable_null_tracking()
  • talloc_enable_leak_report()
  • talloc_enable_leak_report_full()

okay bye bestie! this page is from talloc, Version 2.0, and it's libtalloc_debugging(3) πŸ’œ (date? not in this footer, so I'm not inventing one, ngl). slay.