xorg-libx11/include/X11
Keith Packard 30ccef3a48 Avoid recursing through _XError due to sequence adjustment
This patch is based on research done by Dmitry Osipenko to uncover the
cause of a large class of Xlib lockups.

_XError must unlock and re-lock the display around the call to the
user error handler function. When re-locking the display, two
functions are called to ensure that the display is ready to generate a request:

    _XIDHandler(dpy);
    _XSeqSyncFunction(dpy);

The first ensures that there is at least one XID available to use
(possibly calling _xcb_generate_id to do so). The second makes sure a
reply is received at least every 65535 requests to keep sequence
numbers in sync (possibly generating a GetInputFocus request and
synchronously awaiting the reply).

If the second of these does generate a GetInputFocus request and wait
for the reply, then a pending error will cause recursion into _XError,
which deadlocks the display.

One seemingly easy fix is to have _XError avoid those calls by
invoking InternalLockDisplay instead of LockDisplay. That function
does everything that LockDisplay does *except* call those final two
functions which may end up receiving an error.

However, that doesn't protect the system from applications which call
some legal Xlib function from within their error handler. Any Xlib
function which cannot generate protocol or wait for events is valid,
including many which invoke LockDisplay.

What we need to do is make LockDisplay skip these two function calls
precisely when it is called from within the _XError context for the
same display.

This patch accomplishes this by creating a list of threads in the
display which are in _XError, and then having LockDisplay check the
current thread against those list elements.

Inspired-by: Dmitry Osipenko <digetx@gmail.com>
Signed-off-by: Keith Packard <keithp@keithp.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Dmitry Osipenko <digetx@gmail.com>
2020-11-15 10:09:16 -08:00
..
extensions xkb: Provide <X11/extensions/XKBgeom.h> ourselves 2019-09-24 13:02:47 -04:00
.gitignore .gitignore: use common defaults with custom section # 24239 2009-11-14 09:26:16 -05:00
cursorfont.h Add an "X11_" string to header guards to avoid possible collision 2010-09-26 21:21:16 -07:00
ImUtil.h Avoid the use of "register" keyword in public headers. 2020-08-28 20:28:18 +00:00
Xcms.h Tell clang not to report -Wpadded warnings on public headers we can't fix 2013-01-09 20:50:32 -08:00
XKBlib.h Avoid the use of "register" keyword in XkbTranslateKeySym. 2020-08-15 00:48:56 +03:00
Xlib-xcb.h Add an "X11_" string to header guards to avoid possible collision 2010-09-26 21:21:16 -07:00
Xlib.h Add XSetIOErrorExitHandler() function 2020-10-15 11:14:53 +02:00
XlibConf.h.in nuke RCS Ids 2008-03-09 09:08:07 +01:00
Xlibint.h Avoid recursing through _XError due to sequence adjustment 2020-11-15 10:09:16 -08:00
Xlocale.h Drop X_LOCALE fallback for OS'es without setlocale() 2013-11-22 22:02:17 -08:00
Xregion.h Avoid memory leak/corruption if realloc fails in Xregion.h:MEMCHECK macro 2013-08-20 12:51:04 -07:00
Xresource.h Add an "X11_" string to header guards to avoid possible collision 2010-09-26 21:21:16 -07:00
Xutil.h Tell clang not to report -Wpadded warnings on public headers we can't fix 2013-01-09 20:50:32 -08:00