Skip to contentSkip to conversation

Sepolgen: improve parser error recovery

PATCH In review Karl MacMillan 2 participants 5 messages Mar 12, 2010 – Mar 23, 2010 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Sepolgen: improve parser error recovery

Sepolgen has long not recovered from parsing errors, leading to a blacklist of none bad modules in the source. I finally tracked down the problem (lexer state) and this patch fixes the problem by causing the lexer to be rebuilt on error.

---
 sepolgen/src/sepolgen/refparser.py |   28 ++++++++++++----------------
 1 files changed, 12 insertions(+), 16 deletions(-)
(quoted diff) +0 -0

b/sepolgen/src/sepolgen/refparser.py
index 23beb39..5c5093c 100644

sepolgen/src/sepolgen/refparser.py +2 -4
@@ -279,7 +279,7 @@ parse_file = ""
 #     refpolicy.SupportMacros and should always be present during parsing
 #     though it may not contain any macros.
 spt = None
-success=True
+success = True
 
 # utilities
 def collect(stmts, parent, val=None):
@@ -921,9 +921,7 @@ def p_optional_semi(p):
 #
 
 def p_error(tok):
-    global error
-    global parse_file
-    global success
+    global error, parse_file, success, parser
     error = "%s: Syntax error on line %d %s [type=%s]" % (parse_file,

tok.lineno, tok.value, tok.type)
print error
success = False
@@ -939,6 +937,7 @@ parser = None
lexer = None
def create_globals(module, support, debug):
global parser, lexer, m, spt
+
if not parser:
lexer = lex.lex()
parser = yacc.yacc(method="LALR", debug=debug, write_tables=0)
@@ -955,17 +954,20 @@ def create_globals(module, support, debug):

def parse(text, module=None, support=None, debug=False):
create_globals(module, support, debug)

-    lexer.lexdata = []
-    lexer.lexpos = 0
-    lexer.lineno = 1
+    global error, parser, lexer, success

+
+ success = True

try:
- parser.parse(text, debug=debug)
+ parser.parse(text, debug=debug, lexer=lexer)
except Exception, e:

-        global error
+        parser = None
+        lexer = None

error = "internal parser error: %s" % str(e) + "\n" +
traceback.format_exc()

- if error is not None:
+ if not success:
+ # force the parser and lexer to be rebuilt - we have some
problems otherwise
+ parser = None
msg = 'could not parse text: "%s"' % error
raise ValueError(msg)
return m
@@ -973,15 +975,9 @@ def parse(text, module=None, support=None, debug=False):
def list_headers(root):
modules = []
support_macros = None
- blacklist = ["init.if", "inetd.if", "uml.if", "thunderbird.if"]

for dirpath, dirnames, filenames in os.walk(root):
for name in filenames:
- # FIXME: these make the parser barf in various
unrecoverable ways, so we must skip
- # them.
- if name in blacklist:
- continue
-
modname = os.path.splitext(name) filename = os.path.join(dirpath, name)

--
1.6.6

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 5 lines · Karl MacMillan

Sepolgen has long not recovered from parsing errors, leading to a blacklist of none bad modules in the source. I finally tracked down the problem (lexer state) and this patch fixes the problem by causing the lexer to be rebuilt on error.
---

So do the modules that were blacklisted still fail to parse and this just handles it by reinitializing the lexer and parser?

Quoted 86 lines
  sepolgen/src/sepolgen/refparser.py |   28 ++++++++++++----------------
  1 files changed, 12 insertions(+), 16 deletions(-)
sepolgen/src/sepolgen/refparser.py +2 -4
index 23beb39..5c5093c 100644
@@ -279,7 +279,7 @@ parse_file = ""
  #     refpolicy.SupportMacros and should always be present during parsing
  #     though it may not contain any macros.
  spt = None
-success=True
+success = True
 
  # utilities
  def collect(stmts, parent, val=None):
@@ -921,9 +921,7 @@ def p_optional_semi(p):
  #
 
  def p_error(tok):
-    global error
-    global parse_file
-    global success
+    global error, parse_file, success, parser
      error = "%s: Syntax error on line %d %s [type=%s]" % (parse_file,
 tok.lineno, tok.value, tok.type)
      print error

success = False

sepolgen/src/sepolgen/refparser.py +9 -6
@@ -939,6 +937,7 @@ parser = None
  lexer = None
  def create_globals(module, support, debug):
      global parser, lexer, m, spt
+
      if not parser:
          lexer = lex.lex()
          parser = yacc.yacc(method="LALR", debug=debug, write_tables=0)
@@ -955,17 +954,20 @@ def create_globals(module, support, debug):
 
  def parse(text, module=None, support=None, debug=False):
      create_globals(module, support, debug)
-    lexer.lexdata = []
-    lexer.lexpos = 0
-    lexer.lineno = 1
+    global error, parser, lexer, success
+
+    success = True
 
      try:
-        parser.parse(text, debug=debug)
+        parser.parse(text, debug=debug, lexer=lexer)
      except Exception, e:
-        global error
+        parser = None
+        lexer = None
          error = "internal parser error: %s" % str(e) + "\n" +
 traceback.format_exc()
 
-    if error is not None:
+    if not success:
+        # force the parser and lexer to be rebuilt - we have some

problems otherwise

sepolgen/src/sepolgen/refparser.py +1 -2
+        parser = None
          msg = 'could not parse text: "%s"' % error
          raise ValueError(msg)
      return m
@@ -973,15 +975,9 @@ def parse(text, module=None, support=None, debug=False):
  def list_headers(root):
      modules = []
      support_macros = None
-    blacklist = ["init.if", "inetd.if", "uml.if", "thunderbird.if"]
 
      for dirpath, dirnames, filenames in os.walk(root):
          for name in filenames:
-            # FIXME: these make the parser barf in various

unrecoverable ways, so we must skip

sepolgen/src/sepolgen/refparser.py +0 -5
-            # them.
-            if name in blacklist:
-                continue
-
              modname = os.path.splitext(name)
              filename = os.path.join(dirpath, name)
 
--

1.6.6

Quoted 5 lines

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#2 lore raw
Quoted 11 lines · Joshua Brindle
Quoted 6 lines · Karl MacMillan

Sepolgen has long not recovered from parsing errors, leading to a blacklist of none bad modules in the source. I finally tracked down the problem (lexer state) and this patch fixes the problem by causing the lexer to be rebuilt on error.
---

So do the modules that were blacklisted still fail to parse and this just handles it by reinitializing the lexer and parser?

This patch just re-initializes the lexer and parser - it doesn't do anything to fix the old parser errors. Those are not really fixable in any clean way. However, on my FC12 system all of the modules seem to parse fine because of refpolicy changes.

Karl

Quoted 87 lines
Quoted 87 lines

 sepolgen/src/sepolgen/refparser.py |   28 ++++++++++++----------------

 1 files changed, 12 insertions(+), 16 deletions(-)
sepolgen/src/sepolgen/refparser.py +0 -0
index 23beb39..5c5093c 100644
@@ -279,7 +279,7 @@ parse_file = ""
  #     refpolicy.SupportMacros and should always be present during parsing
  #     though it may not contain any macros.

 spt = None

sepolgen/src/sepolgen/refparser.py +1 -1
-success=True
+success = True
 # utilities
 def collect(stmts, parent, val=None):
sepolgen/src/sepolgen/refparser.py +0 -0
@@ -921,9 +921,7 @@ def p_optional_semi(p):
  #
 

 def p_error(tok):
-    global error
-    global parse_file
-    global success
+    global error, parse_file, success, parser
     error = "%s: Syntax error on line %d %s [type=%s]" % (parse_file, tok.lineno, tok.value, tok.type)
     print error
     success = False

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -939,6 +937,7 @@ parser = None

 lexer = None
 def create_globals(module, support, debug):
     global parser, lexer, m, spt
+
     if not parser:
         lexer = lex.lex()
         parser = yacc.yacc(method="LALR", debug=debug, write_tables=0)

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -955,17 +954,20 @@ def create_globals(module, support, debug):
 

 def parse(text, module=None, support=None, debug=False):
     create_globals(module, support, debug)
-    lexer.lexdata = []
-    lexer.lexpos = 0
-    lexer.lineno = 1
+    global error, parser, lexer, success
+
+    success = True

     try:
