Skip to content

Commit

Permalink
Add verbose debug logging to libia2
Browse files Browse the repository at this point in the history
To print verbose logs of all mprotects and ELF segment computation, build with `-DLIBIA2_DEBUG_LOG=ON`.
  • Loading branch information
rinon committed Oct 4, 2024
1 parent 98fff44 commit bb8fd32
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions runtime/libia2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if(LIBIA2_DEBUG)
target_compile_definitions(libia2 PUBLIC LIBIA2_DEBUG=1)
endif()

if(LIBIA2_DEBUG_LOG)
target_compile_definitions(libia2 PRIVATE LIBIA2_DEBUG_LOG=1)
endif()

target_link_options(libia2
INTERFACE
"-pthread"
Expand Down
16 changes: 15 additions & 1 deletion runtime/libia2/ia2.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
search_args->found_library_count++;
}

// printf("protecting library: %s\n", basename(info->dlpi_name));
#if LIBIA2_DEBUG_LOG
printf("protecting library: %s\n", basename(info->dlpi_name));
#endif

struct AddressRange shared_ranges[NUM_SHARED_RANGES] = {0};
size_t shared_range_count = 0;
Expand Down Expand Up @@ -379,6 +381,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
search_args->shared_sections[i].end);
exit(-1);
}
#if LIBIA2_DEBUG_LOG
printf("Shared range %zu: 0x%" PRIx64 "-0x%" PRIx64 "\n", shared_range_count, cur_range->start, cur_range->end);
#endif

shared_range_count++;
}
Expand Down Expand Up @@ -430,6 +435,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {

Elf64_Addr start = (info->dlpi_addr + phdr.p_vaddr) & ~0xFFFUL;
Elf64_Addr seg_end = (start + phdr.p_memsz + 0xFFFUL) & ~0xFFFUL;
#if LIBIA2_DEBUG_LOG
printf("Segment %zu: 0x%" PRIx64 "-0x%" PRIx64 "\n", i, start, seg_end);
#endif
while (start < seg_end) {
Elf64_Addr cur_end = seg_end;

Expand All @@ -440,6 +448,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
// and remove the overlapping portion.
if (shared_ranges[j].start <= start && shared_ranges[j].end > start) {
start = shared_ranges[j].end;
#if LIBIA2_DEBUG_LOG
printf("Shared range %zu overlaps start of segment %zu, adjusting start to 0x%" PRIx64 "\n", j, i, start);
#endif
}

// Look for a shared range overlapping any of the rest of the current
Expand All @@ -448,6 +459,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) {
if (shared_ranges[j].start > start &&
shared_ranges[j].start < cur_end) {
cur_end = shared_ranges[j].start;
#if LIBIA2_DEBUG_LOG
printf("Shared range %zu overlaps end of segment %zu, adjusting end to 0x%" PRIx64 "\n", j, i, cur_end);
#endif
}
}

Expand Down
7 changes: 7 additions & 0 deletions runtime/libia2/include/ia2_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,16 @@ works as a reasonable signpost no-op. */
#if defined(__aarch64__)
int ia2_mprotect_with_tag(void *addr, size_t len, int prot, int tag);
#elif defined(__x86_64__)
#if LIBIA2_DEBUG_LOG
static int ia2_mprotect_with_tag(void *addr, size_t len, int prot, int tag) {
printf("ia2_mprotect_with_tag(addr=%p, len=%zu, prot=%d, tag=%d)\n", addr, len, prot, tag);
return pkey_mprotect(addr, len, prot, tag);
}
#else
/* We can't use an alias attribute since this points to a function outside the translation unit */
#define ia2_mprotect_with_tag pkey_mprotect
#endif
#endif
char *allocate_stack(int i);
void verify_tls_padding(void);
void ensure_pkeys_allocated(int *n_to_alloc);
Expand Down

0 comments on commit bb8fd32

Please sign in to comment.