xref: /aosp_15_r20/external/cronet/base/files/scoped_file_android.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/files/scoped_file.h"
6 
7 #include <stdint.h>
8 
9 // Copied from <android/fdsan.h>.
10 // This can go away once this header is included in our copy of the NDK.
11 extern "C" {
12 void android_fdsan_exchange_owner_tag(int fd,
13                                       uint64_t expected_tag,
14                                       uint64_t new_tag)
15     __attribute__((__weak__));
16 }
17 
18 namespace base {
19 namespace internal {
20 
ScopedFDToTag(const ScopedFD & owner)21 static uint64_t ScopedFDToTag(const ScopedFD& owner) {
22   return reinterpret_cast<uint64_t>(&owner);
23 }
24 
25 // static
Acquire(const ScopedFD & owner,int fd)26 void ScopedFDCloseTraits::Acquire(const ScopedFD& owner, int fd) {
27   if (android_fdsan_exchange_owner_tag) {
28     android_fdsan_exchange_owner_tag(fd, 0, ScopedFDToTag(owner));
29   }
30 }
31 
32 // static
Release(const ScopedFD & owner,int fd)33 void ScopedFDCloseTraits::Release(const ScopedFD& owner, int fd) {
34   if (android_fdsan_exchange_owner_tag) {
35     android_fdsan_exchange_owner_tag(fd, ScopedFDToTag(owner), 0);
36   }
37 }
38 
39 }  // namespace internal
40 }  // namespace base
41