1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-03 00:08:46 +00:00

[vm/ffi] Add providesSymbol to DynamicLibrary

This adds the providesSymbol method to DynamicLibrary. It returns
whether the library contains a function with the given name.

As per dlsym(3), it is valid for dlsym to return nullptr in a success
case if the symbol actually has a NULL value. So I've changed the logic
to check for dlerror() after we invoke dlsym(), both in the existing
lookup and in the new method.

Closes https://github.com/dart-lang/sdk/issues/46192

TEST=tests/ffi(_2)/has_symbol_test.dart

Change-Id: Ibcb1c051cc0cdd95a104fe86ef2fc76da5bafb5d
Cq-Include-Trybots: luci.dart.try:vm-precomp-ffi-qemu-linux-release-arm-try,vm-ffi-android-debug-arm64-try,vm-ffi-android-debug-arm-try,vm-kernel-linux-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-mac-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201900
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Simon Binder 2021-06-02 16:01:43 +00:00 committed by commit-bot@chromium.org
parent 997487db0f
commit be893fdf27
16 changed files with 125 additions and 11 deletions

View File

@ -14,6 +14,11 @@
daylight saving changes that are not precisely one hour.
(No change on the Web which uses the JavaScript `Date` object.)
#### `dart:ffi`
* Adds the `DynamicLibrary.providesSymbol` function to check whether a symbol
is available in a dynamic library.
#### `dart:io`
* BREAKING CHANGE (for pre-migrated null safe code):

View File

@ -25,4 +25,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -52,4 +52,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -25,4 +25,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -52,4 +52,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -33,4 +33,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array_multi_dimensional.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -84,4 +84,4 @@ Extra constant evaluation: evaluated: 110, effectively constant: 2
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array_multi_dimensional.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -33,4 +33,4 @@ constants {
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array_multi_dimensional.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -84,4 +84,4 @@ Extra constant evaluation: evaluated: 110, effectively constant: 2
Constructor coverage from constants:
org-dartlang-testcase:///ffi_struct_inline_array_multi_dimensional.dart:
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:135:9)
- _ArraySize. (from org-dartlang-sdk:///sdk/lib/ffi/ffi.dart:136:9)

View File

@ -99,8 +99,8 @@ static void* ResolveSymbol(void* handle, const char* symbol) {
defined(HOST_OS_ANDROID) || defined(HOST_OS_FUCHSIA)
dlerror(); // Clear any errors.
void* pointer = dlsym(handle, symbol);
if (pointer == nullptr) {
char* error = dlerror();
char* error = dlerror();
if (error != nullptr) {
const String& msg = String::Handle(
String::NewFormatted("Failed to lookup symbol (%s)", error));
Exceptions::ThrowArgumentError(msg);
@ -147,4 +147,32 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_getHandle, 0, 1) {
return Integer::NewFromUint64(handle);
}
static bool SymbolExists(void* handle, const char* symbol) {
#if defined(HOST_OS_LINUX) || defined(HOST_OS_MACOS) || \
defined(HOST_OS_ANDROID) || defined(HOST_OS_FUCHSIA)
dlerror(); // Clear previous error, if any.
dlsym(handle, symbol);
// Checking whether dlsym returns a nullptr is not enough, as the value of
// the symbol could actually be NULL. Check the error condition instead.
return dlerror() == nullptr;
#elif defined(HOST_OS_WINDOWS)
return GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol) != nullptr;
#else
const Array& args = Array::Handle(Array::New(1));
args.SetAt(0,
String::Handle(String::New(
"The dart:ffi library is not available on this platform.")));
Exceptions::ThrowByType(Exceptions::kUnsupported, args);
#endif
}
DEFINE_NATIVE_ENTRY(Ffi_dl_providesSymbol, 0, 2) {
GET_NON_NULL_NATIVE_ARGUMENT(DynamicLibrary, dlib, arguments->NativeArgAt(0));
GET_NON_NULL_NATIVE_ARGUMENT(String, argSymbolName,
arguments->NativeArgAt(1));
void* handle = dlib.GetHandle();
return Bool::Get(SymbolExists(handle, argSymbolName.ToCString())).ptr();
}
} // namespace dart

View File

@ -402,6 +402,7 @@ namespace dart {
V(Ffi_dl_open, 1) \
V(Ffi_dl_lookup, 2) \
V(Ffi_dl_getHandle, 1) \
V(Ffi_dl_providesSymbol, 2) \
V(Ffi_asExternalTypedData, 2) \
V(Ffi_dl_processLibrary, 0) \
V(Ffi_dl_executableLibrary, 0) \

View File

@ -30,6 +30,9 @@ class DynamicLibrary {
Pointer<T> lookup<T extends NativeType>(String symbolName)
native "Ffi_dl_lookup";
@patch
bool providesSymbol(String symbolName) native "Ffi_dl_providesSymbol";
// TODO(dacoharkes): Expose this to users, or extend Pointer?
// https://github.com/dart-lang/sdk/issues/35881
int getHandle() native "Ffi_dl_getHandle";

View File

@ -39,9 +39,15 @@ class DynamicLibrary {
/// [dlsym(3)](https://man7.org/linux/man-pages/man3/dlsym.3.html) system
/// call.
///
/// The symbol must be provided by the dynamic library.
/// The symbol must be provided by the dynamic library. To check whether
/// the library provides such symbol, use [hasSymbol].
external Pointer<T> lookup<T extends NativeType>(String symbolName);
/// Checks whether this dynamic library provides a symbol with the given
/// name.
@Since('2.14')
external bool providesSymbol(String symbolName);
/// Dynamic libraries are equal if they load the same library.
external bool operator ==(Object other);

View File

@ -11,6 +11,7 @@
*/
library dart.ffi;
import 'dart:_internal' show Since;
import 'dart:isolate';
import 'dart:typed_data';

View File

@ -0,0 +1,34 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'ffi_test_helpers.dart';
void main() {
testHasSymbol();
}
void testHasSymbol() {
Expect.isTrue(ffiTestFunctions.providesSymbol('ReturnMaxUint8'));
Expect.isFalse(ffiTestFunctions.providesSymbol('SymbolNotInLibrary'));
if (Platform.isMacOS ||
Platform.isIOS ||
Platform.isAndroid ||
Platform.isLinux) {
DynamicLibrary p = DynamicLibrary.process();
Expect.isTrue(p.providesSymbol('dlopen'));
Expect.isFalse(p.providesSymbol('symbol_that_does_not_exist_in_process'));
}
DynamicLibrary e = DynamicLibrary.executable();
Expect.isTrue(e.providesSymbol('Dart_Invoke'));
Expect.isFalse(e.providesSymbol('symbol_that_does_not_exist_in_executable'));
}

View File

@ -0,0 +1,36 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// SharedObjects=ffi_test_functions
// @dart=2.9
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'ffi_test_helpers.dart';
void main() {
testHasSymbol();
}
void testHasSymbol() {
Expect.isTrue(ffiTestFunctions.providesSymbol('ReturnMaxUint8'));
Expect.isFalse(ffiTestFunctions.providesSymbol('SymbolNotInLibrary'));
if (Platform.isMacOS ||
Platform.isIOS ||
Platform.isAndroid ||
Platform.isLinux) {
DynamicLibrary p = DynamicLibrary.process();
Expect.isTrue(p.providesSymbol('dlopen'));
Expect.isFalse(p.providesSymbol('symbol_that_does_not_exist_in_process'));
}
DynamicLibrary e = DynamicLibrary.executable();
Expect.isTrue(e.providesSymbol('Dart_Invoke'));
Expect.isFalse(e.providesSymbol('symbol_that_does_not_exist_in_executable'));
}