| 1 | // Copyright 2026 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Compatibility shim for Android NDK ABI changes. |
| 16 | // |
| 17 | // The WebRTC M150 prebuilt library was compiled against an older Android NDK |
| 18 | // (via Chromium's bundled toolchain) in which `std::__ndk1::__hash_memory` is |
| 19 | // an out-of-line function exported from `libc++_static.a`. Starting with NDK |
| 20 | // r28 (LLVM 18), this function was annotated with `_LIBCPP_HIDE_FROM_ABI` and |
| 21 | // is no longer present as an exported symbol. This causes a link failure when |
| 22 | // the prebuilt is linked against a newer system NDK. |
| 23 | // |
| 24 | // This file provides a **weak** fallback definition so that the prebuilt can be |
| 25 | // linked against any NDK version: |
| 26 | // - With NDK r27 and older: the strong definition from `libc++_static.a` |
| 27 | // overrides this weak one; behaviour is identical to before. |
| 28 | // - With NDK r28 and newer: this weak definition is used; it implements the |
| 29 | // same FNV-1a algorithm that LLVM libc++ historically used. |
| 30 | |
| 31 | #if defined(__ANDROID__) |
| 32 | #include <cstddef> |
| 33 | |
| 34 | namespace std { |
| 35 | inline namespace __ndk1 { |
| 36 | |
| 37 | __attribute__((weak)) size_t __hash_memory(void const* p, size_t n) noexcept { |
| 38 | auto const* ptr = static_cast<unsigned char const*>(p); |
| 39 | #if defined(__LP64__) |
| 40 | // 64-bit FNV-1a |
| 41 | size_t hash = 14695981039346656037ULL; |
| 42 | for (size_t i = 0; i < n; ++i) { |
| 43 | hash ^= static_cast<size_t>(ptr[i]); |
| 44 | hash *= 1099511628211ULL; |
| 45 | } |
| 46 | #else |
| 47 | // 32-bit FNV-1a |
| 48 | size_t hash = 2166136261U; |
| 49 | for (size_t i = 0; i < n; ++i) { |
| 50 | hash ^= static_cast<size_t>(ptr[i]); |
| 51 | hash *= 16777619U; |
| 52 | } |
| 53 | #endif |
| 54 | return hash; |
| 55 | } |
| 56 | |
| 57 | } // namespace __ndk1 |
| 58 | } // namespace std |
| 59 | #endif // defined(__ANDROID__) |