From patchwork Tue Oct 31 20:09:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 739596 Delivered-To: patch@linaro.org Received: by 2002:a5d:4c47:0:b0:32d:baff:b0ca with SMTP id n7csp1841658wrt; Tue, 31 Oct 2023 13:09:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzEwwuBMJPk05L+xZnTu8kyhIj40k7Xr5HfmXz3y4v4jp9VAnv5QrPiwyNZ3hIS3ozFRXe X-Received: by 2002:a05:620a:460a:b0:779:cf70:8495 with SMTP id br10-20020a05620a460a00b00779cf708495mr1064294qkb.22.1698782981261; Tue, 31 Oct 2023 13:09:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698782981; cv=pass; d=google.com; s=arc-20160816; b=cRJy2HIY9b+HUQiav/ns4JSO7sHstLQ9jzgpefkfw/BZcQ/Ui2O0retaFsfzeVgwHN ysz3huTmGttpSWR73GpuVKoWWLER1IjUt+8NotbEtwwI/KJTJ5dvXWa17W8yAcvRbi3H r4F83WEYbU+ADCrcaztQ/d59aB67bV8wUxxHUs/B3Pnw6wGHnNQEiEhq/Tcwdo78BcTr MjlYsvxcc+rQ6YtQI9Dk/gJSMYqVzFCsjB0CW6x5p3ZgPWMq16yw3nTSWVAOaeHoaKFV C/xi8LOTVc92a8DA19InIX4QCFTbG390Vwldz6cSHNOLPgOgdA/9aPSi2GHjvz2k3ji1 +EWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=zIauoWevDKiUNM+VEPSV/YJFtLv+N8G4DiKdLq6Kgyw=; fh=pPFR/wQ0k/GuqyNsntNA5LqEpigLoSYvby6BmJvILyA=; b=kQdtbq9gs/bTQo8jPEj4jzPv7DDd+9lv3rWlPS+CHaldaPDLn4wrdMOz8Yc8vKRYB5 8SoDnyyQaINeMFsxwVGT86RlEWbBoKr6SjrhxqP8Yamp5VR8XyGw1G1sJJP91sW64H3r rlCkQZU9ZzlITm4fDtEeiFQ6TpzUDKk4iK2jNhw6W+uq81R8AM4Z2xctSbVWq+eExJIa vgdxUl7MCEHB1NLxfQP69sCaHBJp7gfgigFCvtpkQ11zVHQdtpKDHZvkq3CeSPeaBbHy 66b1RxG8JNwnwRWQaB+YfbsRFcMwyxqCFHuJtKOQURO93xDKNbAAhHhIixEbrvCMunWZ INvQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gOIDZNNB; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ty5-20020a05620a3f4500b00773fa71cbb8si1622692qkn.512.2023.10.31.13.09.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 13:09:41 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gOIDZNNB; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD4973857357 for ; Tue, 31 Oct 2023 20:09:40 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by sourceware.org (Postfix) with ESMTPS id BA5C73858C52 for ; Tue, 31 Oct 2023 20:09:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA5C73858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BA5C73858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1129 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698782974; cv=none; b=aSi+MElv0QWBXeWH+/PCprQmU1IkKMVwTMm7Hu8gf5v75Zt62twgbmuolo8s7hntppywOZMoOlix7IAHuPRn2P81s/WUW5v6Mh8a1HZhzputSCL+5GT2V9kjHNmL554x/e1s1k8iB5h7JOgE2J1tEcfJnxUUubNuDm8GKgetTXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698782974; c=relaxed/simple; bh=m3dkwBibuOaRZ+MwwBzHLQsRix84pP21dJ2j7HtC2ss=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=KgQa+JnmwuLmxWuwMi9n4aHlIXMm4+IZ56ykpIwG4x7TaalphK941umndu/8I/d6dtNMVtaLWZavcBeY578JZVBw5PjtFjt7WniMXuYK1TfN8jO+9Lm/Dt3HeEvB+CHHG/Ri503XN2B6Ftz2GediTd3/Y/7HJBKYUPYevet4wUU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-59e88a28b98so2145857b3.1 for ; Tue, 31 Oct 2023 13:09:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698782971; x=1699387771; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=zIauoWevDKiUNM+VEPSV/YJFtLv+N8G4DiKdLq6Kgyw=; b=gOIDZNNB41etjGsFq6lkrEvETKE7oUvf/zsM4OtF3RSF+ymEPfAAj8sAr7nsuWIFXy bDDzmvc4oSxgetcgLwPtfD8TF133au0oBACrSJc0BqtXPEI00jrEpGJwNF0uhD8lW0Es vzr1dnHEWY9UTYIHZCXXdAaV2gyiHSAw7E1c8C7+NEn4ls1QC7mE4jwTYLzagcsuLM/p smklY3beecjyLBh5a5LD5MiQiNLepMxBxhLHDLCTuP6bjOfrWzgqHRsv+Qh/oEGapSt4 Sy6PoZ2p7Hz8yB7sEkxwdexRMwNrXKv7xZX82QEwsHj9dSA7jwWApE3YLLSz0cxywG4Y pOgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698782971; x=1699387771; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zIauoWevDKiUNM+VEPSV/YJFtLv+N8G4DiKdLq6Kgyw=; b=YfzLwd9QLpAtmXOR+dXFGPAqGpKAr9TL6EMrOewgAq+2aq/McG+D46QCBMb1x/dhpc dciH8sVUrg65lzKb4vFN/tUauw4jeS+U/bZkZuQJOBp+ZuBFp1jjPaNoKEHV1IdZnj6C /EkBuqWi0/IBiTLigehboCH/5YB9eJN6yrUP390jG3MRVfo23ZlPfXN9EOLUaSH/I86a FvQOwQLvKNT3kGZhmNz52qQdNhlPa8GAdhtehWlUSGFXn5sdx+oyMr6f7IVF+7qbc5fg Pk7yPFk/ncysorAKuLgnwl99Apa6f935txAF14VuSbxh6BufqhQ3t7cbZKoH36ztazgh Zf7w== X-Gm-Message-State: AOJu0YyKy6y7Fc9/L/A2S10rT8I/DO7q3aDwQT8Slcb4EHW6KI+yyMRc gOr4hNfu+Nbk4rauUU9dHImfezhsU/is3B3FGavKGw== X-Received: by 2002:a81:4c58:0:b0:5a7:b4d1:c4dd with SMTP id z85-20020a814c58000000b005a7b4d1c4ddmr2761716ywa.5.1698782971412; Tue, 31 Oct 2023 13:09:31 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:3d3c:6c87:9be3:8cfc:976d]) by smtp.gmail.com with ESMTPSA id q69-20020a819948000000b005a7fa3ccb32sm1264111ywg.35.2023.10.31.13.09.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 13:09:30 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , "H . J . Lu" , Bruce Merry Subject: [PATCH 1/4] elf: Add a way to check if tunable is set (BZ 27069) Date: Tue, 31 Oct 2023 17:09:22 -0300 Message-Id: <20231031200925.3297456-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231031200925.3297456-1-adhemerval.zanella@linaro.org> References: <20231031200925.3297456-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org The tunable already keep a field whether it is initialized. To query the default value, it is easier to add a new constant field. The patch adds two new macros, TUNABLE_GET_DEFAULT and TUNABLE_IS_INITIALIZED, where the former get the default value with a signature similar to TUNABLE_GET while the later returns whether the tunable was set by the environment. Checked on x86_64-linux-gnu. --- elf/dl-tunable-types.h | 1 + elf/dl-tunables.c | 40 ++++++++++++++++++++++++++++++++++++++++ elf/dl-tunables.h | 28 ++++++++++++++++++++++++++++ elf/dl-tunables.list | 1 + scripts/gen-tunables.awk | 4 ++-- 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/elf/dl-tunable-types.h b/elf/dl-tunable-types.h index c88332657e..c41a3b3bdb 100644 --- a/elf/dl-tunable-types.h +++ b/elf/dl-tunable-types.h @@ -61,6 +61,7 @@ struct _tunable { const char name[TUNABLE_NAME_MAX]; /* Internal name of the tunable. */ tunable_type_t type; /* Data type of the tunable. */ + const tunable_val_t def; /* The value. */ tunable_val_t val; /* The value. */ bool initialized; /* Flag to indicate that the tunable is initialized. */ diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index cae67efa0a..79b4d542a3 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -145,6 +145,13 @@ tunable_initialize (tunable_t *cur, const char *strval) do_tunable_update_val (cur, &val, NULL, NULL); } +bool +__tunable_is_initialized (tunable_id_t id) +{ + return tunable_list[id].initialized; +} +rtld_hidden_def (__tunable_is_initialized) + void __tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp, tunable_num_t *maxp) @@ -388,6 +395,39 @@ __tunables_print (void) } } +void +__tunable_get_default (tunable_id_t id, void *valp) +{ + tunable_t *cur = &tunable_list[id]; + + switch (cur->type.type_code) + { + case TUNABLE_TYPE_UINT_64: + { + *((uint64_t *) valp) = (uint64_t) cur->def.numval; + break; + } + case TUNABLE_TYPE_INT_32: + { + *((int32_t *) valp) = (int32_t) cur->def.numval; + break; + } + case TUNABLE_TYPE_SIZE_T: + { + *((size_t *) valp) = (size_t) cur->def.numval; + break; + } + case TUNABLE_TYPE_STRING: + { + *((const char **)valp) = cur->def.strval; + break; + } + default: + __builtin_unreachable (); + } +} +rtld_hidden_def (__tunable_get_default) + /* Set the tunable value. This is called by the module that the tunable exists in. */ void diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h index 45c191e021..0df4dde24e 100644 --- a/elf/dl-tunables.h +++ b/elf/dl-tunables.h @@ -45,18 +45,26 @@ typedef void (*tunable_callback_t) (tunable_val_t *); extern void __tunables_init (char **); extern void __tunables_print (void); +extern bool __tunable_is_initialized (tunable_id_t); extern void __tunable_get_val (tunable_id_t, void *, tunable_callback_t); extern void __tunable_set_val (tunable_id_t, tunable_val_t *, tunable_num_t *, tunable_num_t *); +extern void __tunable_get_default (tunable_id_t id, void *valp); rtld_hidden_proto (__tunables_init) rtld_hidden_proto (__tunables_print) +rtld_hidden_proto (__tunable_is_initialized) rtld_hidden_proto (__tunable_get_val) rtld_hidden_proto (__tunable_set_val) +rtld_hidden_proto (__tunable_get_default) /* Define TUNABLE_GET and TUNABLE_SET in short form if TOP_NAMESPACE and TUNABLE_NAMESPACE are defined. This is useful shorthand to get and set tunables within a module. */ #if defined TOP_NAMESPACE && defined TUNABLE_NAMESPACE +# define TUNABLE_IS_INITIALIZED(__id) \ + TUNABLE_IS_INITIALIZED_FULL(TOP_NAMESPACE, TUNABLE_NAMESPACE, __id) +# define TUNABLE_GET_DEFAULT(__id, __type) \ + TUNABLE_GET_DEFAULT_FULL(TOP_NAMESPACE, TUNABLE_NAMESPACE,__id, __type) # define TUNABLE_GET(__id, __type, __cb) \ TUNABLE_GET_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, __type, __cb) # define TUNABLE_SET(__id, __val) \ @@ -65,6 +73,10 @@ rtld_hidden_proto (__tunable_set_val) TUNABLE_SET_WITH_BOUNDS_FULL (TOP_NAMESPACE, TUNABLE_NAMESPACE, __id, \ __val, __min, __max) #else +# define TUNABLE_IS_INITIALIZED(__top, __ns, __id) \ + TUNABLE_IS_INITIALIZED_FULL(__top, __ns, __id) +# define TUNABLE_GET_DEFAULT(__top, __ns, __type) \ + TUNABLE_GET_DEFAULT_FULL(__top, __ns, __id, __type) # define TUNABLE_GET(__top, __ns, __id, __type, __cb) \ TUNABLE_GET_FULL (__top, __ns, __id, __type, __cb) # define TUNABLE_SET(__top, __ns, __id, __val) \ @@ -73,6 +85,22 @@ rtld_hidden_proto (__tunable_set_val) TUNABLE_SET_WITH_BOUNDS_FULL (__top, __ns, __id, __val, __min, __max) #endif +/* Return whether the tunable was initialized by the environment variable. */ +#define TUNABLE_IS_INITIALIZED_FULL(__top, __ns, __id) \ +({ \ + tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \ + __tunable_is_initialized (id); \ +}) + +/* Return the default value of the tunable. */ +#define TUNABLE_GET_DEFAULT_FULL(__top, __ns, __id, __type) \ +({ \ + tunable_id_t id = TUNABLE_ENUM_NAME (__top, __ns, __id); \ + __type __ret; \ + __tunable_get_default (id, &__ret); \ + __ret; \ +}) + /* Get and return a tunable value. If the tunable was set externally and __CB is defined then call __CB before returning the value. */ #define TUNABLE_GET_FULL(__top, __ns, __id, __type, __cb) \ diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 695ba7192e..5bb858b1d8 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -20,6 +20,7 @@ # type: Defaults to STRING # minval: Optional minimum acceptable value # maxval: Optional maximum acceptable value +# default: Optional default value (if not specified it will be 0 or "") # env_alias: An alias environment variable # security_level: Specify security level of the tunable for AT_SECURE binaries. # Valid values are: diff --git a/scripts/gen-tunables.awk b/scripts/gen-tunables.awk index d6de100df0..9726b05217 100644 --- a/scripts/gen-tunables.awk +++ b/scripts/gen-tunables.awk @@ -177,8 +177,8 @@ END { n = indices[2]; m = indices[3]; printf (" {TUNABLE_NAME_S(%s, %s, %s)", t, n, m) - printf (", {TUNABLE_TYPE_%s, %s, %s}, {%s}, false, TUNABLE_SECLEVEL_%s, %s},\n", - types[t,n,m], minvals[t,n,m], maxvals[t,n,m], + printf (", {TUNABLE_TYPE_%s, %s, %s}, {%s}, {%s}, false, TUNABLE_SECLEVEL_%s, %s},\n", + types[t,n,m], minvals[t,n,m], maxvals[t,n,m], default_val[t,n,m], default_val[t,n,m], security_level[t,n,m], env_alias[t,n,m]); } print "};"