Blame SOURCES/0023-JIT-When-making-memory-writable-include-the-exceptio.patch

383017
From 35614462443c100b6753b335b58a134fed4b5c35 Mon Sep 17 00:00:00 2001
383017
From: Ulf Hermann <ulf.hermann@qt.io>
383017
Date: Wed, 16 Dec 2020 16:45:36 +0100
383017
Subject: [PATCH 23/28] JIT: When making memory writable, include the exception
383017
 handler
383017
383017
makeWritable() rounds the memory down to the next page boundary. Usually
383017
we include the exception handler this way, unless the offset from the
383017
page boundary is less than the exception handler size. Make it explicit
383017
that we do want the exception handler to be writable, too.
383017
383017
Fixes: QTBUG-89513
383017
Change-Id: I2fb8fb0e1dcc3450b036924463dc1b40d2020c46
383017
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
383017
(cherry picked from commit 86a595b126bc6794380dc00af80ec4802f7d058c)
383017
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
383017
---
383017
 src/3rdparty/masm/assembler/AssemblerBuffer.h      |  4 ++--
383017
 src/3rdparty/masm/assembler/LinkBuffer.h           |  9 +++++----
383017
 .../masm/assembler/MacroAssemblerCodeRef.h         |  6 +++---
383017
 src/3rdparty/masm/stubs/ExecutableAllocator.h      | 11 ++++++++---
383017
 src/qml/jsruntime/qv4executableallocator.cpp       | 14 ++++++++++++--
383017
 src/qml/jsruntime/qv4executableallocator_p.h       | 10 ++++++++--
383017
 src/qml/jsruntime/qv4functiontable_win64.cpp       |  4 ++--
383017
 7 files changed, 40 insertions(+), 18 deletions(-)
383017
383017
diff --git a/src/3rdparty/masm/assembler/AssemblerBuffer.h b/src/3rdparty/masm/assembler/AssemblerBuffer.h
383017
index 45874235b6..2292a4c244 100644
383017
--- a/src/3rdparty/masm/assembler/AssemblerBuffer.h
383017
+++ b/src/3rdparty/masm/assembler/AssemblerBuffer.h
383017
@@ -140,9 +140,9 @@ namespace JSC {
383017
             if (!result)
383017
                 return 0;
383017
 
383017
-            ExecutableAllocator::makeWritable(result->start(), result->sizeInBytes());
383017
+            ExecutableAllocator::makeWritable(result->memoryStart(), result->memorySize());
383017
 
383017
-            memcpy(result->start(), m_buffer, m_index);
383017
+            memcpy(result->codeStart(), m_buffer, m_index);
383017
             
383017
             return result.release();
383017
         }
383017
diff --git a/src/3rdparty/masm/assembler/LinkBuffer.h b/src/3rdparty/masm/assembler/LinkBuffer.h
383017
index ba57564a1d..fa669deaf9 100644
383017
--- a/src/3rdparty/masm/assembler/LinkBuffer.h
383017
+++ b/src/3rdparty/masm/assembler/LinkBuffer.h
383017
@@ -333,7 +333,7 @@ inline void LinkBufferBase<MacroAssembler, ExecutableOffsetCalculator>::linkCode
383017
     m_executableMemory = m_assembler->m_assembler.executableCopy(*m_globalData, ownerUID, effort);
383017
     if (!m_executableMemory)
383017
         return;
383017
-    m_code = m_executableMemory->start();
383017
+    m_code = m_executableMemory->codeStart();
383017
     m_size = m_assembler->m_assembler.codeSize();
383017
     ASSERT(m_code);
383017
 }
383017
@@ -355,7 +355,8 @@ void LinkBufferBase<MacroAssembler, ExecutableOffsetCalculator>::performFinaliza
383017
 template <typename MacroAssembler, template <typename T> class ExecutableOffsetCalculator>
383017
 inline void LinkBufferBase<MacroAssembler, ExecutableOffsetCalculator>::makeExecutable()
383017
 {
383017
-    ExecutableAllocator::makeExecutable(code(), static_cast<int>(m_size));
383017
+    ExecutableAllocator::makeExecutable(m_executableMemory->memoryStart(),
383017
+                                        m_executableMemory->memorySize());
383017
 }
383017
 
383017
 template <typename MacroAssembler>
383017
@@ -442,9 +443,9 @@ inline void BranchCompactingLinkBuffer<MacroAssembler>::linkCode(void* ownerUID,
383017
     m_executableMemory = m_globalData->executableAllocator.allocate(*m_globalData, m_initialSize, ownerUID, effort);
383017
     if (!m_executableMemory)
383017
         return;
383017
-    m_code = (uint8_t*)m_executableMemory->start();
383017
+    m_code = (uint8_t*)m_executableMemory->codeStart();
383017
     ASSERT(m_code);
383017
-    ExecutableAllocator::makeWritable(m_code, m_initialSize);
383017
+    ExecutableAllocator::makeWritable(m_executableMemory->memoryStart(), m_executableMemory->memorySize());
383017
     uint8_t* inData = (uint8_t*)m_assembler->unlinkedCode();
383017
     uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
383017
     int readPtr = 0;
383017
diff --git a/src/3rdparty/masm/assembler/MacroAssemblerCodeRef.h b/src/3rdparty/masm/assembler/MacroAssemblerCodeRef.h
383017
index a7e78ad78f..cde9751108 100644
383017
--- a/src/3rdparty/masm/assembler/MacroAssemblerCodeRef.h
383017
+++ b/src/3rdparty/masm/assembler/MacroAssemblerCodeRef.h
383017
@@ -357,11 +357,11 @@ public:
383017
     }
383017
 
383017
     MacroAssemblerCodeRef(PassRefPtr<ExecutableMemoryHandle> executableMemory)
383017
-        : m_codePtr(executableMemory->start())
383017
+        : m_codePtr(executableMemory->codeStart())
383017
         , m_executableMemory(executableMemory)
383017
     {
383017
         ASSERT(m_executableMemory->isManaged());
383017
-        ASSERT(m_executableMemory->start());
383017
+        ASSERT(m_executableMemory->codeStart());
383017
         ASSERT(m_codePtr);
383017
     }
383017
     
383017
@@ -395,7 +395,7 @@ public:
383017
     {
383017
         if (!m_executableMemory)
383017
             return 0;
383017
-        return m_executableMemory->sizeInBytes();
383017
+        return m_executableMemory->codeSize();
383017
     }
383017
     
383017
     bool tryToDisassemble(const char* prefix) const
