Skip to content

Commit

Permalink
Fix userdata binding corner case (#1673)
Browse files Browse the repository at this point in the history
This includes two fixes:

 1) When registering symbols that need user data, sort the entries in
 the set so the layer number is ignored. A needed udata iteam shouldn't
 depend on the layer and separating them makes find_userdata_index()
 sometimes find an index with different derivs status.

 2) osl_bind_interpolated_param() is memcpy'ing derivs that might not
 be there, yielding corrupted derivs and possibly a crash.

Signed-off-by: Alejandro Conty <[email protected]>
  • Loading branch information
aconty authored and lgritz committed Apr 25, 2023
1 parent 35aa264 commit b2190fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/liboslexec/oslexec_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ struct UserDataNeeded {
{
if (a.name != b.name)
return a.name < b.name;
if (a.layer_num != b.layer_num)
return a.layer_num < b.layer_num;
// Checking for layer_num means that if derivs differ find_userdata_index
// may find the wrong layer symbol with the wrong derivs setting.
//if (a.layer_num != b.layer_num)
// return a.layer_num < b.layer_num;
if (a.type.basetype != b.type.basetype)
return a.type.basetype < b.type.basetype;
if (a.type.aggregate != b.type.aggregate)
Expand Down
7 changes: 6 additions & 1 deletion src/liboslexec/shadingsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4469,8 +4469,13 @@ osl_bind_interpolated_param(void* sg_, const void* name, long long type,
sg->context->incr_get_userdata_calls();
}
if (status == 2) {
int udata_size = (userdata_has_derivs ? 3 : 1) * TYPEDESC(type).size();
// If userdata was present, copy it to the shader variable
memcpy(symbol_data, userdata_data, symbol_data_size);
memcpy(symbol_data, userdata_data,
std::min(symbol_data_size, udata_size));
if (symbol_data_size > udata_size)
memset((char*)symbol_data + udata_size, 0,
symbol_data_size - udata_size);
return 1;
}
return 0; // no such user data
Expand Down

0 comments on commit b2190fa

Please sign in to comment.