qom: assert integer does not overflow
PATCH 1/1 qom: assert integer does not overflow
QOM reference counting is not designed with an infinite amount of references in mind, trying to take a reference in a loop will overflow the integer. We will then eventually assert when dereferencing, but the real problem is in object_ref so let's assert there to make such issues cleaner and easier to debug.
Some micro-benchmarking shows using fetch and add this is essentially free on x86.
--- qom/object.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
qom/object.c +5 -1
@@ -1167,10 +1167,14 @@ GSList *object_class_get_list_sorted(const char *implements_type,Object *object_ref (void *objptr) {Object *obj =OBJECT (objptr); +uint32_t ref; +if (!obj) {return NULL ; } -qatomic_inc (&obj->ref); + ref =qatomic_fetch_inc (&obj->ref); +/* Assert waaay before the integer overflows */ +g_assert (ref <INT_MAX );return obj; }
Signature · 1 line
MST
Quoted 5 lines · Michael S. Tsirkin
QOM reference counting is not designed with an infinite amount of references in mind, trying to take a reference in a loop will overflow the integer. We will then eventually assert when dereferencing, but the real problem is in
object_refso let's assert there to make such issues cleaner and easier to debug.
What is the actual bug / scenario that led you to hit this problem ?
I'm surprised you saw an assert in object_unref, as that would imply you had exactly UINT32_MAX calls to object_ref and then one to object_unref.
Quoted 15 lines
Some micro-benchmarking shows using fetch and add this is essentially free on x86.
Signed-off-by Michael S. Tsirkin <[email protected]>--- qom/object.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)qom/object.c +0
-0@@ -1167,10 +1167,14 @@ GSList *object_class_get_list_sorted(const char *implements_type,Object *object_ref (void *objptr) {
qom/object.c GSList *object_class_get_list_sorted(const char *implements_type, · line 1177
Quoted 10 lines
qom/object.c +5
-1Object *obj =OBJECT (objptr); +uint32_t ref; +if (!obj) {return NULL ; } -qatomic_inc (&obj->ref); + ref =qatomic_fetch_inc (&obj->ref); +/* Assert waaay before the integer overflows */ +g_assert (ref <INT_MAX );
Not that I expect this to hit, but why choose this lower bound instead of g_assert(ref > 0) which is the actual failure scenario, matching the existing object_unref assert.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 8 lines · Daniel P. Berrangé
Quoted 5 lines · Michael S. Tsirkin
QOM reference counting is not designed with an infinite amount of references in mind, trying to take a reference in a loop will overflow the integer. We will then eventually assert when dereferencing, but the real problem is in
object_refso let's assert there to make such issues cleaner and easier to debug.What is the actual bug / scenario that led you to hit this problem ?
E.g. if during code development I call object_ref but not object_unref, the counter eventually overflows. If this triggers in an error flow and not a good path this kind of bug might thinkably make it through QE into release code.
Quoted 3 lines
I'm surprised you saw an assert in
object_unref, as that would imply you had exactlyUINT32_MAXcalls toobject_refand then one toobject_unref.
Any imbalance with # of unrefs > # refs
will trigger an existing assert in unref.
However, an imbalance with # of refs > # unrefs does not trigger an assert at the moment.
Quoted 22 lines
Quoted 22 lines
Some micro-benchmarking shows using fetch and add this is essentially free on x86.
Signed-off-by Michael S. Tsirkin <[email protected]>--- qom/object.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)qom/object.c +2
-1@@ -1167,10 +1167,14 @@ GSList *object_class_get_list_sorted(const char *implements_type,Object *object_ref (void *objptr) {Object *obj =OBJECT (objptr); +uint32_t ref; +if (!obj) {return NULL ; } -qatomic_inc (&obj->ref);
Quoted 8 lines
qom/object.c GSList *object_class_get_list_sorted(const char *implements_type, · line 1177
Quoted 3 lines
qom/object.c +3
-0+ ref =qatomic_fetch_inc (&obj->ref); +/* Assert waaay before the integer overflows */ +g_assert (ref <INT_MAX );Not that I expect this to hit, but why choose this lower bound instead of
g_assert(ref > 0)which is the actual failure scenario, matching the existingobject_unrefassert.
The earlier we catch it the better, if we overflowed to 0 some other thread might already be confused.
Quoted 6 lines
Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 19 lines · Michael S. Tsirkin
Quoted 8 lines · Daniel P. Berrangé
Quoted 5 lines · Michael S. Tsirkin
QOM reference counting is not designed with an infinite amount of references in mind, trying to take a reference in a loop will overflow the integer. We will then eventually assert when dereferencing, but the real problem is in
object_refso let's assert there to make such issues cleaner and easier to debug.What is the actual bug / scenario that led you to hit this problem ?
E.g. if during code development I call
object_refbut notobject_unref, the counter eventually overflows. If this triggers in an error flow and not a good path this kind of bug might thinkably make it through QE into release code.Quoted 3 lines
I'm surprised you saw an assert in
object_unref, as that would imply you had exactlyUINT32_MAXcalls toobject_refand then one toobject_unref.
Quoted 6 lines
Any imbalance with # of unrefs > # refs
will trigger an existing assert in unref.However, an imbalance with # of refs > # unrefs does not trigger an assert at the moment.
A vsock patch Stefano just posted would be one example where this can happen.
Quoted 41 lines
Quoted 22 lines
Quoted 22 lines
Some micro-benchmarking shows using fetch and add this is essentially free on x86.
Signed-off-by Michael S. Tsirkin <[email protected]>--- qom/object.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)qom/object.c +2
-1@@ -1167,10 +1167,14 @@ GSList *object_class_get_list_sorted(const char *implements_type,Object *object_ref (void *objptr) {Object *obj =OBJECT (objptr); +uint32_t ref; +if (!obj) {return NULL ; } -qatomic_inc (&obj->ref);Quoted 8 lines
qom/object.c GSList *object_class_get_list_sorted(const char *implements_type, · line 1177
Quoted 3 lines
qom/object.c +3
-0+ ref =qatomic_fetch_inc (&obj->ref); +/* Assert waaay before the integer overflows */ +g_assert (ref <INT_MAX );Not that I expect this to hit, but why choose this lower bound instead of
g_assert(ref > 0)which is the actual failure scenario, matching the existingobject_unrefassert.The earlier we catch it the better, if we overflowed to 0 some other thread might already be confused.
Quoted 6 lines
Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|