[release/9.0-staging] JIT: Fix invalid removal of explicit zeroing in methods without .localsinit by jakobbotsch · Pull Request #115568 · dotnet/runtime

Backport of #115556 to release/9.0-staging

Customer Impact

  • Customer reported
  • Found internally

The JIT may mistakenly remove explicit field zeroing for some struct fields in methods without .localsinit (e.g. by having the SkipLocalsInit attribute applied in C#). This can happen when the IL first zero-initializes the full struct local using e.g. initobj, and then later zeroes a particular field of the struct local using stfld. Under certain circumstances, the JIT mistakenly eliminates both explicit zeroings of the field, leaving no zero initialization present, resulting in the field containing stack garbage.

Reported by customer in #113658.

Regression

  • Yes
  • No

This was exposed by support for cross-block assertion prop enabled in #94689.

Testing

Unit test added, and tested manually on user's test case.

Risk

Low