[Bf-blender-cvs] [a1e40087c5c] master: Cycles: Fix undefined behavior which can causes crashes with a misaligned address error

Patrick Mours noreply at git.blender.org
Wed Sep 25 14:25:12 CEST 2019


Commit: a1e40087c5ce327fecf469386a89daaad3880993
Author: Patrick Mours
Date:   Tue Sep 24 17:54:08 2019 +0200
Branches: master
https://developer.blender.org/rBa1e40087c5ce327fecf469386a89daaad3880993

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

===================================================================

M	intern/cycles/kernel/kernel_types.h

===================================================================

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 */



More information about the Bf-blender-cvs mailing list