From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DB91C10DCE for ; Fri, 13 Mar 2020 23:13:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2B1DD2074C for ; Fri, 13 Mar 2020 23:13:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ClF8fqoB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727602AbgCMXNC (ORCPT ); Fri, 13 Mar 2020 19:13:02 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:43475 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726837AbgCMXM6 (ORCPT ); Fri, 13 Mar 2020 19:12:58 -0400 Received: by mail-pg1-f195.google.com with SMTP id u12so5863766pgb.10 for ; Fri, 13 Mar 2020 16:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jVB9ateUNPWjDSRVGMb+4rgbxEOqdavmk19dJVvZMIM=; b=ClF8fqoBumZSnRpv0Utqo38yv0uvnElq5GseaaNLk/d0T0DTwHAZdaKo5jwGvqJDyU RoNsMHO1KDZdq+giL7q3wYKY9HUW0QO045PUqWadbPDAOdsYSOrHjG1QQg/cYr4ufbU4 JnTVX7waraMyka1dAVcE/afVQf2yXqlDROpgE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jVB9ateUNPWjDSRVGMb+4rgbxEOqdavmk19dJVvZMIM=; b=og5YtDxS3za4os+4SFcAB7+cvA+wtToA/N/7vI7IpjReWjvXA309TxmpgqyaP6Tcib R990R6RzPCiz0xwYnR0RiiNigo//jKgiks/nHGPZ+cLVY3GJ5KTSeeVmk5GvWke+AEKA zmHIWmCW4BjBqZQAW8sehhf1e2oO8pb9k8C9QbTyIj8Kks/Mj+ZVOss0X6BIroQXJPrx fht1WXDdbFtb/ZxHuE9kqNwW3IJqs8662WXCUgMY+rFWTXCp31JFAPj3BcWix7u2xotm i30dOiDmtnascdj1A0deroR1xlfjBFBoxPpzUylJvjxbIXA5+FMHEP653AB4q2bjWRuv dR4w== X-Gm-Message-State: ANhLgQ1WUzrzKubOl5ErMgrLtomO39fp0GVfxItAzpR6d4+mzxgSgtda LI4JMlez6BI42WyIVfI72kAU3C/mXCw= X-Google-Smtp-Source: ADFU+vuW5MY2BTvDr6DsWnU09ruFOrR5LSI5+g6PygmDmDlEm+j/U5jO/QNzQgn9N62SKP200VkfvA== X-Received: by 2002:a63:445:: with SMTP id 66mr14736826pge.351.1584141177002; Fri, 13 Mar 2020 16:12:57 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id p21sm58790400pfn.103.2020.03.13.16.12.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Mar 2020 16:12:55 -0700 (PDT) From: Kees Cook To: Shuah Khan Cc: Kees Cook , Andy Lutomirski , Will Drewry , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] selftests/harness: Handle timeouts cleanly Date: Fri, 13 Mar 2020 16:12:52 -0700 Message-Id: <20200313231252.64999-3-keescook@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200313231252.64999-1-keescook@chromium.org> References: <20200313231252.64999-1-keescook@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a selftest would timeout before, the program would just fall over and no accounting of failures would be reported (i.e. it would result in an incomplete TAP report). Instead, add an explicit SIGALRM handler to cleanly catch and report the timeout. Before: [==========] Running 2 tests from 2 test cases. [ RUN ] timeout.finish [ OK ] timeout.finish [ RUN ] timeout.too_long Alarm clock After: [==========] Running 2 tests from 2 test cases. [ RUN ] timeout.finish [ OK ] timeout.finish [ RUN ] timeout.too_long timeout.too_long: Test terminated by timeout [ FAIL ] timeout.too_long [==========] 1 / 2 tests passed. [ FAILED ] Signed-off-by: Kees Cook --- tools/testing/selftests/kselftest_harness.h | 53 ++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h index c7b67e379219..2902f6a78f8a 100644 --- a/tools/testing/selftests/kselftest_harness.h +++ b/tools/testing/selftests/kselftest_harness.h @@ -639,7 +639,8 @@ struct __test_metadata { int termsig; int passed; int trigger; /* extra handler after the evaluation */ - int timeout; + int timeout; /* seconds to wait for test timeout */ + bool timed_out; /* did this test timeout instead of exiting? */ __u8 step; bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ struct __test_metadata *prev, *next; @@ -696,15 +697,63 @@ static inline int __bail(int for_realz, bool no_print, __u8 step) return 0; } +struct __test_metadata *__active_test; +static void __timeout_handler(int sig, siginfo_t *info, void *ucontext) +{ + struct __test_metadata *t = __active_test; + + /* Sanity check handler execution environment. */ + if (!t) { + fprintf(TH_LOG_STREAM, + "no active test in SIGARLM handler!?\n"); + abort(); + } + if (sig != SIGALRM || sig != info->si_signo) { + fprintf(TH_LOG_STREAM, + "%s: SIGALRM handler caught signal %d!?\n", + t->name, sig != SIGALRM ? sig : info->si_signo); + abort(); + } + + t->timed_out = true; + kill(t->pid, SIGKILL); +} + void __wait_for_test(struct __test_metadata *t) { + struct sigaction action = { + .sa_sigaction = __timeout_handler, + .sa_flags = SA_SIGINFO, + }; + struct sigaction saved_action; int status; + if (sigaction(SIGALRM, &action, &saved_action)) { + t->passed = 0; + fprintf(TH_LOG_STREAM, + "%s: unable to install SIGARLM handler\n", + t->name); + return; + } + __active_test = t; + t->timed_out = false; alarm(t->timeout); waitpid(t->pid, &status, 0); alarm(0); + if (sigaction(SIGALRM, &saved_action, NULL)) { + t->passed = 0; + fprintf(TH_LOG_STREAM, + "%s: unable to uninstall SIGARLM handler\n", + t->name); + return; + } + __active_test = NULL; - if (WIFEXITED(status)) { + if (t->timed_out) { + t->passed = 0; + fprintf(TH_LOG_STREAM, + "%s: Test terminated by timeout\n", t->name); + } else if (WIFEXITED(status)) { t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0; if (t->termsig != -1) { fprintf(TH_LOG_STREAM, -- 2.20.1