From patchwork Tue Dec 20 13:46:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?SsOpcsO0bWUgUG91aWxsZXI=?= X-Patchwork-Id: 707435 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tjfGs4Bqcz9t0m for ; Wed, 21 Dec 2016 00:47:17 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 8DB0830041; Tue, 20 Dec 2016 13:47:15 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yAdNxDixq8Yb; Tue, 20 Dec 2016 13:47:08 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id 29CAE2D1B4; Tue, 20 Dec 2016 13:47:02 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id 1AB6C1BFA57 for ; Tue, 20 Dec 2016 13:46:55 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 175C385443 for ; Tue, 20 Dec 2016 13:46:55 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id u-3vLfsoTMWu for ; Tue, 20 Dec 2016 13:46:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from lupi.sysmic.org (sysmic.org [62.210.89.17]) by whitealder.osuosl.org (Postfix) with ESMTPS id 8A9BE855F5 for ; Tue, 20 Dec 2016 13:46:53 +0000 (UTC) Received: from lupi.online.net (sysmic.org [62.210.89.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jezz) by lupi.sysmic.org (Postfix) with ESMTPSA id B149642832; Tue, 20 Dec 2016 14:46:51 +0100 (CET) From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Pouiller?= To: buildroot@busybox.net Date: Tue, 20 Dec 2016 14:46:18 +0100 Message-Id: <1482241596-31688-2-git-send-email-jezz@sysmic.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1482241596-31688-1-git-send-email-jezz@sysmic.org> References: <1482241596-31688-1-git-send-email-jezz@sysmic.org> MIME-Version: 1.0 Cc: Thomas Petazzoni , =?UTF-8?q?J=C3=A9r=C3=B4me=20Pouiller?= Subject: [Buildroot] [PATCH v5 01/19] reproducible: fix DATE/TIME macros in toolchain-wrapper X-BeenThere: buildroot@busybox.net X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: buildroot-bounces@busybox.net Sender: "buildroot" The use __DATE__ and __TIME__ are one of most common sources of non-reproducible binaries. In order to fix that, gcc begin to support SOURCE_DATE_EPOCH variable. This patch take advantage of toolchain-wrapper to provide support of SOURCE_DATE_EPOCH to older gcc versions. Function get_source_date_epoch() come directly from gcc git. This work was sponsored by `BA Robotic Systems'. Signed-off-by: Jérôme Pouiller Reviewed-by: Samuel Martin --- Notes: v3: - Handle $SOURCE_DATE_EPOCH at runtime (Thomas) v2: - Overload __TIME__ and __DATE__ instead of patching gcc (Thomas) toolchain/toolchain-wrapper.c | 74 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/toolchain/toolchain-wrapper.c b/toolchain/toolchain-wrapper.c index d59629b..6150574 100644 --- a/toolchain/toolchain-wrapper.c +++ b/toolchain/toolchain-wrapper.c @@ -22,12 +22,17 @@ #include #include #include +#include #ifdef BR_CCACHE static char ccache_path[PATH_MAX]; #endif static char path[PATH_MAX]; static char sysroot[PATH_MAX]; +// strlen("-D__TIME__=\"HH:MM:SS\"") + 1 = 22 +static char source_time[22]; +// strlen("-D__DATE__=\"MMM DD YYYY\"") + 1 = 25 +static char source_date[25]; /** * GCC errors out with certain combinations of arguments (examples are @@ -39,8 +44,11 @@ static char sysroot[PATH_MAX]; * -mfloat-abi= * -march= * -mcpu= + * -D__TIME__= + * -D__DATE__= + * -Wno-builtin-macro-redefined */ -#define EXCLUSIVE_ARGS 3 +#define EXCLUSIVE_ARGS 6 static char *predef_args[] = { #ifdef BR_CCACHE @@ -139,6 +147,47 @@ static void check_unsafe_path(const char *arg, } } +/* Read SOURCE_DATE_EPOCH from environment to have a deterministic + * timestamp to replace embedded current dates to get reproducible + * results. Returns -1 if SOURCE_DATE_EPOCH is not defined. + */ +time_t get_source_date_epoch() +{ + char *source_date_epoch; + long long epoch; + char *endptr; + + source_date_epoch = getenv("SOURCE_DATE_EPOCH"); + if (!source_date_epoch) + return (time_t) -1; + + errno = 0; + epoch = strtoll (source_date_epoch, &endptr, 10); + if ((errno == ERANGE && (epoch == LLONG_MAX || epoch == LLONG_MIN)) + || (errno != 0 && epoch == 0)) { + fprintf(stderr, "environment variable $SOURCE_DATE_EPOCH: " + "strtoll: %s\n", strerror(errno)); + exit(2); + } + if (endptr == source_date_epoch) { + fprintf(stderr, "environment variable $SOURCE_DATE_EPOCH: " + "no digits were found: %s\n", endptr); + exit(2); + } + if (*endptr != '\0') { + fprintf(stderr, "environment variable $SOURCE_DATE_EPOCH: " + "trailing garbage: %s\n", endptr); + exit(2); + } + if (epoch < 0) { + fprintf(stderr, "environment variable $SOURCE_DATE_EPOCH: " + "value must be nonnegative: %lld \n", epoch); + exit(2); + } + + return (time_t) epoch; +} + int main(int argc, char **argv) { char **args, **cur, **exec_args; @@ -149,6 +198,7 @@ int main(int argc, char **argv) char *paranoid_wrapper; int paranoid; int ret, i, count = 0, debug; + time_t source_date_epoch; /* Calculate the relative paths */ basename = strrchr(progpath, '/'); @@ -254,6 +304,28 @@ int main(int argc, char **argv) } #endif /* ARCH || CPU */ + source_date_epoch = get_source_date_epoch(); + if (source_date_epoch != -1) { + struct tm *tm = localtime(&source_date_epoch); + if (!tm) { + perror("__FILE__: localtime"); + return 3; + } + ret = strftime(source_time, sizeof(source_time), "-D__TIME__=\"%T\"", tm); + if (!ret) { + perror("__FILE__: overflow"); + return 3; + } + *cur++ = source_time; + ret = strftime(source_date, sizeof(source_date), "-D__DATE__=\"%b %e %Y\"", tm); + if (!ret) { + perror("__FILE__: overflow"); + return 3; + } + *cur++ = source_date; + *cur++ = "-Wno-builtin-macro-redefined"; + } + paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH"); if (paranoid_wrapper && strlen(paranoid_wrapper) > 0) paranoid = 1;