Skip to content

Commit dcdbde6

Browse files
happiclaude
andcommitted
Fix Type chunk parsing: not an external term format
The Type chunk (OTP 25+) holds JIT type info as fixed 16-byte records prefixed by a version and count field. The book incorrectly showed binary_to_term which fails at runtime. Updated the binary format description and parsing code to match beamfile.erl. Fixes #227 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 633d498 commit dcdbde6

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

chapters/beam_modules.asciidoc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,19 +485,27 @@ The format for the new chunks is:
485485
TypeChunk = <<
486486
ChunkName:4/unit:8 = "Type",
487487
ChunkSize:32/big,
488-
TypeInfo:ChunkSize/binary,
488+
Version:32/big,
489+
Count:32/big,
490+
TypeData:(Count * 16)/binary,
489491
Padding4:0..3/unit:8
490492
>>
491493
----
492494

495+
The Type chunk holds simplified type information used by the JIT compiler
496+
for optimization. Each type entry is a fixed 16-byte record. The data is
497+
not in Erlang external term format, so it cannot be decoded with
498+
`binary_to_term`. See `beam_types.erl` in the compiler for the encoding.
499+
493500
Parsing:
494501

495-
```erlang
496-
parse_chunks([{"Type", Size, Chunk} | Rest], Acc) ->
497-
<<TypeInfo:Size/binary, _Pad/binary>> = Chunk,
498-
Types = binary_to_term(TypeInfo),
499-
parse_chunks(Rest, [{types, Types}|Acc]);
500-
```
502+
[source,erlang]
503+
----
504+
parse_chunks([{"Type", _Size, Chunk} | Rest], Acc) ->
505+
<<_Version:32/big, Count:32/big, TypeData/binary>> = Chunk,
506+
Types = parse_types(Count, TypeData, []),
507+
parse_chunks(Rest, [{type_info, Types}|Acc]);
508+
----
501509

502510
- **UTF-8 Atom Chunk (`AtU8`)**: Same format as Atom chunk, used when UTF-8 encoding is required:
503511

0 commit comments

Comments
 (0)