From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=liming.gao@intel.com; receiver=edk2-devel@lists.01.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 92F7A2115F519 for ; Mon, 15 Oct 2018 00:19:38 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Oct 2018 00:19:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,383,1534834800"; d="scan'208";a="92024374" Received: from fmsmsx108.amr.corp.intel.com ([10.18.124.206]) by orsmga003.jf.intel.com with ESMTP; 15 Oct 2018 00:19:37 -0700 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by FMSMSX108.amr.corp.intel.com (10.18.124.206) with Microsoft SMTP Server (TLS) id 14.3.319.2; Mon, 15 Oct 2018 00:19:37 -0700 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.48]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.245]) with mapi id 14.03.0319.002; Mon, 15 Oct 2018 15:19:35 +0800 From: "Gao, Liming" To: "Zhao, ZhiqiangX" , "edk2-devel@lists.01.org" Thread-Topic: [PATCH V3] BaseTools: add ASSERT checker for array buffer value assignment. Thread-Index: AQHUVJUwGknTTqcVukKlpieV1lu24KUgBaoA Date: Mon, 15 Oct 2018 07:19:35 +0000 Message-ID: <4A89E2EF3DFEDB4C8BFDE51014F606A14E33A097@SHSMSX104.ccr.corp.intel.com> References: <20180925060108.12412-1-zhiqiangx.zhao@intel.com> In-Reply-To: <20180925060108.12412-1-zhiqiangx.zhao@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH V3] BaseTools: add ASSERT checker for array buffer value assignment. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Oct 2018 07:19:38 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Liming Gao >-----Original Message----- >From: Zhao, ZhiqiangX >Sent: Tuesday, September 25, 2018 2:01 PM >To: edk2-devel@lists.01.org >Cc: Zhao, ZhiqiangX ; Gao, Liming >; Zhu, Yonghong ; Feng, >Bob C >Subject: [PATCH V3] BaseTools: add ASSERT checker for array buffer value >assignment. > >V3: >Update the error message for array checker. >V2: >1. Add comments for each ASSERT. >2. ASSERT need to skip the case of array size of array as zero. For >example, TestArray[] in struct in header file. >V1: >For structure PCD, >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 >defined in DEC/DSC file. > >Contributed-under: TianoCore Contribution Agreement 1.1 >Signed-off-by: ZhiqiangX Zhao >Cc: Liming Gao >Cc: Yonghong Zhu >Cc: Bob Feng >--- > 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(T= YPE, >Field) =3D=3D 0) Size =3D 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 ac0f0bee47..224c8eda62 100644 >--- a/BaseTools/Source/Python/Workspace/DscBuildData.py >+++ b/BaseTools/Source/Python/Workspace/DscBuildData.py >@@ -1826,8 +1826,12 @@ class DscBuildData(PlatformBuildClassObject): > # > CApp =3D CApp + ' FieldSize =3D __FIELD_SIZE(%s, %s)= ;\n' % >(Pcd.DatumType, FieldName) > CApp =3D CApp + ' Value =3D %s; // From %s Line = %d Value %s\n' % >(DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1], >FieldList[FieldName][2], FieldList[FieldName][0]) >+ CApp =3D CApp + ' __STATIC_ASSERT((__FIELD_SIZE(%s, = %s) >=3D %d) >|| (__FIELD_SIZE(%s, %s) =3D=3D 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 =3D CApp + ' memcpy (&Pcd->%s, Value, (FieldSiz= e > 0 && >FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize) > else: >+ if '[' in FieldName and ']' in FieldName: >+ Index =3D int(FieldName.split('[')[1].split(']')[= 0]) >+ CApp =3D CApp + ' __STATIC_ASSERT((%d < __ARRAY_= SIZE(Pcd- >>%s)) || (__ARRAY_SIZE(Pcd->%s) =3D=3D 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 =3D CApp + ' Pcd->%s =3D %dULL; // From %s = Line %d >Value %s\n' % (FieldName, Value, FieldList[FieldName][1], >FieldList[FieldName][2], FieldList[FieldName][0]) > else: >@@ -1905,8 +1909,12 @@ class DscBuildData(PlatformBuildClassObject): > # > CApp =3D CApp + ' FieldSize =3D __FIELD_SIZE(%s,= %s);\n' % >(Pcd.DatumType, FieldName) > CApp =3D CApp + ' Value =3D %s; // From %s L= ine %d Value %s\n' % >(DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1], >FieldList[FieldName][2], FieldList[FieldName][0]) >+ CApp =3D CApp + ' >__STATIC_ASSERT((__FIELD_SIZE(%s, %s) >=3D %d) || (__FIELD_SIZE(%s, %s) >=3D=3D 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 =3D CApp + ' memcpy (&Pcd->%s, Value, (Fiel= dSize > 0 && >FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize) > else: >+ if '[' in FieldName and ']' in FieldName: >+ Index =3D int(FieldName.split('[')[1].split('= ]')[0]) >+ CApp =3D CApp + ' __STATIC_ASSERT((%d < __AR= RAY_SIZE(Pcd- >>%s)) || (__ARRAY_SIZE(Pcd->%s) =3D=3D 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 =3D CApp + ' Pcd->%s =3D %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