clang 23.0.0git
UnsafeBufferUsage.cpp
Go to the documentation of this file.
1//===- UnsafeBufferUsage.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "SSAFAnalysesCommon.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/JSON.h"
17#include <cstdint>
18
19using namespace clang;
20using namespace ssaf;
21using Array = llvm::json::Array;
22using Object = llvm::json::Object;
23
24static constexpr llvm::StringLiteral SummarySerializationKey = "UnsafeBuffers";
25
27ssaf::buildUnsafeBufferUsageEntitySummary(EntityPointerLevelSet UnsafeBuffers) {
28 return UnsafeBufferUsageEntitySummary(std::move(UnsafeBuffers));
29}
30
31llvm::iterator_range<EntityPointerLevelSet::const_iterator>
32ssaf::getUnsafeBuffers(const UnsafeBufferUsageEntitySummary &S) {
33 return llvm::make_range(S.UnsafeBuffers.begin(), S.UnsafeBuffers.end());
34}
35
38 const auto &SS = static_cast<const UnsafeBufferUsageEntitySummary &>(S);
39 Array UnsafeBuffersData;
40
41 for (const auto &EPL : getUnsafeBuffers(SS))
42 UnsafeBuffersData.push_back(entityPointerLevelToJSON(EPL, Fn));
43 return Object{{SummarySerializationKey.data(), std::move(UnsafeBuffersData)}};
44}
45
48 const Array *UnsafeBuffersData =
49 Data.getArray(SummarySerializationKey.data());
50
51 if (!UnsafeBuffersData)
52 return makeSawButExpectedError(Object(Data), "an Object with a key %s",
54
55 EntityPointerLevelSet EPLs;
56
57 for (const auto &EltData : *UnsafeBuffersData) {
59 entityPointerLevelFromJSON(EltData, Fn);
60
61 if (!EPL)
62 return EPL.takeError();
63 EPLs.insert(*EPL);
64 }
65 return std::make_unique<UnsafeBufferUsageEntitySummary>(
66 buildUnsafeBufferUsageEntitySummary(std::move(EPLs)));
67}
68
74
80
81static llvm::Registry<JSONFormat::FormatInfo>::Add<
85 "JSON Format info for UnsafeBufferUsageEntitySummary");
86
87// NOLINTNEXTLINE(misc-use-internal-linkage)
89
90// For unit test:
94 std::function<uint64_t(EntityId)> IdToIntFn,
95 std::function<llvm::Expected<EntityId>(uint64_t)> IdFromIntFn) {
96
97 auto IdToJson = [&IdToIntFn](EntityId Id) -> Object {
98 return Object({{"@", IdToIntFn(Id)}});
99 };
100 auto IdFromJson =
101 [&IdFromIntFn](const Object &O) -> llvm::Expected<EntityId> {
102 const auto *Int = O.get("@");
103
104 if (Int && Int->getAsUINT64())
105 return IdFromIntFn(*Int->getAsUINT64());
106 return llvm::createStringError("failed to get EntityId from Object");
107 };
108
109 return deserializeImpl(serialize(S, IdToJson), IdFromJson);
110}
volatile int UnsafeBufferUsageSSAFJSONFormatAnchorSource
static llvm::Expected< std::unique_ptr< EntitySummary > > deserialize(const Object &Data, EntityIdTable &, JSONFormat::EntityIdFromJSONFn Fn)
static llvm::Expected< std::unique_ptr< EntitySummary > > deserializeImpl(const Object &Data, JSONFormat::EntityIdFromJSONFn Fn)
static llvm::Registry< JSONFormat::FormatInfo >::Add< UnsafeBufferUsageJSONFormatInfo > RegisterUnsafeBufferUsageJSONFormatInfo(UnsafeBufferUsageEntitySummary::Name, "JSON Format info for UnsafeBufferUsageEntitySummary")
llvm::json::Object Object
static Object serialize(const EntitySummary &S, JSONFormat::EntityIdToJSONFn Fn)
static constexpr llvm::StringLiteral SummarySerializationKey
llvm::json::Array Array
Manages entity name interning and provides efficient EntityId handles.
Lightweight opaque handle representing an entity in an EntityIdTable.
Definition EntityId.h:31
Base class for analysis-specific summary data.
llvm::function_ref< llvm::Expected< EntityId >(const Object &)> EntityIdFromJSONFn
Definition JSONFormat.h:71
FormatInfoEntry< SerializerFn, DeserializerFn > FormatInfo
Definition JSONFormat.h:80
llvm::function_ref< Object(EntityId)> EntityIdToJSONFn
Definition JSONFormat.h:70
An UnsafeBufferUsageEntitySummary contains a set of EntityPointerLevels extracted from unsafe buffer ...
static constexpr llvm::StringLiteral Name
llvm::json::Value entityPointerLevelToJSON(const EntityPointerLevel &EPL, JSONFormat::EntityIdToJSONFn EntityId2JSON)
llvm::Expected< std::unique_ptr< EntitySummary > > serializeDeserializeRoundTrip(const UnsafeBufferUsageEntitySummary &S, std::function< uint64_t(EntityId)> IdToIntFn, std::function< llvm::Expected< EntityId >(uint64_t)> IdFromIntFn)
Expected< EntityPointerLevel > entityPointerLevelFromJSON(const llvm::json::Value &EPLData, JSONFormat::EntityIdFromJSONFn EntityIdFromJSON)
llvm::json::Object Object
llvm::Error makeSawButExpectedError(const llvm::json::Value &Saw, llvm::StringRef Expected, const Ts &...ExpectedArgs)
The JSON file list parser is used to communicate input to InstallAPI.
int const char * function
Definition c++config.h:31