From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by sourceware.org (Postfix) with ESMTPS id 274B1385829E for ; Fri, 16 Feb 2024 14:25:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 274B1385829E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 274B1385829E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708093553; cv=none; b=Es1Jdrrtztf0HZYZucRiwJMumAGO247hO7El8Vfun2skMnwqAVh+5quUOZC7Srq4lNS+TNSfxRondAx/YtccTnpInT44Ub+eoaGfFQ8nBhgvp30ND4AbskejCNF0PD3BxE9IESR27M7D/vvnbD5fJWe09F0yQJtSg/qz38LGWxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708093553; c=relaxed/simple; bh=gvUNnE+E44Yep8GTP4NAbO+1bsVJACF7QTacH/twhdw=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=Q9BlmF1gcM9w+wzuct/pjx3zA7gyjIC/XDHWNL9Y8BHkFjiI71SHk4r5tCZggG0S65Jhbrr09YcjfDMmWoIJf32sFnKi66I4cQqHB5jB4aJixqN87OGM46nqVEsMZfUKHC42BsNKIFq6ClaIKg2pg5BOCdACw0+kUWsNLp+73K8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-607f8b96580so7340077b3.0 for ; Fri, 16 Feb 2024 06:25:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708093550; x=1708698350; darn=sourceware.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=KMPGuV9x6DH+/X90hlBuGRCH1zd+uzrhKCLjdNhvRGg=; b=ChncIuj7IJCbw3jhEdkpbdtZ3S5BZP4+dM+Bqhwt0TScEFeeduxV7iI2Efs95tY3gy tu4KXtQ3faTPa8e21NMj+VjGPpTN/0u/o5OFPw8k56xh/ve/xWHCgG0nZ00Z7QFrSz5/ k8Xriu/0R29XKWFlVWvK2GwjESJA7fC1aLeTjAA+e9K+laHZHgLGa8a+CGaufFVLTxfK K0BS0hBpgvrjUd4nmJUMXHOiHsoLHZ4Ibx4VXWg5GBypgN4pvqgIhfmXnii9HOkSHdGv V9o4ZTPkTdLuA9ekkpU8Pi4RV3VOkIpR1SJKI0cbKMBkfJAvs2mXmcTiWHPWDiLLgRIY AwJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708093550; x=1708698350; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KMPGuV9x6DH+/X90hlBuGRCH1zd+uzrhKCLjdNhvRGg=; b=eMesMPj7RpvyYI68D+efowaQPgqbcW4VY1Swi816UEexiNvgoTtP8wGJNeDTbElbaD RaUOaZHIjWP3O4Dnk0GaTkR121vBRyQgdPDs1Ns4IupV+4bWAE09lVhi54FVXHno+/HN mSPNAXjRzzLhwq/alNJ0jes3wlCaXClfmuT0HmSfi8QNyReIVXmIchtZWjHmawOUXPR2 hazeAXomGFHstbncDhsQ2Fu9Og7pnHgDslVSUhPqJYM5Mq31ee4HKIRqa8+Exxyaqh0Q Ia8FXchO3PEFzjHJMe4vXe78Etby3O8L+XJrUNetUNruhBKljhpc/dcnBGLdqZrxEsZN sG6A== X-Forwarded-Encrypted: i=1; AJvYcCWR2j5IdpgohFnfdjMZTEwDdnQ8NHDoN5OtaZcVmFgKZqUtrf+StA2cgCljZS1BjCXSRiJCsag3ZYAIp6X+Q9ZQ/QaUGGWUPUbp X-Gm-Message-State: AOJu0YzWloj0kcggVrajWY7+WjFmeyUt+e6YzEbm9wlFfrCk6ccazLEA G2N0UxU2NLQ/j4xnBCvRqP91X/iFyQ+U23XX6kE50EHalnXNxFGpZs1aKgMI+lpwecQvUVU/BjW Ta4Z9yVRi0a2/zdyesf40j4I1/iettUM8Z7I= X-Google-Smtp-Source: AGHT+IFEiCNI69/gotaAun/RqJvdKYfNlRuEyUN6lFFXY5Opw1R5wa8ktO8XHDM6wewf3XfYiLb0dNpcliHJD0gnjRY= X-Received: by 2002:a81:ac15:0:b0:607:ff7c:cdd9 with SMTP id k21-20020a81ac15000000b00607ff7ccdd9mr1098200ywh.12.1708093550481; Fri, 16 Feb 2024 06:25:50 -0800 (PST) MIME-Version: 1.0 References: <20240213041501.2494232-1-hjl.tools@gmail.com> <20240213041501.2494232-3-hjl.tools@gmail.com> <871q9d7wra.fsf@oldenburg.str.redhat.com> <87mss061s0.fsf@oldenburg.str.redhat.com> <984b51c6-920a-40c5-aa1f-182efdd1af92@linaro.org> <479d0338-46e9-439d-8b95-110ff84e07bb@linaro.org> In-Reply-To: <479d0338-46e9-439d-8b95-110ff84e07bb@linaro.org> From: "H.J. Lu" Date: Fri, 16 Feb 2024 06:25:14 -0800 Message-ID: Subject: Re: [PATCH v4 2/2] x86: Update _dl_tlsdesc_dynamic to preserve caller-saved registers To: Adhemerval Zanella Netto Cc: Florian Weimer , libc-alpha@sourceware.org, Fangrui Song , "Carlos O'Donell" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3019.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Feb 16, 2024 at 5:24=E2=80=AFAM Adhemerval Zanella Netto wrote: > > > > On 16/02/24 09:58, H.J. Lu wrote: > > On Fri, Feb 16, 2024 at 4:47=E2=80=AFAM Adhemerval Zanella Netto > > wrote: > >> > >> > >> > >> On 16/02/24 09:37, H.J. Lu wrote: > >>> On Fri, Feb 16, 2024 at 4:20=E2=80=AFAM H.J. Lu = wrote: > >>>> > >>>> On Fri, Feb 16, 2024 at 4:18=E2=80=AFAM Florian Weimer wrote: > >>>>> > >>>>> * H. J. Lu: > >>>>> > >>>>>> On Thu, Feb 15, 2024 at 10:23=E2=80=AFPM Florian Weimer wrote: > >>>>>>> > >>>>>>> * Adhemerval Zanella Netto: > >>>>>>> > >>>>>>>>> diff --git a/elf/malloc-for-test.map b/elf/malloc-for-test.map > >>>>>>>>> new file mode 100644 > >>>>>>>>> index 0000000000..8437cf4346 > >>>>>>>>> --- /dev/null > >>>>>>>>> +++ b/elf/malloc-for-test.map > >>>>>>>>> @@ -0,0 +1,6 @@ > >>>>>>>>> +GLIBC_2.0 { > >>>>>>>> > >>>>>>>> You need to use the correct version to override the malloc: > >>>>>>>> > >>>>>>>> $ gdb --args tst-gnu2-tls2 --direct > >>>>>>>> [...] > >>>>>>>> (gdb) b apply_tls > >>>>>>>> (gdb) r > >>>>>>>> Thread 2 "tst-gnu2-tls2" hit Breakpoint 1, apply_tls (p=3D0x7fff= f7bfee80) at tst-gnu2-tls2mod1.c:25 > >>>>>>>> 25 { > >>>>>>>> (gdb) b malloc > >>>>>>>> Breakpoint 2 at 0x7ffff7ca8ad0: malloc. (3 locations) > >>>>>>>> (gdb) c > >>>>>>>> Continuing. > >>>>>>> > >>>>>>> Why do we need to set a symbol version here? I think this can be > >>>>>>> removed. > >>>>>> > >>>>>> malloc-for-test.so provides a fake malloc to clobber caller-save > >>>>>> registers. Since malloc in ld.so has a symbol version, > >>>>>> > >>>>>> struct r_found_version version; > >>>>>> version.name =3D symbol_version_string (libc, GLIBC_2_0); > >>>>>> version.hidden =3D 0; > >>>>>> version.hash =3D _dl_elf_hash (version.name); > >>>>>> version.filename =3D NULL; > >>>>>> > >>>>>> void *new_calloc =3D lookup_malloc_symbol (main_map, "calloc", &= version); > >>>>>> void *new_free =3D lookup_malloc_symbol (main_map, "free", &vers= ion); > >>>>>> void *new_malloc =3D lookup_malloc_symbol (main_map, "malloc", &= version); > >>>>>> void *new_realloc =3D lookup_malloc_symbol (main_map, "realloc",= &version); > >>>>>> > >>>>>> malloc in malloc-for-test.so must have the same version. Otherwis= e, > >>>>>> it won't be used in the test. > >>>>> > >>>>> I thought that unversioned symbols interpose all versioned symbols.= Has > >>>>> this changed in the dynamic linker? > >>>> > >>>> Only for this case. > >>>> > >>> > >>> I was wrong. Version isn't needed. Will fix it. > >>> > >> > >> Indeed, but I think we also improve the register check testing since j= ust clobbering > >> on malloc does not really provide much coverage. > > > > Why? The slow path will always call malloc in malloc-for-test.so. > > We can clobber any caller-save registers we need. Without > > malloc-for-test.so, the test doesn't fail for i386 since malloc in libc= .so > > doesn't use any vector registers. > > > > The idea is to check whether _dl_tlsdesc_dynamic call does preserve any > possible caller-saved register and since this test is generic and afaik > all affected ABIs follow the same idea (compiler won't save/restore such > register), clobbering the register will only trigger a possible issue > iff the thread that actually issue the TLS usage does actually use any > possible register. > > That's why I think a better coverage would to also clobber the register > before the TLS access, and check if their values does not change over > the TLS access. How about this struct tls * apply_tls (struct tls *p) { tls_var1[1] =3D *p; BEFORE_TLSDESC_CALL (); struct tls *ret =3D &tls_var1[1]; AFTER_TLSDESC_CALL (); return ret; } An architecture can define BEFORE_TLSDESC_CALL and tAFTER_TLSDESC_CALL o verify that clobber caller-saved registers aren't changed by the implicit TLSDESC call. H.J.