Skip to content

[Clang][SanitizerCoverage] Document trace-args and trace-ret - #227614

Draft
yskzalloc wants to merge 4 commits into
llvm:mainfrom
yskzalloc:sancov-trace-args-docs
Draft

yskzalloc wants to merge 4 commits into
llvm:mainfrom
yskzalloc:sancov-trace-args-docs

Conversation

@yskzalloc

@yskzalloc yskzalloc commented Sep 30, 2026 •

Copy link
Copy Markdown

This is PR 4 of a 4-PR stack:

  1. Depends on (llvm/): the instrumentation pass and its tests, [sancov] Add -fsanitize-coverage=trace-args,trace-ret #201410
  2. Depends on (clang/): -fsanitize-coverage=trace-args,trace-ret driver and frontend support, [Clang][SanitizerCoverage] Add -fsanitize-coverage=trace-args,trace-ret #218254
  3. Depends on (compiler-rt/lib/sanitizer_common/): weak default callbacks, [compiler-rt][SanitizerCoverage] Add weak trace-args/trace-ret callbacks #218265
  4. This PR (clang/docs/SanitizerCoverage.md): documentation
  • The design rule, stated up front: debug information may enrich the interpretation of an observation, but must never determine which instrumentation is emitted. A build with -g emits the same callbacks, operands and machine code as one without it. Neither flag needs another coverage mode.

  • The two callback signatures. A runtime identifies an observation by its own return address minus one, as with the trace-cmp callbacks, and no trace call is a tail call. num_fields says what val holds:

    • the value itself;
    • or the address of bytes to read: an object the IR marks as placed in memory (byval, byref, inalloca, preallocated, sret), or a pointee snapshot. The table lists pieces of at most 8 bytes, which covers rustc's opaque [N x i8] objects.

    A size of zero means there was nothing to report, and a value that may be poison is frozen first. The compiler never creates memory to report a value from.

  • "IR arguments, not source parameters": arg_idx is the IR argument position. The section gives x86-64 and AArch64 examples of where it differs from the source parameter number: a split struct, a first-class aggregate, sret, this. It also states which pointers Clang and rustc mark dereferenceable.

  • "Pointers: values, layouts and snapshots" separates four things about a pointer:

    • the pointer value;
    • the pointee's source-level type and member layout, which comes from debug information and never causes a read;
    • an explicit pointee snapshot;
    • complete, partial and unread members.

    Only an explicit IR dereferenceability contract makes the runtime read behind a pointer: dereferenceable(N), or dereferenceable_or_null(N) with a null check. The read is at most -sanitizer-coverage-trace-pointee-bytes (64) bytes. Nothing is inferred to stand in for a missing contract.

    • C++ references and this, C T p[static N], and Rust &T and &mut T carry the contract.
    • An ordinary C T * and a Rust raw pointer do not; they are pointer values only.

    The section also gives the measured share of pointer arguments with any pointee byte recorded on a Linux kernel build. That is not a share of arguments or structs reconstructed, and the text says so. Recording the values a function loads and stores through a pointer that has no contract is mentioned as possible future work, not as a feature.

  • "Consumers": compiler-rt's weak defaults and the Linux kernel's RFCed KCOV dataflow support.

  • "Supported scope": C, C++ and Rust on ELF x86-64 and AArch64, in userspace and in the Linux kernel, with or without -g, at any optimization level. Other frontends and targets get valid instrumentation.

@github-actions

Copy link
Copy Markdown

Hello @yskzalloc 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

Add two SanitizerCoverage modes that report the values flowing into and
out of instrumented functions. trace-args emits one call per IR argument
at function entry; trace-ret emits one call before every return:

  void __sanitizer_cov_trace_args(u64 tag, u64 val, const u64 *offsets);
  void __sanitizer_cov_trace_ret(u64 size, u64 val, const u64 *offsets);

`tag` is (arg_idx << 32) | size, where arg_idx is the position in the IR
parameter list. If `offsets` is null, `val` is the value itself,
zero-extended to 64 bits. Otherwise `val` is the address of `size`
bytes, and `offsets` points to a constant table: offsets[0] is the
number N of {byte offset, byte size} pairs that follow, each at most
8 bytes. This form reports byval/byref/inalloca/preallocated arguments
and sret buffers, described by the elements of their IR type, and
pointers that are noundef and dereferenceable(N) or
dereferenceable_or_null(N), snapshotted up to
-sanitizer-coverage-trace-pointee-bytes (default 64). A size of 0 means
nothing was reported.

The modes are enabled through SanitizerCoverageOptions::TraceArgs and
TraceRet or, for opt, -sanitizer-coverage-trace-args and
-sanitizer-coverage-trace-ret. Like the other modes, they need a
coverage level. They do not imply trace-pc-guard.

What is emitted depends only on the IR. Debug information gives the
calls a location and nothing else, so -g does not change the
instrumentation. The pass creates no allocas. The calls are notail and
nomerge, because a runtime identifies an observation by its return
address, as with trace-cmp.

Assisted-by: Kiro CLI (Claude Opus 5.5)
Signed-off-by: Yunseong Kim <[email protected]>
Accept trace-args and trace-ret in -fsanitize-coverage= and forward
them to SanitizerCoverageOptions::TraceArgs/TraceRet through the new
cc1 flags -fsanitize-coverage-trace-args and
-fsanitize-coverage-trace-ret.

Like the other instrumentation types, they satisfy the "requires an
instrumentation type" check and, without a coverage level, select the
edge level. That level alone adds no block callbacks, so
-fsanitize-coverage=trace-args emits argument tracing only.

hasSanitizeCoverage() now accounts for both flags, so either one on its
own runs the pass.

Assisted-by: Kiro CLI (Claude Opus 5.5)
Signed-off-by: Yunseong Kim <[email protected]>
Add weak, empty definitions of __sanitizer_cov_trace_args and
__sanitizer_cov_trace_ret to sanitizer_common, and declare them in
sanitizer_interface_internal.h with their contract. A program built with
-fsanitize-coverage=trace-args,trace-ret then links against a sanitizer
runtime without supplying the callbacks itself. A strong definition
overrides the defaults.

The new test overrides both callbacks and checks the reported argument
and return values at -O2 -g and at -O0.

Assisted-by: Kiro CLI (Claude Opus 5.5)
Signed-off-by: Yunseong Kim <[email protected]>
Add a "Tracing function arguments and return values" section to
SanitizerCoverage.md. It covers:

- the callback signatures, the tag encoding, and the offsets table;
- how arg_idx relates to source parameters;
- which pointers have their pointee snapshotted: noundef with
  dereferenceable(N) or dereferenceable_or_null(N);
- the supported scope.

Assisted-by: Kiro CLI (Claude Opus 5.5)
Signed-off-by: Yunseong Kim <[email protected]>
@yskzalloc
yskzalloc force-pushed the sancov-trace-args-docs branch from 53e2fb1 to e6a4c78 Compare November 17, 2026 12:19

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant