I need help with debugging : Stack overflow or misalignment lazarus / 21-General ---------------------------------------------------------------------- AL · 2022-10-27 20:49 I have been struggling for several days on this bug and cannot find a solution.  I am a "Sunday programmer" and I am running out of ideas.  I hope a more experienced person might see what I do not see so far and give me some hints.  Here is the background: I made an App 2 years ago in Windows that record the microphone, do some FFT and present the result in graph with TaChart.  I have ported it to MacOs  Mojave using VMWare.  I have some users using Catalina and the App is working fine too. So far so good everything is fine.  Recently someone tried to run it on BigSur (MacOs 11) and the App crash frequently.  I installed MacOS 12 (Monterey) on my VM and confirmed the the App crash. I use AudioQueue to capture the microphone. I ran some tests and found that the App always crash when it call AudioQueueStart.  Although all AudioQueue functions used before Start return no error.  AudioQueueStart also return no error (The Terminal NSlog just show that).   I suspected the CallBack procedure thinking the procedure call method could have changed with the change of version.  I then tried a very simple program with one form, one button to call the AudioQueue functions and Start and this is working as it should without error. The error is Stack Overflow, and I am thinking the stack size is distributed among all threads reducing the stack size of each individual threads (correct me if wrong),  I do not use threads in my App, but AudioQueue do, and the system also.  In the Threads View of Debug I see the are 13 threads running when it crash.  Could it be the reason and how to solve that?   I cannot step through the code with the debugger as the AudioQueue is part of the OS.  I have put together all Debug Output that I could in a PDF This is on MacOS 12 Monterey running on VMWare 15 on Windows 10. Lazarus 2.2.5 with FPC 3.3.1 (recent download) lldb debugger with fpdebug. Any idea, hint, solution Welcome Thanks ---------------------------------------------------------------------- Martin_fr · 2022-10-27 21:29 Not much to be gotten from the info.... I checked in the fpc source: FPC_STACKCHECK can (depending on OS) check for 1 or 2 things: - overflow (i.e. to much stack used) - misalignment (the stack ptr is not at a 16 byte boundary at the time of calling a function) The latter is only required by some OS.... Looking at the stack, just 17 frames from _pthread_start  => does not look like an overflow. But the stack doesn't give the exact caller. Random guess (I mean it totally random guess, could be totally unrelated) If (and I don't know for sure) it is a misalignment: "ClientMessageHandler::InputBufferComplete" your code or code from some library. If this is a binary distributed lib, maybe it does not adhere to the expectation of FPC? Stack checking should be optional, not sure which setting triggers this. But (again) a guess: FPC_STACKCHECK  may be triggered by   -Ct  ? So (if that is used) what happens without this? ---------------------------------------------------------------------- AL · 2022-10-28 01:23 Thank you Martin for looking at this. Misalignment: This is what I suspected but could not verify.  We supply AudioQueue with the ADDRESS of a CallBack procedure.  When a buffer is full, AudioQueue call this procedure(Address) so we can read the data in the buffer.  The calling is declared as MWPASCAL in the Lazarus cocoa interface to MacOS.  So I had to declare the same in my program. If I remove the stack Check -Ct  then the CallBack is executed and the program run normally.  But I was afraid this could cause problem later in execution.   This is why I would prefer to resolve this if possible. ---------------------------------------------------------------------- SymbolicFrank · 2022-10-28 06:23 So, you're running a Windows executable in a VM on MacOS? ---------------------------------------------------------------------- Martin_fr · 2022-10-28 10:48 Quote from: AL on October 28, 2022, 03:23:33 amIf I remove the stack Check -Ct  then the CallBack is executed and the program run normally.  But I was afraid this could cause problem later in execution.   This is why I would prefer to resolve this if possible. Well I don't know what issues (if any) misalignment can/will cause.  - As far as the stackpointer goes the CPU (intel) afaik doesn't care (other than maybe for speed), or if it cares then for a lesser align. - Dont know if, but I guess the OS somehow requires it. Not sure though when/how. - Also, fpc may access data with special 16bit registers, and they may require it. If that happens fpc relies that the align did not get broken, because afaik it only keeps it by always adding the correct amount. That may depend on optimization level, or even compiling for a minimum supported CPU  (i.e. core-i and up or something) However if the library is pre-compiled, then you can't change it. Maybe someone else has an idea. QuoteThis is what I suspected but could not verifySet a breakpoint on the very first line of the callback. Even better, view the asm (on the first pause in the routing), and in the asm make sure the break is really at the first instruction. Then watch the registers. ESP or RSP. Compare it to other code, that is not called by the lib. Afaik it expects 16 byte align. But I don't know if that include/excludes the return address that the  caller has pushed on the stack. ---------------------------------------------------------------------- AL · 2022-10-28 12:22 OK I will investigate that later Quote from: SymbolicFrank on October 28, 2022, 08:23:04 am Just to complete my previous answer, the callback was executed when I removed the Ct option, but that was in debug mode with no optimization and debug info generated. So far in release mode with the default setting it was crashing.  I will make more checks on this.  Possibly removing the optimization could make it run  in release mode? ---------------------------------------------------------------------- AL · 2022-10-28 12:24 Quote from: SymbolicFrank on October 28, 2022, 08:23:04 amSo, you're running a Windows executable in a VM on MacOS? No , opposite.  I am running a MacOS executable in a VMWare on Windows 10