bridges/source/cpp_uno/shared/vtablefactory.cxx |   81 ++++++++++++------------
 1 file changed, 43 insertions(+), 38 deletions(-)

New commits:
commit 052efd869e12796815401fb29a975e49866e6ce7
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Thu Dec 17 10:48:51 2020 +0100
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Thu Dec 17 13:20:46 2020 +0100

    Make the pthread_jit_write_protect_np call pair exception-safe
    
    ...just in case
    
    Change-Id: Id056ee4dfd64dd186f01d117cfede28f4b7f6c09
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107867
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/bridges/source/cpp_uno/shared/vtablefactory.cxx 
b/bridges/source/cpp_uno/shared/vtablefactory.cxx
index 90c414290c1a..9d4c5d31dd3f 100644
--- a/bridges/source/cpp_uno/shared/vtablefactory.cxx
+++ b/bridges/source/cpp_uno/shared/vtablefactory.cxx
@@ -134,6 +134,13 @@ extern "C" void freeExec(
 #endif
 }
 
+#if defined MACOSX && defined __aarch64__
+struct JitMemoryProtectionGuard {
+    JitMemoryProtectionGuard() { pthread_jit_write_protect_np(0); }
+    ~JitMemoryProtectionGuard() { pthread_jit_write_protect_np(1); }
+};
+#endif
+
 }
 
 class VtableFactory::GuardedBlocks:
@@ -342,52 +349,50 @@ sal_Int32 VtableFactory::createVtables(
     typelib_InterfaceTypeDescription * type, sal_Int32 vtableNumber,
     typelib_InterfaceTypeDescription * mostDerived, bool includePrimary) const
 {
+    {
 #if defined MACOSX && defined __aarch64__
-    // TODO: Should we handle resetting this in a exception-throwing-safe way?
-    pthread_jit_write_protect_np(0);
+        JitMemoryProtectionGuard guard;
 #endif
-    if (includePrimary) {
-        sal_Int32 slotCount
-            = bridges::cpp_uno::shared::getPrimaryFunctions(type);
-        Block block;
-        if (!createBlock(block, slotCount)) {
-            throw std::bad_alloc();
-        }
-        try {
-            Slot * slots = initializeBlock(
-                block.start, slotCount, vtableNumber, mostDerived);
-            unsigned char * codeBegin =
-                reinterpret_cast< unsigned char * >(slots);
-            unsigned char * code = codeBegin;
-            sal_Int32 vtableOffset = blocks.size() * sizeof (Slot *);
-            for (typelib_InterfaceTypeDescription const * type2 = type;
-                 type2 != nullptr; type2 = type2->pBaseTypeDescription)
-            {
-                code = addLocalFunctions(
-                    &slots, code,
+        if (includePrimary) {
+            sal_Int32 slotCount
+                = bridges::cpp_uno::shared::getPrimaryFunctions(type);
+            Block block;
+            if (!createBlock(block, slotCount)) {
+                throw std::bad_alloc();
+            }
+            try {
+                Slot * slots = initializeBlock(
+                    block.start, slotCount, vtableNumber, mostDerived);
+                unsigned char * codeBegin =
+                    reinterpret_cast< unsigned char * >(slots);
+                unsigned char * code = codeBegin;
+                sal_Int32 vtableOffset = blocks.size() * sizeof (Slot *);
+                for (typelib_InterfaceTypeDescription const * type2 = type;
+                     type2 != nullptr; type2 = type2->pBaseTypeDescription)
+                {
+                    code = addLocalFunctions(
+                        &slots, code,
 #ifdef USE_DOUBLE_MMAP
-                    reinterpret_cast<sal_uIntPtr>(block.exec) - 
reinterpret_cast<sal_uIntPtr>(block.start),
+                        reinterpret_cast<sal_uIntPtr>(block.exec) - 
reinterpret_cast<sal_uIntPtr>(block.start),
 #endif
-                    type2,
-                    baseOffset.getFunctionOffset(type2->aBase.pTypeName),
-                    bridges::cpp_uno::shared::getLocalFunctions(type2),
-                    vtableOffset);
-            }
-            flushCode(codeBegin, code);
+                        type2,
+                        baseOffset.getFunctionOffset(type2->aBase.pTypeName),
+                        bridges::cpp_uno::shared::getLocalFunctions(type2),
+                        vtableOffset);
+                }
+                flushCode(codeBegin, code);
 #ifdef USE_DOUBLE_MMAP
-        //Finished generating block, swap writable pointer with executable
-        //pointer
-            std::swap(block.start, block.exec);
+                //Finished generating block, swap writable pointer with 
executable
+                //pointer
+                std::swap(block.start, block.exec);
 #endif
-            blocks.push_back(block);
-        } catch (...) {
-            freeBlock(block);
-            throw;
+                blocks.push_back(block);
+            } catch (...) {
+                freeBlock(block);
+                throw;
+            }
         }
     }
-#if defined MACOSX && defined __aarch64__
-    pthread_jit_write_protect_np(1);
-#endif
     for (sal_Int32 i = 0; i < type->nBaseTypes; ++i) {
         vtableNumber = createVtables(
             blocks, baseOffset, type->ppBaseTypes[i],
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to