Created
September 19, 2022 12:51
-
-
Save mcopik/c6dc64e6b24aea9576d517ca00d1a9c0 to your computer and use it in GitHub Desktop.
Reproducing GDB hanging on reading a shared library from memory-mapped file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gcc -fPIC -shared -g lib.c -o lib.so | |
gcc -DLOAD_FROM_FILE main.c -g -o from_file | |
gcc main.c -g -o from_memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int foo(int x) | |
{ | |
int var = 0; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <assert.h> | |
#define _GNU_SOURCE | |
#include <unistd.h> | |
#include <dlfcn.h> | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
int main(int argc, char ** argv) | |
{ | |
const char* path = "lib.so"; | |
// Receive code information | |
FILE* file = fopen(path, "rb"); | |
assert(file); | |
fseek (file, 0 , SEEK_END); | |
size_t size = ftell(file); | |
rewind(file); | |
int fd = memfd_create("libfunction", 0); | |
assert(fd > 0); | |
int ret = ftruncate(fd, size) ; | |
assert(ret == 0); | |
void* memory_handle = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); | |
assert(memory_handle); | |
size_t bytes_read = fread(memory_handle, 1, size, file); | |
assert(bytes_read == size); | |
fclose(file); | |
msync(memory_handle, size, MS_SYNC); | |
char buf[32]; | |
snprintf(buf, 32, "%s%d", "/proc/self/fd/", fd); | |
#if !defined(LOAD_FROM_FILE) | |
printf("Reading from %s\n", buf); | |
fflush(stdout); | |
void* library_handle = dlopen(buf, RTLD_NOW); | |
#else | |
void* library_handle = dlopen("./lib.so", RTLD_NOW); | |
#endif | |
assert(library_handle); | |
typedef int (*func_t)(int); | |
func_t func = dlsym(library_handle, "foo"); | |
assert(func); | |
func(42); | |
dlclose(library_handle); | |
munmap(memory_handle, size); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment