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

Clang Static Analyzer reports null dereference on infeasible path with linear integer constraints

Open tianxinghe opened this issue 1 month ago • 9 comments

Clang Static Analyzer reports a null-pointer dereference on a path whose integer constraints are jointly infeasible. https://godbolt.org/z/rKPjh7fWb

Could you please help clarify why CSA considers this path feasible?

The reproducer below contains three nested conditions. Each condition is individually feasible, but their conjunction is unsatisfiable. Nevertheless, CSA reaches the innermost branch and reports core.NullDereference.

Reproducer

void test(int s, int c, int d)
{
    int **a = 0;

    if (s < -1000 || s > 1000 ||
        c < -1000 || c > 1000 ||
        d < -1000 || d > 1000)
        return;

    if (s - 2 * c - d <= -1) {
        if (s - c >= 0) {
            if (s + d <= 0) {
                **a;
            }
        }
    }
}

Command

$ clang --analyze test.c
test.c:15:18: warning: Dereference of null pointer (loaded from variable 'a') [core.NullDereference]

Expected result

No null-dereference warning should be emitted because the innermost branch is infeasible.
The three relevant conditions are:
s - 2*c - d <= -1
s - c       >=  0
s + d       <=  0
From the second condition,
s >= c
and from the third condition,
-d >= s.
Therefore,
s - 2*c - d
  >= s - 2*c + s
   = 2*(s-c)
  >= 0,
which contradicts:
s - 2*c - d <= -1.
Hence, there is no assignment to s, c, and d that can reach the
dereference.
The explicit [-1000, 1000] bounds also ensure that all arithmetic in the
conditions is far from signed integer overflow.

tianxinghe avatar Oct 08 '26 18:10 tianxinghe

@llvm/issue-subscribers-clang-static-analyzer

Author: Tianxing He (tianxinghe)

Clang Static Analyzer reports a null-pointer dereference on a path whose integer constraints are jointly infeasible. https://godbolt.org/z/rKPjh7fWb

Could you please help clarify why CSA considers this path feasible?

The reproducer below contains three nested conditions. Each condition is individually feasible, but their conjunction is unsatisfiable. Nevertheless, CSA reaches the innermost branch and reports core.NullDereference.

Reproducer

void test(int s, int c, int d)
{
    int **a = 0;

    if (s &lt; -1000 || s &gt; 1000 ||
        c &lt; -1000 || c &gt; 1000 ||
        d &lt; -1000 || d &gt; 1000)
        return;

    if (s - 2 * c - d &lt;= -1) {
        if (s - c &gt;= 0) {
            if (s + d &lt;= 0) {
                **a;
            }
        }
    }
}

Command
clang --analyze test.c

test.c:15:18: warning: Dereference of null pointer (loaded from variable 'a') [core.NullDereference]

Expected result
No null-dereference warning should be emitted because the innermost branch is infeasible.
The three relevant conditions are:
s - 2*c - d &lt;= -1
s - c       &gt;=  0
s + d       &lt;=  0
From the second condition,
s &gt;= c
and from the third condition,
-d &gt;= s.
Therefore,
s - 2*c - d
  &gt;= s - 2*c + s
   = 2*(s-c)
  &gt;= 0,
which contradicts:
s - 2*c - d &lt;= -1.
Hence, there is no assignment to s, c, and d that can reach the
dereference.
The explicit [-1000, 1000] bounds also ensure that all arithmetic in the
conditions is far from signed integer overflow.
</details>

I’d like to work on this issue. I’ll trace the CSA constraint propagation across the three nested conditions, determine why the constraint solver fails to recognize the resulting infeasible state, and investigate whether this is specific to linear integer constraint handling. I’ll then add a focused regression test and work on a minimal fix.

Architag1503 avatar Oct 08 '26 18:10 Architag1503

Could you please help clarify why CSA considers this path feasible?

Because its native constraint manager has limited capabilities and does not have algorithms that would let it conclude that some constraints that are jointly infeasible. (In fact, even simpler situations can exceed its capabilities...)

However, the analyzer has an option called crosscheck-with-Z3 which postprocesses the bug reports with the Z3 theorem prover and discards the reports where the constraints are jointly infeasible. I strongly suspect that e.g. the false positive shown in the ticket would have been discarded by this Z3 crosscheck logic.

The existence of this crosscheck logic lowers the priorities of improving our native constraint manager, because Z3 knows more than what we can squeeze into the analyzer itself. (In general, Integer programming is NP-hard, so if I understand correctly Z3 and similar libraries need to use complex heuristic algorithms.)

The crosscheck-with-Z3 option is disabled by default, and I don't exactly know why. IIRC it had some stability issues in the past, but those may be fixed now. @steakhal Is there anything that prevents enabling crosschecking by default (assuming that clang is built with Z3 support)?

Note that clang can be built without Z3 support (it is just an optional dependency), so there may be clang binaries that do not support crosscheck-with-Z3 – and if you build clang yourself, you need to jump through extra hoops to ensure Z3 support.

NagyDonat avatar Oct 08 '26 19:10 NagyDonat

5. > Could you please help clarify why CSA considers this path feasible?

