2014-02-12 19:28:09 +08:00
|
|
|
//===-- sanitizer_bvgraph.h -------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-02-12 19:28:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of Sanitizer runtime.
|
|
|
|
// BVGraph -- a directed graph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SANITIZER_BVGRAPH_H
|
|
|
|
#define SANITIZER_BVGRAPH_H
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_bitvector.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
// Directed graph of fixed size implemented as an array of bit vectors.
|
2014-02-14 20:08:23 +08:00
|
|
|
// Not thread-safe, all accesses should be protected by an external lock.
|
2014-02-12 19:28:09 +08:00
|
|
|
template<class BV>
|
|
|
|
class BVGraph {
|
|
|
|
public:
|
2017-12-07 07:02:02 +08:00
|
|
|
enum SizeEnum : uptr { kSize = BV::kSize };
|
2014-02-12 19:28:09 +08:00
|
|
|
uptr size() const { return kSize; }
|
|
|
|
// No CTOR.
|
|
|
|
void clear() {
|
|
|
|
for (uptr i = 0; i < size(); i++)
|
|
|
|
v[i].clear();
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:21:52 +08:00
|
|
|
bool empty() const {
|
|
|
|
for (uptr i = 0; i < size(); i++)
|
|
|
|
if (!v[i].empty())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:28:09 +08:00
|
|
|
// Returns true if a new edge was added.
|
|
|
|
bool addEdge(uptr from, uptr to) {
|
2014-02-13 20:39:21 +08:00
|
|
|
check(from, to);
|
2014-02-12 19:28:09 +08:00
|
|
|
return v[from].setBit(to);
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:47:48 +08:00
|
|
|
// Returns true if at least one new edge was added.
|
2014-03-14 15:09:01 +08:00
|
|
|
uptr addEdges(const BV &from, uptr to, uptr added_edges[],
|
|
|
|
uptr max_added_edges) {
|
|
|
|
uptr res = 0;
|
2014-02-17 16:47:48 +08:00
|
|
|
t1.copyFrom(from);
|
2014-03-14 15:09:01 +08:00
|
|
|
while (!t1.empty()) {
|
|
|
|
uptr node = t1.getAndClearFirstOne();
|
|
|
|
if (v[node].setBit(to))
|
|
|
|
if (res < max_added_edges)
|
|
|
|
added_edges[res++] = node;
|
|
|
|
}
|
2014-02-17 16:47:48 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:06:15 +08:00
|
|
|
// *EXPERIMENTAL*
|
2014-03-31 15:23:50 +08:00
|
|
|
// Returns true if an edge from=>to exist.
|
2014-03-14 16:06:15 +08:00
|
|
|
// This function does not use any global state except for 'this' itself,
|
|
|
|
// and thus can be called from different threads w/o locking.
|
|
|
|
// This would be racy.
|
|
|
|
// FIXME: investigate how much we can prove about this race being "benign".
|
2014-03-31 15:23:50 +08:00
|
|
|
bool hasEdge(uptr from, uptr to) { return v[from].getBit(to); }
|
2014-03-14 16:06:15 +08:00
|
|
|
|
2014-02-17 19:21:52 +08:00
|
|
|
// Returns true if the edge from=>to was removed.
|
|
|
|
bool removeEdge(uptr from, uptr to) {
|
|
|
|
return v[from].clearBit(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if at least one edge *=>to was removed.
|
|
|
|
bool removeEdgesTo(const BV &to) {
|
|
|
|
bool res = 0;
|
|
|
|
for (uptr from = 0; from < size(); from++) {
|
|
|
|
if (v[from].setDifference(to))
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if at least one edge from=>* was removed.
|
|
|
|
bool removeEdgesFrom(const BV &from) {
|
|
|
|
bool res = false;
|
|
|
|
t1.copyFrom(from);
|
|
|
|
while (!t1.empty()) {
|
|
|
|
uptr idx = t1.getAndClearFirstOne();
|
|
|
|
if (!v[idx].empty()) {
|
|
|
|
v[idx].clear();
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeEdgesFrom(uptr from) {
|
|
|
|
return v[from].clear();
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:28:09 +08:00
|
|
|
bool hasEdge(uptr from, uptr to) const {
|
2014-02-13 20:39:21 +08:00
|
|
|
check(from, to);
|
2014-02-12 19:28:09 +08:00
|
|
|
return v[from].getBit(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if there is a path from the node 'from'
|
2014-02-13 15:44:51 +08:00
|
|
|
// to any of the nodes in 'targets'.
|
|
|
|
bool isReachable(uptr from, const BV &targets) {
|
2014-02-14 20:08:23 +08:00
|
|
|
BV &to_visit = t1,
|
|
|
|
&visited = t2;
|
2014-02-13 17:52:15 +08:00
|
|
|
to_visit.copyFrom(v[from]);
|
2014-02-12 19:28:09 +08:00
|
|
|
visited.clear();
|
|
|
|
visited.setBit(from);
|
|
|
|
while (!to_visit.empty()) {
|
|
|
|
uptr idx = to_visit.getAndClearFirstOne();
|
|
|
|
if (visited.setBit(idx))
|
|
|
|
to_visit.setUnion(v[idx]);
|
|
|
|
}
|
2014-02-13 15:44:51 +08:00
|
|
|
return targets.intersectsWith(visited);
|
2014-02-12 19:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-02-13 17:52:15 +08:00
|
|
|
// Finds a path from 'from' to one of the nodes in 'target',
|
|
|
|
// stores up to 'path_size' items of the path into 'path',
|
|
|
|
// returns the path length, or 0 if there is no path of size 'path_size'.
|
|
|
|
uptr findPath(uptr from, const BV &targets, uptr *path, uptr path_size) {
|
|
|
|
if (path_size == 0)
|
|
|
|
return 0;
|
|
|
|
path[0] = from;
|
|
|
|
if (targets.getBit(from))
|
|
|
|
return 1;
|
2014-02-14 20:08:23 +08:00
|
|
|
// The function is recursive, so we don't want to create BV on stack.
|
|
|
|
// Instead of a getAndClearFirstOne loop we use the slower iterator.
|
|
|
|
for (typename BV::Iterator it(v[from]); it.hasNext(); ) {
|
|
|
|
uptr idx = it.next();
|
2014-02-13 17:52:15 +08:00
|
|
|
if (uptr res = findPath(idx, targets, path + 1, path_size - 1))
|
|
|
|
return res + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-18 22:56:19 +08:00
|
|
|
// Same as findPath, but finds a shortest path.
|
|
|
|
uptr findShortestPath(uptr from, const BV &targets, uptr *path,
|
|
|
|
uptr path_size) {
|
|
|
|
for (uptr p = 1; p <= path_size; p++)
|
|
|
|
if (findPath(from, targets, path, p) == p)
|
|
|
|
return p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:28:09 +08:00
|
|
|
private:
|
2014-02-13 23:45:20 +08:00
|
|
|
void check(uptr idx1, uptr idx2) const {
|
|
|
|
CHECK_LT(idx1, size());
|
|
|
|
CHECK_LT(idx2, size());
|
|
|
|
}
|
2014-02-12 19:28:09 +08:00
|
|
|
BV v[kSize];
|
2014-02-14 20:08:23 +08:00
|
|
|
// Keep temporary vectors here since we can not create large objects on stack.
|
|
|
|
BV t1, t2;
|
2014-02-12 19:28:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_BVGRAPH_H
|