ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
@ 2022-09-28 17:12 eightbitraptor (Matthew Valentine-House)
  2022-09-28 18:48 ` [ruby-core:110137] " mame (Yusuke Endoh)
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: eightbitraptor (Matthew Valentine-House) @ 2022-09-28 17:12 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been reported by eightbitraptor (Matthew Valentine-House).

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110137] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
@ 2022-09-28 18:48 ` mame (Yusuke Endoh)
  2022-09-28 23:52 ` [ruby-core:110143] " nobu (Nobuyoshi Nakada)
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mame (Yusuke Endoh) @ 2022-09-28 18:48 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by mame (Yusuke Endoh).


This seems to be [this bug in gcc](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104069). I don't see it getting fixed soon. +1 for `-Wuse-after-free=1`.

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99395

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110143] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
  2022-09-28 18:48 ` [ruby-core:110137] " mame (Yusuke Endoh)
@ 2022-09-28 23:52 ` nobu (Nobuyoshi Nakada)
  2022-09-29  1:42 ` [ruby-core:110144] " mame (Yusuke Endoh)
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2022-09-28 23:52 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by nobu (Nobuyoshi Nakada).


GCC 12 on GitHub Actions doesn’t show that warning.
https://github.com/ruby/ruby/actions/runs/3146751201/jobs/5115549022#step:13:130

I’m not sure what differs.

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99401

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110144] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
  2022-09-28 18:48 ` [ruby-core:110137] " mame (Yusuke Endoh)
  2022-09-28 23:52 ` [ruby-core:110143] " nobu (Nobuyoshi Nakada)
@ 2022-09-29  1:42 ` mame (Yusuke Endoh)
  2022-09-29 12:50 ` [ruby-core:110147] " eightbitraptor (Matthew Valentine-House)
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mame (Yusuke Endoh) @ 2022-09-29  1:42 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by mame (Yusuke Endoh).


I am not able to repro with `gcc-12 (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0` and x86_64-linux.

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99402

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110147] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
                   ` (2 preceding siblings ...)
  2022-09-29  1:42 ` [ruby-core:110144] " mame (Yusuke Endoh)
@ 2022-09-29 12:50 ` eightbitraptor (Matthew Valentine-House)
  2022-09-30  6:34 ` [ruby-core:110152] " nobu (Nobuyoshi Nakada)
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: eightbitraptor (Matthew Valentine-House) @ 2022-09-29 12:50 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by eightbitraptor (Matthew Valentine-House).


nobu (Nobuyoshi Nakada) wrote in #note-4:
> GCC 12 on GitHub Actions doesn’t show that warning.
> https://github.com/ruby/ruby/actions/runs/3146751201/jobs/5115549022#step:13:130
> 
> I’m not sure what differs.

mame (Yusuke Endoh) wrote in #note-5:
> I am not able to repro with `gcc-12 (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0` and x86_64-linux.

Apologies. I had a look into this and realised that this warning only occurs when `optflags=-O0`.

With `O0` I can successfully repro this on:

* `gcc version 12.2.1 20220819 (Red Hat 12.2.1-2) (GCC)` on `Fedora 36` - where I originally saw this warning.
* `gcc version 12.1.0 (Ubuntu 12.1.0-2ubuntu1~22.04)`
* `gcc version 12.2.0 (Ubuntu 12.2.0-3ubuntu1)` on `Ubuntu Kinetic Kudu (development branch) 22.10`

I don't know if we should justify lowering a warning level globally for a warning that only appears in unoptimized code - what do you think?


----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99404

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110152] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
                   ` (3 preceding siblings ...)
  2022-09-29 12:50 ` [ruby-core:110147] " eightbitraptor (Matthew Valentine-House)
@ 2022-09-30  6:34 ` nobu (Nobuyoshi Nakada)
  2022-09-30  8:00 ` [ruby-core:110153] " eightbitraptor (Matthew Valentine-House)
  2022-10-04  2:12 ` [ruby-core:110175] " nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2022-09-30  6:34 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by nobu (Nobuyoshi Nakada).


I see, what about this?

```diff
diff --git a/gc.c b/gc.c
index ac5ed1e7cba..a22da5482be 100644
--- a/gc.c
+++ b/gc.c
@@ -12102,10 +12102,18 @@ objspace_malloc_fixup(rb_objspace_t *objspace, void *mem, size_t size)
 # define RB_BUG_INSTEAD_OF_RB_MEMERROR 0
 #endif
 
