public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] BaseTools: Check the index of array.
@ 2018-09-04  9:26 Zhaozh1x
  2018-09-12  7:31 ` Gao, Liming
  0 siblings, 1 reply; 2+ messages in thread
From: Zhaozh1x @ 2018-09-04  9:26 UTC (permalink / raw)
  To: edk2-devel; +Cc: Zhaozh1x, Liming Gao, Yonghong Zhu, Bob Feng

For structure PCD, add the checker for the ARRAY
element assignment and Buffer FieldSize.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: ZhiqiangX Zhao <zhiqiangx.zhao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
---
 BaseTools/Source/C/Common/PcdValueCommon.h        | 7 +++++++
 BaseTools/Source/Python/Workspace/DscBuildData.py | 8 ++++++++
 2 files changed, 15 insertions(+)

diff --git a/BaseTools/Source/C/Common/PcdValueCommon.h b/BaseTools/Source/C/Common/PcdValueCommon.h
index 3922428ded..255afdfcc3 100644
--- a/BaseTools/Source/C/Common/PcdValueCommon.h
+++ b/BaseTools/Source/C/Common/PcdValueCommon.h
@@ -22,6 +22,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define __ARRAY_ELEMENT_SIZE(TYPE, Field) (sizeof((TYPE *)0)->Field[0])
 #define __OFFSET_OF(TYPE, Field) ((UINT32) &(((TYPE *)0)->Field))
 #define __FLEXIBLE_SIZE(Size, TYPE, Field, MaxIndex)   if (__FIELD_SIZE(TYPE, Field) == 0) Size = MAX((__OFFSET_OF(TYPE, Field) + __ARRAY_ELEMENT_SIZE(TYPE, Field) * (MaxIndex)), Size)
+#define __ARRAY_SIZE(Array) (sizeof(Array)/sizeof(Array[0]))
+
+#if defined(_MSC_EXTENSIONS)
+#define __STATIC_ASSERT static_assert
+#else
+#define __STATIC_ASSERT _Static_assert
+#endif
 
 VOID
 PcdEntryPoint (
diff --git a/BaseTools/Source/Python/Workspace/DscBuildData.py b/BaseTools/Source/Python/Workspace/DscBuildData.py
index 748452623f..98055bcb50 100644
--- a/BaseTools/Source/Python/Workspace/DscBuildData.py
+++ b/BaseTools/Source/Python/Workspace/DscBuildData.py
@@ -1766,8 +1766,12 @@ class DscBuildData(PlatformBuildClassObject):
                     #
                     CApp = CApp + '  FieldSize = __FIELD_SIZE(%s, %s);\n' % (Pcd.DatumType, FieldName)
                     CApp = CApp + '  Value     = %s; // From %s Line %d Value %s\n' % (DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])
+                    CApp = CApp + '  __STATIC_ASSERT(__FIELD_SIZE(%s, %s) >= %d, "Input buffer exceeds the buffer array");\n' % (Pcd.DatumType, FieldName, ValueSize)
                     CApp = CApp + '  memcpy (&Pcd->%s, Value, (FieldSize > 0 && FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize)
                 else:
+                    if '[' in FieldName and ']' in FieldName:
+                        Index = int(FieldName.split('[')[1].split(']')[0])
+                        CApp = CApp + '  __STATIC_ASSERT((%d < __ARRAY_SIZE(Pcd->%s)) || (__ARRAY_SIZE(Pcd->%s) == 0), "array checker error");\n' % (Index, FieldName.split('[')[0], FieldName.split('[')[0])
                     if ValueSize > 4:
                         CApp = CApp + '  Pcd->%s = %dULL; // From %s Line %d Value %s\n' % (FieldName, Value, FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])
                     else:
@@ -1848,8 +1852,12 @@ class DscBuildData(PlatformBuildClassObject):
                     #
                         CApp = CApp + '  FieldSize = __FIELD_SIZE(%s, %s);\n' % (Pcd.DatumType, FieldName)
                         CApp = CApp + '  Value     = %s; // From %s Line %d Value %s\n' % (DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])
+                        CApp = CApp + '  __STATIC_ASSERT(__FIELD_SIZE(%s, %s) >= %d, "Input buffer exceeds the buffer array");\n' % (Pcd.DatumType, FieldName, ValueSize)
                         CApp = CApp + '  memcpy (&Pcd->%s, Value, (FieldSize > 0 && FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize)
                     else:
+                        if '[' in FieldName and ']' in FieldName:
+                            Index = int(FieldName.split('[')[1].split(']')[0])
+                            CApp = CApp + '  __STATIC_ASSERT((%d < __ARRAY_SIZE(Pcd->%s)) || (__ARRAY_SIZE(Pcd->%s) == 0), "array checker error");\n' % (Index, FieldName.split('[')[0], FieldName.split('[')[0])
                         if ValueSize > 4:
                             CApp = CApp + '  Pcd->%s = %dULL; // From %s Line %d Value %s\n' % (FieldName, Value, FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])
                         else:
-- 
2.14.1.windows.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] BaseTools: Check the index of array.
  2018-09-04  9:26 [PATCH] BaseTools: Check the index of array Zhaozh1x
@ 2018-09-12  7:31 ` Gao, Liming
  0 siblings, 0 replies; 2+ messages in thread
From: Gao, Liming @ 2018-09-12  7:31 UTC (permalink / raw)
  To: Zhao, ZhiqiangX, edk2-devel@lists.01.org

Zhiqiang:
  I have two comments. 
1. Please update commit title with the detail message. 
2. Please update the patch to skip the case of file size as zero, and also add comments for each ASSERT. 

Thanks
Liming
>-----Original Message-----
>From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
>Zhaozh1x
>Sent: Tuesday, September 04, 2018 5:26 PM
>To: edk2-devel@lists.01.org
>Cc: Gao, Liming <liming.gao@intel.com>
>Subject: [edk2] [PATCH] BaseTools: Check the index of array.
>
>For structure PCD, add the checker for the ARRAY
>element assignment and Buffer FieldSize.
>
>Contributed-under: TianoCore Contribution Agreement 1.1
>Signed-off-by: ZhiqiangX Zhao <zhiqiangx.zhao@intel.com>
>Cc: Liming Gao <liming.gao@intel.com>
>Cc: Yonghong Zhu <yonghong.zhu@intel.com>
>Cc: Bob Feng <bob.c.feng@intel.com>
>---
> BaseTools/Source/C/Common/PcdValueCommon.h        | 7 +++++++
> BaseTools/Source/Python/Workspace/DscBuildData.py | 8 ++++++++
> 2 files changed, 15 insertions(+)
>
>diff --git a/BaseTools/Source/C/Common/PcdValueCommon.h
>b/BaseTools/Source/C/Common/PcdValueCommon.h
>index 3922428ded..255afdfcc3 100644
>--- a/BaseTools/Source/C/Common/PcdValueCommon.h
>+++ b/BaseTools/Source/C/Common/PcdValueCommon.h
>@@ -22,6 +22,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
>KIND, EITHER EXPRESS OR IMPLIED.
> #define __ARRAY_ELEMENT_SIZE(TYPE, Field) (sizeof((TYPE *)0)->Field[0])
> #define __OFFSET_OF(TYPE, Field) ((UINT32) &(((TYPE *)0)->Field))
> #define __FLEXIBLE_SIZE(Size, TYPE, Field, MaxIndex)   if (__FIELD_SIZE(TYPE,
>Field) == 0) Size = MAX((__OFFSET_OF(TYPE, Field) +
>__ARRAY_ELEMENT_SIZE(TYPE, Field) * (MaxIndex)), Size)
>+#define __ARRAY_SIZE(Array) (sizeof(Array)/sizeof(Array[0]))
>+
>+#if defined(_MSC_EXTENSIONS)
>+#define __STATIC_ASSERT static_assert
>+#else
>+#define __STATIC_ASSERT _Static_assert
>+#endif
>
> VOID
> PcdEntryPoint (
>diff --git a/BaseTools/Source/Python/Workspace/DscBuildData.py
>b/BaseTools/Source/Python/Workspace/DscBuildData.py
>index 748452623f..98055bcb50 100644
>--- a/BaseTools/Source/Python/Workspace/DscBuildData.py
>+++ b/BaseTools/Source/Python/Workspace/DscBuildData.py
>@@ -1766,8 +1766,12 @@ class DscBuildData(PlatformBuildClassObject):
>                     #
>                     CApp = CApp + '  FieldSize = __FIELD_SIZE(%s, %s);\n' %
>(Pcd.DatumType, FieldName)
>                     CApp = CApp + '  Value     = %s; // From %s Line %d Value %s\n' %
>(DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1],
>FieldList[FieldName][2], FieldList[FieldName][0])
>+                    CApp = CApp + '  __STATIC_ASSERT(__FIELD_SIZE(%s, %s) >= %d,
>"Input buffer exceeds the buffer array");\n' % (Pcd.DatumType, FieldName,
>ValueSize)
>                     CApp = CApp + '  memcpy (&Pcd->%s, Value, (FieldSize > 0 &&
>FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize)
>                 else:
>+                    if '[' in FieldName and ']' in FieldName:
>+                        Index = int(FieldName.split('[')[1].split(']')[0])
>+                        CApp = CApp + '  __STATIC_ASSERT((%d < __ARRAY_SIZE(Pcd-
>>%s)) || (__ARRAY_SIZE(Pcd->%s) == 0), "array checker error");\n' % (Index,
>FieldName.split('[')[0], FieldName.split('[')[0])
>                     if ValueSize > 4:
>                         CApp = CApp + '  Pcd->%s = %dULL; // From %s Line %d
>Value %s\n' % (FieldName, Value, FieldList[FieldName][1],
>FieldList[FieldName][2], FieldList[FieldName][0])
>                     else:
>@@ -1848,8 +1852,12 @@ class DscBuildData(PlatformBuildClassObject):
>                     #
>                         CApp = CApp + '  FieldSize = __FIELD_SIZE(%s, %s);\n' %
>(Pcd.DatumType, FieldName)
>                         CApp = CApp + '  Value     = %s; // From %s Line %d Value %s\n' %
>(DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1],
>FieldList[FieldName][2], FieldList[FieldName][0])
>+                        CApp = CApp + '  __STATIC_ASSERT(__FIELD_SIZE(%s, %s) >= %d,
>"Input buffer exceeds the buffer array");\n' % (Pcd.DatumType, FieldName,
>ValueSize)
>                         CApp = CApp + '  memcpy (&Pcd->%s, Value, (FieldSize > 0 &&
>FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize)
>                     else:
>+                        if '[' in FieldName and ']' in FieldName:
>+                            Index = int(FieldName.split('[')[1].split(']')[0])
>+                            CApp = CApp + '  __STATIC_ASSERT((%d < __ARRAY_SIZE(Pcd-
>>%s)) || (__ARRAY_SIZE(Pcd->%s) == 0), "array checker error");\n' % (Index,
>FieldName.split('[')[0], FieldName.split('[')[0])
>                         if ValueSize > 4:
>                             CApp = CApp + '  Pcd->%s = %dULL; // From %s Line %d
>Value %s\n' % (FieldName, Value, FieldList[FieldName][1],
>FieldList[FieldName][2], FieldList[FieldName][0])
>                         else:
>--
>2.14.1.windows.1
>
>_______________________________________________
>edk2-devel mailing list
>edk2-devel@lists.01.org
>https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-09-12  7:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-09-04  9:26 [PATCH] BaseTools: Check the index of array Zhaozh1x
2018-09-12  7:31 ` Gao, Liming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox