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

[FFI][full dart code, equal c code, output] open return-ing error, for direct and sync io #53526

Closed
gintominto5329 opened this issue Sep 14, 2023 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@gintominto5329
Copy link

hello,

I have created a DBMS, which need to do DIRECT and SYNC, io ops, on LARGE files.

The open ffi function returning error 20.

SDK version

Dart SDK: 3.2.0-140.0.dev (Thu Sep 7 01:04:10 2023 -0700); "linux_x64", "debian-latest", "amd64"

Dart code (work, but ffi function returns -1(errno 20))

import "dart:convert" as convert;
import "dart:ffi" as ffi;
import "package:ffi/ffi.dart" as ffi;

void main() {
  const //
      O_CREAT = 0100,
      O_RDWR = 02,
      O_SYNC = 04010000,
      O_DIRECT = 040000,
      O_DSYNC = 010000,
      O_LARGEFILE = 0100000,
      S_IRUSR = 0400,
      S_IWUSR = 0200;

  final //
      path = (const convert.Utf8Encoder()).convert("/tmp/m.c"),
      len = path.length,
      memory = ffi.malloc.allocate<ffi.Uint8>(len + 1 /* for string de-limiter */
          );

  {
    final array = memory.asTypedList(len + 1);

    int i = 0;

    while (i < len) //
      array[i] = path[i++];

    array[len] = 0;
  }

  final //
      fd = ffi.DynamicLibrary.process().lookupFunction<
          ffi.Int32 Function(ffi.Pointer<ffi.Uint8> pathname, ffi.Int32 flags, ffi.Uint32 mode), //
          int Function(ffi.Pointer<ffi.Uint8> file__path, int flags, int mode)>(
        "open64",
      )(
        memory,
        (((O_DSYNC | O_LARGEFILE) | O_SYNC | O_DIRECT) | O_CREAT | O_RDWR),
        (S_IRUSR | S_IWUSR),
      ),
      errno = ffi.DynamicLibrary.process()
          .lookupFunction<
              ffi.Pointer<ffi.Int32> Function(), //
              ffi.Pointer<ffi.Int32> Function()>(
            "__errno_location",
          )()
          .value;

  print("open: $fd");
  print("errno: $errno");
}

Output of dart code

open: -1
errno: 20

Equivalent C code (working correct, fully)

#define _GNU_SOURCE 1
#define _FILE_OFFSET_BITS 64

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main()
{
	int fd = open64(
      "/tmp/m.c",
      (((O_DSYNC | O_LARGEFILE) | O_SYNC | O_DIRECT) | O_CREAT | O_RDWR),
      (S_IRUSR | S_IWUSR)
    );

	printf("%d(fd)\n", fd);
}

thanks

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi labels Sep 14, 2023
@mkustermann
Copy link
Member

The errno value can be clobbered due to other code having run before accessing it in Dart.
=> I think this may be #38832

@mraleph
Copy link
Member

mraleph commented Sep 14, 2023

Dart does not have octal numbers literals, so your flag values are incorrect.

Also I suggest using other channels (dart.dev/community) to get help with your questions. Issue tracker is for reporting bugs and feature requests.

@mraleph mraleph closed this as completed Sep 14, 2023
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. library-ffi
Projects
None yet
Development

No branches or pull requests

4 participants