Skip to contentSkip to conversation

shmctl03: Fix 32-bit compat mode failure by forcing limits

PATCH Superseded Wei Gao via ltp 3 participants 23 messages Apr 7 – Aug 28 Library code

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 shmctl03: Fix 32-bit compat mode failure by forcing limits

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
 include/tst_assert.h                          | 10 +++++---
 lib/tst_assert.c                              | 25 ++++++++++++++-----
 .../kernel/syscalls/ipc/shmctl/shmctl03.c     | 12 ++++++---
 3 files changed, 35 insertions(+), 12 deletions(-)
include/tst_assert.h +7 -3
index dcb62dfea..5b3bd0e12 100644
@@ -21,14 +21,18 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_SATURATED_ULONG 0x02
+#define TST_ASSERT_BITWISE         0x04
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +19 -6
index b68bd5d39..09d7d8a6f 100644
@@ -23,18 +23,31 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+			unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		expected_val = (sys_val_64 > 2147483647ULL) ? 2147483647UL : (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_SATURATED_ULONG) {
+		expected_val = (sys_val_64 > 4294967295ULL) ? 4294967295UL : (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +9 -3
index a1f53e7c1..6d073789c 100644
@@ -30,9 +30,15 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	if (tst_is_compat_mode()) {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 4 lines · Andrea Cervesato via ltp

Hi Wei,

this review is pretty accurate. Please follow the suggestions here first:
https://github.com/acerv/ltp-agent/actions/runs/24074831444

I will update the subject line and use INT_MAX/UINT_MAX as AI suggested. But I'd prefer to keep TST_ASSERT_SATURATED_ULONG even without a current caller, as it completes the logic for the helper function. The logic is clear enough that an extra comment isn't needed also, if you are agree then i will sent next patch.

Thanks.

Quoted 9 lines

King regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Wei,

Quoted 4 lines

I will update the subject line and use INT_MAX/UINT_MAX as AI suggested. But I'd prefer to keep TST_ASSERT_SATURATED_ULONG even without a current caller, as it completes the logic for the helper function. The logic is clear enough that an extra comment isn't needed also, if you are agree then i will sent next patch.

Fair enough, feel free to ignore that part and to send a new patch.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v2 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
 include/tst_assert.h                          | 10 ++++---
 lib/tst_assert.c                              | 26 ++++++++++++++-----
 .../kernel/syscalls/ipc/shmctl/shmctl03.c     | 12 ++++++---
 3 files changed, 36 insertions(+), 12 deletions(-)
include/tst_assert.h +7 -3
index dcb62dfea..5b3bd0e12 100644
@@ -21,14 +21,18 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_SATURATED_ULONG 0x02
+#define TST_ASSERT_BITWISE         0x04
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +20 -6
index b68bd5d39..1598244c0 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,31 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+			unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		expected_val = (sys_val_64 > (unsigned long long)INT_MAX) ? (unsigned long)INT_MAX : (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_SATURATED_ULONG) {
+		expected_val = (sys_val_64 > (unsigned long long)UINT_MAX) ? (unsigned long)UINT_MAX : (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +9 -3
index a1f53e7c1..6d073789c 100644
@@ -30,9 +30,15 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	if (tst_is_compat_mode()) {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 26

Quoted 5 lines · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

include/tst_assert.h +3 -0
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_SATURATED_ULONG 0x02
+#define TST_ASSERT_BITWISE         0x04

TST_ASSERT_SATURATED_ULONG is never used in this patch. Either remove it or rename it to TST_ASSERT_SATURATED_U32 — the current name implies saturation at ULONG_MAX but it actually clamps to UINT_MAX (32-bit).

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 28

Quoted 2 lines
lib/tst_assert.c +2 -0
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+			unsigned long val, int flags)

Continuation should align with the opening '(' to match the declaration in tst_assert.h.

testcases/kernel/syscalls/ipc/shmctl/shmctl03.c static void verify_ipcinfo(void) · line 36

Quoted 4 lines
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +4 -0
+	if (tst_is_compat_mode()) {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall, TST_ASSERT_BITWISE);

Add a comment here explaining why shmmax uses saturation at INT_MAX while shmmni/shmall use bitwise truncation — the choice is non-obvious without knowing the kernel compat path behaviour.

Quoted 1 line

+Signed-off-by: Wei Gao <[email protected]>

This is a bug fix; please add a Fixes: tag pointing to the commit that originally added shmctl03 without compat mode handling.

Pre-existing issues noticed in the surrounding code (not introduced by this patch):

- testcases/kernel/syscalls/ipc/shmctl/shmctl03.c:12 — includes tse_newipc.h using the non-standard tse_ prefix instead of tst_.

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://patchwork.ozlabs.org/project/ltp/list/?series=499648

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH v3 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
 include/tst_assert.h                          |  9 +++++---
 lib/tst_assert.c                              | 22 ++++++++++++++-----
 .../kernel/syscalls/ipc/shmctl/shmctl03.c     | 17 +++++++++++---
 3 files changed, 37 insertions(+), 11 deletions(-)
include/tst_assert.h +6 -3
index dcb62dfea..8b2c8f655 100644
@@ -21,14 +21,17 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x04
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +17 -5
index b68bd5d39..773a10e49 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,29 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
 
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
 
-	if (val == sys_val) {
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		expected_val = (sys_val_64 > (unsigned long long)INT_MAX) ? (unsigned long)INT_MAX : (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
+
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +14 -3
index a1f53e7c1..e6c7a8027 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 4 lines · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

This is a bugfix; please add a Fixes: tag pointing to the commit that introduced the regression.

[...]

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 25

Quoted 2 lines
include/tst_assert.h +2 -0
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x04

0x02 is unused without explanation. Either renumber TST_ASSERT_BITWISE to 0x02 or add a comment marking the gap as reserved.

[...]

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 36

Quoted 2 lines
lib/tst_assert.c +2 -0
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		expected_val = (sys_val_64 > (unsigned long long)INT_MAX) ? (unsigned long)INT_MAX : (unsigned long)sys_val_64;

Line exceeds 80 columns. Split into an if/else block:

	if (sys_val_64 > (unsigned long long)INT_MAX)
		expected_val = (unsigned long)INT_MAX;
	else
		expected_val = (unsigned long)sys_val_64;

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://patchwork.ozlabs.org/project/ltp/list/?series=502039

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

PATCH v4 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
 include/tst_assert.h                          |  9 ++++---
 lib/tst_assert.c                              | 27 ++++++++++++++-----
 .../kernel/syscalls/ipc/shmctl/shmctl03.c     | 17 +++++++++---
 3 files changed, 41 insertions(+), 12 deletions(-)
include/tst_assert.h +6 -3
index dcb62dfea..2f1051270 100644
@@ -21,14 +21,17 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x02
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +21 -6
index b68bd5d39..fd651f53a 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +14 -3
index a1f53e7c1..e6c7a8027 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
-	TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmax", info.shmmax);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmmni", info.shmmni);
+		TST_ASSERT_ULONG("/proc/sys/kernel/shmall", info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 4 lines · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

+#define TST_ASSERT_SATURATED_INT 0x01

[...]

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 37

Quoted 3 lines
lib/tst_assert.c +3 -0
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;

For shmmax in compat mode, can you confirm the saturation is to INT_MAX (2^31-1) rather than UINT_MAX (2^32-1)? The compat_shminfo64.shmmax field is __compat_ulong_t (unsigned 32-bit), so truncation to UINT_MAX would be the natural expectation. A reference to the relevant kernel compat code path in ipc/shm.c would help clarify.

Pre-existing issues noticed in the surrounding code (not introduced by this patch):

- testcases/kernel/syscalls/ipc/shmctl/shmctl03.c:13 — #include "tse_newipc.h" appears unused; no symbols from it are referenced.

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/25150858881

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

the agent review seems to be valid. The shmmax is defined as unsigned long and we should keep the unsigned state of the variable.

Please verify that it works and we can proceed with the patch.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Quoted 5 lines · Andrea Cervesato

Hi Wei,

the agent review seems to be valid. The shmmax is defined as unsigned long and we should keep the unsigned state of the variable.

Kernel indeed saturates shmmax to INT_MAX for compat syscalls Relevant kernel code in ipc/shm.c:

static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
int version)

{
        if (in->shmmax > INT_MAX)
                in->shmmax = INT_MAX; <<<<<<<
        if (version == IPC_64) {
                struct compat_shminfo64 info;
                memset(&info, 0, sizeof(info));
                info.shmmax = in->shmmax;
Quoted 8 lines

Please verify that it works and we can proceed with the patch.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Andrea Cervesato
#12 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 6 lines

Kernel indeed saturates shmmax to INT_MAX for compat syscalls Relevant kernel code in ipc/shm.c:

static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
int version)

Quoted 8 lines
{
        if (in->shmmax > INT_MAX)
                in->shmmax = INT_MAX; <<<<<<<
        if (version == IPC_64) {
                struct compat_shminfo64 info;
                memset(&info, 0, sizeof(info));
                info.shmmax = in->shmmax;

You are right, LGTM

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

PATCH v5 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
v4->v5:
- Rebase to latest upstream code

 include/tst_assert.h                        |  9 ++++---
 lib/tst_assert.c                            | 27 ++++++++++++++++-----
 testcases/kernel/syscalls/shmctl/shmctl03.c | 17 ++++++++++---
 3 files changed, 41 insertions(+), 12 deletions(-)
include/tst_assert.h +6 -3
index dcb62dfea..2f1051270 100644
@@ -21,14 +21,17 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x02
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +21 -6
index b68bd5d39..fd651f53a 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/shmctl/shmctl03.c +14 -3
index 9e1c2f099..dfe5f17e3 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
-	TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
-	TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 4 lines · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

#define TST_ASSERT_ULONG(path, val, ...) \
	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))

Could this avoid the comma expression? When a flag is supplied, its left-hand operand triggers -Wunused-value, so all three new compat-mode assertions emit compiler warnings and builds using -Werror fail. Making the flags argument explicit and passing 0 at no-flag call sites, or dispatching separate two- and three-argument macro forms, would avoid the warning.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 8 lines
Quoted 2 lines
#define TST_ASSERT_ULONG(path, val, ...) \
	tst_assert_ulong(__FILE__, __LINE__, path, val, (0, ##__VA_ARGS__))

Could this avoid the comma expression? When a flag is supplied, its left-hand operand triggers -Wunused-value, so all three new compat-mode assertions emit compiler warnings and builds using -Werror fail. Making the flags argument explicit and passing 0 at no-flag call sites, or dispatching separate two- and three-argument macro forms, would avoid the warning.

This is correct, we can use TST_2_ which is our default way to avoid these issues.

#define TST_ASSERT_ULONG(path, val, ...) \
	tst_assert_ulong(__FILE__, __LINE__, path, val, \
		TST_2_(dummy, ##__VA_ARGS__, 0))

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw

PATCH v6 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
v5->v6:
- Replaced the comma expression (0, ##__VA_ARGS__) in TST_ASSERT_ULONG with TST_2_(dummy, ##__VA_ARGS__, 0).

 include/tst_assert.h                        | 10 +++++---
 lib/tst_assert.c                            | 27 ++++++++++++++++-----
 testcases/kernel/syscalls/shmctl/shmctl03.c | 17 ++++++++++---
 3 files changed, 42 insertions(+), 12 deletions(-)
include/tst_assert.h +7 -3
index dcb62dfea..a21ed5bd6 100644
@@ -21,14 +21,18 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x02
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, 0))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +21 -6
index b68bd5d39..fd651f53a 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/shmctl/shmctl03.c +14 -3
index 9e1c2f099..dfe5f17e3 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
-	TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
-	TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 31

Quoted 10 lines · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

include/tst_assert.h +4 -0
+#define TST_ASSERT_SATURATED_INT   0x01
+#define TST_ASSERT_BITWISE         0x02
+
 /*
  * Same as tst_assert_int() but for unsigned long.
  */
 void tst_assert_ulong(const char *file, const int lineno,
+                      const char *path, unsigned long val, int flags);

Could these new public flags and the flags parameter be documented with kernel-doc? The existing comment does not explain that one mode clamps at INT_MAX while the other keeps the low 32 bits. [Low]

testcases/kernel/syscalls/shmctl/shmctl03.c static void verify_ipcinfo(void) · line 40

Quoted 6 lines
testcases/kernel/syscalls/shmctl/shmctl03.c +6 -0
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_BITWISE);

Could shmmni keep the direct comparison, and could the comment omit it? In Linux 7.2, ipc_namespace.shm_ctlmni is an int, the sysctl limits it to 0..ipc_mni, and the compat path copies it directly to compat_ulong_t, so no truncation occurs. [Low]

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#18 lore raw

PATCH v7 1/1 shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
v6->v7:
- kernel-doc for tst_assert_ulong

 include/tst_assert.h                        | 42 ++++++++++++++++++---
 lib/tst_assert.c                            | 27 ++++++++++---
 testcases/kernel/syscalls/shmctl/shmctl03.c | 17 +++++++--
 3 files changed, 72 insertions(+), 14 deletions(-)
include/tst_assert.h +37 -5
index dcb62dfea..3cc0d334d 100644
@@ -21,14 +21,46 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
-/*
- * Same as tst_assert_int() but for unsigned long.
+/**
+ * TST_ASSERT_SATURATED_INT - Clamp the sysfs/procfs value to INT_MAX.
+ *
+ * If this flag is set and the value read from the sysfs/procfs file exceeds
+ * INT_MAX, the compared value is clamped (saturated) to INT_MAX.
+ */
+#define TST_ASSERT_SATURATED_INT   0x01
+
+/**
+ * TST_ASSERT_BITWISE - Keep only the lower 32 bits of the read value.
+ *
+ * If this flag is set, only the low 32 bits of the value read from the
+ * sysfs/procfs file are compared.
+ */
+#define TST_ASSERT_BITWISE         0x02
+
+/**
+ * tst_assert_ulong() - Assert that an unsigned long value in a file matches.
+ * @file: The source file of the assertion (usually __FILE__).
+ * @lineno: The source line number of the assertion (usually __LINE__).
+ * @path: Path to the sysfs or procfs file to read from.
+ * @val: The expected unsigned long value to compare against.
+ * @flags: Bitwise flags controlling how the read value is processed.
+ *         Supported flags:
+ *         - %TST_ASSERT_SATURATED_INT: Clamps the value at %INT_MAX if it
+ *           exceeds it.
+ *         - %TST_ASSERT_BITWISE: Keeps only the low 32 bits of the read value,
+ *           truncating any higher bits.
+ *         - %0: Direct comparison without any adjustment.
+ *
+ * This function reads an integer value from the file specified by @path and compares
+ * it with @val. It allows handling of 32-bit compat mode truncation/clamping on
+ * 64-bit systems via @flags.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, 0))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +21 -6
index b68bd5d39..fd651f53a 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_BITWISE) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/shmctl/shmctl03.c +14 -3
index 9e1c2f099..dfe5f17e3 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
-	TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
-	TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_BITWISE);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, TST_ASSERT_BITWISE);
+	} else {
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#19 lore raw

Re: [LTP] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Hi Wei,

Quoted 1 line · Wei Gao

shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#20 lore raw

Re: [LTP] [PATCH v7] shmctl03: Fix 32-bit compat mode failure by adjusting comparisons for compat mode truncation

In reply to Wei Gao via ltp

Hi Wei,

title is too long, it needs to be shorten.

Quoted 3 lines

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

Also here we are above 85 lines.

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 38

Quoted 7 lines
include/tst_assert.h +7 -0
+/**
+ * TST_ASSERT_BITWISE - Keep only the lower 32 bits of the read value.
+ *
+ * If this flag is set, only the low 32 bits of the value read from the
+ * sysfs/procfs file are compared.
+ */
+#define TST_ASSERT_BITWISE         0x02

I was thinking that this should be probably TST_ASSERT_TRUNC_32BIT, since bitwise is misleading.

needs to be fixed before merge.

Thanks,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#21 lore raw

PATCH v8 1/1 shmctl03: Fix 32-bit compat mode failure

In reply to Wei Gao via ltp

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

---
v7->v8:
- Improving and Shortening the Commit Message.
- Renaming the Assert Flag: Renamed TST_ASSERT_BITWISE to TST_ASSERT_TRUNC_32BIT.

 include/tst_assert.h                        | 42 ++++++++++++++++++---
 lib/tst_assert.c                            | 27 ++++++++++---
 testcases/kernel/syscalls/shmctl/shmctl03.c | 17 +++++++--
 3 files changed, 72 insertions(+), 14 deletions(-)
include/tst_assert.h +37 -5
index dcb62dfea..045942b1b 100644
@@ -21,14 +21,46 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
-/*
- * Same as tst_assert_int() but for unsigned long.
+/**
+ * TST_ASSERT_SATURATED_INT - Clamp the sysfs/procfs value to INT_MAX.
+ *
+ * If this flag is set and the value read from the sysfs/procfs file exceeds
+ * INT_MAX, the compared value is clamped (saturated) to INT_MAX.
+ */
+#define TST_ASSERT_SATURATED_INT   0x01
+
+/**
+ * TST_ASSERT_TRUNC_32BIT - Keep only the lower 32 bits of the read value.
+ *
+ * If this flag is set, only the low 32 bits of the value read from the
+ * sysfs/procfs file are compared.
+ */
+#define TST_ASSERT_TRUNC_32BIT     0x02
+
+/**
+ * tst_assert_ulong() - Assert that an unsigned long value in a file matches.
+ * @file: The source file of the assertion (usually __FILE__).
+ * @lineno: The source line number of the assertion (usually __LINE__).
+ * @path: Path to the sysfs or procfs file to read from.
+ * @val: The expected unsigned long value to compare against.
+ * @flags: Bitwise flags controlling how the read value is processed.
+ *         Supported flags:
+ *         - %TST_ASSERT_SATURATED_INT: Clamps the value at %INT_MAX if it
+ *           exceeds it.
+ *         - %TST_ASSERT_TRUNC_32BIT: Keeps only the low 32 bits of the
+ *           read value, truncating any higher bits.
+ *         - %0: Direct comparison without any adjustment.
+ *
+ * This function reads an integer value from the file specified by @path
+ * and compares it with @val. It allows handling of 32-bit compat mode
+ * truncation/clamping on 64-bit systems via @flags.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, 0))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +21 -6
index b68bd5d39..9a606d863 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_TRUNC_32BIT) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/shmctl/shmctl03.c +14 -3
index 9e1c2f099..0ac3f45c5 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
-	TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
-	TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_TRUNC_32BIT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, TST_ASSERT_TRUNC_32BIT);
+	} else {
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#22 lore raw

Re: [LTP] [PATCH v8] shmctl03: Fix 32-bit compat mode failure

Hi Wei,

Quoted 4 lines

On 64-bit kernels, the default shmmax and shmall values often exceed the range of a 32-bit unsigned long or are clipped differently by the kernel's compat syscall layer than they appear in /proc.

FYI an old comment from Arnd Bergmann [1]

[1] https://lore.kernel.org/ltp/[email protected]/

Quoted 5 lines
Quoted 7 lines

---
v7->v8:
- Improving and Shortening the Commit Message.
- Renaming the Assert Flag: Renamed TST_ASSERT_BITWISE to TST_ASSERT_TRUNC_32BIT.

 include/tst_assert.h                        | 42 ++++++++++++++++++---
 lib/tst_assert.c                            | 27 ++++++++++---

Wouldn't it make sense to split library part into separate commit?

Quoted 10 lines
 testcases/kernel/syscalls/shmctl/shmctl03.c | 17 +++++++--
 3 files changed, 72 insertions(+), 14 deletions(-)
include/tst_assert.h +0 -0
index dcb62dfea..045942b1b 100644
@@ -21,14 +21,46 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 30

Quoted 10 lines
include/tst_assert.h +7 -2
 
-/*
- * Same as tst_assert_int() but for unsigned long.
+/**
+ * TST_ASSERT_SATURATED_INT - Clamp the sysfs/procfs value to INT_MAX.
+ *
+ * If this flag is set and the value read from the sysfs/procfs file exceeds
+ * INT_MAX, the compared value is clamped (saturated) to INT_MAX.
+ */
+#define TST_ASSERT_SATURATED_INT   0x01

IMHO enum (instead of plain definitions) would be more suitable for this (see tst_cmd.h). First good benefit is that will do the check for correct values for free.

Quoted 8 lines
include/tst_assert.h +8 -0
+
+/**
+ * TST_ASSERT_TRUNC_32BIT - Keep only the lower 32 bits of the read value.
+ *
+ * If this flag is set, only the low 32 bits of the value read from the
+ * sysfs/procfs file are compared.
+ */
+#define TST_ASSERT_TRUNC_32BIT     0x02

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 48

Quoted 10 lines
include/tst_assert.h +10 -0
+
+/**
+ * tst_assert_ulong() - Assert that an unsigned long value in a file matches.
+ * @file: The source file of the assertion (usually __FILE__).
+ * @lineno: The source line number of the assertion (usually __LINE__).
+ * @path: Path to the sysfs or procfs file to read from.
+ * @val: The expected unsigned long value to compare against.
+ * @flags: Bitwise flags controlling how the read value is processed.
+ *         Supported flags:
+ *         - %TST_ASSERT_SATURATED_INT: Clamps the value at %INT_MAX if it

Unfortunately lists aren't supported in kerneldoc parameters. Therefore this will not work, it will be rendered as inline:

Supported flags: - TST_ASSERT_SATURATED_INT: Clamps the value at INT_MAX if it exceeds it. - TST_ASSERT_TRUNC_32BIT: Keeps only the low 32 bits of the read value, truncating any higher bits. - 0: Direct comparison without any adjustment.

You will see it if you rebase the current master (I added tst_assert.h to the docs), it'd be great to double check how the docs actually look like.

Therefore using enum will solve it because we don't need to list supported flags (there will be link to the enum itself) - second benefit for enum.

Quoted 8 lines
include/tst_assert.h +8 -0
+ *           exceeds it.
+ *         - %TST_ASSERT_TRUNC_32BIT: Keeps only the low 32 bits of the
+ *           read value, truncating any higher bits.
+ *         - %0: Direct comparison without any adjustment.
+ *
+ * This function reads an integer value from the file specified by @path
+ * and compares it with @val. It allows handling of 32-bit compat mode
+ * truncation/clamping on 64-bit systems via @flags.

include/tst_assert.h void tst_assert_int(const char *file, const int lineno, · line 63

Quoted 10 lines
include/tst_assert.h +4 -3
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val, int flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, 0))

I've never noticed TST_2_(), nice way to avoid passing 0 as flag.

lib/tst_assert.c line 8

Quoted 11 lines
include/tst_assert.h +0 -0
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
lib/tst_assert.c +1 -0
index b68bd5d39..9a606d863 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
 #include <stdio.h>
+#include <limits.h>

Why is this added?

Quoted 58 lines
lib/tst_assert.c +20 -6
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -23,18 +24,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, int flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_TRUNC_32BIT) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
testcases/kernel/syscalls/shmctl/shmctl03.c +5 -3
index 9e1c2f099..0ac3f45c5 100644
@@ -30,9 +30,20 @@ static void verify_ipcinfo(void)
 	else
 		tst_res(TPASS, "shmmin = 1");
 
-	TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
-	TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
-	TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	if (tst_is_compat_mode()) {
+		/*
+		 * On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
+		 * compat syscall, while shmmni and shmall are truncated
+		 * to 32-bit.

testcases/kernel/syscalls/shmctl/shmctl03.c static void verify_ipcinfo(void) · line 47

Quoted 10 lines
testcases/kernel/syscalls/shmctl/shmctl03.c +9 -0
+		 */
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, TST_ASSERT_SATURATED_INT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, TST_ASSERT_TRUNC_32BIT);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, TST_ASSERT_TRUNC_32BIT);
 
+	} else {
+		TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
+		TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni);
+		TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall);
+	}

nit: I'd personally code it with ternary operator, but whatever you prefer.

bool c = tst_is_compat_mode());
/*
* On 64-bit kernel, shmmax is clamped to INT_MAX for 32-bit
* compat syscall, while shmmni and shmall are truncated
* to 32-bit.
*/
TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax, c ? TST_ASSERT_SATURATED_INT : 0); TST_ASSERT_ULONG(PATH_KERN_SHMMNI, info.shmmni, c ? TST_ASSERT_TRUNC_32BIT : 0); TST_ASSERT_ULONG(PATH_KERN_SHMALL, info.shmall, c ? TST_ASSERT_TRUNC_32BIT: 0);

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp