Skip to content

[BUGFIX] Imports: bound the number of parsed functions - #34

Merged
hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/imports-function-budget
Nov 3, 2026
Merged

hasherezade merged 1 commit into
hasherezade:masterfrom
mkilijanek:fix/imports-function-budget

Conversation

@mkilijanek

Copy link
Copy Markdown
Contributor

Every import descriptor walks its thunk array until a zero thunk, with no limit (LIMIT = -1 in ImportBaseEntryWrapper::wrap). Nothing stops N descriptors from pointing to the same array of M thunks, so a file of size S can produce about (S/20) × (S/4) function wrappers. Same class of problem as hasherezade/pe-bear#77, in the imports instead of the resources; independent of #33.

Change

One budget covers all the functions of a directory: getRawSize() divided by the thunk size (4 or 8). Every imported function of a well-formed file has its own thunk in the file, so the number of functions cannot exceed the number of thunks that fit in it. Each library gets what is left of the budget, a warning is logged when it runs out, and rewrapping one entry does not count its functions twice. The code is in ImportBaseDirWrapper, so the delay-load directory uses it too.

Behaviour change: when several libraries share a thunk array, the functions beyond the budget are not listed.

Verification

Release build with gcc on master. Input: a synthetic PE32 with 400 import descriptors that all point to one array of 10000 thunks (48 KB). bearcommander <file> with the implist command, 30 s limit:

wall time peak memory
master no result at the 30 s limit 409 MB when stopped
this change 0.25 s 13 MB

A smaller file (40 descriptors × 100 thunks, 2 KB) lists 4000 functions on master and 512 with this change, which is the behaviour change above.

test.sh against bearparser_tests: 26 cases print OK with and without the change. x86/kernel32 : explist prints "Failed" on unmodified master too.

Not measured: the delay-load directory. It shares the code path but I did not build an input for it.

Every import descriptor walks its thunk array until a zero thunk, with no
limit (LIMIT = -1 in ImportBaseEntryWrapper::wrap). Nothing stops N
descriptors from pointing to the same array of M thunks, so a file of size S
can produce about (S/20) * (S/4) function wrappers.

A file with 400 descriptors sharing one array of 10000 thunks (48 KB) gives no
result within 30 s and 409 MB of resident memory when stopped; with this
change it is parsed in 0.25 s and 13 MB.

One budget now covers all the functions of a directory: getRawSize() divided
by the thunk size (4 or 8). Every imported function of a well-formed file has
its own thunk in the file, so the number of functions cannot exceed the number
of thunks that fit in it. Each library gets what is left of the budget, a
warning is logged when it runs out, and rewrapping a single entry does not
count its functions twice. The code is in ImportBaseDirWrapper, so the
delay-load directory uses it as well.

Behaviour change: when several libraries share a thunk array, the functions
beyond the budget are not listed.
@hasherezade
hasherezade merged commit be665ad into hasherezade:master Nov 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants