apt 2.9.35 commandline package manager

solver3.h
1/*
2 * solver3.h - The APT 3.0 solver
3 *
4 * Copyright (c) 2023 Julian Andres Klode
5 * Copyright (c) 2023 Canonical Ltd
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <cassert>
11#include <memory>
12#include <optional>
13#include <queue>
14#include <vector>
15
16#include <apt-pkg/configuration.h>
17#include <apt-pkg/depcache.h>
18#include <apt-pkg/edsp.h>
19#include <apt-pkg/pkgcache.h>
20#include <apt-pkg/policy.h>
21
22 template <typename T> struct always_false : std::false_type {};
23
24 namespace APT
25{
26
33 template <typename K, typename V, bool fast = false>
34 class ContiguousCacheMap
35{
36 V *data_; // Avoid std::unique_ptr() as it may check that it's non-null.
37
38 public:
39 ContiguousCacheMap(pkgCache &cache)
40 {
41 static_assert(std::is_constructible_v<V>);
42 if constexpr (fast)
43 {
44 static_assert(std::is_trivially_constructible_v<V>);
45 static_assert(std::is_trivially_destructible_v<V>);
46 }
47
48 size_t size;
49 if constexpr (std::is_same_v<K, pkgCache::Version>)
50 size = cache.Head().VersionCount;
51 else if constexpr (std::is_same_v<K, pkgCache::Package>)
52 size = cache.Head().PackageCount;
53 else
54 static_assert(always_false<K>::value, "Cannot construct map for key type");
55
56 data_ = new V[size]{};
57 }
58 V &operator[](const K *key) { return data_[key->ID]; }
59 const V &operator[](const K *key) const { return data_[key->ID]; }
60 ~ContiguousCacheMap() { delete[] data_; }
61};
62
66 template <typename K, typename V>
67 using FastContiguousCacheMap = ContiguousCacheMap<K, V, true>;
68
69/*
70 * \brief APT 3.0 solver
71 *
72 * This is a simple solver focused on understandability and sensible results, it
73 * will not generally find all solutions to the problem but will try to find the best
74 * ones.
75 *
76 * It is a brute force solver with heuristics, conflicts learning, and 2**32 levels
77 * of backtracking.
78 */
79 class Solver
80{
81 enum class Decision : uint16_t;
82 enum class Hint : uint16_t;
83 struct Var;
84 struct CompareProviders3;
85 struct State;
86 struct Clause;
87 struct Work;
88 struct Solved;
89 friend struct std::hash<APT::Solver::Var>;
90
91 // \brief Groups of works, these are ordered.
92 //
93 // Later items will be skipped if they are optional, or we will when backtracking,
94 // try a different choice for them.
95 enum class Group : uint8_t
96 {
97 HoldOrDelete,
98
99 // Satisfying dependencies on entirely new packages first is a good idea because
100 // it may contain replacement packages like libfoo1t64 whereas we later will see
101 // Depends: libfoo1 where libfoo1t64 Provides libfoo1 and we'd have to choose.
102 SatisfyNew,
103 Satisfy,
104 // On a similar note as for SatisfyNew, if the dependency contains obsolete packages
105 // try it last.
106 SatisfyObsolete,
107
108 // Select a version of a package chosen for install.
109 SelectVersion,
110
111 // My intuition tells me that we should try to schedule upgrades first, then
112 // any non-obsolete installed packages, and only finally obsolete ones, such
113 // that newer packages guide resolution of dependencies for older ones, they
114 // may have more stringent dependencies, like a (>> 2) whereas an obsolete
115 // package may have a (>> 1), for example.
116 UpgradeManual,
117 InstallManual,
118 ObsoleteManual,
119
120 // Automatically installed packages must come last in the group, this allows
121 // us to see if they were installed as a dependency of a manually installed package,
122 // allowing a simple implementation of an autoremoval code.
123 UpgradeAuto,
124 KeepAuto,
125 ObsoleteAuto,
126
127 // Satisfy optional dependencies that were previously satisfied but won't otherwise be installed
128 SatisfySuggests,
129 };
130
131 // \brief Type to record depth at. This may very well be a 16-bit
132 // unsigned integer, then change Solver::State::Decision to be a
133 // uint16_t class enum as well to get a more compact space.
134 using depth_type = unsigned int;
135
136 // Documentation
137 template <typename T>
138 using heap = std::vector<T>;
139
140 static_assert(sizeof(depth_type) >= sizeof(map_id_t));
141
142 // Cache is needed to construct Iterators from Version objects we see
143 pkgCache &cache;
144 // Policy is needed for determining candidate version.
145 pkgDepCache::Policy &policy;
146 // Root state
147 std::unique_ptr<State> rootState;
148 // States for packages
149 ContiguousCacheMap<pkgCache::Package, State> pkgStates;
150 // States for versions
151 ContiguousCacheMap<pkgCache::Version, State> verStates;
152
153 // \brief Helper function for safe access to package state.
154 inline State &operator[](const pkgCache::Package *P)
155 {
156 return pkgStates[P];
157 }
158 inline const State &operator[](const pkgCache::Package *P) const
159 {
160 return pkgStates[P];
161 }
162
163 // \brief Helper function for safe access to version state.
164 inline State &operator[](const pkgCache::Version *V)
165 {
166 return verStates[V];
167 }
168 inline const State &operator[](const pkgCache::Version *V) const
169 {
170 return verStates[V];
171 }
172 // \brief Helper function for safe access to either state.
173 inline State &operator[](Var r);
174 inline const State &operator[](Var r) const;
175
176 mutable FastContiguousCacheMap<pkgCache::Package, char> pkgObsolete;
177 // \brief Check if package is obsolete.
178 // \param AllowManual controls whether manual packages can be obsolete
179 bool Obsolete(pkgCache::PkgIterator pkg, bool AllowManual=false) const;
180 bool ObsoletedByNewerSourceVersion(pkgCache::VerIterator cand) const;
181
182 mutable FastContiguousCacheMap<pkgCache::Version, short> priorities;
183 short GetPriority(pkgCache::VerIterator ver) const
184 {
185 if (priorities[ver] == 0)
186 priorities[ver] = policy.GetPriority(ver);
187 return priorities[ver];
188 }
189
190 mutable ContiguousCacheMap<pkgCache::Package, pkgCache::VerIterator> candidates;
191 pkgCache::VerIterator GetCandidateVer(pkgCache::PkgIterator pkg) const
192 {
193 if (candidates[pkg].end())
194 candidates[pkg] = policy.GetCandidateVer(pkg);
195 return candidates[pkg];
196 }
197
198 // \brief Heap of the remaining work.
199 //
200 // We are using an std::vector with std::make_heap(), std::push_heap(),
201 // and std::pop_heap() rather than a priority_queue because we need to
202 // be able to iterate over the queued work and see if a choice would
203 // invalidate any work.
204 heap<Work> work{};
205
206 // \brief Backlog of solved work.
207 //
208 // Solved work may become invalidated when backtracking, so store it
209 // here to revisit it later. This is similar to what MiniSAT calls the
210 // trail; one distinction is that we have both literals and our work
211 // queue to be concerned about
212 std::vector<Solved> solved{};
213
214 // \brief Propagation queue
215 std::queue<Var> propQ;
216 // \brief Discover variables
217 std::queue<Var> discoverQ;
218
219 // \brief Current decision level.
220 //
221 // This is an index into the solved vector.
222 std::vector<depth_type> choices{};
223
224 // \brief The time we called Solve()
225 time_t startTime;
226
227 EDSP::Request::Flags requestFlags;
229 std::string version{_config->Find("APT::Solver", "3.0")};
230 // \brief Debug level
231 int debug{_config->FindI("Debug::APT::Solver")};
232 // \brief If set, we try to keep automatically installed packages installed.
233 bool KeepAuto{version == "3.0" || not _config->FindB("APT::Get::AutomaticRemove")};
234 // \brief Determines if we are in upgrade mode.
235 bool IsUpgrade{_config->FindB("APT::Solver::Upgrade", requestFlags &EDSP::Request::UPGRADE_ALL)};
236 // \brief If set, removals are allowed.
237 bool AllowRemove{_config->FindB("APT::Solver::Remove", not(requestFlags & EDSP::Request::FORBID_REMOVE))};
238 // \brief If set, removal of manual packages is allowed.
239 bool AllowRemoveManual{AllowRemove && _config->FindB("APT::Solver::RemoveManual", false)};
240 // \brief If set, installs are allowed.
241 bool AllowInstall{_config->FindB("APT::Solver::Install", not(requestFlags & EDSP::Request::FORBID_NEW_INSTALL))};
242 // \brief If set, we use strict pinning.
243 bool StrictPinning{_config->FindB("APT::Solver::Strict-Pinning", true)};
244 // \brief If set, we install missing recommends and pick new best packages.
245 bool FixPolicyBroken{_config->FindB("APT::Get::Fix-Policy-Broken")};
246 // \brief If set, we use strict pinning.
247 bool DeferVersionSelection{_config->FindB("APT::Solver::Defer-Version-Selection", true)};
248 // \brief If set, we use strict pinning.
249 int Timeout{_config->FindI("APT::Solver::Timeout", 10)};
250
251 // \brief Keep recommends installed
252 bool KeepRecommends{_config->FindB("APT::AutoRemove::RecommendsImportant", true)};
253 // \brief Keep suggests installed
254 bool KeepSuggests{_config->FindB("APT::AutoRemove::SuggestsImportant", true)};
255
256 // \brief Discover a variable, translating the underlying dependencies to the SAT presentation
257 //
258 // This does a breadth-first search of the entire dependency tree of var,
259 // utilizing the discoverQ above.
260 void Discover(Var var);
261 // \brief Link a clause into the watchers
262 const Clause *RegisterClause(Clause &&clause);
263 // \brief Enqueue dependencies shared by all versions of the package.
264 void RegisterCommonDependencies(pkgCache::PkgIterator Pkg);
265
266 // \brief Translate an or group into a clause object
267 [[nodiscard]] Clause TranslateOrGroup(pkgCache::DepIterator start, pkgCache::DepIterator end, Var reason);
268 // \brief Propagate all pending propagations
269 [[nodiscard]] bool Propagate();
270
271 // \brief Return the current depth (choices.size() with casting)
272 depth_type depth()
273 {
274 return static_cast<depth_type>(choices.size());
275 }
276 inline Var bestReason(Clause const *clause, Var var) const;
277
278 public:
279 // \brief Create a new decision level.
280 void Push(Work work);
281 // \brief Revert to the previous decision level.
282 [[nodiscard]] bool Pop();
283 // \brief Undo a single assignment / solved work item
284 void UndoOne();
285 // \brief Add work to our work queue.
286 [[nodiscard]] bool AddWork(Work &&work);
287
288 // \brief Basic solver initializer. This cannot fail.
289 Solver(pkgCache &Cache, pkgDepCache::Policy &Policy, EDSP::Request::Flags requestFlags);
290
291 // Assume that the variable is decided as specified.
292 [[nodiscard]] bool Assume(Var var, bool decision, const Clause *reason = nullptr);
293 // Enqueue a decision fact
294 [[nodiscard]] bool Enqueue(Var var, bool decision, const Clause *reason = nullptr);
295
296 // \brief Apply the selections from the dep cache to the solver
297 [[nodiscard]] bool FromDepCache(pkgDepCache &depcache);
298 // \brief Apply the solver result to the depCache
299 [[nodiscard]] bool ToDepCache(pkgDepCache &depcache) const;
300
301 // \brief Solve the dependencies
302 [[nodiscard]] bool Solve();
303
304 // Print dependency chain
305 std::string WhyStr(Var reason) const;
306
318 std::string LongWhyStr(Var var, bool decision, const Clause *rclause, std::string prefix, std::unordered_set<Var> &seen) const;
319};
320
321}; // namespace APT
322
331 struct APT::Solver::Var
332{
333 uint32_t value;
334
335 explicit constexpr Var(uint32_t value = 0) : value{value} {}
336 explicit Var(pkgCache::PkgIterator const &Pkg) : value(uint32_t(Pkg.MapPointer()) << 1) {}
337 explicit Var(pkgCache::VerIterator const &Ver) : value(uint32_t(Ver.MapPointer()) << 1 | 1) {}
338
339 inline constexpr bool isVersion() const { return value & 1; }
340 inline constexpr uint32_t mapPtr() const { return value >> 1; }
341
342 // \brief Return the package, if any, otherwise 0.
343 map_pointer<pkgCache::Package> Pkg() const
344 {
345 return isVersion() ? 0 : map_pointer<pkgCache::Package>{mapPtr()};
346 }
347 // \brief Return the version, if any, otherwise 0.
348 map_pointer<pkgCache::Version> Ver() const
349 {
350 return isVersion() ? map_pointer<pkgCache::Version>{mapPtr()} : 0;
351 }
352 // \brief Return the package iterator if storing a package, or an empty one
353 pkgCache::PkgIterator Pkg(pkgCache &cache) const
354 {
355 return isVersion() ? pkgCache::PkgIterator() : pkgCache::PkgIterator(cache, cache.PkgP + Pkg());
356 }
357 // \brief Return the version iterator if storing a package, or an empty end.
358 pkgCache::VerIterator Ver(pkgCache &cache) const
359 {
360 return isVersion() ? pkgCache::VerIterator(cache, cache.VerP + Ver()) : pkgCache::VerIterator();
361 }
362 // \brief Return a package, cast from version if needed
363 pkgCache::PkgIterator CastPkg(pkgCache &cache) const
364 {
365 return isVersion() ? Ver(cache).ParentPkg() : Pkg(cache);
366 }
367 // \brief Check if there is no reason.
368 constexpr bool empty() const { return value == 0; }
369 constexpr bool operator!=(Var const other) const { return value != other.value; }
370 constexpr bool operator==(Var const other) const { return value == other.value; }
371
372 std::string toString(pkgCache &cache) const
373 {
374 if (auto P = Pkg(cache); not P.end())
375 return P.FullName();
376 if (auto V = Ver(cache); not V.end())
377 return V.ParentPkg().FullName() + "=" + V.VerStr();
378 return "(root)";
379 }
380};
381
388 struct APT::Solver::Clause
389{
390 // \brief Underyling dependency
391 pkgCache::Dependency *dep = nullptr;
392 // \brief Var for the work
393 Var reason;
394 // \brief The group we are in
395 Group group;
396 // \brief Possible solutions to this task, ordered in order of preference.
397 std::vector<Var> solutions{};
398 // \brief An optional clause does not need to be satisfied
399 bool optional;
400
401 // \brief A negative clause negates the solutions, that is X->A|B you get X->!(A|B), aka X->!A&!B
402 bool negative;
403
404 inline Clause(Var reason, Group group, bool optional = false, bool negative = false) : reason(reason), group(group), optional(optional), negative(negative) {}
405
406 std::string toString(pkgCache &cache, bool pretty = false) const;
407};
408
419 struct APT::Solver::Work
420{
421 const Clause *clause;
422
423 // \brief The depth at which the item has been added
424 depth_type depth;
425
426 // This is a union because we only need to store the choice we made when adding
427 // to the choice vector, and we don't need the size of valid choices in there.
428 union
429 {
430 // The choice we took
431 Var choice;
432 // Number of valid choices
433 size_t size{0};
434 };
435
436 // \brief This item should be removed from the queue.
437 bool erased{false};
438
439 bool operator<(APT::Solver::Work const &b) const;
440 std::string toString(pkgCache &cache) const;
441 inline Work(const Clause *clause, depth_type depth) : clause(clause), depth(depth) {}
442};
443
444// \brief This essentially describes the install state in RFC2119 terms.
445 enum class APT::Solver::Decision : uint16_t
446{
447 // \brief We have not made a choice about the package yet
448 NONE,
449 // \brief We need to install this package
450 MUST,
451 // \brief We cannot install this package (need conflicts with it)
452 MUSTNOT,
453};
454
461 struct APT::Solver::State
462{
463 // \brief The reason for causing this state (invalid for NONE).
464 //
465 // Rejects may have been caused by a later state. Consider we select
466 // between x1 and x2 in depth = N. If we now find dependencies of x1
467 // leading to a conflict with a package in K < N, we will record all
468 // of them as REJECT in depth = K.
469 //
470 // You can follow the reason chain upwards as long as the depth
471 // doesn't increase to unwind.
472 //
473 // Vars < 0 are package ID, reasons > 0 are version IDs.
474 const Clause *reason{};
475
476 // \brief The depth at which the decision has been taken
477 depth_type depth{0};
478
479 // \brief This essentially describes the install state in RFC2119 terms.
480 Decision decision{Decision::NONE};
481
482 // \brief Flags.
483 struct
484 {
485 bool discovered{};
486 bool manual{};
487 } flags;
488
489 static_assert(sizeof(flags) <= sizeof(int));
490
491 // \brief Clauses owned by this package/version
492 std::vector<std::unique_ptr<Clause>> clauses;
493 // \brief Reverse clauses, that is dependencies (or conflicts) from other packages on this one
494 std::vector<const Clause *> rclauses;
495};
496
503 struct APT::Solver::Solved
504{
505 // \brief A variable that has been assigned. We store this as a reason (FIXME: Rename Var to Var)
506 Var assigned;
507 // \brief A work item that has been solved. This needs to be put back on the queue.
508 std::optional<Work> work;
509};
510
511 inline APT::Solver::State &APT::Solver::operator[](Var r)
512{
513 if (auto P = r.Pkg())
514 return (*this)[cache.PkgP + P];
515 if (auto V = r.Ver())
516 return (*this)[cache.VerP + V];
517 return *rootState.get();
518}
519
520 inline const APT::Solver::State &APT::Solver::operator[](Var r) const
521{
522 return const_cast<Solver &>(*this)[r];
523}
524
525// Custom specialization of std::hash can be injected in namespace std.
526 template <>
527 struct std::hash<APT::Solver::Var>
528{
529 std::hash<decltype(APT::Solver::Var::value)> hash_value;
530 std::size_t operator()(const APT::Solver::Var &v) const noexcept { return hash_value(v.value); }
531};
APT::ContiguousCacheMap
A simple mapping from objects in the cache to user-defined types.
Definition solver3.h:35
APT::Solver
Definition solver3.h:80
APT::Solver::UndoOne
void UndoOne()
Definition solver3.cc:913
APT::Solver::Solver
Solver(pkgCache &Cache, pkgDepCache::Policy &Policy, EDSP::Request::Flags requestFlags)
Definition solver3.cc:199
APT::Solver::Pop
bool Pop()
Definition solver3.cc:944
APT::Solver::LongWhyStr
std::string LongWhyStr(Var var, bool decision, const Clause *rclause, std::string prefix, std::unordered_set< Var > &seen) const
Print a long reason string.
Definition solver3.cc:320
map_pointer
Definition pkgcache.h:98
pkgCache::DepIterator
Definition cacheiterators.h:327
pkgCache::PkgIterator
Definition cacheiterators.h:147
pkgCache::VerIterator
Definition cacheiterators.h:218
pkgCache
Definition pkgcache.h:126
pkgDepCache::Policy
Definition depcache.h:250
pkgDepCache
Definition depcache.h:63
edsp.h
EDSP::Request::Flags
Flags
Definition edsp.h:31
EDSP::Request::FORBID_REMOVE
@ FORBID_REMOVE
Definition edsp.h:35
EDSP::Request::FORBID_NEW_INSTALL
@ FORBID_NEW_INSTALL
Definition edsp.h:34
EDSP::Request::UPGRADE_ALL
@ UPGRADE_ALL
Definition edsp.h:33
pkgcache.h
pkgCache - Structure definitions for the cache file
APT::Solver::Clause
A single clause.
Definition solver3.h:389
APT::Solver::CompareProviders3
Definition solver3.cc:43
APT::Solver::Solved
A solved item.
Definition solver3.h:504
APT::Solver::State
The solver state.
Definition solver3.h:462
APT::Solver::Var
Tagged union holding either a package, version, or nothing; representing the reason for installing so...
Definition solver3.h:332
APT::Solver::Work
A single work item.
Definition solver3.h:420
always_false
Definition solver3.h:22
pkgCache::Dependency
Definition pkgcache.h:783
pkgCache::Package
contains information for a single unique package
Definition pkgcache.h:460
pkgCache::Version
information for a single version of a package
Definition pkgcache.h:652