{"record":{"id":"4633c519373c6f71","repo":"icsharpcode/ILSpy","slug":"invalid-field-token","errorCode":null,"errorMessage":"Invalid field token","messagePattern":"Invalid field token","errorType":"exception","errorClass":"BadImageFormatException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/IL/ILReader.cs","lineNumber":252,"sourceCode":"\r\n\t\tIType ReadAndDecodeTypeReference()\r\n\t\t{\r\n\t\t\tvar typeReference = ReadAndDecodeMetadataToken();\r\n\t\t\treturn module.ResolveType(typeReference, genericContext);\r\n\t\t}\r\n\r\n\t\tIMethod ReadAndDecodeMethodReference()\r\n\t\t{\r\n\t\t\tvar methodReference = ReadAndDecodeMetadataToken();\r\n\t\t\treturn module.ResolveMethod(methodReference, genericContext);\r\n\t\t}\r\n\r\n\t\tIField ReadAndDecodeFieldReference()\r\n\t\t{\r\n\t\t\tvar fieldReference = ReadAndDecodeMetadataToken();\r\n\t\t\tvar f = module.ResolveEntity(fieldReference, genericContext) as IField;\r\n\t\t\tif (f == null)\r\n\t\t\t\tthrow new BadImageFormatException(\"Invalid field token\");\r\n\t\t\treturn f;\r\n\t\t}\r\n\r\n\t\tILVariable[] InitLocalVariables()\r\n\t\t{\r\n\t\t\tif (body.LocalSignature.IsNil)\r\n\t\t\t\treturn Empty<ILVariable>.Array;\r\n\t\t\tImmutableArray<IType> variableTypes;\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tvariableTypes = module.DecodeLocalSignature(body.LocalSignature, genericContext);\r\n\t\t\t}\r\n\t\t\tcatch (BadImageFormatException ex)\r\n\t\t\t{\r\n\t\t\t\tWarnings.Add(\"Error decoding local variables: \" + ex.Message);\r\n\t\t\t\tvariableTypes = ImmutableArray<IType>.Empty;\r\n\t\t\t}\r\n\t\t\tvar localVariables = new ILVariable[variableTypes.Length];\r","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/IL/ILReader.cs#L234-L270","documentation":"BadImageFormatException thrown by ReadAndDecodeFieldReference when the operand token of a field instruction (ldfld/ldsfld/stfld/ldflda) resolved to an entity that is not an IField. Field instructions must reference a field; resolving their token to a method, type, or other entity means the token's table bits or row do not describe a field.","triggerScenarios":"A field-instruction operand token whose table type is wrong (points at a MethodDef/TypeDef) or whose row is mismatched; produced by corrupt metadata or an obfuscator that rewrites token tables.","commonSituations":"Obfuscated assemblies that swap token tables; assemblies damaged by tooling that rewrote the metadata stream; manually edited IL.","solutions":["Catch BadImageFormatException around decompilation and treat the assembly/method as unreadable.","Use a metadata reader to cross-check that field tokens actually point at FieldDefinition rows before decompiling.","Obtain an unobfuscated or uncorrupted copy of the binary."],"exampleFix":"// before\nvar code = decompiler.DecompileTypeAsString(typeName);\n\n// after\ntry\n{\n    var code = decompiler.DecompileTypeAsString(typeName);\n}\ncatch (BadImageFormatException ex) when (ex.Message.Contains(\"field token\"))\n{\n    logger.Warn($\"Field token corruption in {typeName}: {ex.Message}\");\n}","handlingStrategy":"try-catch","validationCode":"void VerifyFieldTokens(string path)\n{\n    using var pe = new PEReader(File.OpenRead(path));\n    var md = pe.GetMetadataReader();\n    foreach (var h in md.MethodDefinitions)\n    {\n        var body = pe.GetMethodBody(md.GetMethodDefinition(h).RelativeVirtualAddress);\n        // token table byte (>>24) for field ops should be 0x04 (FieldDef) in valid IL\n    }\n}","typeGuard":null,"tryCatchPattern":"try { var code = decompiler.DecompileTypeAsString(typeName); }\ncatch (BadImageFormatException ex) when (ex.Message.Contains(\"field token\")) { logger.Warn($\"Field token corruption: {ex.Message}\"); }","preventionTips":["Catch BadImageFormatException and mark the affected method as unreadable.","Prefer an unobfuscated copy of the assembly when available.","Cross-check field tokens against FieldDefinition rows when preprocessing metadata."],"tags":["il-reader","metadata-token","field","bad-image"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}