public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Asaf Fisher via Gdb-patches <gdb-patches@sourceware.org>,
	gdb-patches@sourceware.org
Cc: Asaf Fisher <asaffisher.dev@gmail.com>
Subject: Re: [PATCH v3 1/2] Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly
Date: Mon, 24 Oct 2022 11:45:56 +0100	[thread overview]
Message-ID: <87bkq1a49n.fsf@redhat.com> (raw)
In-Reply-To: <20221021174205.5389-1-asaffisher.dev@gmail.com>

Asaf Fisher via Gdb-patches <gdb-patches@sourceware.org> writes:

> ---
>  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

Hi!

Thanks for working on this fix.  If you have not contributed to GDB
before then you might want to check out:

  https://sourceware.org/gdb/wiki/ContributionChecklist

I think, given the size of these patches, you would need to complete a
copyright assigment before these patches could be merged.  Details of
the copyright assignment can be found here:

  https://sourceware.org/gdb/wiki/ContributionChecklist#FSF_copyright_Assignment

The best choice for copyright assignment would be this starting point:

  http://git.savannah.gnu.org/cgit/gnulib.git/plain/doc/Copyright/request-assign.future

which would assign copyright for past and future contributions to GDB
over to the FSF.

Thanks,
Andrew

>
> 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 <http://www.gnu.org/licenses/>.  */
> +
> +#include <sys/mman.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <iostream>
> +#include <fstream>
> +#include <sstream>
> +#include <vector>
> +#include <unistd.h>
> +
> +#ifdef __WIN32__
> +#include <windows.h>
> +#define dlopen(name, mode) LoadLibrary (name)
> +#define dlclose(handle) FreeLibrary (handle)
> +#define dlerror() "an error occurred"
> +#else
> +#include <dlfcn.h>
> +#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<char> 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 <http://www.gnu.org/licenses/>.  */
> +
> +# 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
> +    }
> +}
> -- 
> 2.38.0


      parent reply	other threads:[~2022-10-24 10:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21 17:42 Asaf Fisher
2022-10-21 17:42 ` [PATCH v3 2/2] Make GDB resolve dlopen of memory mapped shared libraries Asaf Fisher
2022-11-10 19:37   ` Andrew Burgess
2022-11-11 12:35     ` Dr Lancelot SIX
2022-11-11 12:47       ` Asaf Fisher
2022-11-21 11:55       ` Andrew Burgess
2022-11-21 17:56         ` [PATCHv4] gdb: handle loading shared libraries from /proc/self/fd/ Andrew Burgess
2022-12-14 11:51           ` Andrew Burgess
2022-12-15 16:44           ` [PATCHv5] " Andrew Burgess
2022-12-16 16:59             ` Asaf Fisher
2023-01-20 12:33             ` [PATCHv6] " Andrew Burgess
2023-01-25 13:30               ` Pedro Alves
2022-10-24 10:45 ` Andrew Burgess [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkq1a49n.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=asaffisher.dev@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).