Closed Bug 1897635 Opened 2 years ago Closed 2 years ago

Crash in [@ nsXPTCVariant::IsIndirect] on 32-bit Android

Categories

(Core :: XPCOM, defect)

ARM
Android
defect

Tracking

()

RESOLVED FIXED
128 Branch
Tracking Status
firefox-esr115 --- fixed
firefox126 --- wontfix
firefox127 --- fixed
firefox128 + fixed

People

(Reporter: gsvelto, Assigned: glandium)

References

(Regression)

Details

(4 keywords)

Crash Data

Attachments

(1 file, 3 external files, 1 obsolete file)

Crash report: https://crash-stats.mozilla.org/report/index/29504d20-37c1-414a-b23a-3d9020240519

Reason: SIGSEGV / SEGV_ACCERR

Top 10 frames:

0  libxul.so  nsXPTCVariant::IsIndirect() const  xpcom/reflect/xptcall/xptcall.h:95
0  libxul.so  invoke_copy_to_stack(unsigned int*, unsigned int*, unsigned int, nsXPTCVariant*)  xpcom/reflect/xptcall/md/unix/xptcinvoke_arm.cpp:77
0  libxul.so  NS_InvokeByIndex  xpcom/reflect/xptcall/md/unix/xptcinvoke_arm.cpp:161
1  libxul.so  CallMethodHelper::Invoke()  js/xpconnect/src/XPCWrappedNative.cpp:1620
1  libxul.so  CallMethodHelper::Call()  js/xpconnect/src/XPCWrappedNative.cpp:1174
1  libxul.so  XPCWrappedNative::CallMethod(XPCCallContext&, XPCWrappedNative::CallMode)  js/xpconnect/src/XPCWrappedNative.cpp:1120
2  libxul.so  XPC_WN_CallMethod(JSContext*, unsigned int, JS::Value*)  js/xpconnect/src/XPCWrappedNativeJSOps.cpp:966
3  libxul.so  CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::...  js/src/vm/Interpreter.cpp:480
3  libxul.so  js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstru...  js/src/vm/Interpreter.cpp:574
4  libxul.so  InternalCall(JSContext*, js::AnyInvokeArgs const&, js::CallReason)  js/src/vm/Interpreter.cpp:641

32-bit ARM/Android-specific crash, looks like it might be a regression from bug 1678386 given the timeframe. Andrew can you have a look?

Flags: needinfo?(continuation)

Well, that definitely looks suspicious, but my patch only eliminates a few trivial wrapper function. I'd be surprised if the binary changed at all.

It looks to me like bug 1678386 would have been in the 05-14 Android build, and this crash didn't show up until the 05-16 builds, a whole 2 days later. Given the high volume here, it doesn't seem likely to me that we'd go an entire update [edit: I said "2 updates" but it looks like there was only the 1 update with non-trivial crash volume] with no crashes, even assuming people update more slowly on Android. Looking at crash stats, I see almost 1200 crashes on the 20240514215634 Android Nightly build, which would have contained bug 1678386.

Looking at crash-stats, there are a lot of Fenix crashes on 20240514215634, but the next build to contain more than 3 crashes is 20240516214828. Here is the (rather large) set of changes between those two builds..

Here's some possibly-relevant changes in that range:
Bug 1849075 - Update builders to clang 18
Bug 1894716 - Change Android build to use -O2 optimization level
Bug 1892257 - Move nsStringBuffer to mfbt
Bug 1896806 - Update Kotlin to version 1.9.24
Bug 1896807 - Update AndroidX Annotation to version 1.8.0

The last 2 are Android-specific, though I don't see how they'd actually affect XPCOM so maybe don't worry about those for now. The nsStringBuffer is XPCOM-related, but I think we don't pass around raw string buffers via XPCOM so that doesn't seem too likely either.

That leaves the two compiler-related changes. I'm of course hesitant to blame the compiler, but it does look like the data here is (maybe?) derived from our xptdata.cpp which is a generated file that is almost 200,000 lines long containing one or two data structure definitions, so I could imagine that causing compiler issues.

glandium, does this sound plausible to you?

If not, I can try looking over my patch again to try and figure out if somehow I could have changed the behavior

Flags: needinfo?(continuation) → needinfo?(mh+mozilla)

Looking at Android crashes for the last 7 days, if you look at cpu info for the crashes with a signature that DOES contain nsXPTCVariant, you have:
40.29% ARMv1 ARM part(0x4100d030) features: half,thumb,fastmult,vfpv2,edsp,neon,vfpv3,tls,vfpv4,idiva,idivt
24.76% ARMv7 ARM part(0x4100d030) features: half,thumb,fastmult,vfpv2,edsp,neon,vfpv3,tls,vfpv4,idiva,idivt
7.65% ARMv1 ARM part(0x4100d0b0) features: half,thumb,fastmult,vfpv2,edsp,neon,vfpv3,tls,vfpv4,idiva,idivt
etc.

