Skip to content

Commit 4930805

Browse files
authored
Fix unhandled type class (#95)
1 parent d5195b0 commit 4930805

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

python/anvill/__main__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
# You should have received a copy of the GNU Affero General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import os
19+
import sys
1820
import argparse
1921
import json
2022

23+
from .util import INIT_DEBUG_FILE
2124
from .binja import get_program
2225

2326

@@ -43,8 +46,17 @@ def main():
4346
default=False,
4447
)
4548

49+
arg_parser.add_argument(
50+
"--log_file",
51+
type=argparse.FileType('w'), default=os.devnull,
52+
help="Log to a specific file."
53+
)
54+
4655
args = arg_parser.parse_args()
4756

57+
if args.log_file != os.devnull:
58+
INIT_DEBUG_FILE(args.log_file)
59+
4860
p = get_program(args.bin_in)
4961

5062
ep = None

python/anvill/binja.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .os import *
2525
from .type import *
2626
from .program import *
27+
from .util import *
2728

2829

2930
def is_valid_addr(bv, addr):
@@ -243,9 +244,13 @@ def _convert_bn_type(tinfo: bn.types.Type, cache):
243244
bn.TypeClass.NamedTypeReferenceClass,
244245
bn.TypeClass.WideCharTypeClass,
245246
]:
246-
raise UnhandledTypeException(
247-
"Unhandled VarArgs, Value, or WideChar type: {}".format(str(tinfo)), tinfo
248-
)
247+
err_type_class = {
248+
bn.TypeClass.VarArgsTypeClass : "VarArgsTypeClass",
249+
bn.TypeClass.ValueTypeClass : "ValueTypeClass",
250+
bn.TypeClass.NamedTypeReferenceClass : "NamedTypeReferenceClass",
251+
bn.TypeClass.WideCharTypeClass : "WideCharTypeClass",
252+
}
253+
DEBUG("WARNING: Unhandled type class {}".format(err_type_class[tinfo.type_class]))
249254

250255
else:
251256
raise UnhandledTypeException("Unhandled type: {}".format(str(tinfo)), tinfo)
@@ -472,6 +477,10 @@ def visit(self, program, is_definition, add_refs_as_defs):
472477
if not is_definition:
473478
return
474479

480+
# type could be None if type class not handled
481+
if self._type is None:
482+
return
483+
475484
if isinstance(self._type, VoidType):
476485
return
477486

@@ -484,6 +493,11 @@ def visit(self, program, is_definition, add_refs_as_defs):
484493
for ea in range(begin, end):
485494
br.seek(ea)
486495
seg = bv.get_segment_at(ea)
496+
# _elf_header is getting recovered as variable
497+
# get_segment_at(...) returns None for elf_header
498+
if seg is None:
499+
continue
500+
487501
mem.map_byte(ea, br.read8(), seg.writable, seg.executable)
488502

489503

@@ -598,6 +612,8 @@ def get_program(*args, **kargs):
598612
return _PROGRAM
599613
assert len(args) == 1
600614

615+
DEBUG("Recovering program {}".format(args[0]))
616+
601617
prog = BNProgram(args[0])
602618
if "cache" not in kargs or kargs["cache"]:
603619
_PROGRAM = prog

python/anvill/util.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) 2021 Trail of Bits, Inc.
2+
#
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU Affero General Public License as
5+
# published by the Free Software Foundation, either version 3 of the
6+
# License, or (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU Affero General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Affero General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
17+
_DEBUG_FILE = None
18+
_DEBUG_PREFIX = ""
19+
20+
def INIT_DEBUG_FILE(file):
21+
global _DEBUG_FILE
22+
_DEBUG_FILE = file
23+
24+
def DEBUG_PUSH():
25+
global _DEBUG_PREFIX
26+
_DEBUG_PREFIX += " "
27+
28+
def DEBUG_POP():
29+
global _DEBUG_PREFIX
30+
_DEBUG_PREFIX = _DEBUG_PREFIX[:-2]
31+
32+
def DEBUG(s):
33+
global _DEBUG_FILE
34+
if _DEBUG_FILE:
35+
_DEBUG_FILE.write("{}{}\n".format(_DEBUG_PREFIX, str(s)))

python/anvill/var.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ def is_declaration(self):
4444
def proto(self):
4545
proto = {}
4646
proto["address"] = self.address()
47-
proto["type"] = self.type().proto(self._arch)
47+
if self.type() != None:
48+
proto["type"] = self.type().proto(self._arch)
49+
4850
return proto

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
"anvill.__init__", "anvill.__main__", "anvill.arch", "anvill.binja",
3333
"anvill.exc", "anvill.function", "anvill.ida", "anvill.loc",
3434
"anvill.mem", "anvill.os", "anvill.program", "anvill.type",
35-
"anvill.var"])
35+
"anvill.var", "anvill.util"])

0 commit comments

Comments
 (0)