Skip to content

Commit

Permalink
Initialize all function-level variables
Browse files Browse the repository at this point in the history
  • Loading branch information
TomTheBear committed Sep 17, 2024
1 parent 5726d79 commit 46cc93c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 92 deletions.
8 changes: 4 additions & 4 deletions src/elf_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ static uint32_t gnu_hash_func(const char *str) {
signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
ElfW(Half) * versym, char *symnames,
void *sheader) {
uint32_t *buckets, *vals;
uint32_t hash_val;
uint32_t cur_sym, cur_sym_hashval;
uint32_t *buckets = NULL, *vals = NULL;
uint32_t hash_val = 0;
uint32_t cur_sym = 0, cur_sym_hashval = 0;
signed long latest_sym = -1;
ElfW(Half) latest_sym_ver = 0;
struct gnu_hash_header *header = (struct gnu_hash_header *)(sheader);
Expand Down Expand Up @@ -89,7 +89,7 @@ signed long lookup_gnu_hash_symbol(const char *name, ElfW(Sym) * syms,
}

static unsigned long elf_hash(const unsigned char *name) {
unsigned int h = 0, g;
unsigned int h = 0, g = 0;
while (*name != '\0') {
h = (h << 4) + *name++;
if ((g = h & 0xf0000000)) {
Expand Down
22 changes: 11 additions & 11 deletions src/gotcha.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void setInternalBindingAddressPointer(void **in, void *value) {

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

int prepare_symbol(struct internal_binding_t *binding) {
int result;
struct link_map *lib;
int result = 0;
struct link_map *lib = NULL;
struct gotcha_binding_t *user_binding = binding->user_binding;

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

struct internal_binding_t *head;
int hash_result;
int hash_result = 0;
hash_result =
lookup_hashtable(&function_hash_table, (void *)name, (void **)&head);
if (hash_result != 0) {
Expand Down Expand Up @@ -209,9 +209,9 @@ static int rewrite_wrapper_orders(struct internal_binding_t *binding) {
static int update_lib_bindings(ElfW(Sym) * symbol KNOWN_UNUSED, char *name,
ElfW(Addr) offset, struct link_map *lmap,
hash_table_t *lookuptable) {
int result;
struct internal_binding_t *internal_binding;
void **got_address;
int result = 0;
struct internal_binding_t *internal_binding = NULL;
void **got_address = NULL;

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

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

gotcha_init();

Expand Down
28 changes: 14 additions & 14 deletions src/gotcha_auxv.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ static unsigned int auxv_pagesz = 0;

int parse_auxv_contents() {
char name[] = "/proc/self/auxv";
int fd, done = 0;
char buffer[BUFFER_LEN];
int fd = -1, done = 0;
char buffer[BUFFER_LEN] = {'\0'};
const ssize_t buffer_size = BUFFER_LEN;
ssize_t offset = 0, result;
ElfW(auxv_t) * auxv, *a;
ssize_t offset = 0, result = 0;
ElfW(auxv_t) * auxv = NULL, *a = NULL;
static int parsed_auxv = 0;

if (parsed_auxv) return parsed_auxv == -1 ? parsed_auxv : 0;
Expand Down Expand Up @@ -86,7 +86,7 @@ int parse_auxv_contents() {
}

struct link_map *get_vdso_from_auxv() {
struct link_map *m;
struct link_map *m = NULL;

ElfW(Phdr) *vdso_phdrs = NULL;
ElfW(Half) vdso_phdr_num, p;
Expand Down Expand Up @@ -114,16 +114,16 @@ struct link_map *get_vdso_from_auxv() {
}

unsigned int get_auxv_pagesize() {
int result;
int result = 0;
result = parse_auxv_contents();
return result == -1 ? 0 : auxv_pagesz;
}

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

struct link_map *get_vdso_from_aliases() {
struct link_map *m;
char **aliases;
struct link_map *m = NULL;
char **aliases = NULL;

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

static int read_line(char *line, int size, int fd) {
int i;
int i = 0;
for (i = 0; i < size - 1; i++) {
int result = gotcha_read(fd, line + i, 1);
if (result == -1 && errno == EINTR) continue; // GCOVR_EXCL_LINE
Expand Down Expand Up @@ -201,10 +201,10 @@ static int read_word(char *str, char *word, int word_size) {
}

struct link_map *get_vdso_from_maps() {
int maps, hit_eof;
ElfW(Addr) addr_begin, addr_end, dynamic;
char name[BUFFER_LEN], line[BUFFER_LEN], *line_pos;
struct link_map *m;
int maps = 0, hit_eof = 0;
ElfW(Addr) addr_begin = 0, addr_end = 0, dynamic = 0;
char name[BUFFER_LEN] = {'\0'}, line[BUFFER_LEN] = {'\0'}, *line_pos = NULL;
struct link_map *m = NULL;
maps = gotcha_open("/proc/self/maps", O_RDONLY);
for (;;) {
hit_eof = read_line(line, BUFFER_LEN, maps);
Expand Down Expand Up @@ -239,7 +239,7 @@ struct link_map *get_vdso_from_maps() {
int is_vdso(const struct link_map *map) {
static int vdso_checked = 0;
static struct link_map *vdso = NULL;
struct link_map *result;
struct link_map *result = NULL;

if (!map) return 0;
if (vdso_checked) return (map == vdso);
Expand Down
13 changes: 7 additions & 6 deletions src/gotcha_dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Addrs {
struct link_map *lmap; // output
int found;
};
#define EMPTY_ADDRS {0, NULL, 0}
/**
* This is a callback to get headers for each library.
* We check if the caller's virtual address is between base address and the
Expand All @@ -29,7 +30,7 @@ struct Addrs {
int lib_header_callback(struct dl_phdr_info *info, size_t size, void *data) {
struct Addrs *addrs = data;
const char *name = NULL;
ElfW(Addr) load_address;
ElfW(Addr) load_address = 0;
for (int i = 0; i < info->dlpi_phnum; ++i) {
if (info->dlpi_phdr[i].p_type == PT_LOAD) {
ElfW(Addr) base_addr = info->dlpi_addr;
Expand Down Expand Up @@ -73,11 +74,11 @@ static struct link_map *gotchas_dlsym_rtld_next_lookup(const char *name,
/* Iterative over the library headers and find the caller
* the address of the caller is set in addrs->library_laddr
**/
struct Addrs addrs;
struct Addrs addrs = EMPTY_ADDRS;
addrs.lookup_addr = caller;
addrs.lmap = _r_debug.r_map;
addrs.found = 0;
void *symbol;
void *symbol = NULL;
dl_iterate_phdr(lib_header_callback, &addrs);
if (!addrs.found) { // GCOVR_EXCL_START
error_printf("RTLD_NEXT used in code not dynamically loaded");
Expand Down Expand Up @@ -106,7 +107,7 @@ gotcha_wrappee_handle_t orig_dlsym_handle;

static int per_binding(hash_key_t key, hash_data_t data,
void *opaque KNOWN_UNUSED) {
int result;
int result = 0;
struct internal_binding_t *binding = (struct internal_binding_t *)data;

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

static void *dlopen_wrapper(const char *filename, int flags) {
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
void *handle;
void *handle = NULL;
debug_printf(1, "User called dlopen(%s, 0x%x)\n", filename,
(unsigned int)flags);
handle = orig_dlopen(filename, flags);
Expand All @@ -154,7 +155,7 @@ static void *dlopen_wrapper(const char *filename, int flags) {
static void *dlsym_wrapper(void *handle, const char *symbol_name) {
typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
typeof(&dlsym_wrapper) orig_dlsym = gotcha_get_wrappee(orig_dlsym_handle);
struct internal_binding_t *binding;
struct internal_binding_t *binding = NULL;
debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name);
int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name,
(hash_data_t *)&binding);
Expand Down
10 changes: 5 additions & 5 deletions src/gotcha_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int debug_level;
static void debug_init() {
static int debug_initialized = 0;

char *debug_str;
char *debug_str = NULL;
if (debug_initialized) {
return; // GCOVR_EXCL_LINE
}
Expand All @@ -48,9 +48,9 @@ static void debug_init() {
hash_table_t function_hash_table;
hash_table_t notfound_binding_table;

static hash_table_t library_table;
static hash_table_t library_table = EMPTY_HASH_TABLE;
static library_t *library_list = NULL;
unsigned int current_generation;
unsigned int current_generation = 0;

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

struct library_t *get_library(struct link_map *map) {
library_t *lib;
int result;
library_t *lib = NULL;
int result = 0;
result =
lookup_hashtable(&library_table, (hash_key_t)map, (hash_data_t *)&lib);
if (result == -1) return NULL;
Expand Down
36 changes: 18 additions & 18 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ typedef struct hash_entry_t hash_entry_t;

int create_hashtable(hash_table_t *table, size_t initial_size,
hash_func_t hashfunc, hash_cmp_t keycmp) {
hash_entry_t *newtable;
int entries_per_page;
hash_entry_t *newtable = NULL;
int entries_per_page = 0;

entries_per_page = gotcha_getpagesize() / sizeof(hash_entry_t);
if (initial_size % entries_per_page)
Expand Down Expand Up @@ -86,9 +86,9 @@ static hash_entry_t *insert(hash_table_t *table, hash_key_t key,
}

int grow_hashtable(hash_table_t *table, size_t new_size) {
hash_table_t newtable;
hash_entry_t *result;
size_t i;
hash_table_t newtable = EMPTY_HASH_TABLE;
hash_entry_t *result = NULL;
size_t i = 0;

newtable.table_size = new_size;
newtable.entry_count = 0;
Expand Down Expand Up @@ -126,8 +126,8 @@ int destroy_hashtable(hash_table_t *table) {
}

static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) {
size_t index, startindex;
hash_hashvalue_t hashval;
size_t index = 0, startindex = 0;
hash_hashvalue_t hashval = 0;

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

int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) {
hash_entry_t *entry;
int result;
hash_entry_t *entry = NULL;
int result = 0;

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

int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) {
size_t newsize;
int result;
hash_hashvalue_t val;
hash_entry_t *entry;
size_t newsize = 0;
int result = 0;
hash_hashvalue_t val = 0;
hash_entry_t *entry = NULL;

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

int removefrom_hashtable(hash_table_t *table, hash_key_t key) {
hash_entry_t *entry;
int result;
hash_entry_t *entry = NULL;
int result = 0;

result = lookup(table, key, &entry);
if (result == -1) return -1;
Expand All @@ -202,8 +202,8 @@ int removefrom_hashtable(hash_table_t *table, hash_key_t key) {
int foreach_hash_entry(hash_table_t *table, void *opaque,
int (*cb)(hash_key_t key, hash_data_t data,
void *opaque)) {
int result;
struct hash_entry_t *i;
int result = 0;
struct hash_entry_t *i = NULL;
for (i = table->head; i != NULL; i = i->next) {
result = cb(i->key, i->data, opaque);
if (result != 0) return result; // GCOVR_EXCL_LINE
Expand All @@ -213,7 +213,7 @@ int foreach_hash_entry(hash_table_t *table, void *opaque,

hash_hashvalue_t strhash(const char *str) {
unsigned long hash = 5381;
int c;
int c = 0;

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

Expand Down
1 change: 1 addition & 0 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct {
struct hash_entry_t *table;
struct hash_entry_t *head;
} hash_table_t;
#define EMPTY_HASH_TABLE {0, 0, NULL, NULL, NULL, NULL}

int create_hashtable(hash_table_t *table, size_t initial_size, hash_func_t func,
hash_cmp_t keycmp);
Expand Down
Loading

0 comments on commit 46cc93c

Please sign in to comment.