isValidUtf8 faults with SIGBUS on 32-bit ARM: read_uint64 takes a uint64_t pointer

Open Beginner friendly
#730 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
android, c, haskell

Research direction

Start in cbits/is-valid-utf8.c and inspect read_uint64 plus the calls at the locations described in the issue. Reproduce the failure with the reported ARMv7 setup if available, then verify the pointer type and call arguments no longer imply alignment. Done when UTF-8 validation no longer faults on an unaligned pointer and the relevant tests pass.

Written by the indexing model from the issue text.

Description

cbits/is-valid-utf8.c reads 8-byte blocks with read_uint64(const uint64_t *p), and the callers pass (uint64_t const *)ptr with ptr at any byte offset (lines 64 and 109-117 on master). The pointer type tells the compiler the address is 8-byte aligned, so on ARMv7 clang compiles the memcpy in read_uint64 to ldrd, which faults on an address that is not 4-byte aligned. The fix for #475 replaced the plain loads with memcpy but kept the pointer type.

Observed with GHC 9.14.1 (bytestring 0.12.2.0) cross-compiling for armv7a Android: Template Haskell under qemu stopped with qemu: uncaught target signal 7 (Bus error) in text 2.1.3's UTF-8 validation, which calls this function, with a pointer one byte past an ASCII byte.

Fix: read_uint64 takes uint8_t const *, and the calls pass ptr, ptr + 8, ptr + 16, ptr + 24. The patch we apply in our haskell.nix fork: https://github.com/simplex-chat/haskell.nix/pull/4

Dominant language
Haskell
Stars
302
Forks
146
Avg merge
7d 21h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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 haskell/bytestring

All issues in haskell/bytestring

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.