androidc++vulkandlopen

dlopen doesn't work well on Android for Vulkan custom layer


I have a custom layer called memory_layer that collects memory information. Here is what I want to achieve:

  1. Use memory_layer in my Vulkan program. The Vulkan loader loads libmemory_layer.so into memory when vkCreateInstance is called.
  2. The program runs, and memory_layer collects memory information.
  3. Use dlopen and dlsym to get the memory_info_write_out function handle from libmemory_layer.so:
void *handle = dlopen("libmemory_layer.so", RTLD_LAZY);
void (*memory-info-write-out)(const char*) = (void (*)(const char*)) dlsym(handle, "memory-info-write-out");
  1. Call memory_info_write_out to write the memory information to a file. This function is called before vkDestroyInstance.

This process works on Linux perfectly. But not on Android, memory-info-write-out can not write the information collected by custom_layers.

Investigation on Android:

After vkCreateInstance(1), I checked the memory map of the app thread:

...
6c8a6cc000-6c8ad57000 r-xp 00000000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c8ad57000-6c8ad58000 ---p 00000000 00:00 0 
6c8ad58000-6c8ad7c000 r--p 0068b000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c8ad7c000-6c8ad7d000 rw-p 006af000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
...

The thread loaded libmemory_layer.so into memory. After dlopen(3), I checked the memory map again:

...
6c8562f000-6c85cba000 r-xp 00000000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c85cba000-6c85cbb000 ---p 00000000 00:00 0 
6c85cbb000-6c85cdf000 r--p 0068b000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c85cdf000-6c85ce0000 rw-p 006af000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
...
6c8a6cc000-6c8ad57000 r-xp 00000000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c8ad57000-6c8ad58000 ---p 00000000 00:00 0 
6c8ad58000-6c8ad7c000 r--p 0068b000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
6c8ad7c000-6c8ad7d000 rw-p 006af000 fe:07 4206502                        /data/local/debug/vulkan/libmemory_layer.so
...

This suggests that dlopen created a new context instead of using the existing one(?).

Questions:

  1. Is dlopen the correct way to call functions in Vulkan layers on Android?
  2. How can I ensure that dlopen uses the existing context instead of creating a new one?

I don't know if dlopen the perfect way to call some function in layers. But I do need to do this.

Research:

  1. dlopen on Android is implemented by BIONIC, which differs from GNU on Linux. dlopen() fails on Android but works on Linux
  2. This might be related to parameters used in dlopen: dlopen and implicit library loading: two copies of the same library I am unable to change the dlopen mode in Vulkan-Loader. I have tried various combinations of modes in my dlopen function, but they did not work.

This question is similar to some others but not exactly the same.


Solution

  • Expose a custom Vulkan extension, and load it via the standard layer loading mechanism to avoid the dlopen() messing about.