public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Jens Axboe <[email protected]>
Cc: GNU/Weeb Mailing List <[email protected]>,
	io-uring Mailing List <[email protected]>,
	Ammar Faizi <[email protected]>
Subject: [PATCH liburing 0/3] Fix undefined behavior, acessing dead object
Date: Fri,  7 Jan 2022 20:02:15 +0700	[thread overview]
Message-ID: <[email protected]> (raw)

This series fixes undefined behavior caused by accessing local
variables that have been out of their scope.

FWIW, compile the following code with gcc (Ubuntu 11.2.0-7ubuntu2) 11.2.0:
```
#include <stdio.h>

int main(void)
{
	int *pa, *pb;

	{
		int a;
		pa = &a;
	}

	{
		int b;
		pb = &b;
	}

	*pa = 100;
	*pb = 200;

	printf("(pa == pb) = %d\n", pa == pb);
	printf("*pa == %d; *pb == %d\n", *pa, *pb);
	return 0;
}
```

produces the following output:

```
  ammarfaizi2@integral2:/tmp$ gcc q.c -o q
  ammarfaizi2@integral2:/tmp$ ./q
  (pa == pb) = 1
  *pa == 200; *pb == 200
  ammarfaizi2@integral2:/tmp$
  ammarfaizi2@integral2:/tmp$ gcc -O3 q.c -o q
  ammarfaizi2@integral2:/tmp$ ./q
  (pa == pb) = 0
  *pa == 100; *pb == 200
  ammarfaizi2@integral2:/tmp$
```

Note that the `int a` and `int b` lifetime have ended, but we still
hold the references to them dereference them.

Also the result differs for the different optimization levels.
That's to say, there is no guarantee due to UB. Compiler is free
to reuse "out of scope variable"'s storage.

The same happens with test/socket-rw{,-eagain,-offset}.c.

Signed-off-by: Ammar Faizi <[email protected]>
---
Ammar Faizi (3):
  test/socket-rw-eagain: Fix UB, accessing dead object
  test/socket-rw: Fix UB, accessing dead object
  test/socket-rw-offset: Fix UB, accessing dead object

 test/socket-rw-eagain.c | 17 +++++++----------
 test/socket-rw-offset.c | 17 +++++++----------
 test/socket-rw.c        | 17 +++++++----------
 3 files changed, 21 insertions(+), 30 deletions(-)


base-commit: 918d8061ffdfdf253806a1e8e141c71644e678bd
-- 
2.32.0

-- 
GWML mailing list
[email protected]
https://gwml.gnuweeb.org/listinfo/gwml

             reply	other threads:[~2022-01-07 13:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-07 13:02 Ammar Faizi [this message]
2022-01-07 13:02 ` [PATCH liburing 1/3] test/socket-rw-eagain: Fix UB, accessing dead object Ammar Faizi
2022-01-07 13:02 ` [PATCH liburing 2/3] test/socket-rw: " Ammar Faizi
2022-01-07 13:02 ` [PATCH liburing 3/3] test/socket-rw-offset: " Ammar Faizi
2022-01-09 16:47 ` [PATCH liburing 0/3] Fix undefined behavior, acessing " Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox