Add format attribute to printf-style wrappers and fix format string errors - #123920
Add format attribute to printf-style wrappers and fix format string errors#123920jkoritzinsky with Copilot wants to merge 71 commits into
Conversation
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…, str) pattern Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 206 out of 206 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (3)
src/coreclr/debug/ee/debuggermodule.cpp:127
- Using a literal "0x" prefix with
%pcan lead to duplicated prefixes on platforms where%palready includes0x(resulting in0x0x...). Drop the manual prefix when printing pointers with%p.
LOG((LF_CORDB, LL_INFO1000, "DMT::RM Attempting to remove Module:0x%p\n", pModule));
src/coreclr/debug/ee/debuggermodule.cpp:144
- Using a literal "0x" prefix with
%pcan lead to duplicated prefixes on platforms where%palready includes0x(resulting in0x0x...). Drop the manual prefix when printing pointers with%p.
LOG((LF_CORDB, LL_INFO1000, "DMT::RM Removing DebuggerMod:0x%p - Module:0x%p Asm:0x%p\n",
pDM, pModule, pDM->GetAssembly()));
src/coreclr/debug/ee/debuggermodule.cpp:153
- Using a literal "0x" prefix with
%pcan lead to duplicated prefixes on platforms where%palready includes0x(resulting in0x0x...). Drop the manual prefix when printing pointers with%p.
LOG((LF_CORDB, LL_INFO1000, "DMT::RM No debugger module found for Module:0x%p\n", pModule));
| extern const std::string GetFileName(const std::string& fileName); | ||
| extern const std::string GetDirectory(const std::string& fileName); | ||
| extern std::string FormatString(const char* format, ...); | ||
| extern std::string FormatString(const char* format, ...) MINIPAL_ATTR_FORMAT_PRINTF(1, 2); | ||
| extern std::string ConvertString(const WCHAR* str); |
|
@copilot please triage and fix the build errors encountered in CI. |
…e.cpp and crashinfo.h Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Fixed the CI build failures in commit
Also addressed the unresolved review comments from the latest review:
|
|
@copilot please triage the CI build failures, merge from main, and implement fixes for the CI build failures. |
…tribute-to-wrappers # Conflicts: # src/coreclr/vm/method.cpp Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Done. I merged the latest Validation completed successfully with both Note This reply was generated by GitHub Copilot. |
|
@copilot please triage the CI build failures, merge from main, and implement fixes for the CI build failures. |
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 208 out of 208 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/coreclr/vm/tieredcompilation.cpp:939
- With printf-format checking enabled on LOG/LogSpew,
%p(and%pM, which is parsed as%p+ literalM) requires the corresponding argument to be passed asvoid*. HerepMethodis aMethodDesc*, so this remains varargs-UB and is likely to trigger-Wformatdiagnostics on GCC/Clang builds that compile withLOGGINGenabled.
src/mono/mono/mini/tramp-amd64.c:602 g_assertfultimately uses printf-style formatting;%pexpects avoid*argument. Passingcode/bufas typed pointers is varargs-UB and (with format attributes enabled on the GLib-style helpers) is likely to reintroduce the format diagnostics this PR is trying to eliminate.
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 208 out of 208 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/coreclr/debug/di/divalue.cpp:813
- Using a literal
0xprefix together with%pcan produce0x0x...on platforms where%palready includes the0xprefix (common on Unix/libc). Prefer plain%pfor portability/consistency with other fixes in this PR.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::~CRV: this:0x%p\n",this));
src/coreclr/debug/di/divalue.cpp:744
- Using a literal
0xprefix together with%pcan produce0x0x...on platforms where%palready includes the0xprefix (common on Unix/libc). Prefer plain%pfor portability/consistency with other fixes in this PR.
This issue also appears on line 813 of the same file.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::CRV: this:0x%p\n",this));
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 210 out of 210 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/coreclr/debug/di/divalue.cpp:813
- Same as above: avoid hardcoding
0xbefore%pto prevent potential0x0x...output and keep pointer formatting consistent.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::~CRV: this:0x%p\n",this));
src/coreclr/debug/di/divalue.cpp:744
%pformatting is implementation-defined but commonly already includes a0xprefix. Keeping the literal0xhere can result in0x0x...in logs, which hurts diagnostics consistency (and this PR already removes redundant0xprefixes elsewhere).
This issue also appears on line 813 of the same file.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::CRV: this:0x%p\n",this));
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 210 out of 210 changed files in this pull request and generated no new comments.
Suppressed comments (4)
src/coreclr/debug/di/divalue.cpp:813
- Same issue as the constructor log:
%pexpectsvoid*(cast required for varargs correctness / format checking), and the0xprefix can produce0x0x...on some platforms.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::~CRV: this:0x%p\n",this));
src/coreclr/debug/di/rsmain.cpp:1333
- Same
%pargument typing issue: castprocesstovoid*to avoid format diagnostics under GCC/Clang with printf-style validation enabled.
STRESS_LOG1(LF_CORDB, LL_INFO10, "Cordb::RemoveProcess %p...\n", process);
src/coreclr/debug/di/divalue.cpp:744
%pformat specifier requires avoid*argument. Passingthis(a typed pointer) can trigger-Wformatdiagnostics once printf-style validation is enabled and is technically incorrect for varargs. Also the literal0xprefix can result in0x0x...output on platforms where%palready includes the prefix.
This issue also appears on line 813 of the same file.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::CRV: this:0x%p\n",this));
src/coreclr/debug/di/rsmain.cpp:1308
%prequires avoid*argument for printf-style format checking; passing a typed pointer can trigger-Wformatwarnings/errors with the new format attributes. Cast the argument tovoid*for correctness and portability.
This issue also appears on line 1333 of the same file.
STRESS_LOG1(LF_CORDB, LL_INFO10, "Cordb::AddProcess %p...\n", process);
|
Azure Pipelines: Successfully started running 10 pipeline(s). 6 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 210 out of 210 changed files in this pull request and generated no new comments.
Suppressed comments (4)
src/coreclr/vm/peimagelayout.cpp:304
- This LOG call uses %p but passes GetPreferredBase()/GetBase() without casting to a pointer type. In this file these values are treated as pointer-sized integers (e.g., preferredBase = (void*)GetPreferredBase()), so this will trip format checking (and is UB). Cast to void* at the call site.
src/coreclr/debug/di/divalue.cpp:813 - Same as the constructor: the explicit "0x" prefix with %p will commonly result in "0x0x..." output. Drop the literal prefix here as well.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::~CRV: this:0x%p\n",this));
src/coreclr/inc/stresslog.h:316
- StressLog::LogMsg stores varargs based on cArgs. LogMsgOL currently passes cArgs=0 but also passes one vararg ("%s", format), so the argument is not stored and later formatting of "%s" will read a missing argument (undefined behavior / corrupted stress log output).
src/coreclr/debug/di/divalue.cpp:744 - Using the literal prefix "0x" with %p will typically produce duplicated prefixes ("0x0x...") on platforms where %p already includes 0x. This file only has these two occurrences (constructor and destructor); consider dropping the explicit "0x" in both places for consistent output.
This issue also appears on line 813 of the same file.
LOG((LF_CORDB,LL_EVERYTHING,"CRV::CRV: this:0x%p\n",this));
Description
Enabled compiler validation of printf-style format strings by adding
__attribute__ ((format (printf, ...)))to wrapper functions. Fixed 85+ format string errors discovered by the compiler across CoreCLR, Mono, and Corehost. Merged with latest main branch to ensure compatibility with recent changes.Changes
Centralized format attribute macro in
src/native/minipal/types.h:MINIPAL_ATTR_FORMAT_PRINTF(fmt_pos, arg_pos)macro for consistent usage across the codebase#ifdef __GNUC__blocks throughout the codebaseAdded format attributes to 60+ wrapper functions across 13 headers using
MINIPAL_ATTR_FORMAT_PRINTF:src/native/minipal/types.h- CentralizedMINIPAL_ATTR_FORMAT_PRINTFmacro definitionsrc/native/minipal/log.h-minipal_log_printsrc/native/corehost/hostmisc/trace.h-trace::verbose,info,warning,error,printlnsrc/native/libs/Common/pal_compiler.h-do_abort_unlesssrc/native/libs/System.Native/pal_string.h-SystemNative_SNPrintFsrc/native/libs/System.Security.Cryptography.Native/osslcompat_30.h-ERR_set_error(uses sharedMINIPAL_ATTR_FORMAT_PRINTFmacro)src/coreclr/inc/log.h-LogSpew,LogSpew2,LogSpewAlwayssrc/coreclr/inc/stresslog.h-StressLog::LogMsg,ThreadStressLog::LogMsgsrc/coreclr/inc/sstring.h-Printf,AppendPrintfsrc/coreclr/jit/host.h-jitprintf,logf,flogf,gcDump_logfsrc/coreclr/jit/compiler.h-printfAlloc,JitLogEEsrc/coreclr/gc/gc.h-GCLogsrc/coreclr/gc/gcpriv.h-GCLogConfigsrc/mono/mono/eglib/glib.h-g_error_new,g_set_error,g_print,g_printerr,g_log,g_assertion_message,g_async_safe_*Fixed 85+ format string errors across 24 source files:
%I64d/%Id→%zd/%zuPRIX64/PRIx64/PRIu64from<inttypes.h>#and0flags(void*)cast for pointersprintf(str)→printf("%s", str)thisFiles with format errors fixed:
Added missing header includes:
src/coreclr/ildasm/ildasmpch.h- Added#include <inttypes.h>for PRI macrossrc/coreclr/tools/metainfo/mdinfo.cpp- Added#include <inttypes.h>for PRI macrossrc/coreclr/vm/jitinterface.cpp- Added#include <inttypes.h>for PRI macrossrc/coreclr/jit/gentree.cpp- Added#include <inttypes.h>for PRI macrosPortability for PRI macros in C++: Added guarded
__STDC_FORMAT_MACROSdefine before<inttypes.h>includes in C++ translation units that use PRI* format macros (emitwasm.cpp, jitinterface.cpp, stubgen.cpp, mdinfo.cpp, ildasmpch.h, arm64/loongarch64/riscv64 singlestepper.cpp). This keeps PRI macros visible on older C++ libc implementations (notably glibc) where they are gated behind__STDC_FORMAT_MACROSin C++ mode.Merged from main:
Review feedback addressed:
src/coreclr/vm/crst.cpp: Simplified format strings to avoid unnecessary line splitssrc/coreclr/gc/diagnostics.cpp: Removed unnecessary size_t casts (type already size_t)%zuinstead of%zdfor unsigned size_t%lldback to%dfor INT32 typeprintf("")calls withfflush(stdout)to fix GCCformat-zero-lengtherrors on linux.armel.Checkedsrc/coreclr/vm/gcheaputilities.cpp: Log invalid GC module name as UTF-8 string (MAKE_UTF8PTR_FROMWIDE+%s) instead of pointer (%p) so the diagnostic shows the actual namesrc/coreclr/jit/inlinepolicy.cpp: Printm_ModelCodeSizeEstimatefor thesize=label (the original code had a label/value mismatch where the per-call instruction estimate was being printed under thesizelabel)src/native/corehost/hostpolicy/hostpolicy_init.cpp: Changed%zd→%zuforsize_t input->version_loin two locationssrc/mono/mono/utils/mono-os-mutex.c: Castts.tv_sectolong longfor%lldandts.tv_nsectolongfor%ldin bothpthread_cond_timedwaitandpthread_cond_timedwait_relative_npbranchessrc/native/libs/System.Security.Cryptography.Native/osslcompat_30.h: Replaced inline#ifdef __GNUC__block onERR_set_errorwith the sharedMINIPAL_ATTR_FORMAT_PRINTFmacro and added#include <minipal/types.h>src/native/corehost/bundle/reader.h: Castint64_t m_offset_in_filetounsigned long longto match%llx(avoids varargs UB)src/coreclr/vm/stubgen.cpp: Use0x%zx/0x%08zxwith(size_t)cast forUINT_PTR pInstruction->uArgto fix Win64 truncation (whereunsigned longis 32-bit)src/coreclr/vm/threadsuspend.cpp:4312andsrc/coreclr/vm/amd64/excepamd64.cpp:197: Removed redundant0xliteral prefix before%pformat specifier (which already includes0xon most platforms, resulting in0x0x...output)src/coreclr/debug/di/rsthread.cpp:692: Added explicit(size_t)cast forUINT_PTR m_idused with%zxformat specifiersrc/tools/ilasm: Reverted accidental changes to auto-generated C# files that had introduced Windows-specific absolute paths into// Generated fromcommentsExample fixes:
Impact
#ifdef __GNUC__blocks via centralizedMINIPAL_ATTR_FORMAT_PRINTFmacro (now also used byosslcompat_30.h); restored original indentation to keep git history clean; reverted accidental changes to auto-generatedsrc/tools/ilasmfiles__STDC_FORMAT_MACROSbefore<inttypes.h>in C++ files for older libc compatibility; fixedUINT_PTRformatting in stubgen for Win64 (whereunsigned longis 32-bit); fixed builds across x64, ARM64, and 32-bit ARM platformsgcheaputilities.cpp(UTF-8 module name instead of pointer); fixed a pre-existing label/value mismatch ininlinepolicy.cppso thesize=log label now reports the model code size; removed redundant0xprefixes before%pspecifiers that were producing0x0x...output💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.