OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] protect USM (#38790)
Michal Miotk5f6bbdfdf466
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cppindex cf10f72b530..80939d2c3ee 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp@@ -212,9 +212,16 @@ memory::ptr ocl_engine::create_subbuffer(const memory& memory, const layout& newif (new_layout.format.is_image_2d()) {OPENVINO_NOT_IMPLEMENTED;} else if (memory_capabilities::is_usm_type(memory.get_allocation_type())) {+ OPENVINO_ASSERT(byte_offset + new_layout.bytes_count() <= memory.size(),+ "[GPU] Sub-buffer size (",+ new_layout.bytes_count(),+ ") + offset (",+ byte_offset,+ ") exceeds parent buffer size (",+ memory.size(),+ ")");const auto& new_buf = reinterpret_cast<const ocl::gpu_usm&>(memory);- auto* ptr = new_buf.get_buffer().get();- auto sub_buffer = cl::UsmMemory(get_usm_helper(), ptr, byte_offset);+ auto sub_buffer = cl::UsmMemory(new_buf.get_buffer(), byte_offset);return std::make_shared<ocl::gpu_usm>(this, new_layout, sub_buffer, memory.get_allocation_type(), memory.get_mem_tracker());} else {diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hppindex ac25b3dbd06..7d57201a8b5 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp@@ -955,7 +955,11 @@ private:*/class UsmHolder {public:- UsmHolder(const cl::UsmHelper& usmHelper, void* ptr, bool shared_memory = false) : _usmHelper(usmHelper), _ptr(ptr), _shared_memory(shared_memory) {}+ UsmHolder(const cl::UsmHelper& usmHelper, void* ptr, bool shared_memory = false, std::shared_ptr<UsmHolder> parent = nullptr)+ : _usmHelper(usmHelper),+ _ptr(ptr),+ _shared_memory(shared_memory),+ _parent(std::move(parent)) {}void* ptr() {return _ptr;@@ -978,6 +982,8 @@ private:const cl::UsmHelper& _usmHelper;void* _ptr;bool _shared_memory = false;+ // Keeps the parent allocation alive while this holder points into it (sub-buffer case)+ std::shared_ptr<UsmHolder> _parent;};/*USM base class. Different usm types should derive from this class.@@ -993,6 +999,14 @@ public:}}+ // Creates a view into the parent allocation that shares its ownership+ UsmMemory(const UsmMemory& parent, size_t offset) : _usmHelper(parent._usmHelper) {+ if (!parent._usm_pointer || !parent.get()) {+ throw std::runtime_error("[GPU] Can't create sub-buffer from empty usm memory");+ }+ _usm_pointer = std::make_shared<UsmHolder>(_usmHelper, static_cast<uint8_t*>(parent.get()) + offset, true, parent._usm_pointer);+ }+// Get methods returns original pointer allocated by openCL.void* get() const {return _usm_pointer->ptr();diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/usm_memory_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/usm_memory_test.cppindex 190fc38fad3..ff4053d3ebe 100644--- a/src/plugins/intel_gpu/tests/unit/module_tests/usm_memory_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/module_tests/usm_memory_test.cpp@@ -482,6 +482,61 @@ INSTANTIATE_TEST_SUITE_P(mem_test,mem_test_params{100, 79, 381}),::testing::Values(false, true)));+class subbuffer_lifetime : public BaseUSMTest {};+TEST_P(subbuffer_lifetime, keeps_parent_allocation_alive_after_parent_release) {+ auto p = GetParam();+ if (!_engine->supports_allocation(p.type)) {+ GTEST_SKIP() << "Allocation type is not supported";+ }++ constexpr size_t parent_size = 512;+ constexpr size_t sub_offset = 96;+ constexpr size_t sub_size = 256;+ const layout parent_layout = {{static_cast<int64_t>(parent_size)}, data_types::u8, format::bfyx};+ const layout sub_layout = {{static_cast<int64_t>(sub_size)}, data_types::u8, format::bfyx};++ auto stream = _engine->create_stream({});+ const auto& usm_helper = _engine->get_usm_helper();++ std::vector<uint8_t> src(parent_size);+ for (size_t i = 0; i < parent_size; i++) {+ src[i] = static_cast<uint8_t>((i * 13 + 7) % 251);+ }++ auto parent_mem = _engine->allocate_memory(parent_layout, p.type);+ ASSERT_NE(parent_mem, nullptr);+ OV_ASSERT_NO_THROW(parent_mem->copy_from(*stream, src.data(), true));++ auto sub_mem = _engine->create_subbuffer(*parent_mem, sub_layout, sub_offset);+ ASSERT_NE(sub_mem, nullptr);++ auto* sub_ptr = downcast<ocl::gpu_usm>(sub_mem.get())->get_buffer().get();+ ASSERT_NE(usm_helper.get_usm_allocation_type(sub_ptr), CL_MEM_TYPE_UNKNOWN_INTEL);++ parent_mem.reset();++ // Once the parent is released, the driver must still recognize sub-buffer's pointer as a live USM allocation.+ ASSERT_NE(usm_helper.get_usm_allocation_type(sub_ptr), CL_MEM_TYPE_UNKNOWN_INTEL)+ << "Parent USM allocation was freed while sub-buffer is still alive";++ // Stress allocator reuse paths to make use-after-free visible in data as well.+ for (size_t i = 0; i < 4; i++) {+ auto tmp = _engine->allocate_memory(parent_layout, p.type);+ OV_ASSERT_NO_THROW(tmp->fill(*stream, static_cast<unsigned char>(i + 1), {}, true));+ }++ std::vector<uint8_t> actual(sub_size, 0);+ OV_ASSERT_NO_THROW(sub_mem->copy_to(*stream, actual.data(), true));+ for (size_t i = 0; i < sub_size; i++) {+ ASSERT_EQ(src[sub_offset + i], actual[i]) << "at index " << i;+ }+}++INSTANTIATE_TEST_SUITE_P(cldnn_usm, subbuffer_lifetime, ::testing::ValuesIn(std::vector<usm_test_params>{+ usm_test_params{ allocation_type::usm_host},+ usm_test_params{ allocation_type::usm_device},+}));+TEST(mem_test, copy_small_buf_to_large_with_out_of_bound_access) {auto& ocl_engine = dynamic_cast<ocl::ocl_engine&>(get_test_engine());auto& stream = get_test_stream();