Skip to content

Commit 680fa34

Browse files
stefanatworkCopilot
andcommitted
Validate buffer type and slot in rtcInterpolate/rtcInterpolateN
The bufferType and bufferSlot members of RTCInterpolateArguments and RTCInterpolateNArguments are passed from the public API straight into the per geometry interpolate_impl() routines, where the slot is used to index the fixed size vertices, tangents and vertexAttribs arrays of buffer descriptors. The only guard was an assert that is removed in release builds, and that was additionally off by one for vertex attributes as it used bufferSlot <= vertexAttribs.size() instead of <. An out of range slot therefore read a buffer descriptor past the end of the descriptor array. The resulting pointer and stride were then used to load vertex data, and the loaded bytes were written into the caller visible interpolation output arrays. This affects all interpolatable geometry types, both the vertex and the vertex attribute buffer branch, and both rtcInterpolate and rtcInterpolateN. A buffer type other than RTC_BUFFER_TYPE_VERTEX or RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE was silently treated as a vertex buffer instead of being rejected. Add Geometry::checkInterpolateBuffer(), which rejects unsupported buffer types, vertex slots that are not smaller than numTimeSteps, and vertex attribute slots that are not smaller than the vertex attribute count. These bounds also cover the vertex_buffer_tags and vertex_attrib_buffer_tags arrays of subdivision meshes, which are sized by the same two counts. Invalid requests now raise RTC_ERROR_INVALID_ARGUMENT and interpolate no data. Use the helper at all interpolation sites, replacing the asserts, and document the requirement. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3d9cb89 commit 680fa34

9 files changed

Lines changed: 42 additions & 20 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7188,6 +7188,14 @@ interpolation buffer, one can specify vertex buffers
71887188
(`RTC_BUFFER_TYPE_VERTEX`) and vertex attribute buffers
71897189
(`RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE`) as well.
71907190

7191+
The `bufferType` has to be either `RTC_BUFFER_TYPE_VERTEX` or
7192+
`RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE`, and the `bufferSlot` has to be a
7193+
valid slot of the specified buffer type, thus smaller than the number of
7194+
time steps for vertex buffers and smaller than the vertex attribute
7195+
count set using `rtcSetGeometryVertexAttributeCount` for vertex
7196+
attribute buffers. Otherwise an `RTC_ERROR_INVALID_ARGUMENT` error is
7197+
raised and no data is interpolated.
7198+
71917199
The `rtcInterpolate` call stores `valueCount` number of interpolated
71927200
floating point values to the memory location pointed to by `P`. One can
71937201
avoid storing the interpolated value by setting `P` to `NULL`.

‎doc/src/api/rtcInterpolate.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ interpolation buffer, one can specify vertex buffers
5151
(`RTC_BUFFER_TYPE_VERTEX`) and vertex attribute buffers
5252
(`RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE`) as well.
5353

54+
The `bufferType` has to be either `RTC_BUFFER_TYPE_VERTEX` or
55+
`RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE`, and the `bufferSlot` has to be a
56+
valid slot of the specified buffer type, thus smaller than the number
57+
of time steps for vertex buffers and smaller than the vertex attribute
58+
count set using `rtcSetGeometryVertexAttributeCount` for vertex
59+
attribute buffers. Otherwise an `RTC_ERROR_INVALID_ARGUMENT` error is
60+
raised and no data is interpolated.
61+
5462
The `rtcInterpolate` call stores `valueCount` number of interpolated
5563
floating point values to the memory location pointed to by `P`. One
5664
can avoid storing the interpolated value by setting `P` to `NULL`.

‎kernels/common/geometry.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,23 @@ namespace embree
401401
throw_RTCError(RTC_ERROR_INVALID_OPERATION,"operation not supported for this geometry");
402402
}
403403

404+
/*! validates the buffer type and slot of an interpolation request. The buffer slot
405+
* originates from the public rtcInterpolate/rtcInterpolateN API and is used to index
406+
* fixed size arrays of buffer descriptors, thus it has to be range checked at runtime. */
407+
__forceinline void checkInterpolateBuffer(RTCBufferType bufferType, unsigned int bufferSlot, size_t numVertexAttributes) const
408+
{
409+
if (bufferType == RTC_BUFFER_TYPE_VERTEX) {
410+
if (unlikely(bufferSlot >= numTimeSteps))
411+
throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid vertex buffer slot specified for interpolation");
412+
}
413+
else if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
414+
if (unlikely(bufferSlot >= numVertexAttributes))
415+
throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid vertex attribute buffer slot specified for interpolation");
416+
}
417+
else
418+
throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid buffer type specified for interpolation");
419+
}
420+
404421
/*! interpolates user data to the specified u/v locations */
405422
virtual void interpolateN(const RTCInterpolateNArguments* const args);
406423

‎kernels/common/scene_curves.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,7 @@ namespace embree
545545
unsigned int valueCount = args->valueCount;
546546

547547
/* calculate base pointer and stride */
548-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
549-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
548+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
550549
const char* src = nullptr;
551550
size_t stride = 0;
552551
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
@@ -708,10 +707,11 @@ namespace embree
708707
float* ddPdudu = args->ddPdudu;
709708
unsigned int valueCount = args->valueCount;
710709

710+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
711+
711712
/* we interpolate vertex attributes linearly for hermite basis */
712713
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE)
713714
{
714-
assert(bufferSlot <= vertexAttribs.size());
715715
const char* vsrc = vertexAttribs[bufferSlot].getPtr();
716716
const size_t vstride = vertexAttribs[bufferSlot].getStride();
717717

@@ -732,7 +732,6 @@ namespace embree
732732
/* interpolation for vertex buffers */
733733
else
734734
{
735-
assert(bufferSlot < numTimeSteps);
736735
const char* vsrc = vertices[bufferSlot].getPtr();
737736
const char* tsrc = tangents[bufferSlot].getPtr();
738737
const size_t vstride = vertices[bufferSlot].getStride();

‎kernels/common/scene_grid_mesh.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ namespace embree
8686
unsigned int valueCount = args->valueCount;
8787

8888
/* calculate base pointer and stride */
89-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
90-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
89+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
9190
const char* src = nullptr;
9291
size_t stride = 0;
9392
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {

‎kernels/common/scene_line_segments.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ namespace embree
4949
unsigned int valueCount = args->valueCount;
5050

5151
/* calculate base pointer and stride */
52-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
53-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
52+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
5453
const char* src = nullptr;
5554
size_t stride = 0;
5655
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {

‎kernels/common/scene_quad_mesh.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ namespace embree
6868
unsigned int valueCount = args->valueCount;
6969

7070
/* calculate base pointer and stride */
71-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
72-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
71+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
7372
const char* src = nullptr;
7473
size_t stride = 0;
7574
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {

‎kernels/common/scene_subdiv_mesh.cpp‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -854,21 +854,18 @@ namespace embree
854854
unsigned int valueCount = args->valueCount;
855855

856856
/* calculate base pointer and stride */
857-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < RTC_MAX_TIME_STEP_COUNT) ||
858-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot < RTC_MAX_USER_VERTEX_BUFFERS));
857+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
859858
const char* src = nullptr;
860859
size_t stride = 0;
861860
std::vector<SharedLazyTessellationCache::CacheEntry>* baseEntry = nullptr;
862861
Topology* topo = nullptr;
863862
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
864-
assert(bufferSlot < vertexAttribs.size());
865863
src = vertexAttribs[bufferSlot].getPtr();
866864
stride = vertexAttribs[bufferSlot].getStride();
867865
baseEntry = &vertex_attrib_buffer_tags[bufferSlot];
868866
int topologyID = vertexAttribs[bufferSlot].userData;
869867
topo = &topology[topologyID];
870868
} else {
871-
assert(bufferSlot < numTimeSteps);
872869
src = vertices[bufferSlot].getPtr();
873870
stride = vertices[bufferSlot].getStride();
874871
baseEntry = &vertex_buffer_tags[bufferSlot];
@@ -931,21 +928,18 @@ namespace embree
931928
unsigned int valueCount = args->valueCount;
932929

933930
/* calculate base pointer and stride */
934-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < RTC_MAX_TIME_STEP_COUNT) ||
935-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot < RTC_MAX_USER_VERTEX_BUFFERS));
931+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
936932
const char* src = nullptr;
937933
size_t stride = 0;
938934
std::vector<SharedLazyTessellationCache::CacheEntry>* baseEntry = nullptr;
939935
Topology* topo = nullptr;
940936
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {
941-
assert(bufferSlot < vertexAttribs.size());
942937
src = vertexAttribs[bufferSlot].getPtr();
943938
stride = vertexAttribs[bufferSlot].getStride();
944939
baseEntry = &vertex_attrib_buffer_tags[bufferSlot];
945940
int topologyID = vertexAttribs[bufferSlot].userData;
946941
topo = &topology[topologyID];
947942
} else {
948-
assert(bufferSlot < numTimeSteps);
949943
src = vertices[bufferSlot].getPtr();
950944
stride = vertices[bufferSlot].getStride();
951945
baseEntry = &vertex_buffer_tags[bufferSlot];

‎kernels/common/scene_triangle_mesh.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ namespace embree
6262
unsigned int valueCount = args->valueCount;
6363

6464
/* calculate base pointer and stride */
65-
assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) ||
66-
(bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size()));
65+
checkInterpolateBuffer(bufferType,bufferSlot,vertexAttribs.size());
6766
const char* src = nullptr;
6867
size_t stride = 0;
6968
if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) {

0 commit comments

Comments
 (0)