[libcamera-devel] [PATCH/RFC] libcamera: bound_method: Please gcc undefined behaviour sanitizer

Laurent Pinchart laurent.pinchart at ideasonboard.com
Tue Apr 13 00:59:48 CEST 2021


Enabling the gcc undefined behaviour sanitizer (with the meson configure
-Db_sanitize=undefined option) causes many tests to fail, with errors
such as the following (for test/object-invoke):

../../include/libcamera/bound_method.h:198:27: runtime error: member access within address 0x55fcd7bfbd38 which does not point to an object of type 'BoundMethodBase'
0x55fcd7bfbd38: note: object has invalid vptr
 fc 55 00 00  2a 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  31 00 00 00 00 00 00 00  4b c6 72 88
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
../../include/libcamera/bound_method.h:198:41: runtime error: member call on null pointer of type 'struct InvokedObject'
../../include/libcamera/bound_method.h:198:41: runtime error: member access within null pointer of type 'struct InvokedObject'
Segmentation fault

The root cause isn't clear, but this change fixes the issue. It may be a
bug in gcc.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 include/libcamera/bound_method.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

If anyone is interested in trying the gcc undefined behaviour sanitizer,
this patch is needed. Bonus points if you can spot why it helps.

diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h
index f216e3b56826..de17fdee3612 100644
--- a/include/libcamera/bound_method.h
+++ b/include/libcamera/bound_method.h
@@ -163,7 +163,8 @@ public:
 
 	R invoke(Args... args) override
 	{
-		return (static_cast<T *>(this->obj_)->*func_)(args...);
+		T *obj = static_cast<T *>(this->obj_);
+		return (obj->*func_)(args...);
 	}
 
 private:
@@ -195,7 +196,8 @@ public:
 
 	void invoke(Args... args) override
 	{
-		(static_cast<T *>(this->obj_)->*func_)(args...);
+		T *obj = static_cast<T *>(this->obj_);
+		(obj->*func_)(args...);
 	}
 
 private:
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list