Skip to content

Commit a1b0275

Browse files
committed
Merge tag 'printk-for-5.20-sane' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux
Pull printk updates from Petr Mladek: - Allow reading kernel log in gdb even on 32 bits systems - More granular check of the buffer usage in printf selftest - Clang warning fix * tag 'printk-for-5.20-sane' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux: lib/test_printf.c: fix clang -Wformat warnings scripts/gdb: fix 'lx-dmesg' on 32 bits arch lib/test_printf.c: split write-beyond-buffer check in two
2 parents 965a9d7 + 96dd9a2 commit a1b0275

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/test_printf.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#define PAD_SIZE 16
3131
#define FILL_CHAR '$'
3232

33+
#define NOWARN(option, comment, block) \
34+
__diag_push(); \
35+
__diag_ignore_all(#option, comment); \
36+
block \
37+
__diag_pop();
38+
3339
KSTM_MODULE_GLOBALS();
3440

3541
static char *test_buffer __initdata;
@@ -78,12 +84,17 @@ do_test(int bufsize, const char *expect, int elen,
7884
return 1;
7985
}
8086

81-
if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
87+
if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) {
8288
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
8389
bufsize, fmt);
8490
return 1;
8591
}
8692

93+
if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) {
94+
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt);
95+
return 1;
96+
}
97+
8798
if (memcmp(test_buffer, expect, written)) {
8899
pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
89100
bufsize, fmt, test_buffer, written, expect);
@@ -154,9 +165,11 @@ test_number(void)
154165
test("0x1234abcd ", "%#-12x", 0x1234abcd);
155166
test(" 0x1234abcd", "%#12x", 0x1234abcd);
156167
test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
157-
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
158-
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
159-
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
168+
NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", {
169+
test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
170+
test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
171+
test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
172+
})
160173
/*
161174
* POSIX/C99: »The result of converting zero with an explicit
162175
* precision of zero shall be no characters.« Hence the output

scripts/gdb/linux/dmesg.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
2323
prb_data_ring_type = utils.CachedType("struct prb_data_ring")
2424
printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
25-
atomic_long_type = utils.CachedType("atomic_long_t")
2625

2726
class LxDmesg(gdb.Command):
2827
"""Print Linux kernel log buffer."""
@@ -68,8 +67,6 @@ def invoke(self, arg, from_tty):
6867
off = prb_data_ring_type.get_type()['data'].bitpos // 8
6968
text_data_addr = utils.read_ulong(text_data_ring, off)
7069

71-
counter_off = atomic_long_type.get_type()['counter'].bitpos // 8
72-
7370
sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
7471

7572
off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
@@ -89,9 +86,9 @@ def invoke(self, arg, from_tty):
8986

9087
# read in tail and head descriptor ids
9188
off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
92-
tail_id = utils.read_u64(desc_ring, off + counter_off)
89+
tail_id = utils.read_atomic_long(desc_ring, off)
9390
off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
94-
head_id = utils.read_u64(desc_ring, off + counter_off)
91+
head_id = utils.read_atomic_long(desc_ring, off)
9592

9693
did = tail_id
9794
while True:
@@ -102,7 +99,7 @@ def invoke(self, arg, from_tty):
10299
desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()
103100

104101
# skip non-committed record
105-
state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift)
102+
state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift)
106103
if state != desc_committed and state != desc_finalized:
107104
if did == head_id:
108105
break

scripts/gdb/linux/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ def get_type(self):
3535

3636

3737
long_type = CachedType("long")
38-
38+
atomic_long_type = CachedType("atomic_long_t")
3939

4040
def get_long_type():
4141
global long_type
4242
return long_type.get_type()
4343

44-
4544
def offset_of(typeobj, field):
4645
element = gdb.Value(0).cast(typeobj)
4746
return int(str(element[field].address).split()[0], 16)
@@ -129,6 +128,17 @@ def read_ulong(buffer, offset):
129128
else:
130129
return read_u32(buffer, offset)
131130

131+
atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos
132+
atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof
133+
134+
def read_atomic_long(buffer, offset):
135+
global atomic_long_counter_offset
136+
global atomic_long_counter_sizeof
137+
138+
if atomic_long_counter_sizeof == 8:
139+
return read_u64(buffer, offset + atomic_long_counter_offset)
140+
else:
141+
return read_u32(buffer, offset + atomic_long_counter_offset)
132142

133143
target_arch = None
134144

0 commit comments

Comments
 (0)