Title: [274479] trunk/Source
Revision
274479
Author
[email protected]
Date
2021-03-16 08:05:21 -0700 (Tue, 16 Mar 2021)

Log Message

[GTK][WPE] Stop using g_memdup
https://bugs.webkit.org/show_bug.cgi?id=223189

Reviewed by Philippe Normand.

Source/WebCore:

Add gstBufferNewWrappedFast() to create a GstBuffer wrapping data allocated with fast malloc and use it when
possible in combination with fastMemDup() instead of g_memdup().

* platform/graphics/gstreamer/GStreamerCommon.cpp:
(WebCore::gstBufferNewWrappedFast):
* platform/graphics/gstreamer/GStreamerCommon.h:
* platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp:
(CachedResourceStreamingClient::dataReceived):
* platform/mediastream/gstreamer/RealtimeIncomingAudioSourceLibWebRTC.cpp:
(WebCore::RealtimeIncomingAudioSourceLibWebRTC::OnData):
* platform/mediastream/libwebrtc/GStreamerVideoDecoderFactory.cpp:

Source/WebKit:

* UIProcess/API/glib/WebKitWebResource.cpp:
(webkit_web_resource_get_data_finish): Use g_malloc + memcpy instead of g_memdup.
* UIProcess/API/glib/WebKitWebView.cpp:
(webkit_web_view_save_finish): Use fastMemDup instead g_memdup.

Source/WTF:

Add fastMemDup() to replace g_memdup() that is now deprecated in GLib because of the possibility of overflow
when converting from size_t to unsigned int. There's a replacement in GLib already, but we would need to depend
on very new GLib version, so better use fastMemDup() when possible. In cases where we still need to use GLib
allocator, we can simply call g_malloc() + memcpy().

* wtf/FastMalloc.cpp:
(WTF::fastMemDup):
* wtf/FastMalloc.h:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (274478 => 274479)


--- trunk/Source/WTF/ChangeLog	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WTF/ChangeLog	2021-03-16 15:05:21 UTC (rev 274479)
@@ -1,3 +1,19 @@
+2021-03-16  Carlos Garcia Campos  <[email protected]>
+
+        [GTK][WPE] Stop using g_memdup
+        https://bugs.webkit.org/show_bug.cgi?id=223189
+
+        Reviewed by Philippe Normand.
+
+        Add fastMemDup() to replace g_memdup() that is now deprecated in GLib because of the possibility of overflow
+        when converting from size_t to unsigned int. There's a replacement in GLib already, but we would need to depend
+        on very new GLib version, so better use fastMemDup() when possible. In cases where we still need to use GLib
+        allocator, we can simply call g_malloc() + memcpy().
+
+        * wtf/FastMalloc.cpp:
+        (WTF::fastMemDup):
+        * wtf/FastMalloc.h:
+
 2021-03-16  Khem Raj  <[email protected]>
 
         [CMake] Build fails on RISC-V with GCC 11

Modified: trunk/Source/WTF/wtf/FastMalloc.cpp (274478 => 274479)


--- trunk/Source/WTF/wtf/FastMalloc.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WTF/wtf/FastMalloc.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -97,6 +97,16 @@
     return dup;
 }
 
