[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using asserts from other threads


I'm working on writing unit tests for Unicorn
[https://github.com/unicorn-engine/unicorn].

One peculiarity about the software is that, because QEMU runs each
virtual CPU in its own thread, all Unicorn callbacks actually come in
on a different thread than the one making calls to the library.

This poses a problem for writing unit tests with cmocka. If I have a
test assertion in a callback function, and it fails, cmocka will
simply exit with a status of 255. No output, no test continuation,
nothing.  This happens because _fail() calls exit_test(1), and
global_running_test is a thread-local variable, and the tests are
running on a different thread.  (It's a good thing it is thread-local;
otherwise cmocka would try to longjmp back to the wrong thread stack!)

/* Exit the currently executing test. */
static void exit_test(const int quit_application)
{
    const char *abort_test = getenv("CMOCKA_TEST_ABORT");

    if (abort_test != NULL && abort_test[0] == '1') {
        print_error("%s", cm_error_message);
        abort();
    } else if (global_running_test) {
        cm_longjmp(global_run_test_env, 1);
    } else if (quit_application) {
        exit(-1);
    }
}

I understand why the library has no choice here but to exit. I don't
however, understand why I'm not seeing a failure message either.

Is there any straightforward way I can make cmocka act as one would
expect, despite the unfortunate cross-thread situation?

Best regards,
Jonathon Reinhart