Title: [284533] trunk
Revision
284533
Author
commit-qu...@webkit.org
Date
2021-10-20 08:04:26 -0700 (Wed, 20 Oct 2021)

Log Message

Do not use strerror()
https://bugs.webkit.org/show_bug.cgi?id=231913

Patch by Michael Catanzaro <mcatanz...@gnome.org> on 2021-10-20
Reviewed by Chris Dumez.

Source/bmalloc:

* libpas/src/libpas/pas_page_malloc.c:
(pas_page_malloc_commit):

Source/_javascript_Core:

* API/JSScript.mm:
(-[JSScript writeCache:]):
* API/tests/testapi.mm:
(resolvePathToScripts):
* jsc.cpp:
* runtime/BytecodeCacheError.cpp:
(JSC::BytecodeCacheError::StandardError::message const):
* tools/FunctionAllowlist.cpp:
(JSC::FunctionAllowlist::FunctionAllowlist):
* tools/FunctionOverrides.cpp:
(JSC::FunctionOverrides::parseOverridesInFile):
* wasm/WasmMemory.cpp:
(JSC::Wasm::MemoryHandle::~MemoryHandle):
(JSC::Wasm::Memory::tryCreate):
(JSC::Wasm::Memory::growShared):
(JSC::Wasm::Memory::grow):

Source/WebKit:

* Platform/IPC/unix/ConnectionUnix.cpp:
(IPC::Connection::readyReadHandler):
(IPC::Connection::sendOutputMessage):
* Platform/unix/SharedMemoryUnix.cpp:
(WebKit::SharedMemory::allocate):
* Shared/mac/AuxiliaryProcessMac.mm:
(WebKit::setAndSerializeSandboxParameters):
(WebKit::sandboxDataVaultParentDirectory):
(WebKit::ensureSandboxCacheDirectory):
(WebKit::tryApplyCachedSandbox):
(WebKit::applySandbox):

Source/WTF:

Add a new safeStrerror function that we can use without worrying about thread safety.

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/SafeStrerror.cpp: Added.
(WTF::safeStrerror):
* wtf/SafeStrerror.h: Added.
* wtf/linux/RealTimeThreads.cpp:
(WTF::RealTimeThreads::promoteThreadToRealTime):
* wtf/playstation/FileSystemPlayStation.cpp:
(WTF::FileSystemImpl::deleteFile):
* wtf/posix/FileSystemPOSIX.cpp:
(WTF::FileSystemImpl::deleteFile):
* wtf/posix/ThreadingPOSIX.cpp:
(WTF::Thread::establishHandle):
* wtf/threads/Signals.cpp:
(WTF::jscSignalHandler):

Tools:

