Cycles: Fix undefined behavior which can causes crashes with a misaligned address error

Cycles casts a pointer from ShaderDataTinyStorage to ShaderData, these structs by default had different alignments however (the former was 1-byte aligned, the latter 16-byte). This caused undefined behavior on at least the CUDA platform. Forcing both structs to use the same alignment fixes this.

CUDA toolkits newer than 10.1 run into this because of a compiler optimization.

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D5883
This commit is contained in:
Patrick Mours 2019-09-24 17:54:08 +02:00
parent 900a9a4b06
commit a1e40087c5
1 changed files with 10 additions and 4 deletions

View File

@ -925,7 +925,8 @@ enum ShaderDataObjectFlag {
SD_OBJECT_HAS_VOLUME_ATTRIBUTES)
};
typedef ccl_addr_space struct ShaderData {
typedef ccl_addr_space struct ccl_align(16) ShaderData
{
/* position */
float3 P;
/* smooth normal for shading */
@ -1010,11 +1011,16 @@ typedef ccl_addr_space struct ShaderData {
/* At the end so we can adjust size in ShaderDataTinyStorage. */
struct ShaderClosure closure[MAX_CLOSURE];
} ShaderData;
}
ShaderData;
typedef ccl_addr_space struct ShaderDataTinyStorage {
/* ShaderDataTinyStorage needs the same alignment as ShaderData, or else
* the pointer cast in AS_SHADER_DATA invokes undefined behavior. */
typedef ccl_addr_space struct ccl_align(16) ShaderDataTinyStorage
{
char pad[sizeof(ShaderData) - sizeof(ShaderClosure) * MAX_CLOSURE];
} ShaderDataTinyStorage;
}
ShaderDataTinyStorage;
#define AS_SHADER_DATA(shader_data_tiny_storage) ((ShaderData *)shader_data_tiny_storage)
/* Path State */