-        parser.parse(text, debug=debug)
+        parser.parse(text, debug=debug, lexer=lexer) except Exception, e:
-        global error
+        parser = None
+        lexer = None
         error = "internal parser error: %s" % str(e) + "\n" + traceback.format_exc()

-    if error is not None:
+    if not success:
+        # force the parser and lexer to be rebuilt - we have some problems otherwise
+        parser = None
         msg = 'could not parse text: "%s"' % error raise ValueError(msg)
     return m

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -973,15 +975,9 @@ def parse(text, module=None, support=None,

debug=False):
 def list_headers(root):
     modules = []
     support_macros = None
-    blacklist = ["init.if", "inetd.if", "uml.if", "thunderbird.if"]

     for dirpath, dirnames, filenames in os.walk(root):
         for name in filenames:
-            # FIXME: these make the parser barf in various unrecoverable ways, so we must skip
-            # them.
-            if name in blacklist:
-                continue
-
             modname = os.path.splitext(name) filename = os.path.join(dirpath, name)

--
1.6.6

Quoted 7 lines
Quoted 6 lines

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#3 lore raw
Quoted 7 lines · Karl MacMillan
Quoted 6 lines · Joshua Brindle
Quoted 5 lines · Karl MacMillan

Sepolgen has long not recovered from parsing errors, leading to a blacklist of none bad modules in the source. I finally tracked down the problem (lexer state) and this patch fixes the problem by causing the lexer to be rebuilt on error.
---

Quoted 9 lines
Quoted 3 lines

So do the modules that were blacklisted still fail to parse and this just handles it by reinitializing the lexer and parser?

This patch just re-initializes the lexer and parser - it doesn't do anything to fix the old parser errors. Those are not really fixable in any clean way. However, on my FC12 system all of the modules seem to parse fine because of refpolicy changes.

Ok. The change looks reasonable, do you want to merge it or do you want me to?

Quoted 89 lines

Karl

Quoted 87 lines
Quoted 87 lines
  sepolgen/src/sepolgen/refparser.py |   28 ++++++++++++----------------
  1 files changed, 12 insertions(+), 16 deletions(-)
sepolgen/src/sepolgen/refparser.py +2 -4
index 23beb39..5c5093c 100644
@@ -279,7 +279,7 @@ parse_file = ""
  #     refpolicy.SupportMacros and should always be present during parsing
  #     though it may not contain any macros.
  spt = None
-success=True
+success = True
 
  # utilities
  def collect(stmts, parent, val=None):
@@ -921,9 +921,7 @@ def p_optional_semi(p):
  #
 
  def p_error(tok):
-    global error
-    global parse_file
-    global success
+    global error, parse_file, success, parser
      error = "%s: Syntax error on line %d %s [type=%s]" % (parse_file,
 tok.lineno, tok.value, tok.type)
      print error

success = False

sepolgen/src/sepolgen/refparser.py +9 -6
@@ -939,6 +937,7 @@ parser = None
  lexer = None
  def create_globals(module, support, debug):
      global parser, lexer, m, spt
+
      if not parser:
          lexer = lex.lex()
          parser = yacc.yacc(method="LALR", debug=debug, write_tables=0)
@@ -955,17 +954,20 @@ def create_globals(module, support, debug):
 
  def parse(text, module=None, support=None, debug=False):
      create_globals(module, support, debug)
-    lexer.lexdata = []
-    lexer.lexpos = 0
-    lexer.lineno = 1
+    global error, parser, lexer, success
+
+    success = True
 
      try:
-        parser.parse(text, debug=debug)
+        parser.parse(text, debug=debug, lexer=lexer)
      except Exception, e:
-        global error
+        parser = None
+        lexer = None
          error = "internal parser error: %s" % str(e) + "\n" +
 traceback.format_exc()
 
-    if error is not None:
+    if not success:
+        # force the parser and lexer to be rebuilt - we have some

problems otherwise

sepolgen/src/sepolgen/refparser.py +1 -0
+        parser = None
          msg = 'could not parse text: "%s"' % error
          raise ValueError(msg)
      return m
@@ -973,15 +975,9 @@ def parse(text, module=None, support=None,

debug=False):
def list_headers(root):
modules = []
support_macros = None

