Hash :
25390156
Author :
Date :
2025-08-21T00:13:19
Suppress unsafe buffers on a file-by-file basis in src/ [1 of N] In this CL, we suppress many files but stop short of actually enabling the warning by not removing the line from the unsafe_buffers_paths.txt file. That will happen in a follow-on CL, along with resolving any stragglers missed here. This is mostly a manual change so as to familiarize myself with the kinds of issues faced by the Angle codebase when applying buffer safety warnings. -- Re-generate affected hashes. -- Clang-format applied to all changed files. -- Add a few missing .reserve() calls to vectors as noticed. -- Fix some mismatches between file names and header comments. -- Be more consistent with header comment format (blank lines and trailing //-only lines when a filename comment adjoins license boilerplate). Bug: b/436880895 Change-Id: I3bde5cc2059acbe8345057289214f1a26f1c34aa Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6869022 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ExternalBufferTest:
// Tests the correctness of external buffer ext extension.
//
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
#include "common/android_util.h"
#if defined(ANGLE_PLATFORM_ANDROID) && __ANDROID_API__ >= 26
# define ANGLE_AHARDWARE_BUFFER_SUPPORT
// NDK header file for access to Android Hardware Buffers
# include <android/hardware_buffer.h>
#endif
namespace angle
{
class ExternalBufferTestES31 : public ANGLETest<>
{
protected:
ExternalBufferTestES31()
{
setWindowWidth(16);
setWindowHeight(16);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
}
AHardwareBuffer *createAndroidHardwareBuffer(size_t size, const GLubyte *data)
{
#if defined(ANGLE_AHARDWARE_BUFFER_SUPPORT)
// The height and width are number of pixels of size format
AHardwareBuffer_Desc aHardwareBufferDescription = {};
aHardwareBufferDescription.width = size;
aHardwareBufferDescription.height = 1;
aHardwareBufferDescription.layers = 1;
aHardwareBufferDescription.format = AHARDWAREBUFFER_FORMAT_BLOB;
aHardwareBufferDescription.usage =
AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER;
aHardwareBufferDescription.stride = 0;
// Allocate memory from Android Hardware Buffer
AHardwareBuffer *aHardwareBuffer = nullptr;
EXPECT_EQ(0, AHardwareBuffer_allocate(&aHardwareBufferDescription, &aHardwareBuffer));
void *mappedMemory = nullptr;
EXPECT_EQ(0, AHardwareBuffer_lock(aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY,
-1, nullptr, &mappedMemory));
// Need to grab the stride the implementation might have enforced
AHardwareBuffer_describe(aHardwareBuffer, &aHardwareBufferDescription);
if (data)
{
memcpy(mappedMemory, data, size);
}
EXPECT_EQ(0, AHardwareBuffer_unlock(aHardwareBuffer, nullptr));
return aHardwareBuffer;
#else
return nullptr;
#endif // ANGLE_PLATFORM_ANDROID
}
void destroyAndroidHardwareBuffer(AHardwareBuffer *aHardwareBuffer)
{
#if defined(ANGLE_AHARDWARE_BUFFER_SUPPORT)
AHardwareBuffer_release(aHardwareBuffer);
#endif
}
void *lockAndroidHardwareBuffer(AHardwareBuffer *aHardwareBuffer)
{
void *data = nullptr;
#if defined(ANGLE_AHARDWARE_BUFFER_SUPPORT)
EXPECT_EQ(0, AHardwareBuffer_lock(aHardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY,
-1, nullptr, &data));
#endif
return data;
}
void unlockAndroidHardwareBuffer(AHardwareBuffer *aHardwareBuffer)
{
#if defined(ANGLE_AHARDWARE_BUFFER_SUPPORT)
AHardwareBuffer_unlock(aHardwareBuffer, nullptr);
#endif
}
};
// Testing subdata update with external buffer from AHB
TEST_P(ExternalBufferTestES31, BufferSubData)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
constexpr uint8_t kBufferSize = 16;
std::vector<GLubyte> initData(kBufferSize, 0xA);
// Create the Image
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = GL_DYNAMIC_STORAGE_BIT_EXT;
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
GLBuffer buffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
std::vector<GLubyte> expectedData(kBufferSize, 0xFF);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize, expectedData.data());
glFinish();
ASSERT_GL_NO_ERROR();
// Inspect the data written into the buffer using CPU access.
uint8_t *data = static_cast<uint8_t *>(lockAndroidHardwareBuffer(aHardwareBuffer));
for (uint32_t i = 0; i < kBufferSize; ++i)
{
EXPECT_EQ(data[i], 0xFF);
}
unlockAndroidHardwareBuffer(aHardwareBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
buffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
// Verify that subdata updates to an external buffer backed by an AHB doesn't orphan the AHB
TEST_P(ExternalBufferTestES31, SubDataDoesNotCauseOrphaning)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
constexpr uint8_t kBufferSize = 16;
std::vector<GLubyte> initData(kBufferSize, 0xA);
// Create the AHB
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = GL_DYNAMIC_STORAGE_BIT_EXT;
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
// Create externalBuffer
GLBuffer externalBuffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, externalBuffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
// Create a copy read buffer
std::vector<GLubyte> copyReadBufferData(kBufferSize, 0xB);
GLBuffer copyReadBuffer;
glBindBuffer(GL_COPY_READ_BUFFER, copyReadBuffer);
glBufferData(GL_COPY_READ_BUFFER, kBufferSize, copyReadBufferData.data(), GL_STATIC_READ);
ASSERT_GL_NO_ERROR();
// Copy from copyReadBuffer to externalBuffer
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_SHADER_STORAGE_BUFFER, 0, 0, kBufferSize);
ASSERT_GL_NO_ERROR();
// Update externalBuffer
std::vector<GLubyte> expectedData(kBufferSize, 0xFF);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize, expectedData.data());
glFinish();
ASSERT_GL_NO_ERROR();
// Inspect the data written into the AHB, through externalBuffer, using CPU access.
uint8_t *data = static_cast<uint8_t *>(lockAndroidHardwareBuffer(aHardwareBuffer));
for (uint32_t i = 0; i < kBufferSize; ++i)
{
EXPECT_EQ(data[i], 0xFF);
}
unlockAndroidHardwareBuffer(aHardwareBuffer);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
copyReadBuffer.reset();
externalBuffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
// Testing dispatch compute shader external from source AHB
TEST_P(ExternalBufferTestES31, DispatchCompute)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
constexpr char kCS[] = R"(#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(std430, binding=0) buffer Output {
uint data[];
} bOutput;
void main() {
bOutput.data[gl_GlobalInvocationID.x] =
gl_GlobalInvocationID.x * 3u;
}
)";
constexpr uint8_t kBufferSize = 16 * 4;
std::vector<GLubyte> initData(kBufferSize, 0xA);
// Create the Image
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = GL_MAP_READ_BIT;
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
GLBuffer buffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
GLProgram program;
program.makeCompute(kCS);
ASSERT_NE(program, 0U);
ASSERT_GL_NO_ERROR();
glUseProgram(program);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffer);
glDispatchCompute(kBufferSize, 1, 1);
glFinish();
ASSERT_GL_NO_ERROR();
// Inspect the data written into the buffer using CPU access.
uint32_t *data = static_cast<uint32_t *>(lockAndroidHardwareBuffer(aHardwareBuffer));
for (uint32_t i = 0; i < (kBufferSize / sizeof(uint32_t)); ++i)
{
EXPECT_EQ(data[i], static_cast<uint32_t>(i * 3));
}
unlockAndroidHardwareBuffer(aHardwareBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
buffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
// Test interaction between GL_OES_mapbuffer and GL_EXT_external_buffer extensions.
TEST_P(ExternalBufferTestES31, MapBuffer)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage") ||
!IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
constexpr uint8_t kBufferSize = 16;
std::vector<GLubyte> initData(kBufferSize, 0xFF);
// Create the AHB
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = (GL_MAP_READ_BIT_EXT | GL_MAP_WRITE_BIT_EXT);
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
GLBuffer buffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
// Inspect the data written into the buffer using CPU access.
uint8_t *data = static_cast<uint8_t *>(
glMapBufferRangeEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize, GL_MAP_READ_BIT_EXT));
ASSERT_GL_NO_ERROR();
for (uint32_t i = 0; i < kBufferSize; ++i)
{
EXPECT_EQ(data[i], 0xFF);
}
glUnmapBufferOES(GL_SHADER_STORAGE_BUFFER);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
buffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
// Verify that mapping an external buffer backed by an AHB doesn't orphan the AHB
TEST_P(ExternalBufferTestES31, MapBufferDoesNotCauseOrphaning)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage") ||
!IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
constexpr uint8_t kBufferSize = 16;
std::vector<GLubyte> initData(kBufferSize, 0xA);
// Create the AHB
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags =
(GL_MAP_READ_BIT_EXT | GL_MAP_WRITE_BIT_EXT | GL_DYNAMIC_STORAGE_BIT_EXT);
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
GLBuffer buffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
// Create a copy read buffer
std::vector<GLubyte> copyReadBufferData(kBufferSize, 0xB);
GLBuffer copyReadBuffer;
glBindBuffer(GL_COPY_READ_BUFFER, copyReadBuffer);
glBufferData(GL_COPY_READ_BUFFER, kBufferSize, copyReadBufferData.data(), GL_STATIC_READ);
ASSERT_GL_NO_ERROR();
// Copy from copyReadBuffer to externalBuffer
glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_SHADER_STORAGE_BUFFER, 0, 0, kBufferSize);
ASSERT_GL_NO_ERROR();
// Inspect the data written into the buffer using map buffer API.
constexpr GLbitfield kMapFlags = (GL_MAP_WRITE_BIT_EXT | GL_MAP_INVALIDATE_BUFFER_BIT);
uint8_t *mapData = static_cast<uint8_t *>(
glMapBufferRangeEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize, kMapFlags));
ASSERT_GL_NO_ERROR();
EXPECT_NE(mapData, nullptr);
glUnmapBufferOES(GL_SHADER_STORAGE_BUFFER);
// Update externalBuffer
std::vector<GLubyte> expectedData(kBufferSize, 0xFF);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize, expectedData.data());
glFinish();
ASSERT_GL_NO_ERROR();
// Inspect the data written into the AHB, through externalBuffer, using CPU access.
uint8_t *data = static_cast<uint8_t *>(lockAndroidHardwareBuffer(aHardwareBuffer));
for (uint32_t i = 0; i < kBufferSize; ++i)
{
EXPECT_EQ(data[i], 0xFF);
}
unlockAndroidHardwareBuffer(aHardwareBuffer);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
copyReadBuffer.reset();
buffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
// Verify that create and destroy external buffer backed by an AHB doesn't leak AHB
TEST_P(ExternalBufferTestES31, BufferDoesNotLeakAHB)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
// Create and destroy 128M AHB backed buffer in a loop. If we leak AHB, it will fail due to AHB
// allocation failure before loop ends.
constexpr size_t kBufferSize = 128 * 1024 * 1024;
for (int loop = 0; loop < 1000; loop++)
{
// Create the AHB
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = GL_DYNAMIC_STORAGE_BIT_EXT;
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, nullptr);
GLBuffer buffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBufferStorageExternalEXT(GL_SHADER_STORAGE_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
buffer.reset();
// Delete the source AHB
destroyAndroidHardwareBuffer(aHardwareBuffer);
}
}
// Test that checks buffer object state after calling glBufferStorageExternalEXT
TEST_P(ExternalBufferTestES31, getBufferParameter)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_external_buffer") ||
!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
GLint64 value = -1;
constexpr uint8_t kBufferSize = 3;
std::vector<GLubyte> initData(kBufferSize, 0xA);
// Create the Image
AHardwareBuffer *aHardwareBuffer;
constexpr GLbitfield kFlags = GL_MAP_WRITE_BIT;
aHardwareBuffer = createAndroidHardwareBuffer(kBufferSize, initData.data());
GLBuffer buffer;
glBindBuffer(GL_COPY_READ_BUFFER, buffer);
glBufferStorageExternalEXT(GL_COPY_READ_BUFFER, 0, kBufferSize,
eglGetNativeClientBufferANDROID(aHardwareBuffer), kFlags);
ASSERT_GL_NO_ERROR();
/* Check the value of GL_BUFFER_SIZE. */
glGetBufferParameteri64v(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &value);
ASSERT_GL_NO_ERROR();
ASSERT_EQ(kBufferSize, static_cast<GLint>(value));
/* Check the value of GL_BUFFER_USAGE. */
value = -1;
glGetBufferParameteri64v(GL_COPY_READ_BUFFER, GL_BUFFER_USAGE, &value);
ASSERT_GL_NO_ERROR();
ASSERT_EQ(GL_DYNAMIC_DRAW, value);
/* Check the value of GL_BUFFER_IMMUTABLE_STORAGE_EXT. */
value = -1;
glGetBufferParameteri64v(GL_COPY_READ_BUFFER, GL_BUFFER_IMMUTABLE_STORAGE_EXT, &value);
ASSERT_GL_NO_ERROR();
ASSERT_EQ(GL_TRUE, value);
/* Check the value of GL_BUFFER_STORAGE_FLAGS_EXT. */
value = -1;
glGetBufferParameteri64v(GL_COPY_READ_BUFFER, GL_BUFFER_STORAGE_FLAGS_EXT, &value);
ASSERT_GL_NO_ERROR();
ASSERT_EQ(kFlags, value);
/* Clean up. */
glUnmapBuffer(GL_COPY_READ_BUFFER);
glBindBuffer(GL_COPY_READ_BUFFER, 0);
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ExternalBufferTestES31);
ANGLE_INSTANTIATE_TEST_ES31(ExternalBufferTestES31);
} // namespace angle