From patchwork Fri Mar 31 03:44:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67141 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 87568385781F for ; Fri, 31 Mar 2023 03:45:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87568385781F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234354; bh=J28skYPVmzjAWA1dGyEtay8SXd2J00VvU+kkpBxUsqE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Gm7kHL3XZRPCcdJYCHsiveeP16Y1RxSKGKa656zvhGkT+JQnX7Z6QSpcSgmsw35zX ZP+lNzOAg5k2ij9pwDZE2ze1pakRgHm9L62FvNjbD4VP2sLOFz5JyrPW6eh8yIYkk1 jAytWZJRD9D32rM/LwtMm76K2LQH2zXHKj4Ga1h8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id DACC73857438 for ; Fri, 31 Mar 2023 03:45:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DACC73857438 Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-17683b570b8so21918892fac.13 for ; Thu, 30 Mar 2023 20:45:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234307; x=1682826307; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=J28skYPVmzjAWA1dGyEtay8SXd2J00VvU+kkpBxUsqE=; b=HqNQx2L103Qvuhy80lljDfIzVrZVVsc33ssaUTQkVRfdtJc+I2H6qpgP9Yxaps/7o+ CI2nbAq0nSUeCE4ZmEu38gICjoc58Et4N/ujOzu/vQD3AlPXQMWvb7wpY1qSXJT2JcZj ylNFfED2qYpG/1fR+AAtIyP2etgmcsH0TjkdRXoJ1Xshb6pL0d6+FkyhctZcM6X8NlG/ +0Ylw55ZxqI8OJMzFShwn4MKE8a0y605Pd0s6ekk9NfsKFiyEYiBLBkCz9qat2sJpyZz C4kX6Pzyt9xAslp0hgGo9hKcl/fV/+fqjvSkn26NUaya5s3kJL+Op/X2xsQTBnFvNud0 iC+A== X-Gm-Message-State: AO0yUKXUYM2zfxtX434gnhixsUhvWXz0Wc7dsEZJ2tU8CjOkjjVEiGDE K1dnm3dH6NgculsY7BqE/0vDtdFLGMUP3IbikLw= X-Google-Smtp-Source: AKy350bbUdIM+yBBwt+Ir/X4BeWh040Y4BUPPSyFrIuv0pOYWSgo6BA18fZOwTT0JCuN+QsZkMJuRA== X-Received: by 2002:a05:6870:8917:b0:16a:b45a:e2d9 with SMTP id i23-20020a056870891700b0016ab45ae2d9mr15715577oao.7.1680234307294; Thu, 30 Mar 2023 20:45:07 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id zd8-20020a056871278800b0017eccc3fed9sm556304oab.47.2023.03.30.20.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:45:07 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann , Pedro Alves Subject: [PATCH 4/5] gdb/linux-nat: Read auxv from any thread of the process Date: Fri, 31 Mar 2023 03:44:31 +0000 Message-Id: <20230331034432.3037148-5-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 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 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" If the initial thread of the process exits, reading the process' auxiliary vector via /proc/PID/auxv fails in one of two ways: 1. If GDB is root, then opening the file succeeds but reading from it returns 0 bytes. 2. If gdbserver isn't root, then opening the file fails with EACCES. This race isn't easy to run into because one of the first things that GDB does when starting an inferior is to read its auxiliary vector and store it in the auxv cache. All further queries of the auxiliary vector will be served from there, unless one of the cache-clearing events ("inferior_exit", "inferior_appeared", "executable_changed") occurs. To fix the race, use linux_proc_read_auxv instead of the generic procfs implementation. Suggested-by: Pedro Alves --- gdb/linux-nat.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index fd80fd975c14..938ae1c9b8c6 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3667,6 +3667,33 @@ linux_xfer_siginfo (ptid_t ptid, enum target_object object, return TARGET_XFER_OK; } +/* Implement the to_xfer_partial target_ops method for TARGET_OBJECT_AUXV. This + function handles access via /proc/LWP/auxv, which allows handling possible + races in multi-threaded inferiors. */ + +static enum target_xfer_status +linux_nat_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf, + ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) +{ + /* Linux doesn't support writing to the auxv file. */ + if (readbuf == nullptr || writebuf != nullptr) + return TARGET_XFER_E_IO; + + ssize_t xfered; + bool rc = linux_proc_read_auxv (inferior_ptid.pid (), readbuf, (off_t) offset, + (size_t) len, xfered); + + if (!rc) + return TARGET_XFER_E_IO; + else if (xfered == 0) + return TARGET_XFER_EOF; + else + { + *xfered_len = (ULONGEST) xfered; + return TARGET_XFER_OK; + } +} + static enum target_xfer_status linux_nat_xfer_osdata (enum target_object object, const char *annex, gdb_byte *readbuf, @@ -3695,8 +3722,17 @@ linux_nat_target::xfer_partial (enum target_object object, return TARGET_XFER_EOF; if (object == TARGET_OBJECT_AUXV) - return memory_xfer_auxv (this, object, annex, readbuf, writebuf, - offset, len, xfered_len); + { + /* For attached inferiors, use memory_xfer_auxv's ld.so support which + works with virtual executables being executed by Valgrind's + memcheck. */ + if (current_inferior ()->attach_flag) + return memory_xfer_auxv (this, object, annex, readbuf, writebuf, + offset, len, xfered_len); + else + return linux_nat_xfer_auxv (readbuf, writebuf, offset, len, + xfered_len); + } if (object == TARGET_OBJECT_OSDATA) return linux_nat_xfer_osdata (object, annex, readbuf, writebuf,