Byte-aligned bignum segment is written over the rest of the binary

Open
#2,443 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
56/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, erlang

Research direction

Start in opcodesswitch.h at bs_create_bin and bs_put_integer, then inspect jit.c at jit_bitstring_insert_integer and the intn_to_integer_bytes helper. Compare the byte count passed to the helper with the integer field size, using the BEAM examples in the issue to check truncation and whether following segments remain intact. The issue is done when interpreter and JIT output matches BEAM for these cases.

Written by the indexing model from the issue text.

Description

Found while reviewing #2357; pre-existing (same code on release-0.7 at 18b35870). The interpreter and the x86_64 JIT behave the same, BEAM differs.

A byte-aligned integer segment whose value is outside the int64 range (on 64-bit builds any value >= 2^63, such as a 64-bit hash) is written with intn_to_integer_bytes(..., dst, avail) in bs_create_bin and bs_put_integer (opcodesswitch.h) and in jit_bitstring_insert_integer (jit.c), where avail is the number of bytes left in the binary, not the size of the field. The helper writes and sign-fills avail bytes and, for big-endian, reverses that whole block:

V = id(16#D2EDBB11013FBAE3), Z = id(0), X = id(5),
<<V:64, X:8>>               %% BEAM <<210,237,187,17,1,63,186,227,5>>    AtomVM <<0,210,237,187,17,1,63,186,5>> (value shifted by one byte)
<<V:24>>                    %% BEAM <<63,186,227>>                        AtomVM badarg (the 8-byte bignum does not fit the 3-byte binary)
<<V:16, Z:16>>              %% BEAM <<186,227,0,0>>                       AtomVM badarg
<<V:24/little, Z:48, X:3>>  %% BEAM <<227,186,63,0,0,0,0,0,0,5:3>>        AtomVM <<227,186,63,1,17,187,237,210,0,5:3>> (high bytes leak into the next segment)

<<V:64>> alone is fine because the field ends the binary. The field size in bytes should be passed instead of avail, truncating the value as BEAM does. Non-byte-aligned fields are not affected: #2357 writes those through bitstring_bigint_to_field64 for values up to 64 bits.

Dominant language
Erlang
Stars
2.3k
Forks
171
Avg merge
4d 11h
Merged PRs (30d)
20

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from atomvm/AtomVM

All issues in atomvm/AtomVM

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.