Skip to contentSkip to conversation

libunwind: refresh ppc32/musl patch

PATCH In review Ross Burton 2 participants 5 messages Apr 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 libunwind: refresh ppc32/musl patch

There was a precedence order problem in the patch so update to the latest version in the pull request.

---
 .../libunwind/0005-Handle-musl-on-PPC32.patch          | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
meta/recipes-support/libunwind/libunwind/0005-Handle-musl-on-PPC32.patch +5 -5
index 7cad7a5b756..63d3c85de49 100644
@@ -30,9 +30,9 @@ index 9444cbb8..5e94ed8a 100644
  
 +/* glibc and musl disagree over the layout of this struct */
 +#ifdef __GLIBC__
-+#define _UC_MCONTEXT_REGS(x) x->uc_mcontext.uc_regs
++#define _UCONTEXT_UC_REGS(uc) uc->uc_mcontext.uc_regs
 +#else
-+#define _UC_MCONTEXT_REGS(x) x->uc_regs
++#define _UCONTEXT_UC_REGS(uc) uc->uc_regs
 +#endif
 +
  static void *
@@ -43,7 +43,7 @@ index 9444cbb8..5e94ed8a 100644
    if ((unsigned) (reg - UNW_PPC32_R0) < 32)
  #if defined(__linux__)
 -    addr = &uc->uc_mcontext.uc_regs->gregs[reg - UNW_PPC32_R0];
-+    addr = _UC_MCONTEXT_REGS(&uc)->gregs[reg - UNW_PPC32_R0];
++    addr = &_UCONTEXT_UC_REGS(uc)->gregs[reg - UNW_PPC32_R0];
  #elif defined(__FreeBSD__)
      addr = &uc->uc_mcontext.mc_gpr[reg - UNW_PPC32_R0];
  #endif
@@ -52,7 +52,7 @@ index 9444cbb8..5e94ed8a 100644
         ((unsigned) (reg - UNW_PPC32_F0) >= 0) )
  #if defined(__linux__)
 -    addr = &uc->uc_mcontext.uc_regs->fpregs.fpregs[reg - UNW_PPC32_F0];
-+    addr = _UC_MCONTEXT_REGS(&uc)->fpregs.fpregs[reg - UNW_PPC32_F0];
++    addr = &_UCONTEXT_UC_REGS(uc)->fpregs.fpregs[reg - UNW_PPC32_F0];
   #elif defined(__FreeBSD__)
      addr = &uc->uc_mcontext.mc_fpreg[reg - UNW_PPC32_F0];
  #endif
@@ -61,7 +61,7 @@ index 9444cbb8..5e94ed8a 100644
          }
  #if defined(__linux__)
 -      addr = &uc->uc_mcontext.uc_regs->gregs[gregs_idx];
-+      addr = _UC_MCONTEXT_REGS(&uc)->gregs[gregs_idx];
++      addr = &_UCONTEXT_UC_REGS(uc)->gregs[gregs_idx];
  #elif defined(__FreeBSD__)
        addr = &uc->uc_mcontext.mc_gpr[gregs_idx];
  #endif
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/4 libunwind: check if libatomic is needed at configure time

Backport a patch submitted upstream to conditionally link explicitly to libatomic, so we don't need to that in the recipe.

---
 .../libunwind/libunwind/libatomic.patch       | 38 +++++++++++++++++++
 .../libunwind/libunwind_1.8.1.bb              |  2 +-
 2 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 meta/recipes-support/libunwind/libunwind/libatomic.patch
