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

Segmentation fault while deconstructing isolate on dart console app exit #49460

Closed
timsneath opened this issue Jul 16, 2022 · 4 comments
Closed
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. P2 A bug or feature request we're likely to work on

Comments

@timsneath
Copy link
Contributor

timsneath commented Jul 16, 2022

Running the dart_console demo example crashes on the latest Dart build checked into the flutter master channel, running on an M1 MacBook Air:

===== CRASH =====
si_signo=Segmentation fault: 11(11), si_code=2, si_addr=0x1e002bc00640026
version=2.19.0-3.0.dev (dev) (Fri Jul 15 07:03:13 2022 -0700) on "macos_arm64"
pid=21167, thread=259, isolate_group=(nil)(0x0), isolate=(nil)(0x0)
isolate_instructions=0, vm_instructions=100e077e0
  pc 0x0000000100f4a568 fp 0x000000016f04a480 dart::Isolate::~Isolate()+0x88
  pc 0x0000000100f4aed8 fp 0x000000016f04a500 dart::Isolate::LowLevelCleanup(dart::Isolate*)+0xe0
  pc 0x0000000100f4c6c0 fp 0x000000016f04af50 dart::Isolate::Shutdown()+0x184
  pc 0x00000001014a5dbc fp 0x000000016f04b4a0 Dart_ShutdownIsolate+0xac
  pc 0x0000000100de2060 fp 0x000000016f04b550 dart::bin::RunMainIsolate(char const*, char const*, dart::bin::CommandLineOptions*)+0x2cc
  pc 0x0000000100de2cf4 fp 0x000000016f04b6c0 dart::bin::main(int, char**)+0x60c
  pc 0x0000000100de3b08 fp 0x000000016f04b6d0 main+0xc
  pc 0x000000010355908c fp 0x000000016f04b820 Unknown symbol
-- End of DumpStackTrace
[1]    21167 abort      dart example/demo.dart

dart --version reports:

Dart SDK version: 2.19.0-3.0.dev (dev) (Fri Jul 15 07:03:13 2022 -0700) on "macos_arm64"

I tested this on various other versions of Dart:

  • [ERROR] Dart SDK version: 2.19.0-edge.ea7f3d4d967be60ff78d3abbc12bcd67c7a28af9 (be) (Sat Jul 16 06:59:02 2022 +0000) on "macos_arm64"
  • [ERROR] Dart SDK version: 2.18.0-165.1.beta (beta) (Mon Jun 13 15:34:45 2022 +0200) on "macos_arm64"
  • [NO ERROR] Dart SDK version: 2.17.6 (stable) (Tue Jul 12 12:54:37 2022 +0200) on "macos_arm64"
  • [NO ERROR] Dart SDK version: 2.19.0-edge.ea7f3d4d967be60ff78d3abbc12bcd67c7a28af9 (be) (Sat Jul 16 06:59:02 2022 +0000) on "macos_x64" (running on Rosetta)

So it looks like something regressed on ARM64 builds between stable and beta.

Repro by cloning https://github.com/timsneath/dart_console/tree/f88ab598bf896878b347013400049d0702eecd0c and running dart example/demo.dart from the top-level directory.

@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Jul 17, 2022
@a-siva a-siva added the P2 A bug or feature request we're likely to work on label Jul 18, 2022
@aam
Copy link
Contributor

aam commented Jul 19, 2022

It looks like culprit is the way how variadic ioctl function is defined in dart https://github.com/timsneath/dart_console/blob/f88ab598bf896878b347013400049d0702eecd0c/lib/src/ffi/unix/ioctl.dart#L35

// int ioctl(int, unsigned long, ...);
typedef IOCtlNative = Int32 Function(Int32, Int64, Pointer<Void>);
typedef IOCtlDart = int Function(int, int, Pointer<Void>);

Such ioctl invocation ends up corrupting vm isolate structure causing a crashes.

thanks @rmacnak-google for lending a hand with this!

cc @dcharkes

@aam
Copy link
Contributor

aam commented Jul 19, 2022

see dart-lang/native#238 that tracks ffi support for variadic functions

@aam aam closed this as completed Jul 19, 2022
@dcharkes
Copy link
Contributor

dcharkes commented Jul 24, 2022

see dart-lang/native#238 that tracks ffi support for variadic functions

This tracks package:ffigens support for variadic functions.

Tracking bug for dart:ffi variadic function support:

