Skip to content

Commit eb5d798

Browse files
shikharishdcharkes
authored andcommitted
[ffi] Separate native asset presence from loading
A null handle may be valid, so dlopen_v2 cannot distinguish a missing asset from a load failure. Replace it with contains_asset and dlopen_asset. Missing @Native assets fall back; codeAsset misses and loader errors stay fatal. Supersedes https://dart-review.googlesource.com/c/sdk/+/521960 TEST=Five focused run_vm_tests cases TEST=tools/test.py -n vm-linux-debug-x64 ffi/native_assets TEST=tools/test.py -n vm-aot-linux-debug-x64 ffi/native_assets [email protected] Cq-Include-Trybots: luci.dart.try:vm-aot-linux-debug-x64-try,vm-gcc-linux-debug-x64-try,vm-win-debug-x64-try,vm-ubsan-linux-release-x64-try,vm-tsan-linux-release-x64-try,vm-reload-linux-debug-x64-try,vm-msvc-win-debug-x64-try,vm-msan-linux-release-x64-try,vm-mac-debug-x64-try,vm-linux-release-x64-try,vm-linux-debug-x64-try,vm-asan-linux-release-x64-try,vm-appjit-linux-debug-x64-try,vm-aot-win-debug-x64-try,vm-aot-mac-debug-x64-try,vm-aot-dwarf-linux-product-x64-try,vm-aot-android-debug-x64c-try,pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-linux-release-try,pkg-mac-release-try,pkg-mac-release-arm64-try,pkg-win-release-arm64-try,pkg-win-release-try,flutter-linux-try,flutter-mac-try Change-Id: I09e9ff7833b712e7f22ed40325d5815d988c8769 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/533620 Reviewed-by: Daco Harkes <[email protected]> Reviewed-by: Slava Egorov <[email protected]>
1 parent 41fa766 commit eb5d798

3 files changed

Lines changed: 306 additions & 62 deletions

File tree

‎runtime/include/dart_api.h‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,18 +3350,14 @@ typedef void* (*Dart_NativeAssetsDlopenCallback)(const char* path,
33503350
typedef void* (*Dart_NativeAssetsDlopenCallbackNoPath)(char** error);
33513351

33523352
/**
3353-
* Callback provided by the embedder that is used by the VM to resolve asset
3354-
* ids.
3353+
* Callback provided by the embedder that is used by the VM to load a native
3354+
* asset by id.
33553355
*
3356-
* The embedder can freely chose how to bundle asset id to asset path mappings
3356+
* The embedder can freely choose how to bundle asset id to asset path mappings
33573357
* and how to perform this lookup.
33583358
*
3359-
* If the embedder provides this callback, it must also provide
3360-
* `Dart_NativeAssetsAvailableAssets`.
3361-
*
3362-
* If provided, takes precedence over `Dart_NativeAssetsDlopenCallback`.
3363-
*
3364-
* \param asset_id The asset id requested by an `@Native` external function.
3359+
* \param asset_id The asset id requested by an `@Native` external function or
3360+
* `DynamicLibrary.codeAsset`.
33653361
*
33663362
* \param error Returns NULL if successful, an error message otherwise. The
33673363
* caller is responsible for calling free() on the error message.
@@ -3372,6 +3368,20 @@ typedef void* (*Dart_NativeAssetsDlopenCallbackNoPath)(char** error);
33723368
typedef void* (*Dart_NativeAssetsDlopenAssetId)(const char* asset_id,
33733369
char** error);
33743370

3371+
/**
3372+
* Callback provided by the embedder that is used by the VM to determine
3373+
* whether a native asset id is registered.
3374+
*
3375+
* The embedder can freely choose how to bundle asset id to asset path mappings
3376+
* and how to perform this lookup.
3377+
*
3378+
* \param asset_id The asset id requested by an `@Native` external function or
3379+
* `DynamicLibrary.codeAsset`.
3380+
*
3381+
* \return Whether the asset id is registered with the embedder.
3382+
*/
3383+
typedef bool (*Dart_NativeAssetsContainsAsset)(const char* asset_id);
3384+
33753385
/**
33763386
* Callback provided by the embedder that is used by the VM to request a
33773387
* description of the available assets
@@ -3424,17 +3434,42 @@ typedef struct {
34243434
Dart_NativeAssetsDlopenCallbackNoPath dlopen_executable;
34253435
Dart_NativeAssetsDlsymCallback dlsym;
34263436
Dart_NativeAssetsDlcloseCallback dlclose;
3437+
3438+
/*
3439+
* Loads a native asset by id for `@Native` resolution.
3440+
*
3441+
* If provided, takes precedence over the path-based dlopen callbacks and
3442+
* must be accompanied by `available_assets`.
3443+
*
3444+
* This callback cannot distinguish a missing asset from a valid `NULL`
3445+
* handle and is not used by `DynamicLibrary.codeAsset`.
3446+
*/
34273447
Dart_NativeAssetsDlopenAssetId dlopen;
34283448
Dart_NativeAssetsAvailableAssets available_assets;
34293449

34303450
/*
3431-
* If provided, takes precedence over `dlopen`.
3451+
* Determines whether a native asset id is registered.
3452+
*
3453+
* Must be provided together with `dlopen_asset`. If both are provided, they
3454+
* take precedence over `dlopen`.
3455+
*
3456+
* A missing asset is not a loading error. `@Native` falls back to process
3457+
* lookup, while `DynamicLibrary.codeAsset` reports that the asset does not
3458+
* exist.
3459+
*/
3460+
Dart_NativeAssetsContainsAsset contains_asset;
3461+
3462+
/*
3463+
* Loads a native asset that `contains_asset` reported as registered.
3464+
*
3465+
* Must be provided together with `contains_asset` and must be accompanied
3466+
* by `available_assets`. Together, `contains_asset` and `dlopen_asset` take
3467+
* precedence over `dlopen`.
34323468
*
3433-
* For this callback, `error == NULL` means the asset was found, even if
3434-
* the returned handle is `NULL`. Missing assets must be reported through
3435-
* `error`.
3469+
* Any error is a hard loading failure. A successful process or executable
3470+
* lookup may return a `NULL` handle.
34363471
*/
3437-
Dart_NativeAssetsDlopenAssetId dlopen_v2;
3472+
Dart_NativeAssetsDlopenAssetId dlopen_asset;
34383473
} NativeAssetsApi;
34393474

