Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/sgl/device/cursor_access_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ class SGL_API CursorWriteWrappers {

slang::TypeLayoutReflection* _get_slang_type_layout() const
{
return static_cast<const BaseCursor*>(this)->slang_type_layout();
slang::TypeLayoutReflection* type_layout = static_cast<const BaseCursor*>(this)->slang_type_layout();
SGL_CHECK(
type_layout != nullptr,
"Cursor has no resolved type layout; cannot write typed data. This usually means the parameter's "
"layout could not be reflected on the current backend."
);
return type_layout;
}

DeviceType _get_device_type_internal() const { return static_cast<const BaseCursor*>(this)->_get_device_type(); }
Expand Down
2 changes: 1 addition & 1 deletion src/sgl/device/shader_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class SGL_API ShaderCursor : public CursorWriteWrappers<ShaderCursor, ShaderOffs
DeviceType _get_device_type() const;

private:
slang::TypeLayoutReflection* m_type_layout;
slang::TypeLayoutReflection* m_type_layout{nullptr};
ShaderObject* m_shader_object{nullptr};
ShaderOffset m_offset;
};
Expand Down
5 changes: 5 additions & 0 deletions src/slangpy_ext/device/cursor_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,11 @@ class WriteConverterTable {
}

slang::TypeLayoutReflection* type_layout = self.slang_type_layout();
SGL_CHECK(
type_layout != nullptr,
"Cannot marshal value: cursor has no resolved type layout (the layout could not be reflected on the "
"current backend)."
);
auto kind = (TypeReflection::Kind)type_layout->getKind();

switch (kind) {
Expand Down
16 changes: 16 additions & 0 deletions tests/sgl/device/test_cursors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ TEST_CASE("register_cursor_writer_duplicate_rejected")
CHECK_THROWS(cursor_utils::register_cursor_writer<RegistryDuplicateStruct>());
}

TEST_CASE("shader_cursor_typed_write_without_type_layout_throws")
{
// A cursor with no resolved type layout must raise a catchable error rather than dereference a
// null TypeLayoutReflection* and crash the host process. ShaderCursor::find_element assigns a
// child cursor's layout directly from getElementTypeLayout(), which can be null and bypasses the
// constructor's assert, so is_valid() (offset-only) is not enough to keep it out of typed access.
ShaderCursor cursor{};
REQUIRE_FALSE(cursor.slang_type_layout());

float scalar = 1.0f;
CHECK_THROWS(cursor.set(scalar));

float array[3] = {1.0f, 2.0f, 3.0f};
CHECK_THROWS(cursor._set_array(array, sizeof(array), TypeReflection::ScalarType::float32, 3));
}

TEST_CASE_GPU("write_to_cursor")
{

Expand Down
Loading