Skip to content

Properly initialise some structures #1591

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 11, 2025
Merged

Properly initialise some structures #1591

merged 2 commits into from
Apr 11, 2025

Conversation

ac000
Copy link
Member

@ac000 ac000 commented Apr 9, 2025

While tracking down some memory leaks valgrind(1) was highlighting numerous issues. Including the following

==166470== Syscall param sendmsg(msg.msg_iov[0]) points to uninitialised byte(s)
==166470==    at 0x4AE6514: sendmsg (sendmsg.c:28)
==166470==    by 0x42D86C: nxt_sendmsg (nxt_socket_msg.c:32)
==166470==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6013)
==166470==    by 0x4FEB6E2: nxt_unit_ready (nxt_unit.c:963)
==166470==    by 0x4FEB6E2: nxt_unit_init (nxt_unit.c:557)
==166470==    by 0x4FEEC56: nxt_php_start (nxt_php_sapi.c:507)
==166470==    by 0x426BA0: nxt_app_setup (nxt_application.c:1029)
==166470==    by 0x403153: nxt_process_do_start (nxt_process.c:718)
==166470==    by 0x4042A3: nxt_process_whoami_ok (nxt_process.c:846)
==166470==    by 0x407A28: nxt_port_rpc_handler (nxt_port_rpc.c:347)
==166470==    by 0x407E42: nxt_port_handler (nxt_port.c:184)
==166470==    by 0x40501B: nxt_port_read_msg_process (nxt_port_socket.c:1271)
==166470==    by 0x4055B3: nxt_port_read_handler (nxt_port_socket.c:778)
==166470==  Address 0x1ffefffc7f is on thread 1's stack
==166470==  in frame #3, created by nxt_unit_init (nxt_unit.c:428)
==166470==  Uninitialised value was created by a stack allocation
==166470==    at 0x4FEABFE: nxt_unit_init (nxt_unit.c:436)

==166470== Syscall param sendmsg(msg.msg_control) points to uninitialised byte(s)
==166470==    at 0x4AE6514: sendmsg (sendmsg.c:28)
==166470==    by 0x42D86C: nxt_sendmsg (nxt_socket_msg.c:32)
==166470==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6013)
==166470==    by 0x4FEB6E2: nxt_unit_ready (nxt_unit.c:963)
==166470==    by 0x4FEB6E2: nxt_unit_init (nxt_unit.c:557)
==166470==    by 0x4FEEC56: nxt_php_start (nxt_php_sapi.c:507)
==166470==    by 0x426BA0: nxt_app_setup (nxt_application.c:1029)
==166470==    by 0x403153: nxt_process_do_start (nxt_process.c:718)
==166470==    by 0x4042A3: nxt_process_whoami_ok (nxt_process.c:846)
==166470==    by 0x407A28: nxt_port_rpc_handler (nxt_port_rpc.c:347)
==166470==    by 0x407E42: nxt_port_handler (nxt_port.c:184)
==166470==    by 0x40501B: nxt_port_read_msg_process (nxt_port_socket.c:1271)
==166470==    by 0x4055B3: nxt_port_read_handler (nxt_port_socket.c:778)
==166470==  Address 0x1ffefffc9c is on thread 1's stack
==166470==  in frame #3, created by nxt_unit_init (nxt_unit.c:428)
==166470==  Uninitialised value was created by a stack allocation
==166470==    at 0x4FEABFE: nxt_unit_init (nxt_unit.c:436)

==166690== Syscall param sendmsg(msg.msg_iov[0]) points to uninitialised byte(s)
==166690==    at 0x4AE6514: sendmsg (sendmsg.c:28)
==166690==    by 0x42D871: nxt_sendmsg (nxt_socket_msg.c:32)
==166690==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6009)
==166690==    by 0x4FE69C8: nxt_unit_port_send (nxt_unit.c:5939)
==166690==    by 0x4FE8C77: nxt_unit_request_done (nxt_unit.c:3309)
==166690==    by 0x4FEE13B: nxt_php_execute (nxt_php_sapi.c:1257)
==166690==    by 0x4FEE2F1: nxt_php_dynamic_request (nxt_php_sapi.c:1128)
==166690==    by 0x4FEE79E: nxt_php_request_handler (nxt_php_sapi.c:1023)
==166690==    by 0x4FE92AD: nxt_unit_process_ready_req (nxt_unit.c:4846)
==166690==    by 0x4FED1B4: nxt_unit_run_once_impl (nxt_unit.c:4605)
==166690==    by 0x4FED3AE: nxt_unit_run (nxt_unit.c:4548)
==166690==    by 0x4FEEC2A: nxt_php_start (nxt_php_sapi.c:514)
==166690==  Address 0x1ffeffea5f is on thread 1's stack
==166690==  in frame #3, created by nxt_unit_port_send (nxt_unit.c:5907)
==166690==  Uninitialised value was created by a stack allocation
==166690==    at 0x4FE8C05: nxt_unit_request_done (nxt_unit.c:3255)

These are down to structures being passed around with uninitialised members.

(Note: There are some more similar cases but these commits resolve the above while being obviously correct and good in their own right).

The first commit Fully initialise nxt_port_msg_t msg structures resolves the first and last of the above alerts, by fully empty-initialising the msg structure, this then negates the need to further initialise some members to 0.

The second commit Fully initialise the oob struct in nxt_socket_msg_oob_init() resolves the middle of the above alerts, by fully empty-initialising the oob structure.

The following changes since commit 3b18ffe09370573f81220fda5e924124fcf8f0df:

  tests: Fix TLS tests with Python 3.13 (2025-03-27 23:39:32 +0000)

are available in the Git repository at:

  [email protected]:ac000/unit.git struct-init

for you to fetch changes up to 0c59a033b6fa5c02c98128cef9492264d489a32f:

  Fully initialise the oob struct in nxt_socket_msg_oob_init() (2025-04-09 17:46:14 +0100)

----------------------------------------------------------------
Andrew Clayton (2):
      Fully initialise nxt_port_msg_t msg structures
      Fully initialise the oob struct in nxt_socket_msg_oob_init()

 src/nxt_socket_msg.h |  2 ++
 src/nxt_unit.c       | 12 ++----------
 2 files changed, 4 insertions(+), 10 deletions(-)

@ac000 ac000 changed the title Properly intitalise some structures Properly initialise some structures Apr 9, 2025
@ac000 ac000 marked this pull request as ready for review April 9, 2025 16:59
@ac000 ac000 requested a review from hongzhidao April 9, 2025 16:59
ac000 added 2 commits April 11, 2025 17:56
valgrind(1) was producing the following alerts

  ==166470== Syscall param sendmsg(msg.msg_iov[0]) points to uninitialised byte(s)
  ==166470==    at 0x4AE6514: sendmsg (sendmsg.c:28)
  ==166470==    by 0x42D86C: nxt_sendmsg (nxt_socket_msg.c:32)
  ==166470==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6013)
  ==166470==    by 0x4FEB6E2: nxt_unit_ready (nxt_unit.c:963)
  ==166470==    by 0x4FEB6E2: nxt_unit_init (nxt_unit.c:557)
  ==166470==    by 0x4FEEC56: nxt_php_start (nxt_php_sapi.c:507)
  ==166470==    by 0x426BA0: nxt_app_setup (nxt_application.c:1029)
  ==166470==    by 0x403153: nxt_process_do_start (nxt_process.c:718)
  ==166470==    by 0x4042A3: nxt_process_whoami_ok (nxt_process.c:846)
  ==166470==    by 0x407A28: nxt_port_rpc_handler (nxt_port_rpc.c:347)
  ==166470==    by 0x407E42: nxt_port_handler (nxt_port.c:184)
  ==166470==    by 0x40501B: nxt_port_read_msg_process (nxt_port_socket.c:1271)
  ==166470==    by 0x4055B3: nxt_port_read_handler (nxt_port_socket.c:778)
  ==166470==  Address 0x1ffefffc7f is on thread 1's stack
  ==166470==  in frame #3, created by nxt_unit_init (nxt_unit.c:428)
  ==166470==  Uninitialised value was created by a stack allocation
  ==166470==    at 0x4FEABFE: nxt_unit_init (nxt_unit.c:436)

  ==166690== Syscall param sendmsg(msg.msg_iov[0]) points to uninitialised byte(s)
  ==166690==    at 0x4AE6514: sendmsg (sendmsg.c:28)
  ==166690==    by 0x42D871: nxt_sendmsg (nxt_socket_msg.c:32)
  ==166690==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6009)
  ==166690==    by 0x4FE69C8: nxt_unit_port_send (nxt_unit.c:5939)
  ==166690==    by 0x4FE8C77: nxt_unit_request_done (nxt_unit.c:3309)
  ==166690==    by 0x4FEE13B: nxt_php_execute (nxt_php_sapi.c:1257)
  ==166690==    by 0x4FEE2F1: nxt_php_dynamic_request (nxt_php_sapi.c:1128)
  ==166690==    by 0x4FEE79E: nxt_php_request_handler (nxt_php_sapi.c:1023)
  ==166690==    by 0x4FE92AD: nxt_unit_process_ready_req (nxt_unit.c:4846)
  ==166690==    by 0x4FED1B4: nxt_unit_run_once_impl (nxt_unit.c:4605)
  ==166690==    by 0x4FED3AE: nxt_unit_run (nxt_unit.c:4548)
  ==166690==    by 0x4FEEC2A: nxt_php_start (nxt_php_sapi.c:514)
  ==166690==  Address 0x1ffeffea5f is on thread 1's stack
  ==166690==  in frame #3, created by nxt_unit_port_send (nxt_unit.c:5907)
  ==166690==  Uninitialised value was created by a stack allocation
  ==166690==    at 0x4FE8C05: nxt_unit_request_done (nxt_unit.c:3255)

These were due to the nxt_port_msg_t msg struct in nxt_unit_ready() and
nxt_unit_request_done() not being fully initialised.

Whether or not this is an actual problem an obviously correct thing to
do is to fully empty-initialise the structure and then we don't need to
explicitly set any members to 0 afterwards providing a nice cleanup as
well.

Link: <https://en.cppreference.com/w/c/language/initialization#Empty_initialization>
Signed-off-by: Andrew Clayton <[email protected]>
valgrind(1) was producing the following alert

  ==166470== Syscall param sendmsg(msg.msg_control) points to uninitialised byte(s)
  ==166470==    at 0x4AE6514: sendmsg (sendmsg.c:28)
  ==166470==    by 0x42D86C: nxt_sendmsg (nxt_socket_msg.c:32)
  ==166470==    by 0x4FE6695: nxt_unit_sendmsg (nxt_unit.c:6013)
  ==166470==    by 0x4FEB6E2: nxt_unit_ready (nxt_unit.c:963)
  ==166470==    by 0x4FEB6E2: nxt_unit_init (nxt_unit.c:557)
  ==166470==    by 0x4FEEC56: nxt_php_start (nxt_php_sapi.c:507)
  ==166470==    by 0x426BA0: nxt_app_setup (nxt_application.c:1029)
  ==166470==    by 0x403153: nxt_process_do_start (nxt_process.c:718)
  ==166470==    by 0x4042A3: nxt_process_whoami_ok (nxt_process.c:846)
  ==166470==    by 0x407A28: nxt_port_rpc_handler (nxt_port_rpc.c:347)
  ==166470==    by 0x407E42: nxt_port_handler (nxt_port.c:184)
  ==166470==    by 0x40501B: nxt_port_read_msg_process (nxt_port_socket.c:1271)
  ==166470==    by 0x4055B3: nxt_port_read_handler (nxt_port_socket.c:778)
  ==166470==  Address 0x1ffefffc9c is on thread 1's stack
  ==166470==  in frame #3, created by nxt_unit_init (nxt_unit.c:428)
  ==166470==  Uninitialised value was created by a stack allocation
  ==166470==    at 0x4FEABFE: nxt_unit_init (nxt_unit.c:436)

This was due to the nxt_send_oob_t oob structure not being fully
initialised.

Given the name and intention of this function lets *fully*
empty-initialise this structure.

Link: <https://en.cppreference.com/w/c/language/initialization#Empty_initialization>
Signed-off-by: Andrew Clayton <[email protected]>
@ac000
Copy link
Member Author

ac000 commented Apr 11, 2025

Rebased with master

$ git range-diff 0c59a033...89149c06
-:  -------- > 1:  f2e97bf3 rust: Update rust crates
-:  -------- > 2:  0cbdcb15 pkg/contrib: Bump wasmtime to 31.0.0
1:  1f317c17 = 3:  326f42a5 Fully initialise nxt_port_msg_t msg structures
2:  0c59a033 = 4:  89149c06 Fully initialise the oob struct in nxt_socket_msg_oob_init()

@ac000 ac000 merged commit 89149c0 into nginx:master Apr 11, 2025
25 checks passed
@ac000
Copy link
Member Author

ac000 commented Apr 11, 2025

Thanks Zhidao!

@ac000 ac000 deleted the struct-init branch April 11, 2025 17:07
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.

2 participants