FluidX3D icon indicating copy to clipboard operation
FluidX3D copied to clipboard

`Camera` move assignment doesn't swap the owning pointers

Open tirios opened this issue 3 months ago • 1 comments

Hi Moritz, thanks for FluidX3D, I've really enjoyed using it!

I've been getting crashes a couple of seconds after startup. Not every time, maybe half of them, and always at the same point - the info table prints, a few time steps run, then it dies with 0xC0000005. I assumed for a while it was something I'd done, then went back to a clean checkout of your master at ac470fe and got the same thing there.

What I did

Built that clean checkout with /fsanitize=address and ran it once. Trimmed error (full output is at the bottom):

WRITE of size 4 at 0x011d81b87684 thread T0
    #0 draw_pixel        src\graphics.cpp:249
    #1 draw_text         src\graphics.cpp:312
    #2 draw_label        src\graphics.cpp:317
    #3 main_label        src\main.cpp:67
    #4 main              src\graphics.cpp:836

0x011d81b87684 is located 7364228 bytes inside of 8294400-byte region

freed by thread T0 here:
    #0 operator delete[]
    #1 main              src\graphics.cpp:832

previously allocated by thread T0 here:
    #0 operator new[]
    #1 Camera::Camera    src\graphics.hpp:57
    #2 main              src\graphics.cpp:832

The allocation and the free are both attributed to graphics.cpp:832, which is the camera = Camera(...) line itself. And the block is 8,294,400 bytes, which is 1920 x 1080 x 4, so it's the bitmap?

Then I checked v3.7, which doesn't do this. Looking at what changed in between, 647a162 added the allocated_bitmap / allocated_zbuffer pair and pointed the destructor at them, but operator=(Camera&&) still only swaps bitmap and zbuffer.

camera = Camera(w, h, fps) hands the two arrays over to the global camera, but the temporary keeps the owning pointers and frees them when it goes out of scope at the end of that statement. So the global camera's bitmap and zbuffer are loose from that point on.

Nothing writes there straight away, which is why I'm guessing it doesn't break immediately. Then LBM::run sets allow_rendering just after initialize() and main_label() starts drawing the overlay into that freed block every 50 ms. That would explain the two second delay, and the intermittency too - the writes land in freed but still mapped heap, so whether it faults depends on what the allocator does with the block next.

What seemed to fix it

Adding the other two swaps:

--- a/src/graphics.hpp
+++ b/src/graphics.hpp
@@ -72,6 +72,8 @@ public:
 		this->fps_limit = camera.fps_limit;
 		std::swap(bitmap, camera.bitmap);
 		std::swap(zbuffer, camera.zbuffer);
+		std::swap(allocated_bitmap, camera.allocated_bitmap);
+		std::swap(allocated_zbuffer, camera.allocated_zbuffer);
 		set_zoom(1.0f); // set initial zoom
 		update_matrix();
 		return *this;

I'm not sure that's the shape you'd want, since you may have had a reason for keeping the two pairs separate. For what it's worth, allocated_bitmap and allocated_zbuffer only appear in four places in the tree, being their declaration, the constructor, the destructor and this operator, so as far as I can tell the move assignment is the only place ownership can change hands.

It does stop it. Same source, same compiler, same machine, launching repeatedly and counting how many survive 25 seconds:

build survived
ac470fe as is 5 / 12
ac470fe + the two swaps 12 / 12

RTX 4090, driver 610.47. The failures were all 0xC0000005 at 2.0 to 2.8 seconds. The same ASan build with the two lines added reported nothing and was still running when I stopped it after five minutes.

Reproducing the error

Clean checkout at ac470fe, then three toggles that all ship commented out:

  1. defines.hpp line 16: comment out #define BENCHMARK
  2. defines.hpp line 29: uncomment #define GRAPHICS
  3. setup.cpp lines 40 and 59: uncomment the 3D Taylor-Green vortices main_setup, which needs no external geometry

I built with MSVC 19.44.35228, /std:c++17 /O2 /EHsc /MD. Then either launch it a few times, or build once with /fsanitize=address and launch it once, which is quicker and comes out the same every time. The trace, the launch counts and the builds are all from my own runs, and they reproduce from the steps here.

Perhaps it could affect anything that constructs a Camera 🤔

I had a look through the tracker first and couldn't find this one already reported. #312 looked close, but it's a different defect: it quotes the older destructor from before 647a162, where the two pointers still agreed.

Hope that's useful. If you fix it your own way, happy to re-run the ASan build and the 12 launch test against it and report back.

Full AddressSanitizer output
=================================================================
==163436==ERROR: AddressSanitizer: heap-use-after-free on address 0x011d81b87684 at pc 0x7ff7266eaa7f bp 0x001a0f6fc860 sp 0x001a0f6fc868
WRITE of size 4 at 0x011d81b87684 thread T0
    #0 0x7ff7266eaa7e in draw_pixel src\graphics.cpp:249
    #1 0x7ff7266eaa7e in draw_text(int, int, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const &, int) src\graphics.cpp:312
    #2 0x7ff7266ea402 in draw_label(int, int, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const &, int) src\graphics.cpp:317
    #3 0x7ff7267e3dad in main_label(double) src\main.cpp:67
    #4 0x7ff7266ee7b5 in main src\graphics.cpp:836
    #5 0x7ff726815bdb in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #6 0x7ff726815bdb in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #7 0x7ffd0f8dccb6  (C:\Windows\System32\KERNEL32.DLL+0x18002ccb6)
    #8 0x7ffd10aaad6b  (C:\Windows\SYSTEM32\ntdll.dll+0x1800aad6b)

0x011d81b87684 is located 7364228 bytes inside of 8294400-byte region [0x011d81481800,0x011d81c6a800)
freed by thread T0 here:
    #0 0x7ff726814dde in operator delete[](void *) D:\a\_work\1\s\src\vctools\asan\llvm\compiler-rt\lib\asan\asan_win_delete_array_thunk.cpp:43
    #1 0x7ff7266ee6aa in main src\graphics.cpp:832
    #2 0x7ff726815bdb in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #3 0x7ff726815bdb in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #4 0x7ffd0f8dccb6  (C:\Windows\System32\KERNEL32.DLL+0x18002ccb6)
    #5 0x7ffd10aaad6b  (C:\Windows\SYSTEM32\ntdll.dll+0x1800aad6b)

previously allocated by thread T0 here:
    #0 0x7ff726814d2e in operator new[](unsigned __int64) D:\a\_work\1\s\src\vctools\asan\llvm\compiler-rt\lib\asan\asan_win_new_array_thunk.cpp:41
    #1 0x7ff7266e4caa in Camera::Camera(unsigned int, unsigned int, unsigned int) src\graphics.hpp:57
    #2 0x7ff7266ee66f in main src\graphics.cpp:832
    #3 0x7ff726815bdb in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
    #4 0x7ff726815bdb in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #5 0x7ffd0f8dccb6  (C:\Windows\System32\KERNEL32.DLL+0x18002ccb6)
    #6 0x7ffd10aaad6b  (C:\Windows\SYSTEM32\ntdll.dll+0x1800aad6b)

SUMMARY: AddressSanitizer: heap-use-after-free src\graphics.cpp:249 in draw_pixel
Shadow bytes around the buggy address:
  0x011d81b87400: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87480: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87500: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87580: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87600: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
=>0x011d81b87680:[fd]fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87700: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87780: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87800: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87880: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x011d81b87900: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==163436==ABORTING

tirios avatar Aug 29 '26 00:08 tirios

Hi @tirios,

good catch, that was indeed a bug. I've applied your proposed fix - https://github.com/ProjectPhysX/FluidX3D/commit/aba941305a2cc67b0953ba1d2ba177b590dcccc3 Thanks!!

Kind regards, Moritz

ProjectPhysX avatar Aug 29 '26 05:08 ProjectPhysX