[libcxx] [libcxxabi] [libunwind] [libunwind][libcxx][libcxxabi] Fix Exception Handling build for wasm (PR #79667)

via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 16 21:39:05 PDT 2024


https://github.com/trcrsired updated https://github.com/llvm/llvm-project/pull/79667

>From b5fee4962c347016a732a1310275651a8c74f23a Mon Sep 17 00:00:00 2001
From: trcrsired <uwgghhbcad at gmail.com>
Date: Fri, 26 Jan 2024 18:44:41 -0500
Subject: [PATCH] [libunwind] Fix build for wasm

The wasm unwind build appears to be dysfunctional, likely because the author has only supplied a customized LLVM build on request, rather than a fully functional patch.

This patch fixes the build

Apply formatting patch proposed by github bot

use "" to prevent CMAKE_SYSTEM_PROCESSOR not defined

[libunwind] logAPI functions should also be built

[libcxxabi] Fix function signatures for wasm

wasm does not define the function signatures correctly for cxxabi
Fix them

Fix formatting issues for libcxxabi's wasm eh change
---
 libcxx/include/__exception/exception_ptr.h |  7 ++-
 libcxxabi/include/cxxabi.h                 |  8 +++-
 libcxxabi/src/cxa_exception.cpp            |  8 +++-
 libunwind/include/__libunwind_config.h     |  1 +
 libunwind/include/libunwind.h              |  2 +
 libunwind/src/CMakeLists.txt               | 56 +++++++++++++---------
 libunwind/src/Unwind-wasm.c                | 16 +++----
 libunwind/src/config.h                     | 15 +++---
 libunwind/src/libunwind.cpp                |  2 +
 9 files changed, 73 insertions(+), 42 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 53e2f718bc1b35..ce3f77f599f9c9 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -36,7 +36,12 @@ struct __cxa_exception;
 _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
     void*,
     std::type_info*,
-    void(
+#  if defined(__USING_WASM_EXCEPTIONS__)
+    void*
+#  else
+    void
+#  endif
+    (
 #  if defined(_WIN32)
         __thiscall
 #  endif
diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index d0701181751c50..78d60cdccdffc2 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -47,8 +47,12 @@ __cxa_allocate_exception(size_t thrown_size) throw();
 extern _LIBCXXABI_FUNC_VIS void
 __cxa_free_exception(void *thrown_exception) throw();
 // This function is an LLVM extension, which mirrors the same extension in libsupc++ and libcxxrt
-extern _LIBCXXABI_FUNC_VIS __cxa_exception*
-__cxa_init_primary_exception(void* object, std::type_info* tinfo, void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
+extern _LIBCXXABI_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
+                                                                         void
+#  ifdef __USING_WASM_EXCEPTIONS__
+                                                                             *
+#  endif
+                                                                         (_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw();
 
 // 2.4.3 Throwing the Exception Object
 extern _LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp
index 65e9f4504ddade..638ef1bb593cf4 100644
--- a/libcxxabi/src/cxa_exception.cpp
+++ b/libcxxabi/src/cxa_exception.cpp
@@ -206,8 +206,14 @@ void __cxa_free_exception(void *thrown_object) throw() {
     __aligned_free_with_fallback((void *)raw_buffer);
 }
 
+#ifdef __USING_WASM_EXCEPTIONS__
+__cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
+                                              void*(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw()
+#else
 __cxa_exception* __cxa_init_primary_exception(void* object, std::type_info* tinfo,
-                                              void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw() {
+                                              void(_LIBCXXABI_DTOR_FUNC* dest)(void*)) throw()
+#endif
+{
   __cxa_exception* exception_header = cxa_exception_from_thrown_object(object);
   exception_header->referenceCount = 0;
   exception_header->unexpectedHandler = std::get_unexpected();
diff --git a/libunwind/include/__libunwind_config.h b/libunwind/include/__libunwind_config.h
index 8db336b2d727ce..1cda20d7225500 100644
--- a/libunwind/include/__libunwind_config.h
+++ b/libunwind/include/__libunwind_config.h
@@ -180,6 +180,7 @@
 #endif
 #define _LIBUNWIND_HIGHEST_DWARF_REGISTER                                      \
   _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
+#elif defined(__wasm__)
 # else
 #  error "Unsupported architecture."
 # endif
diff --git a/libunwind/include/libunwind.h b/libunwind/include/libunwind.h
index b2dae8feed9a3b..63e147ae2423e2 100644
--- a/libunwind/include/libunwind.h
+++ b/libunwind/include/libunwind.h
@@ -15,6 +15,7 @@
 
 #include <__libunwind_config.h>
 
+#ifndef __wasm__
 #include <stdint.h>
 #include <stddef.h>
 
@@ -1299,5 +1300,6 @@ enum {
   UNW_LOONGARCH_F30 = 62,
   UNW_LOONGARCH_F31 = 63,
 };
+#endif
 
 #endif
diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index 9c6f5d908b0945..2b21f4400797ac 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -1,31 +1,41 @@
 # Get sources
 
-set(LIBUNWIND_CXX_SOURCES
-    libunwind.cpp
-    Unwind-EHABI.cpp
-    Unwind-seh.cpp
-    )
+if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32" OR
+  "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm64")
+  set(LIBUNWIND_C_SOURCES
+      Unwind-wasm.c
+      )
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+     )
+else()
+  set(LIBUNWIND_CXX_SOURCES
+      libunwind.cpp
+      Unwind-EHABI.cpp
+      Unwind-seh.cpp
+      )
+
+  if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
+    list(APPEND LIBUNWIND_CXX_SOURCES
+      Unwind_AIXExtras.cpp
+      )
+  endif()
 
-if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
-  list(APPEND LIBUNWIND_CXX_SOURCES
-    Unwind_AIXExtras.cpp
-    )
-endif()
+  set(LIBUNWIND_C_SOURCES
+      UnwindLevel1.c
+      UnwindLevel1-gcc-ext.c
+      Unwind-sjlj.c
+      )
 
-set(LIBUNWIND_C_SOURCES
-    UnwindLevel1.c
-    UnwindLevel1-gcc-ext.c
-    Unwind-sjlj.c
-    Unwind-wasm.c
-    )
-set_source_files_properties(${LIBUNWIND_C_SOURCES}
-                            PROPERTIES
-                              COMPILE_FLAGS "-std=c99")
+  set(LIBUNWIND_ASM_SOURCES
+      UnwindRegistersRestore.S
+      UnwindRegistersSave.S
+      )
 
-set(LIBUNWIND_ASM_SOURCES
-    UnwindRegistersRestore.S
-    UnwindRegistersSave.S
-    )
+  set_source_files_properties(${LIBUNWIND_C_SOURCES}
+                              PROPERTIES
+                                COMPILE_FLAGS "-std=c99")
+endif()
 
 set(LIBUNWIND_HEADERS
     AddressSpace.hpp
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
index f7f39d38b59c18..87be87e9fd92a8 100644
--- a/libunwind/src/Unwind-wasm.c
+++ b/libunwind/src/Unwind-wasm.c
@@ -10,14 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if __STDC_VERSION__ < 202311L
 #include <stdbool.h>
-
+#endif
 #include "config.h"
-
-#ifdef __USING_WASM_EXCEPTIONS__
-
 #include "unwind.h"
-#include <threads.h>
 
 _Unwind_Reason_Code __gxx_personality_wasm0(int version, _Unwind_Action actions,
                                             uint64_t exceptionClass,
@@ -35,7 +32,12 @@ struct _Unwind_LandingPadContext {
 
 // Communication channel between compiler-generated user code and personality
 // function
-thread_local struct _Unwind_LandingPadContext __wasm_lpad_context;
+#if __STDC_VERSION__ >= 202311L
+thread_local
+#else
+_Thread_local
+#endif
+    struct _Unwind_LandingPadContext __wasm_lpad_context;
 
 /// Calls to this function is in landing pads in compiler-generated user code.
 /// In other EH schemes, stack unwinding is done by libunwind library, which
@@ -119,5 +121,3 @@ _LIBUNWIND_EXPORT uintptr_t
 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
   return 0;
 }
-
-#endif // defined(__USING_WASM_EXCEPTIONS__)
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
index deb5a4d4d73d46..2a57df41acca9f 100644
--- a/libunwind/src/config.h
+++ b/libunwind/src/config.h
@@ -66,13 +66,14 @@
   #define _LIBUNWIND_EXPORT
   #define _LIBUNWIND_HIDDEN
 #else
-  #if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX)
-    #define _LIBUNWIND_EXPORT __declspec(dllexport)
-    #define _LIBUNWIND_HIDDEN
-  #else
-    #define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
-    #define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
-  #endif
+#if !defined(__ELF__) && !defined(__MACH__) && !defined(_AIX) &&               \
+    !defined(__wasm__)
+#define _LIBUNWIND_EXPORT __declspec(dllexport)
+#define _LIBUNWIND_HIDDEN
+#else
+#define _LIBUNWIND_EXPORT __attribute__((visibility("default")))
+#define _LIBUNWIND_HIDDEN __attribute__((visibility("hidden")))
+#endif
 #endif
 
 #define STR(a) #a
diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
index 217dde90986379..cd12570da1141a 100644
--- a/libunwind/src/libunwind.cpp
+++ b/libunwind/src/libunwind.cpp
@@ -12,6 +12,7 @@
 #include <libunwind.h>
 
 #include "config.h"
+#ifndef __wasm__
 #include "libunwind_ext.h"
 
 #include <stdlib.h>
@@ -431,6 +432,7 @@ int __unw_remove_find_dynamic_unwind_sections(
 }
 
 #endif // __APPLE__
+#endif
 
 // Add logging hooks in Debug builds only
 #ifndef NDEBUG



More information about the cfe-commits mailing list