mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-08-28 12:58:07 +00:00
Seperate Partition definition for States. This is a small step to cleaning
up the code
This commit is contained in:
parent
1179c1a42c
commit
e984b6ff74
@ -1272,6 +1272,7 @@ public:
|
|||||||
* have seen already from new ones when constructing the DFA.
|
* have seen already from new ones when constructing the DFA.
|
||||||
*/
|
*/
|
||||||
typedef set<State *, deref_less_than<State *> > States;
|
typedef set<State *, deref_less_than<State *> > States;
|
||||||
|
typedef set<State *> Partition;
|
||||||
/* Transitions in the DFA. */
|
/* Transitions in the DFA. */
|
||||||
typedef map<State *, Cases> Trans;
|
typedef map<State *, Cases> Trans;
|
||||||
|
|
||||||
@ -1280,7 +1281,7 @@ public:
|
|||||||
DFA(Node *root, dfaflags_t flags);
|
DFA(Node *root, dfaflags_t flags);
|
||||||
virtual ~DFA();
|
virtual ~DFA();
|
||||||
void remove_unreachable(dfaflags_t flags);
|
void remove_unreachable(dfaflags_t flags);
|
||||||
bool same_mappings(map <State *, States *> &partition_map, State *s1,
|
bool same_mappings(map <State *, Partition *> &partition_map, State *s1,
|
||||||
State *s2);
|
State *s2);
|
||||||
size_t hash_trans(State *s);
|
size_t hash_trans(State *s);
|
||||||
void minimize(dfaflags_t flags);
|
void minimize(dfaflags_t flags);
|
||||||
@ -1493,7 +1494,7 @@ void DFA::remove_unreachable(dfaflags_t flags)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* test if two states have the same transitions under partition_map */
|
/* test if two states have the same transitions under partition_map */
|
||||||
bool DFA::same_mappings(map <State *, States *> &partition_map, State *s1,
|
bool DFA::same_mappings(map <State *, Partition *> &partition_map, State *s1,
|
||||||
State *s2)
|
State *s2)
|
||||||
{
|
{
|
||||||
Trans::iterator i1 = trans.find(s1);
|
Trans::iterator i1 = trans.find(s1);
|
||||||
@ -1511,8 +1512,8 @@ bool DFA::same_mappings(map <State *, States *> &partition_map, State *s1,
|
|||||||
if (i1->second.otherwise) {
|
if (i1->second.otherwise) {
|
||||||
if (!i2->second.otherwise)
|
if (!i2->second.otherwise)
|
||||||
return false;
|
return false;
|
||||||
States *p1 = partition_map.find(i1->second.otherwise)->second;
|
Partition *p1 = partition_map.find(i1->second.otherwise)->second;
|
||||||
States *p2 = partition_map.find(i2->second.otherwise)->second;
|
Partition *p2 = partition_map.find(i2->second.otherwise)->second;
|
||||||
if (p1 != p2)
|
if (p1 != p2)
|
||||||
return false;
|
return false;
|
||||||
} else if (i2->second.otherwise) {
|
} else if (i2->second.otherwise) {
|
||||||
@ -1526,8 +1527,8 @@ bool DFA::same_mappings(map <State *, States *> &partition_map, State *s1,
|
|||||||
Cases::iterator j2 = i2->second.cases.find(j1->first);
|
Cases::iterator j2 = i2->second.cases.find(j1->first);
|
||||||
if (j2 == i2->second.end())
|
if (j2 == i2->second.end())
|
||||||
return false;
|
return false;
|
||||||
States *p1 = partition_map.find(j1->second)->second;
|
Partition *p1 = partition_map.find(j1->second)->second;
|
||||||
States *p2 = partition_map.find(j2->second)->second;
|
Partition *p2 = partition_map.find(j2->second)->second;
|
||||||
if (p1 != p2)
|
if (p1 != p2)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -1569,9 +1570,9 @@ size_t DFA::hash_trans(State *s)
|
|||||||
/* minimize the number of dfa states */
|
/* minimize the number of dfa states */
|
||||||
void DFA::minimize(dfaflags_t flags)
|
void DFA::minimize(dfaflags_t flags)
|
||||||
{
|
{
|
||||||
map <pair <uint64_t, size_t>, States *> perm_map;
|
map <pair <uint64_t, size_t>, Partition *> perm_map;
|
||||||
list <States *> partitions;
|
list <Partition *> partitions;
|
||||||
map <State *, States *> partition_map;
|
map <State *, Partition *> partition_map;
|
||||||
|
|
||||||
/* Set up the initial partitions - 1 non accepting, and a
|
/* Set up the initial partitions - 1 non accepting, and a
|
||||||
* partion for each unique combination of permissions
|
* partion for each unique combination of permissions
|
||||||
@ -1590,9 +1591,9 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
if (!(flags & DFA_CONTROL_NO_HASH_PART))
|
if (!(flags & DFA_CONTROL_NO_HASH_PART))
|
||||||
size = hash_trans(*i);
|
size = hash_trans(*i);
|
||||||
pair <uint64_t, size_t> group = make_pair(combined, size);
|
pair <uint64_t, size_t> group = make_pair(combined, size);
|
||||||
map <pair <uint64_t, size_t>, States *>::iterator p = perm_map.find(group);
|
map <pair <uint64_t, size_t>, Partition *>::iterator p = perm_map.find(group);
|
||||||
if (p == perm_map.end()) {
|
if (p == perm_map.end()) {
|
||||||
States *part = new States();
|
Partition *part = new Partition();
|
||||||
part->insert(*i);
|
part->insert(*i);
|
||||||
perm_map.insert(make_pair(group, part));
|
perm_map.insert(make_pair(group, part));
|
||||||
partitions.push_back(part);
|
partitions.push_back(part);
|
||||||
@ -1617,32 +1618,32 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
* splitting stables. With a worse case of 1 state per partition
|
* splitting stables. With a worse case of 1 state per partition
|
||||||
* ie. already minimized.
|
* ie. already minimized.
|
||||||
*/
|
*/
|
||||||
States *new_part;
|
Partition *new_part;
|
||||||
int new_part_count;
|
int new_part_count;
|
||||||
do {
|
do {
|
||||||
new_part_count = 0;
|
new_part_count = 0;
|
||||||
for (list <States *>::iterator p = partitions.begin();
|
for (list <Partition *>::iterator p = partitions.begin();
|
||||||
p != partitions.end(); p++) {
|
p != partitions.end(); p++) {
|
||||||
new_part = NULL;
|
new_part = NULL;
|
||||||
State *rep = *((*p)->begin());
|
State *rep = *((*p)->begin());
|
||||||
States::iterator next;
|
Partition::iterator next;
|
||||||
for (States::iterator s = ++(*p)->begin();
|
for (Partition::iterator s = ++(*p)->begin();
|
||||||
s != (*p)->end(); s++) {
|
s != (*p)->end(); s++) {
|
||||||
if (same_mappings(partition_map, rep, *s))
|
if (same_mappings(partition_map, rep, *s))
|
||||||
continue;
|
continue;
|
||||||
if (!new_part) {
|
if (!new_part) {
|
||||||
new_part = new States;
|
new_part = new Partition;
|
||||||
}
|
}
|
||||||
new_part->insert(*s);
|
new_part->insert(*s);
|
||||||
}
|
}
|
||||||
if (new_part) {
|
if (new_part) {
|
||||||
for (States::iterator m = new_part->begin();
|
for (Partition::iterator m = new_part->begin();
|
||||||
m != new_part->end(); m++) {
|
m != new_part->end(); m++) {
|
||||||
(*p)->erase(*m);
|
(*p)->erase(*m);
|
||||||
partition_map.erase(*m);
|
partition_map.erase(*m);
|
||||||
partition_map.insert(make_pair(*m, new_part));
|
partition_map.insert(make_pair(*m, new_part));
|
||||||
}
|
}
|
||||||
list <States *>::iterator tmp = p;
|
list <Partition *>::iterator tmp = p;
|
||||||
partitions.insert(++tmp, new_part);
|
partitions.insert(++tmp, new_part);
|
||||||
new_part_count++;
|
new_part_count++;
|
||||||
}
|
}
|
||||||
@ -1665,7 +1666,7 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
* At this point all states with in a partion have transitions
|
* At this point all states with in a partion have transitions
|
||||||
* to same states within the same partitions
|
* to same states within the same partitions
|
||||||
*/
|
*/
|
||||||
for (list <States *>::iterator p = partitions.begin();
|
for (list <Partition *>::iterator p = partitions.begin();
|
||||||
p != partitions.end(); p++) {
|
p != partitions.end(); p++) {
|
||||||
/* representative state for this partition */
|
/* representative state for this partition */
|
||||||
State *rep = *((*p)->begin());
|
State *rep = *((*p)->begin());
|
||||||
@ -1674,13 +1675,13 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
Trans::iterator i = trans.find(rep);
|
Trans::iterator i = trans.find(rep);
|
||||||
if (i != trans.end()) {
|
if (i != trans.end()) {
|
||||||
if (i->second.otherwise) {
|
if (i->second.otherwise) {
|
||||||
map <State *, States *>::iterator z = partition_map.find(i->second.otherwise);
|
map <State *, Partition *>::iterator z = partition_map.find(i->second.otherwise);
|
||||||
States *partition = partition_map.find(i->second.otherwise)->second;
|
Partition *partition = partition_map.find(i->second.otherwise)->second;
|
||||||
i->second.otherwise = *partition->begin();
|
i->second.otherwise = *partition->begin();
|
||||||
}
|
}
|
||||||
for (Cases::iterator c = i->second.begin();
|
for (Cases::iterator c = i->second.begin();
|
||||||
c != i->second.end(); c++) {
|
c != i->second.end(); c++) {
|
||||||
States *partition = partition_map.find(c->second)->second;
|
Partition *partition = partition_map.find(c->second)->second;
|
||||||
c->second = *partition->begin();
|
c->second = *partition->begin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1689,7 +1690,7 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
/* make sure nonmatching and start state are up to date with the
|
/* make sure nonmatching and start state are up to date with the
|
||||||
* mappings */
|
* mappings */
|
||||||
{
|
{
|
||||||
States *partition = partition_map.find(nonmatching)->second;
|
Partition *partition = partition_map.find(nonmatching)->second;
|
||||||
if (*partition->begin() != nonmatching) {
|
if (*partition->begin() != nonmatching) {
|
||||||
nonmatching = *partition->begin();
|
nonmatching = *partition->begin();
|
||||||
}
|
}
|
||||||
@ -1703,9 +1704,9 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
/* Now that the states have been remapped, remove all states
|
/* Now that the states have been remapped, remove all states
|
||||||
* that are not the representive states for their partition
|
* that are not the representive states for their partition
|
||||||
*/
|
*/
|
||||||
for (list <States *>::iterator p = partitions.begin();
|
for (list <Partition *>::iterator p = partitions.begin();
|
||||||
p != partitions.end(); p++) {
|
p != partitions.end(); p++) {
|
||||||
for (States::iterator i = ++(*p)->begin(); i != (*p)->end(); i++) {
|
for (Partition::iterator i = ++(*p)->begin(); i != (*p)->end(); i++) {
|
||||||
Trans::iterator j = trans.find(*i);
|
Trans::iterator j = trans.find(*i);
|
||||||
if (j != trans.end())
|
if (j != trans.end())
|
||||||
trans.erase(j);
|
trans.erase(j);
|
||||||
@ -1716,7 +1717,7 @@ void DFA::minimize(dfaflags_t flags)
|
|||||||
out:
|
out:
|
||||||
/* Cleanup */
|
/* Cleanup */
|
||||||
while (!partitions.empty()) {
|
while (!partitions.empty()) {
|
||||||
States *p = partitions.front();
|
Partition *p = partitions.front();
|
||||||
partitions.pop_front();
|
partitions.pop_front();
|
||||||
delete(p);
|
delete(p);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user