adding securities

This commit is contained in:
2024-12-09 16:30:55 +01:00
parent 288015d355
commit e23ddd6cd1
9 changed files with 103 additions and 4 deletions

View File

@@ -164,6 +164,7 @@ PULSE_API const char* PulseVerbaliseErrorType(PulseErrorType error)
case PULSE_ERROR_DEVICE_LOST: return "device has been lost";
case PULSE_ERROR_INVALID_INTERNAL_POINTER: return "invalid internal pointer";
case PULSE_ERROR_MAP_FAILED: return "memory mapping failed";
case PULSE_ERROR_INVALID_DEVICE: return "device is invalid";
default: return "invalid error type";
};

View File

@@ -5,6 +5,7 @@
#include "Pulse.h"
#include "PulseDefs.h"
#include "PulseInternal.h"
#include "PulseProfile.h"
PULSE_API PulseBuffer PulseCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos)
{
@@ -18,7 +19,13 @@ PULSE_API PulseBuffer PulseCreateBuffer(PulseDevice device, const PulseBufferCre
return PULSE_NULL_HANDLE;
}
}
return device->PFN_CreateBuffer(device, create_infos);
PulseBuffer buffer = device->PFN_CreateBuffer(device, create_infos);
if(buffer == PULSE_NULL_HANDLE)
return PULSE_NULL_HANDLE;
PULSE_EXPAND_ARRAY_IF_NEEDED(device->allocated_buffers, PulseBuffer, device->allocated_buffers_size, device->allocated_buffers_capacity, 64);
device->allocated_buffers[device->allocated_buffers_size] = buffer;
device->allocated_buffers_size++;
return buffer;
}
PULSE_API bool PulseMapBuffer(PulseBuffer buffer, void** data)
@@ -53,5 +60,20 @@ PULSE_API void PulseDestroyBuffer(PulseDevice device, PulseBuffer buffer)
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
PulseLogWarning(device->backend, "buffer is still mapped, consider unmapping it before destroy");
}
return device->PFN_DestroyBuffer(device, buffer);
if(buffer->device != device)
{
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
PulseLogErrorFmt(device->backend, "cannot destroy buffer [%p] that have been allocated with device [%p] using device [%p]", buffer, buffer->device, device);
PulseSetInternalError(PULSE_ERROR_INVALID_DEVICE);
return;
}
for(uint32_t i = 0; i < device->allocated_buffers_size; i++)
{
if(device->allocated_buffers[i] == buffer)
{
PULSE_DEFRAG_ARRAY(device->allocated_buffers, device->allocated_buffers_size, i);
break;
}
}
device->PFN_DestroyBuffer(device, buffer);
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "PulseDefs.h"
#include "PulseInternal.h"
PULSE_API PulseDevice PulseCreateDevice(PulseBackend backend, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
@@ -14,6 +15,15 @@ PULSE_API PulseDevice PulseCreateDevice(PulseBackend backend, PulseDevice* forbi
PULSE_API void PulseDestroyDevice(PulseDevice device)
{
PULSE_CHECK_HANDLE(device);
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
{
if(device->allocated_buffers_size != 0)
PulseLogErrorFmt(device->backend, "some buffers allocated using device [%p] were not freed (%d active allocations)", device, device->allocated_buffers_size);
if(device->allocated_images_size != 0)
PulseLogErrorFmt(device->backend, "some images allocated using device [%p] were not freed (%d active allocations)", device, device->allocated_images_size);
}
free(device->allocated_buffers);
free(device->allocated_images);
device->PFN_DestroyDevice(device);
device->driver_data = PULSE_NULLPTR;
}

View File

@@ -99,7 +99,13 @@ PULSE_API PulseImage PulseCreateImage(PulseDevice device, const PulseImageCreate
if(failed)
return PULSE_NULL_HANDLE;
}
return device->PFN_CreateImage(device, create_infos);
PulseImage image = device->PFN_CreateImage(device, create_infos);
if(image == PULSE_NULL_HANDLE)
return PULSE_NULL_HANDLE;
PULSE_EXPAND_ARRAY_IF_NEEDED(device->allocated_images, PulseImage, device->allocated_images_size, device->allocated_images_capacity, 64);
device->allocated_images[device->allocated_images_size] = image;
device->allocated_images_size++;
return image;
}
PULSE_API bool PulseIsImageFormatValid(PulseDevice device, PulseImageFormat format, PulseImageType type, PulseImageUsageFlags usage)
@@ -118,5 +124,20 @@ PULSE_API void PulseDestroyImage(PulseDevice device, PulseImage image)
PulseLogWarning(device->backend, "image is NULL, this may be a bug in your application");
return;
}
return device->PFN_DestroyImage(device, image);
if(image->device != device)
{
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
PulseLogErrorFmt(device->backend, "cannot destroy image [%p] that have been allocated with device [%p] using device [%p]", image, image->device, device);
PulseSetInternalError(PULSE_ERROR_INVALID_DEVICE);
return;
}
for(uint32_t i = 0; i < device->allocated_images_size; i++)
{
if(device->allocated_images[i] == image)
{
PULSE_DEFRAG_ARRAY(device->allocated_images, device->allocated_images_size, i);
break;
}
}
device->PFN_DestroyImage(device, image);
}

View File

@@ -81,6 +81,14 @@ typedef struct PulseDeviceHandler
// Attributes
void* driver_data;
PulseBackend backend;
PulseBuffer* allocated_buffers;
uint32_t allocated_buffers_size;
uint32_t allocated_buffers_capacity;
PulseImage* allocated_images;
uint32_t allocated_images_size;
uint32_t allocated_images_capacity;
} PulseDeviceHandler;
typedef struct PulseFenceHandler