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

Start use DynamicLibrary.invoke() but not only DynamicLibrary.invokeEx() #4

Open
mezoni opened this issue Mar 25, 2015 · 2 comments
Open

Comments

@mezoni
Copy link

mezoni commented Mar 25, 2015

You use only invokeEx() and not use invoke().

static dynamic invoke(String name, [List<dynamic> args = const [], List<BinaryType> vartypes]) {
    return libc.invokeEx(name, args, vartypes);
  }

The method invoke() not much slower than invokeEx()
But it are more intellectual way and it will be improved further.

Improved performance of the DynamicLibrary.invoke() with a variable parameters through the precompilation of the commonly used binary types (bool, char, char *,int, double)

This has effect also when you use strings but not only with variadic functions.

Also I plan implement propogating null's (converting) to appropriate pointers.

Code fragment of invoke().

if (argument is String) {
          if (parameter is PointerType) {
            var valueType = parameter.type;
            if (valueType.kind == basicTypes.charType.kind) {
              if (strings == null) {
                strings = <BinaryObject>[];
              }

              var string = valueType.array(argument.length + 1).alloc(argument.codeUnits);
              strings.add(string);
              newArguments[i] = string;
            } else {
              throw new ArgumentError("Unable to allocate string object for parameter $i: $parameter");
            }
          }
        } else if (argument == null) {
          // TODO: "Promoting NULL values not implemented yet"
          throw new UnimplementedError("Promoting NULL values not implemented yet");
        } 

As you can see it automatically perform the following tasks.

  • Converts the String (allocates them without invoke the helper.allocString())
  • Creates the heap for them
  • Promoting NULL values (will be added soon)

P.S.

Also I plan to add segmentation fault handler into unsafe extension for the graceful exits with a Dart stack trace (instead of only exit code 139 "post mortem").

After that I implement "auto NULL pointer promoting" (if SEGFAULT will work with a success in Dart VM).

As for me second looks (and works) better than first.

_checkResult(invoke("sysctlbyname", [n, getType("void*").nullPtr, len, getType("void*").nullPtr, 0]));

_checkResult(invoke__("sysctlbyname", [n, null, len, null, 0]));

Of course, if the idea with a SEGFAULT will be able to display" post mortem "stack trace.

@azenla
Copy link

azenla commented Mar 25, 2015

Thanks! @mezoni Let me know when you make the changes to unsafe_extension, I'll recompile for ya :)

@mezoni
Copy link
Author

mezoni commented Mar 26, 2015

Thanks! @mezoni Let me know when you make the changes to unsafe_extension, I'll recompile for ya :)

It works!

Unhandled exception:
Segmentation fault
#0      Unsafe._readInt32 (package:unsafe_extension/src/unsafe_extension.dart:982:72)
#1      Unsafe.readInt32 (package:unsafe_extension/src/unsafe_extension.dart:450:22)
#2      main (file:///home/andrew/dart/for_web/unsafe_extension/test/test_sigsegv.dart:4:19)
#3      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:255)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:142)

Update instruction.

  1. Clone from GitHub latest version of unsafe_extension
  2. Delete files that you want recompile (eg. compiled/X86/macos/libunsafe_extension.dylib)
  3. Delete any binaries in lib/src
  4. Open and run tool/setup.dart

If you want repeat a recompilation then you should repeat steps everything from step 2

Last step:
5. Delete any binaries in lib/src

P.S.

This binary already compiled X86/linux/libunsafe_extension.so
I will recompile other later today (Windows 32/64, Linux 64)

Temporary test file can be found here test/test_sigsegv.dart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants