[clang] [analyzer] Add MemSpace trait to program state (PR #123003)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 15 06:00:39 PST 2025


================
@@ -0,0 +1,62 @@
+//===-- MemSpaces.cpp ---------------------------------------------*- C++ -*--//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// TODO
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/StaticAnalyzer/Core/PathSensitive/MemSpaces.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
+#include <cassert>
+
+REGISTER_MAP_WITH_PROGRAMSTATE(MemSpacesMap, const clang::ento::MemRegion *,
+                               const clang::ento::MemSpaceRegion *)
+
+namespace clang {
+namespace ento {
+namespace memspace {
+
+ProgramStateRef setMemSpaceTrait(ProgramStateRef State, const MemRegion *MR,
+                                 const MemSpaceRegion *MS) {
+  // for now, this should only be called to update the trait for mem regions
+  // that have an unknown mem spaces since we assume everywhere else that the
+  // memspace trait is set only for unknown mem spaces (setting this info
+  // otherwise would go unused).
+  assert(isa<UnknownSpaceRegion>(MR->getMemorySpace()));
+
+  // Shouldn't use the memspace trait to associate UnknownSpaceRegion with an
+  // already UnknownSpaceRegion
+  assert(!isa<UnknownSpaceRegion>(MS));
+
+  ProgramStateRef NewState = State->set<MemSpacesMap>(MR, MS);
+  return NewState;
+}
+
+bool hasMemSpaceTrait(ProgramStateRef State, const MemRegion *MR) {
+  if (!isa<UnknownSpaceRegion>(MR->getMemorySpace()))
+    return false;
+
+  const MemSpaceRegion *const *Result = State->get<MemSpacesMap>(MR);
+  return Result;
+}
+
+const MemSpaceRegion *getMemSpaceTrait(ProgramStateRef State,
+                                       const MemRegion *MR) {
+  if (!isa<UnknownSpaceRegion>(MR->getMemorySpace()))
+    return nullptr;
+
+  const MemSpaceRegion *const *Result = State->get<MemSpacesMap>(MR);
+  if (!Result)
+    return nullptr;
+  return *Result;
+}
----------------
NagyDonat wrote:

Instead this function, you should introduce a function that returns
- either `State->get<MemSpacesMap>(MR)` if it's non-null
- or `MR->getMemorySpace()` otherwise.
This function would reflect the fact that there is no underlying semantic difference between the two ways of storing the memory space (if a region _is on the heap_, we don't care if the region was initially created that way or we determined this later).

If you introduce this, you can simplify many complicated `if` conditions that currently use `II` to handle the two case.

https://github.com/llvm/llvm-project/pull/123003


More information about the cfe-commits mailing list