Skip to content

Fix prune of callback modules from AVM libs#60

Open
petermm wants to merge 1 commit intoatomvm:masterfrom
petermm:fix-prune-callbacks
Open

Fix prune of callback modules from AVM libs#60
petermm wants to merge 1 commit intoatomvm:masterfrom
petermm:fix-prune-callbacks

Conversation

@petermm
Copy link
Contributor

@petermm petermm commented Feb 18, 2026

Fixes: #58

https://ampcode.com/threads/T-019c6de0-ba10-7688-bedb-4ce12d207c0a

When using -p (prune) with modules sourced from a .avm library, modules referenced only in literals (e.g., supervisor callback modules in child specs) were incorrectly pruned.

The root cause was that parse_beam (the AVM parsing path) did not extract uncompressed_literals, while the .beam parsing path (parse_file(beam, ...)) did. This meant get_atoms/1 could not find atoms that only appear in the literals table — such as a worker module name inside a supervisor child spec map — when the containing module came from an .avm file.

The fix adds literal extraction to the AVM parsing path, making it consistent with the .beam path.

Test modules added (test/prune_sup/):

start_mod.erl — entrypoint that calls my_sup
my_sup.erl — supervisor-like module with my_worker only in a literal child spec (not in atoms chunk or imports) my_worker.erl — callback module, never directly imported

Tests added:

packbeam_create_prune_supervisor_callback_test — verifies pruning keeps literal-only references (all .beam inputs) packbeam_create_prune_supervisor_callback_from_avm_test — same scenario with modules from a .avm lib (was failing before the fix)

StrippedData = strip_padding(Data),
{ok, {Module, ChunkRefs}} = beam_lib:chunks(StrippedData, [imports, exports, atoms]),
[{module, Module}, {chunk_refs, ChunkRefs}, {data, StrippedData} | Accum];
{ok, _, AllChunks} = beam_lib:all_chunks(StrippedData),
Copy link
Collaborator

Choose a reason for hiding this comment

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

We do chunks and then all_chunks?

@petermm petermm force-pushed the fix-prune-callbacks branch from e314a31 to 4ca9f8a Compare February 18, 2026 13:08
When using prune with modules sourced from a .avm library, modules
referenced only in literals (e.g., supervisor callback modules in
child specs) were incorrectly pruned.

The root cause was that parse_beam (the AVM parsing path) did not
extract the literals chunk, so get_atoms/1 could not find atoms that
only appear in the literals table — such as a worker module name
inside a supervisor child spec map.

The fix extends the existing beam_lib:chunks/2 call in parse_beam to
also fetch the raw "LitT" and "LitU" chunks (compressed and
uncompressed literals, respectively), and extracts
uncompressed_literals inline. This keeps it to a single beam_lib call
and handles both OTP < 28 (compressed LitT) and OTP >= 28
(uncompressed LitT, or LitU from repackaged AVMs).

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the fix-prune-callbacks branch from 4ca9f8a to 4ce5cb6 Compare February 18, 2026 13:09
@@ -0,0 +1,14 @@
%%
%% Copyright (c) 2026 AtomVM
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have a tradition of identifying authors with names or pseudos, even for very small files.

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.

-p prunes callbacks

2 participants

Comments