{"record":{"id":"688dbc622ff5c699","repo":"icsharpcode/ILSpy","slug":"invalid-calli-metadata-token","errorCode":null,"errorMessage":"Invalid calli metadata token","messagePattern":"Invalid calli metadata token","errorType":"exception","errorClass":"BadImageFormatException","httpStatus":null,"severity":"error","filePath":"ICSharpCode.Decompiler/IL/ILReader.cs","lineNumber":1845,"sourceCode":"\t\t\t\t\t}\r\n\t\t\t\t\targuments[0] = firstArgumentInstruction;\r\n\t\t\t\t}\r\n\t\t\t\t// arguments is in reverse order of the Pop calls, thus\r\n\t\t\t\t// arguments is now in the correct evaluation order.\r\n\t\t\t\treturn arguments;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tDecodedInstruction DecodeCallIndirect()\r\n\t\t{\r\n\t\t\tStandaloneSignatureHandle signatureHandle;\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tsignatureHandle = (StandaloneSignatureHandle)ReadAndDecodeMetadataToken();\r\n\t\t\t}\r\n\t\t\tcatch (InvalidCastException ex)\r\n\t\t\t{\r\n\t\t\t\tthrow new BadImageFormatException(\"Invalid calli metadata token\", ex);\r\n\t\t\t}\r\n\t\t\tvar (header, fpt) = module.DecodeMethodSignature(signatureHandle, genericContext);\r\n\t\t\tvar functionPointer = Pop(StackType.I);\r\n\t\t\tint firstArgument = header.IsInstance ? 1 : 0;\r\n\t\t\tvar arguments = new ILInstruction[firstArgument + fpt.ParameterTypes.Length];\r\n\t\t\tfor (int i = fpt.ParameterTypes.Length - 1; i >= 0; i--)\r\n\t\t\t{\r\n\t\t\t\targuments[firstArgument + i] = Pop(fpt.ParameterTypes[i].GetStackType());\r\n\t\t\t}\r\n\t\t\tif (firstArgument == 1)\r\n\t\t\t{\r\n\t\t\t\targuments[0] = Pop();\r\n\t\t\t}\r\n\t\t\t// arguments is in reverse order of the Pop calls, thus\r\n\t\t\t// arguments is now in the correct evaluation order.\r\n\t\t\tvar call = new CallIndirect(\r\n\t\t\t\theader.IsInstance,\r\n\t\t\t\theader.HasExplicitThis,\r","sourceCodeStart":1827,"sourceCodeEnd":1863,"githubUrl":"https://github.com/icsharpcode/ILSpy/blob/60c08fcb74fcc183130f73c861ed35cf944d0bf1/ICSharpCode.Decompiler/IL/ILReader.cs#L1827-L1863","documentation":"BadImageFormatException (wrapping InvalidCastException) thrown by DecodeCallIndirect when the calli instruction's operand token cannot be cast to StandaloneSignatureHandle. A calli instruction must reference a function-pointer standalone signature; any other table type for its operand is invalid IL.","triggerScenarios":"A calli instruction whose operand token is not a StandaloneSignature (e.g. points at a MethodDef, MethodSpec, or TypeSpec row); corrupt or hand-edited IL.","commonSituations":"Obfuscated assemblies that rewrite calli operands; damaged metadata streams; IL generated by tools that emit the wrong table for function-pointer signatures.","solutions":["Catch BadImageFormatException during decompilation and mark the method unreadable.","Cross-check the token's table against StandaloneSignature before decompiling if you preprocess metadata.","Use a clean copy of the assembly if tampering is suspected."],"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(\"calli\"))\n{\n    logger.Warn($\"Invalid calli token in {typeName}: {ex.Message}\");\n}","handlingStrategy":"try-catch","validationCode":"void VerifyCalliTokens(string path)\n{\n    using var pe = new PEReader(File.OpenRead(path));\n    var md = pe.GetMetadataReader();\n    // calli operands must be StandaloneSignature (table 0x11); flag any whose table byte differs\n}","typeGuard":null,"tryCatchPattern":"try { var code = decompiler.DecompileTypeAsString(typeName); }\ncatch (BadImageFormatException ex) when (ex.Message.Contains(\"calli\")) { logger.Warn($\"Invalid calli signature token: {ex.Message}\"); }","preventionTips":["Catch BadImageFormatException during decompilation and degrade gracefully.","When preprocessing, confirm calli operands resolve to StandaloneSignature rows.","Suspect obfuscation when calli tokens point at unexpected tables."],"tags":["il-reader","metadata-token","calli","bad-image"],"backgroundTag":null,"analyzedSha":"60c08fcb74fcc183130f73c861ed35cf944d0bf1","analyzedAt":"2026-08-13T11:34:51.223Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}