Subject: bug#58732: installer: finalizers & device destroy segfault

From: Mathieu Othacehe <[email-masked]>
Date: Thu, 03 Nov 2022 12:09:16 +0100

Hey,

Thanks for your help :)

>   1. Bindings create wrappers for C pointers—e.g., with
>      ‘pointer->device’.  If several C functions return a pointer P, you
>      must make sure to return always the same wrapper and not create a
>      new one.

Agreed.

>
>      ‘pointer->device!’ attempts to do that but I think it’s bogus: it
>      uses a weak-value hash table, where the value is the wrapper.  So
>      if the wrapper disappears before the underlying C object, then the
>      pointer is called and bad things ensue.

I'm not sure to understand how could the wrapper disappear before the
underlying C object? We are only exposing <device> records to the
Guile-Parted users so my assumption is that when <device> goes out of
scope, the pointer it wraps can be freed, but I'm maybe missing
something?

>      ‘define-wrapped-pointer-type’ in Guile is meant to help with these
>      things (info "(guile) Void Pointers and Byte Access").  We can’t
>      use it directly here because we’re using bytestructures and all
>      that.

Turns out, the "wrap" procedure defined in define-wrapped-pointer-type
is a clone of pointer->device! except that it doesn't set a
finalizer.

Regarding object lifetime, I wrote a small memo in 2019 here:
https://issues.guix.gnu.org/36402#11.

We have three weak hash tables in Guile-Parted:

%devices: To make sure that we do not set multiple finalizers on the
same pointers.

%disk-devices: So that a device always outlives its disks.

%partition-disks: So that a disk always outlives its partitions.

This means that as far as I can tell we are OK regarding your second
point about "aggregation relations".

Mathieu