The code generated for the simulator in Gui Guider stores `lv_disp_drv_t disp_drv` and `lv_indev_drv_t indev_drv` on the stack of `hal_init()` in lvgl-simulator/main.c leading to UB/segfault as `lv_disp_drv_register(&*_drv)` only copy the pointers.
./build/bin/simulator
=================================================================
==148306==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f40f2700098 at pc 0x55f3cd41ac5d bp 0x7ffdeb5ac6a0 sp 0x7ffdeb5ac690
READ of size 4 at 0x7f40f2700098 thread T0
#0 0x55f3cd41ac5c in lv_disp_get_hor_res /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../lvgl/src/hal/lv_hal_disp.c:349
#1 0x55f3cd388a7e in lv_obj_class_create_obj /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../lvgl/src/core/lv_obj_class.c:77
#2 0x55f3cd38400c in lv_obj_create /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../lvgl/src/core/lv_obj.c:207
#3 0x55f3cd453862 in setup_scr_coffeePour /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../generated/setup_scr_coffeePour.c:50
#4 0x55f3cd45376d in setup_ui /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../generated/gui_guider.c:28
#5 0x55f3cd45231e in main /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/main.c:68
#6 0x7f40f443984f (/usr/lib/libc.so.6+0x2384f) (BuildId: 2f005a79cd1a8e385972f5a102f16adba414d75e)
#7 0x7f40f4439909 in __libc_start_main (/usr/lib/libc.so.6+0x23909) (BuildId: 2f005a79cd1a8e385972f5a102f16adba414d75e)
#8 0x55f3cd3723c4 in _start (/home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/build/bin/simulator+0xf3c4) (BuildId: 49c53d2fc9c2b643291ed3888b09d53f081dc058)
Address 0x7f40f2700098 is located in stack of thread T0 at offset 152 in frame
#0 0x55f3cd45235d in hal_init /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/main.c:93
This frame has 2 object(s):
[32, 88) 'indev_drv' (line 114)
[128, 280) 'disp_drv' (line 103) <== Memory access at offset 152 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-return /home/ludovic/Documents/projects/gui_guider/test_non_static_drv/lvgl-simulator/../lvgl/src/hal/lv_hal_disp.c:349 in lv_disp_get_hor_res
They should be static instead:
diff --git a/lvgl-simulator/main.c b/lvgl-simulator/main.c
index eba894e..5257d81 100644
--- a/lvgl-simulator/main.c
+++ b/lvgl-simulator/main.c
@@ -100,7 +100,7 @@ static void hal_init(void)
lv_disp_draw_buf_init(&disp_buf1, buf1_1, NULL, 480 * 10);
/*Create a display*/
- lv_disp_drv_t disp_drv;
+ static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.draw_buf = &disp_buf1;
disp_drv.flush_cb = monitor_flush;
@@ -111,7 +111,7 @@ static void hal_init(void)
/* Add the mouse as input device
* Use the 'mouse' driver which reads the PC's mouse*/
mouse_init();
- lv_indev_drv_t indev_drv;
+ static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv); /*Basic initialization*/
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = mouse_read; /*This function will be called periodically (by the library) to get the mouse position and state*/
Hi @FederAndInk
Thank you very much for using GuiGuider. Your findings are beneficial. We will update the v8 project templates in the 1.6.0 release on 31 July. Additionally, The v8 board SDK template is the static variable.
After checking the lvgl document, it is found that the registration implementation of drv in v7 and v8 is different.
Best regards
Liubin
Hi @LiubinGong, thanks for your reply, I am happy to help. I am looking forward to the 1.6.0 release!