dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.18k stars 1.56k forks source link

gen_snapshot crashes when generating an AOT snapshot of dartdev #54128

Closed a-siva closed 10 months ago

a-siva commented 10 months ago
asiva-desktop[sdk]>tools/sdks/dart-sdk/bin/dart --dfe=tools/sdks/dart-sdk/bin/snapshots/kernel-service.dart.snapshot out/DebugX64/gen/utils/gen_kernel/bootstrap_gen_kernel.dill --packages=.dart_tool/package_config.json --platform=out/DebugX64/vm_platform_strong.dill --aot --output=/tmp/generate_dartdev_snapshot.dart.dill --depfile=/tmp/generate_dartdev_snapshot.dart.dill.d pkg/dartdev/bin/dartdev.dart
asiva-desktop[sdk]>/usr/local/google/home/asiva/workspace/dart-nnbd/sdk/out/DebugX64/gen_snapshot --deterministic --snapshot-kind=app-aot-elf --elf=out/DebugX64/gen/dartdev_aot.dart.snapshot out/DebugX64/gen/utils/dartdev/generate_dartdev_snapshot.dart.dill
../../runtime/vm/compiler/backend/flow_graph_checker.cc: 547: error: expected: (arg_def == env_def) || (arg_def->IsConstant() && env_def->IsConstant() && arg_def->AsConstant()->value().ptr() == env_def->AsConstant()->value().ptr()) (arg_def=v174 <- UnboxedConstant(#0) int64, env_def=v3 <- Parameter(1) T{*?~})
version=3.3.0-edge.3939bf3dadd4418a111743cb33490408666daa2c (be) (Fri Nov 10 21:25:25 2023 +0000) on "linux_x64"
pid=433636, thread=433636, isolate_group=isolate(0x55af1c54a830), isolate=(nil)((nil))
os=linux, arch=x64, comp=no, sim=no
isolate_instructions=0, vm_instructions=0
fp=7ffc8b7e2240, sp=7ffc8b7e2108, pc=55af1b14669c
  pc 0x000055af1b14669c fp 0x00007ffc8b7e2240 dart::Profiler::DumpStackTrace+0x7c
  pc 0x000055af1ae882f4 fp 0x00007ffc8b7e2320 dart::Assert::Fail+0x84
  pc 0x000055af1b550eb8 fp 0x00007ffc8b7e2390 dart::FlowGraphChecker::AssertArgumentsInEnv+0x298
  pc 0x000055af1b54f675 fp 0x00007ffc8b7e2410 dart::FlowGraphChecker::VisitInstruction+0x2b5
  pc 0x000055af1b54efa5 fp 0x00007ffc8b7e2470 dart::FlowGraphChecker::VisitInstructions+0x305
  pc 0x000055af1b54e9cc fp 0x00007ffc8b7e24f0 dart::FlowGraphChecker::VisitBlocks+0x38c
  pc 0x000055af1b64d47e fp 0x00007ffc8b7e25c0 dart::CompilerPass::Run+0x21e
  pc 0x000055af1b64d9d6 fp 0x00007ffc8b7e25e0 dart::CompilerPass::RunPipeline+0x1f6
  pc 0x000055af1b4fa36b fp 0x00007ffc8b7e2cc0 dart::PrecompileParsedFunctionHelper::Compile+0x4cb
  pc 0x000055af1b4fb157 fp 0x00007ffc8b7e3580 dart::PrecompileFunctionHelper+0x397
  pc 0x000055af1b4f573c fp 0x00007ffc8b7e36a0 dart::Precompiler::CompileFunction+0x1ac
  pc 0x000055af1b4f40fe fp 0x00007ffc8b7e3740 dart::Precompiler::ProcessFunction+0x1ae
  pc 0x000055af1b4ee6d4 fp 0x00007ffc8b7e3790 dart::Precompiler::Iterate+0x94
  pc 0x000055af1b4eaf1c fp 0x00007ffc8b7e40a0 dart::Precompiler::DoCompileAll+0x192c
  pc 0x000055af1b4e9546 fp 0x00007ffc8b7e4530 dart::Precompiler::CompileAll+0xb6
  pc 0x000055af1b72834f fp 0x00007ffc8b7e4690 Dart_Precompile+0x1bf
  pc 0x000055af1ae5a294 fp 0x00007ffc8b7e4820 dart::bin::main+0x8e4
-- End of DumpStackTrace
=== Crash occurred when compiling package:http_multi_server/http_multi_server.dart_HttpMultiServer__loopback@228067801 in AOT mode in OptimizeBranches pass
*** BEGIN CFG
OptimizeBranches
==== package:http_multi_server/http_multi_server.dart_HttpMultiServer__loopback@228067801 (RegularFunction)
B0[graph]:0 {
      v0 <- Constant(#null) T{Null?}
      v1 <- Constant(#<optimized out>) T{Sentinel~}
      v15 <- Constant(#Type: SocketException) T{_Type}
      v17 <- Constant(#true) T{bool}
      v24 <- Constant(#0) [0, 0] T{_Smi}
      v27 <- Constant(#1) [1, 1] T{_Smi}
      v34 <- Constant(#2) [2, 2] T{_Smi}
      v39 <- Constant(#5) [5, 5] T{_Smi}
      v41 <- Constant(#TypeArguments: (H33910da4) [Type: HttpServer]) T{TypeArguments}
      v53 <- Constant(#TypeArguments: (H47f2607) [Type: HttpRequest]) T{TypeArguments}
      v155 <- UnboxedConstant(#5) [5, 5] int64
      v156 <- UnboxedConstant(#1) [1, 1] int64
      v174 <- UnboxedConstant(#0) int64
}
B1[function entry]:2 {
      v31 <- SpecialParameter(ArgDescriptor) T{_ImmutableList}
}
    v32 <- LoadField(v31 . ArgumentsDescriptor.positional_count {final}) [0, 4611686018427387903] T{_Smi}
    v33 <- LoadField(v31 . ArgumentsDescriptor.count {final}) [0, 4611686018427387903] T{_Smi}
    v35 <- BinarySmiOp:4(- [tr], v33, v34) [-2, 4611686018427387901] T{_Smi}
    v36 <- LoadIndexedUnsafe(rbp[v35 + 24]) [-9223372036854775808, 9223372036854775807] int64
    v37 <- LoadIndexedUnsafe(rbp[v35 + 16]) T{_Closure}
    Branch if RelationalOp:6(<=, v27, v35) T{bool} goto (2, 3)
B2[target]:12
    v59 <- LoadIndexedUnsafe(rbp[v35 T{_Smi} + 8]) [-9223372036854775808, 9223372036854775807] T{int}
    v149 <- UnboxInt64([non-speculative], v59) [-9223372036854775808, 9223372036854775807] int64
    goto:20 B5
B3[target]:14
    goto:18 B5
B5[join]:16 pred(B2, B3) {
      v40 <- phi(v149 T{int}, v155 T{_Smi}) alive [-9223372036854775808, 9223372036854775807] int64 T{int}
}
    CheckStackOverflow:26(stack=0, loop=0)
    Call1ArgStub:30(InitAsync(v41))
    v103 <- LoadStaticField:4(supportsIPv4, CallsInitializer) T{_Future}
    v43 <- Suspend:36(Await(v103)) T{*?}
    v157 <- AssertBoolean:40(v43) T{bool}
    Branch if StrictCompare:42(!==, v157 T{bool}, v17) goto (6, 7)
B6[target]:46
    v120 <- LoadStaticField:4(loopbackIPv6, CallsInitializer) T{_InternetAddress}
    v147 <- BoxInt64(v36) [-9223372036854775808, 9223372036854775807] T{int}
    v57 <- ClosureCall:54( closure=v37<0>, v37, v120 T{_InternetAddress}, v147 T{int}) T{*?}
    v58 <- Suspend:58(Await(v57)) T{*?}
    Return:64(v58)
B7[target]:66
    v117 <- LoadStaticField:4(loopbackIPv4, CallsInitializer) T{_InternetAddress}
    v148 <- BoxInt64(v36) [-9223372036854775808, 9223372036854775807] T{int}
    v45 <- ClosureCall:74( closure=v37<0>, v37, v117 T{_InternetAddress}, v148 T{int}) T{*?}
    v46 <- Suspend:78(Await(v45)) T{*?}
    v97 <- LoadStaticField:4(supportsIPv6, CallsInitializer) T{_Future}
    v48 <- Suspend:86(Await(v97)) T{*?}
    v158 <- AssertBoolean:90(v48) T{bool}
    Branch if StrictCompare:92(!==, v158 T{bool}, v17) goto (8, 9)
B8[target]:96
    Return:100(v46)
B9[target]:102
    goto:106 B10
B10[join try_idx 0]:104 pred(B9)
    v106 <- LoadStaticField:4(loopbackIPv6, CallsInitializer) T{_InternetAddress}
    v50 <- InstanceCall:112( get:port<0>, v46 IC[0: ]) [-9223372036854775808, 9223372036854775807] int64
    v162 <- Redefinition(v46 ^ T{*}) T{*}
    v150 <- BoxInt64(v50) [-9223372036854775808, 9223372036854775807] T{int}
    v51 <- ClosureCall:118( closure=v37<0>, v37, v106 T{_InternetAddress}, v150 T{int}) T{*?}
    v52 <- Suspend:122(Await(v51)) T{*?}
    v54 <- AllocateObject:126(cls=HttpMultiServer, v53) T{HttpMultiServer}
    v87 <- CreateArray:12(v0 T{Null?}, v34 T{_Smi}) T{_List}
    StoreIndexed:14(v87, v24 T{_Smi}, v162 T{X0})
    StoreIndexed:16(v87, v27 T{_Smi}, v52 T{X0?})
    v113 <- AllocateObject:10(cls=_GrowableList, v41 T{TypeArguments}) T{_GrowableList}
    StoreField(v113 . GrowableObjectArray.data = v87 T{_List}, NoStoreBarrier)
    StoreField(v113 T{_GrowableList} . GrowableObjectArray.length = v34 T{_Smi}, NoStoreBarrier)
    StaticCall:130( HttpMultiServer.<0> v54, v113 T{_GrowableList})
    Return:134(v54)
B12[target catch try_idx -1 catch_try_idx 0] {
      v2 <- Parameter(0) T{*?~}
      v3 <- Parameter(1) T{*?~}
      v4 <- Parameter(2) T{*?~}
      v5 <- Parameter(3) T{*?~}
      v6 <- Parameter(4) T{*?~}
      v7 <- Parameter(5) T{*?~}
      v8 <- Parameter(6) T{*?~}
      v9 <- SpecialParameter(Exception) T{*}
      v10 <- SpecialParameter(StackTrace) T{*?}
      v11 <- Parameter(9) T{*?~}
      v12 <- Parameter(10) T{*?~}
      v13 <- Parameter(11) T{*?~}
      v14 <- Parameter(12) T{*?~}
}
    v16 <- InstanceOf:232(v9 IS SocketException, instantiator_type_args(v0), function_type_args(v0)) T{bool}
    Branch if StrictCompare:234(===, v16, v17) goto (22, 23)
B22[target]:238
    v159 <- Redefinition(v9 ^ T{SocketException}) T{SocketException}
    v18 <- InstanceCall:140( close<0>, v12 IC[0: ]) T{_Future}
    Suspend:144(Await(v18))
    v19 <- InstanceCall:148( get:osError<0>, v159 T{SocketException} IC[0: ], result_type = T{OSError?}) T{OSError?}
    Branch if StrictCompare:152(===, v19, v0) goto (13, 14)
B13[target]:156
    goto:164 B15
B14[target]:158
    v161 <- Redefinition(v19 ^ T{OSError}) T{OSError}
    v20 <- LoadField(v161 T{OSError} . errorCode {final}) [-9223372036854775808, 9223372036854775807] int64
    v151 <- BoxInt64(v20) [-9223372036854775808, 9223372036854775807] T{int}
    goto:166 B15
B15[join]:162 pred(B13, B14) {
      v21 <- phi(v0, v151 T{int}) alive T{int?}
}
    v70 <- LoadStaticField:4(_addressInUseErrno@228067801, CallsInitializer) [-4611686018427387904, 4611686018427387903] T{_Smi}
    Branch if StrictCompare(!==, v21, v70 T{_Smi}) T{bool} goto (16, 17)
B16[target]:178
    ReThrow:182(v159 T{SocketException}, v10)
B17[target]:184
    Branch if StrictCompare(!==, v3 T{int??}, v24) T{bool} goto (18, 19)
B18[target]:194
    ReThrow:198(v159 T{SocketException}, v10)
B19[target]:200
    Branch if StrictCompare(===, v5 T{int??}, v24) T{bool} goto (20, 21)
B20[target]:210
    ReThrow:214(v159 T{SocketException}, v10)
B21[target]:216
    v64 <- CheckNull:218(v5 T{int??}, NoSuchMethodError) [-9223372036854775808, 9223372036854775807] T{int?}
    v153 <- UnboxInt64([non-speculative], v64 T{int?}) [-9223372036854775808, 9223372036854775807] int64
    v28 <- BinaryInt64Op(- [tr], v153 T{int?}, v156 T{_Smi}) [-9223372036854775808, 9223372036854775807] int64
    v154 <- BoxInt64(v28) [-9223372036854775808, 9223372036854775807] T{int}
    v29 <- StaticCall:220( _loopback@228067801<0> v174, v4, v154 T{int}) T{_Future}
    v30 <- Suspend:224(Await(v29)) T{*?}
    Return:230(v30)
B23[target]:240
    ReThrow:242(v9, v10)
*** END CFG
Abort
a-siva commented 10 months ago

//cc @alexmarkov

alexmarkov commented 10 months ago

Small repro:

@pragma('vm:never-inline')
void foo(int port) {
  try {
    print('hi');
  } catch (e) {
    if (port != 0) rethrow;
    bar(port);
  }
}

@pragma('vm:never-inline')
void bar(int x) {
  print(x);
}

void main() {
  foo(int.parse('2'));
  bar(int.parse('3'));
}
DART_CONFIGURATION=DebugX64 pkg/vm/tool/precompiler2 --print-environments foo.dart foo.snapshot
../../runtime/vm/compiler/backend/flow_graph_checker.cc: 547: error: expected: (arg_def == env_def) || (arg_def->IsConstant() && env_def->IsConstant() && arg_def->AsConstant()->value().ptr() == env_def->AsConstant()->value().ptr()) (arg_def=v30 <- UnboxedConstant(#0) int64, env_def=v2 <- Parameter(0) T{*?~})
version=3.3.0-edge.dd6c961bf0582b94a578ede5a6767988d0f78aed (main) (Tue Nov 21 18:55:01 2023 +0000) on "linux_x64"
pid=1631311, thread=1631311, isolate_group=isolate(0x55b55f186830), isolate=(nil)((nil))
os=linux, arch=x64, comp=no, sim=no
isolate_instructions=0, vm_instructions=0
fp=7fff3020c2a0, sp=7fff3020c168, pc=55b55e71cd9c
  pc 0x000055b55e71cd9c fp 0x00007fff3020c2a0 dart::Profiler::DumpStackTrace+0x7c
  pc 0x000055b55e45ebb4 fp 0x00007fff3020c380 dart::Assert::Fail+0x84
  pc 0x000055b55eb279b8 fp 0x00007fff3020c3f0 dart::FlowGraphChecker::AssertArgumentsInEnv+0x298
  pc 0x000055b55eb26175 fp 0x00007fff3020c470 dart::FlowGraphChecker::VisitInstruction+0x2b5
  pc 0x000055b55eb25aa5 fp 0x00007fff3020c4d0 dart::FlowGraphChecker::VisitInstructions+0x305
  pc 0x000055b55eb254cc fp 0x00007fff3020c550 dart::FlowGraphChecker::VisitBlocks+0x38c
  pc 0x000055b55ec2491e fp 0x00007fff3020c620 dart::CompilerPass::Run+0x21e
  pc 0x000055b55ec24e76 fp 0x00007fff3020c640 dart::CompilerPass::RunPipeline+0x1f6
  pc 0x000055b55ead0bbb fp 0x00007fff3020cd20 dart::PrecompileParsedFunctionHelper::Compile+0x4cb
  pc 0x000055b55ead19a7 fp 0x00007fff3020d5e0 dart::PrecompileFunctionHelper+0x397
  pc 0x000055b55eacbf8c fp 0x00007fff3020d700 dart::Precompiler::CompileFunction+0x1ac
  pc 0x000055b55eaca94e fp 0x00007fff3020d7a0 dart::Precompiler::ProcessFunction+0x1ae
  pc 0x000055b55eac4f24 fp 0x00007fff3020d7f0 dart::Precompiler::Iterate+0x94
  pc 0x000055b55eac176c fp 0x00007fff3020e100 dart::Precompiler::DoCompileAll+0x192c
  pc 0x000055b55eabfd96 fp 0x00007fff3020e590 dart::Precompiler::CompileAll+0xb6
  pc 0x000055b55ecffaef fp 0x00007fff3020e6f0 Dart_Precompile+0x1bf
  pc 0x000055b55e4309f4 fp 0x00007fff3020e880 dart::bin::main+0x8e4
-- End of DumpStackTrace
=== Crash occurred when compiling file:///.../foo.dart_::_foo in AOT mode in OptimizeBranches pass
*** BEGIN CFG
OptimizeBranches
==== file:///.../foo.dart_::_foo (RegularFunction)
B0[graph]:0 {
      v0 <- Constant(#null) T{Null?}
      v1 <- Constant(#<optimized out>) T{Sentinel~}
      v8 <- Constant(#0) [0, 0] T{_Smi}
      v18 <- Constant(#hi) T{_OneByteString}
      v30 <- UnboxedConstant(#0) int64
} env={ v1, v0, v0, v0, v0, v0 }
B1[function entry]:2 {
      v17 <- Parameter(0) [-9223372036854775808, 9223372036854775807] int64
} env={ v17, v0, v0, v0, v0, v0 }
    CheckStackOverflow:8(stack=0, loop=0)
    goto:14 B3 env={ v17, v0, v0, v0, v0, v0 }
B3[join try_idx 0]:12 pred(B1) env={ v17, v0, v0, v0, v0, v0 }
    StaticCall:16( printToConsole<0> v18 T{_OneByteString}) env={ v18, v0, v18 T{_OneByteString}, v0, v18 T{_OneByteString} } env={ v17, v0, v0, v0, v0, v0 }
    goto:20 B4 env={ v17, v0, v0, v0, v0, v0 }
B5[target catch try_idx -1 catch_try_idx 0] {
      v2 <- Parameter(0) T{*?~}
      v3 <- Parameter(1) T{*?~}
      v4 <- Parameter(2) T{*?~}
      v5 <- SpecialParameter(Exception) T{*}
      v6 <- SpecialParameter(StackTrace) T{*?}
      v7 <- Parameter(5) T{*?~}
} env={ v2, v3, v4, v5, v6, v7 }
    Branch if StrictCompare(!==, v2 T{int??}, v8) T{bool} goto (6, 7) env={ v2, v4, v1, v5, v6, v1, v2, v8 }
B6[target]:32 env={ v1, v4, v1, v5, v6, v1 }
    ReThrow:36(v5, v6) env={ v1, v4, v1, v1, v1, v1, v5, v6 }
B7[target]:38 env={ v2, v4, v1, v1, v1, v1 }
    StaticCall:40( bar<0> v30) env={ v1, v4, v1, v1, v1, v1, v2 }
    goto:42 B4 env={ v1, v4, v1, v1, v1, v1 }
B4[join]:16 pred(B3, B7) {
      v12 <- phi(v0, v4) alive T{*?~}
} env={ v1, v12, v1, v1, v1, v1 }
    Return:46(v0) env={ v1, v12, v1, v1, v1, v1, v0 }
*** END CFG

Constant propagator replaces only input uses of port with constant 0, leaving env uses as is.

Should we replace all uses including environment uses? Or maybe just relax flow graph checker to allow this discrepancy? @mraleph WDYT?

alexmarkov commented 10 months ago

It turns out changing conditional constant propagation to update environments is not enough, as it may replace a use of redefinition with a constant while environment references an original definition. For this to work we need to consistently update environments when inserting all sorts of redefinitions and boxing/unboxing. This seems like an overkill, so I'm leaning towards relaxing flow graph checker: https://dart-review.googlesource.com/c/sdk/+/338941.