383017
diff --git a/src/3rdparty/masm/stubs/ExecutableAllocator.h b/src/3rdparty/masm/stubs/ExecutableAllocator.h
383017
index a439c53827..f984704023 100644
383017
--- a/src/3rdparty/masm/stubs/ExecutableAllocator.h
383017
+++ b/src/3rdparty/masm/stubs/ExecutableAllocator.h
383017
@@ -82,9 +82,14 @@ struct ExecutableMemoryHandle : public RefCounted<ExecutableMemoryHandle> {
383017
 
383017
     inline bool isManaged() const { return true; }
383017
 
383017
-    void *exceptionHandler() { return m_allocation->exceptionHandler(); }
383017
-    void *start() { return m_allocation->start(); }
383017
-    size_t sizeInBytes() { return m_size; }
383017
+    void *memoryStart() { return m_allocation->memoryStart(); }
383017
+    size_t memorySize() { return m_allocation->memorySize(); }
383017
+
383017
+    void *exceptionHandlerStart() { return m_allocation->exceptionHandlerStart(); }
383017
+    size_t exceptionHandlerSize() { return m_allocation->exceptionHandlerSize(); }
383017
+
383017
+    void *codeStart() { return m_allocation->codeStart(); }
383017
+    size_t codeSize() { return m_size; }
383017
 
383017
     QV4::ExecutableAllocator::ChunkOfPages *chunk() const
383017
     { return m_allocator->chunkForAllocation(m_allocation); }
383017
diff --git a/src/qml/jsruntime/qv4executableallocator.cpp b/src/qml/jsruntime/qv4executableallocator.cpp
383017
index 7ee6f39aa2..c06773d3c5 100644
383017
--- a/src/qml/jsruntime/qv4executableallocator.cpp
383017
+++ b/src/qml/jsruntime/qv4executableallocator.cpp
383017
@@ -45,12 +45,22 @@
383017
 
383017
 using namespace QV4;
383017
 
383017
-void *ExecutableAllocator::Allocation::exceptionHandler() const
383017
+void *ExecutableAllocator::Allocation::exceptionHandlerStart() const
383017
 {
383017
     return reinterpret_cast<void*>(addr);
383017
 }
383017
 
383017
-void *ExecutableAllocator::Allocation::start() const
383017
+size_t ExecutableAllocator::Allocation::exceptionHandlerSize() const
383017
+{
383017
+    return QV4::exceptionHandlerSize();
383017
+}
383017
+
383017
+void *ExecutableAllocator::Allocation::memoryStart() const
383017
+{
383017
+    return reinterpret_cast<void*>(addr);
383017
+}
383017
+
383017
+void *ExecutableAllocator::Allocation::codeStart() const
383017
 {
383017
     return reinterpret_cast<void*>(addr + exceptionHandlerSize());
383017
 }
383017
diff --git a/src/qml/jsruntime/qv4executableallocator_p.h b/src/qml/jsruntime/qv4executableallocator_p.h
383017
index f98f2c7d33..4735fb151f 100644
383017
--- a/src/qml/jsruntime/qv4executableallocator_p.h
383017
+++ b/src/qml/jsruntime/qv4executableallocator_p.h
383017
@@ -86,8 +86,14 @@ public:
383017
             , free(true)
383017
         {}
383017
 
383017
-        void *exceptionHandler() const;
383017
-        void *start() const;
383017
+        void *memoryStart() const;
383017
+        size_t memorySize() const { return size; }
383017
+
383017
+        void *exceptionHandlerStart() const;
383017
+        size_t exceptionHandlerSize() const;
383017
+
383017
+        void *codeStart() const;
383017
+
383017
         void invalidate() { addr = 0; }
383017
         bool isValid() const { return addr != 0; }
383017
         void deallocate(ExecutableAllocator *allocator);
383017
diff --git a/src/qml/jsruntime/qv4functiontable_win64.cpp b/src/qml/jsruntime/qv4functiontable_win64.cpp
383017
index fc13dc2602..0cb98641cd 100644
383017
--- a/src/qml/jsruntime/qv4functiontable_win64.cpp
383017
+++ b/src/qml/jsruntime/qv4functiontable_win64.cpp
383017
@@ -106,7 +106,7 @@ struct ExceptionHandlerRecord
383017
 void generateFunctionTable(Function *, JSC::MacroAssemblerCodeRef *codeRef)
383017
 {
383017
     ExceptionHandlerRecord *record = reinterpret_cast<ExceptionHandlerRecord *>(
383017
-                codeRef->executableMemory()->exceptionHandler());
383017
+                codeRef->executableMemory()->exceptionHandlerStart());
383017
 
383017
     record->info.Version             = 1;
383017
     record->info.Flags               = 0;
383017
@@ -136,7 +136,7 @@ void generateFunctionTable(Function *, JSC::MacroAssemblerCodeRef *codeRef)
383017
 void destroyFunctionTable(Function *, JSC::MacroAssemblerCodeRef *codeRef)
383017
 {
383017
     ExceptionHandlerRecord *record = reinterpret_cast<ExceptionHandlerRecord *>(
383017
-                codeRef->executableMemory()->exceptionHandler());
383017
+                codeRef->executableMemory()->exceptionHandlerStart());
383017
     if (!RtlDeleteFunctionTable(&record->handler)) {
383017
         const unsigned int errorCode = GetLastError();
383017
         qWarning() << "Failed to remove win64 unwind hook. Error code:" << errorCode;
383017
-- 
383017
2.31.1
383017