Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions floss/features/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def extract_function_loop(f):
parse if a function has a loop
"""
edges = []
bb_by_va = {bb.va: bb for bb in f.basic_blocks}
Comment on lines 293 to +294
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The dictionary bb_by_va is created for every function, even those without loops. Since most functions do not contain loops (SCCs of size >= 2), this is an unnecessary overhead. It is more efficient to initialize this dictionary lazily only when a loop is detected.

Suggested change
edges = []
bb_by_va = {bb.va: bb for bb in f.basic_blocks}
edges = []


for bb in f.basic_blocks:
if len(bb.instructions) > 0:
Expand All @@ -313,8 +314,15 @@ def extract_function_loop(f):
comps = strongly_connected_components(g)
for comp in comps:
if len(comp) >= 2:
# TODO get list of bb start/end eas
yield Loop(comp)
loop_bb_ranges = []
for bb_va in sorted(comp):
bb = bb_by_va.get(bb_va)
if bb is None:
continue

loop_bb_ranges.append((bb.va, bb.va + bb.size))

yield Loop(comp, bb_ranges=loop_bb_ranges)
Comment on lines 315 to +325
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Implementing lazy initialization for bb_by_va here ensures that the dictionary is only constructed when at least one loop is identified in the function, avoiding unnecessary computation for the majority of functions.

    bb_by_va = None
    for comp in comps:
        if len(comp) >= 2:
            if bb_by_va is None:
                bb_by_va = {bb.va: bb for bb in f.basic_blocks}
            loop_bb_ranges = []
            for bb_va in sorted(comp):
                bb = bb_by_va.get(bb_va)
                if bb is None:
                    continue

                loop_bb_ranges.append((bb.va, bb.va + bb.size))

            yield Loop(comp, bb_ranges=loop_bb_ranges)



FUNCTION_HANDLERS = (
Expand Down
3 changes: 2 additions & 1 deletion floss/features/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def score(self):
class Loop(Feature):
weight = MEDIUM

def __init__(self, comp):
def __init__(self, comp, bb_ranges=None):
super(Loop, self).__init__(len(comp))

self.comp = comp
self.bb_ranges = bb_ranges or []

def score(self):
return 1.0
Expand Down
12 changes: 8 additions & 4 deletions floss/main.py
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this please

Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def make_parser(argv):
" 1. Go: strings from binaries written in Go\n"
" 2. Rust: strings from binaries written in Rust\n"
)
epilog = textwrap.dedent("""
epilog = textwrap.dedent(
"""
only displaying core arguments, run `floss -H` to see all supported options

examples:
Expand All @@ -138,8 +139,10 @@ def make_parser(argv):

only extract stack and tight strings
floss --only stack tight -- suspicious.exe
""")
epilog_advanced = textwrap.dedent("""
"""
)
epilog_advanced = textwrap.dedent(
"""
examples:
extract all strings from 32-bit shellcode
floss -f sc32 shellcode.bin
Expand All @@ -149,7 +152,8 @@ def make_parser(argv):

extract strings from a binary written in Go (if automatic language identification fails)
floss --language go program.exe
""")
"""
)

show_all_options = "-H" in argv

Expand Down
6 changes: 4 additions & 2 deletions tests/test_load.py
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import floss.main

# floss --no static -j tests/data/src/decode-in-place/bin/test-decode-in-place.exe
RESULTS = textwrap.dedent("""
RESULTS = textwrap.dedent(
"""
{
"analysis": {
"enable_decoded_strings": true,
Expand Down Expand Up @@ -83,7 +84,8 @@
"tight_strings": []
}
}
""")
"""
)


def test_load(tmp_path):
Expand Down