4242#include "bh_log.h"
4343#include "bh_dl_monitor.h"
4444
45+ // block list
46+ typedef struct bh_elf_manager_block
47+ {
48+ char * caller_path_name ;
49+ TAILQ_ENTRY (bh_elf_manager_block ,) link ;
50+ } bh_elf_manager_block_t ;
51+ typedef TAILQ_HEAD (bh_elf_manager_block_list , bh_elf_manager_block ,) bh_elf_manager_block_list_t ;
52+
4553// RB-tree for ELF info (bh_elf_t)
4654static __inline__ int bh_elf_cmp (bh_elf_t * a , bh_elf_t * b )
4755{
@@ -63,6 +71,8 @@ struct bh_elf_manager
6371 size_t elfs_cnt ;
6472 bh_elf_list_t abandoned_elfs ;
6573 pthread_rwlock_t elfs_lock ;
74+ bh_elf_manager_block_list_t blocklist ;
75+ pthread_mutex_t blocklist_lock ;
6676};
6777#pragma clang diagnostic pop
6878
@@ -76,10 +86,75 @@ bh_elf_manager_t *bh_elf_manager_create(void)
7686 self -> elfs_cnt = 0 ;
7787 TAILQ_INIT (& self -> abandoned_elfs );
7888 pthread_rwlock_init (& self -> elfs_lock , NULL );
89+ TAILQ_INIT (& self -> blocklist );
90+ pthread_mutex_init (& self -> blocklist_lock , NULL );
7991
8092 return self ;
8193}
8294
95+ int bh_elf_manager_add_ignore (bh_elf_manager_t * self , const char * caller_path_name )
96+ {
97+ bh_elf_manager_block_t * block ;
98+ if (NULL == (block = calloc (1 , sizeof (bh_elf_t )))) return -1 ;
99+ if (NULL == (block -> caller_path_name = strdup (caller_path_name )))
100+ {
101+ free (block );
102+ return -1 ;
103+ }
104+
105+ bh_elf_manager_block_t * tmp ;
106+ pthread_mutex_lock (& self -> blocklist_lock );
107+ TAILQ_FOREACH (tmp , & self -> blocklist , link )
108+ {
109+ if (0 == strcmp (tmp -> caller_path_name , caller_path_name ))
110+ break ;
111+ }
112+ if (NULL == tmp )
113+ {
114+ TAILQ_INSERT_TAIL (& self -> blocklist , block , link );
115+ block = NULL ;
116+ }
117+ pthread_mutex_unlock (& self -> blocklist_lock );
118+
119+ if (NULL != block )
120+ {
121+ free (block -> caller_path_name );
122+ free (block );
123+ }
124+ return 0 ;
125+ }
126+
127+ static bool bh_elf_manager_check_ignore (bh_elf_manager_t * self , const char * caller_path_name )
128+ {
129+ bool result = true;
130+ pthread_mutex_lock (& self -> blocklist_lock );
131+
132+ bh_elf_manager_block_t * tmp ;
133+ TAILQ_FOREACH (tmp , & self -> blocklist , link )
134+ {
135+ if ('/' == caller_path_name [0 ] && '/' != tmp -> caller_path_name [0 ])
136+ {
137+ if (bh_util_ends_with (caller_path_name , tmp -> caller_path_name ))
138+ goto ignore ;
139+ }
140+ else if ('/' != caller_path_name [0 ] && '/' == tmp -> caller_path_name [0 ])
141+ {
142+ if (bh_util_ends_with (tmp -> caller_path_name , caller_path_name ))
143+ goto ignore ;
144+ }
145+ else
146+ {
147+ if (0 == strcmp (caller_path_name , tmp -> caller_path_name ))
148+ goto ignore ;
149+ }
150+ }
151+ result = false;
152+
153+ ignore :
154+ pthread_mutex_unlock (& self -> blocklist_lock );
155+ return result ;
156+ }
157+
83158static int bh_elf_manager_iterate_cb (struct dl_phdr_info * info , size_t size , void * arg )
84159{
85160 (void )size ;
@@ -91,6 +166,7 @@ static int bh_elf_manager_iterate_cb(struct dl_phdr_info *info, size_t size, voi
91166 // ignore invalid or unwanted ELF
92167 if (bh_util_ends_with (info -> dlpi_name , BH_CONST_BASENAME_BYTEHOOK )) return 0 ;
93168 if (!bh_util_ends_with (info -> dlpi_name , BH_CONST_BASENAME_APP_PROCESS ) && !bh_util_ends_with (info -> dlpi_name , ".so" )) return 0 ;
169+ if (bh_elf_manager_check_ignore (self , info -> dlpi_name )) return 0 ;
94170
95171 bh_elf_t elf_key = {.pathname = info -> dlpi_name };
96172 bh_elf_t * elf = RB_FIND (bh_elf_tree , & self -> elfs , & elf_key );
0 commit comments