+void* fastMemDup(const void* mem, size_t bytes)
+{
+    if (!mem || !bytes)
+        return nullptr;
+
+    void* result = fastMalloc(bytes);
+    memcpy(result, mem, bytes);
+    return result;
+}
+
 TryMallocReturnValue tryFastZeroedMalloc(size_t n) 
 {
     void* result;

Modified: trunk/Source/WTF/wtf/FastMalloc.h (274478 => 274479)


--- trunk/Source/WTF/wtf/FastMalloc.h	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WTF/wtf/FastMalloc.h	2021-03-16 15:05:21 UTC (rev 274479)
@@ -93,6 +93,7 @@
 WTF_EXPORT_PRIVATE void* fastCalloc(size_t numElements, size_t elementSize) RETURNS_NONNULL;
 WTF_EXPORT_PRIVATE void* fastRealloc(void*, size_t) RETURNS_NONNULL;
 WTF_EXPORT_PRIVATE char* fastStrDup(const char*) RETURNS_NONNULL;
+WTF_EXPORT_PRIVATE void* fastMemDup(const void*, size_t);
 
 WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastMalloc(size_t);
 WTF_EXPORT_PRIVATE TryMallocReturnValue tryFastZeroedMalloc(size_t);
@@ -308,6 +309,7 @@
 using WTF::fastMalloc;
 using WTF::fastMallocGoodSize;
 using WTF::fastMallocSize;
+using WTF::fastMemDup;
 using WTF::fastRealloc;
 using WTF::fastStrDup;
 using WTF::fastZeroedMalloc;

Modified: trunk/Source/WebCore/ChangeLog (274478 => 274479)


--- trunk/Source/WebCore/ChangeLog	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/ChangeLog	2021-03-16 15:05:21 UTC (rev 274479)
@@ -1,3 +1,22 @@
+2021-03-16  Carlos Garcia Campos  <[email protected]>
+
+        [GTK][WPE] Stop using g_memdup
+        https://bugs.webkit.org/show_bug.cgi?id=223189
+
+        Reviewed by Philippe Normand.
+
+        Add gstBufferNewWrappedFast() to create a GstBuffer wrapping data allocated with fast malloc and use it when
+        possible in combination with fastMemDup() instead of g_memdup().
+
+        * platform/graphics/gstreamer/GStreamerCommon.cpp:
+        (WebCore::gstBufferNewWrappedFast):
+        * platform/graphics/gstreamer/GStreamerCommon.h:
+        * platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp:
+        (CachedResourceStreamingClient::dataReceived):
+        * platform/mediastream/gstreamer/RealtimeIncomingAudioSourceLibWebRTC.cpp:
+        (WebCore::RealtimeIncomingAudioSourceLibWebRTC::OnData):
+        * platform/mediastream/libwebrtc/GStreamerVideoDecoderFactory.cpp:
+
 2021-03-16  Xabier Rodriguez Calvar  <[email protected]>
 
         [GStreamer][MSE] fix video freeze in NASA TV feed

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp (274478 => 274479)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -503,6 +503,11 @@
     return true;
 }
 
+GstBuffer* gstBufferNewWrappedFast(void* data, size_t length)
+{
+    return gst_buffer_new_wrapped_full(static_cast<GstMemoryFlags>(0), data, length, 0, length, data, fastFree);
 }
 
+}
+
 #endif // USE(GSTREAMER)

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h (274478 => 274479)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerCommon.h	2021-03-16 15:05:21 UTC (rev 274479)
@@ -300,6 +300,8 @@
     return true;
 });
 
+GstBuffer* gstBufferNewWrappedFast(void* data, size_t length);
+
 }
 
 #ifndef GST_BUFFER_DTS_OR_PTS

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp (274478 => 274479)


--- trunk/Source/WebCore/platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -1126,7 +1126,7 @@
 
     checkUpdateBlocksize(length);
 
-    GstBuffer* buffer = gst_buffer_new_wrapped(g_memdup(data, length), length);
+    GstBuffer* buffer = gstBufferNewWrappedFast(fastMemDup(data, length), length);
     gst_adapter_push(members->adapter.get(), buffer);
     stopLoaderIfNeeded(src, members);
     members->responseCondition.notifyOne();

Modified: trunk/Source/WebCore/platform/mediastream/gstreamer/RealtimeIncomingAudioSourceLibWebRTC.cpp (274478 => 274479)


--- trunk/Source/WebCore/platform/mediastream/gstreamer/RealtimeIncomingAudioSourceLibWebRTC.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/platform/mediastream/gstreamer/RealtimeIncomingAudioSourceLibWebRTC.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -65,13 +65,13 @@
     gst_audio_info_set_format(&info, format, sampleRate, numberOfChannels, NULL);
 
     auto bufferSize = GST_AUDIO_INFO_BPF(&info) * numberOfFrames;
-    gpointer bufferData = g_malloc(bufferSize);
+    gpointer bufferData = fastMalloc(bufferSize);
     if (muted())
         gst_audio_format_fill_silence(info.finfo, bufferData, bufferSize);
     else
         memcpy(bufferData, audioData, bufferSize);
 
-    auto buffer = adoptGRef(gst_buffer_new_wrapped(bufferData, bufferSize));
+    auto buffer = adoptGRef(gstBufferNewWrappedFast(bufferData, bufferSize));
     auto caps = adoptGRef(gst_audio_info_to_caps(&info));
     auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
     GStreamerAudioData data(WTFMove(sample), info);

