[Xcb] Invalid XIDs

Barton C Massey bart at cs.pdx.edu
Sun Jan 25 15:32:27 PST 2009


In message <961fa2b40901242247i4071c5b6va73d02e0523bd35d at mail.gmail.com> you wrote:
> Checking X.org, it looks like the exact rules are:
>   if there is at least one XID available:
>     range->start_id is the first available xid
>     range->start_id + count is the first *un*available xid
>   if there are no XIDs available:
>     range->start_id is 0
>     range->count is 1 (??)
> This latter part is arguably a bug in the X server or
> something, but I don't see how it would ever matter, and
> anyway, yes, count is always at least 1.

Ow.  OK, we need code for that latter case.  Good catch.
It's unlikely this is what happened to you, though, as the
start_id was nowhere near 0 when you crashed.

BTW, where did you find this information?  I can't see it
anywhere.  What Xlib does is weird, as one might predict.

  if (dpy->xcmisc_opcode > 0) {
      GetReq(XCMiscGetXIDRange, greq);
      greq->reqType = dpy->xcmisc_opcode;
      greq->miscReqType = X_XCMiscGetXIDRange;
      if (_XReply (dpy, (xReply *)&grep, 0, xTrue) && grep.count) {
	  dpy->resource_id = ((grep.start_id - dpy->resource_base) >>
			      dpy->resource_shift);
	  dpy->resource_max = dpy->resource_id;
	  if (grep.count > 5)
	      dpy->resource_max += grep.count - 6;
	  dpy->resource_max <<= dpy->resource_shift;
      }
  }

It looks like Xlib thinks count comes back 0 on failure, but
then there's the martian line:

  if (grep.count > 5)
      dpy->resource_max += grep.count - 6;

I have no idea what *that* is all about.  Why 5 or 6?

This code also brings up another issue.  While ubiquitous,
technically XC_MISC is an extension, so we should be
checking for its existence before trying to use it, I think.

BTW, a really cool implementation would fall back to
XCMiscGetXIDList when XCMiscGetXIDRange failed, on the
grounds that one might be able to reuse some old XIDs that
way.  I haven't bothered, but maybe I should?

> That definitely looks more correct (whether it fixed my
> bug or not :-)).  I think the actual invariants are
> perhaps clearer with the asserts and tests arranged like
> this?:

I actually wanted the assertions inside the if so that they
wouldn't be checked so often, and so that if they were
removed the code would still do something vaguely sensible
if xid.last got too big.  Not a big deal.

> Anyway, since the crash is so rare (occurs maybe once a
> week, unpredictably), it's very hard to tell whether a
> patch works or not by trying it.  ("It's been a week
> without a crash... so did we fix it or just get lucky?").

I know.

> So I'm going to instrument the range resetting code
> instead, and the next time the crash happens we'll know
> for sure whether it was proceeded by a count==1 reply.

Just stick an assertion for count > 1 in the new code.  If
you ever hit it we will know what happened, and in the
meantime you can be testing code we care about.

OK, attached is what I currently have.  I put my assertions
back :-), tightened the check a little bit, took into
account the no-allocation case, fixed the initialization so
that we don't drop the first XID on the ground, and added a
check for XC_MISC.  I compiled and ran against this, and it
doesn't obviously blow up right away. :-)

Give it a try, and definitely drop us a line with any new
failure information!

Thanks much.

     Bart

-------------- next part --------------
/* Copyright (C) 2001-2008 Bart Massey and Jamey Sharp.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the names of the authors or their
 * institutions shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization from the authors.
 */

/* XID allocators. */

#include <assert.h>
#include <stdlib.h>
#include "xcb.h"
#include "xcbext.h"
#include "xcbint.h"
#include "xc_misc.h"

/* Public interface */

uint32_t xcb_generate_id(xcb_connection_t *c)
{
    uint32_t ret;
    if(c->has_error)
        return -1;
    pthread_mutex_lock(&c->xid.lock);
    if(c->xid.last >= c->xid.max - c->xid.inc + 1)
    {
        xcb_xc_misc_get_xid_range_reply_t *range;
        assert(c->xid.last == c->xid.max);
        if (c->xid.last == 0) {
            /* finish setting up initial range */
            c->xid.max = c->setup->resource_id_mask;
        } else {
            /* check for extension */
            const xcb_query_extension_reply_t *xc_misc_reply =
              xcb_get_extension_data(c, &xcb_xc_misc_id);
            if (!xc_misc_reply) {
                pthread_mutex_unlock(&c->xid.lock);
                return -1;
            }
            /* get new range */
            range = xcb_xc_misc_get_xid_range_reply(c,
                      xcb_xc_misc_get_xid_range(c), 0);
            /* XXX The latter disjunct is what the server returns
               when it is out of XIDs.  Sweet. */
            if(!range || (range->start_id == 0 && range->count == 1))
            {
                pthread_mutex_unlock(&c->xid.lock);
                return -1;
            }
            assert(range->count > 0 && range->start_id > 0);
            c->xid.last = range->start_id;
            c->xid.max = range->start_id + (range->count - 1) * c->xid.inc;
            free(range);
        }
    } else {
        c->xid.last += c->xid.inc;
    }
    ret = c->xid.last | c->xid.base;
    pthread_mutex_unlock(&c->xid.lock);
    return ret;
}

/* Private interface */

int _xcb_xid_init(xcb_connection_t *c)
{
    if(pthread_mutex_init(&c->xid.lock, 0))
        return 0;
    c->xid.last = 0;
    c->xid.max = 0;
    c->xid.base = c->setup->resource_id_base;
    c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask);
    return 1;
}

void _xcb_xid_destroy(xcb_connection_t *c)
{
    pthread_mutex_destroy(&c->xid.lock);
}


More information about the Xcb mailing list