Reland "[vm/ffi] Support Windows 64 bit"

Enables dart:ffi on Windows 64 bit.

Note that function_stress_test.dart fails in two different ways, these are known bugs. https://github.com/dart-lang/sdk/issues/36138

Relanding: Fixed compilation on Android.

Closes: https://github.com/dart-lang/sdk/issues/35771
Change-Id: I7d0c8b64ca8c1726b7d264d4fd9213299a9f7df6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96781
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Daco Harkes 2019-03-13 11:47:37 +00:00 committed by commit-bot@chromium.org
parent 3bc3205e39
commit a43c525de2
11 changed files with 125 additions and 48 deletions

View file

@ -4,13 +4,17 @@
// This file contains test functions for the dart:ffi test cases.
// The tests which use this don't run on Windows yet.
#if !defined(_WIN32)
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include "platform/globals.h"
#if defined(HOST_OS_WINDOWS)
#include <psapi.h>
#else
#include <unistd.h>
#endif
#include <iostream>
#include <limits>
@ -406,6 +410,7 @@ DART_EXPORT void* SmallPointer() {
return reinterpret_cast<void*>(-0x80000000L);
}
#if !defined(_WIN32)
DART_EXPORT int RedirectStderr() {
char filename[256];
snprintf(filename, sizeof(filename), "/tmp/captured_stderr_%d", getpid());
@ -413,7 +418,6 @@ DART_EXPORT int RedirectStderr() {
printf("Got file %s\n", filename);
return getpid();
}
#endif
} // namespace dart
#endif

View file

@ -500,9 +500,9 @@ static RawCode* TrampolineCode(const Function& function,
#elif !defined(TARGET_ARCH_X64)
// https://github.com/dart-lang/sdk/issues/35774
UNREACHABLE();
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS) && \
!defined(TARGET_OS_WINDOWS)
// https://github.com/dart-lang/sdk/issues/35760 Arm32 && Android
// https://github.com/dart-lang/sdk/issues/35771 Windows
// https://github.com/dart-lang/sdk/issues/35772 Arm64
// https://github.com/dart-lang/sdk/issues/35773 DBC
UNREACHABLE();
@ -588,9 +588,9 @@ static void* GenerateFfiInverseTrampoline(const Function& signature,
#elif !defined(TARGET_ARCH_X64)
// https://github.com/dart-lang/sdk/issues/35774
UNREACHABLE();
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS) && \
!defined(TARGET_OS_WINDOWS)
// https://github.com/dart-lang/sdk/issues/35760 Arm32 && Android
// https://github.com/dart-lang/sdk/issues/35771 Windows
// https://github.com/dart-lang/sdk/issues/35772 Arm64
// https://github.com/dart-lang/sdk/issues/35773 DBC
UNREACHABLE();

View file

