crashDumpAnnotations.test.ts 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Buffer } from "node:buffer";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. extractCrashDumpAnnotations,
  5. parseV8SizeAnnotation,
  6. summarizeCrashDumpAnnotations,
  7. } from "../src/main/crashDumpAnnotations.js";
  8. import { buildCrashDumpFixture, CODE_SPACE_OOM_ANNOTATIONS } from "./crashDumpFixture.js";
  9. describe("crashDumpAnnotations", () => {
  10. it("按 crashpad 长度前缀布局提取已知注解,忽略长度对不上的伪键", () => {
  11. const decoy = Buffer.from("rapid v8-oom-location=fake ptype junk", "latin1");
  12. const dump = buildCrashDumpFixture(CODE_SPACE_OOM_ANNOTATIONS, {
  13. leadingBytes: Buffer.concat([Buffer.from("MDMP ", "latin1"), decoy]),
  14. trailingBytes: Buffer.from(" pid ", "latin1"),
  15. });
  16. const annotations = extractCrashDumpAnnotations(dump);
  17. expect(annotations).toEqual(CODE_SPACE_OOM_ANNOTATIONS);
  18. expect(annotations["v8-oom-stack"]).toContain("catalogTree-BYrDsScn.js");
  19. });
  20. it("同一个键只取第一次出现的值,并容忍非 4 字节对齐的文件头", () => {
  21. const first = buildCrashDumpFixture(
  22. { "v8-oom-location": "CALL_AND_RETRY_LAST" },
  23. { leadingBytes: Buffer.from("xyz", "latin1") },
  24. );
  25. const second = buildCrashDumpFixture({ "v8-oom-location": "OTHER" });
  26. expect(extractCrashDumpAnnotations(Buffer.concat([first, second]))).toEqual({
  27. "v8-oom-location": "CALL_AND_RETRY_LAST",
  28. });
  29. });
  30. it("无注解或损坏输入返回空对象而不是抛错", () => {
  31. expect(extractCrashDumpAnnotations(Buffer.alloc(0))).toEqual({});
  32. expect(extractCrashDumpAnnotations(Buffer.from("v8-oom-", "latin1"))).toEqual({});
  33. const truncated = buildCrashDumpFixture({ "v8-oom-location": "CALL_AND_RETRY_LAST" });
  34. expect(extractCrashDumpAnnotations(truncated.subarray(0, truncated.length - 6))).toEqual({});
  35. });
  36. it("解析 V8 大小文本并按 1024 进位", () => {
  37. expect(parseV8SizeAnnotation("0B")).toBe(0);
  38. expect(parseV8SizeAnnotation("1023.94KB")).toBe(Math.round(1023.94 * 1024));
  39. expect(parseV8SizeAnnotation("256.00MB")).toBe(256 * 1024 * 1024);
  40. expect(parseV8SizeAnnotation("4096.00MB")).toBe(4096 * 1024 * 1024);
  41. expect(parseV8SizeAnnotation("5727%")).toBeNull();
  42. expect(parseV8SizeAnnotation(undefined)).toBeNull();
  43. });
  44. it("code cage 用尽时判定为 code_space_exhausted,并带上栈头与最后一条 GC 日志", () => {
  45. const summary = summarizeCrashDumpAnnotations(CODE_SPACE_OOM_ANNOTATIONS);
  46. expect(summary).toMatchObject({
  47. processType: "renderer",
  48. location: "CALL_AND_RETRY_LAST",
  49. oomKind: "code_space_exhausted",
  50. isMainIsolate: true,
  51. isolateCount: 5,
  52. codeCageSizeBytes: 256 * 1024 * 1024,
  53. codeCageFreeBytes: 0,
  54. codeCageLastAllocStatus: "ran out of reservation",
  55. codeSpaceBytes: Math.round(149.89 * 1024 * 1024),
  56. codeLargeObjectSpaceBytes: Math.round(40.34 * 1024 * 1024),
  57. oldSpaceBytes: Math.round(284.93 * 1024 * 1024),
  58. mainCageLastAllocStatus: "success",
  59. });
  60. expect(summary?.stackHead).toEqual([
  61. "next",
  62. "WI in file:///Applications/ZCode.app/Contents/Resources/app.asar/out/renderer/assets/catalogTree-BYrDsScn.js",
  63. "kq in file:///Applications/ZCode.app/Contents/Resources/app.asar/out/renderer/assets/styles-DyAcaLKy.js",
  64. "Aq in =",
  65. ]);
  66. expect(summary?.lastGcMessage).toContain("189710714 ms: Mark-Compact (reduce)");
  67. });
  68. it("old-space 撞上限的普通堆 OOM 判定为 js_heap_exhausted", () => {
  69. const summary = summarizeCrashDumpAnnotations({
  70. "v8-oom-location": "CALL_AND_RETRY_LAST",
  71. "v8-oom-code-cage-size": "256.00MB",
  72. "v8-oom-code-cage-free-size": "200.00MB",
  73. "v8-oom-code-cage-last-alloc-status": "success",
  74. "v8-oom-main-cage-last-alloc-status": "success",
  75. "v8-oom-old-space-size": "3900.00MB",
  76. });
  77. expect(summary?.oomKind).toBe("js_heap_exhausted");
  78. });
  79. it("信息不足时 oomKind 为 unknown;没有 v8-oom-location 则不是 V8 OOM", () => {
  80. expect(summarizeCrashDumpAnnotations({ "v8-oom-location": "NewArray" })?.oomKind).toBe(
  81. "unknown",
  82. );
  83. expect(summarizeCrashDumpAnnotations({ process_type: "gpu-process" })).toBeNull();
  84. });
  85. });