From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id D68DA3858038 for ; Tue, 25 May 2021 16:03:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D68DA3858038 Received: by mail-lf1-x12a.google.com with SMTP id j10so46844901lfb.12 for ; Tue, 25 May 2021 09:03:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Shl56VsLDTkZNUw68yEyerb4ofBEL2SmXqMlx6szSpQ=; b=AQQ+WXkiihOhVKjUj8aCIAhy7ZMv9sGMuORmh87fc/Yig7cP2Po0RuLsyE7fGlM9YX OwonU/TJszkOozqGPLBhWpdDYV+MFxqg2BfvksgUxcLM6UwfOeqH8OpAqU3yt9AcUjyB 0WouSKdRngWfOxt9HuLtw1UxjMz02ZxgAbepLP7bk7XzklUkivxS5Buk64SwaAcRcUey dqFP1qub0IEuKbyTETIFl7E6KN7Iq5oYsnKCd3E7cWo8riVCDDVwLW/fxfHLBnX/hG6r otp4ryMeWAKRFceZOyeQCyUvV8DZML60f5NE4fTmSmP4w4ACOhGyn6RKLxt8zBhYoMZy ZZeg== X-Gm-Message-State: AOAM530IDbWkFtdiAPAclu/anNpntd0M5VoGkTyDSeyX3dQdEz1JjknJ CSoAXmkxGrhEvPwEm8/phAnR/MwfzuPQbhvbJfU= X-Google-Smtp-Source: ABdhPJyFC5ztMdeEFdAEd04nhOr7+m6EJvWtlKSUiNnk2U4Hrhluj1oCK7iwYGOOVx5o5adSKyVE3wYb/2BLnqVourg= X-Received: by 2002:a05:6512:214f:: with SMTP id s15mr14374570lfr.243.1621958626634; Tue, 25 May 2021 09:03:46 -0700 (PDT) MIME-Version: 1.0 References: <87eedv19yw.fsf@oldenburg.str.redhat.com> In-Reply-To: <87eedv19yw.fsf@oldenburg.str.redhat.com> From: Fengkai Sun Date: Wed, 26 May 2021 00:03:34 +0800 Message-ID: Subject: Re: Can LD_PRELOAD set different symbol definitions for shared objects and executable? To: Florian Weimer Cc: libc-help@sourceware.org X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 May 2021 16:03:50 -0000 Hi Florian, Thanks for your help as always. I've done some research on LD_AUDIT interfaces, and found it very useful to me. However, I had some problems when implementing a separate malloc for executable and shared objects: I use la_symbind64 to monitor symbol binding. The pseudocode is as follows: IF symname == "malloc" AND refcook == "shared objects" RETURN custom_malloc ELSE RETURN malloc Then I discovered I have no way to get the address of custom malloc in the audit library... I tried to dlopen the audit library, hoping it would return the handle for the audit library, so that I could define a pointer in audit library pointing to custom malloc. However it turns out dlopen is mapping a new copy like this: ***dlopen-ed audit library*** 7f798d9e6000-7f798d9e7000 r--p 00000000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9e7000-7f798d9e8000 r-xp 00001000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9e8000-7f798d9e9000 r--p 00002000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9e9000-7f798d9ea000 r--p 00002000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9ea000-7f798d9eb000 rw-p 00003000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9eb000-7f798d9ec000 r--p 00000000 08:02 130290060 /home/hypermoon/Qcloud/tst-setenv/mymalloc.so 7f798d9ec000-7f798d9ed000 r-xp 00001000 08:02 130290060 /home/hypermoon/Qcloud/tst-setenv/mymalloc.so 7f798d9ed000-7f798d9ee000 r--p 00002000 08:02 130290060 /home/hypermoon/Qcloud/tst-setenv/mymalloc.so 7f798d9ee000-7f798d9ef000 r--p 00002000 08:02 130290060 /home/hypermoon/Qcloud/tst-setenv/mymalloc.so 7f798d9ef000-7f798d9f0000 rw-p 00003000 08:02 130290060 /home/hypermoon/Qcloud/tst-setenv/mymalloc.so ***original audit library*** 7f798d9f0000-7f798d9f1000 r--p 00000000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9f1000-7f798d9f2000 r-xp 00001000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9f2000-7f798d9f3000 r--p 00002000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9f3000-7f798d9f4000 r--p 00002000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so 7f798d9f4000-7f798d9f5000 rw-p 00003000 08:02 130290069 /home/hypermoon/Qcloud/tst-setenv/libaudit.so So I can't find a way to get the link map for the original audit library. Also, moving the implementation of malloc directly into audit library will result in a segfault that gdb isn't even able to run. Can you give me some advice on how to work with it? Thank you very much. Best, Fengkai On Tue, May 25, 2021 at 4:41 PM Florian Weimer wrote: > * Fengkai Sun via Libc-help: > > > For example, if I set LD_PRELOAD="./mymalloc.so", then all references to > > malloc will be interposed. There is no way to specify a custom allocator > > only for shared objects(maybe I can make a difference in the source code > of > > custom malloc, but that's off topic). > > Yes, LD_* variables are captured at startup, and changing their values > later using setenv etc. does not have an impact on the current process. > > > I wonder if there is any way to do this, or if I missed out some > > fundamental ideas. > > It is possible to do this using the LD_AUDIT framework. > > Thanks, > Florian > >