[PATCH] gcc-plugins: Use swap() to simplify code

Liao Yuanhong posted 1 patch 1 month, 4 weeks ago
scripts/gcc-plugins/randomize_layout_plugin.c | 16 +++-------------
1 file changed, 3 insertions(+), 13 deletions(-)
[PATCH] gcc-plugins: Use swap() to simplify code
Posted by Liao Yuanhong 1 month, 4 weeks ago
Replace the original swapping logic with swap() to improve readability and
remove temporary variables

Signed-off-by: Liao Yuanhong <liaoyuanhong@vivo.com>
---
 scripts/gcc-plugins/randomize_layout_plugin.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/scripts/gcc-plugins/randomize_layout_plugin.c b/scripts/gcc-plugins/randomize_layout_plugin.c
index ff65a4f87f24..5d6ebe292cbf 100644
--- a/scripts/gcc-plugins/randomize_layout_plugin.c
+++ b/scripts/gcc-plugins/randomize_layout_plugin.c
@@ -199,17 +199,12 @@ static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prn
 
 	/* FIXME: this group shuffle is currently a no-op. */
 	for (i = num_groups - 1; i > 0; i--) {
-		struct partition_group tmp;
 		randnum = ranval(prng_state) % (i + 1);
-		tmp = size_group[i];
-		size_group[i] = size_group[randnum];
-		size_group[randnum] = tmp;
+		swap(size_group[randnum], size_group[i]);
 	}
 
 	for (x = 0; x < num_groups; x++) {
 		for (index = size_group[x].length - 1; index > 0; index--) {
-			tree tmp;
-
 			i = size_group[x].start + index;
 			if (DECL_BIT_FIELD_TYPE(newtree[i]))
 				continue;
@@ -218,9 +213,7 @@ static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prn
 			// we could handle this case differently if desired
 			if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
 				continue;
-			tmp = newtree[i];
-			newtree[i] = newtree[randnum];
-			newtree[randnum] = tmp;
+			swap(newtree[randnum], newtree[i]);
 		}
 	}
 }
@@ -230,11 +223,8 @@ static void full_shuffle(tree *newtree, unsigned long length, ranctx *prng_state
 	unsigned long i, randnum;
 
 	for (i = length - 1; i > 0; i--) {
-		tree tmp;
 		randnum = ranval(prng_state) % (i + 1);
-		tmp = newtree[i];
-		newtree[i] = newtree[randnum];
-		newtree[randnum] = tmp;
+		swap(newtree[randnum], newtree[i]);
 	}
 }
 
-- 
2.34.1
Re: [PATCH] gcc-plugins: Use swap() to simplify code
Posted by kernel test robot 1 month, 4 weeks ago
Hi Liao,

kernel test robot noticed the following build errors:

