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

Ensure allocated objects are fully initialized (e.g. ObjC blocks) #224

Closed
mkustermann opened this issue Oct 7, 2022 · 3 comments · Fixed by dart-archive/ffigen#475
Closed
Assignees

Comments

@mkustermann
Copy link
Member

From looking at the examples (e.g. example/objective_c/avf_audio_bindings.dart):

  ffi.Pointer<_ObjCBlock> _newBlock1(ffi.Pointer<ffi.Void> invoke, ffi.Pointer<ffi.Void> target) {
    final b = pkg_ffi.calloc.allocate<_ObjCBlock>(ffi.sizeOf<_ObjCBlock>());
    b.ref.isa = _objc_concrete_global_block1;
    b.ref.invoke = invoke;
    b.ref.target = target;
    b.ref.descriptor = _objc_block_desc1;
    final copy = _Block_copy(b.cast()).cast<_ObjCBlock>();
    pkg_ffi.calloc.free(b);
    return copy;
  }

as well as the generator in lib/src/code_generator/objc_built_in_functions.dart

  late final newBlock = ObjCInternalFunction('_newBlock', _blockCopyFunc, (Writer w, String name) {
    ...
    return '''
$blockPtr $name($voidPtr invoke, $voidPtr target) {
  final b = ${w.ffiPkgLibraryPrefix}.calloc.allocate<$blockType>(
      ${w.ffiLibraryPrefix}.sizeOf<$blockType>());
  b.ref.isa = ${concreteGlobalBlock.name};
  b.ref.invoke = invoke;
  b.ref.target = target;
  b.ref.descriptor = ${blockDescSingleton.name};
  final copy = ${_blockCopyFunc.name}(b.cast()).cast<$blockType>();
  ${w.ffiPkgLibraryPrefix}.calloc.free(b);
  return copy;
}
''';
  });

It seems that for flags field of an ObjC block isn't initialized

class _ObjCBlock extends ffi.Struct {
  external ffi.Pointer<ffi.Void> isa;
  @ffi.Int()
  external int flags;
  @ffi.Int()
  external int reserved;
  external ffi.Pointer<ffi.Void> invoke;
  external ffi.Pointer<_ObjCBlockDesc> descriptor;
  external ffi.Pointer<ffi.Void> target;
}

The memory allocation will leave the contents of memory as uninitialized, yet those (uninitialized) flags will influence how the block is e.g. copied, etc.

/cc @liamappelbe Was it intentional to not initialize flags?

@dcharkes
Copy link
Collaborator

dcharkes commented Oct 7, 2022

I believe you're looking for #233.

@mkustermann
Copy link
Member Author

I believe you're looking for #233.

It doesn't explain why .flags isn't initialized, does it?

The code seems to allocate an ObjC block (without initializing .flags) and then calls _Block_copy which is reading those flags. Maybe I misunderstand something?

@liamappelbe
Copy link
Contributor

I was assuming that the allocated memory is zero initialized. I'll fix it.

@liamappelbe liamappelbe transferred this issue from dart-archive/ffigen Nov 15, 2023
HosseinYousefi pushed a commit that referenced this issue Nov 16, 2023
Bumps [coverallsapp/github-action](https://github.com/coverallsapp/github-action) from 1.2.4 to 2.0.0.
- [Release notes](https://github.com/coverallsapp/github-action/releases)
- [Commits](coverallsapp/github-action@50c33ad...67662d2)

---
updated-dependencies:
- dependency-name: coverallsapp/github-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
HosseinYousefi pushed a commit that referenced this issue Nov 16, 2023
Bumps [coverallsapp/github-action](https://github.com/coverallsapp/github-action) from 1.2.4 to 2.0.0.
- [Release notes](https://github.com/coverallsapp/github-action/releases)
- [Commits](coverallsapp/github-action@50c33ad...67662d2)

---
updated-dependencies:
- dependency-name: coverallsapp/github-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants