From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by sourceware.org (Postfix) with ESMTPS id 38B29383941A; Thu, 19 May 2022 22:19:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 38B29383941A Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-2f863469afbso71341557b3.0; Thu, 19 May 2022 15:19:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nOGzk9LtxUNO553CuKFUOFaVUqTqNBbZO0seiXLSuzk=; b=nX2gPTr6TdV2pD9HOQjQDB3BhGq17hP0h4UDY56zD3T/Mwc6tM56bJHHXfaLfIWWWJ bkLhNdpgsHhA/SBXx4jzz2Ltd5IWW6FNm0X9sSYAE7sS/DAd9baEEUIyLg3+8wGjki/7 IOuEfKwa/eFtjXd1ilnnFDndPsLUVApFXy7J+KVNgp9VSkMW6KhDDIYZfnekfsu9KjzF FCoBx1kNG54kMrhh5USO9SM5eFDdXd0TWo/r3DGjlxP6Me8BBgYjsUFXKuuoj3/hO1br v4oBP7PZDeUuG6T/msAyOQyGhoFzimU8eGAOZM7r/sGoOccdHVoQYUHs7LfchzRvdixT tDOg== X-Gm-Message-State: AOAM530q3n+mH8+Du44DfUyq+X2kaq/u7Hon5imErVACFZR9a256xg9s biy3XUHrabOewg9jJLxX3I1lbgIhK78rlSoesM6npJvC/wQ= X-Google-Smtp-Source: ABdhPJxM/KOJxUTLqJJAp47pq9BiI5x6PJqyF6F8b1/RepscDpNQXlPBZ8g/o/QSOlZX+afdvhfaBXEusN3jXjlHNRg= X-Received: by 2002:a81:25d3:0:b0:2ff:111c:a5ce with SMTP id l202-20020a8125d3000000b002ff111ca5cemr7011600ywl.372.1652998766813; Thu, 19 May 2022 15:19:26 -0700 (PDT) MIME-Version: 1.0 References: <20220414041231.926415-1-goldstein.w.n@gmail.com> <20220519221803.57957-1-goldstein.w.n@gmail.com> <20220519221803.57957-2-goldstein.w.n@gmail.com> In-Reply-To: <20220519221803.57957-2-goldstein.w.n@gmail.com> From: Noah Goldstein Date: Thu, 19 May 2022 17:19:16 -0500 Message-ID: Subject: Re: [PATCH v11 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) To: GNU C Library Cc: "H.J. Lu" , "Carlos O'Donell" , Siddhesh Poyarekar Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.9 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-List-Received-Date: Thu, 19 May 2022 22:19:31 -0000 On Thu, May 19, 2022 at 5:18 PM Noah Goldstein wrote: > > If we want to further optimize the functions tests are needed. > Reviewed-by: Siddhesh Poyarekar No changes v10 -> v11 so left reviewed by. > --- > 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 6f4ea78007..838fe39afb 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 > -- > 2.34.1 >