Looking at Android crashes for the last 7 days, if you look at cpu info for the crashes with a signature that DOES NOT contain nsXPTCVariant, you have:
39.96% unknown
...and basically nothing else (eg for most crashes it is blank).

Maybe this is a sign there's something CPU-specific going on? There's a rather high number of crashes per install time, but there are still around 130 distinct install times for this crash, so it isn't just one person on a specific device having a very bad time.

[Tracking Requested - why for this release]: These are all startup crashes, and look to be specific to certain CPUs. I wouldn't be surprised if Firefox is unusable for the people who are experiencing this issue.

ni? for acreskey as they also have a compiler change in the suspect range.

Flags: needinfo?(acreskey)

Do we have any 32-bit Android devices we can try to reproduce this with? I wonder if it'd reproduce in an emulator too. If so, maybe we can see if it also reproduces with GVE (and by extension, mozregression)?

Flags: needinfo?(mlobontiuroman)

Should we try disabling O2 for 32-bit builds and see what happens? Note that we're seeing 48+ hr review lag with the Play Store at the moment, so the sooner we get a build shipped there, the better.

I've got a 32-bit Android device on hand, I'll check if I can reproduce.

I can reproduce the issue on my Fairphone 2, Fenix nightly crashes on startup.

The bug is linked to a topcrash signature, which matches the following criterion:

  • Top 10 AArch64 and ARM crashes on nightly (startup)

For more information, please visit BugBot documentation.

The bug is marked as tracked for firefox128 (nightly). However, the bug still isn't assigned.

:gcp, could you please find an assignee for this tracked bug? If you disagree with the tracking decision, please talk with the release managers.

For more information, please visit BugBot documentation.

Flags: needinfo?(gpascutto)

FYI I'm running a bisection to figure out what commit caused the issue.

Attached file logcat.txt —

Hi all,

I've encountered a crash on Fenix Nightly 128.0a1 from 5/17, and Nightly from 5/20, with a Sony Xperia Z5 Premium (Android 7.1.1) device.
Unfortunately, I don't know if it has this signature, I'm not able to open it enough to get the stats from about:crashes. The crash prompt "Sorry. Firefox Nightly had a problem and crashed." keeps popping even when I selected the "Close" option.
I saved a logcat from the console, hope it helps.

I'll try finding the last good build.

The Beta 126.0b9, Beta 127.0b3, RC 126.0, Focus 126.0, and GVE 1.0.2419 (GV: 126.0-20240509170740) does not crash, work properly with the same device.
The crash is not reproducible on Xiaomi mi10 Lite (Android 10), Huawei MediaPad M2 (Android 5.1.1), LG G7 fit (Android 9), Google Pixel (Android 10).

Flags: needinfo?(mlobontiuroman)

Patch to revert arm32 back to the previous flags is looking good on Try so far: https://treeherder.mozilla.org/jobs?repo=try&revision=67ef9c559d6ca68c8473589e0cf425631ef5a456

Assignee: nobody → ryanvm
Status: NEW → ASSIGNED

Thanks for investigating, Ryan. In light of that, I'll move this over to the same component as bug 1894716.

Component: XPCOM → Performance
Product: Core → Fenix
Summary: Crash in [@ nsXPTCVariant::IsIndirect] → Crash in [@ nsXPTCVariant::IsIndirect] on 32-bit Android

As a follow-up to this, I wonder if we can/should run the startup sanity tests on more architectures than just arm64.

Flags: needinfo?(aaron.train)

This is now assigned, so I'll clear the needinfo.

Flags: needinfo?(gpascutto)
Attachment #9402775 - Attachment description: Bug 1897635 - Disable O2 optimization for ARM32 CPUs. → Bug 1897635 - Disable O2 optimization for arm32 CPUs.

Thanks to an extremely helpful community member in #Fenix on Matrix, we've been able to confirm that GVE builds also reproduce the crash. Unfortunately, the Try push I ran with O2 reverted was still crashing for them :(. Gonna try to confirm that it was the clang bump next.

Attachment #9402775 - Attachment is obsolete: true
Flags: needinfo?(acreskey)

My tester on Matrix confirms that the crashes went away on the Try push with the Clang & Rust compiler updates reverted. I'm going to proceed with backing them out and respinning Android nightlies. I'll set the clang-18 bump as the regressor, though I don't really have any good way of determining whether it was that or the Rust update that did it.

Regressed by: clang-18
Component: Performance → Toolchains
Product: Fenix → Firefox Build System

Compiler bumps backed out. Android nightly respins en route.
https://hg.mozilla.org/mozilla-central/rev/b617b6edf2cc29f542fa31532b2a43f2c6a6cc2e

I'll leave this bug open for now until glandium has had a chance to look this over.

Duplicate of this bug: 1897699

Confirmed that build ID 20240520180757 is no longer crashing. It's currently waiting on Play Store review and approval.

The good news is that it's reproductible on "normal" opt builds (so, without PGO/LTO). It's sad we don't run the most basic tests on those builds... and just in case, I checked one of the earlier landings of this bug (before bug 1678386), and it has the same problem, so bug 1678386 is not involved (the hypothesis being that it could have triggered something new that the code before didn't, but that's not the case).

Flags: needinfo?(mh+mozilla)

clang ends up removing the i < paramCount guard on the first iteration of the loop in https://searchfox.org/mozilla-central/rev/00b18126ec6a14a710086eb24e6a281460a678d5/xpcom/reflect/xptcall/md/unix/xptcinvoke_arm.cpp#71 when invoke_copy_to_stack is inlined. I'm still trying to figure out whether this is caused by some subtle UB or a real bug in clang.

The consequence is that we get a crash when paramCount is 0.

Assignee: ryanvm → mh+mozilla
Component: Toolchains → XPCOM
Product: Firefox Build System → Core
Depends on: 532198

Triggered by this change in LLVM:
https://github.com/llvm/llvm-project/commit/e13bed4c5f3544c076ce57e36d9a11eefa5a7815

Before the change, the end of NS_InvokeByIndex (https://github.com/llvm/llvm-project/commit/e13bed4c5f3544c076ce57e36d9a11eefa5a7815) would be transformed to this IR:

90:                                               ; preds = %84, %4
  %91 = load ptr, ptr %0, align 4
  %92 = getelementptr inbounds ptr, ptr %91, i32 %1
  %93 = load ptr, ptr %92, align 4
  %94 = add nsw i32 %8, -3
  %95 = getelementptr inbounds i32, ptr %7, i32 %94
  %96 = load i32, ptr %95, align 4
  %97 = add nsw i32 %8, -2
  %98 = getelementptr inbounds i32, ptr %7, i32 %97
  %99 = load i32, ptr %98, align 8
  %100 = add nsw i32 %8, -1
  %101 = getelementptr inbounds i32, ptr %7, i32 %100
  %102 = load i32, ptr %101, align 4
  %103 = call noundef i32 %93(ptr noundef nonnull %0, i32 noundef %96, i32 noundef %99, i32 noundef %102) #2
  ret i32 %103

After the change, that would become:

90:                                               ; preds = %4, %84
  %91 = load ptr, ptr %0, align 4
  %92 = getelementptr inbounds ptr, ptr %91, i32 %1
  %93 = load ptr, ptr %92, align 4
  %94 = getelementptr i32, ptr %7, i32 %8
  %95 = getelementptr i32, ptr %94, i32 -3
  %96 = load i32, ptr %95, align 4
  %97 = getelementptr i32, ptr %7, i32 %8
  %98 = getelementptr i32, ptr %97, i32 -2
  %99 = load i32, ptr %98, align 4
  %100 = getelementptr i32, ptr %7, i32 %8
  %101 = getelementptr i32, ptr %100, i32 -1
  %102 = load i32, ptr %101, align 4
  %103 = call noundef i32 %93(ptr noundef nonnull %0, i32 noundef %96, i32 noundef %99, i32 noundef %102) #2
  ret i32 %103

With the new IR, after the GVN pass, it becomes:

96:                                               ; preds = %90, %4
  %97 = phi i32 [ %95, %90 ], [ undef, %4 ]
  %98 = phi i32 [ %93, %90 ], [ undef, %4 ]
  %99 = phi i32 [ %91, %90 ], [ undef, %4 ]
  %100 = load ptr, ptr %0, align 4
  %101 = getelementptr inbounds ptr, ptr %100, i32 %1
  %102 = load ptr, ptr %101, align 4
  %103 = getelementptr i32, ptr %9, i32 -3
  %104 = getelementptr i32, ptr %9, i32 -2
  %105 = getelementptr i32, ptr %9, i32 -1
  %106 = call noundef i32 %102(ptr noundef nonnull %0, i32 noundef %99, i32 noundef %98, i32 noundef %97) #2
  ret i32 %106

With the old IR, that didn't happen. Unfortunately, when paramCount is 0, we end up in the undef case annotated on %97 through %99, which is why LLVM allows itself to remove the test because, that's not supposed to happen.

Now, looking at the relevant C++ code:

  vtable_func *vtable, func;
  int base_size = (paramCount > 1) ? paramCount : 2;
  uint32_t *stack_space = (uint32_t *) __builtin_alloca(base_size * 8);

  invoke_copy_to_stack(stack_space, &stack_space[base_size * 2],
                       paramCount, params);

  vtable = *reinterpret_cast<vtable_func **>(that);
  func = vtable[methodIndex];

  return func(that, stack_space[base_size * 2 - 3],
                    stack_space[base_size * 2 - 2],
                    stack_space[base_size * 2 - 1]);

So, when paramCount is 0, we have base_size = 2, stack_space is an stack allocation of 16 bytes, invoke_copy_to_stack does nothing (so stack_space stays uninitialized), and we call func with arguments that are uninitialized.

In absolute, clang is right, this is UB. The intent is that it doesn't matter because in that case the actual func is not going to look at those arguments anyways.

The code has been fragile since its introduction in bug 532198, but in
practice it (surprisingly) hadn't caused problems so far. What the C++
implementation saves us, albeit at the cost of using undefined behavior,
is having to write several different version of the corresponding
assembly for the various different ARM variants that are supported
(including thumb vs. non-thumb).

With this being the first time the compiler breaks this code in 14
years, I'd say it's worth keeping that way. We may reconsider if it
breaks further.

An alternative way to handle the situation would be to test for
paramCount=0 and call func(that) in that case, but the compiler
generates much more code than necessary for some reason.

(In reply to Kayacan Kaya [:kaya] from comment #27)

Could this crash be relevant?

Yes, we routinely fail to generate minidumps on Android which is what's tracked there. When overall crashes spike that also spikes, as we fail to generate more than usually. Hopefully I'll finish bug 1620998 by the end of the quarter and bug 1644486 will go away (mostly).

Flags: needinfo?(mh+mozilla)
Pushed by mh@glandium.org: https://hg.mozilla.org/integration/autoland/rev/617ba43c9f55 Avoid clang 18 exploiting undefined behavior when optimizing NS_InvokeByIndex after inlining invoke_copy_to_stack. r=xpcom-reviewers,emilio
See Also: → 1897984

Set release status flags based on info from the regressing bug 1849075

Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 128 Branch

The code has been fragile since its introduction in bug 532198, but in
practice it (surprisingly) hadn't caused problems so far. What the C++
implementation saves us, albeit at the cost of using undefined behavior,
is having to write several different version of the corresponding
assembly for the various different ARM variants that are supported
(including thumb vs. non-thumb).

With this being the first time the compiler breaks this code in 14
years, I'd say it's worth keeping that way. We may reconsider if it
breaks further.

An alternative way to handle the situation would be to test for
paramCount=0 and call func(that) in that case, but the compiler
generates much more code than necessary for some reason.

Original Revision: https://phabricator.services.mozilla.com/D210984

Attachment #9403083 - Flags: approval-mozilla-beta?

The code has been fragile since its introduction in bug 532198, but in
practice it (surprisingly) hadn't caused problems so far. What the C++
implementation saves us, albeit at the cost of using undefined behavior,
is having to write several different version of the corresponding
assembly for the various different ARM variants that are supported
(including thumb vs. non-thumb).

With this being the first time the compiler breaks this code in 14
years, I'd say it's worth keeping that way. We may reconsider if it
breaks further.

An alternative way to handle the situation would be to test for
paramCount=0 and call func(that) in that case, but the compiler
generates much more code than necessary for some reason.

Original Revision: https://phabricator.services.mozilla.com/D210984

Attachment #9403085 - Flags: approval-mozilla-esr115?

beta Uplift Approval Request

  • User impact if declined: Possible crashes in downstream builds using newer compilers
  • Code covered by automated testing: no
  • Fix verified in Nightly: yes
  • Needs manual QE test: no
  • Steps to reproduce for manual QE testing: N/A
  • Risk associated with taking this patch: Low
  • Explanation of risk level: It's essentially only adding a compiler hint
  • String changes made/needed: N/A
  • Is Android affected?: no

esr115 Uplift Approval Request

  • User impact if declined: Possible crashes in downstream builds using newer compilers
  • Code covered by automated testing: no
  • Fix verified in Nightly: yes
  • Needs manual QE test: no
  • Steps to reproduce for manual QE testing: N/A
  • Risk associated with taking this patch: Low
  • Explanation of risk level: It's essentially only adding a compiler hint
  • String changes made/needed: N/A
  • Is Android affected?: no
Attachment #9403083 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
Attachment #9403085 - Flags: approval-mozilla-esr115? → approval-mozilla-esr115+
Flags: needinfo?(aaron.train)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: