Skip to content

Commit fe1929b

Browse files
committed
feat: enforce minimal required alignment for pool allocations
Refuse to compile if the NGX_ALIGNMENT value detected or specified while building nginx is insufficient.
1 parent edca9e2 commit fe1929b

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

nginx-sys/build/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ fn generate_binding(nginx: &NginxSource) {
216216
// Bindings will not compile on Linux without block listing this item
217217
// It is worth investigating why this is
218218
.blocklist_item("IPPORT_RESERVED")
219+
// will be restored later in build.rs
220+
.blocklist_item("NGX_ALIGNMENT")
219221
.generate_cstr(true)
220222
// The input header we would like to generate bindings for.
221223
.header("build/wrapper.h")

nginx-sys/build/wrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE;
2222

23+
// NGX_ALIGNMENT could be defined as a constant or an expression, with the
24+
// latter being unsupported by bindgen.
25+
const size_t NGX_RS_ALIGNMENT = NGX_ALIGNMENT;
26+
2327
// `--prefix=` results in not emitting the declaration
2428
#ifndef NGX_PREFIX
2529
#define NGX_PREFIX ""

nginx-sys/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ pub use queue::*;
3636
#[cfg(ngx_feature = "stream")]
3737
pub use stream::*;
3838

39+
/// Default alignment for pool allocations.
40+
pub const NGX_ALIGNMENT: usize = NGX_RS_ALIGNMENT;
41+
42+
// Check if the allocations made with ngx_palloc are properly aligned.
43+
// If the check fails, objects allocated from `ngx_pool` can violate Rust pointer alignment
44+
// requirements.
45+
const _: () = assert!(core::mem::align_of::<ngx_str_t>() <= NGX_ALIGNMENT);
46+
3947
impl ngx_command_t {
4048
/// Creates a new empty [`ngx_command_t`] instance.
4149
///

0 commit comments

Comments
 (0)