Because its native constraint manager has limited capabilities and does not have algorithms that would let it conclude that some constraints that are jointly infeasible. (In fact, even simpler situations can exceed its capabilities...)
However, the analyzer has an option called [crosscheck-with-Z3](https://clang.llvm.org/docs/analyzer/user-docs/Options.html#crosscheck-with-z3) which postprocesses the bug reports with the [Z3 theorem prover](https://github.com/Z3Prover/z3) and discards the reports where the constraints are jointly infeasible. I strongly suspect that e.g. the false positive shown in the ticket would have been discarded by this Z3 crosscheck logic.
The existence of this crosscheck logic lowers the priorities of improving our native constraint manager, because Z3 knows more than what we can squeeze into the analyzer itself. (In general, [Integer programming](https://en.wikipedia.org/wiki/Integer_programming) is NP-hard, so if I understand correctly Z3 and similar libraries need to use complex heuristic algorithms.)
The `crosscheck-with-Z3` option is disabled by default, and I don't exactly know why. IIRC it had some stability issues in the past, but those may be fixed now. [@steakhal](https://github.com/steakhal) Is there anything that prevents enabling crosschecking by default (assuming that clang is built with Z3 support)?
Note that clang can be built without Z3 support (it is just an optional dependency), so there may be clang binaries that do not support `crosscheck-with-Z3` – and if you build clang yourself, you need to jump through extra hoops to ensure Z3 support.

Thanks, I understand. However, I seem to recall some CSA issues mentioning logic errors related to z3; perhaps I'll check a few of those z3-related issues tomorrow to see if the latest version still has the problem.

tianxinghe avatar Oct 08 '26 20:10 tianxinghe

some CSA issues mentioning logic errors related to z3

Note that there are two ways to use Z3 in the analyzer:

  • -analyzer-config crosscheck-with-z3=true is when the analysis uses the native constraint manager, and then the bug reports are postprocessed with Z3 to discard the infeasible ones – this is reasonably stable and can be used in production;
  • -analyzer-constraints=unsupported-z3 (previously known as -analyzer-constraints=z3) would use Z3 as the constraint manager during the analysis – but this is unsupported and and crashes very often.

I don't recall any recent crosscheck-with-z3 issues (since @steakhal worked a lot on eliminating some issues where the crosscheck was way too slow on a few outlier bug reports), but there were several somewhat recent issues and PRs about the "Z3 as constraint manager" mode, so your vague memories may come from those.

NagyDonat avatar Oct 08 '26 23:10 NagyDonat

some CSA issues mentioning logic errors related to z3

Note that there are two ways to use Z3 in the analyzer:

  • -analyzer-config crosscheck-with-z3=true is when the analysis uses the native constraint manager, and then the bug reports are postprocessed with Z3 to discard the infeasible ones – this is reasonably stable and can be used in production;
  • -analyzer-constraints=unsupported-z3 (previously known as -analyzer-constraints=z3) would use Z3 as the constraint manager during the analysis – but this is unsupported and and crashes very often.

I don't recall any recent crosscheck-with-z3 issues (since @steakhal worked a lot on eliminating some issues where the crosscheck was way too slow on a few outlier bug reports), but there were several somewhat recent issues and PRs about the "Z3 as constraint manager" mode, so your vague memories may come from those.

Enabling the Z3 cross-check yields the expected analysis results. #62215 #61511 #51282

tianxinghe avatar Oct 10 '26 16:10 tianxinghe

Enabling the Z3 cross-check yields the expected analysis results.

If Z3 crosscheck ensures correct behavior on the code reported here, then I think we can close this ticket.

The only remaining question is whether we want to enable crosscheck-with-z3 by default (when Z3 is available). @steakhal Do you know why crosscheck-with-z3 is disabled by default?


@tianxinghe Why did you list these tickets: https://github.com/llvm/llvm-project/issues/62215 https://github.com/llvm/llvm-project/issues/61511 https://github.com/llvm/llvm-project/issues/51282 ? What do you claim about them?

As I quickly glance over them:

  • https://github.com/llvm/llvm-project/issues/62215 is another case where the native constraint manager behaves badly, but IIUC Z3 crosscheck would produce correct behavior (according to a claim in the description of https://github.com/llvm/llvm-project/pull/71284).
  • https://github.com/llvm/llvm-project/issues/61511 is another case where the native constraint manager is inadequate. Note that a comment only claims that using Z3 as a constraint manager (the unstable unsupported mode) is also inadequate – but there was no experiment to see whether crosscheck-with-z3 would refute a concrete false positive report that was derived from this inadequate behavior. (Deducing something "freely" is sometimes more difficult than noticing that its negation is infeasible when combined with the preexisting constraints.)
  • https://github.com/llvm/llvm-project/issues/51282 is closed: that issue was resolved by a patch in 2021.

NagyDonat avatar Oct 11 '26 15:10 NagyDonat

Enabling the Z3 cross-check yields the expected analysis results.

If Z3 crosscheck ensures correct behavior on the code reported here, then I think we can close this ticket.

The only remaining question is whether we want to enable crosscheck-with-z3 by default (when Z3 is available). @steakhal Do you know why crosscheck-with-z3 is disabled by default?

@tianxinghe Why did you list these tickets: #62215 #61511 #51282 ? What do you claim about them?

As I quickly glance over them:

I was collecting historical CSA issues related to constraint solving. The reason I listed these tickets is that they are cases where crosscheck-with-z3 can provide the correct constraint evaluation. Sorry that my previous explanation was not clear enough, and thank you for taking the time to review these issues.

tianxinghe avatar Oct 11 '26 15:10 tianxinghe

@tianxinghe Thanks for the clarification!

The reason I listed these tickets is that they are cases where crosscheck-with-z3 can provide the correct constraint evaluation.

This was my guess but I though that it's better to ask.

NagyDonat avatar Oct 11 '26 18:10 NagyDonat