Skip to content

Commit 2fbab0a

Browse files
committed
Be more tolerant of unexpected struct sizes for memory errors
1 parent 3e9db6e commit 2fbab0a

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/DecodeWheaRecord/Errors/UEFI/Memory.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal sealed class WHEA_MEMORY_ERROR_SECTION : WheaRecord {
4040
internal const uint MinStructSize = 73;
4141

4242
// Size up to and including the ModuleHandle field
43-
private const uint StructSizeWin1803 = 80;
43+
private const uint ExtendedStructSize = 80;
4444

4545
private WHEA_MEMORY_ERROR_SECTION_VALIDBITS _ValidBits;
4646

@@ -70,11 +70,11 @@ internal sealed class WHEA_MEMORY_ERROR_SECTION : WheaRecord {
7070
[JsonProperty(Order = 8)]
7171
public ushort Bank;
7272

73-
// From Windows 10, version 1803
73+
// Only with extended structure
7474
[JsonProperty(Order = 8)]
7575
public byte BankAddress => (byte)Bank;
7676

77-
// From Windows 10, version 1803
77+
// Only with extended structure
7878
[JsonProperty(Order = 9)]
7979
public byte BankGroup => (byte)(Bank >> 8);
8080

@@ -84,7 +84,7 @@ internal sealed class WHEA_MEMORY_ERROR_SECTION : WheaRecord {
8484
[JsonProperty(Order = 11)]
8585
public ushort Row;
8686

87-
// From Windows 10, version 1803
87+
// Only with extended structure
8888
[JsonProperty(Order = 11)]
8989
public uint ExtendedRow => ((Extended & (uint)0x3) << 16) + Row;
9090

@@ -113,8 +113,10 @@ internal sealed class WHEA_MEMORY_ERROR_SECTION : WheaRecord {
113113
public string ErrorType => GetEnumFlagsAsString(_ErrorType);
114114

115115
/*
116-
* From Windows 10, version 1803
117-
*
116+
* Extended fields
117+
*/
118+
119+
/*
118120
* This field contains bits which are either interpreted as stand-alone
119121
* new fields or are added as high-order bits to existing fields. There
120122
* is no corresponding ShouldSerialize method as multiple new flags are
@@ -123,19 +125,15 @@ internal sealed class WHEA_MEMORY_ERROR_SECTION : WheaRecord {
123125
*/
124126
private byte Extended;
125127

126-
// From Windows 10, version 1803
127128
[JsonProperty(Order = 18)]
128129
public uint ChipIdentification => (uint)(Extended >> 5);
129130

130-
// From Windows 10, version 1803
131131
[JsonProperty(Order = 19)]
132132
public ushort RankNumber;
133133

134-
// From Windows 10, version 1803
135134
[JsonProperty(Order = 20)]
136135
public ushort CardHandle;
137136

138-
// From Windows 10, version 1803
139137
[JsonProperty(Order = 21)]
140138
public ushort ModuleHandle;
141139

@@ -152,16 +150,10 @@ public WHEA_MEMORY_ERROR_SECTION(WHEA_ERROR_RECORD_SECTION_DESCRIPTOR sectionDsc
152150
private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint bytesRemaining) {
153151
var structAddr = recordAddr + (int)structOffset;
154152

155-
// TODO: May be overly conservative?
156-
if (bytesRemaining != MinStructSize && bytesRemaining != StructSizeWin1803) {
157-
throw new InvalidDataException($"Unexpected structure size: {bytesRemaining}");
158-
}
159-
160-
_StructSize = bytesRemaining;
161-
153+
_StructSize = bytesRemaining >= ExtendedStructSize ? ExtendedStructSize : MinStructSize;
162154
_ValidBits = (WHEA_MEMORY_ERROR_SECTION_VALIDBITS)Marshal.ReadInt64(structAddr);
163155

164-
if (HasWin1803Fields()) {
156+
if (HasExtendedFields()) {
165157
if (ShouldSerializeRow() && ShouldSerializeExtendedRow()) {
166158
throw new InvalidDataException($"The {nameof(Row)} and {nameof(ExtendedRow)} flags in {nameof(ValidBits)} cannot both be set.");
167159
}
@@ -194,7 +186,7 @@ private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint b
194186
TargetId = (ulong)Marshal.ReadInt64(structAddr, 64);
195187
_ErrorType = (WHEA_MEMORY_ERROR_TYPE)Marshal.ReadByte(structAddr, 72);
196188

197-
if (_StructSize >= StructSizeWin1803) {
189+
if (HasExtendedFields()) {
198190
Extended = Marshal.ReadByte(structAddr, 73);
199191
RankNumber = (ushort)Marshal.ReadInt16(structAddr, 74);
200192
CardHandle = (ushort)Marshal.ReadInt16(structAddr, 76);
@@ -204,7 +196,7 @@ private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint b
204196
FinalizeRecord(recordAddr, _StructSize);
205197
}
206198

207-
private bool HasWin1803Fields() => _StructSize >= StructSizeWin1803;
199+
private bool HasExtendedFields() => _StructSize >= ExtendedStructSize;
208200

209201
[UsedImplicitly]
210202
public bool ShouldSerializeErrorStatus() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ErrorStatus) != 0;
@@ -228,10 +220,10 @@ private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint b
228220
public bool ShouldSerializeBank() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.Bank) != 0;
229221

230222
[UsedImplicitly]
231-
public bool ShouldSerializeBankAddress() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.BankAddress) != 0;
223+
public bool ShouldSerializeBankAddress() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.BankAddress) != 0;
232224

233225
[UsedImplicitly]
234-
public bool ShouldSerializeBankGroup() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.BankGroup) != 0;
226+
public bool ShouldSerializeBankGroup() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.BankGroup) != 0;
235227

236228
[UsedImplicitly]
237229
public bool ShouldSerializeDevice() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.Device) != 0;
@@ -240,7 +232,7 @@ private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint b
240232
public bool ShouldSerializeRow() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.Row) != 0;
241233

242234
[UsedImplicitly]
243-
public bool ShouldSerializeExtendedRow() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ExtendedRow) != 0;
235+
public bool ShouldSerializeExtendedRow() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ExtendedRow) != 0;
244236

245237
[UsedImplicitly]
246238
public bool ShouldSerializeColumn() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.Column) != 0;
@@ -261,16 +253,16 @@ private void WheaMemoryErrorSection(IntPtr recordAddr, uint structOffset, uint b
261253
public bool ShouldSerializeErrorType() => (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ErrorType) != 0;
262254

263255
[UsedImplicitly]
264-
public bool ShouldSerializeChipIdentification() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ChipIdentification) != 0;
256+
public bool ShouldSerializeChipIdentification() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ChipIdentification) != 0;
265257

266258
[UsedImplicitly]
267-
public bool ShouldSerializeRankNumber() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.RankNumber) != 0;
259+
public bool ShouldSerializeRankNumber() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.RankNumber) != 0;
268260

269261
[UsedImplicitly]
270-
public bool ShouldSerializeCardHandle() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.CardHandle) != 0;
262+
public bool ShouldSerializeCardHandle() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.CardHandle) != 0;
271263

272264
[UsedImplicitly]
273-
public bool ShouldSerializeModuleHandle() => HasWin1803Fields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ModuleHandle) != 0;
265+
public bool ShouldSerializeModuleHandle() => HasExtendedFields() && (_ValidBits & WHEA_MEMORY_ERROR_SECTION_VALIDBITS.ModuleHandle) != 0;
274266
}
275267

276268
// @formatter:int_align_fields true

0 commit comments

Comments
 (0)