34403475
/**

‎runtime/lib/ffi_dynamic_library.cc‎

Lines changed: 85 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,8 @@ static char* AvailableAssetsToCString(Thread* const thread) {
374374

375375
struct AssetLibraryLoadResult {
376376
void* handle = nullptr;
377-
bool asset_found = false;
378377
Dart_NativeAssetsDlsymCallback dlsym = nullptr;
379378
Dart_NativeAssetsDlcloseCallback dlclose = nullptr;
380-
bool unsupported = false;
381379
};
382380

383381
// If an error occurs populates |error| with an error message
@@ -394,10 +392,7 @@ static AssetLibraryLoadResult LoadAssetLibraryFromKernelMapping(
394392
Zone* const zone = thread->zone();
395393
const auto& asset_location =
396394
Array::Handle(zone, GetAssetLocation(thread, asset));
397-
if (asset_location.IsNull()) {
398-
return result;
399-
}
400-
result.asset_found = true;
395+
ASSERT(!asset_location.IsNull());
401396
result.dlsym = native_assets_api->dlsym;
402397
result.dlclose = native_assets_api->dlclose;
403398

@@ -457,46 +452,55 @@ static AssetLibraryLoadResult LoadAssetLibraryFromKernelMapping(
457452
return result;
458453
}
459454

460-
// If an error occurs populates |error| with an error message
461-
// (caller must free this message when it is no longer needed).
455+
// Returns whether |asset| is registered with either the embedder or the
456+
// native-assets mapping in the kernel.
457+
//
458+
// If an error occurs populates |error| with an error message (caller must free
459+
// this message when it is no longer needed).
460+
static bool ContainsAsset(Thread* const thread,
461+
const String& asset,
462+
char** error) {
463+
NativeAssetsApi* native_assets_api =
464+
thread->isolate_group()->native_assets_api();
465+
const bool has_contains_asset = native_assets_api->contains_asset != nullptr;
466+
const bool has_dlopen_asset = native_assets_api->dlopen_asset != nullptr;
467+
if (has_contains_asset != has_dlopen_asset) {
468+
*error = OS::SCreate(
469+
/*use malloc*/ nullptr,
470+
"NativeAssetsApi::contains_asset and NativeAssetsApi::dlopen_asset "
471+
"must be provided together.");
472+
return false;
473+
}
474+
if (has_dlopen_asset) {
475+
NoActiveIsolateScope no_active_isolate_scope;
476+
return native_assets_api->contains_asset(asset.ToCString());
477+
}
478+
479+
Zone* const zone = thread->zone();
480+
const auto& asset_location =
481+
Array::Handle(zone, GetAssetLocation(thread, asset));
482+
return !asset_location.IsNull();
483+
}
484+
485+
// Loads |asset|, which must have been found by ContainsAsset.
486+
//
487+
// If an error occurs populates |error| with an error message (caller must free
488+
// this message when it is no longer needed).
462489
//
463490
// A successful process or executable lookup can return a nullptr handle.
464-
// NativeAssetsApi::dlopen_v2 reports unknown assets through |error|.
465-
static AssetLibraryLoadResult LoadAssetLibrary(
466-
Thread* const thread,
467-
const String& asset,
468-
bool allow_legacy_embedder_dlopen,
469-
char** error) {
491+
static AssetLibraryLoadResult LoadAssetLibrary(Thread* const thread,
492+
const String& asset,
493+
char** error) {
470494
AssetLibraryLoadResult result;
471495
NativeAssetsApi* native_assets_api =
472496
thread->isolate_group()->native_assets_api();
473-
if (native_assets_api->dlopen_v2 != nullptr) {
497+
if (native_assets_api->dlopen_asset != nullptr) {
474498
// Let embedder resolve the asset id to asset path.
475499
NoActiveIsolateScope no_active_isolate_scope;
476-
result.handle = native_assets_api->dlopen_v2(asset.ToCString(), error);
477-
result.asset_found = *error == nullptr;
500+
result.handle = native_assets_api->dlopen_asset(asset.ToCString(), error);
478501
result.dlsym = native_assets_api->dlsym;
479502
result.dlclose = native_assets_api->dlclose;
480503
return result;
481-
} else if (native_assets_api->dlopen != nullptr) {
482-
if (!allow_legacy_embedder_dlopen) {
483-
result.unsupported = true;
484-
*error = OS::SCreate(
485-
/*use malloc*/ nullptr,
486-
"DynamicLibrary.codeAsset is not supported by this embedder. "
487-
"NativeAssetsApi::dlopen_v2 must be provided.");
488-
return result;
489-
}
490-
// Legacy embedder resolution cannot distinguish a missing asset from a
491-
// successful process lookup. Keep it only for @Native compatibility.
492-
NoActiveIsolateScope no_active_isolate_scope;
493-
result.handle = native_assets_api->dlopen(asset.ToCString(), error);
494-
if (*error != nullptr || result.handle != nullptr) {
495-
result.asset_found = true;
496-
result.dlsym = native_assets_api->dlsym;
497-
result.dlclose = native_assets_api->dlclose;
498-
return result;
499-
}
500504
}
501505
return LoadAssetLibraryFromKernelMapping(thread, asset, error);
502506
}
@@ -505,13 +509,35 @@ static void* FfiResolveAsset(Thread* const thread,
505509
const String& asset,
506510
const String& symbol,
507511
char** error) {
508-
auto library = LoadAssetLibrary(thread, asset,
509-
/*allow_legacy_embedder_dlopen=*/true, error);
510-
if (*error != nullptr || !library.asset_found) {
511-
return nullptr;
512-
}
513512
NativeAssetsApi* native_assets_api =
514513
thread->isolate_group()->native_assets_api();
514+
515+
if (native_assets_api->dlopen_asset == nullptr &&
516+
native_assets_api->dlopen != nullptr) {
517+
// Legacy embedder resolution cannot distinguish a missing asset from a
518+
// successful process lookup. Keep it only for @Native compatibility.
519+
void* handle = nullptr;
520+
{
521+
NoActiveIsolateScope no_active_isolate_scope;
522+
handle = native_assets_api->dlopen(asset.ToCString(), error);
523+
}
524+
if (*error != nullptr) {
525+
return nullptr;
526+
}
527+
if (handle != nullptr) {
528+
return ResolveNativeAssetsSymbol(native_assets_api, handle,
529+
symbol.ToCString(), error);
530+
}
531+
}
532+
533+
if (!ContainsAsset(thread, asset, error)) {
534+
return nullptr;
535+
}
536+
537+
auto library = LoadAssetLibrary(thread, asset, error);
538+
if (*error != nullptr) {
539+
return nullptr;
540+
}
515541
return ResolveNativeAssetsSymbol(native_assets_api, library.handle,
516542
symbol.ToCString(), error);
517543
}
@@ -521,18 +547,22 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_codeAsset, 0, 1) {
521547
NativeAssetsApi* native_assets_api =
522548
thread->isolate_group()->native_assets_api();
523549
char* error = nullptr;
524-
auto result =
525-
LoadAssetLibrary(thread, asset_id,
526-
/*allow_legacy_embedder_dlopen=*/false, &error);
550+
if (native_assets_api->dlopen_asset == nullptr &&
551+
native_assets_api->dlopen != nullptr) {
552+
const char* const message =
553+
"DynamicLibrary.codeAsset is not supported by this embedder. "
554+
"NativeAssetsApi::dlopen_asset and NativeAssetsApi::contains_asset "
555+
"must be provided.";
556+
Exceptions::ThrowUnsupportedError(message);
557+
}
558+
559+
const bool asset_found = ContainsAsset(thread, asset_id, &error);
527560
if (error != nullptr) {
528561
const String& msg = String::Handle(String::New(error));
529562
free(error);
530-
if (result.unsupported) {
531-
Exceptions::ThrowUnsupportedError(msg.ToCString());
532-
}
533563
Exceptions::ThrowArgumentError(msg);
534564
}
535-
if (!result.asset_found) {
565+
if (!asset_found) {
536566
const char* const format = "No asset with id '%s' found. %s";
537567
String& msg = String::Handle();
538568
if (native_assets_api->available_assets != nullptr) {
@@ -546,6 +576,13 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_codeAsset, 0, 1) {
546576
}
547577
Exceptions::ThrowArgumentError(msg);
548578
}
579+
580+
auto result = LoadAssetLibrary(thread, asset_id, &error);
581+
if (error != nullptr) {
582+
const String& msg = String::Handle(String::New(error));
583+
free(error);
584+
Exceptions::ThrowArgumentError(msg);
585+
}
549586
return DynamicLibrary::New(result.handle, result.dlsym, result.dlclose);
550587
}
551588

0 commit comments

Comments
 (0)