Skip to content

Veracode: Use internal alignment logarithm in free_aligned_sized - #3053

Merged
spredolac merged 1 commit into
jemalloc:devfrom
spredolac:veracode_3050
Oct 29, 2026
Merged

spredolac merged 1 commit into
jemalloc:devfrom
spredolac:veracode_3050

Conversation

@spredolac

Copy link
Copy Markdown
Contributor

For valid nonzero power-of-two alignments, the existing lg_floor helper produces the same flags as MALLOCX_ALIGN. Use it at this internal call site to avoid expanding the public macro arithmetic reported in issue #3050.

Similar to before I cannot test this as I do not have veracode license. It is speculative based on screenshot. As an added benefit, the tested Clang 21 ThinLTO builds generate fewer instructions for this path on both x86-64 and AArch64.

@spredolac

Copy link
Copy Markdown
Contributor Author
Before/after assembly and benchmark results

Additional code-generation and runtime validation

Compared the final linked libraries before and after this change using
GCC 11.5 (-O3) and Clang 21 (-O3 -flto=thin) on native x86-64 and
AArch64 hosts.

Selected alignment-decoding instructions are shown below. Labels are
normalized; unchanged allocator work and argument forwarding are omitted.

GCC, x86-64

Before, the low-word path uses a 32-bit scan, with a separate path for
the upper 32 bits:

cmpq    $0x7ffffffe, %rsi
ja      .Lhigh_word
bsfl    %esi, %eax
movl    $-1, %edx
cmovnel %eax, %edx

After, the compiler can handle alignment 1 without scanning. The other
path uses a full-width scan:

movq    %rsi, %rax
# ... fast-path selection omitted ...
bsrq    %rax, %rdx

Clang ThinLTO, x86-64

Before, the low-word path is:

cmpq    $0x7ffffffe, %rsi
ja      .Lhigh_word
testq   %rsi, %rsi
je      .Lzero
tzcntl  %esi, %eax
testl   %eax, %eax
jne     .Ldefault

After:

bsrq    %rsi, %rax
xorq    $0x3f, %rax
cmpq    $0x3f, %rax
jne     .Lconvert_flags
# ... unchanged fast-path work omitted ...
.Lconvert_flags:
xorl    $0x3f, %eax

On AArch64, the split-word rbit/clz calculation becomes a full-width
clz with the corresponding flag conversion.

Measurements

The benchmark preallocates objects using mallocx, then measures a tight
loop of free_aligned_sized(p, 64, 64). Allocation is outside the measured
region. Runs are CPU-pinned, with Linux perf user-mode instruction/cycle
counters. Loop and timer overhead are included identically before/after.

Values below are before → after medians:

Compiler / architecture Function bytes Retired instructions/op ns/op
GCC, x86-64 210 → 180 207.22 → 206.22 16.18 → 15.16
GCC, AArch64 196 → 144 219.80 → 214.80 12.35 → 12.02
Clang ThinLTO, x86-64 230 → 184 211.65 → 209.65 15.63 → 16.31
Clang ThinLTO, AArch64 216 → 164 204.47 → 200.47 12.88 → 12.72

The GCC and AArch64 measurements use seven alternating before/after
repetitions of 2,097,152 operations. The x86-64 Clang ThinLTO row uses
the longer confirmation run: 21 repetitions of 16,777,216 operations.

The change produces smaller code and fewer retired instructions in these
tested builds. It is not a universal runtime improvement: the x86-64
Clang ThinLTO case showed approximately a 4% slowdown in this microbenchmark.

Native debug/profiling and ThinLTO tests passed on both architectures.
Veracode was not run, so clearance of the reported finding remains
unverified.

Comment thread test/unit/bit_util.c Outdated
TEST_BEGIN(test_mallocx_alignment_flags) {
for (unsigned lg_align = 0; lg_align < sizeof(size_t) * 8; lg_align++) {
size_t alignment = ZU(1) << lg_align;
expect_u_eq(lg_floor(alignment), lg_align,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test aims at proving the old flag calculation is the same as the new one? That is useful for validating the change, but it couples the suite to both implementations without really exercising free_aligned_sized. If in the future we further change how the flag is processed, it will not automatically reflect here anyway.

I would suggest expand test/integration/free_size.c where free_aligned_sized is really called and exercise several alignment/size combinations there.

For valid nonzero power-of-two alignments, the existing lg_floor helper
produces the same flags as MALLOCX_ALIGN. Use it at this internal call
site to avoid expanding the public macro arithmetic reported in issue
jemalloc#3050.
@spredolac
spredolac merged commit dc1ed73 into jemalloc:dev Oct 29, 2026
205 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Veracode Flaw (static): Numeric Errors in 5.4.0

2 participants