Skip to content

Commit 34c3cda

Browse files
JPEWdevamikan
authored andcommitted
bitbake: Remove custom exception backtrace formatting
Backport of upstream bitbake patch c25e7ed128b9fd5b53d28d678238e2f3af52ef8b. Removes the code in bitbake to show custom backtrace formatting for exceptions. In particular, the bitbake exception code prints function arguments, which while helpful is a security problem when passwords and other secrets can be passed as function arguments. As it turns out, the handling of the custom serialized exception stack frames was pretty much made obsolete by d7db75020ed ("event/msg: Pass formatted exceptions"), which changed the events to pass a preformatted stacktrack list of strings, but the passing of the serialized data was never removed. Change all the code to use the python traceback API to format exceptions instead of the custom code; conveniently traceback.format_exception() also returns a list of stack trace strings, so it can be used as a drop in replacement for bb.exception.format_exception() [Felix: adjusted to bitbake path in isar repo] Signed-off-by: Joshua Watt <JPEWhacker@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
1 parent 09521ab commit 34c3cda

File tree

5 files changed

+25
-121
lines changed

5 files changed

+25
-121
lines changed

bitbake/lib/bb/cooker.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from io import StringIO, UnsupportedOperation
1818
from contextlib import closing
1919
from collections import defaultdict, namedtuple
20-
import bb, bb.exceptions, bb.command
20+
import bb, bb.command
2121
from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build
2222
import queue
2323
import signal
@@ -2098,7 +2098,6 @@ def parse_filter(self, record):
20982098
except Exception as exc:
20992099
tb = sys.exc_info()[2]
21002100
exc.recipe = filename
2101-
exc.traceback = list(bb.exceptions.extract_traceback(tb, context=3))
21022101
return True, None, exc
21032102
# Need to turn BaseExceptions into Exceptions here so we gracefully shutdown
21042103
# and for example a worker thread doesn't just exit on its own in response to
@@ -2299,8 +2298,12 @@ def parse_next(self):
22992298
return False
23002299
except ParsingFailure as exc:
23012300
self.error += 1
2302-
logger.error('Unable to parse %s: %s' %
2303-
(exc.recipe, bb.exceptions.to_string(exc.realexception)))
2301+
2302+
exc_desc = str(exc)
2303+
if isinstance(exc, SystemExit) and not isinstance(exc.code, str):
2304+
exc_desc = 'Exited with "%d"' % exc.code
2305+
2306+
logger.error('Unable to parse %s: %s' % (exc.recipe, exc_desc))
23042307
self.shutdown(clean=False)
23052308
return False
23062309
except bb.parse.ParseError as exc:
@@ -2309,20 +2312,33 @@ def parse_next(self):
23092312
self.shutdown(clean=False, eventmsg=str(exc))
23102313
return False
23112314
except bb.data_smart.ExpansionError as exc:
2315+
def skip_frames(f, fn_prefix):
2316+
while f and f.tb_frame.f_code.co_filename.startswith(fn_prefix):
2317+
f = f.tb_next
2318+
return f
2319+
23122320
self.error += 1
23132321
bbdir = os.path.dirname(__file__) + os.sep
2314-
etype, value, _ = sys.exc_info()
2315-
tb = list(itertools.dropwhile(lambda e: e.filename.startswith(bbdir), exc.traceback))
2322+
etype, value, tb = sys.exc_info()
2323+
2324+
# Remove any frames where the code comes from bitbake. This
2325+
# prevents deep (and pretty useless) backtraces for expansion error
2326+
tb = skip_frames(tb, bbdir)
2327+
cur = tb
2328+
while cur:
2329+
cur.tb_next = skip_frames(cur.tb_next, bbdir)
2330+
cur = cur.tb_next
2331+
23162332
logger.error('ExpansionError during parsing %s', value.recipe,
23172333
exc_info=(etype, value, tb))
23182334
self.shutdown(clean=False)
23192335
return False
23202336
except Exception as exc:
23212337
self.error += 1
2322-
etype, value, tb = sys.exc_info()
2338+
_, value, _ = sys.exc_info()
23232339
if hasattr(value, "recipe"):
23242340
logger.error('Unable to parse %s' % value.recipe,
2325-
exc_info=(etype, value, exc.traceback))
2341+
exc_info=sys.exc_info())
23262342
else:
23272343
# Most likely, an exception occurred during raising an exception
23282344
import traceback

bitbake/lib/bb/event.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import threading
2020
import traceback
2121

22-
import bb.exceptions
2322
import bb.utils
2423

2524
# This is the pid for which we should generate the event. This is set when
@@ -759,13 +758,7 @@ class LogHandler(logging.Handler):
759758

760759
def emit(self, record):
761760
if record.exc_info:
762-
etype, value, tb = record.exc_info
763-
if hasattr(tb, 'tb_next'):
764-
tb = list(bb.exceptions.extract_traceback(tb, context=3))
765-
# Need to turn the value into something the logging system can pickle
766-
record.bb_exc_info = (etype, value, tb)
767-
record.bb_exc_formatted = bb.exceptions.format_exception(etype, value, tb, limit=5)
768-
value = str(value)
761+
record.bb_exc_formatted = traceback.format_exception(*record.exc_info)
769762
record.exc_info = None
770763
fire(record, None)
771764

bitbake/lib/bb/exceptions.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

bitbake/lib/bb/msg.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ def format(self, record):
8989
msg = logging.Formatter.format(self, record)
9090
if hasattr(record, 'bb_exc_formatted'):
9191
msg += '\n' + ''.join(record.bb_exc_formatted)
92-
elif hasattr(record, 'bb_exc_info'):
93-
etype, value, tb = record.bb_exc_info
94-
formatted = bb.exceptions.format_exception(etype, value, tb, limit=5)
95-
msg += '\n' + ''.join(formatted)
9692
return msg
9793

9894
def colorize(self, record):

bitbake/lib/bb/ui/teamcity.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import bb.command
3131
import bb.cooker
3232
import bb.event
33-
import bb.exceptions
3433
import bb.runqueue
3534
from bb.ui import uihelper
3635

@@ -102,10 +101,6 @@ def format(self, record):
102101
details = ""
103102
if hasattr(record, 'bb_exc_formatted'):
104103
details = ''.join(record.bb_exc_formatted)
105-
elif hasattr(record, 'bb_exc_info'):
106-
etype, value, tb = record.bb_exc_info
107-
formatted = bb.exceptions.format_exception(etype, value, tb, limit=5)
108-
details = ''.join(formatted)
109104

110105
if record.levelno in [bb.msg.BBLogFormatter.ERROR, bb.msg.BBLogFormatter.CRITICAL]:
111106
# ERROR gets a separate errorDetails field

0 commit comments

Comments
 (0)