@ -3,12 +3,12 @@
// BSD-style license that can be found in the LICENSE file.
#if !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
// TODO(dacoharkes): implement dynamic libraries for other targets.
// see
// - runtime/vm/native_symbol.h
// - runtime/vm/native_symbol_linux.cc
// - runtime/bin/extensions.h (but we cannot import from bin)
// TODO(dacoharkes): Implement dynamic libraries for other targets & merge the
// implementation with:
// - runtime/bin/extensions.h
// - runtime/bin/extensions_linux.cc
// TODO(dacoharkes): Make the code from bin available in a manner similar to
// runtime/vm/dart.h Dart_FileReadCallback.
#else
#include <dlfcn.h>
#endif
@ -19,29 +19,85 @@
namespace dart {
DEFINE_NATIVE_ENTRY(Ffi_dl_open, 0, 1) {
#if !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
UNREACHABLE();
#else
GET_NON_NULL_NATIVE_ARGUMENT(String, lib_path, arguments->NativeArgAt(0));
dlerror(); // Clear any errors.
void* handle = dlopen(lib_path.ToCString(), RTLD_LAZY);
static void* LoadExtensionLibrary(const char* library_file) {
#if defined(TARGET_OS_LINUX) || defined(TARGET_OS_MACOS)
void* handle = dlopen(library_file, RTLD_LAZY);
if (handle == nullptr) {
char* error = dlerror();
const String& msg = String::Handle(
String::NewFormatted("Failed to load dynamic library(%s)", error));
String::NewFormatted("Failed to load dynamic library (%s)", error));
Exceptions::ThrowArgumentError(msg);
}
return handle;
#elif defined(TARGET_OS_WINDOWS)
SetLastError(0); // Clear any errors.
// Convert to wchar_t string.
const int name_len =
MultiByteToWideChar(CP_UTF8, 0, library_file, -1, NULL, 0);
wchar_t* name = new wchar_t[name_len];
MultiByteToWideChar(CP_UTF8, 0, library_file, -1, name, name_len);
void* ext = LoadLibraryW(name);
delete[] name;
if (ext == nullptr) {
const int error = GetLastError();
const String& msg = String::Handle(
String::NewFormatted("Failed to load dynamic library (%i)", error));
Exceptions::ThrowArgumentError(msg);
}
return ext;
#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_open, 0, 1) {
GET_NON_NULL_NATIVE_ARGUMENT(String, lib_path, arguments->NativeArgAt(0));
void* handle = LoadExtensionLibrary(lib_path.ToCString());
return DynamicLibrary::New(handle);
}
static void* ResolveSymbol(void* handle, const char* symbol) {
#if defined(TARGET_OS_LINUX) || defined(TARGET_OS_MACOS)
dlerror(); // Clear any errors.
void* pointer = dlsym(handle, symbol);
if (pointer == nullptr) {
char* error = dlerror();
const String& msg = String::Handle(
String::NewFormatted("Failed to lookup symbol (%s)", error));
Exceptions::ThrowArgumentError(msg);
}
return pointer;
#elif defined(TARGET_OS_WINDOWS)
SetLastError(0);
void* pointer = GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol);
if (pointer == nullptr) {
const int error = GetLastError();
const String& msg = String::Handle(
String::NewFormatted("Failed to lookup symbol (%i)", error));
Exceptions::ThrowArgumentError(msg);
}
return pointer;
#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_lookup, 1, 2) {
#if !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
UNREACHABLE();
#else
GET_NATIVE_TYPE_ARGUMENT(type_arg, arguments->NativeTypeArgAt(0));
GET_NON_NULL_NATIVE_ARGUMENT(DynamicLibrary, dlib, arguments->NativeArgAt(0));
@ -50,22 +106,14 @@ DEFINE_NATIVE_ENTRY(Ffi_dl_lookup, 1, 2) {
void* handle = dlib.GetHandle();
dlerror(); // Clear any errors.
intptr_t pointer =
reinterpret_cast<intptr_t>(dlsym(handle, argSymbolName.ToCString()));
char* error;
if ((error = dlerror()) != NULL) {
const String& msg = String::Handle(
String::NewFormatted("Failed to lookup symbol (%s)", error));
Exceptions::ThrowArgumentError(msg);
}
const intptr_t pointer = reinterpret_cast<intptr_t>(
ResolveSymbol(handle, argSymbolName.ToCString()));
// TODO(dacoharkes): should this return NULL if addres is 0?
// TODO(dacoharkes): should this return Object::null() if address is 0?
// https://github.com/dart-lang/sdk/issues/35756
RawPointer* result =
Pointer::New(type_arg, Integer::Handle(zone, Integer::New(pointer)));
return result;
#endif
}
DEFINE_NATIVE_ENTRY(Ffi_dl_getHandle, 0, 1) {

View file

@ -300,9 +300,9 @@ class Api : AllStatic {
#if !defined(TARGET_ARCH_X64)
// https://github.com/dart-lang/sdk/issues/35774
return false;
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS)
#elif !defined(TARGET_OS_LINUX) && !defined(TARGET_OS_MACOS) && \
!defined(TARGET_OS_WINDOWS)
// https://github.com/dart-lang/sdk/issues/35760 Arm32 && Android
// https://github.com/dart-lang/sdk/issues/35771 Windows
// https://github.com/dart-lang/sdk/issues/35772 Arm64
// https://github.com/dart-lang/sdk/issues/35773 DBC
return false;

View file

@ -9,6 +9,7 @@ String _platformPath(String name, {String path}) {
if (path == null) path = "";
if (Platform.isLinux) return path + "lib" + name + ".so";
if (Platform.isMacOS) return path + "lib" + name + ".dylib";
if (Platform.isWindows) return path + name + ".dll";
throw Exception("Platform not implemented");
}

View file

@ -9,6 +9,7 @@ String _platformPath(String name, {String path}) {
if (path == null) path = "";
if (Platform.isLinux) return path + "lib" + name + ".so";
if (Platform.isMacOS) return path + "lib" + name + ".dylib";
if (Platform.isWindows) return path + name + ".dll";
throw Exception("Platform not implemented");
}

View file

@ -32,7 +32,7 @@ test(GCWatcher watcher, void Function() testee,
}
main() async {
final watcher = GCWatcher();
final watcher = GCWatcher.ifAvailable();
try {
await test(watcher, testBoxInt64);
// On 64-bit platforms this won't trigger GC because the result fits into a

View file

@ -3,25 +3,48 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'dart:convert';
import 'dylib_utils.dart';
import 'dart:ffi';
import 'dart:io' show Platform;
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
abstract class GCWatcher {
factory GCWatcher() => _GCWatcherImpl();
factory GCWatcher.dummy() => _MockGCWatcher();
factory GCWatcher.ifAvailable() =>
Platform.isWindows ? GCWatcher.dummy() : GCWatcher();
Future<int> size();
void dispose();
}
// Requires --verbose-gc.
class GCWatcher {
class _GCWatcherImpl implements GCWatcher {
int _suffix;
Future<int> size() async {
return await File("/tmp/captured_stderr_$_suffix").length();
}
GCWatcher() {
_GCWatcherImpl() {
print("Starting...");
_suffix = ffiTestFunctions
.lookupFunction<Int32 Function(), int Function()>("RedirectStderr")();
}
dispose() => File("/tmp/captured_stderr_$_suffix").deleteSync();
dispose() {
try {
File("/tmp/captured_stderr_$_suffix").deleteSync();
} catch (e) {
print("deleting file failed");
}
}
}
class _MockGCWatcher implements GCWatcher {
int _ctr = 0;
Future<int> size() async => ++_ctr;
dispose() {}
}

View file

@ -39,7 +39,7 @@ Future<void> testGC() async {
CString cs = ffi.fromAddress<CString>(11);
bar = cs;
foo = "";
final watcher = GCWatcher();
final watcher = GCWatcher.ifAvailable();
int counts = await watcher.size();
for (int i = 0; i < 1000000; ++i) {
foo = new X(i);

View file

@ -229,8 +229,8 @@ io/socket_many_connections_test: Skip # Timeout
io/web_socket_compression_test: Skip # Timeout
io/web_socket_test: Skip # Timeout
[ $arch != x64 || $compiler != dartk || $mode == product || $system != linux && $system != macos]
ffi/function_stress_test: SkipByDesign # FFI must be supported. Also requires --verbose-gc, which isn't included in product.
[ $arch != x64 || $compiler != dartk || $mode == product || $system != linux && $system != macos ]
ffi/function_stress_test: SkipByDesign # FFI must be supported. Also requires --verbose-gc, which isn't included in product. Windows issues will be fixed in IL CL: https://github.com/dart-lang/sdk/issues/36138
ffi/subtype_test: SkipByDesign # FFI must be supported. Also requires --verbose-gc, which isn't included in product.
[ $mode == product || $mode != product ]

View file

@ -133,5 +133,5 @@ full_coverage_test: Skip # TODO(vegorov) SIMDBC interpreter doesn't support cove
link_natives_lazily_test: SkipByDesign # SIMDBC interpreter doesn't support lazy linking of natives.
no_lazy_dispatchers_test: SkipByDesign # SIMDBC interpreter doesn't support --no_lazy_dispatchers
[ $arch != x64 || $system != linux && $system != macos ]
[ $arch != x64 || $system != linux && $system != macos && $system != windows ]
ffi: Skip # ffi not yet supported on other systems than linux/macos 64