-#define GC_MEMERROR(...) \
+#define GC_MEMERROR(...) /* NORETURN */ \
     ((RB_BUG_INSTEAD_OF_RB_MEMERROR+0) ? rb_bug("" __VA_ARGS__) : rb_memerror())
 
-#define TRY_WITH_GC(siz, expr) do {                          \
+#if RBIMPL_COMPILER_SINCE(GCC, 12, 0, 0) && !defined(__OPTIMIZE__)
+# define DISABLE_GNUC_USE_AFTRE_FREE_WARNING() \
+    RBIMPL_WARNING_IGNORED(-Wuse-after-free)
+#else
+# define DISABLE_GNUC_USE_AFTRE_FREE_WARNING()
+#endif
+
+#define TRY_WITH_GC(siz, expr) (siz, expr, (void)0)
+#define TRY_WITH_GC_WITHOUT_WARNING(siz, expr, warn2nd) do { \
         const gc_profile_record_flag gpr =                   \
             GPR_FLAG_FULL_MARK           |                   \
             GPR_FLAG_IMMEDIATE_MARK      |                   \
@@ -12114,16 +12122,19 @@ objspace_malloc_fixup(rb_objspace_t *objspace, void *mem, size_t size)
         objspace_malloc_gc_stress(objspace);                 \
                                                              \
         if (LIKELY((expr))) {                                \
-            /* Success on 1st try */                         \
+            break; /* Success on 1st try */                  \
         }                                                    \