[auto build test ERROR on kees/for-next/hardening]
[also build test ERROR on kees/for-next/kspp kees/for-next/pstore linus/master v6.16 next-20250806]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Liao-Yuanhong/gcc-plugins-Use-swap-to-simplify-code/20250806-204609
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
patch link:    https://lore.kernel.org/r/20250806124341.382446-1-liaoyuanhong%40vivo.com
patch subject: [PATCH] gcc-plugins: Use swap() to simplify code
config: i386-randconfig-012-20250807 (https://download.01.org/0day-ci/archive/20250807/202508071233.Bf6EgGd2-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250807/202508071233.Bf6EgGd2-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202508071233.Bf6EgGd2-lkp@intel.com/

All errors (new ones prefixed by >>):

   scripts/gcc-plugins/randomize_layout_plugin.c: In function 'void performance_shuffle(tree_node**, long unsigned int, ranctx*)':
>> scripts/gcc-plugins/randomize_layout_plugin.c:203:17: error: 'swap' was not declared in this scope
     203 |                 swap(size_group[randnum], size_group[i]);
         |                 ^~~~
   scripts/gcc-plugins/randomize_layout_plugin.c:203:17: note: suggested alternatives:
   In file included from /usr/include/c++/12/utility:69,
                    from /usr/lib/gcc/x86_64-linux-gnu/12/plugin/include/system.h:244,
                    from /usr/lib/gcc/x86_64-linux-gnu/12/plugin/include/gcc-plugin.h:28,
                    from scripts/gcc-plugins/gcc-common.h:6,
                    from scripts/gcc-plugins/randomize_layout_plugin.c:19:
   /usr/include/c++/12/bits/stl_pair.h:715:5: note:   'std::swap'
     715 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
         |     ^~~~
   In file included from /usr/include/c++/12/bits/stl_pair.h:61:
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
     196 |     swap(_Tp& __a, _Tp& __b)
         |     ^~~~
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
   scripts/gcc-plugins/randomize_layout_plugin.c:216:25: error: 'swap' was not declared in this scope
     216 |                         swap(newtree[randnum], newtree[i]);
         |                         ^~~~
   scripts/gcc-plugins/randomize_layout_plugin.c:216:25: note: suggested alternatives:
   /usr/include/c++/12/bits/stl_pair.h:715:5: note:   'std::swap'
     715 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
         |     ^~~~
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
     196 |     swap(_Tp& __a, _Tp& __b)
         |     ^~~~
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
   scripts/gcc-plugins/randomize_layout_plugin.c: In function 'void full_shuffle(tree_node**, long unsigned int, ranctx*)':
   scripts/gcc-plugins/randomize_layout_plugin.c:227:17: error: 'swap' was not declared in this scope
     227 |                 swap(newtree[randnum], newtree[i]);
         |                 ^~~~
   scripts/gcc-plugins/randomize_layout_plugin.c:227:17: note: suggested alternatives:
   /usr/include/c++/12/bits/stl_pair.h:715:5: note:   'std::swap'
     715 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
         |     ^~~~
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
     196 |     swap(_Tp& __a, _Tp& __b)
         |     ^~~~
   /usr/include/c++/12/bits/move.h:196:5: note:   'std::swap'
   make[4]: *** [scripts/gcc-plugins/Makefile:54: scripts/gcc-plugins/randomize_layout_plugin.so] Error 1 shuffle=2972943921
   make[4]: Target 'scripts/gcc-plugins/' not remade because of errors.
   make[3]: *** [scripts/Makefile.build:554: scripts/gcc-plugins] Error 2 shuffle=2972943921
   make[3]: Target 'scripts/' not remade because of errors.
   make[2]: *** [Makefile:1258: scripts] Error 2 shuffle=2972943921
   make[2]: Target 'prepare' not remade because of errors.
   make[1]: *** [Makefile:248: __sub-make] Error 2 shuffle=2972943921
   make[1]: Target 'prepare' not remade because of errors.
   make: *** [Makefile:248: __sub-make] Error 2 shuffle=2972943921
   make: Target 'prepare' not remade because of errors.


vim +/swap +203 scripts/gcc-plugins/randomize_layout_plugin.c

   190	
   191	static void performance_shuffle(tree *newtree, unsigned long length, ranctx *prng_state)
   192	{
   193		unsigned long i, x, index;
   194		struct partition_group size_group[length];
   195		unsigned long num_groups = 0;
   196		unsigned long randnum;
   197	
   198		partition_struct(newtree, length, (struct partition_group *)&size_group, &num_groups);
   199	
   200		/* FIXME: this group shuffle is currently a no-op. */
   201		for (i = num_groups - 1; i > 0; i--) {
   202			randnum = ranval(prng_state) % (i + 1);
 > 203			swap(size_group[randnum], size_group[i]);
   204		}
   205	
   206		for (x = 0; x < num_groups; x++) {
   207			for (index = size_group[x].length - 1; index > 0; index--) {
   208				i = size_group[x].start + index;
   209				if (DECL_BIT_FIELD_TYPE(newtree[i]))
   210					continue;
   211				randnum = ranval(prng_state) % (index + 1);
   212				randnum += size_group[x].start;
   213				// we could handle this case differently if desired
   214				if (DECL_BIT_FIELD_TYPE(newtree[randnum]))
   215					continue;
   216				swap(newtree[randnum], newtree[i]);
   217			}
   218		}
   219	}
   220	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki