Skip to content

Win32: couple of platform detection fixes #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2025

Conversation

bavshin-f5
Copy link
Member

@bavshin-f5 bavshin-f5 commented Jan 16, 2025

See commit messages.

The NGX_ALIGNMENT one is actually necessary for the Rust bindings to work on any 64-bit Windows; there are optimizations in Rust that may require pointers to be properly aligned, and thus the standard library enforces this with assertions.

The ARM64 stems from the same source, while debugging ^ I found that we pass incorrect configuration template to the OpenSSL; VC-WIN64-ARM is what Configure would pick without our meddling. Also, having an accurate NGX_MACHINE value helps when I needed to guess a target triple.

For reference, cl.exe version strings:

  • Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27051 for ARM64 (2017, first available ARM64 toolchain)
  • Microsoft (R) C/C++ Optimizing Compiler Version 19.42.34435 for ARM64 (2022)

The change has been verified with OpenSSL 3.0-3.4 in a Win64 cross-compile environment and on a real Windows 11 ARM64 VM.

Fixes #446

@bavshin-f5 bavshin-f5 requested review from arut and pluknet March 27, 2025 16:13
@Maryna-f5 Maryna-f5 modified the milestone: nginx-1.27.5 Apr 1, 2025
@pluknet
Copy link
Contributor

pluknet commented Apr 18, 2025

Core: improved NGX_ALIGNMENT detection on some x86_64 platforms.

Previously, we detected the default pool alignment as sizeof(unsigned),

sizeof(unsigned long) ?

expecting that it would result in a platform word size. Certain 64-bit
platforms prove this assumption wrong by keeping the long type 32-bit,
which is fully compliant with the C standard.

Use a real pointer-sized type to set the default NGX_ALIGNMENT.

Is there a support claim to use a pointer-sized value?

BTW, personally, I find the "platform word" guidance provided by comment
in the code rather confusing. Probably its needs to be removed.

NGX_ALIGNMENT is used to align ngx_palloc() sub-allocations from pool memory
backed by ngx_memalign() that falls back to malloc() on win32. I assume we need
here to align them to malloc() alignment as well, as per
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/malloc
which is 8 bytes, and 16 bytes on 64-bit platforms.
(As far as malloc() chosen alignment is chosen on purpose.)

Effectively, this is what you do here by operating a pointer-sized value,
which matches ILP32 / LP64 / LLP64.

As we override the detection in auto/os/conf for all the machine types
except x86, and Unix-like x86 systems prefer 64-bit long, the impact

Unix-like 64-bit systems ?

of this change should be limited to Win64.

The impact here appears that ngx_palloc() alignment might
result in suboptimal misaligned access. Probably this needs
to be spelled out in the commit log, this is up to you.

Copy link
Contributor

@pluknet pluknet left a comment

Choose a reason for hiding this comment

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

Approved with comments.

Previously, the default pool alignment used sizeof(unsigned long), with
the expectation that this would match to a platform word size.  Certain
64-bit platforms prove this assumption wrong by keeping the 32-bit long
type, which is fully compliant with the C standard.

This introduces a possibility of suboptimal misaligned access to the
data allocated with ngx_palloc() on the affected platforms, which is
addressed here by changing the default NGX_ALIGNMENT to a pointer size.

As we override the detection in auto/os/conf for all the machine types
except x86, and Unix-like 64-bit systems prefer the 64-bit long, the
impact of the change should be limited to Win64 x64.
This extends the target selection implemented in dad6ec3 to support
Windows ARM64 platforms.  OpenSSL support for VC-WIN64-ARM target first
appeared in 1.1.1 and is present in all currently supported (3.x)
branches.

As a side effect, ARM64 Windows builds will get 16-byte alignment along
with the rest of non-x86 platforms.  This is safe, as malloc on 64-bit
Windows guarantees the fundamental alignment of allocations, 16 bytes.
@pluknet
Copy link
Contributor

pluknet commented Apr 18, 2025

Looks good.

@Maryna-f5 Maryna-f5 added this to the nginx-1.27.6 milestone Apr 18, 2025
@bavshin-f5 bavshin-f5 merged commit 020b1db into nginx:master Apr 18, 2025
1 check passed
@bavshin-f5 bavshin-f5 deleted the win32-alignment branch April 18, 2025 19:57
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.

NGX_ALIGNMENT on Windows x64 seems insufficient
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy