From patchwork Fri Oct 21 17:42:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asaf Fisher X-Patchwork-Id: 59305 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 D305C3851ABD for ; Fri, 21 Oct 2022 17:42:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D305C3851ABD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666374159; bh=tk38nUpz0JCyobZcQToej4TjFDYhhGeuedSXQq/XGXU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=OSyV73dVrciI+6T2DO2wydS24I4HAwLoA7N4aixlzCTEz8L2zB1G0gSPLqLNIw2MJ F+ax0/zgEbeMRC3ZSTzU5IhQMXbT1HC9VJxjmqVFRBz85kwrtkqauG1XZ2cFma1ZlG gy98rxOO8s8UeaAlWQnlccSQBRENgOicC0M3xXfg= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id A43253856DC0 for ; Fri, 21 Oct 2022 17:42:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A43253856DC0 Received: by mail-ed1-x536.google.com with SMTP id a67so8576680edf.12 for ; Fri, 21 Oct 2022 10:42:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tk38nUpz0JCyobZcQToej4TjFDYhhGeuedSXQq/XGXU=; b=b7sezuPWQnhTgCWb26pvg8Esu3A3L+2kkMORJ5j1cc7jtDz5WwXW4cuHImohopP53x bv33G8MCE+PRYKZJt0VPK6oYXBu0F2BFm+f2c5hBhexkqmRC8j8uUsQbZax1y1N11EiN 7h8hgTpKRp9mUb21qChG5hm0isGA2IKJgJ4yXIIpgBFMyLADWZkOV+7XRoL5cLUMfi7/ /TKHbZ1YtZcpwaJbwJ1Ft2QWo0QsudDJHW8tuNWy0JSXK/Wl/Xfqm9ikb013RQdvSnY+ l7u4KIogb0VGV9YjdtBZLkDb4Bgk24z2XWzZTVDRY+r9UFeRuz8k/CF7tZ+U/MekdFnF txxA== X-Gm-Message-State: ACrzQf0yeA/qfTtNbttiHETLz7rTy4S+ILxcDOu2pfMxz6MF9CZfYl9w r56mdtHjZRCEtuI7ij1PmIL64SnCdViPsA== X-Google-Smtp-Source: AMsMyM6Ba7IDvhddE6fHT1gZyr+/Q1RGqh4MQ1viXKSUjlcU0aaTV1NWIZN0Y8yKVY+c2YVIB0dE+A== X-Received: by 2002:a05:6402:847:b0:453:944a:ba8e with SMTP id b7-20020a056402084700b00453944aba8emr18277216edz.326.1666374129063; Fri, 21 Oct 2022 10:42:09 -0700 (PDT) Received: from codespaces-99e6ae.cwehd3ikdxye1e5jw03kh0ngkf.ax.internal.cloudapp.net ([104.40.192.11]) by smtp.gmail.com with ESMTPSA id 1-20020a170906200100b007933047f930sm3593150ejo.157.2022.10.21.10.42.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Oct 2022 10:42:08 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH v3 1/2] Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly Date: Fri, 21 Oct 2022 17:42:04 +0000 Message-Id: <20221021174205.5389-1-asaffisher.dev@gmail.com> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 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 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: Asaf Fisher via Gdb-patches From: Asaf Fisher Reply-To: Asaf Fisher Cc: Asaf Fisher Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" --- gdb/testsuite/gdb.base/solib-proc-self.cc | 72 ++++++++++++++++++ gdb/testsuite/gdb.base/solib-proc-self.exp | 86 ++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 gdb/testsuite/gdb.base/solib-proc-self.cc create mode 100644 gdb/testsuite/gdb.base/solib-proc-self.exp diff --git a/gdb/testsuite/gdb.base/solib-proc-self.cc b/gdb/testsuite/gdb.base/solib-proc-self.cc new file mode 100644 index 00000000000..dc0b446d53c --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-proc-self.cc @@ -0,0 +1,72 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2007-2022 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __WIN32__ +#include +#define dlopen(name, mode) LoadLibrary (name) +#define dlclose(handle) FreeLibrary (handle) +#define dlerror() "an error occurred" +#else +#include +#endif + +int main() +{ + void *handle; + /* Read the so's content to a buffer */ + std::ifstream read_so_file = std::ifstream(SHLIB_NAME); + read_so_file.seekg(0, std::ios::end); + std::streamsize size = read_so_file.tellg(); + read_so_file.seekg(0, std::ios::beg); + std::vector buffer(size); + if (!read_so_file.read(buffer.data(), size)) + { + fprintf (stderr, "Failed to load solib\n"); + exit(1); + } + + int mem_fd = memfd_create("test", 0); + + /* Write the so's data to the memory mapped file. */ + write(mem_fd, buffer.data(), buffer.size()); + + /* Generate the /proc/self/fd/[num] path */ + std::string prof_self_fd_path; /* break-here */ + std::stringstream prof_self_fd_path_stream = std::stringstream(prof_self_fd_path); + prof_self_fd_path_stream << "/proc/self/fd/" << mem_fd; + + /* Call dlopen on it */ + handle = dlopen (prof_self_fd_path_stream.str().c_str(), RTLD_LAZY); + if (!handle) + { + fprintf (stderr, "%s\n", dlerror ()); + exit (1); + } + /* YAY it worked */ + dlclose (handle); + + return 0; +} diff --git a/gdb/testsuite/gdb.base/solib-proc-self.exp b/gdb/testsuite/gdb.base/solib-proc-self.exp new file mode 100644 index 00000000000..b59ba357492 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-proc-self.exp @@ -0,0 +1,86 @@ +# Copyright 2007-2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . */ + +# Test connecting and disconnecting at shared library events. + +if {[skip_shlib_tests]} { + untested "could not run to main" + return 0 +} + +standard_testfile .cc + +# Chose random lib +set libfile so-disc-shr +set libsrc "${srcdir}/${subdir}/${libfile}.c" +set libname "${libfile}.so" +set libobj [standard_output_file ${libname}] + +# Compile the shared lib +if { [gdb_compile_shlib $libsrc $libobj {debug}] != ""} { + return -1 +} + +# Compile test +if [ prepare_for_testing "failed to prepare" $testfile $srcfile "list shlib_load debug c++ additional_flags=-DSHLIB_NAME=\"${libobj}\"" ] { + return -1 +} + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir +gdb_load ${binfile} +gdb_load_shlib $libobj + +if ![runto_main] then { + return 0 +} + +# Get inferior's PID for later +set inferior_pid -1 +gdb_test_multiple "info inferior 1" "get inferior pid" { + -re "process (\[0-9\]*).*$gdb_prompt $" { + set inferior_pid $expect_out(1,string) + pass $gdb_test_name + } +} + +# Turn on the solib-events so we can see that gdb resolves everything correctly +gdb_test_no_output "set stop-on-solib-events 1" + +# I use this breakpoint to get the memory mapped fd. +gdb_breakpoint [gdb_get_line_number "break-here"] +gdb_continue_to_breakpoint "break-here" ".* break-here .*" + +set msg "Getting MEMFD" +set memfd "" +gdb_test_multiple "p mem_fd" $msg { + -re "\\\$$decimal = (\[^\r\n\]*)\r\n$gdb_prompt $" { + set memfd $expect_out(1,string) + pass $msg + } +} + +gdb_test "continue" "Stopped due to shared library event.*" "continue to load" + +# Check if inferior resolved the /proc/self/fd/[num] to /proc/[pid]/fd/[num] +set msg "Inferior's /proc/self resolving $inferior_pid $memfd" +set inferior_proc_self_path "" +gdb_test_multiple "continue" $msg { + -re "Attempting to replace `self` with inferior's PID. -> (\/proc\/$inferior_pid\/fd\/$memfd\[^\r\n\]*)\r\n.*$gdb_prompt $" { + set inferior_proc_self_path $expect_out(1,string) + pass $msg + } +}