Skip to content

Move Shadow's main function from C to Rust #2095

@stevenengler

Description

@stevenengler

Shadow's main function is a small C wrapper around a Rust function:

int main(int argc, char* argv[]) { return main_runShadow(argc, (const char**)argv); }

#[no_mangle]
pub extern "C" fn main_runShadow(
argc: libc::c_int,
argv: *const *const libc::c_char,
) -> libc::c_int {

It would be nice to remove the C main function and build the Rust version directly.

Shadow currently uses CMake to build a C library shadow-c, a Rust library shadow-rs, and then builds the final C binary:

add_library(shadow-c-and-rs INTERFACE)
target_link_libraries(shadow-c-and-rs INTERFACE "-Wl,--start-group" shadow-rs shadow-c "-Wl,--end-group")

add_executable(shadow main.c)
target_link_libraries(shadow shadow-c-and-rs)
set_target_properties(shadow PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")

If we built the main Shadow binary as a Cargo project, we could reduce this to two steps: the C library and a Rust binary. This would also allow us to skip the final link step, which would save us a few seconds of build time.

One issue is that we set the runpath to the shim library directories and shadow reads this runpath to use the correct shim libraries. For example, after building Shadow, the runpath will be set to /abs-path/shadow/build/src/lib/shim:/abs-path/shadow/build/src/lib/injector_preload:/abs-path/shadow/build/src/lib/libc_preload:/abs-path/shadow/build/src/lib/openssl_preload:. After installing Shadow, the runpath will be changed to /home/user/.local/lib. We will either need to use a different mechanism for specifying the correct path of shim libraries, or use something like patchelf to manually modify the runpath as part of the build and installation steps (CMake does this for us, but we'd have to do it manually if building the binary with Cargo). And instead of the runpath, we could maybe use a different custom elf section instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: MaintenanceRefactoring, cleanup, documenation, or process improvements

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions