From 678652dd36872b3b736a54a83b9faa7eb03c6bb2 Mon Sep 17 00:00:00 2001 From: Dmitry Koltunov Date: Tue, 30 Jul 2019 06:11:38 +0300 Subject: [PATCH] Fix for `CFIEntry.get_decoded()` (#232) * test: test `CFIEntry.get_decoded()` This test detects an error in `CFIEntry.get_decoded()`, that occurs when decodes the `DW_CFA_def_cfa_register` instruction without some CFA definition previously. Signed-off-by: Koltunov Dmitry * add empty `cfa` for fixup decode of the `DW_CFA_def_cfa_register` Signed-off-by: Koltunov Dmitry --- elftools/dwarf/callframe.py | 6 ++--- test/test_callframe.py | 23 +++++++++++++++++- test/testfiles_for_unittests/simple_mipsel.c | 14 +++++++++++ .../testfiles_for_unittests/simple_mipsel.elf | Bin 0 -> 8948 bytes 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 test/testfiles_for_unittests/simple_mipsel.c create mode 100755 test/testfiles_for_unittests/simple_mipsel.elf diff --git a/elftools/dwarf/callframe.py b/elftools/dwarf/callframe.py index bcef78d..46116ca 100644 --- a/elftools/dwarf/callframe.py +++ b/elftools/dwarf/callframe.py @@ -467,7 +467,7 @@ class CFIEntry(object): if isinstance(self, CIE): # For a CIE, initialize cur_line to an "empty" line cie = self - cur_line = dict(pc=0, cfa=None) + cur_line = dict(pc=0, cfa=CFARule(reg=None, offset=0)) reg_order = [] else: # FDE # For a FDE, we need to decode the attached CIE first, because its @@ -479,7 +479,7 @@ class CFIEntry(object): last_line_in_CIE = copy.copy(cie_decoded_table.table[-1]) cur_line = copy.copy(last_line_in_CIE) else: - cur_line = dict(cfa=None) + cur_line = dict(cfa=CFARule(reg=None, offset=0)) cur_line['pc'] = self['initial_location'] reg_order = copy.copy(cie_decoded_table.reg_order) @@ -575,7 +575,7 @@ class CFIEntry(object): # The current line is appended to the table after all instructions # have ended, if there were instructions. - if cur_line['cfa'] is not None or len(cur_line) > 2: + if cur_line['cfa'].reg is not None or len(cur_line) > 2: table.append(cur_line) return DecodedCallFrameTable(table=table, reg_order=reg_order) diff --git a/test/test_callframe.py b/test/test_callframe.py index 5be2717..0fb9dde 100644 --- a/test/test_callframe.py +++ b/test/test_callframe.py @@ -9,10 +9,12 @@ import unittest from elftools.common.py3compat import BytesIO from elftools.dwarf.callframe import ( CallFrameInfo, CIE, FDE, instruction_name, CallFrameInstruction, - RegisterRule) + RegisterRule, DecodedCallFrameTable, CFARule) from elftools.dwarf.structs import DWARFStructs from elftools.dwarf.descriptions import (describe_CFI_instructions, set_global_machine_arch) +from elftools.elf.elffile import ELFFile +from os.path import join class TestCallFrame(unittest.TestCase): @@ -145,6 +147,25 @@ class TestCallFrame(unittest.TestCase): ( ' DW_CFA_def_cfa: r7 (edi) ofs 2\n' + ' DW_CFA_expression: r2 (edx) (DW_OP_addr: 201; DW_OP_deref; DW_OP_deref)\n')) + def test_CFIEntry_get_decoded(self): + oracle_decoded = DecodedCallFrameTable( + table = [ + {'pc': 0, 'cfa': CFARule(reg = 29, offset = 0, expr = None)} + ], + reg_order = [] + ) + + test_dir = join('test', 'testfiles_for_unittests') + with open(join(test_dir, 'simple_mipsel.elf'), 'rb') as f: + elf = ELFFile(f) + di = elf.get_dwarf_info() + entries = di.CFI_entries() + decoded = entries[0].get_decoded() + self.assertEqual(oracle_decoded.table[0]['cfa'].reg, + decoded.table[0]['cfa'].reg + ) + self.assertEqual(oracle_decoded.table[0]['cfa'].offset, + decoded.table[0]['cfa'].offset) if __name__ == '__main__': unittest.main() diff --git a/test/testfiles_for_unittests/simple_mipsel.c b/test/testfiles_for_unittests/simple_mipsel.c new file mode 100644 index 0000000..601434b --- /dev/null +++ b/test/testfiles_for_unittests/simple_mipsel.c @@ -0,0 +1,14 @@ +/* Generated by compiling with any GCC version for MIPS Little Endian. +** GCC 5.4.0 is fine. + +/usr/bin/mipsel-linux-gnu-gcc -g -O0 ./simple_mipsel.c -o ./simple_mipsel.elf +*/ + +void main(void) +{ + int a = 1, b = 2, res; + + res = a + b; + + return; +} diff --git a/test/testfiles_for_unittests/simple_mipsel.elf b/test/testfiles_for_unittests/simple_mipsel.elf new file mode 100755 index 0000000000000000000000000000000000000000..86c25611ea9cc2687d4d7f02e56ee934faa91073 GIT binary patch literal 8948 zcmeHMU2I&%6+XMyP8>|)YsV!v7}zB1*lpwMpEwbr2G&3MF*rXYP6ZX}_5Rsjvfkb9 z-HjYkQS&1ap@?-6Dj=nm3zX&o)dk8!RYIfmp@D~rhf<*mA*&J=MxQnsS#3h)gkFc`9X`wT2stk2}rH9 zOPd5`4cf`;70QHU3>}EoXsZGmz-K@|;|nQV51L3Gn#s>xMCBs*#3r;6$*%zdG7I1@ zfJdz4YaOmw2t`O9SSJc;pB8B2lR9F*!fDXY0O>dRemq<7Xz_;>E`TOdx5k&Cx5f+lMIk&kk*O8brA5y?lls{&;@lZi-YGG;kxtDC}XGlA^8@8B^! z-57HmW$+88(G5YlQ9kp@YZaubnzQ{%Cw(%2)cIM%D;JMn_JtOw4axh|yoF8M>sSYuU4#dXLC$#vkr1FWSS@CMAIY4*!bl&`G!$yd-u zpv?f3Pw2@|M?Ff~Y(N=Axe+DT$A5~&^djsQQL_IflplirvOqtm ze=U%D8RwNi8IFsA#WGW4rEiQ0Z7-h#GyveYJYEx0IvZf&2cRiS*eHK5zKU|xi(@?^ z^;6J{6QLeaqwK<3tUZ+MZ=sw8J>=1}&-oxUz>{yQn)?W#;Cj~(KxH%V68s?`ma;?D z+{YFa0~K_Db=$@YjM`BrZzocTTqQk@22DG;a5iU8g%c^XOiZOyw%cNhJv@5Aj>WU_ ziG-7jXGaeVCex|-XgHFLyS&vocb~A2cwGjQVaJI(LPxj}c8NW7|H1wP!-Mw7@WFeA z5AK7_BO^ohf&Rl#c?hOsiIkmp;xXAbGCVM7_gGyR3f~6hKMu~FF}87SbDa{*L;_2} zFXt7-T1(&JlV{pCFFbE76wkGp^5%St{Ca->%%g=W37M@m0ogzEd|};Uz+l}c^=kPH zpr1K=rY#6w-`d@6C%ks_FRUgrTeG|Ef+qw0g&J8f0&=d+b3k2n-9pVi2K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