Loading...
Searching...
No Matches
RuntimeManifest.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "RuntimeManifest.h"
6
7#include <algorithm>
8#include <mutex>
9#include <sstream>
10#include <tuple>
11#include <utility>
12
13#include "AbiReport.h"
14#include "auxiliary/Sha256.h"
15
16namespace gpr::runtime {
17namespace {
18
19std::mutex registry_mutex;
20std::vector<Component> registry;
21
22std::string quote(const std::string& value)
23{
24 std::string out = "\"";
25 for (const char c: value) {
26 switch (c) {
27 case '\\':
28 out += "\\\\";
29 break;
30 case '"':
31 out += "\\\"";
32 break;
33 case '\n':
34 out += "\\n";
35 break;
36 case '\r':
37 out += "\\r";
38 break;
39 case '\t':
40 out += "\\t";
41 break;
42 default:
43 out += c;
44 }
45 }
46 return out + '"';
47}
48
49std::vector<Component> linkedComponents()
50{
51 std::vector<Component> out;
52 for (const auto& library: abi::libraries())
53 out.push_back({library.name,
54 library.library,
55 library.name,
56 "linked",
57 {},
58 {},
59 library.verified});
60 return out;
61}
62
63std::vector<Component> sortedSnapshot()
64{
65 auto out = linkedComponents();
66 {
67 const std::lock_guard<std::mutex> lock(registry_mutex);
68 for (const auto& component: registry) {
69 const auto match = std::find_if(
70 out.begin(), out.end(), [&](const Component& current) {
71 return current.role == component.role &&
72 current.name == component.name;
73 });
74 if (match == out.end())
75 out.push_back(component);
76 else
77 *match = component;
78 }
79 }
80 std::sort(
81 out.begin(), out.end(),
82 [](const Component& left, const Component& right) {
83 return std::tie(left.role, left.name, left.version, left.path) <
84 std::tie(right.role, right.name, right.version, right.path);
85 });
86 return out;
87}
88
89std::string componentJson(const Component& component)
90{
91 std::ostringstream out;
92 out << "{\"load\":" << quote(component.load)
93 << ",\"name\":" << quote(component.name)
94 << ",\"path\":" << quote(component.path)
95 << ",\"role\":" << quote(component.role)
96 << ",\"sha256\":" << quote(component.sha256)
97 << ",\"verified\":" << (component.verified ? "true" : "false")
98 << ",\"version\":" << quote(component.version) << "}";
99 return out.str();
100}
101
102std::string manifestJsonWithoutDigest(const std::vector<Component>& snapshot)
103{
104 std::ostringstream out;
105 out << "{\"components\":[";
106 for (std::size_t i = 0; i < snapshot.size(); ++i) {
107 if (i) out << ',';
108 out << componentJson(snapshot[i]);
109 }
110 out << "],\"format\":\"gpridge-runtime-components\",\"version\":1}";
111 return out.str();
112}
113} // namespace
114
116{
117 const std::lock_guard<std::mutex> lock(registry_mutex);
118 const auto existing = std::find_if(
119 registry.begin(), registry.end(), [&](const Component& current) {
120 return current.role == component.role &&
121 current.name == component.name;
122 });
123 if (existing == registry.end())
124 registry.push_back(std::move(component));
125 else
126 *existing = std::move(component);
127}
128
129std::vector<Component> components()
130{
131 return sortedSnapshot();
132}
133
134std::string manifestJson()
135{
136 const auto snapshot = sortedSnapshot();
137 const auto payload = manifestJsonWithoutDigest(snapshot);
138 const auto digest = sha256Hex(payload.data(), payload.size());
139 std::ostringstream out;
140 out << "{\"components\":[";
141 for (std::size_t i = 0; i < snapshot.size(); ++i) {
142 if (i) out << ',';
143 out << componentJson(snapshot[i]);
144 }
145 out << "],\"format\":\"gpridge-runtime-components\",\"sha256\":"
146 << quote(digest) << ",\"version\":1}";
147 return out.str();
148}
149
150std::string manifestSha256()
151{
152 const auto manifest = manifestJsonWithoutDigest(sortedSnapshot());
153 return sha256Hex(manifest.data(), manifest.size());
154}
155
156} // namespace gpr::runtime
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
std::vector< LibraryStatus > libraries()
Definition AbiReport.h:62
void registerComponent(Component component)
std::string manifestJson()
std::vector< Component > components()
A stable snapshot including the linked ABI dependencies.
std::string manifestSha256()
std::string sha256Hex(const void *data, std::size_t size)
Lowercase hexadecimal SHA-256 digest of a byte range (FIPS 180-4).
Definition Sha256.cpp:127
std::string load
linked, dlopen, or inprocess