[14/16] libdw,libdwfl: Use eu-search for thread-safety

Message ID 20231010134300.53830-14-mark@klomp.org
State Changes Requested
Headers
Series [01/16] lib: Add new once_define and once macros to eu-config.h |

Commit Message

Mark Wielaard Oct. 10, 2023, 1:42 p.m. UTC
  From: Heather McIntyre <hsm2@rice.edu>

	* libdw/cie.c: Add eu-search.h and remove search.h.
	Change calls of tsearch/tfind to eu_tsearch/eu_tfind.
	* libdw/dwarf_getlocation.c: Likewise.
	* libdw/dwarf_getmacros.c: Likewise.
	* libdw/dwarf_getsrclines.c: Likewise.
	* libdw/fde.c: Likewise.
	* libdwfl/cu.c: Likewise.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/cie.c               |  8 ++++----
 libdw/dwarf_getlocation.c | 16 ++++++++--------
 libdw/dwarf_getmacros.c   |  6 +++---
 libdw/dwarf_getsrclines.c |  6 +++---
 libdw/fde.c               |  6 +++---
 libdwfl/cu.c              |  4 ++--
 6 files changed, 23 insertions(+), 23 deletions(-)
  

Comments

Mark Wielaard Oct. 12, 2023, 10:05 p.m. UTC | #1
Hi Heather,

On Tue, Oct 10, 2023 at 03:42:58PM +0200, Mark Wielaard wrote:
> 
> 	* libdw/cie.c: Add eu-search.h and remove search.h.
> 	Change calls of tsearch/tfind to eu_tsearch/eu_tfind.
> 	* libdw/dwarf_getlocation.c: Likewise.
> 	* libdw/dwarf_getmacros.c: Likewise.
> 	* libdw/dwarf_getsrclines.c: Likewise.
> 	* libdw/fde.c: Likewise.
> 	* libdwfl/cu.c: Likewise.

These all look good.  Just have to think about whether the global
static lock currently used in eu-search is the right approach.

Cheers,

Mark
  

Patch

diff --git a/libdw/cie.c b/libdw/cie.c
index 1b0aae7c..758daef5 100644
--- a/libdw/cie.c
+++ b/libdw/cie.c
@@ -33,7 +33,7 @@ 
 #include "cfi.h"
 #include "encoded-value.h"
 #include <assert.h>
-#include <search.h>
+#include <eu-search.h>
 #include <stdlib.h>
 
 
@@ -144,7 +144,7 @@  intern_new_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
   cie->initial_state = NULL;
 
   /* Add the new entry to the search tree.  */
-  if (tsearch (cie, &cache->cie_tree, &compare_cie) == NULL)
+  if (eu_tsearch (cie, &cache->cie_tree, &compare_cie) == NULL)
     {
       free (cie);
       __libdw_seterrno (DWARF_E_NOMEM);
@@ -160,7 +160,7 @@  internal_function
 __libdw_find_cie (Dwarf_CFI *cache, Dwarf_Off offset)
 {
   const struct dwarf_cie cie_key = { .offset = offset };
-  struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
+  struct dwarf_cie **found = eu_tfind (&cie_key, &cache->cie_tree, &compare_cie);
   if (found != NULL)
     return *found;
 
@@ -189,7 +189,7 @@  internal_function
 __libdw_intern_cie (Dwarf_CFI *cache, Dwarf_Off offset, const Dwarf_CIE *info)
 {
   const struct dwarf_cie cie_key = { .offset = offset };
-  struct dwarf_cie **found = tfind (&cie_key, &cache->cie_tree, &compare_cie);
+  struct dwarf_cie **found = eu_tfind (&cie_key, &cache->cie_tree, &compare_cie);
   if (found == NULL)
     /* We have not read this CIE yet.  Enter it.  */
     (void) intern_new_cie (cache, offset, info);
diff --git a/libdw/dwarf_getlocation.c b/libdw/dwarf_getlocation.c
index 553fdc98..e26afe37 100644
--- a/libdw/dwarf_getlocation.c
+++ b/libdw/dwarf_getlocation.c
@@ -31,7 +31,7 @@ 
 #endif
 
 #include <dwarf.h>
-#include <search.h>
+#include <eu-search.h>
 #include <stdlib.h>
 #include <assert.h>
 
@@ -137,7 +137,7 @@  loc_compare (const void *p1, const void *p2)
 
 /* For each DW_OP_implicit_value, we store a special entry in the cache.
    This points us directly to the block data for later fetching.
-   Returns zero on success, -1 on bad DWARF or 1 if tsearch failed.  */
+   Returns zero on success, -1 on bad DWARF or 1 if eu_tsearch failed.  */
 static int
 store_implicit_value (Dwarf *dbg, void **cache, Dwarf_Op *op)
 {
@@ -154,7 +154,7 @@  store_implicit_value (Dwarf *dbg, void **cache, Dwarf_Op *op)
   block->addr = op;
   block->data = (unsigned char *) data;
   block->length = op->number;
-  if (unlikely (tsearch (block, cache, loc_compare) == NULL))
+  if (unlikely (eu_tsearch (block, cache, loc_compare) == NULL))
     return 1;
   return 0;
 }
@@ -167,7 +167,7 @@  dwarf_getlocation_implicit_value (Dwarf_Attribute *attr, const Dwarf_Op *op,
     return -1;
 
   struct loc_block_s fake = { .addr = (void *) op };
-  struct loc_block_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
+  struct loc_block_s **found = eu_tfind (&fake, &attr->cu->locs, loc_compare);
   if (unlikely (found == NULL))
     {
       __libdw_seterrno (DWARF_E_NO_BLOCK);
@@ -211,7 +211,7 @@  is_constant_offset (Dwarf_Attribute *attr,
 
   /* Check whether we already cached this location.  */
   struct loc_s fake = { .addr = attr->valp };
-  struct loc_s **found = tfind (&fake, &attr->cu->locs, loc_compare);
+  struct loc_s **found = eu_tfind (&fake, &attr->cu->locs, loc_compare);
 
   if (found == NULL)
     {
@@ -235,7 +235,7 @@  is_constant_offset (Dwarf_Attribute *attr,
       newp->loc = result;
       newp->nloc = 1;
 
-      found = tsearch (newp, &attr->cu->locs, loc_compare);
+      found = eu_tsearch (newp, &attr->cu->locs, loc_compare);
     }
 
   assert ((*found)->nloc == 1);
@@ -266,7 +266,7 @@  __libdw_intern_expression (Dwarf *dbg, bool other_byte_order,
 
   /* Check whether we already looked at this list.  */
   struct loc_s fake = { .addr = block->data };
-  struct loc_s **found = tfind (&fake, cache, loc_compare);
+  struct loc_s **found = eu_tfind (&fake, cache, loc_compare);
   if (found != NULL)
     {
       /* We already saw it.  */
@@ -655,7 +655,7 @@  __libdw_intern_expression (Dwarf *dbg, bool other_byte_order,
   newp->addr = block->data;
   newp->loc = result;
   newp->nloc = *listlen;
-  (void) tsearch (newp, cache, loc_compare);
+  (void) eu_tsearch (newp, cache, loc_compare);
 
   /* We did it.  */
   return 0;
diff --git a/libdw/dwarf_getmacros.c b/libdw/dwarf_getmacros.c
index a3a78884..d150f05e 100644
--- a/libdw/dwarf_getmacros.c
+++ b/libdw/dwarf_getmacros.c
@@ -32,7 +32,7 @@ 
 
 #include <assert.h>
 #include <dwarf.h>
-#include <search.h>
+#include <eu-search.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -293,7 +293,7 @@  cache_op_table (Dwarf *dbg, int sec_index, Dwarf_Off macoff,
 		Dwarf_Die *cudie)
 {
   Dwarf_Macro_Op_Table fake = { .offset = macoff, .sec_index = sec_index };
-  Dwarf_Macro_Op_Table **found = tfind (&fake, &dbg->macro_ops,
+  Dwarf_Macro_Op_Table **found = eu_tfind (&fake, &dbg->macro_ops,
 					macro_op_compare);
   if (found != NULL)
     return *found;
@@ -305,7 +305,7 @@  cache_op_table (Dwarf *dbg, int sec_index, Dwarf_Off macoff,
   if (table == NULL)
     return NULL;
 
-  Dwarf_Macro_Op_Table **ret = tsearch (table, &dbg->macro_ops,
+  Dwarf_Macro_Op_Table **ret = eu_tsearch (table, &dbg->macro_ops,
 					macro_op_compare);
   if (unlikely (ret == NULL))
     {
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index df003c5f..356284a0 100644
--- a/libdw/dwarf_getsrclines.c
+++ b/libdw/dwarf_getsrclines.c
@@ -33,7 +33,7 @@ 
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
-#include <search.h>
+#include <eu-search.h>
 
 #include "dwarf.h"
 #include "libdwP.h"
@@ -1138,7 +1138,7 @@  __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
 		     Dwarf_Lines **linesp, Dwarf_Files **filesp)
 {
   struct files_lines_s fake = { .debug_line_offset = debug_line_offset };
-  struct files_lines_s **found = tfind (&fake, &dbg->files_lines,
+  struct files_lines_s **found = eu_tfind (&fake, &dbg->files_lines,
 					files_lines_compare);
   if (found == NULL)
     {
@@ -1160,7 +1160,7 @@  __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
 
       node->debug_line_offset = debug_line_offset;
 
-      found = tsearch (node, &dbg->files_lines, files_lines_compare);
+      found = eu_tsearch (node, &dbg->files_lines, files_lines_compare);
       if (found == NULL)
 	{
 	  __libdw_seterrno (DWARF_E_NOMEM);
diff --git a/libdw/fde.c b/libdw/fde.c
index 73d551b6..55065528 100644
--- a/libdw/fde.c
+++ b/libdw/fde.c
@@ -31,7 +31,7 @@ 
 #endif
 
 #include "cfi.h"
-#include <search.h>
+#include <eu-search.h>
 #include <stdlib.h>
 
 #include "encoded-value.h"
@@ -122,7 +122,7 @@  intern_fde (Dwarf_CFI *cache, const Dwarf_FDE *entry)
     fde->instructions += cie->fde_augmentation_data_size;
 
   /* Add the new entry to the search tree.  */
-  struct dwarf_fde **tres = tsearch (fde, &cache->fde_tree, &compare_fde);
+  struct dwarf_fde **tres = eu_tsearch (fde, &cache->fde_tree, &compare_fde);
   if (tres == NULL)
     {
       free (fde);
@@ -252,7 +252,7 @@  __libdw_find_fde (Dwarf_CFI *cache, Dwarf_Addr address)
   /* Look for a cached FDE covering this address.  */
 
   const struct dwarf_fde fde_key = { .start = address, .end = 0 };
-  struct dwarf_fde **found = tfind (&fde_key, &cache->fde_tree, &compare_fde);
+  struct dwarf_fde **found = eu_tfind (&fde_key, &cache->fde_tree, &compare_fde);
   if (found != NULL)
     return *found;
 
diff --git a/libdwfl/cu.c b/libdwfl/cu.c
index b1afb19a..71838011 100644
--- a/libdwfl/cu.c
+++ b/libdwfl/cu.c
@@ -33,7 +33,7 @@ 
 #include "libdwflP.h"
 #include "libdwP.h"
 #include "memory-access.h"
-#include <search.h>
+#include <eu-search.h>
 
 
 static inline Dwarf_Arange *
@@ -198,7 +198,7 @@  intern_cu (Dwfl_Module *mod, Dwarf_Off cuoff, struct dwfl_cu **result)
 
   struct dwfl_cu key;
   key.die.cu = die->cu;
-  struct dwfl_cu **found = tsearch (&key, &mod->lazy_cu_root, &compare_cukey);
+  struct dwfl_cu **found = eu_tsearch (&key, &mod->lazy_cu_root, &compare_cukey);
   if (unlikely (found == NULL))
     return DWFL_E_NOMEM;