Skip to content

Commit

Permalink
Merge pull request #123 from LLNL/improv/coverage
Browse files Browse the repository at this point in the history
Improve coverage of our code.
  • Loading branch information
hariharan-devarajan committed Sep 14, 2023
2 parents 2b88ca5 + 544deef commit e700c00
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Install additional packages
run: |
sudo apt-get update
sudo apt-get install cmake gcovr
sudo apt-get install cmake gcovr check
- name: Configure and Build
run: |
Expand Down
3 changes: 2 additions & 1 deletion src/elf_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ signed long lookup_elf_hash_symbol(const char *name, ElfW(Sym) * syms,
while (y != STN_UNDEF) {
if (gotcha_strcmp(name, symnames + syms[y].st_name) == 0) {
if (!versym) {
return y;
// in general all libs would have version but it is a guard condition.
return y; // GCOVR_EXCL_LINE
}

if ((versym[y] & 0x7fff) > latest_sym_ver) {
Expand Down
22 changes: 11 additions & 11 deletions src/gotcha.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ long lookup_exported_symbol(const char* name, const struct link_map *lib, void**
INIT_DYNAMIC(lib);

if (!gnu_hash && !elf_hash) {
debug_printf(3, "Library %s does not export or import symbols\n", LIB_NAME(lib));
return -1;
debug_printf(3, "Library %s does not export or import symbols\n", LIB_NAME(lib)); // GCOVR_EXCL_START
return -1; // GCOVR_EXCL_START
}
result = -1;
if (gnu_hash) {
Expand All @@ -79,10 +79,10 @@ long lookup_exported_symbol(const char* name, const struct link_map *lib, void**
return -1;
}
if (! GOTCHA_CHECK_VISIBILITY(symtab[result])) {
debug_printf(3, "Symbol %s found but not exported in %s\n",
debug_printf(3, "Symbol %s found but not exported in %s\n", // GCOVR_EXCL_START
name, LIB_NAME(lib));
return -1;
}
} // GCOVR_EXCL_STOP

debug_printf(2, "Symbol %s found in %s at 0x%lx\n",
name, LIB_NAME(lib),
Expand Down Expand Up @@ -378,8 +378,8 @@ GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* user_bind
if (!tool)
tool = create_tool(tool_name);
if (!tool) {
error_printf("Failed to create tool %s\n", tool_name);
return GOTCHA_INTERNAL;
error_printf("Failed to create tool %s\n", tool_name); // GCOVR_EXCL_LINE
return GOTCHA_INTERNAL; // GCOVR_EXCL_LINE
}

current_generation++;
Expand All @@ -388,8 +388,8 @@ GOTCHA_EXPORT enum gotcha_error_t gotcha_wrap(struct gotcha_binding_t* user_bind
debug_printf(2, "Creating internal binding data structures and adding binding to tool\n");
binding_t *bindings = add_binding_to_tool(tool, user_bindings, num_actions);
if (!bindings) {
error_printf("Failed to create bindings for tool %s\n", tool_name);
return GOTCHA_INTERNAL;
error_printf("Failed to create bindings for tool %s\n", tool_name); // GCOVR_EXCL_LINE
return GOTCHA_INTERNAL; // GCOVR_EXCL_LINE
}

debug_printf(2, "Processing %d bindings\n", num_actions);
Expand Down Expand Up @@ -449,11 +449,11 @@ GOTCHA_EXPORT enum gotcha_error_t gotcha_set_priority(const char* tool_name, int
debug_printf(1, "User called gotcha_set_priority(%s, %d)\n", tool_name, value);
enum gotcha_error_t error_on_set = gotcha_configure_int(tool_name, GOTCHA_PRIORITY, value);
if(error_on_set != GOTCHA_SUCCESS) {
return error_on_set;
return error_on_set; // GCOVR_EXCL_LINE
}
tool_t* tool_to_place = get_tool(tool_name);
if(!tool_to_place){
tool_to_place = create_tool(tool_name);
if(!tool_to_place){ // will not happen as gotcha_configure_init creats tool if not exists
tool_to_place = create_tool(tool_name); // GCOVR_EXCL_LINE
}
remove_tool_from_list(tool_to_place);
reorder_tool(tool_to_place);
Expand Down
75 changes: 36 additions & 39 deletions src/gotcha_auxv.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ int parse_auxv_contents()

fd = gotcha_open(name, O_RDONLY);
if (fd == -1) {
parsed_auxv = -1;
return -1;
parsed_auxv = -1; // GCOVR_EXCL_LINE
return -1; // GCOVR_EXCL_LINE
}

do {
for (;;) {
result = gotcha_read(fd, buffer+offset, buffer_size-offset);
if (result == -1) {
if (errno == EINTR)
if (errno == EINTR) // GCOVR_EXCL_START
continue;
gotcha_close(fd);
parsed_auxv = -1;
return -1;
}
} // GCOVR_EXCL_STOP
if (result == 0) {
gotcha_close(fd);
done = 1;
break;
}
if (offset == buffer_size) {
break;
break; // GCOVR_EXCL_LINE
}
offset += result;
}
Expand Down Expand Up @@ -97,33 +97,30 @@ struct link_map *get_vdso_from_auxv()
ElfW(Addr) vdso_dynamic;

parse_auxv_contents();
if (!vdso_ehdr)
return NULL;

vdso_phdrs = (ElfW(Phdr) *) (vdso_ehdr->e_phoff + ((unsigned char *) vdso_ehdr));
vdso_phdr_num = vdso_ehdr->e_phnum;
if (vdso_ehdr) {
vdso_phdrs = (ElfW(Phdr) *) (vdso_ehdr->e_phoff + ((unsigned char *) vdso_ehdr));
vdso_phdr_num = vdso_ehdr->e_phnum;

for (p = 0; p < vdso_phdr_num; p++) {
if (vdso_phdrs[p].p_type == PT_DYNAMIC) {
vdso_dynamic = (ElfW(Addr)) vdso_phdrs[p].p_vaddr;
}
}
for (p = 0; p < vdso_phdr_num; p++) {
if (vdso_phdrs[p].p_type == PT_DYNAMIC) {
vdso_dynamic = (ElfW(Addr)) vdso_phdrs[p].p_vaddr;
}
}

for (m = _r_debug.r_map; m; m = m->l_next) {
if (m->l_addr + vdso_dynamic == (ElfW(Addr)) m->l_ld) {
return m;
}
for (m = _r_debug.r_map; m; m = m->l_next) {
if (m->l_addr + vdso_dynamic == (ElfW(Addr)) m->l_ld) {
return m;
}
}
}
return NULL;
return NULL; // GCOVR_EXCL_LINE
}

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

static char* vdso_aliases[] = { "linux-vdso.so",
Expand All @@ -138,7 +135,7 @@ struct link_map *get_vdso_from_aliases()
for (m = _r_debug.r_map; m; m = m->l_next) {
for (aliases = vdso_aliases; *aliases; aliases++) {
if (m->l_name && gotcha_strcmp(m->l_name, *aliases) == 0) {
return m;
return m; // GCOVR_EXCL_LINE
}
}
}
Expand All @@ -151,18 +148,18 @@ static int read_line(char *line, int size, int fd)
for (i = 0; i < size - 1; i++) {
int result = gotcha_read(fd, line + i, 1);
if (result == -1 && errno == EINTR)
continue;
continue; // GCOVR_EXCL_LINE
if (result == -1 || result == 0) {
line[i] = '\0';
return -1;
line[i] = '\0'; // GCOVR_EXCL_LINE
return -1; // GCOVR_EXCL_LINE
}
if (line[i] == '\n') {
line[i + 1] = '\0';
return 0;
}
}
line[size-1] = '\0';
return 0;
line[size-1] = '\0'; // GCOVR_EXCL_LINE
return 0; // GCOVR_EXCL_LINE
}

static int read_hex(char *str, unsigned long *val)
Expand Down Expand Up @@ -203,10 +200,10 @@ static int read_word(char *str, char *word, int word_size)
}
while (*str != ' ' && *str != '\t' && *str != '\n' && *str != '\0') {
if (word && word_cur >= word_size) {
if (word_size > 0 && word)
if (word_size > 0 && word) // GCOVR_EXCL_START
word[word_size-1] = '\0';
return word_cur;
}
}// GCOVR_EXCL_STOP
if (word)
word[word_cur] = *str;
word_cur++;
Expand All @@ -230,13 +227,13 @@ struct link_map *get_vdso_from_maps()
for (;;) {
hit_eof = read_line(line, BUFFER_LEN, maps);
if (hit_eof) {
gotcha_close(maps);
return NULL;
gotcha_close(maps); // GCOVR_EXCL_LINE
return NULL; // GCOVR_EXCL_LINE
}
line_pos = line;
line_pos += read_hex(line_pos, &addr_begin);
if (*line_pos != '-')
continue;
continue; // GCOVR_EXCL_LINE
line_pos++;
line_pos += read_hex(line_pos, &addr_end);
line_pos += read_word(line_pos, NULL, 0);
Expand All @@ -256,7 +253,7 @@ struct link_map *get_vdso_from_maps()
return m;
}

return NULL;
return NULL; // GCOVR_EXCL_LINE
}

int is_vdso(const struct link_map *map)
Expand All @@ -274,8 +271,8 @@ int is_vdso(const struct link_map *map)

result = get_vdso_from_aliases();
if (result) {
vdso = result;
return (map == vdso);
vdso = result; // GCOVR_EXCL_LINE
return (map == vdso); // GCOVR_EXCL_LINE
}

result = get_vdso_from_auxv();
Expand All @@ -284,11 +281,11 @@ int is_vdso(const struct link_map *map)
return (map == vdso);
}

result = get_vdso_from_maps();
result = get_vdso_from_maps(); // GCOVR_EXCL_START
if (result) {
vdso = result;
return (map == vdso);
}

return 0;
}
} // GCOVR_EXCL_STOP
18 changes: 9 additions & 9 deletions src/gotcha_dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ int lib_header_callback(struct dl_phdr_info * info, size_t size, void * data) {
addrs->found = 1;
return 1;
}
current = current->l_next;
current = current->l_next; // GCOVR_EXCL_LINE
}
}
return 0;
return 0; // GCOVR_EXCL_LINE
}

/**
Expand All @@ -75,8 +75,8 @@ static struct link_map* gotchas_dlsym_rtld_next_lookup(const char *name, void *w
void* symbol;
dl_iterate_phdr(lib_header_callback, &addrs);
if (!addrs.found) {
error_printf("RTLD_NEXT used in code not dynamically loaded");
exit (127);
error_printf("RTLD_NEXT used in code not dynamically loaded"); // GCOVR_EXCL_LINE
exit (127); // GCOVR_EXCL_LINE
}
struct link_map *handle = addrs.lmap->l_next;
while(handle) {
Expand Down Expand Up @@ -107,15 +107,15 @@ static int per_binding(hash_key_t key, hash_data_t data, void *opaque KNOWN_UNUS
binding->user_binding->name, binding->associated_binding_table->tool->tool_name);

while (binding->next_binding) {
binding = binding->next_binding;
debug_printf(3, "Selecting new innermost version of binding %s from tool %s.\n",
binding->user_binding->name, binding->associated_binding_table->tool->tool_name);
binding = binding->next_binding; // GCOVR_EXCL_LINE
debug_printf(3, "Selecting new innermost version of binding %s from tool %s.\n", // GCOVR_EXCL_LINE
binding->user_binding->name, binding->associated_binding_table->tool->tool_name); // GCOVR_EXCL_LINE
}

result = prepare_symbol(binding);
if (result == -1) {
debug_printf(3, "Still could not prepare binding %s after dlopen\n", binding->user_binding->name);
return 0;
debug_printf(3, "Still could not prepare binding %s after dlopen\n", binding->user_binding->name); // GCOVR_EXCL_LINE
return 0; // GCOVR_EXCL_LINE
}

removefrom_hashtable(&notfound_binding_table, key);
Expand Down
4 changes: 2 additions & 2 deletions src/gotcha_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void debug_init()

char *debug_str;
if (debug_initialized) {
return;
return; // GCOVR_EXCL_LINE
}
debug_initialized = 1;

Expand All @@ -40,7 +40,7 @@ static void debug_init()

debug_level = gotcha_atoi(debug_str);
if (debug_level <= 0)
debug_level = 1;
debug_level = 1; // GCOVR_EXCL_LINE

debug_printf(0, "Gotcha debug initialized at level %d\n", debug_level);
}
Expand Down
16 changes: 8 additions & 8 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int create_hashtable(hash_table_t *table, size_t initial_size, hash_func_t hashf

newtable = (hash_entry_t *) gotcha_malloc(initial_size * sizeof(hash_entry_t));
if (!newtable)
return -1;
return -1; // GCOVR_EXCL_LINE
gotcha_memset(newtable, 0, initial_size * sizeof(hash_entry_t));

table->table_size = initial_size;
Expand Down Expand Up @@ -77,7 +77,7 @@ static hash_entry_t *insert(hash_table_t *table, hash_key_t key, hash_data_t dat
} while (index != startindex);

if (!entry)
return NULL;
return NULL; // GCOVR_EXCL_LINE this is unreachable code.

entry->next = table->head;
entry->prev = NULL;
Expand Down Expand Up @@ -109,7 +109,7 @@ int grow_hashtable(hash_table_t *table, size_t new_size)
result = insert(&newtable, table->table[i].key, table->table[i].data,
table->table[i].hash_value);
if (!result) {
return -1;
return -1; // GCOVR_EXCL_LINE this is unreachable code
}
}

Expand Down Expand Up @@ -152,9 +152,9 @@ static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry)
return -1;
index++;
if (index == table->table_size)
index = 0;
index = 0; // GCOVR_EXCL_LINE
if (index == startindex)
return -1;
return -1; // GCOVR_EXCL_LINE
}
}

Expand Down Expand Up @@ -183,13 +183,13 @@ int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data)
if (newsize != table->table_size) {
result = grow_hashtable(table, newsize);
if (result == -1)
return -1;
return -1; // GCOVR_EXCL_LINE unreachable code
}

val = table->hashfunc(key);
entry = insert(table, key, data, val);
if (!entry)
return -1;
return -1; // GCOVR_EXCL_LINE unreachable code

return 0;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ int foreach_hash_entry(hash_table_t *table, void *opaque, int (*cb)(hash_key_t k
for (i = table->head; i != NULL; i = i->next) {
result = cb(i->key, i->data, opaque);
if (result != 0)
return result;
return result; // GCOVR_EXCL_LINE
}
return 0;
}
Expand Down

0 comments on commit e700c00

Please sign in to comment.