[llvm] [llubi] Validate argument types in `printf` (PR #229702)

Zhige Chen via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 03:55:04 PDT 2026


https://github.com/nofe1248 updated https://github.com/llvm/llvm-project/pull/229702

>From d8d7b1c407d42156db76d8589a3f4f4ee253fe42 Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Wed, 7 Oct 2026 17:14:17 +0800
Subject: [PATCH 1/3] [llubi] Validate argument types in `printf`

---
 .../tools/llubi/lib_printf_type_mismatch.ll   | 33 +++++++++++++++++++
 llvm/tools/llubi/lib/Library.cpp              | 12 +++++++
 2 files changed, 45 insertions(+)
 create mode 100644 llvm/test/tools/llubi/lib_printf_type_mismatch.ll

diff --git a/llvm/test/tools/llubi/lib_printf_type_mismatch.ll b/llvm/test/tools/llubi/lib_printf_type_mismatch.ll
new file mode 100644
index 0000000000000..d7bd7fa3e1beb
--- /dev/null
+++ b/llvm/test/tools/llubi/lib_printf_type_mismatch.ll
@@ -0,0 +1,33 @@
+; RUN: not llubi --entry-function=integer < %s 2>&1 | FileCheck %s --check-prefix=INTEGER
+; RUN: not llubi --entry-function=floating < %s 2>&1 | FileCheck %s --check-prefix=FLOAT
+; RUN: not llubi --entry-function=count < %s 2>&1 | FileCheck %s --check-prefix=COUNT
+
+ at fmt_integer = constant [3 x i8] c"%d\00"
+ at fmt_float = constant [3 x i8] c"%f\00"
+ at fmt_count = constant [3 x i8] c"%n\00"
+
+declare i32 @printf(ptr, ...)
+
+define void @integer() {
+  call i32 (ptr, ...) @printf(ptr @fmt_integer, ptr null)
+  ret void
+}
+
+; INTEGER: Immediate UB detected: Argument type mismatch in printf for format specifier 'd' at argument index 1.
+; INTEGER-NEXT: error: Execution of function 'integer' failed.
+
+define void @floating() {
+  call i32 (ptr, ...) @printf(ptr @fmt_float, i32 0)
+  ret void
+}
+
+; FLOAT: Immediate UB detected: Argument type mismatch in printf for format specifier 'f' at argument index 1.
+; FLOAT-NEXT: error: Execution of function 'floating' failed.
+
+define void @count() {
+  call i32 (ptr, ...) @printf(ptr @fmt_count, i32 0)
+  ret void
+}
+
+; COUNT: Immediate UB detected: Argument type mismatch in printf for format specifier 'n' at argument index 1.
+; COUNT-NEXT: error: Execution of function 'count' failed.
diff --git a/llvm/tools/llubi/lib/Library.cpp b/llvm/tools/llubi/lib/Library.cpp
index c3452b8af92c0..51b04a5572bdb 100644
--- a/llvm/tools/llubi/lib/Library.cpp
+++ b/llvm/tools/llubi/lib/Library.cpp
@@ -239,6 +239,18 @@ AnyValue Library::executePrintf(ArrayRef<AnyValue> Args) {
       return AnyValue::poison();
     }
 
+    bool TypeMismatch =
+        (StringRef("diuoxXc").contains(Specifier) && !Arg.isInteger()) ||
+        (StringRef("feEgG").contains(Specifier) && !Arg.isFloat()) ||
+        (StringRef("nps").contains(Specifier) && !Arg.isPointer());
+
+    if (TypeMismatch) {
+      Executor.reportImmediateUB()
+          << "Argument type mismatch in printf for format specifier '"
+          << Specifier << "' at argument index " << (ArgIndex - 1) << ".";
+      return AnyValue::poison();
+    }
+
     switch (Specifier) {
     case 'd':
     case 'i': {

>From 05f3478cc81e0259992b7184b561661771b2d8ed Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Wed, 7 Oct 2026 17:58:28 +0800
Subject: [PATCH 2/3] [llubi] Address comments

---
 llvm/tools/llubi/lib/Library.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/tools/llubi/lib/Library.cpp b/llvm/tools/llubi/lib/Library.cpp
index 51b04a5572bdb..e9ce6ecdc40e9 100644
--- a/llvm/tools/llubi/lib/Library.cpp
+++ b/llvm/tools/llubi/lib/Library.cpp
@@ -241,7 +241,9 @@ AnyValue Library::executePrintf(ArrayRef<AnyValue> Args) {
 
     bool TypeMismatch =
         (StringRef("diuoxXc").contains(Specifier) && !Arg.isInteger()) ||
-        (StringRef("feEgG").contains(Specifier) && !Arg.isFloat()) ||
+        (StringRef("feEgGaA").contains(Specifier) &&
+         !(Arg.isFloat() &&
+           &Arg.asFloat().getSemantics() == &APFloat::IEEEdouble())) ||
         (StringRef("nps").contains(Specifier) && !Arg.isPointer());
 
     if (TypeMismatch) {

>From fb6e19e46654f6677db6b6ddee99a4ef843c57aa Mon Sep 17 00:00:00 2001
From: Zhige Chen <zhigec_cpp at outlook.com>
Date: Wed, 7 Oct 2026 18:54:33 +0800
Subject: [PATCH 3/3] [llubi] One more test

---
 .../test/tools/llubi/lib_printf_float_type.ll | 82 +++++++++++++++++++
 1 file changed, 82 insertions(+)
 create mode 100644 llvm/test/tools/llubi/lib_printf_float_type.ll

diff --git a/llvm/test/tools/llubi/lib_printf_float_type.ll b/llvm/test/tools/llubi/lib_printf_float_type.ll
new file mode 100644
index 0000000000000..5f84c9b18a453
--- /dev/null
+++ b/llvm/test/tools/llubi/lib_printf_float_type.ll
@@ -0,0 +1,82 @@
+; RUN: llubi < %s 2>&1 | FileCheck %s --check-prefix=DOUBLE
+; RUN: not llubi --entry-function=half_arg < %s 2>&1 | FileCheck %s --check-prefix=HALF
+; RUN: not llubi --entry-function=bfloat_arg < %s 2>&1 | FileCheck %s --check-prefix=BFLOAT
+; RUN: not llubi --entry-function=float_arg < %s 2>&1 | FileCheck %s --check-prefix=FLOAT
+; RUN: not llubi --entry-function=fp80_arg < %s 2>&1 | FileCheck %s --check-prefix=FP80
+; RUN: not llubi --entry-function=fp128_arg < %s 2>&1 | FileCheck %s --check-prefix=FP128
+; RUN: not llubi --entry-function=ppc_fp128_arg < %s 2>&1 | FileCheck %s --check-prefix=PPC-FP128
+; RUN: not llubi --entry-function=float_upper_hex_arg < %s 2>&1 | FileCheck %s --check-prefix=FLOAT-UPPER-HEX
+
+ at fmt_all = constant [22 x i8] c"%f %e %E %g %G %a %A\0A\00"
+ at fmt_f = constant [3 x i8] c"%f\00"
+ at fmt_e = constant [3 x i8] c"%e\00"
+ at fmt_E = constant [3 x i8] c"%E\00"
+ at fmt_g = constant [3 x i8] c"%g\00"
+ at fmt_G = constant [3 x i8] c"%G\00"
+ at fmt_a = constant [3 x i8] c"%a\00"
+ at fmt_A = constant [3 x i8] c"%A\00"
+
+declare i32 @printf(ptr, ...)
+
+define void @main() {
+  call i32 (ptr, ...) @printf(ptr @fmt_all, double 1.0, double 1.0, double 1.0, double 1.0, double 1.0, double 1.0, double 1.0)
+  ret void
+}
+
+; DOUBLE: 1.000000 1.000000e+00 1.000000E+00 1 1 {{0x1(\.0+)?p\+0+}} {{0X1(\.0+)?P\+0+}}
+
+define void @half_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_f, half 0.0)
+  ret void
+}
+
+; HALF: Immediate UB detected: Argument type mismatch in printf for format specifier 'f' at argument index 1.
+; HALF-NEXT: error: Execution of function 'half_arg' failed.
+
+define void @bfloat_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_e, bfloat 0.0)
+  ret void
+}
+
+; BFLOAT: Immediate UB detected: Argument type mismatch in printf for format specifier 'e' at argument index 1.
+; BFLOAT-NEXT: error: Execution of function 'bfloat_arg' failed.
+
+define void @float_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_E, float 0.0)
+  ret void
+}
+
+; FLOAT: Immediate UB detected: Argument type mismatch in printf for format specifier 'E' at argument index 1.
+; FLOAT-NEXT: error: Execution of function 'float_arg' failed.
+
+define void @fp80_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_g, x86_fp80 0xK00000000000000000000)
+  ret void
+}
+
+; FP80: Immediate UB detected: Argument type mismatch in printf for format specifier 'g' at argument index 1.
+; FP80-NEXT: error: Execution of function 'fp80_arg' failed.
+
+define void @fp128_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_G, fp128 0xL00000000000000000000000000000000)
+  ret void
+}
+
+; FP128: Immediate UB detected: Argument type mismatch in printf for format specifier 'G' at argument index 1.
+; FP128-NEXT: error: Execution of function 'fp128_arg' failed.
+
+define void @ppc_fp128_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_a, ppc_fp128 0xM00000000000000000000000000000000)
+  ret void
+}
+
+; PPC-FP128: Immediate UB detected: Argument type mismatch in printf for format specifier 'a' at argument index 1.
+; PPC-FP128-NEXT: error: Execution of function 'ppc_fp128_arg' failed.
+
+define void @float_upper_hex_arg() {
+  call i32 (ptr, ...) @printf(ptr @fmt_A, float 0.0)
+  ret void
+}
+
+; FLOAT-UPPER-HEX: Immediate UB detected: Argument type mismatch in printf for format specifier 'A' at argument index 1.
+; FLOAT-UPPER-HEX-NEXT: error: Execution of function 'float_upper_hex_arg' failed.



More information about the llvm-commits mailing list