* TestWebKitAPI/CMakeLists.txt:
* TestWebKitAPI/Tests/WTF/SafeStrerror.cpp: Added.
(TestWebKitAPI::TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/API/JSScript.mm (284532 => 284533)


--- trunk/Source/_javascript_Core/API/JSScript.mm	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/API/JSScript.mm	2021-10-20 15:04:26 UTC (rev 284533)
@@ -40,6 +40,7 @@
 #import <sys/stat.h>
 #import <wtf/FileSystem.h>
 #import <wtf/SHA1.h>
+#import <wtf/SafeStrerror.h>
 #import <wtf/Scope.h>
 #import <wtf/WeakObjCPtr.h>
 #import <wtf/spi/darwin/DataVaultSPI.h>
@@ -304,7 +305,7 @@
     const char* tempFileName = [cachePathString stringByAppendingString:@".tmp"].UTF8String;
     int fd = open(cacheFileName, O_CREAT | O_WRONLY | O_EXLOCK | O_NONBLOCK, 0600);
     if (fd == -1) {
-        error = makeString("Could not open or lock the bytecode cache file. It's likely another VM or process is already using it. Error: ", strerror(errno));
+        error = makeString("Could not open or lock the bytecode cache file. It's likely another VM or process is already using it. Error: ", safeStrerror(errno).data());
         return NO;
     }
 
@@ -314,7 +315,7 @@
 
     int tempFD = open(tempFileName, O_CREAT | O_RDWR | O_EXLOCK | O_NONBLOCK, 0600);
     if (tempFD == -1) {
-        error = makeString("Could not open or lock the bytecode cache temp file. Error: ", strerror(errno));
+        error = makeString("Could not open or lock the bytecode cache temp file. Error: ", safeStrerror(errno).data());
         return NO;
     }
 

Modified: trunk/Source/_javascript_Core/API/tests/testapi.mm (284532 => 284533)


--- trunk/Source/_javascript_Core/API/tests/testapi.mm	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/API/tests/testapi.mm	2021-10-20 15:04:26 UTC (rev 284533)
@@ -39,6 +39,7 @@
 #import "JSWrapperMapTests.h"
 #import "Regress141275.h"
 #import "Regress141809.h"
+#import <wtf/SafeStrerror.h>
 #import <wtf/spi/darwin/DataVaultSPI.h>
 
 
@@ -2448,7 +2449,7 @@
         const size_t maxLength = 10000;
         char cwd[maxLength];
         if (!getcwd(cwd, maxLength)) {
-            NSLog(@"getcwd errored with code: %s", strerror(errno));
+            NSLog(@"getcwd errored with code: %s", safeStrerror(errno).data());
             exit(1);
         }
         NSURL *cwdURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%s", cwd]];

Modified: trunk/Source/_javascript_Core/ChangeLog (284532 => 284533)


--- trunk/Source/_javascript_Core/ChangeLog	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-10-20 15:04:26 UTC (rev 284533)
@@ -1,5 +1,29 @@
 2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
 
+        Do not use strerror()
+        https://bugs.webkit.org/show_bug.cgi?id=231913
+
+        Reviewed by Chris Dumez.
+
+        * API/JSScript.mm:
+        (-[JSScript writeCache:]):
+        * API/tests/testapi.mm:
+        (resolvePathToScripts):
+        * jsc.cpp:
+        * runtime/BytecodeCacheError.cpp:
+        (JSC::BytecodeCacheError::StandardError::message const):
+        * tools/FunctionAllowlist.cpp:
+        (JSC::FunctionAllowlist::FunctionAllowlist):
+        * tools/FunctionOverrides.cpp:
+        (JSC::FunctionOverrides::parseOverridesInFile):
+        * wasm/WasmMemory.cpp:
+        (JSC::Wasm::MemoryHandle::~MemoryHandle):
+        (JSC::Wasm::Memory::tryCreate):
+        (JSC::Wasm::Memory::growShared):
+        (JSC::Wasm::Memory::grow):
+
+2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
+
         Suppress a -Wreturn-type warning
         https://bugs.webkit.org/show_bug.cgi?id=229681
         <rdar://81603387>

Modified: trunk/Source/_javascript_Core/jsc.cpp (284532 => 284533)


--- trunk/Source/_javascript_Core/jsc.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/jsc.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -88,6 +88,7 @@
 #include <wtf/MainThread.h>
 #include <wtf/MemoryPressureHandler.h>
 #include <wtf/MonotonicTime.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/Scope.h>
 #include <wtf/StringPrintStream.h>
 #include <wtf/URL.h>
@@ -1819,7 +1820,7 @@
 
     FILE* descriptor = fopen(filePath.fileSystemPath().ascii().data(), "r");
     if (!descriptor)
-        return throwVMException(globalObject, scope, createURIError(globalObject, makeString("Could not open file at "_s, filePath.string(), " fopen had error: "_s, strerror(errno))));
+        return throwVMException(globalObject, scope, createURIError(globalObject, makeString("Could not open file at "_s, filePath.string(), " fopen had error: "_s, safeStrerror(errno).data())));
 
     RELEASE_AND_RETURN(scope, JSValue::encode(JSFileDescriptor::create(vm, globalObject, WTFMove(descriptor))));
 }

Modified: trunk/Source/_javascript_Core/runtime/BytecodeCacheError.cpp (284532 => 284533)


--- trunk/Source/_javascript_Core/runtime/BytecodeCacheError.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/runtime/BytecodeCacheError.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -26,6 +26,8 @@
 #include "config.h"
 #include "BytecodeCacheError.h"
 
+#include <wtf/SafeStrerror.h>
+
 namespace JSC {
 
 bool BytecodeCacheError::StandardError::isValid() const
@@ -35,7 +37,7 @@
 
 String BytecodeCacheError::StandardError::message() const
 {
-    return strerror(m_errno);
+    return safeStrerror(m_errno).data();
 }
 
 bool BytecodeCacheError::WriteError::isValid() const

Modified: trunk/Source/_javascript_Core/tools/FunctionAllowlist.cpp (284532 => 284533)


--- trunk/Source/_javascript_Core/tools/FunctionAllowlist.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/tools/FunctionAllowlist.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -31,6 +31,7 @@
 #include "CodeBlock.h"
 #include <stdio.h>
 #include <string.h>
+#include <wtf/SafeStrerror.h>
 
 namespace JSC {
 
@@ -45,7 +46,7 @@
             m_hasActiveAllowlist = true;
             m_entries.add(filename);
         } else
-            dataLogF("Failed to open file %s. Did you add the file-read-data entitlement to WebProcess.sb? Error code: %s\n", filename, strerror(errno));
+            dataLogF("Failed to open file %s. Did you add the file-read-data entitlement to WebProcess.sb? Error code: %s\n", filename, safeStrerror(errno).data());
         return;
     }
 
@@ -73,7 +74,7 @@
 
     int result = fclose(f);
     if (result)
-        dataLogF("Failed to close file %s: %s\n", filename, strerror(errno));
+        dataLogF("Failed to close file %s: %s\n", filename, safeStrerror(errno).data());
 }
 
 bool FunctionAllowlist::contains(CodeBlock* codeBlock) const

Modified: trunk/Source/_javascript_Core/tools/FunctionOverrides.cpp (284532 => 284533)


--- trunk/Source/_javascript_Core/tools/FunctionOverrides.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/tools/FunctionOverrides.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <wtf/DataLog.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/StringBuilder.h>
 #include <wtf/text/StringHash.h>
@@ -283,7 +284,7 @@
     
     int result = fclose(file);
     if (result)
-        dataLogF("Failed to close file %s: %s\n", fileName, strerror(errno));
+        dataLogF("Failed to close file %s: %s\n", fileName, safeStrerror(errno).data());
 }
     
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/wasm/WasmMemory.cpp (284532 => 284533)


--- trunk/Source/_javascript_Core/wasm/WasmMemory.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/_javascript_Core/wasm/WasmMemory.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -37,6 +37,7 @@
 #include <wtf/Platform.h>
 #include <wtf/PrintStream.h>
 #include <wtf/RAMSize.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/StdSet.h>
 #include <wtf/Vector.h>
 
@@ -301,7 +302,7 @@
         switch (m_mode) {
         case MemoryMode::Signaling:
             if (mprotect(memory, Memory::fastMappedBytes(), PROT_READ | PROT_WRITE)) {
-                dataLog("mprotect failed: ", strerror(errno), "\n");
+                dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
                 RELEASE_ASSERT_NOT_REACHED();
             }
             memoryManager().freeFastMemory(memory);
@@ -313,7 +314,7 @@
                 break;
             case MemorySharingMode::Shared: {
                 if (mprotect(memory, m_mappedCapacity, PROT_READ | PROT_WRITE)) {
-                    dataLog("mprotect failed: ", strerror(errno), "\n");
+                    dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
                     RELEASE_ASSERT_NOT_REACHED();
                 }
                 memoryManager().freeGrowableBoundsCheckingMemory(memory, m_mappedCapacity);
@@ -406,7 +407,7 @@
     
     if (fastMemory) {
         if (mprotect(fastMemory + initialBytes, Memory::fastMappedBytes() - initialBytes, PROT_NONE)) {
-            dataLog("mprotect failed: ", strerror(errno), "\n");
+            dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
             RELEASE_ASSERT_NOT_REACHED();
         }
 
@@ -442,7 +443,7 @@
         }
 
         if (mprotect(slowMemory + initialBytes, maximumBytes - initialBytes, PROT_NONE)) {
-            dataLog("mprotect failed: ", strerror(errno), "\n");
+            dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
             RELEASE_ASSERT_NOT_REACHED();
         }
 
@@ -516,7 +517,7 @@
 
         dataLogLnIf(verbose, "Marking WebAssembly memory's ", RawPointer(memory), " as read+write in range [", RawPointer(startAddress), ", ", RawPointer(startAddress + extraBytes), ")");
         if (mprotect(startAddress, extraBytes, PROT_READ | PROT_WRITE)) {
-            dataLog("mprotect failed: ", strerror(errno), "\n");
+            dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
             RELEASE_ASSERT_NOT_REACHED();
         }
 
@@ -605,7 +606,7 @@
         
         dataLogLnIf(verbose, "Marking WebAssembly memory's ", RawPointer(memory), " as read+write in range [", RawPointer(startAddress), ", ", RawPointer(startAddress + extraBytes), ")");
         if (mprotect(startAddress, extraBytes, PROT_READ | PROT_WRITE)) {
-            dataLog("mprotect failed: ", strerror(errno), "\n");
+            dataLog("mprotect failed: ", safeStrerror(errno).data(), "\n");
             RELEASE_ASSERT_NOT_REACHED();
         }
 

Modified: trunk/Source/WTF/ChangeLog (284532 => 284533)


--- trunk/Source/WTF/ChangeLog	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/ChangeLog	2021-10-20 15:04:26 UTC (rev 284533)
@@ -1,3 +1,28 @@
+2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
+
+        Do not use strerror()
+        https://bugs.webkit.org/show_bug.cgi?id=231913
+
+        Reviewed by Chris Dumez.
+
+        Add a new safeStrerror function that we can use without worrying about thread safety.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/SafeStrerror.cpp: Added.
+        (WTF::safeStrerror):
+        * wtf/SafeStrerror.h: Added.
+        * wtf/linux/RealTimeThreads.cpp:
+        (WTF::RealTimeThreads::promoteThreadToRealTime):
+        * wtf/playstation/FileSystemPlayStation.cpp:
+        (WTF::FileSystemImpl::deleteFile):
+        * wtf/posix/FileSystemPOSIX.cpp:
+        (WTF::FileSystemImpl::deleteFile):
+        * wtf/posix/ThreadingPOSIX.cpp:
+        (WTF::Thread::establishHandle):
+        * wtf/threads/Signals.cpp:
+        (WTF::jscSignalHandler):
+
 2021-10-20  Youenn Fablet  <you...@apple.com>
 
         Add support for requestVideoFrameCallback API and MediaStreamTrack-based backend support

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (284532 => 284533)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-10-20 15:04:26 UTC (rev 284533)
@@ -73,6 +73,7 @@
 		337B2D6A26546EB300DDFD3D /* LikelyDenseUnsignedIntegerSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 337B2D6826546EAA00DDFD3D /* LikelyDenseUnsignedIntegerSet.cpp */; };
 		4427C5AA21F6D6C300A612A4 /* ASCIICType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */; };
 		46BEB6EB22FFE24900269867 /* RefCounted.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BEB6E922FFDDD500269867 /* RefCounted.cpp */; };
