UBSAN: left shifts into the sign bit in unaligned-read helpers

Open
#2,445 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
73/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

Research direction

Start with the listed shift expressions in src/libAtomVM/bitstring.h, src/libAtomVM/utils.h, src/libAtomVM/opcodesswitch.h, and src/libAtomVM/inet.c; read how each helper builds its integer value. Run the test suite with -fsanitize=undefined to reproduce the reports, then rerun it to confirm they are gone after the shifts are made defined. Done means all listed cases pass without UBSAN reports.

Written by the indexing model from the issue text.

Description

Running the test suite with -fsanitize=undefined reports "left shift of N by 24 places cannot be represented in type 'int'" (and by 56 places into 'long') from:

  • READ_32LE_UNALIGNED in src/libAtomVM/bitstring.h:45 and READ_32_UNALIGNED in src/libAtomVM/utils.h:57, :85 and :138: ((uint8_t *) ptr)[3] << 24 promotes the byte to int before shifting, so any byte >= 0x80 overflows.
  • large_integer_to_term in src/libAtomVM/opcodesswitch.h:1339 and :1378: ((int32_t) compact_term[0]) << 24 and ((int64_t) compact_term[0]) << 56.
  • src/libAtomVM/inet.c:66: (term_to_int32(...) & 0xFF) << 24.

The generated code does what is intended today, but it is undefined behaviour and a compiler may legitimately miscompile it. Casting each byte to uint32_t/uint64_t before the shift (and converting to the signed type afterwards) fixes all of them.

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.