meta/recipes-support/libunwind/libunwind/libatomic.patch new file +38 -0
new file mode 100644
index 00000000000..f39e92f2eef
@@ -0,0 +1,38 @@
+From d6a0d8ce07c761e51b9dc7c5d16a9b06c3e93560 Mon Sep 17 00:00:00 2001
+From: Stephen Webb <[email protected]>
+Date: Mon, 18 Mar 2024 10:22:26 -0400
+Subject: [PATCH] configure.ac: detect if -latomic is required
+
+Some OS runtimes require libatomic be linked in separately to get
+standard atomic operations to work. Try to detect that at configure
+time.
+
+Upstream-Status: Submitted [https://github.com/libunwind/libunwind/pull/732/]
+Signed-off-by: Ross Burton <[email protected]>
+---
+ configure.ac | 12 ++++++++++++
+ 1 file changed, 12 insertions(+)
+
+diff --git a/configure.ac b/configure.ac
+index 758a27d19..d0ef5f3d9 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -94,6 +94,18 @@ dnl Checks for library functions.
+ AC_CHECK_FUNCS(dl_iterate_phdr dl_phdr_removals_counter dlmodinfo getunwind \
+ 		ttrace mincore pipe2 sigaltstack execvpe)
+ 
++AC_MSG_CHECKING([if -latomic is required])
++  AC_LINK_IFELSE([AC_LANG_PROGRAM([[
++                        #include <stdatomic.h>
++                        atomic_bool ab_ = 0; atomic_llong al_ = 0;
++                    ]],[[
++                        atomic_load(&ab_); atomic_load(&al_);
++                    ]])],
++                 [use_libatomic=no],
++                 [use_libatomic=yes])
++AC_MSG_RESULT([$use_libatomic])
++AS_IF([test "$use_libatomic" = "yes"],[LIBS="-latomic $LIBS"])
++
+ AC_MSG_CHECKING([if building with AltiVec])
+ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+ #ifndef __ALTIVEC__
meta/recipes-support/libunwind/libunwind_1.8.1.bb +1 -1
index 1885da4d09f..38022569df2 100644
@@ -12,6 +12,7 @@ SRC_URI = "https://github.com/libunwind/libunwind/releases/download/v${PV}/${BP}
            file://0003-Fixed-miscompilation-of-unw_getcontext-on-ARM.patch \
            file://0004-Rework-inline-aarch64-as-for-setcontext.patch \
            file://0005-Handle-musl-on-PPC32.patch \
+           file://libatomic.patch \
            "
 
 SRC_URI[sha256sum] = "ddf0e32dd5fafe5283198d37e4bf9decf7ba1770b6e7e006c33e6df79e6a6157"
@@ -32,7 +33,6 @@ ARM_INSTRUCTION_SET:armv4 = "arm"
 ARM_INSTRUCTION_SET:armv5 = "arm"
 
 LDFLAGS += "-Wl,-z,relro,-z,now"
-LDFLAGS:append:powerpc:libc-musl = " -latomic"
 
 SECURITY_LDFLAGS:append:libc-musl = " -lssp_nonshared"
 CACHED_CONFIGUREVARS:append:libc-musl = " LDFLAGS='${LDFLAGS} -lucontext'"
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 3/4 libunwind: fix build when unused sections are garbage collected

In reply to Ross Burton

poky-tiny uses gcsections.inc to strip unused sections from the binaries. However, on qemuarm5/qemuarm64 with poky-tiny this appears to result in too much being removed and the link fails:

ld: .libs/Gtest-trace: hidden symbol `__aarch64_cas8_acq_rel' in libgcc.a(cas_8_4.o) is referenced by DSO

Work around this by disabling gcsections.inc.

---
 meta/recipes-support/libunwind/libunwind_1.8.1.bb | 4 ++++
 1 file changed, 4 insertions(+)
meta/recipes-support/libunwind/libunwind_1.8.1.bb +4 -0
index 38022569df2..b22cb30646e 100644
@@ -32,6 +32,10 @@ EXTRA_OECONF = "--enable-static"
 ARM_INSTRUCTION_SET:armv4 = "arm"
 ARM_INSTRUCTION_SET:armv5 = "arm"
 
+# With qemuarm64 and poky-tiny:
+# ld: .libs/Gtest-trace: hidden symbol `__aarch64_cas8_acq_rel' in libgcc.a(cas_8_4.o) is referenced by DSO
+LDFLAGS_SECTION_REMOVAL = ""
+
 LDFLAGS += "-Wl,-z,relro,-z,now"
 
 SECURITY_LDFLAGS:append:libc-musl = " -lssp_nonshared"
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 4/4 libunwind: fix the build with GCC 15

In reply to Ross Burton

Backport a patch from upstream to fix the build when GCC 15 is used.

---
 .../libunwind/libunwind/malloc.patch          | 255 ++++++++++++++++++
 .../libunwind/libunwind_1.8.1.bb              |   1 +
 2 files changed, 256 insertions(+)
 create mode 100644 meta/recipes-support/libunwind/libunwind/malloc.patch
meta/recipes-support/libunwind/libunwind/malloc.patch new file +255 -0
new file mode 100644
index 00000000000..026a56f1559
@@ -0,0 +1,255 @@
+From b67d508a93bf1ba231c18dce3894cfee25c16e0d Mon Sep 17 00:00:00 2001
+From: Stephen Webb <[email protected]>
+Date: Wed, 12 Feb 2025 12:08:07 -0500
+Subject: [PATCH] Fix bad prototype for malloc() in test
+
+The unit test Gtest-nomalloc.c had an incorrect prototype for malloc()
+which caused newer compilers to fail compiling for newer C editions.
+
+Corrected the prototype and corrected a few other errors. The unit now
+compiles using GCC with `CFLAGS=-Wall -Wextra -pedantic -C11` with no
+warnings, which is the minimum requirement.
+
+Added the tests/unw_test.h header as a first step to cleaning up some
+unit tests further.
+
+Upstream-Status: Backport [https://github.com/libunwind/libunwind/commit/b67d508]]
+Signed-off-by: Ross Burton <[email protected]>
+---
+ tests/Gtest-nomalloc.c | 124 +++++++++++++++++++++++------------------
+ tests/Makefile.am      |   2 +-
+ tests/unw_test.h       |  47 ++++++++++++++++
+ 3 files changed, 117 insertions(+), 56 deletions(-)
+ create mode 100644 tests/unw_test.h
+
+diff --git a/tests/Gtest-nomalloc.c b/tests/Gtest-nomalloc.c
+index 5b97fc709..e770ff614 100644
+--- a/tests/Gtest-nomalloc.c
++++ b/tests/Gtest-nomalloc.c
+@@ -1,78 +1,92 @@
+-/* libunwind - a platform-independent unwind library
+-   Copyright (C) 2009 Google, Inc
+-	Contributed by Arun Sharma <[email protected]>
++/**
++ * @file tests/Gtest-nomalloc.c
++ *
++ * Verify that @c malloc() is not called during an unwinding operation.
++ */
++/*
++ * This file is part of libunwind.
++ *   Copyright 2025 Stephen M. Webb <[email protected]>
++ *   Copyright (C) 2009 Google, Inc
++ *   Contributed by Arun Sharma <[email protected]>
++ *
++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++ * of this software and associated documentation files (the "Software"), to
++ * deal in the Software without restriction, including without limitation the
++ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++ * sell copies of the Software, and to permit persons to whom the Software is
++ * furnished to do so, subject to the following conditions:
++ *
++ * The above copyright notice and this permission notice shall be included in
++ * all copies or substantial portions of the Software.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++ * IN THE SOFTWARE.
++ */
+ 
+-Permission is hereby granted, free of charge, to any person obtaining
+-a copy of this software and associated documentation files (the
+-"Software"), to deal in the Software without restriction, including
+-without limitation the rights to use, copy, modify, merge, publish,
+-distribute, sublicense, and/or sell copies of the Software, and to
+-permit persons to whom the Software is furnished to do so, subject to
+-the following conditions:
+-
+-The above copyright notice and this permission notice shall be
+-included in all copies or substantial portions of the Software.
+-
+-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
+-
+-#include <unistd.h>
+-#include <stdio.h>
+-#include <stdlib.h>
+ #include <dlfcn.h>
+ #include <libunwind.h>
++#include <stdio.h>
++#include <stdlib.h>
++#include <unistd.h>
+ 
+-#define panic(args...)				\
+-	{ fprintf (stderr, args); exit (-1); }
++#include "unw_test.h"
+ 
+-int verbose;
+-int num_errors;
++int malloc_call_count;
+ int in_unwind;
+ 
++/**
++ * Intercepted malloc() call.
++ *
++ * If invoked during unwinding this call will increment the test error count
++ * and indicate a failure by returning NULL. Otherwise it just calls the real
++ * malloc().
++ */
+ void *
+-malloc(size_t s)
++malloc (size_t sz)
+ {
+-  static void * (*func)();
++  typedef void *(*malloc_t) (size_t);
+ 
+-  if(!func)
+-    func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
++  static malloc_t real_malloc = NULL;
++  if (real_malloc == NULL)
++    {
++      real_malloc = (malloc_t)(intptr_t)dlsym (RTLD_NEXT, "malloc");
++      if (real_malloc == NULL)
++        {
++          fprintf (stderr, "no malloc() found\n");
++          exit (UNW_TEST_EXIT_HARD_ERROR);                                          \
++        }
++    }
+ 
+-  if (in_unwind) {
+-    num_errors++;
+-    return NULL;
+-  } else {
+-    return func(s);
+-  }
++  if (in_unwind)
++    {
++      malloc_call_count++;
++    }
++  return real_malloc (sz);
+ }
+ 
+ static void
+ do_backtrace (void)
+ {
+-  unw_word_t ip, sp;
+-  unw_cursor_t cursor;
++  unw_cursor_t  cursor;
+   unw_context_t uc;
+-  int ret;
++  int           ret;
+ 
+   in_unwind = 1;
+   unw_getcontext (&uc);
+   if (unw_init_local (&cursor, &uc) < 0)
+-    panic ("unw_init_local failed!\n");
++    {
++      fprintf (stderr, "unw_init_local failed!\n");
++      exit (UNW_TEST_EXIT_HARD_ERROR);                                          \
++    }
+ 
+   do
+     {
+-      unw_get_reg (&cursor, UNW_REG_IP, &ip);
+-      unw_get_reg (&cursor, UNW_REG_SP, &sp);
+-
+       ret = unw_step (&cursor);
+-      if (ret < 0)
+-	{
+-	  ++num_errors;
+-	}
+     }
+   while (ret > 0);
+   in_unwind = 0;
+@@ -99,12 +113,12 @@ foo1 (void)
+ int
+ main (void)
+ {
+-  foo1();
++  foo1 ();
+ 
+-  if (num_errors > 0)
++  if (malloc_call_count > 0)
+     {
+-      fprintf (stderr, "FAILURE: detected %d errors\n", num_errors);
+-      exit (-1);
++      fprintf (stderr, "FAILURE: malloc called %d times, expected 0\n", malloc_call_count);
++      exit (UNW_TEST_EXIT_FAIL);
+     }
+-  return 0;
++  exit (UNW_TEST_EXIT_PASS);
+ }
+diff --git a/tests/Makefile.am b/tests/Makefile.am
+index adc34ac63..60f3f3adc 100644
+--- a/tests/Makefile.am
++++ b/tests/Makefile.am
+@@ -214,7 +214,7 @@ endif
+ 
+ noinst_PROGRAMS = $(noinst_PROGRAMS_common) $(noinst_PROGRAMS_cdep) \
+ 	$(noinst_PROGRAMS_arch)
+-noinst_HEADERS = ident.h
++noinst_HEADERS = ident.h unw_test.h
+ 
+ do_test_subst = sed -e 's,[@]TESTS[@],$(TESTS),g' \
+                     -e 's,[@]XFAIL_TESTS[@],$(XFAIL_TESTS),g' \
+diff --git a/tests/unw_test.h b/tests/unw_test.h
+new file mode 100644
+index 000000000..9ae86dce1
+--- /dev/null
++++ b/tests/unw_test.h
+@@ -0,0 +1,47 @@
++/**
++ * @file tests/unw_test.h
++ *
++ * Common unit test API for libunwind.
++ */
++/*
++ * This file is part of libunwind.
++ *   Copyright 2025 Stephen M. Webb <[email protected]>
++ *
++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++ * of this software and associated documentation files (the "Software"), to
++ * deal in the Software without restriction, including without limitation the
++ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++ * sell copies of the Software, and to permit persons to whom the Software is
++ * furnished to do so, subject to the following conditions:
++ *
++ * The above copyright notice and this permission notice shall be included in
++ * all copies or substantial portions of the Software.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++ * IN THE SOFTWARE.
++ */
++#ifndef LIBUNWIND_UNW_TEST_H
++#define LIBUNWIND_UNW_TEST_H 1
++
++/**
++ * Exit values for test programs.
++ * Based on https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html
++ *
++ * These are used to interact with the test harness (eg. a TAP-based harness,
++ * CTest, or automake).
++ */
++enum {
++  UNW_TEST_EXIT_PASS        =  0,  /* Item under test is a PASS */
++  UNW_TEST_EXIT_FAIL        =  1,  /* Item under test is a FAIL */
++  UNW_TEST_EXIT_BAD_COMMAND =  2,  /* Test program is invoked with invalid arguments */
++  UNW_TEST_EXIT_SKIP        = 77,  /* Test should be skipped */
++  UNW_TEST_EXIT_HARD_ERROR  = 99   /* Test program itself has failed */
++};
++
++#endif /* LIBUNWIND_UNW_TEST_H */
++
meta/recipes-support/libunwind/libunwind_1.8.1.bb +1 -0
index b22cb30646e..caa110c6da4 100644
@@ -13,6 +13,7 @@ SRC_URI = "https://github.com/libunwind/libunwind/releases/download/v${PV}/${BP}
            file://0004-Rework-inline-aarch64-as-for-setcontext.patch \
            file://0005-Handle-musl-on-PPC32.patch \
            file://libatomic.patch \
+           file://malloc.patch \
            "
 
 SRC_URI[sha256sum] = "ddf0e32dd5fafe5283198d37e4bf9decf7ba1770b6e7e006c33e6df79e6a6157"
Signature · 1 line
2.43.0
Acked-by on v2 4/4
#4 lore raw

Re: [OE-core] [PATCH v2 4/4] libunwind: fix the build with GCC 15

Thank you.

Quoted 295 lines · Ross Burton via lists.openembedded.org

Backport a patch from upstream to fix the build when GCC 15 is used.

---
 .../libunwind/libunwind/malloc.patch          | 255 ++++++++++++++++++
 .../libunwind/libunwind_1.8.1.bb              |   1 +
 2 files changed, 256 insertions(+)
 create mode 100644 meta/recipes-support/libunwind/libunwind/malloc.patch
meta/recipes-support/libunwind/libunwind/malloc.patch new file +255 -0
new file mode 100644
index 00000000000..026a56f1559
@@ -0,0 +1,255 @@
+From b67d508a93bf1ba231c18dce3894cfee25c16e0d Mon Sep 17 00:00:00 2001
+From: Stephen Webb <[email protected]>
+Date: Wed, 12 Feb 2025 12:08:07 -0500
+Subject: [PATCH] Fix bad prototype for malloc() in test
+
+The unit test Gtest-nomalloc.c had an incorrect prototype for malloc()
+which caused newer compilers to fail compiling for newer C editions.
+
+Corrected the prototype and corrected a few other errors. The unit now
+compiles using GCC with `CFLAGS=-Wall -Wextra -pedantic -C11` with no
+warnings, which is the minimum requirement.
+
+Added the tests/unw_test.h header as a first step to cleaning up some
+unit tests further.
+
+Upstream-Status: Backport [https://github.com/libunwind/libunwind/commit/b67d508]]
+Signed-off-by: Ross Burton <[email protected]>
+---
+ tests/Gtest-nomalloc.c | 124 +++++++++++++++++++++++------------------
+ tests/Makefile.am      |   2 +-
+ tests/unw_test.h       |  47 ++++++++++++++++
+ 3 files changed, 117 insertions(+), 56 deletions(-)
+ create mode 100644 tests/unw_test.h
+
+diff --git a/tests/Gtest-nomalloc.c b/tests/Gtest-nomalloc.c
+index 5b97fc709..e770ff614 100644
+--- a/tests/Gtest-nomalloc.c
++++ b/tests/Gtest-nomalloc.c
+@@ -1,78 +1,92 @@
+-/* libunwind - a platform-independent unwind library
+-   Copyright (C) 2009 Google, Inc
+-      Contributed by Arun Sharma <[email protected]>
++/**
++ * @file tests/Gtest-nomalloc.c
++ *
++ * Verify that @c malloc() is not called during an unwinding operation.
++ */
++/*
++ * This file is part of libunwind.
++ *   Copyright 2025 Stephen M. Webb <[email protected]>
++ *   Copyright (C) 2009 Google, Inc
++ *   Contributed by Arun Sharma <[email protected]>
++ *
++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++ * of this software and associated documentation files (the "Software"), to
++ * deal in the Software without restriction, including without limitation the
++ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++ * sell copies of the Software, and to permit persons to whom the Software is
++ * furnished to do so, subject to the following conditions:
++ *
++ * The above copyright notice and this permission notice shall be included in
++ * all copies or substantial portions of the Software.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++ * IN THE SOFTWARE.
++ */
+
+-Permission is hereby granted, free of charge, to any person obtaining
+-a copy of this software and associated documentation files (the
+-"Software"), to deal in the Software without restriction, including
+-without limitation the rights to use, copy, modify, merge, publish,
+-distribute, sublicense, and/or sell copies of the Software, and to
+-permit persons to whom the Software is furnished to do so, subject to
+-the following conditions:
+-
+-The above copyright notice and this permission notice shall be
+-included in all copies or substantial portions of the Software.
+-
+-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
+-
+-#include <unistd.h>
+-#include <stdio.h>
+-#include <stdlib.h>
+ #include <dlfcn.h>
+ #include <libunwind.h>
++#include <stdio.h>
++#include <stdlib.h>
++#include <unistd.h>
+
+-#define panic(args...)                                \
+-      { fprintf (stderr, args); exit (-1); }
++#include "unw_test.h"
+
+-int verbose;
+-int num_errors;
++int malloc_call_count;
+ int in_unwind;
+
++/**
++ * Intercepted malloc() call.
++ *
++ * If invoked during unwinding this call will increment the test error count
++ * and indicate a failure by returning NULL. Otherwise it just calls the real
++ * malloc().
++ */
+ void *
+-malloc(size_t s)
++malloc (size_t sz)
+ {
+-  static void * (*func)();
++  typedef void *(*malloc_t) (size_t);
+
+-  if(!func)
+-    func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
++  static malloc_t real_malloc = NULL;
++  if (real_malloc == NULL)
++    {
++      real_malloc = (malloc_t)(intptr_t)dlsym (RTLD_NEXT, "malloc");
++      if (real_malloc == NULL)
++        {
++          fprintf (stderr, "no malloc() found\n");
++          exit (UNW_TEST_EXIT_HARD_ERROR);                                          \
++        }
++    }
+
+-  if (in_unwind) {
+-    num_errors++;
+-    return NULL;
+-  } else {
+-    return func(s);
+-  }
++  if (in_unwind)
++    {
++      malloc_call_count++;
++    }
++  return real_malloc (sz);
+ }
+
+ static void
+ do_backtrace (void)
+ {
+-  unw_word_t ip, sp;
+-  unw_cursor_t cursor;
++  unw_cursor_t  cursor;
+   unw_context_t uc;
+-  int ret;
++  int           ret;
+
+   in_unwind = 1;
+   unw_getcontext (&uc);
+   if (unw_init_local (&cursor, &uc) < 0)
+-    panic ("unw_init_local failed!\n");
++    {
++      fprintf (stderr, "unw_init_local failed!\n");
++      exit (UNW_TEST_EXIT_HARD_ERROR);                                          \
++    }
+
+   do
+     {
+-      unw_get_reg (&cursor, UNW_REG_IP, &ip);
+-      unw_get_reg (&cursor, UNW_REG_SP, &sp);
+-
+       ret = unw_step (&cursor);
+-      if (ret < 0)
+-      {
+-        ++num_errors;
+-      }
+     }
+   while (ret > 0);
+   in_unwind = 0;
+@@ -99,12 +113,12 @@ foo1 (void)
+ int
+ main (void)
+ {
+-  foo1();
++  foo1 ();
+
+-  if (num_errors > 0)
++  if (malloc_call_count > 0)
+     {
+-      fprintf (stderr, "FAILURE: detected %d errors\n", num_errors);
+-      exit (-1);
++      fprintf (stderr, "FAILURE: malloc called %d times, expected 0\n", malloc_call_count);
++      exit (UNW_TEST_EXIT_FAIL);
+     }
+-  return 0;
++  exit (UNW_TEST_EXIT_PASS);
+ }
+diff --git a/tests/Makefile.am b/tests/Makefile.am
+index adc34ac63..60f3f3adc 100644
+--- a/tests/Makefile.am
++++ b/tests/Makefile.am
+@@ -214,7 +214,7 @@ endif
+
+ noinst_PROGRAMS = $(noinst_PROGRAMS_common) $(noinst_PROGRAMS_cdep) \
+       $(noinst_PROGRAMS_arch)
+-noinst_HEADERS = ident.h
++noinst_HEADERS = ident.h unw_test.h
+
+ do_test_subst = sed -e 's,[@]TESTS[@],$(TESTS),g' \
+                     -e 's,[@]XFAIL_TESTS[@],$(XFAIL_TESTS),g' \
+diff --git a/tests/unw_test.h b/tests/unw_test.h
+new file mode 100644
+index 000000000..9ae86dce1
+--- /dev/null
++++ b/tests/unw_test.h
+@@ -0,0 +1,47 @@
++/**
++ * @file tests/unw_test.h
++ *
++ * Common unit test API for libunwind.
++ */
++/*
++ * This file is part of libunwind.
++ *   Copyright 2025 Stephen M. Webb <[email protected]>
++ *
++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++ * of this software and associated documentation files (the "Software"), to
++ * deal in the Software without restriction, including without limitation the
++ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++ * sell copies of the Software, and to permit persons to whom the Software is
++ * furnished to do so, subject to the following conditions:
++ *
++ * The above copyright notice and this permission notice shall be included in
++ * all copies or substantial portions of the Software.
++ *
++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++ * IN THE SOFTWARE.
++ */
++#ifndef LIBUNWIND_UNW_TEST_H
++#define LIBUNWIND_UNW_TEST_H 1
++
++/**
++ * Exit values for test programs.
++ * Based on https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html
++ *
++ * These are used to interact with the test harness (eg. a TAP-based harness,
++ * CTest, or automake).
++ */
++enum {
++  UNW_TEST_EXIT_PASS        =  0,  /* Item under test is a PASS */
++  UNW_TEST_EXIT_FAIL        =  1,  /* Item under test is a FAIL */
++  UNW_TEST_EXIT_BAD_COMMAND =  2,  /* Test program is invoked with invalid arguments */
++  UNW_TEST_EXIT_SKIP        = 77,  /* Test should be skipped */
++  UNW_TEST_EXIT_HARD_ERROR  = 99   /* Test program itself has failed */
++};
++
++#endif /* LIBUNWIND_UNW_TEST_H */
++
meta/recipes-support/libunwind/libunwind_1.8.1.bb +1 -1
index b22cb30646e..caa110c6da4 100644
@@ -13,6 +13,7 @@ SRC_URI = "https://github.com/libunwind/libunwind/releases/download/v${PV}/${BP}
            file://0004-Rework-inline-aarch64-as-for-setcontext.patch \
            file://0005-Handle-musl-on-PPC32.patch \
            file://libatomic.patch \
+           file://malloc.patch \
            "
 
 SRC_URI[sha256sum] = "ddf0e32dd5fafe5283198d37e4bf9decf7ba1770b6e7e006c33e6df79e6a6157"
--

2.43.0

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#215463): https://lists.openembedded.org/g/openembedded-core/message/215463
Mute This Topic: https://lists.openembedded.org/mt/112449328/3617156
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-