+		46E93049271F1205005BA6E5 /* SafeStrerror.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46E43647271F10AA00C88C90 /* SafeStrerror.cpp */; };
 		50DE35F5215BB01500B979C7 /* ExternalStringImpl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 50DE35F3215BB01500B979C7 /* ExternalStringImpl.cpp */; };
 		515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515F794B1CFC9F4A00CCED93 /* CrossThreadCopier.cpp */; };
 		517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */; };
@@ -397,6 +398,8 @@
 		46209A27266D543A007F8F4A /* CancellableTask.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CancellableTask.h; sourceTree = "<group>"; };
 		46BA9EAB1F4CD61E009A2BBC /* CompletionHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompletionHandler.h; sourceTree = "<group>"; };
 		46BEB6E922FFDDD500269867 /* RefCounted.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounted.cpp; sourceTree = "<group>"; };
+		46E43646271F10AA00C88C90 /* SafeStrerror.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SafeStrerror.h; sourceTree = "<group>"; };
+		46E43647271F10AA00C88C90 /* SafeStrerror.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = SafeStrerror.cpp; sourceTree = "<group>"; };
 		50DE35F3215BB01500B979C7 /* ExternalStringImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExternalStringImpl.cpp; sourceTree = "<group>"; };
 		50DE35F4215BB01500B979C7 /* ExternalStringImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExternalStringImpl.h; sourceTree = "<group>"; };
 		513E170A1CD7D5BF00E3650B /* LoggingAccumulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoggingAccumulator.h; sourceTree = "<group>"; };
@@ -1277,6 +1280,8 @@
 				E31CF0A5261058580036E673 /* RobinHoodHashTable.h */,
 				2CDED0F118115C85004DBA70 /* RunLoop.cpp */,
 				2CDED0F218115C85004DBA70 /* RunLoop.h */,
+				46E43647271F10AA00C88C90 /* SafeStrerror.cpp */,
+				46E43646271F10AA00C88C90 /* SafeStrerror.h */,
 				14F3B0F615E45E4600210069 /* SaturatedArithmetic.h */,
 				1469419416EAAFF80024E146 /* SchedulePair.h */,
 				1A3524AA1D63A2FF0031729B /* Scope.h */,
@@ -1821,6 +1826,7 @@
 				2CDED0F318115C85004DBA70 /* RunLoop.cpp in Sources */,
 				2CDED0EF18115C38004DBA70 /* RunLoopCF.cpp in Sources */,
 				1CA85CA9241B0B260071C2F5 /* RuntimeApplicationChecksCocoa.cpp in Sources */,
+				46E93049271F1205005BA6E5 /* SafeStrerror.cpp in Sources */,
 				A3EE5C3D21FFAC7D00FABD61 /* SchedulePairCF.cpp in Sources */,
 				A3EE5C4021FFACA200FABD61 /* SchedulePairMac.mm in Sources */,
 				0F66B28E1DC97BAB004A1D3F /* Seconds.cpp in Sources */,

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (284532 => 284533)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2021-10-20 15:04:26 UTC (rev 284533)
@@ -230,6 +230,7 @@
     RobinHoodHashTable.h
     RunLoop.h
     SHA1.h
