seL4 / sel4runtime

A minimal runtime for seL4 applications.
Other
12 stars 28 forks source link

start.c: Ensure that __init_libc is called #7

Closed alistair23 closed 4 years ago

alistair23 commented 4 years ago

musl includes some constructors that are used to initalise global variables. This is all setup from the __init_libc() function.

Currently __init_libc() and the constuctors are included in seL4 user space apps, but are never called. This can result in crashes when code tries to use malloc as the __sysinfo variable is never set.

This patch ensures that the __init_libc() function is called so that all global variables are configured.

For more details on how the boot flow looks, see: https://github.com/jhand2/openenclave/blob/36c8b9bc662bf575d0f21af43e24c9cf69bcc4fe/docs/DesignDocs/libc_initialization.md

Signed-off-by: Alistair Francis alistair.francis@wdc.com

xurtis commented 4 years ago

sel4runtime is designed to have no external dependencies beyond libsel4. This change would implicitly introduce a dependency on musl libc which we are explicitly trying to avoid so that we can investigate alternative libc implementations.

sel4runtime does call all constructors at startup, so the recommended way to ensure __init_libc is called is to call it from a constructor, although care needs to be taken when doing this. __init_libc assumes a functioning implementation of mmap that is guaranteed to succeed when it it is called to map a thread-local storage region for the initial thread. The initialiser that invokes __init_libc must be of a priority such that it is initialised after and implementation of mmap.

This method has already been implemented in sel4test and sel4bench and libsel4muslcsys (https://github.com/seL4/seL4_libs/commit/24f2b526d5c4a5b093dab68540dc2665741cabf1, https://github.com/seL4/seL4_libs/commit/781d618951dd38eb97b8ebe84c51fc1a854cabc6) is already configured to link a constructor that calls __init_libc, so adding it as a dependency is sufficient for this call to be added.

alistair23 commented 4 years ago

Do you see the constrctors being called when running the test application? What I see in the objdump and when running on a board is that it is never called.

xurtis commented 3 years ago

Yes, all of the constructors are called. Where in the binary are you looking? The calls should be at the end of __sel4runtime_load_env. GCC will place the function pointers in the array starting at __init_array_start rather than compiling them into the _init function.