-        else if (!garbage_collect_with_gvl(objspace, gpr)) { \
+        if (!garbage_collect_with_gvl(objspace, gpr)) {      \
             /* @shyouhei thinks this doesn't happen */       \
             GC_MEMERROR("TRY_WITH_GC: could not GC");        \
         }                                                    \
-        else if ((expr)) {                                   \
-            /* Success on 2nd try */                         \
+        RBIMPL_WARNING_PUSH();                               \
+        warn2nd;                                             \
+        if ((expr)) {                                        \
+            break; /* Success on 2nd try */                  \
         }                                                    \
-        else {                                               \
+        RBIMPL_WARNING_POP();                                \
+        {                                                    \
             GC_MEMERROR("TRY_WITH_GC: could not allocate:"   \
                         "%"PRIdSIZE" bytes for %s",          \
                         siz, # expr);                        \
@@ -12210,7 +12221,8 @@ objspace_xrealloc(rb_objspace_t *objspace, void *ptr, size_t new_size, size_t ol
 #endif
 
     old_size = objspace_malloc_size(objspace, ptr, old_size);
-    TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));
+    TRY_WITH_GC_WITHOUT_WARNING(new_size, mem = realloc(ptr, new_size),
+                                DISABLE_GNUC_USE_AFTRE_FREE_WARNING());
     new_size = objspace_malloc_size(objspace, mem, new_size);
 
 #if CALC_EXACT_MALLOC_SIZE
```

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99409

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [ruby-core:110153] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
                   ` (4 preceding siblings ...)
  2022-09-30  6:34 ` [ruby-core:110152] " nobu (Nobuyoshi Nakada)
@ 2022-09-30  8:00 ` eightbitraptor (Matthew Valentine-House)
  2022-10-04  2:12 ` [ruby-core:110175] " nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: eightbitraptor (Matthew Valentine-House) @ 2022-09-30  8:00 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by eightbitraptor (Matthew Valentine-House).


nobu (Nobuyoshi Nakada) wrote in #note-7:
> I see, what about this?
> 

I like this approach. It makes sense to just disable the warning explicitly for code that we know is correct. Thanks



----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99410

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-core:110175] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free`
  2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
                   ` (5 preceding siblings ...)
  2022-09-30  8:00 ` [ruby-core:110153] " eightbitraptor (Matthew Valentine-House)
@ 2022-10-04  2:12 ` nobu (Nobuyoshi Nakada)
  6 siblings, 0 replies; 8+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2022-10-04  2:12 UTC (permalink / raw)
  To: ruby-core

Issue #19028 has been updated by nobu (Nobuyoshi Nakada).


I found the warning is suppressed by a statement-expression.
Don't ask why 🤪
https://github.com/ruby/ruby/compare/master...nobu:ruby:bug/19028-realloc-warning

----------------------------------------
Bug #19028: GCC12 Introduces new warn flags `-Wuse-after-free`
https://bugs.ruby-lang.org/issues/19028#change-99449

* Author: eightbitraptor (Matthew Valentine-House)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
[GCC 12 introduced a new warning flag `-Wuse-after-free`](https://github.com/gcc-mirror/gcc/commit/671a283636de75f7ed638ee6b01ed2d44361b8b6) which attempts to warn about uses of pointers to dynamically allocated objects that have been rendered indeterminate by a call to a deallocation function

Details of the levels are in [the C++ Dialect Options section of the GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html).

Compiling with `-Wall` uses the default setting of `2` for this flag. Which warns on the `TRY_WITH_GC` macro defined in `gc.c`

```
gc.c: In function ‘objspace_xrealloc’:                                                                             
gc.c:12213:33: warning: pointer ‘ptr’ may be used after ‘realloc’ [-Wuse-after-free]                               
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                     
gc.c:12123:19: note: in definition of macro ‘TRY_WITH_GC’                                                          
12123 |         else if ((expr)) {                                   \                                             
      |                   ^~~~                                                                                     
In file included from ./include/ruby/defines.h:72,                                                                 
                 from ./include/ruby/ruby.h:25,                                                                    
                 from constant.h:13,                                                                               
                 from gc.c:97:                                                                                     
gc.c:12213:33: note: call to ‘realloc’ here                                                                        
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |                                 ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                        
./include/ruby/backward/2/assume.h:43:46: note: in definition of macro ‘RB_LIKELY’                                 
   43 | # define RB_LIKELY(x)   (__builtin_expect(!!(x), 1))                                                       
      |                                              ^                                                             
gc.c:12116:13: note: in expansion of macro ‘LIKELY’ 
12116 |         if (LIKELY((expr))) {                                \                                             
      |             ^~~~~~                       
gc.c:12213:5: note: in expansion of macro ‘TRY_WITH_GC’
12213 |     TRY_WITH_GC(new_size, mem = realloc(ptr, new_size));                                                   
      |     ^~~~~~~~~~~            
```

My understanding is that if `realloc` returns a null pointer then the memory requested for reallocation is guaranteed to not be touched (according to [the Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html) - thank you @nobu for bringing this to my attention).

Given that this is a new warning, my proposed solution is to lower the level down to the base level `1`. This will only warn on unconditional calls to deallocation functions or successful calls to realloc.

I've opened a PR that sets `-Wuse-after-free=1` only for GCC versions > 11. An alternative approach might be to use `#pragma GCC diagnostic` to suppress this just for GCC but I opted for what I thought was the easiest fix to start with.

[Github PR #6465](https://github.com/ruby/ruby/pull/6465)



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-10-04  2:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-28 17:12 [ruby-core:110133] [Ruby master Bug#19028] GCC12 Introduces new warn flags `-Wuse-after-free` eightbitraptor (Matthew Valentine-House)
2022-09-28 18:48 ` [ruby-core:110137] " mame (Yusuke Endoh)
2022-09-28 23:52 ` [ruby-core:110143] " nobu (Nobuyoshi Nakada)
2022-09-29  1:42 ` [ruby-core:110144] " mame (Yusuke Endoh)
2022-09-29 12:50 ` [ruby-core:110147] " eightbitraptor (Matthew Valentine-House)
2022-09-30  6:34 ` [ruby-core:110152] " nobu (Nobuyoshi Nakada)
2022-09-30  8:00 ` [ruby-core:110153] " eightbitraptor (Matthew Valentine-House)
2022-10-04  2:12 ` [ruby-core:110175] " nobu (Nobuyoshi Nakada)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).