Skip to content

Commit 7d467e8

Browse files
Clement Skaucommit-bot@chromium.org
Clement Skau
authored andcommitted
[vm] Adds args_n to FFI resolver.
This makes it more closely mirror the Dart_NativeEntryResolver, and acts as an extra sanity check that signatures (roughly) align between the FfiNative decl. and the native function. TEST=Updated runtime/vm/dart_api_impl_test.cc Change-Id: I40799dc583ec14db14dc453afed4e2d1eb06fced Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212566 Commit-Queue: Clement Skau <[email protected]> Reviewed-by: Daco Harkes <[email protected]>
1 parent 7c7f662 commit 7d467e8

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

pkg/vm/lib/transformations/ffi_native.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,26 @@ class FfiNativeTransformer extends Transformer {
102102
final DartType dartType =
103103
node.function.computeThisFunctionType(Nullability.nonNullable);
104104
// Double Function(Double)
105-
final nativeType = annotationConst.typeArguments[0];
105+
final nativeType = annotationConst.typeArguments[0] as FunctionType;
106106
// InterfaceType(NativeFunction<Double Function(Double)>*)
107107
final DartType nativeInterfaceType =
108108
InterfaceType(nativeFunctionClass, Nullability.legacy, [nativeType]);
109109

110+
// Derive number of arguments from the native function signature.
111+
final args_n = nativeType.positionalParameters.length;
112+
110113
// TODO(dartbug.com/31579): Add `..fileOffset`s once we can handle these in
111114
// patch files.
112115

113-
// _ffi_resolver('dart:math', 'Math_sqrt')
116+
// _ffi_resolver('dart:math', 'Math_sqrt', 1)
114117
final resolverInvocation = FunctionInvocation(
115118
FunctionAccessKind.FunctionType,
116119
StaticGet(resolverField),
117120
Arguments([
118121
ConstantExpression(
119122
StringConstant(currentLibrary!.importUri.toString())),
120-
ConstantExpression(functionName)
123+
ConstantExpression(functionName),
124+
ConstantExpression(IntConstant(args_n)),
121125
]),
122126
functionType: resolverField.type as FunctionType);
123127

runtime/bin/ffi_test/ffi_test_functions_vmspecific.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,11 @@ intptr_t ReturnIntPtr(intptr_t x) {
10941094
return x;
10951095
}
10961096

1097-
static void* FfiNativeResolver(const char* name) {
1098-
if (strcmp(name, "ReturnIntPtr") == 0) {
1097+
static void* FfiNativeResolver(const char* name, uintptr_t args_n) {
1098+
if (strcmp(name, "ReturnIntPtr") == 0 && args_n == 1) {
10991099
return reinterpret_cast<void*>(ReturnIntPtr);
11001100
}
1101-
if (strcmp(name, "IsThreadInGenerated") == 0) {
1101+
if (strcmp(name, "IsThreadInGenerated") == 0 && args_n == 0) {
11021102
return reinterpret_cast<void*>(IsThreadInGenerated);
11031103
}
11041104
// This should be unreachable in tests.

runtime/include/dart_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3046,7 +3046,7 @@ typedef const uint8_t* (*Dart_NativeEntrySymbol)(Dart_NativeFunction nf);
30463046
*
30473047
* See Dart_SetFfiNativeResolver.
30483048
*/
3049-
typedef void* (*Dart_FfiNativeResolver)(const char* name);
3049+
typedef void* (*Dart_FfiNativeResolver)(const char* name, uintptr_t args_n);
30503050

30513051
/*
30523052
* ===========

runtime/lib/ffi.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ DEFINE_NATIVE_ENTRY(DartApiDLInitializeData, 0, 0) {
276276
}
277277

278278
// FFI native C function pointer resolver.
279-
static intptr_t FfiResolve(Dart_Handle lib_url, Dart_Handle name) {
279+
static intptr_t FfiResolve(Dart_Handle lib_url,
280+
Dart_Handle name,
281+
uintptr_t args_n) {
280282
DARTSCOPE(Thread::Current());
281283

282284
const String& lib_url_str = Api::UnwrapStringHandle(T->zone(), lib_url);
@@ -296,7 +298,7 @@ static intptr_t FfiResolve(Dart_Handle lib_url, Dart_Handle name) {
296298
Exceptions::ThrowArgumentError(error);
297299
}
298300

299-
auto* f = resolver(function_name.ToCString());
301+
auto* f = resolver(function_name.ToCString(), args_n);
300302
if (f == nullptr) {
301303
const String& error = String::Handle(String::NewFormatted(
302304
"Couldn't resolve function: '%s'.", function_name.ToCString()));

runtime/vm/dart_api_impl_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9585,8 +9585,9 @@ static intptr_t EchoInt(double x) {
95859585
return x;
95869586
}
95879587

9588-
static void* FfiNativeResolver(const char* name) {
9588+
static void* FfiNativeResolver(const char* name, uintptr_t args_n) {
95899589
ASSERT(strcmp(name, "EchoInt") == 0);
9590+
ASSERT(args_n == 1);
95909591
return reinterpret_cast<void*>(EchoInt);
95919592
}
95929593

@@ -9628,7 +9629,7 @@ TEST_CASE(Dart_SetFfiNativeResolver_MissingResolver) {
96289629
"Invalid argument(s): Library has no handler: 'file:///test-lib'.");
96299630
}
96309631

9631-
static void* NopResolver(const char* name) {
9632+
static void* NopResolver(const char* name, uintptr_t args_n) {
96329633
return nullptr;
96339634
}
96349635

sdk/lib/ffi/ffi.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,11 @@ class FfiNative<T> {
824824
// Bootstrapping native for getting the FFI native C function pointer to look
825825
// up the FFI resolver.
826826
@pragma("vm:external-name", "Ffi_GetFfiNativeResolver")
827-
external Pointer<NativeFunction<IntPtr Function(Handle, Handle)>>
827+
external Pointer<NativeFunction<IntPtr Function(Handle, Handle, IntPtr)>>
828828
_get_ffi_native_resolver<T extends NativeFunction>();
829829

830830
// Resolver for FFI Native C function pointers.
831831
@pragma('vm:entry-point')
832-
final _ffi_resolver =
833-
_get_ffi_native_resolver<NativeFunction<IntPtr Function(Handle, Handle)>>()
834-
.asFunction<int Function(Object, Object)>();
832+
final _ffi_resolver = _get_ffi_native_resolver<
833+
NativeFunction<IntPtr Function(Handle, Handle, IntPtr)>>()
834+
.asFunction<int Function(Object, Object, int)>();

0 commit comments

Comments
 (0)