1 // Copyright 2023 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 <stdint.h> 6 7 #include <memory> 8 9 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h" 10 #include "build/buildflag.h" 11 #include "build/rust/tests/test_rust_shared_library/src/lib.rs.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 #if PA_BUILDFLAG(HAS_64_BIT_POINTERS) 15 #include "base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h" 16 #else 17 #include "base/allocator/partition_allocator/src/partition_alloc/address_pool_manager_bitmap.h" 18 #endif 19 TEST(RustSharedTest,CppCallingIntoRust_BasicFFI)20TEST(RustSharedTest, CppCallingIntoRust_BasicFFI) { 21 EXPECT_EQ(7, add_two_ints_via_rust(3, 4)); 22 } 23 TEST(RustSharedTest,RustComponentUsesPartitionAlloc)24TEST(RustSharedTest, RustComponentUsesPartitionAlloc) { 25 // Verify that PartitionAlloc is consistently used in C++ and Rust. 26 auto cpp_allocated_int = std::make_unique<int>(); 27 SomeStruct* rust_allocated_ptr = allocate_via_rust().into_raw(); 28 EXPECT_EQ(partition_alloc::IsManagedByPartitionAlloc( 29 reinterpret_cast<uintptr_t>(rust_allocated_ptr)), 30 partition_alloc::IsManagedByPartitionAlloc( 31 reinterpret_cast<uintptr_t>(cpp_allocated_int.get()))); 32 rust::Box<SomeStruct>::from_raw(rust_allocated_ptr); 33 } 34 TEST(RustSharedTest,AllocAligned)35TEST(RustSharedTest, AllocAligned) { 36 alloc_aligned(); 37 } 38