Modified: trunk/Source/WebCore/platform/mediastream/libwebrtc/GStreamerVideoDecoderFactory.cpp (274478 => 274479)


--- trunk/Source/WebCore/platform/mediastream/libwebrtc/GStreamerVideoDecoderFactory.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebCore/platform/mediastream/libwebrtc/GStreamerVideoDecoderFactory.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -213,7 +213,7 @@
         }
 
         // FIXME- Use a GstBufferPool.
-        auto buffer = adoptGRef(gst_buffer_new_wrapped(g_memdup(inputImage.data(), inputImage.size()),
+        auto buffer = adoptGRef(gstBufferNewWrappedFast(fastMemDup(inputImage.data(), inputImage.size()),
             inputImage.size()));
         GST_BUFFER_DTS(buffer.get()) = (static_cast<guint64>(inputImage.Timestamp()) * GST_MSECOND) - m_firstBufferDts;
         GST_BUFFER_PTS(buffer.get()) = (static_cast<guint64>(renderTimeMs) * GST_MSECOND) - m_firstBufferPts;

Modified: trunk/Source/WebKit/ChangeLog (274478 => 274479)


--- trunk/Source/WebKit/ChangeLog	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebKit/ChangeLog	2021-03-16 15:05:21 UTC (rev 274479)
@@ -1,3 +1,15 @@
+2021-03-16  Carlos Garcia Campos  <[email protected]>
+
+        [GTK][WPE] Stop using g_memdup
+        https://bugs.webkit.org/show_bug.cgi?id=223189
+
+        Reviewed by Philippe Normand.
+
+        * UIProcess/API/glib/WebKitWebResource.cpp:
+        (webkit_web_resource_get_data_finish): Use g_malloc + memcpy instead of g_memdup.
+        * UIProcess/API/glib/WebKitWebView.cpp:
+        (webkit_web_view_save_finish): Use fastMemDup instead g_memdup.
+
 2021-03-16  Khem Raj  <[email protected]>
 
         [CMake] Build fails on RISC-V with GCC 11

Modified: trunk/Source/WebKit/UIProcess/API/glib/WebKitWebResource.cpp (274478 => 274479)


--- trunk/Source/WebKit/UIProcess/API/glib/WebKitWebResource.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebKit/UIProcess/API/glib/WebKitWebResource.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -401,15 +401,22 @@
  */
 guchar* webkit_web_resource_get_data_finish(WebKitWebResource* resource, GAsyncResult* result, gsize* length, GError** error)
 {
-    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(resource), 0);
-    g_return_val_if_fail(g_task_is_valid(result, resource), 0);
+    g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(resource), nullptr);
+    g_return_val_if_fail(g_task_is_valid(result, resource), nullptr);
 
     GTask* task = G_TASK(result);
     if (!g_task_propagate_boolean(task, error))
-        return 0;
+        return nullptr;
 
     ResourceGetDataAsyncData* data = ""
     if (length)
         *length = data->webData->size();
-    return static_cast<guchar*>(g_memdup(data->webData->bytes(), data->webData->size()));
+
+    auto* bytes = data->webData->bytes();
+    if (!bytes || !data->webData->size())
+        return nullptr;
+
+    auto* returnValue = g_malloc(data->webData->size());
+    memcpy(returnValue, bytes, data->webData->size());
+    return static_cast<guchar*>(returnValue);
 }

Modified: trunk/Source/WebKit/UIProcess/API/glib/WebKitWebView.cpp (274478 => 274479)


--- trunk/Source/WebKit/UIProcess/API/glib/WebKitWebView.cpp	2021-03-16 15:04:10 UTC (rev 274478)
+++ trunk/Source/WebKit/UIProcess/API/glib/WebKitWebView.cpp	2021-03-16 15:05:21 UTC (rev 274479)
@@ -4196,7 +4196,7 @@
     ViewSaveAsyncData* data = ""
     gsize length = data->webData->size();
     if (length)
-        g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(dataStream), g_memdup(data->webData->bytes(), length), length, g_free);
+        g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(dataStream), fastMemDup(data->webData->bytes(), length), length, fastFree);
 
     return dataStream;
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to