diff --git a/common/util/Util/FFI.hs b/common/util/Util/FFI.hs index 5ff71607..0626fd62 100644 --- a/common/util/Util/FFI.hs +++ b/common/util/Util/FFI.hs @@ -18,9 +18,6 @@ import System.IO.Unsafe (unsafePerformIO) import Control.Exception import Control.Monad -foreign import ccall unsafe "&hs_ffi_free_error" hs_ffi_free_error - :: FunPtr (CString -> IO ()) - newtype FFIError = FFIError (ForeignPtr CChar) ffiErrorMessage :: FFIError -> String @@ -39,7 +36,13 @@ call :: IO CString -> IO () call f = do p <- f when (p /= nullPtr) $ do - fp <- newForeignPtr hs_ffi_free_error p + fp <- do + b <- peek p + -- A '\1' prefix indicates that we shouldn't free the string (e.g., + -- because it's static) - cf. ffi::wrap. + if b /= toEnum 1 + then newForeignPtr finalizerFree p + else newForeignPtr_ (p `plusPtr` 1) throwIO $ FFIError fp infixr 5 :> diff --git a/common/util/cpp/ffi.cpp b/common/util/cpp/ffi.cpp deleted file mode 100644 index 646df50a..00000000 --- a/common/util/cpp/ffi.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "cpp/ffi.h" -#include "cpp/wrap.h" - -namespace facebook { -namespace hs { -namespace ffi { - -const char* outOfMemory = "out of memory"; -const char* unknownError = "unknown error"; - -} // namespace ffi -} // namespace hs -} // namespace facebook - -extern "C" { - -void hs_ffi_free_error(const char* err) { - if (err != facebook::hs::ffi::outOfMemory && - err != facebook::hs::ffi::unknownError) { - std::free(const_cast(err)); - } -} -} diff --git a/common/util/cpp/ffi.h b/common/util/cpp/ffi.h deleted file mode 100644 index d244e172..00000000 --- a/common/util/cpp/ffi.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -void hs_ffi_free_error(const char* error); - -#ifdef __cplusplus -} -#endif diff --git a/common/util/cpp/wrap.h b/common/util/cpp/wrap.h index bed22457..d1264721 100644 --- a/common/util/cpp/wrap.h +++ b/common/util/cpp/wrap.h @@ -15,11 +15,15 @@ namespace facebook { namespace hs { namespace ffi { -extern const char* outOfMemory; -extern const char* unknownError; - template const char* wrap(F&& f) noexcept { + // The '\1' prefix instructs the marshaller in FFI.hs to not free those + // strings. The prefix itself will be stripped out. This is only really + // necessary specifically for the outOfMemory message where we might not be + // able to allocate a new message string - although arguably, we might just + // want to abort in such a case. + static const char *outOfMemory = "\1out of memory"; + static const char *unknownError = "\1unknown error"; try { f(); return nullptr; diff --git a/common/util/fb-util.cabal b/common/util/fb-util.cabal index 48544269..6ffd9a1a 100644 --- a/common/util/fb-util.cabal +++ b/common/util/fb-util.cabal @@ -156,7 +156,6 @@ library cxx-sources: cpp/cdynamic.cpp - cpp/ffi.cpp cpp/logging.cpp cpp/HsStruct.cpp cpp/IOBuf.cpp @@ -171,7 +170,6 @@ library cpp/HsStdVariant.h cpp/HsStruct.h cpp/HsStructDefines.h - cpp/ffi.h cpp/memory.h cpp/wrap.h Util/AsanAlloc.h