[clang] [nfc][analyzer] Add MemSpace trait to program state (PR #123003)
Michael Flanders via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 16 13:03:50 PST 2025
================
@@ -0,0 +1,72 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines getters and setters for the memory space trait which
+// associates memory regions with memory spaces in the program state. It also
+// defines the MemSpacesMap which maps memory regions to memory spaces.
+//
+// These functions keep the following invariants over the MemSpacesMap:
+// 1. (Temporary as an intermediate step) Memory space traits are only
+// mapped for memory regions that have an unknown memory space
+// 2. Only base memory regions are mapped in the trait
+// 3. Memory regions which have no mapping in the trait are assumed to be
+// unknown; no memory region is allowed to be mapped to an unknown memory
+// space in the trait;
+//
+//===----------------------------------------------------------------------===//
+
+#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 {
+
+// Canonicalize to base, in case of subregions, we don't want base regions and
+// subregions to have different memory spaces
+[[nodiscard]] static const MemRegion *
+canonicalizeMemRegion(const MemRegion *MR) {
+ return MR->getBaseRegion();
+}
+
+ProgramStateRef setMemSpaceTrait(ProgramStateRef State, const MemRegion *MR,
+ const MemSpaceRegion *MS) {
+ MR = canonicalizeMemRegion(MR);
+
+ // For now, this should only be called to update the trait for memory regions
+ // that have an unknown memory spaces since we assume everywhere else that the
+ // memory space trait is set only for unknown memory spaces (setting this info
+ // otherwise would go unused).
+ assert(isa<UnknownSpaceRegion>(MR->getMemorySpace()));
+
+ // Shouldn't use the memory space trait to associate UnknownSpaceRegion with
+ // an already UnknownSpaceRegion
+ assert(!isa<UnknownSpaceRegion>(MS));
+
+ ProgramStateRef NewState = State->set<MemSpacesMap>(MR, MS);
+ return NewState;
+}
+
+const MemSpaceRegion *getMemSpace(ProgramStateRef State, const MemRegion *MR) {
+ MR = canonicalizeMemRegion(MR);
+
+ const MemSpaceRegion *const *Result = State->get<MemSpacesMap>(MR);
----------------
Flandini wrote:
I think the goal is to do this for every memregion, not just the ones with UnknownSpaceRegion, so we would eventually pay this price. I could do some benchmarking to see what the impact is like without the guard on UnknownSpaceRegion.
https://github.com/llvm/llvm-project/pull/123003
More information about the cfe-commits
mailing list