Skip to content

Commit 16140b1

Browse files
authored
Update GOTCHA (#641)
1 parent e3a3331 commit 16140b1

11 files changed

+108
-101
lines changed

ext/gotcha/src/elf_ops.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ static uint32_t gnu_hash_func(const char *str) {
4848
signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
4949
ElfW(Half) * versym, char *symnames,
5050
void *sheader) {
51-
uint32_t *buckets, *vals;
52-
uint32_t hash_val;
53-
uint32_t cur_sym, cur_sym_hashval;
51+
uint32_t *buckets = NULL, *vals = NULL;
52+
uint32_t hash_val = 0;
53+
uint32_t cur_sym = 0, cur_sym_hashval = 0;
5454
signed long latest_sym = -1;
5555
ElfW(Half) latest_sym_ver = 0;
5656
struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader);
@@ -89,7 +89,7 @@ signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
8989
}
9090

9191
static unsigned long elf_hash(const unsigned char *name) {
92-
unsigned int h = 0, g;
92+
unsigned int h = 0, g = 0;
9393
while (*name != '\0') {
9494
h = (h << 4) + *name++;
9595
if ((g = h & 0xf0000000)) {

ext/gotcha/src/gotcha.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void setInternalBindingAddressPointer(void **in, void *value) {
5454

5555
long lookup_exported_symbol(const char *name, const struct link_map *lib,
5656
void **symbol) {
57-
long result;
57+
long result = 0;
5858
if (is_vdso(lib)) {
5959
debug_printf(2, "Skipping VDSO library at 0x%lx with name %s\n",
6060
lib->l_addr, LIB_NAME(lib));
@@ -96,8 +96,8 @@ long lookup_exported_symbol(const char *name, const struct link_map *lib,
9696
}
9797

9898
int prepare_symbol(struct internal_binding_t *binding) {
99-
int result;
100-
struct link_map *lib;
99+
int result = 0;
100+
struct link_map *lib = NULL;
101101
struct gotcha_binding_t *user_binding = binding->user_binding;
102102

103103
debug_printf(2, "Looking up exported symbols for %s\n", user_binding->name);
@@ -162,7 +162,7 @@ static int rewrite_wrapper_orders(struct internal_binding_t *binding) {
162162
insert_priority);
163163

164164
struct internal_binding_t *head;
165-
int hash_result;
165+
int hash_result = 0;
166166
hash_result =
167167
lookup_hashtable(&function_hash_table, (void *)name, (void **)&head);
168168
if (hash_result != 0) {
@@ -209,9 +209,9 @@ static int rewrite_wrapper_orders(struct internal_binding_t *binding) {
209209
static int update_lib_bindings(ElfW(Sym) * symbol KNOWN_UNUSED, char *name,
210210
ElfW(Addr) offset, struct link_map *lmap,
211211
hash_table_t *lookuptable) {
212-
int result;
213-
struct internal_binding_t *internal_binding;
214-
void **got_address;
212+
int result = 0;
213+
struct internal_binding_t *internal_binding = NULL;
214+
void **got_address = NULL;
215215

216216
result = lookup_hashtable(lookuptable, name, (void **)&internal_binding);
217217
if (result != 0) return -1;
@@ -391,7 +391,7 @@ static int update_library_got(struct link_map *map,
391391
}
392392

393393
void update_all_library_gots(hash_table_t *bindings) {
394-
struct link_map *lib_iter;
394+
struct link_map *lib_iter = NULL;
395395
debug_printf(2, "Searching all callsites for %lu bindings\n",
396396
(unsigned long)bindings->entry_count);
397397
for (lib_iter = _r_debug.r_map; lib_iter != 0; lib_iter = lib_iter->l_next) {
@@ -402,9 +402,9 @@ void update_all_library_gots(hash_table_t *bindings) {
402402
GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(
403403
struct gotcha_binding_t *user_bindings, int num_actions,
404404
const char *tool_name) {
405-
int i, not_found = 0, new_bindings_count = 0;
406-
tool_t *tool;
407-
hash_table_t new_bindings;
405+
int i = 0, not_found = 0, new_bindings_count = 0;
406+
tool_t *tool = NULL;
407+
hash_table_t new_bindings = EMPTY_HASH_TABLE;
408408

409409
gotcha_init();
410410

ext/gotcha/src/gotcha_auxv.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ static unsigned int auxv_pagesz = 0;
3535

3636
int parse_auxv_contents() {
3737
char name[] = "/proc/self/auxv";
38-
int fd, done = 0;
39-
char buffer[BUFFER_LEN];
38+
int fd = -1, done = 0;
39+
char buffer[BUFFER_LEN] = {'\0'};
4040
const ssize_t buffer_size = BUFFER_LEN;
41-
ssize_t offset = 0, result;
42-
ElfW(auxv_t) * auxv, *a;
41+
ssize_t offset = 0, result = 0;
42+
ElfW(auxv_t) *auxv = NULL, *a = NULL;
4343
static int parsed_auxv = 0;
4444

4545
if (parsed_auxv) return parsed_auxv == -1 ? parsed_auxv : 0;
@@ -86,7 +86,7 @@ int parse_auxv_contents() {
8686
}
8787

8888
struct link_map *get_vdso_from_auxv() {
89-
struct link_map *m;
89+
struct link_map *m = NULL;
9090

9191
ElfW(Phdr) *vdso_phdrs = NULL;
9292
ElfW(Half) vdso_phdr_num, p;
@@ -114,16 +114,16 @@ struct link_map *get_vdso_from_auxv() {
114114
}
115115

116116
unsigned int get_auxv_pagesize() {
117-
int result;
117+
int result = 0;
118118
result = parse_auxv_contents();
119119
return result == -1 ? 0 : auxv_pagesz;
120120
}
121121

122122
static char *vdso_aliases[] = {"linux-vdso.so", "linux-gate.so", NULL};
123123

124124
struct link_map *get_vdso_from_aliases() {
125-
struct link_map *m;
126-
char **aliases;
125+
struct link_map *m = NULL;
126+
char **aliases = NULL;
127127

128128
for (m = _r_debug.r_map; m; m = m->l_next) {
129129
for (aliases = vdso_aliases; *aliases; aliases++) {
@@ -136,7 +136,7 @@ struct link_map *get_vdso_from_aliases() {
136136
}
137137

138138
static int read_line(char *line, int size, int fd) {
139-
int i;
139+
int i = 0;
140140
for (i = 0; i < size - 1; i++) {
141141
int result = gotcha_read(fd, line + i, 1);
142142
if (result == -1 && errno == EINTR) continue; // GCOVR_EXCL_LINE
@@ -201,10 +201,10 @@ static int read_word(char *str, char *word, int word_size) {
201201
}
202202

203203
struct link_map *get_vdso_from_maps() {
204-
int maps, hit_eof;
205-
ElfW(Addr) addr_begin, addr_end, dynamic;
206-
char name[BUFFER_LEN], line[BUFFER_LEN], *line_pos;
207-
struct link_map *m;
204+
int maps = 0, hit_eof = 0;
205+
ElfW(Addr) addr_begin = 0, addr_end = 0, dynamic = 0;
206+
char name[BUFFER_LEN] = {'\0'}, line[BUFFER_LEN] = {'\0'}, *line_pos = NULL;
207+
struct link_map *m = NULL;
208208
maps = gotcha_open("/proc/self/maps", O_RDONLY);
209209
for (;;) {
210210
hit_eof = read_line(line, BUFFER_LEN, maps);
@@ -239,7 +239,7 @@ struct link_map *get_vdso_from_maps() {
239239
int is_vdso(const struct link_map *map) {
240240
static int vdso_checked = 0;
241241
static struct link_map *vdso = NULL;
242-
struct link_map *result;
242+
struct link_map *result = NULL;
243243

244244
if (!map) return 0;
245245
if (vdso_checked) return (map == vdso);

ext/gotcha/src/gotcha_dl.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct Addrs {
1515
struct link_map *lmap; // output
1616
int found;
1717
};
18+
#define EMPTY_ADDRS \
19+
{0, NULL, 0}
1820
/**
1921
* This is a callback to get headers for each library.
2022
* We check if the caller's virtual address is between base address and the
@@ -29,7 +31,7 @@ struct Addrs {
2931
int lib_header_callback(struct dl_phdr_info *info, size_t size, void *data) {
3032
struct Addrs *addrs = data;
3133
const char *name = NULL;
32-
ElfW(Addr) load_address;
34+
ElfW(Addr) load_address = 0;
3335
for (int i = 0; i < info->dlpi_phnum; ++i) {
3436
if (info->dlpi_phdr[i].p_type == PT_LOAD) {
3537
ElfW(Addr) base_addr = info->dlpi_addr;
@@ -73,11 +75,11 @@ static struct link_map *gotchas_dlsym_rtld_next_lookup(const char *name,
7375
/* Iterative over the library headers and find the caller
7476
* the address of the caller is set in addrs->library_laddr
7577
**/
76-
struct Addrs addrs;
78+
struct Addrs addrs = EMPTY_ADDRS;
7779
addrs.lookup_addr = caller;
7880
addrs.lmap = _r_debug.r_map;
7981
addrs.found = 0;
80-
void *symbol;
82+
void *symbol = NULL;
8183
dl_iterate_phdr(lib_header_callback, &addrs);
8284
if (!addrs.found) { // GCOVR_EXCL_START
8385
error_printf("RTLD_NEXT used in code not dynamically loaded");
@@ -106,7 +108,7 @@ gotcha_wrappee_handle_t orig_dlsym_handle;
106108

107109
static int per_binding(hash_key_t key, hash_data_t data,
108110
void *opaque KNOWN_UNUSED) {
109-
int result;
111+
int result = 0;
110112
struct internal_binding_t *binding = (struct internal_binding_t *)data;
111113

112114
debug_printf(3, "Trying to re-bind %s from tool %s after dlopen\n",
@@ -136,7 +138,7 @@ static int per_binding(hash_key_t key, hash_data_t data,
136138

137139
static void *dlopen_wrapper(const char *filename, int flags) {
138140
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
139-
void *handle;
141+
void *handle = NULL;
140142
debug_printf(1, "User called dlopen(%s, 0x%x)\n", filename,
141143
(unsigned int)flags);
142144
handle = orig_dlopen(filename, flags);
@@ -154,7 +156,7 @@ static void *dlopen_wrapper(const char *filename, int flags) {
154156
static void *dlsym_wrapper(void *handle, const char *symbol_name) {
155157
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
156158
typeof(&dlsym_wrapper) orig_dlsym = gotcha_get_wrappee(orig_dlsym_handle);
157-
struct internal_binding_t *binding;
159+
struct internal_binding_t *binding = NULL;
158160
debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name);
159161
int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name,
160162
(hash_data_t *)&binding);

ext/gotcha/src/gotcha_utils.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int debug_level;
2828
static void debug_init() {
2929
static int debug_initialized = 0;
3030

31-
char *debug_str;
31+
char *debug_str = NULL;
3232
if (debug_initialized) {
3333
return; // GCOVR_EXCL_LINE
3434
}
@@ -48,9 +48,9 @@ static void debug_init() {
4848
hash_table_t function_hash_table;
4949
hash_table_t notfound_binding_table;
5050

51-
static hash_table_t library_table;
51+
static hash_table_t library_table = EMPTY_HASH_TABLE;
5252
static library_t *library_list = NULL;
53-
unsigned int current_generation;
53+
unsigned int current_generation = 0;
5454

5555
static hash_hashvalue_t link_map_hash(struct link_map *map) {
5656
hash_hashvalue_t hashval = (hash_hashvalue_t)((unsigned long)map);
@@ -72,8 +72,8 @@ static void setup_hash_tables() {
7272
}
7373

7474
struct library_t *get_library(struct link_map *map) {
75-
library_t *lib;
76-
int result;
75+
library_t *lib = NULL;
76+
int result = 0;
7777
result =
7878
lookup_hashtable(&library_table, (hash_key_t)map, (hash_data_t *)&lib);
7979
if (result == -1) return NULL;

ext/gotcha/src/hash.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ typedef struct hash_entry_t hash_entry_t;
3434

3535
int create_hashtable(hash_table_t *table, size_t initial_size,
3636
hash_func_t hashfunc, hash_cmp_t keycmp) {
37-
hash_entry_t *newtable;
38-
int entries_per_page;
37+
hash_entry_t *newtable = NULL;
38+
int entries_per_page = 0;
3939

4040
entries_per_page = gotcha_getpagesize() / sizeof(hash_entry_t);
4141
if (initial_size % entries_per_page)
@@ -86,9 +86,9 @@ static hash_entry_t *insert(hash_table_t *table, hash_key_t key,
8686
}
8787

8888
int grow_hashtable(hash_table_t *table, size_t new_size) {
89-
hash_table_t newtable;
90-
hash_entry_t *result;
91-
size_t i;
89+
hash_table_t newtable = EMPTY_HASH_TABLE;
90+
hash_entry_t *result = NULL;
91+
size_t i = 0;
9292

9393
newtable.table_size = new_size;
9494
newtable.entry_count = 0;
@@ -126,8 +126,8 @@ int destroy_hashtable(hash_table_t *table) {
126126
}
127127

128128
static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) {
129-
size_t index, startindex;
130-
hash_hashvalue_t hashval;
129+
size_t index = 0, startindex = 0;
130+
hash_hashvalue_t hashval = 0;
131131

132132
hashval = table->hashfunc(key);
133133
index = hashval % table->table_size;
@@ -149,8 +149,8 @@ static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) {
149149
}
150150

151151
int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) {
152-
hash_entry_t *entry;
153-
int result;
152+
hash_entry_t *entry = NULL;
153+
int result = 0;
154154

155155
result = lookup(table, key, &entry);
156156
if (result == -1) return -1;
@@ -159,10 +159,10 @@ int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) {
159159
}
160160

161161
int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) {
162-
size_t newsize;
163-
int result;
164-
hash_hashvalue_t val;
165-
hash_entry_t *entry;
162+
size_t newsize = 0;
163+
int result = 0;
164+
hash_hashvalue_t val = 0;
165+
hash_entry_t *entry = NULL;
166166

167167
newsize = table->table_size;
168168
while (table->entry_count > newsize / 2) newsize *= 2;
@@ -179,8 +179,8 @@ int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) {
179179
}
180180

181181
int removefrom_hashtable(hash_table_t *table, hash_key_t key) {
182-
hash_entry_t *entry;
183-
int result;
182+
hash_entry_t *entry = NULL;
183+
int result = 0;
184184

185185
result = lookup(table, key, &entry);
186186
if (result == -1) return -1;
@@ -202,8 +202,8 @@ int removefrom_hashtable(hash_table_t *table, hash_key_t key) {
202202
int foreach_hash_entry(hash_table_t *table, void *opaque,
203203
int (*cb)(hash_key_t key, hash_data_t data,
204204
void *opaque)) {
205-
int result;
206-
struct hash_entry_t *i;
205+
int result = 0;
206+
struct hash_entry_t *i = NULL;
207207
for (i = table->head; i != NULL; i = i->next) {
208208
result = cb(i->key, i->data, opaque);
209209
if (result != 0) return result; // GCOVR_EXCL_LINE
@@ -213,7 +213,7 @@ int foreach_hash_entry(hash_table_t *table, void *opaque,
213213

214214
hash_hashvalue_t strhash(const char *str) {
215215
unsigned long hash = 5381;
216-
int c;
216+
int c = 0;
217217

218218
while ((c = *str++)) hash = hash * 33 + c;
219219

ext/gotcha/src/hash.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef struct {
3535
struct hash_entry_t *table;
3636
struct hash_entry_t *head;
3737
} hash_table_t;
38+
#define EMPTY_HASH_TABLE \
39+
{0, 0, NULL, NULL, NULL, NULL}
3840

3941
int create_hashtable(hash_table_t *table, size_t initial_size, hash_func_t func,
4042
hash_cmp_t keycmp);

0 commit comments

Comments
 (0)