Skip to content

Commit

Permalink
wrap and export __lsan_ignore_object
Browse files Browse the repository at this point in the history
Summary: As we do for some of the other sanitizer interface points.

Reviewed By: luciang, dmm-fb

Differential Revision: D40360035

fbshipit-source-id: 8f0f58495e93f9a5a061eb29db89ac176a8664f9
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Oct 17, 2022
1 parent 48008f4 commit 73e5f8c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
23 changes: 22 additions & 1 deletion folly/memory/SanitizeLeak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,34 @@
* limitations under the License.
*/

#include <folly/memory/SanitizeLeak.h>

#include <mutex>
#include <unordered_set>

#include <folly/memory/SanitizeLeak.h>
#include <folly/lang/Extern.h>

// Leak Sanitizer interface may be found at:
// https://github.com/llvm/llvm-project/blob/main/compiler-rt/include/sanitizer/lsan_interface.h
extern "C" void __lsan_ignore_object(void const*);

namespace {

FOLLY_CREATE_EXTERN_ACCESSOR( //
lsan_ignore_object_access_v,
__lsan_ignore_object);

constexpr bool E = folly::kIsLibrarySanitizeAddress;

} // namespace

namespace folly {

namespace detail {

FOLLY_STORAGE_CONSTEXPR lsan_ignore_object_t* const //
lsan_ignore_object_v = lsan_ignore_object_access_v<E>;

namespace {
struct LeakedPtrs {
std::mutex mutex;
Expand Down
15 changes: 15 additions & 0 deletions folly/memory/SanitizeLeak.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,25 @@
namespace folly {

namespace detail {

using lsan_ignore_object_t = void(void const*);

extern lsan_ignore_object_t* const lsan_ignore_object_v;

void annotate_object_leaked_impl(void const* ptr);
void annotate_object_collected_impl(void const* ptr);

} // namespace detail

// lsan_ignore_object
//
// Marks an allocation to be treated as a root when Leak Sanitizer scans for
// leaked allocations.
FOLLY_ALWAYS_INLINE static void lsan_ignore_object(void const* const ptr) {
auto fun = detail::lsan_ignore_object_v;
return kIsSanitizeAddress && fun ? fun(ptr) : void();
}

/**
* When the current compilation unit is being compiled with ASAN enabled this
* function will suppress an intentionally leaked pointer from the LSAN report.
Expand Down
7 changes: 7 additions & 0 deletions folly/memory/test/SanitizeLeakTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

namespace folly {

TEST(SanitizeLeak, lsan_ignore_object) {
int* ptr = new int(5);
EXPECT_EQ(*ptr, 5);
lsan_ignore_object(ptr);
EXPECT_EQ(*ptr, 5);
}

TEST(SanitizeLeak, ImplementationAlwaysWorks) {
int* ptr = new int(5);
EXPECT_EQ(*ptr, 5);
Expand Down

0 comments on commit 73e5f8c

Please sign in to comment.