From e86e457d6c22044cfb1738815847cadd52b03b03 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 20 Oct 2017 21:28:01 +0300 Subject: [PATCH] [FreeBSD][SR-3635] Fix __gnustep_objcxx_personality_v0 symbol emitted Resolves https://bugs.swift.org/browse/SR-3635 std::string + strdup being used in the function causes some weird behavior on FreeBSD where __gnustep_objcxx_personality_v0 is emmitted. It's not happening if the code is in a .cpp file. --- stdlib/public/runtime/Demangle.cpp | 62 +++++++++++++++++++++++++++++ stdlib/public/runtime/Reflection.mm | 62 ----------------------------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/stdlib/public/runtime/Demangle.cpp b/stdlib/public/runtime/Demangle.cpp index 3f2aaa1bae64a..99cf84f37ad97 100644 --- a/stdlib/public/runtime/Demangle.cpp +++ b/stdlib/public/runtime/Demangle.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "swift/Runtime/Metadata.h" +#include "swift/Runtime/Portability.h" #include "swift/Strings.h" #include "Private.h" @@ -415,3 +416,64 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type, // Not a type. return nullptr; } + +// NB: This function is not used directly in the Swift codebase, but is +// exported for Xcode support and is used by the sanitizers. Please coordinate +// before changing. +// +/// Demangles a Swift symbol name. +/// +/// \param mangledName is the symbol name that needs to be demangled. +/// \param mangledNameLength is the length of the string that should be +/// demangled. +/// \param outputBuffer is the user provided buffer where the demangled name +/// will be placed. If nullptr, a new buffer will be malloced. In that case, +/// the user of this API is responsible for freeing the returned buffer. +/// \param outputBufferSize is the size of the output buffer. If the demangled +/// name does not fit into the outputBuffer, the output will be truncated and +/// the size will be updated, indicating how large the buffer should be. +/// \param flags can be used to select the demangling style. TODO: We should +//// define what these will be. +/// \returns the demangled name. Returns nullptr if the input String is not a +/// Swift mangled name. +SWIFT_RUNTIME_EXPORT +char *swift_demangle(const char *mangledName, + size_t mangledNameLength, + char *outputBuffer, + size_t *outputBufferSize, + uint32_t flags) { + if (flags != 0) { + swift::fatalError(0, "Only 'flags' value of '0' is currently supported."); + } + if (outputBuffer != nullptr && outputBufferSize == nullptr) { + swift::fatalError(0, "'outputBuffer' is passed but the size is 'nullptr'."); + } + + // Check if we are dealing with Swift mangled name, otherwise, don't try + // to demangle and send indication to the user. + if (!Demangle::isSwiftSymbol(mangledName)) + return nullptr; // Not a mangled name + + // Demangle the name. + auto options = Demangle::DemangleOptions(); + options.DisplayDebuggerGeneratedModule = false; + auto result = + Demangle::demangleSymbolAsString(mangledName, + mangledNameLength, + options); + + // If the output buffer is not provided, malloc memory ourselves. + if (outputBuffer == nullptr || *outputBufferSize == 0) { + return strdup(result.c_str()); + } + + // Indicate a failure if the result does not fit and will be truncated + // and set the required outputBufferSize. + if (*outputBufferSize < result.length() + 1) { + *outputBufferSize = result.length() + 1; + } + + // Copy into the provided buffer. + _swift_strlcpy(outputBuffer, result.c_str(), *outputBufferSize); + return outputBuffer; +} diff --git a/stdlib/public/runtime/Reflection.mm b/stdlib/public/runtime/Reflection.mm index f40781171bbc6..2089dfd249213 100644 --- a/stdlib/public/runtime/Reflection.mm +++ b/stdlib/public/runtime/Reflection.mm @@ -19,7 +19,6 @@ #include "swift/Runtime/Unreachable.h" #include "swift/Demangling/Demangle.h" #include "swift/Runtime/Debug.h" -#include "swift/Runtime/Portability.h" #include "Private.h" #include "WeakReference.h" #include "llvm/Support/Compiler.h" @@ -1080,64 +1079,3 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup, T->vw_destroy(value); return MirrorReturn(result); } - -// NB: This function is not used directly in the Swift codebase, but is -// exported for Xcode support and is used by the sanitizers. Please coordinate -// before changing. -// -/// Demangles a Swift symbol name. -/// -/// \param mangledName is the symbol name that needs to be demangled. -/// \param mangledNameLength is the length of the string that should be -/// demangled. -/// \param outputBuffer is the user provided buffer where the demangled name -/// will be placed. If nullptr, a new buffer will be malloced. In that case, -/// the user of this API is responsible for freeing the returned buffer. -/// \param outputBufferSize is the size of the output buffer. If the demangled -/// name does not fit into the outputBuffer, the output will be truncated and -/// the size will be updated, indicating how large the buffer should be. -/// \param flags can be used to select the demangling style. TODO: We should -//// define what these will be. -/// \returns the demangled name. Returns nullptr if the input String is not a -/// Swift mangled name. -SWIFT_RUNTIME_EXPORT -char *swift_demangle(const char *mangledName, - size_t mangledNameLength, - char *outputBuffer, - size_t *outputBufferSize, - uint32_t flags) { - if (flags != 0) { - swift::fatalError(0, "Only 'flags' value of '0' is currently supported."); - } - if (outputBuffer != nullptr && outputBufferSize == nullptr) { - swift::fatalError(0, "'outputBuffer' is passed but the size is 'nullptr'."); - } - - // Check if we are dealing with Swift mangled name, otherwise, don't try - // to demangle and send indication to the user. - if (!Demangle::isSwiftSymbol(mangledName)) - return nullptr; // Not a mangled name - - // Demangle the name. - auto options = Demangle::DemangleOptions(); - options.DisplayDebuggerGeneratedModule = false; - auto result = - Demangle::demangleSymbolAsString(mangledName, - mangledNameLength, - options); - - // If the output buffer is not provided, malloc memory ourselves. - if (outputBuffer == nullptr || *outputBufferSize == 0) { - return strdup(result.c_str()); - } - - // Indicate a failure if the result does not fit and will be truncated - // and set the required outputBufferSize. - if (*outputBufferSize < result.length() + 1) { - *outputBufferSize = result.length() + 1; - } - - // Copy into the provided buffer. - _swift_strlcpy(outputBuffer, result.c_str(), *outputBufferSize); - return outputBuffer; -}