[v3,1/2] Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly

Message ID 20221021174205.5389-1-asaffisher.dev@gmail.com
State New
Headers
Series [v3,1/2] Add test to check GDB handles dlopen of /proc/self/fd/[num] correctly |

Commit Message

Asaf Fisher Oct. 21, 2022, 5:42 p.m. UTC
  ---
 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
  

Comments

Andrew Burgess Oct. 24, 2022, 10:45 a.m. UTC | #1
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
  

Patch

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