<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>261497</bug_id>
          
          <creation_ts>2023-09-13 00:51:22 -0700</creation_ts>
          <short_desc>REGRESSION(r255164) [PlayStation] WTFReportBacktrace tries to print backtrace even when backtrace cannot be obtained and crashes</short_desc>
          <delta_ts>2023-09-19 06:17:14 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Web Template Framework</component>
          <version>WebKit Local Build</version>
          <rep_platform>Other</rep_platform>
          <op_sys>Other</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=245826</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Tomoki Imai">tomoki.imai</reporter>
          <assigned_to name="Tomoki Imai">tomoki.imai</assigned_to>
          <cc>fujii</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1977234</commentid>
    <comment_count>0</comment_count>
    <who name="Tomoki Imai">tomoki.imai</who>
    <bug_when>2023-09-13 00:51:22 -0700</bug_when>
    <thetext>When !HAVE(BACKTRACE) &amp;&amp; !OS(WINDOWS),
- WTFGetBacktrace(samples, &amp;frames) make frames = 0
- WTFReportBacktraceWithPrefixAndPrintStream passes -2 (=frames-framesToSkip) to WTFPrintBacktraceWithPrefixAndPrintStream.
- WTFPrintBacktraceWithPrefixAndPrintStream static_cast -2 to size_t, which can overflow and make large number.
- It possibly tries to print the large stack and eventually crashes.

void WTFReportBacktraceWithPrefixAndPrintStream(PrintStream&amp; out, const char* prefix)
{
    static constexpr int framesToShow = 31;
    static constexpr int framesToSkip = 2;
    void* samples[framesToShow + framesToSkip];
    int frames = framesToShow + framesToSkip;

    WTFGetBacktrace(samples, &amp;frames);
    WTFPrintBacktraceWithPrefixAndPrintStream(out, samples + framesToSkip, frames - framesToSkip, prefix);
}

https://github.com/WebKit/WebKit/blob/f33e99829e4f572a15eb8c2a6ca3d78fa227e9cc/Source/WTF/wtf/Assertions.cpp#L298-L307

void WTFGetBacktrace(void** stack, int* size)
{
#if HAVE(BACKTRACE)
    *size = backtrace(stack, *size);
#elif OS(WINDOWS)
    *size = RtlCaptureStackBackTrace(0, *size, stack, nullptr);
#else
    UNUSED_PARAM(stack);
    *size = 0;
#endif
}

https://github.com/WebKit/WebKit/blob/f33e99829e4f572a15eb8c2a6ca3d78fa227e9cc/Source/WTF/wtf/StackTrace.cpp#L34-L44

void WTFPrintBacktraceWithPrefixAndPrintStream(PrintStream&amp; out, void** stack, int size, const char* prefix)
{
    out.print(StackTracePrinter { { stack, static_cast&lt;size_t&gt;(size) }, prefix });
}

https://github.com/WebKit/WebKit/blob/f33e99829e4f572a15eb8c2a6ca3d78fa227e9cc/Source/WTF/wtf/Assertions.cpp#L309-L312</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1977235</commentid>
    <comment_count>1</comment_count>
    <who name="Tomoki Imai">tomoki.imai</who>
    <bug_when>2023-09-13 00:52:52 -0700</bug_when>
    <thetext>Note: 

WTF::StackTrace::captureStackTrace has similar code, but it blocks the code by &quot;static_cast&lt;size_t&gt;(capturedFrames) &gt; framesToSkip&quot;.

std::unique_ptr&lt;StackTrace&gt; StackTrace::captureStackTrace(size_t maxFrames, size_t framesToSkip)
{
    static_assert(sizeof(StackTrace) == sizeof(void*) * 3);
    // We overwrite the memory of the two first skipped frames, m_stack[0] will hold the third one.
    static_assert(offsetof(StackTrace, m_stack) == sizeof(void*) * 2);

    maxFrames = std::max&lt;size_t&gt;(1, maxFrames);
    // Skip 2 additional frames i.e. StackTrace::captureStackTrace and WTFGetBacktrace.
    framesToSkip += 2;
    size_t capacity = maxFrames + framesToSkip;
    void** storage = static_cast&lt;void**&gt;(fastMalloc(capacity * sizeof(void*)));
    size_t size = 0;
    size_t initialFrame = 0;
    int capturedFrames = static_cast&lt;int&gt;(capacity);
    WTFGetBacktrace(storage, &amp;capturedFrames);
    if (static_cast&lt;size_t&gt;(capturedFrames) &gt; framesToSkip) {
        size = static_cast&lt;size_t&gt;(capturedFrames) - framesToSkip;
        initialFrame = framesToSkip - 2; 
    }
    return std::unique_ptr&lt;StackTrace&gt; { new (NotNull, storage) StackTrace(size, initialFrame) };
}

https://github.com/WebKit/WebKit/blob/f33e99829e4f572a15eb8c2a6ca3d78fa227e9cc/Source/WTF/wtf/StackTrace.cpp#L48-L68</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1977240</commentid>
    <comment_count>2</comment_count>
    <who name="Tomoki Imai">tomoki.imai</who>
    <bug_when>2023-09-13 01:39:16 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/17726</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1978261</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2023-09-19 06:16:41 -0700</bug_when>
    <thetext>Committed 268121@main (a4279526dfa5): &lt;https://commits.webkit.org/268121@main&gt;

Reviewed commits have been landed. Closing PR #17726 and removing active labels.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1978262</commentid>
    <comment_count>4</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2023-09-19 06:17:14 -0700</bug_when>
    <thetext>&lt;rdar://problem/115721690&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>