@Sunbreak
Copy link

  • [ERROR] Dart SDK version: 2.19.0-32.0.dev (dev) (Sun Jul 24 15:18:55 2022 -0700) on "macos_arm64"
  • [NO ERROR] Dart SDK version: 2.18.0-271.4.beta (beta) (Tue Jul 26 10:14:06 2022 +0200) on "macos_arm64"
// int ioctl(int, unsigned long, ...);
import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';

typedef IOCtlNative = Int32 Function(Int32, Int64, Pointer<Void>);
typedef IOCtlDart = int Function(int, int, Pointer<Void>);

final TIOCGWINSZ = Platform.isMacOS ? 0x40087468 : 0x5413;
const STDIN_FILENO = 0;
const STDOUT_FILENO = 1;
const STDERR_FILENO = 2;

// struct winsize {
//      unsigned short  ws_row;         /* rows, in characters */
//      unsigned short  ws_col;         /* columns, in characters */
//      unsigned short  ws_xpixel;      /* horizontal size, pixels */
//      unsigned short  ws_ypixel;      /* vertical size, pixels */
// };
class WinSize extends Struct {
  @Int16()
  external int ws_row;

  @Int16()
  external int ws_col;

  @Int16()
  external int ws_xpixel;

  @Int16()
  external int ws_ypixel;
}

void main() {
  final ioctl = DynamicLibrary.process().lookupFunction<IOCtlNative, IOCtlDart>('ioctl');

  final winSizePointer = calloc<WinSize>();
  final result = ioctl(STDOUT_FILENO, TIOCGWINSZ, winSizePointer.cast());
  print('result is $result');

  final winSize = winSizePointer.ref;
  print('Per ioctl, this console window has ${winSize.ws_col} cols and '
      '${winSize.ws_row} rows.');

  calloc.free(winSizePointer);
}

copybara-service bot pushed a commit that referenced this issue Jan 20, 2023
This CL introduces `VarArgs` to `NativeFunction` signatures. The
`VarArgs` type takes a single type argument. This type argument is a
subtype of `NativeType` if there is a single variadic argument, and a
record with native types if there are multiple variadic arguments.
For example:
`NativeFunction<Void Function(Pointer<Char>, VarArgs<(Int32,Int32)>)>`
for calling refering to a `printf` binding with two `int32_t` arguments
passed as variadic arguments.

The logic of the native calling conventions are detailed in
https://dart-review.googlesource.com/c/sdk/+/278342.
Here we explain how this influences the FFI pipeline.

First, now that `VarArgs` is part of signatures, we have to unwrap
that when with the C types in the CFE transform and checking (analyzer
is in a separate CL), and also in the marshaller when looking up the
C type of arguments.

Second, we have to deal with `BothNativeLocations`. On windows x64,
floating point arguments must be passed both in FPU _and_ CPU
registers. For FFI calls, we solve this in the argument moves by just
copying to both locations. For FFI callbacks, we just take the FPU
register location (which avoids an extra bitcast).

Third, on System-V, we have to pass an upper bound of the number of
XMM registers used in AL. This means we instead RAX, we use R13 for the
target address. For variadic calls, we always pass 8 in AL as the valid
upper bound. We could consider passing the actual number of XMM
registers used.
We keep using RAX as default register for the function address on non-
variadic calls, because changing to R13 (the first free) register
creates more spilling in leaf calls. R13 is callee-saved while RAX is
not, so using R13 instead of RAX causes us to have to spill the value
from RAX on leaf calls.

Fourth, on both x64 and RISC-V, we pass floats in integer locations.
`EmitNativeMove` has been modified to deal with this, so that we do not
have to insert more `BitCastInstr`s.

The tests are generated by a test generator: `tests/ffi/generator/`.

The formatter doesn't support records yet, so the tests are not properly
formatted.
Bug: #50798

TEST=tests/ffi/*_varargs_*

Closes: #38578
Closes: #49460
Closes: #50858

Change-Id: I6a6296fe972527f8a54ac75a630131769e3cc540
Cq-Include-Trybots: luci.dart.try:vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-kernel-win-debug-ia32-try,vm-kernel-linux-debug-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-nnbd-win-release-ia32-try,vm-kernel-nnbd-win-debug-x64-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try,vm-kernel-precomp-android-release-arm64c-try,vm-kernel-precomp-android-release-arm_x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-precomp-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,app-kernel-linux-debug-x64-try,vm-kernel-mac-release-arm64-try,vm-kernel-nnbd-mac-debug-arm64-try,vm-kernel-nnbd-mac-debug-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276921
Reviewed-by: Devon Carew <devoncarew@google.com>
Reviewed-by: Ryan Macnak <rmacnak@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. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

6 participants