Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DLCs-Pattern] Create a core native DLL Loader libraries and a pattern for DLCs using the Finite-Automata model #35

Open
pavly-gerges opened this issue Aug 27, 2024 · 2 comments
Labels
advanced Advanced stuff. core Core API related stuff help wanted Extra attention is needed question Further information is requested

Comments

@pavly-gerges
Copy link
Member

This is an enhancement proposal that entails building a native DLL Loader API coded in C, so that the native libraries (e.g., Bullet Physics) can dynamically link other dependencies through a generic JNI layer without rewriting JNI layers for those native libraries. This issue requires the plugins from the Electrostatic-Sandbox project.

@pavly-gerges pavly-gerges added enhancement New feature or request core Core API related stuff labels Aug 27, 2024
@pavly-gerges pavly-gerges added this to the 1.1.0-stable Release milestone Aug 27, 2024
@pavly-gerges
Copy link
Member Author

pavly-gerges commented Aug 27, 2024

Here is a simple example to what the provisional design might entail:

[electrostatic4j_dll_loader_DllPluginLoader.cpp]

#include <jni.h>
#include <electrostatic4j_dll_loader_DllPluginLoader.h>
#include <dll_loader_cpp_ext.h>
#include <jni/util.h>
#include <electrostatic/algorithm/arithmos/adt/map.h>

extern "C" {
   hash_map map = {
       ...
    };

   JNIEXPORT void 
         JNICALL Java_electrostatic4j_dll_loader_DllPluginLoader_loadFunction(JNIEnv *env, jweak file_handle, jstring handle, jclass clazz) {
         const char *c_handle = to_cstring(env, handle);
         void *c_filehandle = to_cfilehandle(env, file_handle);
         void *address = loadFunction(c_filehandle, c_handle);
         map->insert(HASH(c_handle), address);
   }

     JNIEXPORT void 
         JNICALL Java_electrostatic4j_dll_loader_DllPluginLoader_callMethod(JNIEnv *env, jstring handle, jobjectArray params, jclass clazz) {
         const char *c_handle = to_cstring(env, handle);
         void *callMethod = map->get(HASH(c_handle));
         void **params = to_carray(env, params);
        // ???
        // 2 automata could be used here!
        // First Automata: written in C and involves a common conventional call API.
       // Second Automata: written C/Asm that involves a platform-specific conventional call API.
   }
}

// ???

[dll_loader_cpp_ext.cpp]

extern "C" {

   void* loadFunction(void *file_handle, const char *function_handle) {
         // sanity checks 
        ...
        // link and load symbol 
      dlsym(handle, "create_footbar");
   }

}

[DllPluginLoader.java]

public final DllPluginLoader extends NativeBinaryLoader {
     ...
    private static native Object callFunction(final String functionHandler);

    <T> public static T call(final String functionHandle) {
            return (T) callFunction(functionHandle);
    }
    
    ...
    // extracts the library to the file system and defer loading to the Native plugin loader coded in C
   // this removes the burden of writing JNI layers
}

@pavly-gerges pavly-gerges added help wanted Extra attention is needed question Further information is requested labels Aug 27, 2024
@pavly-gerges pavly-gerges pinned this issue Aug 27, 2024
@pavly-gerges pavly-gerges unpinned this issue Aug 28, 2024
@pavly-gerges pavly-gerges removed this from the 1.1.0-stable Release milestone Aug 28, 2024
@pavly-gerges pavly-gerges added advanced Advanced stuff. and removed enhancement New feature or request labels Aug 28, 2024
@pavly-gerges pavly-gerges added this to the 1.1.0-stable Release milestone Aug 29, 2024
@pavly-gerges
Copy link
Member Author

Alright here is a simple example from the current WIP Electrostatic-Sandbox conventional caller API:
[hello_dll_service.c]

#include <electrostatic/util/loader/dll_loader.h>
#include <electrostatic/util/console/colors.h>
#include <stdlib.h>
#include <stdio.h>

void *dll_conventional_routine(routine_data *data);

void *dll_conventional_routine(routine_data *data) {
    // interpret the conventional call into a service job
    const char *caller = data->dispatcher_name;
    
    fprintf(stdout, MAGENTA "Dispatched service from caller substrate --- %s\n" MAGENTA, caller);
    fprintf(stdout, GREEN "%s" GREEN, ((const char **) data->routine_inputs)[0]);
    fprintf(stdout, GREEN "%s" GREEN, ((const char **) data->routine_inputs)[1]);
    
    return "Hello Back\n";
}

[hello_dll_substrate.c]

#include <electrostatic/util/loader/dll_loader.h>
#include <electrostatic/util/console/colors.h>
#include <stdlib.h>
#include <stdio.h>
#include <gnu/lib-names.h>

int main() {
    dll_function_table function_table = {
    };
    
    routine_data *data = calloc(1, sizeof(routine_data));
    // allocate 2 blocks for memory addresses of binary address size
    data->routine_inputs = calloc(2, sizeof(void *));
    // name caller service handler
    data->dispatcher_name = "DLL_SUBSTRATE-00144";
    data->dll_flags = RTLD_LAZY;
    // pass some args
    data->routine_inputs[0] = "Hello";
    data->routine_inputs[1] = " World\n";
    // define the absolute directory for the dll file
    data->dll_name = "/media/pavl-machine/pavl-g/Projects/Electrostatic-Sandbox/electrostatic-sandbox-framework/electrostatic-examples/build/linux/x86-64/libhello_dll_service.c.so";
    // define the service conventional routine name
    data->routine_name = "dll_conventional_routine";
    
    // initialize the default functions
    init(&function_table);
    
    // open the dll
    function_table.dll_open(data);
    function_table.dll_function_loading(data, &function_table);
    call_dll_convention(data, &function_table);
    
    printf("Convention Output = %s", (const char *) data->routine_output);
    
    function_table.dll_close(data);
    
    free(data->routine_inputs);
    free(data);
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advanced Advanced stuff. core Core API related stuff help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant