public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Feng, Bob C" <bob.c.feng@intel.com>
To: "Zhao, ZhiqiangX" <zhiqiangx.zhao@intel.com>,
	"edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: "Gao, Liming" <liming.gao@intel.com>,
	"Zhu, Yonghong" <yonghong.zhu@intel.com>
Subject: Re: [PATCH] BaseTools: add ASSERT checker for array buffer in fdf and command line
Date: Wed, 24 Oct 2018 07:24:53 +0000	[thread overview]
Message-ID: <08650203BA1BD64D8AD9B6D5D74A85D15FFE5ECB@SHSMSX101.ccr.corp.intel.com> (raw)
In-Reply-To: <20181022083743.92736-1-zhiqiangx.zhao@intel.com>

Reviewed-by: Bob Feng <bob.c.feng@intel.com>

-----Original Message-----
From: Zhao, ZhiqiangX 
Sent: Monday, October 22, 2018 4:38 PM
To: edk2-devel@lists.01.org
Cc: Zhao, ZhiqiangX <zhiqiangx.zhao@intel.com>; Gao, Liming <liming.gao@intel.com>; Zhu, Yonghong <yonghong.zhu@intel.com>; Feng, Bob C <bob.c.feng@intel.com>
Subject: [PATCH] BaseTools: add ASSERT checker for array buffer in fdf and command line

For structure PCD in fdf file and command line, 1. use compiler time assert to check the array index, report error if array index exceeds the array number.
2. use compiler time assert to check the array size, report error if the user declared size in header file is smaller than the user used in fdf file and command line.

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/Python/Workspace/DscBuildData.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/BaseTools/Source/Python/Workspace/DscBuildData.py b/BaseTools/Source/Python/Workspace/DscBuildData.py
index b0e88a93ce..01a565aa08 100644
--- a/BaseTools/Source/Python/Workspace/DscBuildData.py
+++ b/BaseTools/Source/Python/Workspace/DscBuildData.py
@@ -2013,8 +2013,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) || (__FIELD_SIZE(%s, %s) == 0), "Input buffer exceeds the 
+ buffer array"); // From %s Line %d Value %s\n' % (Pcd.DatumType, 
+ FieldName, ValueSize, Pcd.DatumType, FieldName, 
+ FieldList[FieldName][1], FieldList[FieldName][2], 
+ FieldList[FieldName][0])
                     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 index 
+ exceeds the array number"); // From %s Line %d Index of %s\n' % 
+ (Index, FieldName.split('[')[0], FieldName.split('[')[0], 
+ FieldList[FieldName][1], FieldList[FieldName][2], FieldName)
                     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:
@@ -2077,8 +2081,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) || (__FIELD_SIZE(%s, %s) == 0), "Input buffer exceeds the 
+ buffer array"); // From %s Line %d Value %s\n' % (Pcd.DatumType, 
+ FieldName, ValueSize, Pcd.DatumType, FieldName, 
+ FieldList[FieldName][1], FieldList[FieldName][2], 
+ FieldList[FieldName][0])
                     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 index 
+ exceeds the array number"); // From %s Line %d Index of %s\n' % 
+ (Index, FieldName.split('[')[0], FieldName.split('[')[0], 
+ FieldList[FieldName][1], FieldList[FieldName][2], FieldName)
                     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



      reply	other threads:[~2018-10-24  7:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-22  8:37 [PATCH] BaseTools: add ASSERT checker for array buffer in fdf and command line Zhaozh1x
2018-10-24  7:24 ` Feng, Bob C [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=08650203BA1BD64D8AD9B6D5D74A85D15FFE5ECB@SHSMSX101.ccr.corp.intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox