diff options
author | Patrick Mours <pmours@nvidia.com> | 2019-09-24 18:54:08 +0300 |
---|---|---|
committer | Patrick Mours <pmours@nvidia.com> | 2019-09-25 15:22:21 +0300 |
commit | a1e40087c5ce327fecf469386a89daaad3880993 (patch) | |
tree | 7c762687a10060e09f0fa3b5e79462bd7754e864 /intern/cycles/kernel/kernel_types.h | |
parent | 900a9a4b06a6d8deb2465c2b4b9f3fb9e7fc1bdd (diff) |
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
Diffstat (limited to 'intern/cycles/kernel/kernel_types.h')
-rw-r--r-- | intern/cycles/kernel/kernel_types.h | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/intern/cycles/kernel/kernel_types.h b/intern/cycles/kernel/kernel_types.h index 7aef34b00a2..1e5534b0c17 100644 --- a/intern/cycles/kernel/kernel_types.h +++ b/intern/cycles/kernel/kernel_types.h @@ -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 */ |