Skip to content

Commit

Permalink
allow invalid handles to exist and defend against using them through …
Browse files Browse the repository at this point in the history
…ANARI
  • Loading branch information
jeffamstutz committed Jan 7, 2025
1 parent 116a8d4 commit b3e6b45
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions tsd/src/tsd/core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ void Object::updateANARIParameter(anari::Device d,
const char *n,
AnariObjectCache *cache) const
{
if (!o)
return;

if (cache && !p.isEnabled()) {
anari::unsetParameter(d, o, n);
} else if (cache && p.value().holdsObject()) {
Expand Down Expand Up @@ -188,6 +191,9 @@ void Object::updateANARIParameter(anari::Device d,
void Object::updateAllANARIParameters(
anari::Device d, anari::Object o, AnariObjectCache *cache) const
{
if (!o)
return;

for (size_t i = 0; i < numParameters(); i++)
updateANARIParameter(d, o, parameterAt(i), parameterNameAt(i), cache);
}
Expand Down
8 changes: 8 additions & 0 deletions tsd/src/tsd/objects/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif

#include "tsd/objects/Array.hpp"
#include "tsd/core/Logging.hpp"
// std
#include <stdexcept>
#if TSD_USE_CUDA
Expand Down Expand Up @@ -182,6 +183,13 @@ Array::Array(anari::DataType arrayType,
else if (anari::isObject(type))
return;

if (isEmpty()) {
logWarning("%s of %s elements created with 0 size",
anari::toString(this->type()),
anari::toString(this->elementType()));
return;
}

if (kind == MemoryKind::CUDA) {
#if TSD_USE_CUDA
cudaMalloc(&m_data, size() * elementSize());
Expand Down
23 changes: 13 additions & 10 deletions tsd/src/tsd/rendering/RenderIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ void RenderIndex::populate(Context &ctx, bool setAsUpdateDelegate)
foreach_item_const(objArray, [&](auto *obj) {
if (!obj)
return;
auto o = handleArray[obj->index()];
obj->updateAllANARIParameters(d, o, &m_cache);
if (obj->type() == ANARI_SURFACE)
anari::setParameter(d, o, "id", uint32_t(obj->index()));
else if (obj->type() == ANARI_VOLUME)
anari::setParameter(d, o, "id", uint32_t(obj->index()) | 0x80000000u);
anari::commitParameters(d, o);
if (auto o = handleArray[obj->index()]; o != nullptr) {
obj->updateAllANARIParameters(d, o, &m_cache);
if (obj->type() == ANARI_SURFACE)
anari::setParameter(d, o, "id", uint32_t(obj->index()));
else if (obj->type() == ANARI_VOLUME)
anari::setParameter(d, o, "id", uint32_t(obj->index()) | 0x80000000u);
anari::commitParameters(d, o);
}
});
};

Expand Down Expand Up @@ -125,7 +126,8 @@ void RenderIndex::signalObjectAdded(const Object *obj)
switch (obj->type()) {
case ANARI_SURFACE: {
auto s = m_cache.surface.insert((anari::Surface)o);
anari::setParameter(d, o, "id", uint32_t(obj->index()));
if (o)
anari::setParameter(d, o, "id", uint32_t(obj->index()));
} break;
case ANARI_GEOMETRY:
m_cache.geometry.insert((anari::Geometry)o);
Expand Down Expand Up @@ -155,7 +157,8 @@ void RenderIndex::signalObjectAdded(const Object *obj)
break; // no-op
}

anari::commitParameters(d, o);
if (o)
anari::commitParameters(d, o);
}

void RenderIndex::signalParameterUpdated(const Object *o, const Parameter *p)
Expand Down Expand Up @@ -221,7 +224,7 @@ void RenderIndex::signalRemoveAllObjects()
void RenderIndex::updateObjectArrayData(const Array *a) const
{
auto elementType = a->elementType();
if (!a || !anari::isObject(elementType))
if (!a || !anari::isObject(elementType) || a->isEmpty())
return;

if (auto arr = (anari::Array)m_cache.getHandle(a); arr != nullptr) {
Expand Down

0 comments on commit b3e6b45

Please sign in to comment.