DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] usertools/pmdinfo: remove dependency to ldd
@ 2022-10-13 13:41 Robin Jarry
  2022-10-14 14:02 ` Olivier Matz
  0 siblings, 1 reply; 3+ messages in thread
From: Robin Jarry @ 2022-10-13 13:41 UTC (permalink / raw)
  To: dev; +Cc: Robin Jarry, Olivier Matz

Some environments (buildroot) do not have the ldd utility installed by
default. However, ldd is often only a wrapper shell script that actually
checks that the arguments are valid ELF files and executes them with
the LD_TRACE_LOADED_OBJECTS=1 variable set in the environment.

Since ld.so is the actual ELF interpreter which is loaded first when
executing a program, executing any dynamic ELF program/library with that
variable set will cause all dependent dynamic libraries to be printed
and ld.so will exit before even running main.

Excerpt from ld.so(7) man page:

  LD_TRACE_LOADED_OBJECTS
    If set (to any value), causes the program to list its dynamic
    dependencies, as if run by ldd(1), instead of running normally.

Change dpdk-pmdinfo.py to actually "execute" the files provided on the
command line with LD_TRACE_LOADED_OBJECTS=1 set. Ensure that the files
are valid dynamically executable ELF programs to avoid obscure and
confusing errors.

Reported-by: Olivier Matz <olivier.matz@6wind.com>
Signed-off-by: Robin Jarry <rjarry@redhat.com>
---
 doc/guides/tools/pmdinfo.rst |  4 ++--
 usertools/dpdk-pmdinfo.py    | 34 +++++++++++++++++++++++-----------
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/doc/guides/tools/pmdinfo.rst b/doc/guides/tools/pmdinfo.rst
index a9217de4eef2..1406b9c442eb 100644
--- a/doc/guides/tools/pmdinfo.rst
+++ b/doc/guides/tools/pmdinfo.rst
@@ -37,8 +37,8 @@ Arguments
 
 .. option:: ELF_FILE
 
-   DPDK application binary or dynamic library.
-   Any linked ``librte_*.so`` library (as reported by ``ldd``) will also be analyzed.
+   Executable DPDK application binary or dynamic library.
+   Any linked ``librte_*.so`` library (as reported by ``ld.so``) will also be analyzed.
    Can be specified multiple times.
 
 Environment Variables
diff --git a/usertools/dpdk-pmdinfo.py b/usertools/dpdk-pmdinfo.py
index 67d023a04711..01bb90666bcc 100755
--- a/usertools/dpdk-pmdinfo.py
+++ b/usertools/dpdk-pmdinfo.py
@@ -97,9 +97,9 @@ def parse_args() -> argparse.Namespace:
         "elf_files",
         metavar="ELF_FILE",
         nargs="+",
-        type=existing_file,
+        type=executable_elf_file,
         help="""
-        DPDK application binary or dynamic library.
+        Executable DPDK application binary or dynamic library.
         """,
     )
     return parser.parse_args()
@@ -180,14 +180,24 @@ def get_plugin_libs(binaries: Iterable[Path]) -> Iterator[Path]:
 
 
 # ----------------------------------------------------------------------------
-def existing_file(value: str) -> Path:
+def executable_elf_file(value: str) -> Path:
     """
-    Argparse type= callback to ensure an argument points to a valid file path.
+    Argparse type= callback to ensure an argument points to a valid ELF file
+    path which can be executed.
     """
-    path = Path(value)
-    if not path.is_file():
-        raise argparse.ArgumentTypeError(f"{value}: No such file")
-    return path
+    try:
+        with open(value, "rb") as f:
+            elf = ELFFile(f)
+            if elf.header.e_type not in ("ET_DYN", "ET_EXEC"):
+                raise ELFError(f"unknown type: {elf.header.e_type!r}")
+        if not os.access(value, os.X_OK):
+            raise OSError("is not executable")
+    except ELFError as e:
+        raise argparse.ArgumentTypeError(f"{value}: invalid ELF: {e}") from e
+    except OSError as e:
+        raise argparse.ArgumentTypeError(f"{value}: {e}") from e
+
+    return Path(value)
 
 
 # ----------------------------------------------------------------------------
@@ -270,7 +280,7 @@ def get_elf_strings(path: Path, section: str, prefix: str) -> Iterator[str]:
 
 
 # ----------------------------------------------------------------------------
-LDD_LIB_RE = re.compile(
+LOADED_OBJECT_RE = re.compile(
     r"""
     ^                  # beginning of line
     \t                 # tab
@@ -290,14 +300,16 @@ def get_needed_libs(path: Path) -> Iterator[Path]:
     """
     Extract the dynamic library dependencies from an ELF executable.
     """
+    env = os.environ.copy()
+    env["LD_TRACE_LOADED_OBJECTS"] = "1"
     with subprocess.Popen(
-        ["ldd", str(path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE
+        [str(path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
     ) as proc:
         out, err = proc.communicate()
         if proc.returncode != 0:
             err = err.decode("utf-8").splitlines()[-1].strip()
             raise Exception(f"cannot read ELF file: {err}")
-        for match in LDD_LIB_RE.finditer(out.decode("utf-8")):
+        for match in LOADED_OBJECT_RE.finditer(out.decode("utf-8")):
             libname, libpath = match.groups()
             if libname.startswith("librte_"):
                 libpath = Path(libpath)
-- 
2.37.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usertools/pmdinfo: remove dependency to ldd
  2022-10-13 13:41 [PATCH] usertools/pmdinfo: remove dependency to ldd Robin Jarry
@ 2022-10-14 14:02 ` Olivier Matz
  2022-10-18  7:40   ` Olivier Matz
  0 siblings, 1 reply; 3+ messages in thread
From: Olivier Matz @ 2022-10-14 14:02 UTC (permalink / raw)
  To: Robin Jarry; +Cc: dev

Hi Robin,

On Thu, Oct 13, 2022 at 03:41:25PM +0200, Robin Jarry wrote:
> Some environments (buildroot) do not have the ldd utility installed by
> default. However, ldd is often only a wrapper shell script that actually
> checks that the arguments are valid ELF files and executes them with
> the LD_TRACE_LOADED_OBJECTS=1 variable set in the environment.
> 
> Since ld.so is the actual ELF interpreter which is loaded first when
> executing a program, executing any dynamic ELF program/library with that
> variable set will cause all dependent dynamic libraries to be printed
> and ld.so will exit before even running main.
> 
> Excerpt from ld.so(7) man page:
> 
>   LD_TRACE_LOADED_OBJECTS
>     If set (to any value), causes the program to list its dynamic
>     dependencies, as if run by ldd(1), instead of running normally.
> 
> Change dpdk-pmdinfo.py to actually "execute" the files provided on the
> command line with LD_TRACE_LOADED_OBJECTS=1 set. Ensure that the files
> are valid dynamically executable ELF programs to avoid obscure and
> confusing errors.
> 
> Reported-by: Olivier Matz <olivier.matz@6wind.com>
> Signed-off-by: Robin Jarry <rjarry@redhat.com>

Tested on buildroot without ldd.

Reviewed-by: Olivier Matz <olivier.matz@6wind.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usertools/pmdinfo: remove dependency to ldd
  2022-10-14 14:02 ` Olivier Matz
@ 2022-10-18  7:40   ` Olivier Matz
  0 siblings, 0 replies; 3+ messages in thread
From: Olivier Matz @ 2022-10-18  7:40 UTC (permalink / raw)
  To: Robin Jarry; +Cc: dev

On Fri, Oct 14, 2022 at 04:02:02PM +0200, Olivier Matz wrote:
> Hi Robin,
> 
> On Thu, Oct 13, 2022 at 03:41:25PM +0200, Robin Jarry wrote:
> > Some environments (buildroot) do not have the ldd utility installed by
> > default. However, ldd is often only a wrapper shell script that actually
> > checks that the arguments are valid ELF files and executes them with
> > the LD_TRACE_LOADED_OBJECTS=1 variable set in the environment.
> > 
> > Since ld.so is the actual ELF interpreter which is loaded first when
> > executing a program, executing any dynamic ELF program/library with that
> > variable set will cause all dependent dynamic libraries to be printed
> > and ld.so will exit before even running main.
> > 
> > Excerpt from ld.so(7) man page:
> > 
> >   LD_TRACE_LOADED_OBJECTS
> >     If set (to any value), causes the program to list its dynamic
> >     dependencies, as if run by ldd(1), instead of running normally.
> > 
> > Change dpdk-pmdinfo.py to actually "execute" the files provided on the
> > command line with LD_TRACE_LOADED_OBJECTS=1 set. Ensure that the files
> > are valid dynamically executable ELF programs to avoid obscure and
> > confusing errors.
> > 
> > Reported-by: Olivier Matz <olivier.matz@6wind.com>
> > Signed-off-by: Robin Jarry <rjarry@redhat.com>
> 
> Tested on buildroot without ldd.
> 
> Reviewed-by: Olivier Matz <olivier.matz@6wind.com>

After some more tests, it appears that it only works for an executable
binary, but it does not work for .so PMDs:

# LD_TRACE_LOADED_OBJECTS=1 /usr/lib/x86_64-linux-gnu/dpdk/pmds-22.0/librte_net_ixgbe.so
Segmentation fault (core dumped)

So it is maybe better to keep the original code using ldd.

Thank you Robin anyway.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-18  7:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-13 13:41 [PATCH] usertools/pmdinfo: remove dependency to ldd Robin Jarry
2022-10-14 14:02 ` Olivier Matz
2022-10-18  7:40   ` Olivier Matz

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 NNTP newsgroup(s).