From patchwork Wed Apr 6 22:43:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 1614176 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=YMynSy+S; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KYfhx38Y6z9sFs for ; Thu, 7 Apr 2022 08:44:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EDE583857C49 for ; Wed, 6 Apr 2022 22:44:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EDE583857C49 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649285083; bh=CVXyj7AHzu6oCtGk6ElYYpGpPoES/+3dAxvXI/7v/+o=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=YMynSy+SUb+316zkTAp6I8KUh/sVPxo1XdkwtBBxBYAazGJeYYUoFnSDaq72Q10JM FEp02+KR2lxOhqy5R/T0k6X8I1qxlFf68ZjWfpAgSFRftcKj4P6jPlzvgIwqkzPOaP geV84T8zROVDJB+dhaYwo/QkEo8CDvjuXLSAqyMc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by sourceware.org (Postfix) with ESMTPS id 6211E3858D37; Wed, 6 Apr 2022 22:44:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6211E3858D37 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5252361CB9; Wed, 6 Apr 2022 22:44:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03804C385A3; Wed, 6 Apr 2022 22:43:58 +0000 (UTC) Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 167f8ac3 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Wed, 6 Apr 2022 22:43:56 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH] toplev: use HOST_WIDE_INT for local_tick to prevent overflow Date: Thu, 7 Apr 2022 00:43:43 +0200 Message-Id: <20220406224343.80110-1-Jason@zx2c4.com> MIME-Version: 1.0 X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Jason A. Donenfeld via Gcc-patches" From: "Jason A. Donenfeld" Reply-To: "Jason A. Donenfeld" Cc: PaX Team , Andrew Pinski , Brad Spengler , Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" In gcc/toplev.c, there's the comment: /* A local time stamp derived from the time of compilation. It will be zero if the system cannot provide a time. It will be -1u, if the user has specified a particular random seed. */ unsigned local_tick; This is affirmed by init_local_tick()'s comment: /* Initialize local_tick with the time of day, or -1 if flag_random_seed is set. */ static void init_local_tick (void) And indeed we see it assigned -1 when flag_random_seed != NULL: else local_tick = -1; So far so good. However, in the case where flag_random_seed == NULL, local_tick is assigned like this: struct timeval tv; gettimeofday (&tv, NULL); local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000; local_tick is currently of type "unsigned int". Somewhat often, that expression calculates to be 0xffffffff, which means local_tick winds up being -1 even when flag_random_seed == NULL. Interestingly enough, Jakub already fixed one such overflow with that assignment with 3db31fd1cc7 ("toplev.c (init_local_tick): Avoid signed integer multiplication overflow."), but this patch missed the integer size issue. This is a problem for plugins that follow the documentation comments in order to determine whether -frandom-seed is being used. To work around this bug, these plugins must either call get_random_seed(noinit=true) in their plugin init functions and check there whether the return value is zero, or more laboriously reparse common_deferred_options or save_decoded_options. If they use a local_tick==-1 check, once in a blue moon, it'll be wrong. Actually, one such user of this isn't a plugin and is actually in tree: coverage.cc, which unlink()s a file that it shouldn't from time to time: if (!flag_branch_probabilities && flag_test_coverage && (!local_tick || local_tick == (unsigned)-1)) /* Only remove the da file, if we're emitting coverage code and cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */ unlink (da_file_name); This patch fixes the issue by just making local_tick 64 bits, which should also allow that timestamp to be a bit more unique as well. This way there's no possibility of overflowing to -1. It then changes the comparisons to use the properly typed HOST_WIDE_INT_M1U macro. Cc: PaX Team Cc: Brad Spengler Cc: Andrew Pinski Cc: Jakub Jelinek Closes: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105171 Fixes: c07e5477521 ("tree.h (default_flag_random_seed): Remove.") Signed-off-by: Jason A. Donenfeld --- gcc/coverage.cc | 4 ++-- gcc/toplev.cc | 10 +++++----- gcc/toplev.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gcc/coverage.cc b/gcc/coverage.cc index 8ece5db680e..aa482152b3b 100644 --- a/gcc/coverage.cc +++ b/gcc/coverage.cc @@ -1310,7 +1310,7 @@ coverage_init (const char *filename) memcpy (da_file_name + prefix_len, filename, len); strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX); - bbg_file_stamp = local_tick; + bbg_file_stamp = (unsigned) local_tick; if (flag_auto_profile) read_autofdo_file (); else if (flag_branch_probabilities) @@ -1360,7 +1360,7 @@ coverage_finish (void) unlink (bbg_file_name); if (!flag_branch_probabilities && flag_test_coverage - && (!local_tick || local_tick == (unsigned)-1)) + && (!local_tick || local_tick == HOST_WIDE_INT_M1U)) /* Only remove the da file, if we're emitting coverage code and cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */ unlink (da_file_name); diff --git a/gcc/toplev.cc b/gcc/toplev.cc index 2d432fb2d84..7c6badeb052 100644 --- a/gcc/toplev.cc +++ b/gcc/toplev.cc @@ -135,9 +135,9 @@ const char * current_function_func_begin_label; static const char *flag_random_seed; /* A local time stamp derived from the time of compilation. It will be - zero if the system cannot provide a time. It will be -1u, if the + zero if the system cannot provide a time. It will be -1, if the user has specified a particular random seed. */ -unsigned local_tick; +unsigned HOST_WIDE_INT local_tick; /* Random number for this compilation */ HOST_WIDE_INT random_seed; @@ -248,19 +248,19 @@ init_local_tick (void) struct timeval tv; gettimeofday (&tv, NULL); - local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000; + local_tick = (unsigned HOST_WIDE_INT) tv.tv_sec * 1000 + tv.tv_usec / 1000; } #else { time_t now = time (NULL); if (now != (time_t)-1) - local_tick = (unsigned) now; + local_tick = (unsigned HOST_WIDE_INT) now; } #endif } else - local_tick = -1; + local_tick = HOST_WIDE_INT_M1U; } /* Obtain the random_seed. Unless NOINIT, initialize it if diff --git a/gcc/toplev.h b/gcc/toplev.h index a82ef8b8fd3..4468396b725 100644 --- a/gcc/toplev.h +++ b/gcc/toplev.h @@ -74,7 +74,7 @@ extern void dump_profile_report (void); extern void target_reinit (void); /* A unique local time stamp, might be zero if none is available. */ -extern unsigned local_tick; +extern unsigned HOST_WIDE_INT local_tick; /* See toplev.cc. */ extern int flag_rerun_cse_after_global_opts;