GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: `npy_memchr` has misaligned memory access · Issue #21116 · numpy/numpy

BUG: npy_memchr has misaligned memory access #21116

New issue
Copy link
New issue
Copy link
Closed
#21467
Closed
BUG: npy_memchr has misaligned memory access#21116
#21467
Copy link
Labels
00 - Bug

Description

@saran-t
saran-t
opened on Feb 24, 2022
Issue body actions

Describe the issue:

The function npy_memchr (in multiarray/common.h) has an optimisation for the case !NPY_ALIGNMENT_REQUIRED && needle == 0 && stride == 1 where it start by find the first nonzero 4-byte block before iterating within the block. However, the coarse-grained loop begins at haystack itself, and since the stride is 1 there's no expectation that this pointer would have any particular alignment.

While this is benign on x86_64, the misaligned access is nevertheless picked up by UBsan as it is undefined behaviour according to the C standard. npy_memchr should first scan each byte until it reaches the first 4-byte aligned block before entering the coarse-grained loop.

How to reproduce:

Build NumPy with UBsan and run a test suite containing a sufficiently large number of np.testing.assert_ calls. Since func_assert_same_pos constructs a boolean mask that is used to index into the arrays under test, with enough calls there's a decent chance that it will end up creating an unaligned boolean array at least once.

Error message:

third_party/py/numpy/core/src/multiarray/common.h:277:35: runtime error: load of misaligned address 0x... for type 'unsigned int', which requires 4 byte alignment
0x615000673205: note: pointer points here
 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  
             ^ 
    #0 0x... in npy_memchr third_party/py/numpy/core/src/multiarray/common.h:277:35
    #1 0x... in array_boolean_subscript third_party/py/numpy/core/src/multiarray/mapping.c:1113:30
    #2 0x... in array_subscript third_party/py/numpy/core/src/multiarray/mapping.c:1572:30
    #3 0x... in PyObject_GetItem third_party/python_runtime/v3_7/Objects/abstract.c:182:26
...
SUMMARY: UndefinedBehaviorSanitizer: misaligned-pointer-use third_party/py/numpy/core/src/multiarray/common.h:277:35

NumPy/Python version information:

Bug exists at HEAD.

Activity

  1. saran-t
    added
    00 - Bug
    on Feb 24, 2022
  2. saran-t
    added a commit that references this issue on Feb 24, 2022

    BUG: Fix unaligned memory access in `npy_memchr`.

    This issue will close once commit 01f1ac9 is merged into the 'main' branch.
    01f1ac9
  3. seberg
    added 2 commits that reference this issue on May 6, 2022

    MAINT: Mark `npy_memchr` with `no_sanitize("alignment")` on clang

    This issue will close once commit bc1efb3 is merged into the 'main' branch.
    bc1efb3

    MAINT: Mark `npy_memchr` with `no_sanitize("alignment")` on clang

    This issue will close once commit 3257ab0 is merged into the 'main' branch.
    3257ab0
  4. charris
    closed this as completedin #21467on May 9, 2022
  5. charris
    added a commit that references this issue on May 9, 2022

    MAINT: Mark `npy_memchr` with `no_sanitize("alignment")` on clang

    This issue will close once commit a8f4e78 is merged into the 'main' branch.
    a8f4e78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.