From: Daniel Vetter Subject: [PATCH] lib/igt_core: document the caveats of magic code blocks Date: Fri, 14 Mar 2014 08:18:08 +0100 Message-ID: <1394781488-20586-1-git-send-email-daniel.vetter@ffwll.ch> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail-ee0-f54.google.com (mail-ee0-f54.google.com [74.125.83.54]) by gabe.freedesktop.org (Postfix) with ESMTP id BD593FA8BE for ; Fri, 14 Mar 2014 00:18:18 -0700 (PDT) Received: by mail-ee0-f54.google.com with SMTP id d49so979154eek.41 for ; Fri, 14 Mar 2014 00:18:16 -0700 (PDT) List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: intel-gfx-bounces@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org To: Intel Graphics Development Cc: Daniel Vetter List-Id: intel-gfx@lists.freedesktop.org Signed-off-by: Daniel Vetter --- lib/igt_core.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/igt_core.c b/lib/igt_core.c index ff4711785654..3e2350e9982a 100644 --- a/lib/igt_core.c +++ b/lib/igt_core.c @@ -83,6 +83,38 @@ * To allow this i-g-t provides #igt_fixture code blocks for setup code outside * of subtests and automatically skips the subtest code blocks themselves. For * special cases igt_only_list_subtests() is also provided. + * + * # Magic Control Blocks + * + * i-g-t makes heavy use of C macros which serve as magic control blocks. They + * work fairly well and transparently but since C doesn't have full-blown + * closures there are caveats: + * + * - Asynchronous blocks which are used to spawn children internally use fork(). + * Which means that impossible control flow like jumping out of the control + * block is possible, but it will horribly badly the i-g-t library code. And + * of course all caveats of a real fork() call apply, namely that file + * descriptors are copied, but still point at the original file. This will + * terminally upset the libdrm buffer manager if both parent and child keep on + * using the same open instance of the drm device. Usually everything related + * to interacting with the kernel driver must be reinitialized to avoid such + * issues. + * + * - Code blocks with magic control flow are implemented with setjmp() and + * longjmp(). This applies to #igt_fixture and #igt_subtest blocks and all the + * three variants to finish test: igt_success(), igt_skip() and igt_fail(). + * Mostly this is of no concern, except when such a control block changes + * stack variables defined in the same function as the control block resides. + * Any store/load behaviour after a longjmp() is ill-defined for these + * variables. Avoid such code. + * + * Quoting the man page for longjmp(): + * + * "The values of automatic variables are unspecified after a call to + * longjmp() if they meet all the following criteria:" + * - "they are local to the function that made the corresponding setjmp() call; + * - "their values are changed between the calls to setjmp() and longjmp(); and + * - "they are not declared as volatile." */ static unsigned int exit_handler_count; -- 1.8.5.2