Skip to contentSkip to conversation

shmctl03: Fix 32-bit compat mode failure

PATCH In review Wei Gao via ltp 5 participants 9 messages Sep 3 – Sep 23 Library code

lore
Share & tools
lore
#0 lore raw

v11 cover shmctl03: Fix 32-bit compat mode failure

v10-v11:
- Fix make check format issue

Wei Gao (2):
lib: Extend tst_assert_ulong() with enum flags
shmctl03: Fix 32-bit compat mode failure

 include/tst_assert.h                        | 34 ++++++++++++++++++---
 lib/tst_assert.c                            | 26 ++++++++++++----
 testcases/kernel/syscalls/shmctl/shmctl03.c | 13 ++++++--
 3 files changed, 59 insertions(+), 14 deletions(-)
Signature · 5 lines
2.55.0


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

PATCH v11 1/2 lib: Extend tst_assert_ulong() with enum flags

Introduce enum tst_assert_flags (including TST_ASSERT_SATURATED_INT and TST_ASSERT_TRUNC_32BIT) to tst_assert_ulong(). This enables type-safe handling of 32-bit compat-mode truncation and clamping behaviors on 64-bit kernels when verifying sysfs/procfs limits.

---
 include/tst_assert.h | 34 +++++++++++++++++++++++++++++-----
 lib/tst_assert.c     | 26 ++++++++++++++++++++------
 2 files changed, 49 insertions(+), 11 deletions(-)
include/tst_assert.h +29 -5
index dcb62dfea..ee4f735e4 100644
@@ -21,14 +21,38 @@ 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.
+/**
+ * enum tst_assert_flags - Bitwise flags for tst_assert_ulong().
+ *
+ * @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.
+ */
+enum tst_assert_flags {
+	TST_ASSERT_SATURATED_INT = 1,
+	TST_ASSERT_TRUNC_32BIT   = 2,
+};
+
+/**
+ * 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.
+ *         See &enum tst_assert_flags.
+ *
+ * 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,
+		      enum tst_assert_flags 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 +20 -6
index b68bd5d39..04241d31b 100644
@@ -23,18 +23,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, enum tst_assert_flags 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)
Signature · 5 lines
2.55.0


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

Re: [LTP] lib: Extend tst_assert_ulong() with enum flags

Hi Wei,

Quoted 1 line · Wei Gao

lib: Extend tst_assert_ulong() with enum flags

--- [PATCH 1/2] ---

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

Quoted 3 lines
include/tst_assert.h +3 -0
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, 0))

TST_ASSERT_ULONG is a modified public macro but lacks the required kernel-doc comment. Please document its optional flags argument and the zero-flags default at the macro itself.

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
#3 lore raw

Re: [LTP] [PATCH v11 1/2] lib: Extend tst_assert_ulong() with enum flags

In reply to Wei Gao via ltp

H Wei,

I was re-checking the whole patch-set and it looks a bit overengineered from my point of view. A few points below.

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

Quoted 4 lines
include/tst_assert.h +4 -0
+enum tst_assert_flags {
+	TST_ASSERT_SATURATED_INT = 1,
+	TST_ASSERT_TRUNC_32BIT   = 2,
+};

Adding new flags for the whole LTP core library trying to fix a single test is not a good idea in general. Also, TST_ASSERT_TRUNC_32BIT is probably not needed.

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

Quoted 1 line
lib/tst_assert.c +1 -0
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);

This is the real fix. Read below.

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

Quoted 7 lines
lib/tst_assert.c +7 -0
+
+	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) {

In 32bit compat mode unsigned long becomes 32bit, so we can simply do:

void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)

{
	unsigned long long sys_val;

	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val);

	if (val == (unsigned long)sys_val) {
		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
		return;
	}

	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu",
		path, val, (unsigned long)sys_val);
}

And then, inside the test:

	if (tst_is_compat_mode() && info.shmmax == INT_MAX)
		tst_res(TPASS, "shmmax clamped to INT_MAX in compat mode");
	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);

Please verify if this is working anyway. We can probably fix the whole patch-set with a couple of lines instead of defining redundant flags and specific code for a single test.

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

Re: [LTP] [PATCH v11 1/2] lib: Extend tst_assert_ulong() with enum flags

Quoted 5 lines · Andrea Cervesato

H Wei,

I was re-checking the whole patch-set and it looks a bit overengineered from my point of view. A few points below.

Quoted 8 lines

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

Quoted 4 lines
include/tst_assert.h +4 -0
+enum tst_assert_flags {
+	TST_ASSERT_SATURATED_INT = 1,
+	TST_ASSERT_TRUNC_32BIT   = 2,
+};

Adding new flags for the whole LTP core library trying to fix a single test is not a good idea in general. Also, TST_ASSERT_TRUNC_32BIT is probably not needed.

But this is follow Cyril's suggestion:
https://lore.kernel.org/ltp/[email protected]/

Quoted 50 lines

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

Quoted 2 lines
lib/tst_assert.c +1 -0
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);

This is the real fix. Read below.

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

Quoted 7 lines
lib/tst_assert.c +7 -0
+
+	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) {

In 32bit compat mode unsigned long becomes 32bit, so we can simply do:

void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)

{
	unsigned long long sys_val;

	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val);

	if (val == (unsigned long)sys_val) {
		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
		return;
	}

	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu",
		path, val, (unsigned long)sys_val);
}

And then, inside the test:

	if (tst_is_compat_mode() && info.shmmax == INT_MAX)
		tst_res(TPASS, "shmmax clamped to INT_MAX in compat mode");
	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);

Please verify if this is working anyway. We can probably fix the whole patch-set with a couple of lines instead of defining redundant flags and specific code for a single test.

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

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

Re: [LTP] [PATCH v11 1/2] lib: Extend tst_assert_ulong() with enum flags

Quoted 15 lines · Andrea Cervesato
Quoted 5 lines

H Wei,

I was re-checking the whole patch-set and it looks a bit overengineered from my point of view. A few points below.

Quoted 8 lines

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

Quoted 4 lines
include/tst_assert.h +4 -0
+enum tst_assert_flags {
+	TST_ASSERT_SATURATED_INT = 1,
+	TST_ASSERT_TRUNC_32BIT   = 2,
+};

Adding new flags for the whole LTP core library trying to fix a single test is not a good idea in general. Also, TST_ASSERT_TRUNC_32BIT is probably not needed.

Quoted 2 lines

But this is follow Cyril's suggestion:
https://lore.kernel.org/ltp/[email protected]/

FYI the informal rule Cyril announced few weeks ago "at least two tests needs it" (we talked about .max_kver member of struct tst_test, I guess this would more or less applied to library API code.

When I acked this in v9 [1] (after suggesting to use enum instead of #define in v8 [2]) I expected it'd be actually needed in more tests, based on Cyril's suggestion.

Anyway, Dan Carpenter mentioned [3] kernel commit af7c693f1460 ("Cap shmmax at INT_MAX in compat shminfo") which does

+ if (smi->shmmax > INT_MAX)
+ smi->shmmax = INT_MAX;

[1] https://lore.kernel.org/ltp/20260902090418.GA910767@pevik/
[2] https://lore.kernel.org/ltp/20260828092602.GA715959@pevik/
[3] https://lore.kernel.org/ltp/[email protected]/

Searching in kernel git

$ git grep -A2 'if.* > INT_MAX' |grep ' = INT_MAX' 

I found only:

block/bfq-iosched.c
drivers/usb/class/usbtmc.c
drivers/xen/pvcalls-front.c
fs/ext4/inode.c
include/net/netfilter/nf_conntrack_core.h
ipc/shm.c
kernel/time/itimer.c
lib/vsprintf.c
net/mptcp/protocol.c
sound/core/oss/pcm_oss.c
tools/lib/bpf/ringbuf.c

IMHO none of them uses export data truncated by INT_MAX to sysfs.

Quoted 30 lines
Quoted 29 lines

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

Quoted 1 line
lib/tst_assert.c +1 -0
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);

This is the real fix. Read below.

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

Quoted 7 lines
lib/tst_assert.c +7 -0
+
+	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) {

In 32bit compat mode unsigned long becomes 32bit, so we can simply do:

void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)

{
	unsigned long long sys_val;

	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val);

	if (val == (unsigned long)sys_val) {
		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
	}

	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu",
		path, val, (unsigned long)sys_val);
}

And then, inside the test:

Quoted 7 lines
Quoted 7 lines
	if (tst_is_compat_mode() && info.shmmax == INT_MAX)
		tst_res(TPASS, "shmmax clamped to INT_MAX in compat mode");
	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);

Looks like quite elegant solution, give we really need it just for a single test.

Quoted 3 lines
Quoted 3 lines

Please verify if this is working anyway. We can probably fix the whole patch-set with a couple of lines instead of defining redundant flags and specific code for a single test.

Yes it works.

=> I vote to merge Andrea's solution instead.
@Cyril @Li WDYT?

Kind regards,
Petr

Quoted 5 lines
Quoted 5 lines

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

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

Re: [LTP] [PATCH v11 1/2] lib: Extend tst_assert_ulong() with enum flags

Quoted 2 lines

=> I vote to merge Andrea's solution instead.
@Cyril @Li WDYT?

Agreed.

Signature · 5 lines
Regards,
Li Wang

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

Re: [LTP] [PATCH v11 1/2] lib: Extend tst_assert_ulong() with enum flags

In reply to Andrea Cervesato via ltp

Hi!

Quoted 25 lines

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

Quoted 7 lines
lib/tst_assert.c +7 -0
+
+	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) {

In 32bit compat mode unsigned long becomes 32bit, so we can simply do:

void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)

{
	unsigned long long sys_val;

	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val);

	if (val == (unsigned long)sys_val) {
		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
		return;
	}

	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu",
		path, val, (unsigned long)sys_val);
}
Quoted 4 lines

And then, inside the test:

	if (tst_is_compat_mode() && info.shmmax == INT_MAX)
		tst_res(TPASS, "shmmax clamped to INT_MAX in compat mode");

As I commented on the patch from pvorel, I do not like the blind TPASS here. Either we put the saturated logic into the test library, or we need to open-code it here.

Quoted 5 lines
	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);
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v11 2/2 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.

Fix this by utilizing the new TST_ASSERT_ULONG() flags to properly clamp and truncate the expected values under compat mode.

---
 testcases/kernel/syscalls/shmctl/shmctl03.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/shmctl/shmctl03.c +10 -3
index 9e1c2f099..cf021be4c 100644
@@ -30,9 +30,16 @@ 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);
+	int 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);
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp