Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divergence 64 vs 32 bit FFI using intptr_t #38008

Closed
feli-citas opened this issue Aug 26, 2019 · 1 comment
Closed

Divergence 64 vs 32 bit FFI using intptr_t #38008

feli-citas opened this issue Aug 26, 2019 · 1 comment
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.)

Comments

@feli-citas
Copy link
Contributor

32 bit Integer overflows lead to divergence between 32 and 64 bit dart versions when parameter is passed through an FFI intptr_t.

DART_EXPORT intptr_t SumManyInts(intptr_t a,
                                 intptr_t b,
                                 intptr_t c,
                                 intptr_t d,
                                 intptr_t e,
                                 intptr_t f,
                                 intptr_t g,
                                 intptr_t h,
                                 intptr_t i,
                                 intptr_t j) {
  std::cout << "SumManyInts(" << a << ", " << b << ", " << c << ", " << d
            << ", " << e << ", " << f << ", " << g << ", " << h << ", " << i
            << ", " << j << ")\n";
  intptr_t retval = a + b + c + d + e + f + g + h + i + j;
  std::cout << "returning " << retval << "\n";
  return retval;
}
import 'dart:ffi' as ffi;
import 'dart:io' show Platform;

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");
}
    
ffi.DynamicLibrary dlopenPlatformSpecific(String name, {String path}) {
  String fullPath = _platformPath(name, path: path);
  return ffi.DynamicLibrary.open(fullPath);
}   
    
ffi.DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");


typedef DecenaryOp = int Function(int, int, int, int, int, int, int, int, int, int);
typedef NativeDecenaryOp = ffi.IntPtr Function(
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr,
    ffi.IntPtr);
DecenaryOp sumManyInts = ffiTestFunctions
    .lookupFunction<NativeDecenaryOp, DecenaryOp>("SumManyInts");

main() {
  sumManyInts(9223372032559809536, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}

64 bit

SumManyInts(9223372032559809536, 0, 0, 0, 0, 0, 0, 0, 0, 0)
returning 9223372032559809536

32 bit

SumManyInts(1024, 0, 0, 0, 0, 0, 0, 0, 0, 0)
returning 1024
@feli-citas feli-citas added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.) labels Aug 26, 2019
@aartbik
Copy link
Contributor

aartbik commented Aug 26, 2019

Ah, Dart int-s are always 64-bit. However, intptr_t-s are architecture dependent. So this behavior is to be expected. Let's not call C++ functions that depend on intptr_t.

@aartbik aartbik removed their assignment Aug 26, 2019
dart-bot pushed a commit that referenced this issue Aug 29, 2019
Rationale:
Uses a simple C library to test FFI interface.
Bug: #38008
#37606
Change-Id: I0bb57db5a9977e2300dcace8fef85a2a92d50e41
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/114556
Commit-Queue: Felicitas Hetzelt <felih@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
Reviewed-by: Aart Bik <ajcbik@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. dartfuzz Found with Dart fuzzing (DartFuzz, libFuzzer, etc.)
Projects
None yet
Development

No branches or pull requests

2 participants