Skip to content

Commit 3499aca

Browse files
committed
a
1 parent 7228b45 commit 3499aca

File tree

14 files changed

+163
-90
lines changed

14 files changed

+163
-90
lines changed

benchmark/ubench.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,9 @@ static void do_umf_mem_props_benchmark(ze_context_handle_t context,
454454
assert(res == UMF_RESULT_SUCCESS);
455455

456456
umf_usm_memory_type_t type = UMF_MEMORY_TYPE_UNKNOWN;
457-
res = umfGetMemoryProperty(
458-
props_handle, UMF_MEMORY_PROPERTY_POINTER_TYPE, &type);
457+
res = umfGetMemoryProperty(props_handle,
458+
UMF_MEMORY_PROPERTY_POINTER_TYPE,
459+
sizeof(type), &type);
459460
assert(res == UMF_RESULT_SUCCESS);
460461
if (type != UMF_MEMORY_TYPE_DEVICE) {
461462
fprintf(stderr,

include/umf/memory_props.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ umfGetMemoryPropertiesHandle(const void *ptr,
2727
/// @brief Get a specific memory property from the properties handle
2828
/// @param props_handle handle to the memory properties
2929
/// @param memory_property_id ID of the memory property to get
30-
/// @param value [out] pointer to the value of the memory property which will
31-
/// be filled. NOTE: the type and size of the value depends on the
32-
/// memory property ID and should be checked in the documentation
30+
/// @param max_property_size size of the property value buffer
31+
/// @param property_value [out] pointer to the value of the memory property
32+
/// which will be filled
33+
// TODO check return type
3334
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
3435
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
3536
umf_memory_property_id_t memory_property_id,
36-
void *value);
37+
size_t max_property_size,
38+
void *property_value);
3739

3840
#ifdef __cplusplus
3941
}

include/umf/memory_provider.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,18 @@ umfMemoryProviderAllocationMerge(umf_memory_provider_handle_t hProvider,
266266
/// @param hProvider pointer to the memory provider
267267
/// @param ptr pointer to the allocated memory
268268
/// @param propertyId identifier of the memory property to retrieve
269-
/// @param value [out] pointer to the preallocated variable where the value will be stored
269+
/// @param max_property_size size of the property value buffer
270+
/// @param property_value [out] pointer to the value of the memory property
271+
/// which will be filled
272+
// TODO check
270273
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure,
271274
/// UMF_RESULT_ERROR_INVALID_ARGUMENT if propertyId is invalid or value is NULL,
272275
/// UMF_RESULT_ERROR_NOT_SUPPORTED if the property is not supported by this provider.
273276
///
274277
umf_result_t umfMemoryProviderGetAllocationProperties(
275278
umf_memory_provider_handle_t hProvider, const void *ptr,
276-
umf_memory_property_id_t propertyId, void *value);
279+
umf_memory_property_id_t propertyId, size_t max_property_size,
280+
void *property_value);
277281

278282
#ifdef __cplusplus
279283
}

include/umf/memory_provider_ops.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,18 @@ typedef struct umf_memory_provider_ops_t {
274274
/// @param provider pointer to the memory provider
275275
/// @param ptr pointer to the allocated memory
276276
/// @param memory_property_id identifier of the memory property to retrieve
277-
/// @param value [out] pointer to the preallocated variable where the value will be stored
277+
/// @param max_property_size size of the property value buffer
278+
/// @param property_value [out] pointer to the value of the memory property
279+
/// which will be filled
280+
// TODO check
278281
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure,
279282
/// UMF_RESULT_ERROR_INVALID_ARGUMENT if memory_property_id is invalid or value is NULL,
280283
/// UMF_RESULT_ERROR_NOT_SUPPORTED if the property is not supported by this provider.
281284
///
282285
umf_result_t (*ext_get_allocation_properties)(
283286
void *provider, const void *ptr,
284-
umf_memory_property_id_t memory_property_id, void *value);
287+
umf_memory_property_id_t memory_property_id, size_t max_property_size,
288+
void *value);
285289

286290
} umf_memory_provider_ops_t;
287291

src/memory_props.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
*/
99

10+
#include <inttypes.h>
11+
1012
#include <umf/memory_props.h>
1113
#include <umf/providers/provider_cuda.h>
1214
#include <umf/providers/provider_level_zero.h>
@@ -19,6 +21,9 @@ umf_result_t
1921
umfGetMemoryPropertiesHandle(const void *ptr,
2022
umf_memory_properties_handle_t *props_handle) {
2123

24+
LOG_DEBUG("umfGetMemoryPropertiesHandle: ptr=%p, props_handle=%p", ptr,
25+
props_handle);
26+
2227
if (props_handle == NULL) {
2328
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
2429
}
@@ -31,39 +36,67 @@ umfGetMemoryPropertiesHandle(const void *ptr,
3136

3237
*props_handle = &info->props;
3338

39+
LOG_DEBUG("umfGetMemoryPropertiesHandle: props_handle=%p, id=%" PRIu64,
40+
*props_handle, (*props_handle)->id);
41+
3442
return UMF_RESULT_SUCCESS;
3543
}
3644

3745
umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
3846
umf_memory_property_id_t memory_property_id,
39-
void *value) {
40-
if ((value == NULL) || (props_handle == NULL)) {
47+
size_t max_property_size, void *value) {
48+
49+
LOG_DEBUG("umfGetMemoryProperty: props_handle=%p, memory_property_id=%d, "
50+
"max_property_size=%zu, value=%p",
51+
props_handle, memory_property_id, max_property_size, value);
52+
53+
if ((value == NULL) || (props_handle == NULL) || (max_property_size == 0)) {
4154
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
4255
}
4356

4457
umf_memory_provider_t *provider = props_handle->provider;
4558

59+
LOG_DEBUG("umfGetMemoryProperty: provider=%p", provider);
60+
LOG_DEBUG("dereferencing value...");
61+
62+
LOG_DEBUG("value: %zu", *(size_t *)value);
63+
4664
switch (memory_property_id) {
4765
case UMF_MEMORY_PROPERTY_INVALID:
4866
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
4967

5068
case UMF_MEMORY_PROPERTY_POOL_HANDLE:
69+
if (max_property_size < sizeof(umf_memory_pool_handle_t)) {
70+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
71+
}
5172
*(umf_memory_pool_handle_t *)value = props_handle->pool;
5273
return UMF_RESULT_SUCCESS;
5374

5475
case UMF_MEMORY_PROPERTY_PROVIDER_HANDLE:
76+
if (max_property_size < sizeof(umf_memory_provider_handle_t)) {
77+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
78+
}
5579
*(umf_memory_provider_handle_t *)value = provider;
5680
return UMF_RESULT_SUCCESS;
5781

5882
case UMF_MEMORY_PROPERTY_BUFFER_ID:
83+
if (max_property_size < sizeof(uint64_t)) {
84+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
85+
}
5986
*(uint64_t *)value = props_handle->id;
6087
return UMF_RESULT_SUCCESS;
6188

6289
case UMF_MEMORY_PROPERTY_BASE_ADDRESS:
90+
if (max_property_size < sizeof(uintptr_t)) {
91+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
92+
}
6393
*(uintptr_t *)value = (uintptr_t)props_handle->base;
6494
return UMF_RESULT_SUCCESS;
6595

6696
case UMF_MEMORY_PROPERTY_BASE_SIZE:
97+
if (max_property_size < sizeof(size_t)) {
98+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
99+
}
67100
*(size_t *)value = props_handle->base_size;
68101
return UMF_RESULT_SUCCESS;
69102

@@ -73,7 +106,7 @@ umf_result_t umfGetMemoryProperty(umf_memory_properties_handle_t props_handle,
73106
case UMF_MEMORY_PROPERTY_DEVICE:
74107
return provider->ops.ext_get_allocation_properties(
75108
provider->provider_priv, props_handle->ptr, memory_property_id,
76-
value);
109+
max_property_size, value);
77110

78111
default:
79112
break;

src/memory_provider.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,11 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
424424

425425
umf_result_t umfMemoryProviderGetAllocationProperties(
426426
umf_memory_provider_handle_t hProvider, const void *ptr,
427-
umf_memory_property_id_t propertyId, void *value) {
427+
umf_memory_property_id_t propertyId, size_t max_property_size,
428+
void *property_value) {
428429
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
429-
UMF_CHECK((value != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
430+
UMF_CHECK((property_value != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
430431
return hProvider->ops.ext_get_allocation_properties(
431-
hProvider->provider_priv, ptr, propertyId, value);
432+
hProvider->provider_priv, ptr, propertyId, max_property_size,
433+
property_value);
432434
}

src/provider/provider_cuda.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ cu_memory_provider_close_ipc_handle(void *provider, void *ptr, size_t size) {
683683

684684
static umf_result_t cu_memory_provider_get_allocation_properties(
685685
void *provider, const void *ptr,
686-
umf_memory_property_id_t memory_property_id, void *value) {
686+
umf_memory_property_id_t memory_property_id, size_t max_property_size,
687+
void *value) {
687688

688689
// unused
689690
(void)ptr;
@@ -692,14 +693,23 @@ static umf_result_t cu_memory_provider_get_allocation_properties(
692693

693694
switch (memory_property_id) {
694695
case UMF_MEMORY_PROPERTY_POINTER_TYPE:
696+
if (max_property_size < sizeof(umf_usm_memory_type_t)) {
697+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
698+
}
695699
*(umf_usm_memory_type_t *)value = cuda_provider->memory_type;
696700
return UMF_RESULT_SUCCESS;
697701

698702
case UMF_MEMORY_PROPERTY_CONTEXT:
703+
if (max_property_size < sizeof(CUcontext)) {
704+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
705+
}
699706
*(CUcontext *)value = cuda_provider->context;
700707
return UMF_RESULT_SUCCESS;
701708

702709
case UMF_MEMORY_PROPERTY_DEVICE:
710+
if (max_property_size < sizeof(CUdevice)) {
711+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
712+
}
703713
*(CUdevice *)value = cuda_provider->device;
704714
return UMF_RESULT_SUCCESS;
705715

src/provider/provider_level_zero.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,23 +793,33 @@ ze_memory_provider_close_ipc_handle(void *provider, void *ptr, size_t size) {
793793

794794
static umf_result_t ze_memory_provider_get_allocation_properties(
795795
void *provider, const void *ptr,
796-
umf_memory_property_id_t memory_property_id, void *value) {
796+
umf_memory_property_id_t memory_property_id, size_t max_property_size,
797+
void *value) {
797798
(void)ptr;
798799

799800
struct ze_memory_provider_t *ze_provider =
800801
(struct ze_memory_provider_t *)provider;
801802

802803
switch (memory_property_id) {
803804
case UMF_MEMORY_PROPERTY_POINTER_TYPE:
805+
if (max_property_size < sizeof(umf_usm_memory_type_t)) {
806+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
807+
}
804808
*(umf_usm_memory_type_t *)value =
805809
ze2umf_memory_type(ze_provider->memory_type);
806810
return UMF_RESULT_SUCCESS;
807811

808812
case UMF_MEMORY_PROPERTY_CONTEXT:
813+
if (max_property_size < sizeof(ze_context_handle_t)) {
814+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
815+
}
809816
*(ze_context_handle_t *)value = ze_provider->context;
810817
return UMF_RESULT_SUCCESS;
811818

812819
case UMF_MEMORY_PROPERTY_DEVICE:
820+
if (max_property_size < sizeof(ze_device_handle_t)) {
821+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
822+
}
813823
*(ze_device_handle_t *)value = ze_provider->device;
814824
return UMF_RESULT_SUCCESS;
815825

src/provider/provider_tracking.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,11 +1291,11 @@ static umf_result_t trackingCloseIpcHandle(void *provider, void *ptr,
12911291
static umf_result_t
12921292
trackingGetAllocationProperties(void *provider, const void *ptr,
12931293
umf_memory_property_id_t memory_property_id,
1294-
void *value) {
1294+
size_t max_property_size, void *value) {
12951295
umf_tracking_memory_provider_t *p =
12961296
(umf_tracking_memory_provider_t *)provider;
1297-
return umfMemoryProviderGetAllocationProperties(p->hUpstream, ptr,
1298-
memory_property_id, value);
1297+
return umfMemoryProviderGetAllocationProperties(
1298+
p->hUpstream, ptr, memory_property_id, max_property_size, value);
12991299
}
13001300

13011301
umf_memory_provider_ops_t UMF_TRACKING_MEMORY_PROVIDER_OPS = {

test/common/provider_null.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ static umf_result_t nullCloseIpcHandle(void *provider, void *ptr, size_t size) {
135135

136136
static umf_result_t
137137
nullGetAllocationProperties(void *provider, const void *ptr,
138-
umf_memory_property_id_t propertyId, void *value) {
138+
umf_memory_property_id_t propertyId,
139+
size_t max_property_size, void *value) {
139140
(void)provider;
140141
(void)ptr;
141142
(void)propertyId;
143+
(void)max_property_size;
142144
(void)value;
143145
return UMF_RESULT_SUCCESS;
144146
}

0 commit comments

Comments
 (0)