From patchwork Wed May 18 17:26:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 54170 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 876FD385734A for ; Wed, 18 May 2022 17:27:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 876FD385734A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1652894864; bh=1RB1f/sc7ciFSDiAcHwVA9ScT5CB5kCl4yr2asRkkac=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ujS1gAafW2QvA3/VwF8NmtYbN54yhktnmTkI576XJuPSoBgOxkb/lkD98+K0JEjDV Jg/wamkok+ExXo7g2noB4Gfh95adEM6LGCyQJ0SUxfUK+HJX2Lc4sXRKw+5YUlYipL xbDj42n2tT0gTN/NAvfDVh0fGIeQyiFbKv8AxBD0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by sourceware.org (Postfix) with ESMTPS id CF1193858D33 for ; Wed, 18 May 2022 17:26:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CF1193858D33 Received: by mail-io1-xd36.google.com with SMTP id q203so3107493iod.0 for ; Wed, 18 May 2022 10:26:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1RB1f/sc7ciFSDiAcHwVA9ScT5CB5kCl4yr2asRkkac=; b=evKDqDJwNBZlHKvuqa8J+O6anU4bsj4sbIcdKerzeQ54u+sB5skTXNHVwSd/W7JM6b qYgDZPchXQZ9Sxa/tWcC9Hlp50NeRz3AAutHsV4zL09c1vZm80BqFy5ZpXP91oCZM7wB A1/zTUk3elZlvmEBKQ6iKEOiHWZ8HFZw2RpvdS4FLUELj6zDUKP2XZIcSYI3U9nPdENc irG3BLc8Rk/exU8PhBvSl6t5Z5QbSeZlP38dVuW2gn2+Xq8XryBgJhMKRh1k2cQlhZPg SCcJRa17mZxypTjcWDN0CYywdZb3pY/j0YngN0XDGNi+sTZBNyvYA4uWKg5b1TPGPDDL MwJQ== X-Gm-Message-State: AOAM5327oebEfXQlKnvo/QxrBk/PNfSMO2tcTsqSpqEr8wxiTU2pDL78 DtmCF8wyDckGbZ0Ddvytzreis7qLfZA= X-Google-Smtp-Source: ABdhPJxNLQ6gJWjFBz76WzVWXBAguiFzwlZQudwj1Bu6uWYnzp6xNsXjufJ4xB/xlu9AwJ1GET/TWw== X-Received: by 2002:a05:6638:d13:b0:32b:cf94:275b with SMTP id q19-20020a0566380d1300b0032bcf94275bmr378641jaj.22.1652894801995; Wed, 18 May 2022 10:26:41 -0700 (PDT) Received: from noah-tgl.. (node-17-161.flex.volo.net. [76.191.17.161]) by smtp.gmail.com with ESMTPSA id r2-20020a02b102000000b0032b3a78179fsm18598jah.99.2022.05.18.10.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 May 2022 10:26:41 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v10 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Date: Wed, 18 May 2022 12:26:31 -0500 Message-Id: <20220518172635.291119-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220518172635.291119-1-goldstein.w.n@gmail.com> References: <20220414041231.926415-1-goldstein.w.n@gmail.com> <20220518172635.291119-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" If we want to further optimize the functions tests are needed. Reviewed-by: Siddhesh Poyarekar --- elf/Makefile | 1 + elf/simple-dl-hash.h | 42 ++++++++++++++++ elf/tst-dl-hash.c | 115 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 elf/simple-dl-hash.h create mode 100644 elf/tst-dl-hash.c diff --git a/elf/Makefile b/elf/Makefile index ce3345ed92..adf1bcf6ce 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -312,6 +312,7 @@ tests := \ tst-array4 \ tst-array5 \ tst-auxv \ + tst-dl-hash \ tst-leaks1 \ tst-stringtable \ tst-tls9 \ diff --git a/elf/simple-dl-hash.h b/elf/simple-dl-hash.h new file mode 100644 index 0000000000..53702b3c55 --- /dev/null +++ b/elf/simple-dl-hash.h @@ -0,0 +1,42 @@ +/* __simple_dl_elf_hash for testing true elf symbol lookup. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SIMPLE_DL_ELF_HASH_H +#define _SIMPLE_DL_ELF_HASH_H 1 + +#include + +/* For testing/benchmarking purposes. Real implementation in + sysdeps/generic/dl-hash.h. */ +static uint32_t +__attribute__ ((unused)) +__simple_dl_elf_hash (const char *name_arg) +{ + unsigned long int hash = 0; + for (unsigned char c = *name_arg; c != '\0'; c = *(++name_arg)) + { + unsigned long int hi; + hash = (hash << 4) + c; + hi = hash & 0xf0000000; + hash ^= hi >> 24; + hash &= 0x0fffffff; + } + return hash; +} + +#endif /* simple-dl-hash.h */ diff --git a/elf/tst-dl-hash.c b/elf/tst-dl-hash.c new file mode 100644 index 0000000000..8697eb73a0 --- /dev/null +++ b/elf/tst-dl-hash.c @@ -0,0 +1,115 @@ +/* Test dl-hash functions. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef unsigned int (*hash_f) (const char *); + + + +static int +do_fill_test (size_t len, int fill, const char *name, hash_f testf, + hash_f expecf) +{ + uint32_t expec, res; + char buf[len + 1]; + memset (buf, fill, len); + buf[len] = '\0'; + + expec = expecf (buf); + res = testf (buf); + if (expec != res) + FAIL_EXIT1 ("FAIL: fill(%d) %s(%zu), %x != %x\n", fill, name, len, expec, + res); + + return 0; +} + +static int +do_fill_tests (size_t len, int fill) +{ + if (do_fill_test (len, fill, "dl_new_hash", &_dl_new_hash, + &__simple_dl_new_hash)) + return 1; + + return do_fill_test (len, fill, "dl_elf_hash", &_dl_elf_hash, + &__simple_dl_elf_hash); +} + +static int +do_rand_test (size_t len, const char *name, hash_f testf, hash_f expecf) +{ + uint32_t expec, res; + size_t i; + char buf[len + 1]; + char v; + for (i = 0; i < len; ++i) + { + v = random (); + if (v == 0) + v = 1; + + buf[i] = v; + } + buf[len] = '\0'; + + expec = expecf (buf); + res = testf (buf); + if (expec != res) + FAIL_EXIT1 ("FAIL: random %s(%zu), %x != %x\n", name, len, expec, res); + + return 0; +} + +static int +do_rand_tests (size_t len) +{ + if (do_rand_test (len, "dl_new_hash", &_dl_new_hash, &__simple_dl_new_hash)) + return 1; + + return do_rand_test (len, "dl_elf_hash", &_dl_elf_hash, &__simple_dl_elf_hash); +} + +static int +do_test (void) +{ + size_t i, j; + for (i = 0; i < 100; ++i) + { + for (j = 0; j < 8192; ++j) + { + if (do_rand_tests (i)) + return 1; + + if (do_fill_tests (i, -1) || do_fill_tests (i, 1) + || do_fill_tests (i, 0x80) || do_fill_tests (i, 0x88)) + return 1; + } + } + return 0; +} + +#include