Skip to content

require newer glib2 to enable autofree'ing of stack variables exiting scope

Daniel P. Berrangé qemu-devel 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest3 patches 6 repliesIn review
  2. v13 patches 5 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3glib: bump min required glib library version to 2.48configure crypto/hmac-glib.c include/glib-compat.h+3 -460 replies
2/3crypto: define cleanup functions for use with g_autoptrinclude/crypto/block.h include/crypto/cipher.h include/crypto/hmac.h +2 more+10 -00 replies
3/3crypto: use auto cleanup for many stack variablescrypto/afsplit.c crypto/block-luks.c crypto/block.c +5 more+65 -1321 reply

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchStefan HajnocziMarc-André Lureau
SeriesR-b
1/3R-b
2/3R-b
3/3R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [Qemu-devel] [PATCH v2 0/3] require newer glib2 to enable autofree'ing of stack variables exiting scope

Both GCC and CLang support a C extension attribute((cleanup)) which allows you to define a function that is invoked when a stack variable exits scope. This typically used to free the memory allocated to it, though you're not restricted to this. For example it could be used to unlock a mutex.

We could use that functionality now, but the syntax is a bit ugly in plain C. Since version 2.44 of GLib, there have been a few macros to make it more friendly to use - g_autofree, g_autoptr and G_DEFINE_AUTOPTR_CLEANUP_FUNC.

https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html

https://blogs.gnome.org/desrt/2015/01/30/g_autoptr/

The key selling point is that it simplifies the cleanup code paths, often eliminating the need to goto cleanup labels. This improves the readability of the code and makes it less likely that you'll leak memory accidentally.

Inspired by seeing it added to glib, and used in systemd, Libvirt finally got around to adopting this in Feb 2019. Overall our experience with it has been favourable/positive, with the code simplification being very nice.

The main caveats with it are

- Only works with GCC or CLang. We don't care as those are the only two compilers we declare support for.

- You must always initialize the variables when declared to ensure predictable behaviour when they leave scope. Chances are most methods with goto jumps for cleanup are doing this already

- You must not directly return the value that's assigned to a auto-cleaned variable. You must steal the pointer in some way. ie

BAD:
g_autofree char *wibble = g_strdup("wibble")
....
return wibble;

GOOD:
g_autofree char *wibble = g_strdup("wibble")
...
return g_steal_pointer(wibble);

g_steal_pointer is an inline function which simply copies the pointer to a new variable, and sets the original variable to NULL, thus avoiding cleanup.

I've illustrated the usage by converting a bunch of the crypto code in QEMU to use auto cleanup.

Changed on v2:

- Actually commit the rest of the changes to patch 3 so that what's posted works :-) Sigh.

Daniel P. Berrangé (3):
glib: bump min required glib library version to 2.48
crypto: define cleanup functions for use with g_autoptr
crypto: use auto cleanup for many stack variables

 configure                   |  2 +-
 crypto/afsplit.c            | 28 ++++----------
 crypto/block-luks.c         | 74 +++++++++++--------------------------
 crypto/block.c              | 15 +++-----
 crypto/hmac-glib.c          |  5 ---
 crypto/pbkdf.c              |  5 +--
 crypto/secret.c             | 38 ++++++++-----------
 crypto/tlscredsanon.c       | 16 +++-----
 crypto/tlscredspsk.c        |  5 +--
 crypto/tlscredsx509.c       | 16 +++-----
 include/crypto/block.h      |  2 +
 include/crypto/cipher.h     |  2 +
 include/crypto/hmac.h       |  2 +
 include/crypto/ivgen.h      |  2 +
 include/crypto/tlssession.h |  2 +
 include/glib-compat.h       | 42 +--------------------
 16 files changed, 78 insertions(+), 178 deletions(-)
Signature · 1 line
2.21.0