dart-lang / dart-lang/native

Using @Native/@FfiNative in an FFI plugin on Android

Open
#923 12 comments 0 reactions 0 assignees View on GitHub
package:ffi
Dominant language
Dart
Stars
275
Forks
144
Avg merge
2d 10h
Merged PRs (30d)
47

Description

I'm struggling with declaring and loading native libraries but something is wrong on Android.

Library load function with `DynamicLibrary.open` with which there are no problems.

```
String libraryNameOrigin = "native";
String libraryName = "lib${libraryNameOrigin}.so";
String libraryPathTest1 = "";
String libraryPathCompleteTest1 = platformPath(libraryName, path: libraryPathTest1);

print(File(libraryPathCompleteTest1).existsSync()); // true
print(DynamicLibrary.process().providesSymbol("dlopen")); // true
print(DynamicLibrary.executable().providesSymbol("dlopen")); // true

DynamicLibrary library = DynamicLibrary.open(libraryPathCompleteTest1); // handler with correct address
```

Library load function with dlopen native function that there are problems with.

```
const RTLD_LAZY = 0x00001;
const RTLD_LOCAL = 0x00000;
const RTLD_GLOBAL = 0x00100;
String libraryNameOrigin = "native";
String libraryName = "lib${libraryNameOrigin}.so";
String libraryPathTest2 = "/data/user/0/com.example./lib";
String libraryPathCompleteTest2 = platformPath(libraryName, path: libraryPathTest2);

print(File(libraryPathCompleteTest2).existsSync()); // true
print(DynamicLibrary.process().providesSymbol("dlopen")); // true
print(DynamicLibrary.executable().providesSymbol("dlopen")); // true
Pointer libraryHandle = using((arena) {
final libraryHandle = dlopen(
platformPath(name, path: path).toNativeUtf8(allocator: arena).cast(),
RTLD_LAZY | RTLD_GLOBAL);
return libraryHandle;
}); // handler with address 0x0
```

No errors but the handle has an empty pointer and is not usable to use symbols of the library.

Types of declarations used in different tests without success (in addition to others).

```
@FfiNative Function(Pointer, Int)>("dlopen")
external Pointer dlopen(Pointer filename, int flags);
```

```
final Pointer Function(Pointer, int) dlopen =
DynamicLibrary.process()
.lookup Function(Pointer, Int)>>(
"dlope")
.asFunction();
```

Same thing also for function release with dlclose native function.

```
print(DynamicLibrary.process().providesSymbol("dlclose")); // true
print(DynamicLibrary.executable().providesSymbol("dlclose")); // true
final int Function(Pointer) dlclose = DynamicLibrary.process()
.lookup)>>("dlclose")
.asFunction();
int result = dlclose(library.handle);
print(result); // 0 but library (successfully loaded with DynamicLibrary.open) is not released
```

Types of declarations used in different tests without success (in addition to others).

```
@FfiNative)>("dlclose")
external int dlclose(Pointer handle);
```

```
final int Function(Pointer) dlclose = DynamicLibrary.process()
.lookup)>>("dlclose")
.asFunction();
```

I have to be able to use the `@FfiNative` and `external `declaration, but since I couldn't I also tried with lookup noting that it doesn't change anything.

If it serves as information and if it impacts the flow, programming in C `dlopen` works fine in my system (after fixing it because the `dlfcn.c` and `dlfcn.h` files were missing).

So, why?

SDK: Flutter 3.7.3 • stable channel • Dart 2.19.2
Workspace: Flutter
Target platform: Android
Environment OS: Windows 10

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.