electron: [Bug]: crash of a window, renderer process cause of memory cage and asar

Preflight Checklist

Electron Version

22.0.x

What operating system are you using?

Windows

Operating System Version

Windows 7, 10, 11

What arch are you using?

x64

Last Known Working Electron version

20.x

Expected Behavior

no crash

Actual Behavior

It actually crashes in:

cpp-marking-state-inl.h

void CppMarkingState::MarkAndPush(const EmbedderDataSlot type_slot,
                                  const EmbedderDataSlot instance_slot) {
  LocalEmbedderHeapTracer::WrapperInfo info;
>>>>  if (LocalEmbedderHeapTracer::ExtractWrappableInfo(
          isolate_, wrapper_descriptor_, type_slot, instance_slot, &info)) {
    marking_state_.MarkAndPush(
        cppgc::internal::HeapObjectHeader::FromObject(info.second));```

with exception code: 0xc0000005

At first glace this seems to be coming from a native module which does not work with the new caging system, but i removed all native modules and it still crashed. I figured that it does not happen when running locally through npm/yarn starts but after building to a package it does.

After looking a bit more careful at the stack trace i found that the calls originate from

::Archive::New(const v8::FunctionCallbackInfo<v8::Value> & args) Line 63
C:\projects\src\electron\shell\common\api\electron_api_asar.cc (63)
    auto* archive_wrap = new Archive(std::move(archive));
    archive_wrap->Wrap(args.This());

If the origin is asar this would explain that it only crashes after build and not on locally runs started with npm.

I debugged with 22.0.3 (but happens with any 22.0.x) which happens to be chrome: 108.0.5359.179 which has v8 version 10.8.168.25

Testcase Gist URL

No response

Additional Information

This crashes also on linux (kernel 6.1, x64 arch)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 21 (3 by maintainers)

Most upvoted comments

I believe this is a bug in v8, exposed recently in v22 since the v8 sandbox is enabled by default for x64/arm64 (https://chromium.googlesource.com/v8/v8.git/+/a8c27fcc9f9f15a0110a409190a2b514ec86e37f).

This seems to be caused by an out-of-bounds embedder slot index, as pinpointed by the DCHECK failure above:

# Fatal error in ..\..\v8\src\objects\js-objects-inl.h, line 329
# Debug check failed: static_cast<unsigned>(index) < static_cast<unsigned>(GetEmbedderFieldCount()) (1 vs. 1).

There’s several frames here inlined, seemingly even in the debug build, so the full call stack is really:

v8::internal::JSObject::GetEmbedderFieldOffset(int)
v8::internal::EmbedderDataSlot::EmbedderDataSlot(v8::internal::JSObject)
v8::internal::LocalEmbedderHeapTracer::EmbedderWriteBarrier(v8::internal::Heap *)
WriteBarrier::MarkingSlowFromInternalFields
internal::WriteBarrier::MarkingFromInternalFields
v8::Object::SetAlignedPointerInInternalField(int)
node::ObjectWrap::Wrap(v8::Local<v8::Object>)

Rough reproduce steps:

  1. Internal application and build of Electron, so unfortunately can’t provide project, utilizes asar file
  2. In our case this is exhibited after several attempts (1-30x) when reloading (ctrl-r)
  3. Possibly exhibited with --log-level=0 and --trace-warnings flags
  4. 64-bit build that uses the v8 sandbox

Analysis:

  1. DCHECK asserts from a lack of a field at index 1, where it’s expecting an instance field. The embedder field count in this case is 1, and it needs to be at least 2.

  2. There is an existing check on the embedder field count in src/heap/embedder-tracing-inl.h in another overload to LocalEmbedderHeapTracer::ExtractWrappableInfo: https://chromium.googlesource.com/v8/v8.git/+/refs/heads/10.8.169/src/heap/embedder-tracing-inl.h#19

  3. In the original change that check also protected the EmbedderDataSlot ctor that looked up the indexes for wrapper_descriptor.wrappable_type_index (0) and wrapper_descriptor.wrappable_instance_index (1): https://chromium.googlesource.com/v8/v8.git/+/d10f61e1/src/heap/embedder-tracing-inl.h#19

  4. However a later refactoring moved the EmbedderDataSlot out into a separate overloaded function (https://chromium-review.googlesource.com/c/v8/v8/+/3386600): https://chromium.googlesource.com/v8/v8.git/+/804aaa5c6^!/src/heap/embedder-tracing-inl.h

  5. In the same change the LocalEmbedderHeapTracer::EmbedderWriteBarrier was updated to lookup the type_slot and instance_slot indices: https://chromium-review.googlesource.com/c/v8/v8/+/3386600/14/src/heap/embedder-tracing.cc From the call stack above though this path isn’t protected by the original count check.

  6. I believe this may not be observed in non-sandbox builds because of the point where it later attempts to actually convert the embedded data slot as a pointer in EmbedderDataSlot::ToAlignedPointer: https://chromium.googlesource.com/v8/v8.git/+/refs/heads/10.8.169/src/objects/embedder-data-slot-inl.h#91 This is called from LocalEmbedderHeapTracer::ExtractWrappableInfo, in release builds this path is reached: https://chromium.googlesource.com/v8/v8.git/+/refs/heads/10.8.169/src/heap/embedder-tracing-inl.h#33

  7. The fix here seems to be to add a similar count check in LocalEmbedderHeapTracer::EmbedderWriteBarrier prior to the EmbedderDataSlot construction src/heap/embedder-tracing.cc:

void LocalEmbedderHeapTracer::EmbedderWriteBarrier(Heap* heap,
						JSObject js_object) {
	DCHECK(InUse());
	DCHECK(js_object.MayHaveEmbedderFields());
	if (cpp_heap_) {
		DCHECK_NOT_NULL(heap->mark_compact_collector());
		if (js_object.GetEmbedderFieldCount() < 2) return;

		const EmbedderDataSlot type_slot(js_object,
				  wrapper_descriptor_.wrappable_type_index);
		const EmbedderDataSlot instance_slot(
				  js_object, wrapper_descriptor_.wrappable_instance_index);
...

Note 1: The root cause is unclear to me how the embedder field count can be become different from the assumed fields for the type and instance. It’s also unclear why the Electron asar path exhibits this issue, I’m not familiar enough with the v8 project to really dig in here. However, since they already have a field count check it seems like there were some expected cases where it might have mismatched.

Note 2: As far as future v8 releases, they recently removed the local embedder code in favor of cppgc: https://chromium-review.googlesource.com/c/v8/v8/+/4174081 Unsure if the current cppgc logic is affected: https://chromium.googlesource.com/v8/v8.git/+/fd6efc347d78ff/src/heap/cppgc-js/cpp-heap.cc#785

I’ve opened a v8 issue here to get their input: https://bugs.chromium.org/p/v8/issues/detail?id=13777