sepolgen/src/sepolgen/refparser.py +0 -2
-    blacklist = ["init.if", "inetd.if", "uml.if", "thunderbird.if"]
 
      for dirpath, dirnames, filenames in os.walk(root):
          for name in filenames:
-            # FIXME: these make the parser barf in various

unrecoverable ways, so we must skip

sepolgen/src/sepolgen/refparser.py +0 -5
-            # them.
-            if name in blacklist:
-                continue
-
              modname = os.path.splitext(name)
              filename = os.path.join(dirpath, name)
 
--

1.6.6

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#4 lore raw
Quoted 23 lines · Joshua Brindle
Quoted 12 lines · Karl MacMillan
Quoted 9 lines · Joshua Brindle
Quoted 6 lines · Karl MacMillan

Sepolgen has long not recovered from parsing errors, leading to a blacklist of none bad modules in the source. I finally tracked down the problem (lexer state) and this patch fixes the problem by causing the lexer to be rebuilt on error.
---

Quoted 9 lines
Quoted 3 lines

So do the modules that were blacklisted still fail to parse and this just handles it by reinitializing the lexer and parser?

This patch just re-initializes the lexer and parser - it doesn't do anything to fix the old parser errors. Those are not really fixable in any clean way. However, on my FC12 system all of the modules seem to parse fine because of refpolicy changes.

Quoted 5 lines

Ok. The change looks reasonable, do you want to merge it or do you want me to?

Merged as sepolgen 1.0.22

Quoted 90 lines
Quoted 90 lines

Karl

Quoted 88 lines
Quoted 88 lines

 sepolgen/src/sepolgen/refparser.py |   28 ++++++++++++----------------

 1 files changed, 12 insertions(+), 16 deletions(-)
sepolgen/src/sepolgen/refparser.py +0 -0
index 23beb39..5c5093c 100644
@@ -279,7 +279,7 @@ parse_file = ""
  #     refpolicy.SupportMacros and should always be present during

parsing
 #     though it may not contain any macros.
 spt = None

sepolgen/src/sepolgen/refparser.py +1 -1
-success=True
+success = True
 # utilities
 def collect(stmts, parent, val=None):
sepolgen/src/sepolgen/refparser.py +0 -0
@@ -921,9 +921,7 @@ def p_optional_semi(p):
  #
 

 def p_error(tok):
-    global error
-    global parse_file
-    global success
+    global error, parse_file, success, parser
     error = "%s: Syntax error on line %d %s [type=%s]" % (parse_file, tok.lineno, tok.value, tok.type)
     print error
     success = False

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -939,6 +937,7 @@ parser = None

 lexer = None
 def create_globals(module, support, debug):
     global parser, lexer, m, spt
+
     if not parser:
         lexer = lex.lex()
         parser = yacc.yacc(method="LALR", debug=debug, write_tables=0)

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -955,17 +954,20 @@ def create_globals(module, support, debug):
 

 def parse(text, module=None, support=None, debug=False):
     create_globals(module, support, debug)
-    lexer.lexdata = []
-    lexer.lexpos = 0
-    lexer.lineno = 1
+    global error, parser, lexer, success
+
+    success = True

     try:
-        parser.parse(text, debug=debug)
+        parser.parse(text, debug=debug, lexer=lexer) except Exception, e:
-        global error
+        parser = None
+        lexer = None
         error = "internal parser error: %s" % str(e) + "\n" + traceback.format_exc()

-    if error is not None:
+    if not success:
+        # force the parser and lexer to be rebuilt - we have some problems otherwise
+        parser = None
         msg = 'could not parse text: "%s"' % error raise ValueError(msg)
     return m

sepolgen/src/sepolgen/refparser.py +0 -0
@@ -973,15 +975,9 @@ def parse(text, module=None, support=None,

debug=False):
 def list_headers(root):
     modules = []
     support_macros = None
-    blacklist = ["init.if", "inetd.if", "uml.if", "thunderbird.if"]

     for dirpath, dirnames, filenames in os.walk(root):
         for name in filenames:
-            # FIXME: these make the parser barf in various unrecoverable ways, so we must skip
-            # them.
-            if name in blacklist:
-                continue
-
             modname = os.path.splitext(name) filename = os.path.join(dirpath, name)

--
1.6.6

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.