llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RFC][Format] Encapsulate chained pointer access for improved robustness

Open zhangweize9-cyber opened this issue 10 months ago • 1 comments

Description:

While auditing the current implementation of clang-format and the WASM linker driver (lld/wasm), I identified several areas where the code logic could be more robust and maintainable. Specifically:

  • Violation of the Law of Demeter: In clang/lib/Format/Format.cpp, there are numerous chained accesses such as Line->First->TokenText. This pattern is error-prone and risks null-pointer dereferences. I propose encapsulating these into semantic methods like Line->getHeadText() or Line->startsWith().
  • Inconsistent Error Reporting: There is a noticeable gap between internal logic (e.g., AffectedRangeMgr.computeAffectedLines) and user-facing error reporting. The current reliance on raw llvm::errs() and scattered make_string_error calls makes structured diagnostics difficult.
  • WASM Opcode Emission: In lld/wasm/Writer.cpp, the manual emission of opcodes (e.g., local.get) lacks a unified audit layer, which complicates debugging in restricted environments like Windows LTSC.
  • Proposed Action: Introduce a unified diagnostic or encapsulation layer to provide structured state snapshots. This will ensure that before any formatting or linking failure occurs, the internal state is traceable and robust.

I've been auditing the clang-format core logic and found several fragile chained pointer accesses that could lead to potential null-pointer dereferences (e.g., Line->First->TokenText). I plan to refactor these by encapsulating access logic into safer helper methods. Target functions and areas include:

  1. AnnotatedLine class: Add getFirstTokenText() and startsWith() helpers to handle null checks internally.
  2. AffectedRangeMgr.computeAffectedLines: Refactor range calculations to use these safe helpers.
  3. clang/lib/Format/Format.cpp: Replace repeated Line->First && Line->First->TokenText == ... patterns (specifically around line 3290 and 3909).

Here's my debug step:

grep -rn <keyword> <directory>
  1. Start with......
  • ./clang/utils/CIndex/completion_logger_server.py
  • ./clang/utils/analyzer/CmpRuns.py
  • ./clang/lib/Format/Format.cpp
  • .............
  1. Start search wasm grammar.
  • block, loop, br, call, global.get, global.set, local.get, local.set, i32.add
  1. Using grep to search local.get keyword.
./lld/wasm/Writer.cpp
./lld/test/wasm/
  1. Search for ./lld/wasm/ and ./lld/test/wasm/
  • ./lld/wasm/
$ grep -rn "local.get" ./lld/wasm/
./lld/wasm/Writer.cpp:1275:        writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
./lld/wasm/Writer.cpp:1590:      writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
./lld/wasm/Writer.cpp:1624:      writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
./lld/wasm/Writer.cpp:1631:      writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
......
  • ./lld/test/wasm/
$ grep -rn "local.get" ./lld/test/wasm/
./lld/test/wasm/gc-imports.s:11:  local.get 0
./lld/test/wasm/externref.s:18:  local.get 0
./lld/test/wasm/shared.s:75:  local.get 0
./lld/test/wasm/shared.s:79:  local.get 0
......
  1. ./lld/test/wasm/globals.s
.globl _start
.globl read_global
.globl write_global
  ......
  1. ./lld/wasm/Driver.cpp (important)
// Line 526~
static void readConfigs(opt :: InputArgList &args) {
    // ......
    // ctx.arg -> Packaging a function.
    ctx.arg.bsymbolic = args.hasArg(OPT_Bsymbolic);
    ctx.arg. checkFeatures =
args.hasFlag(OPT_check_features, OPT_no_check_features, true);
    ctx.arg. compressRelocations = args.hasArg(OPT_compress_relocations);
    ctx.arg.demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
    ctx.arg.disableVerify = args.hasArg(OPT_disable_verify);
    // ......
}
  1. ./clang/lib/Format/Format.cpp
// Line 2703
if (Err) {
    // ........
}

// Line 2981
AffectedRangeMgr.computeAffectedLines(AnnotatedLines);

// Line 3290
// Line->First->TokenText.starts_with("#") || Line->First->TokenText
if (Line->First->TokenText......) {}

// Line 4417
return make_string_error()

// Line 4455
LLVM_DEBUG(llvm::dbgs());

// Line 4297
Filename.ends_with_insensitive()

// Line 3909
llvm::errs()

zhangweize9-cyber avatar Jan 29 '26 13:01 zhangweize9-cyber

WIP: I'm currently prototyping the helper functions in my local environment and running regression tests. Will update with a draft PR once the atomic changes are verified.

zhangweize9-cyber avatar Jan 29 '26 13:01 zhangweize9-cyber