+    SafeStrerror.h
     SaturatedArithmetic.h
     SchedulePair.h
     Scope.h
@@ -459,6 +460,7 @@
     RefCountedLeakCounter.cpp
     RunLoop.cpp
     SHA1.cpp
+    SafeStrerror.cpp
     Seconds.cpp
     SegmentedVector.cpp
     SixCharacterHash.cpp

Copied: trunk/Source/WTF/wtf/SafeStrerror.cpp (from rev 284532, trunk/Source/_javascript_Core/runtime/BytecodeCacheError.cpp) (0 => 284533)


--- trunk/Source/WTF/wtf/SafeStrerror.cpp	                        (rev 0)
+++ trunk/Source/WTF/wtf/SafeStrerror.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -0,0 +1,61 @@
+/* 
+ * Copyright (C) 2021 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/SafeStrerror.h>
+
+#include <cstring>
+#include <type_traits>
+#include <wtf/Platform.h>
+#include <wtf/text/CString.h>
+
+namespace WTF {
+
+CString safeStrerror(int errnum)
+{
+    constexpr size_t bufferLength = 1024;
+    char* cstringBuffer = nullptr;
+    auto result = CString::newUninitialized(bufferLength, cstringBuffer);
+#if OS(WINDOWS)
+    strerror_s(cstringBuffer, bufferLength, errnum);
+#else
+    auto ret = strerror_r(errnum, cstringBuffer, bufferLength);
+    if constexpr (std::is_same<decltype(ret), char*>::value) {
+        // We have GNU strerror_r(), which returns char*. This may or may not be a pointer into
+        // cstringBuffer. We also have to be careful because this has to compile even if ret is
+        // an int, hence the reinterpret_casts.
+        char* message = reinterpret_cast<char*>(ret);
+        if (message != cstringBuffer)
+            strncpy(cstringBuffer, message, bufferLength);
+    } else {
+        // We have POSIX strerror_r, which returns int and may fail.
+        if (ret)
+            snprintf(cstringBuffer, bufferLength, "%s %d", "Unknown error", errnum);
+    }
+#endif // OS(WINDOWS)
+    return result;
+}
+
+} // namespace WTF

Added: trunk/Source/WTF/wtf/SafeStrerror.h (0 => 284533)


--- trunk/Source/WTF/wtf/SafeStrerror.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/SafeStrerror.h	2021-10-20 15:04:26 UTC (rev 284533)
@@ -0,0 +1,48 @@
+/* 
+ * Copyright (C) 2021 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/Forward.h>
+
+namespace WTF {
+
+// This is strerror, except it is threadsafe. The problem with normal strerror is it returns a
+// pointer to static storage, and it may actually modify that storage, so it can never be used in
+// any multithreaded application, or any library that may be linked to a multithreaded application.
+// (Why does it modify its storage? So that it can append the error number to the error string, as
+// in "Unknown error n." Also, because it will localize the error message.) The standard
+// alternatives are strerror_s and strerror_r, but both have problems. strerror_s is specified by
+// C11, but not by C++ (as of C++20), and it is optional so glibc decided to ignore it. We can only
+// rely on it to exist on Windows. Then strerror_r is even worse. First, it doesn't exist at all on
+// Windows. Second, the GNU version is incompatible with the POSIX version, and it is impossible to
+// use correctly unless you know which version you have. Both strerror_s and strerror_r are
+// cumbersome because they force you to allocate the buffer for the result manually. It's all such a
+// mess that we should deal with the complexity here rather than elsewhere in WebKit.
+WTF_EXPORT_PRIVATE CString safeStrerror(int errnum);
+
+}
+
+using WTF::safeStrerror;

Modified: trunk/Source/WTF/wtf/linux/RealTimeThreads.cpp (284532 => 284533)


--- trunk/Source/WTF/wtf/linux/RealTimeThreads.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/linux/RealTimeThreads.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -30,6 +30,7 @@
 #include <signal.h>
 #include <string.h>
 #include <wtf/MainThread.h>
+#include <wtf/SafeStrerror.h>
 
 #if USE(GLIB)
 #include <gio/gio.h>
@@ -127,7 +128,7 @@
 #if USE(GLIB)
         realTimeKitMakeThreadRealTime(getpid(), thread.id(), param.sched_priority);
 #else
-        LOG_ERROR("Failed to set thread %d as real time: %s", thread.id(), strerror(error));
+        LOG_ERROR("Failed to set thread %d as real time: %s", thread.id(), safeStrerror(error).data());
 #endif
     }
 }

Modified: trunk/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp (284532 => 284533)


--- trunk/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -37,6 +37,7 @@
 #include <sys/statvfs.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/text/StringBuilder.h>
 
 namespace WTF {
@@ -88,7 +89,7 @@
     // unlink(...) returns 0 on successful deletion of the path and non-zero in any other case (including invalid permissions or non-existent file)
     bool unlinked = !unlink(fsRep.data());
     if (!unlinked && errno != ENOENT)
-        LOG_ERROR("File failed to delete. Error message: %s", strerror(errno));
+        LOG_ERROR("File failed to delete. Error message: %s", safeStrerror(errno).data());
 
     return unlinked;
 }

Modified: trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp (284532 => 284533)


--- trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/posix/FileSystemPOSIX.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -40,6 +40,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <wtf/EnumTraits.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/StringBuilder.h>
 #include <wtf/text/WTFString.h>
@@ -261,7 +262,7 @@
     // unlink(...) returns 0 on successful deletion of the path and non-zero in any other case (including invalid permissions or non-existent file)
     bool unlinked = !unlink(fileSystemRepresentation(path).data());
     if (!unlinked && errno != ENOENT)
-        LOG_ERROR("File failed to delete. Error message: %s", strerror(errno));
+        LOG_ERROR("File failed to delete. Error message: %s", safeStrerror(errno).data());
 
     return unlinked;
 }

Modified: trunk/Source/WTF/wtf/posix/ThreadingPOSIX.cpp (284532 => 284533)


--- trunk/Source/WTF/wtf/posix/ThreadingPOSIX.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/posix/ThreadingPOSIX.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -36,6 +36,7 @@
 
 #include <errno.h>
 #include <wtf/NeverDestroyed.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/StdLibExtras.h>
 #include <wtf/ThreadingPrimitives.h>
 #include <wtf/WTFConfig.h>
@@ -305,7 +306,7 @@
         struct sched_param param = { 0 };
         error = pthread_setschedparam(threadHandle, policy | SCHED_RESET_ON_FORK, &param);
         if (error)
-            LOG_ERROR("Failed to set sched policy %d for thread %d: %s", policy, threadHandle, strerror(error));
+            LOG_ERROR("Failed to set sched policy %d for thread %d: %s", policy, threadHandle, safeStrerror(error).data());
     }
 #elif !HAVE(QOS_CLASSES)
     UNUSED_PARAM(qos);

Modified: trunk/Source/WTF/wtf/threads/Signals.cpp (284532 => 284533)


--- trunk/Source/WTF/wtf/threads/Signals.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WTF/wtf/threads/Signals.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -384,7 +384,7 @@
         sigfillset(&defaultAction.sa_mask);
         defaultAction.sa_flags = 0;
         auto result = sigaction(sig, &defaultAction, nullptr);
-        dataLogLnIf(result == -1, "Unable to restore the default handler while proccessing signal ", sig, " the process is probably deadlocked. (errno: ", strerror(errno), ")");
+        dataLogLnIf(result == -1, "Unable to restore the default handler while processing signal ", sig, " the process is probably deadlocked. (errno: ", errno, ")");
     };
 
     // This shouldn't happen but we might as well be careful.

Modified: trunk/Source/WebKit/ChangeLog (284532 => 284533)


--- trunk/Source/WebKit/ChangeLog	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WebKit/ChangeLog	2021-10-20 15:04:26 UTC (rev 284533)
@@ -1,3 +1,22 @@
+2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
+
+        Do not use strerror()
+        https://bugs.webkit.org/show_bug.cgi?id=231913
+
+        Reviewed by Chris Dumez.
+
+        * Platform/IPC/unix/ConnectionUnix.cpp:
+        (IPC::Connection::readyReadHandler):
+        (IPC::Connection::sendOutputMessage):
+        * Platform/unix/SharedMemoryUnix.cpp:
+        (WebKit::SharedMemory::allocate):
+        * Shared/mac/AuxiliaryProcessMac.mm:
+        (WebKit::setAndSerializeSandboxParameters):
+        (WebKit::sandboxDataVaultParentDirectory):
+        (WebKit::ensureSandboxCacheDirectory):
+        (WebKit::tryApplyCachedSandbox):
+        (WebKit::applySandbox):
+
 2021-10-20  John Pascoe  <j_pas...@apple.com>
         [WebAuthn] Obtain consent to create new credential when platform authenticator in excludedCredentials
         https://bugs.webkit.org/show_bug.cgi?id=219813

Modified: trunk/Source/WebKit/Platform/IPC/unix/ConnectionUnix.cpp (284532 => 284533)


--- trunk/Source/WebKit/Platform/IPC/unix/ConnectionUnix.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WebKit/Platform/IPC/unix/ConnectionUnix.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -37,6 +37,7 @@
 #include <fcntl.h>
 #include <poll.h>
 #include <wtf/Assertions.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/StdLibExtras.h>
 #include <wtf/UniStdExtras.h>
 
@@ -352,7 +353,7 @@
             }
 
             if (m_isConnected) {
-                WTFLogAlways("Error receiving IPC message on socket %d in process %d: %s", m_socketDescriptor, getpid(), strerror(errno));
+                WTFLogAlways("Error receiving IPC message on socket %d in process %d: %s", m_socketDescriptor, getpid(), safeStrerror(errno).data());
                 connectionDidClose();
             }
             return;
@@ -589,7 +590,7 @@
         }
 
         if (m_isConnected)
-            WTFLogAlways("Error sending IPC message: %s", strerror(errno));
+            WTFLogAlways("Error sending IPC message: %s", safeStrerror(errno).data());
         return false;
     }
 

Modified: trunk/Source/WebKit/Platform/unix/SharedMemoryUnix.cpp (284532 => 284533)


--- trunk/Source/WebKit/Platform/unix/SharedMemoryUnix.cpp	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WebKit/Platform/unix/SharedMemoryUnix.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -41,6 +41,7 @@
 #include <unistd.h>
 #include <wtf/Assertions.h>
 #include <wtf/RandomNumber.h>
+#include <wtf/SafeStrerror.h>
 #include <wtf/UniStdExtras.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/WTFString.h>
@@ -165,7 +166,7 @@
 {
     int fileDescriptor = createSharedMemory();
     if (fileDescriptor == -1) {
-        WTFLogAlways("Failed to create shared memory: %s", strerror(errno));
+        WTFLogAlways("Failed to create shared memory: %s", safeStrerror(errno).data());
         return nullptr;
     }
 

Modified: trunk/Source/WebKit/Shared/mac/AuxiliaryProcessMac.mm (284532 => 284533)


--- trunk/Source/WebKit/Shared/mac/AuxiliaryProcessMac.mm	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/WebKit/Shared/mac/AuxiliaryProcessMac.mm	2021-10-20 15:04:26 UTC (rev 284533)
@@ -51,6 +51,7 @@
 #import <wtf/DataLog.h>
 #import <wtf/FileSystem.h>
 #import <wtf/RandomNumber.h>
+#import <wtf/SafeStrerror.h>
 #import <wtf/Scope.h>
 #import <wtf/SoftLinking.h>
 #import <wtf/SystemTracing.h>
@@ -251,7 +252,7 @@
         const char* name = initializationParameters.name(i);
         const char* value = initializationParameters.value(i);
         if (sandbox_set_param(sandboxParameters.get(), name, value)) {
-            WTFLogAlways("%s: Could not set sandbox parameter: %s\n", getprogname(), strerror(errno));
+            WTFLogAlways("%s: Could not set sandbox parameter: %s\n", getprogname(), safeStrerror(errno).data());
             CRASH();
         }
         builder.append(name, ':', value, ':');
@@ -271,13 +272,13 @@
     char temp[PATH_MAX];
     size_t length = confstr(_CS_DARWIN_USER_CACHE_DIR, temp, sizeof(temp));
     if (!length) {
-        WTFLogAlways("%s: Could not retrieve user temporary directory path: %s\n", getprogname(), strerror(errno));
+        WTFLogAlways("%s: Could not retrieve user temporary directory path: %s\n", getprogname(), safeStrerror(errno).data());
         exit(EX_NOPERM);
     }
     RELEASE_ASSERT(length <= sizeof(temp));
     char resolvedPath[PATH_MAX];
     if (!realpath(temp, resolvedPath)) {
-        WTFLogAlways("%s: Could not canonicalize user temporary directory path: %s\n", getprogname(), strerror(errno));
+        WTFLogAlways("%s: Could not canonicalize user temporary directory path: %s\n", getprogname(), safeStrerror(errno).data());
         exit(EX_NOPERM);
     }
     return resolvedPath;
@@ -373,7 +374,7 @@
         if (!makeDataVault())
             return false;
     } else {
-        WTFLogAlways("%s: Sandbox directory couldn't be created: ", getprogname(), strerror(errno));
+        WTFLogAlways("%s: Sandbox directory couldn't be created: ", getprogname(), safeStrerror(errno).data());
         return false;
     }
 #else
@@ -521,7 +522,7 @@
     setNotifyOptions();
 
     if (sandbox_apply(&profile)) {
-        WTFLogAlways("%s: Could not apply cached sandbox: %s\n", getprogname(), strerror(errno));
+        WTFLogAlways("%s: Could not apply cached sandbox: %s\n", getprogname(), safeStrerror(errno).data());
         return false;
     }
 
@@ -623,7 +624,7 @@
     setNotifyOptions();
     
     if (sandbox_apply(sandboxProfile.get())) {
-        WTFLogAlways("%s: Could not apply compiled sandbox: %s\n", getprogname(), strerror(errno));
+        WTFLogAlways("%s: Could not apply compiled sandbox: %s\n", getprogname(), safeStrerror(errno).data());
         CRASH();
     }
 

Modified: trunk/Source/bmalloc/ChangeLog (284532 => 284533)


--- trunk/Source/bmalloc/ChangeLog	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/bmalloc/ChangeLog	2021-10-20 15:04:26 UTC (rev 284533)
@@ -1,3 +1,13 @@
+2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
+
+        Do not use strerror()
+        https://bugs.webkit.org/show_bug.cgi?id=231913
+
+        Reviewed by Chris Dumez.
+
+        * libpas/src/libpas/pas_page_malloc.c:
+        (pas_page_malloc_commit):
+
 2021-10-20  Carlos Garcia Campos  <cgar...@igalia.com>
 
         [WPE] Reenable -fvisibility=hidden and -fvisibility-inlines-hidden

Modified: trunk/Source/bmalloc/libpas/src/libpas/pas_page_malloc.c (284532 => 284533)


--- trunk/Source/bmalloc/libpas/src/libpas/pas_page_malloc.c	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Source/bmalloc/libpas/src/libpas/pas_page_malloc.c	2021-10-20 15:04:26 UTC (rev 284533)
@@ -176,7 +176,7 @@
     if (pas_page_malloc_mprotect_decommitted) {
         result = mprotect((void*)base_as_int, end_as_int - base_as_int, PROT_READ | PROT_WRITE);
         if (result) {
-            pas_log("Could not mprotect on commit: %s\n", strerror(errno));
+            pas_log("Could not mprotect on commit: error code %d\n", result);
             PAS_ASSERT(!result);
         }
     }

Modified: trunk/Tools/ChangeLog (284532 => 284533)


--- trunk/Tools/ChangeLog	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Tools/ChangeLog	2021-10-20 15:04:26 UTC (rev 284533)
@@ -1,3 +1,14 @@
+2021-10-20  Michael Catanzaro  <mcatanz...@gnome.org>
+
+        Do not use strerror()
+        https://bugs.webkit.org/show_bug.cgi?id=231913
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/CMakeLists.txt:
+        * TestWebKitAPI/Tests/WTF/SafeStrerror.cpp: Added.
+        (TestWebKitAPI::TEST):
+
 2021-10-20  John Pascoe  <j_pas...@apple.com>
         [WebAuthn] Obtain consent to create new credential when platform authenticator in excludedCredentials
         https://bugs.webkit.org/show_bug.cgi?id=219813

Modified: trunk/Tools/TestWebKitAPI/CMakeLists.txt (284532 => 284533)


--- trunk/Tools/TestWebKitAPI/CMakeLists.txt	2021-10-20 15:01:41 UTC (rev 284532)
+++ trunk/Tools/TestWebKitAPI/CMakeLists.txt	2021-10-20 15:04:26 UTC (rev 284533)
@@ -82,6 +82,7 @@
     Tests/WTF/RobinHoodHashSet.cpp
     Tests/WTF/RunLoop.cpp
     Tests/WTF/SHA1.cpp
+    Tests/WTF/SafeStrerror.cpp
     Tests/WTF/SaturatedArithmeticOperations.cpp
     Tests/WTF/Scope.cpp
     Tests/WTF/ScopedLambda.cpp

Added: trunk/Tools/TestWebKitAPI/Tests/WTF/SafeStrerror.cpp (0 => 284533)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/SafeStrerror.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/SafeStrerror.cpp	2021-10-20 15:04:26 UTC (rev 284533)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2021 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/SafeStrerror.h>
+
+#include <cstring>
+#include <wtf/text/CString.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF_SafeStrerror, StringsAreEqual)
+{
+    // We test only a few known error codes because our error message when passing an unknown error
+    // code won't be localized and might not match the system libc anyway.
+    for (int i = 0; i < 10; i++)
+        EXPECT_STREQ(strerror(i), safeStrerror(i).data());
+}
+
+} // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to