2
* (C) 2006, 2007 Andreas Gruenbacher <agruen@suse.de>
3
* Copyright (c) 2003-2008 Novell, Inc. (All rights reserved)
4
* Copyright 2009-2010 Canonical Ltd.
6
* The libapparmor library is licensed under the terms of the GNU
7
* Lesser General Public License, version 2.1. Please see the file
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
* Base of implementation based on the Lexical Analysis chapter of:
20
* Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman:
21
* Compilers: Principles, Techniques, and Tools (The "Dragon Book"),
22
* Addison-Wesley, 1986.
24
#ifndef __LIBAA_RE_HFA_H
25
#define __LIBAA_RE_HFA_H
33
#include "expr-tree.h"
37
* State cases are identical to NodesCases except they map to State *
39
* Out-edges from a state to another: we store the follow State
40
* for each input character that is not a default match in cases and
41
* default matches in otherwise as well as in all matching explicit cases
42
* This avoids enumerating all the explicit tranitions for default matches.
44
typedef struct Cases {
45
typedef map<uchar, State *>::iterator iterator;
46
iterator begin() { return cases.begin(); }
47
iterator end() { return cases.end(); }
49
Cases(): otherwise(0) { }
51
map<uchar, State *> cases;
55
typedef list<State *> Partition;
57
uint32_t accept_perms(NodeSet *state, uint32_t *audit_ctl, int *error);
60
* State - DFA individual state information
61
* label: a unique label to identify the state used for pretty printing
62
* the non-matching state is setup to have label == 0 and
63
* the start state is setup to have label == 1
64
* audit: the audit permission mask for the state
65
* accept: the accept permissions for the state
66
* cases: set of transitions from this state
67
* parition: Is a temporary work variable used during dfa minimization.
68
* it can be replaced with a map, but that is slower and uses more
70
* nodes: Is a temporary work variable used during dfa creation. It can
71
* be replaced by using the nodemap, but that is slower
75
State(): label(0), audit(0), accept(0), cases(), nodes(NULL) { };
76
State(int l): label(l), audit(0), accept(0), cases(), nodes(NULL) { };
77
State(int l, NodeSet * n) throw(int):
78
label(l), audit(0), accept(0), cases(), nodes(n)
82
/* Compute permissions associated with the State. */
83
accept = accept_perms(nodes, &audit, &error);
85
//cerr << "Failing on accept perms " << error << "\n";
91
uint32_t audit, accept;
99
ostream &operator<<(ostream &os, const State &state);
101
typedef map<pair<unsigned long, NodeSet *>, State *, deref_less_than> NodeMap;
102
/* Transitions in the DFA. */
104
/* dfa_stats - structure to group various stats about dfa creation
105
* duplicates - how many duplicate NodeSets where encountered and discarded
106
* proto_max - maximum length of a NodeSet encountered during dfa construction
107
* proto_sum - sum of NodeSet length during dfa construction. Used to find
110
typedef struct dfa_stats {
111
unsigned int duplicates, proto_max, proto_sum;
115
void dump_node_to_dfa(void);
116
State *add_new_state(NodeMap &nodemap,
117
pair<unsigned long, NodeSet *> index,
118
NodeSet *nodes, dfa_stats_t &stats);
119
void update_state_transitions(NodeMap &nodemap,
120
list<State *> &work_queue,
121
State *state, dfa_stats_t &stats);
122
State *find_target_state(NodeMap &nodemap, list<State *> &work_queue,
123
NodeSet *nodes, dfa_stats_t &stats);
125
DFA(Node *root, dfaflags_t flags);
127
void remove_unreachable(dfaflags_t flags);
128
bool same_mappings(State *s1, State *s2);
129
size_t hash_trans(State *s);
130
void minimize(dfaflags_t flags);
131
void dump(ostream &os);
132
void dump_dot_graph(ostream &os);
133
void dump_uniq_perms(const char *s);
134
map<uchar, uchar> equivalence_classes(dfaflags_t flags);
135
void apply_equivalence_classes(map<uchar, uchar> &eq);
137
State *nonmatching, *start;
141
void dump_equivalence_classes(ostream &os, map<uchar, uchar> &eq);
143
#endif /* __LIBAA_RE_HFA_H */