* * * * *

                     I don't need no steeeenking debugger

I was still having troubles with lldb accepting breakpoints [1]; the
breakpoints were just not being hit. Thinking about the issue, it seemed to
me that the code might be requesting a too large amount of memory and an easy
eay to do that is to track all allocations made by Lua. It was easy enough to
test:

-----[ C ]-----
static void *l_alloc(void *ud,void *ptr,size_t osize,size_t nsize)
{
 (void)ud;

 fprintf(stderr,"osize=%zu nsize=%zu\n",osize,nsize);

 if (nsize == 0)
 {
   free(ptr);
   return NULL;
 }
 else
   return realloc(ptr,nsize);
}

int main(int argc,char *argv[])
{
 /* ... */

 L = lua_newstate(l_alloc,NULL);

 /* ... */
 return 0;
}
-----[ END OF LINE ]-----

Then it was a simple matter of running the program and check each allocation:

-----[ shell ]-----
...
osize=0 nsize=64
osize=0 nsize=40
osize=0 nsize=64
osize=0 nsize=0
osize=0 nsize=64
osize=0 nsize=16
osize=0 nsize=64
osize=0 nsize=0
osize=0 nsize=160
osize=0 nsize=148
osize=0 nsize=64
osize=0 nsize=16
osize=16 nsize=32
osize=0 nsize=16
osize=0 nsize=64
osize=0 nsize=18446744073709551600
...
-----[ END OF LINE ]-----

Hmmmmm [2] …

Okay, force a core dump we can examine:

-----[ C ]-----
static void *l_alloc(void *ud,void *ptr,size_t osize,size_t nsize)
{
 (void)ud;
 (void)osize;

 if (nsize > 10uL * 1024uL * 1024uL)
   abort(); /* dump core! */

 if (nsize == 0)
 {
   free(ptr);
   return NULL;
 }
 else
   return realloc(ptr,nsize);
}
-----[ END OF LINE ]-----

And the problem is immedately resolved.

The Linux version used epoll() [3] for events, while for whatever reason I
don't remember, the Mac OS-X version used select() [4] [Yes, I know that's a
Linux reference and not a Mac OS-X reference, but the call is the same
between the two systems, and I couldn't find a version of the Mac OS-X page
online. —Sean], and that code path was … a bit problematic.

The fix was easy—use poll() [5] for events. With that change, the code worked
fine on Mac OS-X (not that we use Mac OS-X in production, but it makes it
easy to test in development if it works on the Mac).

[1] gopher://gopher.conman.org/0Phlog:2018/08/21.1
[2] https://www.youtube.com/watch?v=gCxrkl2igGY&t=32s
[3] http://man7.org/linux/man-pages/man7/epoll.7.html
[4] http://man7.org/linux/man-pages/man2/select.2.html
[5] http://man7.org/linux/man-pages/man2/poll.2.html

Email author at [email protected]