Skip to content

Commit 6c65610

Browse files
Merge "libvulkan: refactor for _FORTIFY_SOURCE=3 support." into main am: 7827597
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/3542879 Change-Id: I390d6f24d160f747aeaa7cf24e0f1c1515fb29f0 Signed-off-by: Automerger Merge Worker <[email protected]>
2 parents 3ea3bd3 + 7827597 commit 6c65610

File tree

5 files changed

+171
-84
lines changed

5 files changed

+171
-84
lines changed

vulkan/libvulkan/Android.bp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,9 @@ cc_aconfig_library {
4141
aconfig_declarations: "libvulkan_flags",
4242
}
4343

44-
cc_library_shared {
45-
name: "libvulkan",
46-
llndk: {
47-
symbol_file: "libvulkan.map.txt",
48-
export_llndk_headers: [
49-
"vulkan_headers",
50-
],
51-
},
44+
cc_defaults {
45+
name: "libvulkan_defaults",
46+
5247
sanitize: {
5348
misc_undefined: ["integer"],
5449
},
@@ -81,6 +76,34 @@ cc_library_shared {
8176
"-Wno-global-constructors",
8277
"-Wno-zero-length-array",
8378
],
79+
}
80+
81+
cc_library {
82+
name: "libvulkanallocator",
83+
defaults: ["libvulkan_defaults"],
84+
cflags: [
85+
// This code uses malloc_usable_size(),
86+
// and thus can't be built with _FORTIFY_SOURCE=3.
87+
"-U_FORTIFY_SOURCE",
88+
"-D_FORTIFY_SOURCE=2",
89+
],
90+
srcs: [
91+
"allocator.cpp",
92+
],
93+
header_libs: [
94+
"vulkan_headers",
95+
],
96+
}
97+
98+
cc_library_shared {
99+
name: "libvulkan",
100+
defaults: ["libvulkan_defaults"],
101+
llndk: {
102+
symbol_file: "libvulkan.map.txt",
103+
export_llndk_headers: [
104+
"vulkan_headers",
105+
],
106+
},
84107

85108
srcs: [
86109
"api.cpp",
@@ -124,6 +147,7 @@ cc_library_shared {
124147
],
125148
static_libs: [
126149
"libgrallocusage",
150+
"libvulkanallocator",
127151
"libvulkanflags",
128152
],
129153
}

vulkan/libvulkan/allocator.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18+
19+
#include "allocator.h"
20+
21+
#include <stdlib.h>
22+
23+
#include <algorithm>
24+
25+
#include <log/log.h>
26+
27+
// #define ENABLE_ALLOC_CALLSTACKS 1
28+
#if ENABLE_ALLOC_CALLSTACKS
29+
#include <utils/CallStack.h>
30+
#define ALOGD_CALLSTACK(...) \
31+
do { \
32+
ALOGD(__VA_ARGS__); \
33+
android::CallStack callstack; \
34+
callstack.update(); \
35+
callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
36+
} while (false)
37+
#else
38+
#define ALOGD_CALLSTACK(...) \
39+
do { \
40+
} while (false)
41+
#endif
42+
43+
namespace vulkan {
44+
namespace driver {
45+
46+
namespace {
47+
48+
VKAPI_ATTR void* DefaultAllocate(void*,
49+
size_t size,
50+
size_t alignment,
51+
VkSystemAllocationScope) {
52+
void* ptr = nullptr;
53+
// Vulkan requires 'alignment' to be a power of two, but posix_memalign
54+
// additionally requires that it be at least sizeof(void*).
55+
int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
56+
ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
57+
ret, ptr);
58+
return ret == 0 ? ptr : nullptr;
59+
}
60+
61+
// This function is marked `noinline` so that LLVM can't infer an object size
62+
// for FORTIFY through it, given that it's abusing malloc_usable_size().
63+
__attribute__((__noinline__))
64+
VKAPI_ATTR void* DefaultReallocate(void*,
65+
void* ptr,
66+
size_t size,
67+
size_t alignment,
68+
VkSystemAllocationScope) {
69+
if (size == 0) {
70+
free(ptr);
71+
return nullptr;
72+
}
73+
74+
// TODO(b/143295633): Right now we never shrink allocations; if the new
75+
// request is smaller than the existing chunk, we just continue using it.
76+
// Right now the loader never reallocs, so this doesn't matter. If that
77+
// changes, or if this code is copied into some other project, this should
78+
// probably have a heuristic to allocate-copy-free when doing so will save
79+
// "enough" space.
80+
size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
81+
if (size <= old_size)
82+
return ptr;
83+
84+
void* new_ptr = nullptr;
85+
if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
86+
return nullptr;
87+
if (ptr) {
88+
memcpy(new_ptr, ptr, std::min(old_size, size));
89+
free(ptr);
90+
}
91+
return new_ptr;
92+
}
93+
94+
VKAPI_ATTR void DefaultFree(void*, void* ptr) {
95+
ALOGD_CALLSTACK("Free: %p", ptr);
96+
free(ptr);
97+
}
98+
99+
} // anonymous namespace
100+
101+
const VkAllocationCallbacks& GetDefaultAllocator() {
102+
static const VkAllocationCallbacks kDefaultAllocCallbacks = {
103+
.pUserData = nullptr,
104+
.pfnAllocation = DefaultAllocate,
105+
.pfnReallocation = DefaultReallocate,
106+
.pfnFree = DefaultFree,
107+
};
108+
109+
return kDefaultAllocCallbacks;
110+
}
111+
112+
} // namespace driver
113+
} // namespace vulkan

vulkan/libvulkan/allocator.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <vulkan/vulkan.h>
18+
19+
namespace vulkan {
20+
namespace driver {
21+
22+
const VkAllocationCallbacks& GetDefaultAllocator();
23+
24+
} // namespace driver
25+
} // namespace vulkan

vulkan/libvulkan/driver.cpp

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ using namespace com::android::graphics::libvulkan;
5050

5151
extern "C" android_namespace_t* android_get_exported_namespace(const char*);
5252

53-
// #define ENABLE_ALLOC_CALLSTACKS 1
54-
#if ENABLE_ALLOC_CALLSTACKS
55-
#include <utils/CallStack.h>
56-
#define ALOGD_CALLSTACK(...) \
57-
do { \
58-
ALOGD(__VA_ARGS__); \
59-
android::CallStack callstack; \
60-
callstack.update(); \
61-
callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
62-
} while (false)
63-
#else
64-
#define ALOGD_CALLSTACK(...) \
65-
do { \
66-
} while (false)
67-
#endif
68-
6953
namespace vulkan {
7054
namespace driver {
7155

@@ -829,54 +813,6 @@ void CreateInfoWrapper::FilterExtension(const char* name) {
829813
}
830814
}
831815

832-
VKAPI_ATTR void* DefaultAllocate(void*,
833-
size_t size,
834-
size_t alignment,
835-
VkSystemAllocationScope) {
836-
void* ptr = nullptr;
837-
// Vulkan requires 'alignment' to be a power of two, but posix_memalign
838-
// additionally requires that it be at least sizeof(void*).
839-
int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
840-
ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
841-
ret, ptr);
842-
return ret == 0 ? ptr : nullptr;
843-
}
844-
845-
VKAPI_ATTR void* DefaultReallocate(void*,
846-
void* ptr,
847-
size_t size,
848-
size_t alignment,
849-
VkSystemAllocationScope) {
850-
if (size == 0) {
851-
free(ptr);
852-
return nullptr;
853-
}
854-
855-
// TODO(b/143295633): Right now we never shrink allocations; if the new
856-
// request is smaller than the existing chunk, we just continue using it.
857-
// Right now the loader never reallocs, so this doesn't matter. If that
858-
// changes, or if this code is copied into some other project, this should
859-
// probably have a heuristic to allocate-copy-free when doing so will save
860-
// "enough" space.
861-
size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
862-
if (size <= old_size)
863-
return ptr;
864-
865-
void* new_ptr = nullptr;
866-
if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
867-
return nullptr;
868-
if (ptr) {
869-
memcpy(new_ptr, ptr, std::min(old_size, size));
870-
free(ptr);
871-
}
872-
return new_ptr;
873-
}
874-
875-
VKAPI_ATTR void DefaultFree(void*, void* ptr) {
876-
ALOGD_CALLSTACK("Free: %p", ptr);
877-
free(ptr);
878-
}
879-
880816
InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
881817
void* data_mem = allocator.pfnAllocation(
882818
allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
@@ -916,17 +852,6 @@ bool OpenHAL() {
916852
return Hal::Open();
917853
}
918854

919-
const VkAllocationCallbacks& GetDefaultAllocator() {
920-
static const VkAllocationCallbacks kDefaultAllocCallbacks = {
921-
.pUserData = nullptr,
922-
.pfnAllocation = DefaultAllocate,
923-
.pfnReallocation = DefaultReallocate,
924-
.pfnFree = DefaultFree,
925-
};
926-
927-
return kDefaultAllocCallbacks;
928-
}
929-
930855
PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
931856
const ProcHook* hook = GetProcHook(pName);
932857
if (!hook)

vulkan/libvulkan/driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <vulkan/vulkan.h>
2828
#include <hardware/hwvulkan.h>
2929

30+
#include "allocator.h"
3031
#include "api_gen.h"
3132
#include "driver_gen.h"
3233
#include "debug_report.h"
@@ -102,7 +103,6 @@ struct DeviceData {
102103
};
103104

104105
bool OpenHAL();
105-
const VkAllocationCallbacks& GetDefaultAllocator();
106106

107107
void QueryPresentationProperties(
108108
VkPhysicalDevice physicalDevice,

0 commit comments

Comments
 (0)