已合并
更新 Cfi 日志的检查路径 #1716
AtomGit-Bot创建于 2024年9月24日
更新 Cfi 日志的检查路径 #1716
已合并
AtomGit-Bot创建于 2024年9月24日
refs/pull/1716/head合入到master
8 个文件变更+318-95
Mlibc-test/src/common/cfi_util.h+6-11
@@ -17,12 +17,12 @@
17#include <string.h>17#include <string.h>
18#include <dirent.h>18#include <dirent.h>
19#include <string>19#include <string>
20+#include <unistd.h>
20 21 
21extern "C" {22extern "C" {
22 #include "test.h"23 #include "test.h"
23}24}
24 25 
25-#define UBSAN_LOG_DIR "/data/log/sanitizer/ubsan/"
26#define FAULTLOG_DIR "/data/log/faultlog/faultlogger/"26#define FAULTLOG_DIR "/data/log/faultlog/faultlogger/"
27#define UBSAN_LOG_TAG "ubsan"27#define UBSAN_LOG_TAG "ubsan"
28#define DEBUG 028#define DEBUG 0
@@ -35,10 +35,10 @@ static void ShowCfiLogFile()
35{35{
36 DIR *dir;36 DIR *dir;
37 struct dirent *ptr;37 struct dirent *ptr;
38- dir = opendir(UBSAN_LOG_DIR);38+ dir = opendir(FAULTLOG_DIR);
39 while ((ptr = readdir(dir)) != NULL) {39 while ((ptr = readdir(dir)) != NULL) {
40 if (strstr(ptr->d_name, UBSAN_LOG_TAG) != NULL) {40 if (strstr(ptr->d_name, UBSAN_LOG_TAG) != NULL) {
41- printf("%s: %s\n", UBSAN_LOG_DIR, ptr->d_name);41+ printf("%s: %s\n", FAULTLOG_DIR, ptr->d_name);
42 }42 }
43 }43 }
44 closedir(dir);44 closedir(dir);
@@ -46,7 +46,7 @@ static void ShowCfiLogFile()
46 46 
47static void ClearCfiLog()47static void ClearCfiLog()
48{48{
49- ClearCfiLog(UBSAN_LOG_TAG, UBSAN_LOG_DIR);49+ ClearCfiLog(UBSAN_LOG_TAG, FAULTLOG_DIR);
50}50}
51 51 
52static void ClearCfiLog(const char *log_tag, const char *log_dir)52static void ClearCfiLog(const char *log_tag, const char *log_dir)
@@ -108,12 +108,13 @@ static void CheckCfiLog(char *file, const char *needle)
108template<typename CallbackT>108template<typename CallbackT>
109static void FindDirAndCheck(DIR *dir, CallbackT &&callback)109static void FindDirAndCheck(DIR *dir, CallbackT &&callback)
110{110{
111- FindDirAndCheck(dir, UBSAN_LOG_TAG, UBSAN_LOG_DIR, callback);111+ FindDirAndCheck(dir, UBSAN_LOG_TAG, FAULTLOG_DIR, callback);
112}112}
113 113 
114template<typename CallbackT>114template<typename CallbackT>
115static void FindDirAndCheck(DIR *dir, const char *log_tag, const char *log_dir, CallbackT &&callback)115static void FindDirAndCheck(DIR *dir, const char *log_tag, const char *log_dir, CallbackT &&callback)
116{116{
117+ sleep(1);
belanLuhujinchengJason
belanLubelanLu2024年9月29日

这个等待cfi日志落盘是否合理,请审视

likedislike
belanLubelanLu2024年9月29日

这个等待cfi日志落盘是否合理,请审视

为什么之前/dev/ubsan路径落盘速度会快?

likedislike
hujinchengJasonhujinchengJason2024年9月29日

这个等待cfi日志落盘是否合理,请审视

如果指定了 log_path 不为 nullptr,ubsan 会在初始化的时候就创建好落盘的文件,一次运行的多次触发的日志都写进这个文件里 而 DFX 的落盘,是在触发之后才去创建日志文件,每次触发都单独落盘一个日志文件

likedislike
liyiming13liyiming132024年10月22日

【一般】这里不能通过某种方式主动检查吗,sleep有失败的可能性

likedislike
hujinchengJasonhujinchengJason2024年10月24日

这么做目的是等 DFX 日志落盘,然后才能进行后续的日志内容检查 所以不得不 sleep 1 秒钟

likedislike
117 struct dirent *ptr;118 struct dirent *ptr;
118 while ((ptr = readdir(dir)) != NULL) {119 while ((ptr = readdir(dir)) != NULL) {
119 if (strstr(ptr->d_name, log_tag) != NULL) {120 if (strstr(ptr->d_name, log_tag) != NULL) {
@@ -126,12 +127,9 @@ static void FindDirAndCheck(DIR *dir, const char *log_tag, const char *log_dir,
126 127 
127static void FindAndCheck(const char *pattern)128static void FindAndCheck(const char *pattern)
128{129{
129- DIR *ubsanDir = opendir(UBSAN_LOG_DIR);
130 DIR *faultlogDir = opendir(FAULTLOG_DIR);130 DIR *faultlogDir = opendir(FAULTLOG_DIR);
131 auto callback = [=](char *file) { CheckCfiLog(file, pattern); };131 auto callback = [=](char *file) { CheckCfiLog(file, pattern); };
132- FindDirAndCheck(ubsanDir, callback);
133 FindDirAndCheck(faultlogDir, callback);132 FindDirAndCheck(faultlogDir, callback);
134- closedir(ubsanDir);
135 closedir(faultlogDir);133 closedir(faultlogDir);
136}134}
137 135 
@@ -145,11 +143,8 @@ static void FindAndCheck(const char *pattern, const char *log_tag, const char *l
145 143 
146static void ExpectCfiOk()144static void ExpectCfiOk()
147{145{
148- DIR *ubsanDir = opendir(UBSAN_LOG_DIR);
149 DIR *faultlogDir = opendir(FAULTLOG_DIR);146 DIR *faultlogDir = opendir(FAULTLOG_DIR);
150 auto callback = [](char *file) { t_error("FAIL CFI check failed!\n"); };147 auto callback = [](char *file) { t_error("FAIL CFI check failed!\n"); };
151- FindDirAndCheck(ubsanDir, callback);
152 FindDirAndCheck(faultlogDir, callback);148 FindDirAndCheck(faultlogDir, callback);
153- closedir(ubsanDir);
154 closedir(faultlogDir);149 closedir(faultlogDir);
155}150}
Mlibc-test/src/functionalext/ldso_cfi/BUILD.gn+87-3
@@ -16,7 +16,11 @@ import("../../../test_template.gni")
16group("ldso_cfi_test") {16group("ldso_cfi_test") {
17 testonly = true17 testonly = true
18 deps = [18 deps = [
19- ":cfi_avaiable_schemes_test",19+ ":cfi_available_derived_cast_test",
20+ ":cfi_available_icall_test",
21+ ":cfi_available_nvcall_test",
22+ ":cfi_available_unrelated_cast_test",
23+ ":cfi_available_vccall_test",
20 ":cfi_cross_dso_dtor_test_exe",24 ":cfi_cross_dso_dtor_test_exe",
21 ":cfi_cross_dso_dtor_test_use",25 ":cfi_cross_dso_dtor_test_use",
22 ":cfi_cross_dso_test_exe",26 ":cfi_cross_dso_test_exe",
@@ -159,7 +163,7 @@ ohos_executable("cfi_cross_dso_dtor_test_exe") {
159 deps = [ ":cfi_cross_dso_dtor_test_base" ]163 deps = [ ":cfi_cross_dso_dtor_test_base" ]
160}164}
161 165 
162-ohos_executable("cfi_avaiable_schemes_test") {166+ohos_executable("cfi_available_derived_cast_test") {
163 sanitize = {167 sanitize = {
164 cfi = true168 cfi = true
165 cfi_cross_dso = true169 cfi_cross_dso = true
@@ -175,7 +179,87 @@ ohos_executable("cfi_avaiable_schemes_test") {
175 ]179 ]
176 use_rtti = true180 use_rtti = true
177 181 
178- sources = [ "cfi_avaiable_schemes_test.cpp" ]182+ sources = [ "cfi_available_derived_cast_test.cpp" ]
183+ configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
184+}
185+ 
186+ohos_executable("cfi_available_icall_test") {
187+ sanitize = {
188+ cfi = true
189+ cfi_cross_dso = true
190+ debug = true
191+ }
192+ subsystem_name = "musl"
193+ part_name = "libc-test"
194+ include_dirs = [
195+ "../common",
196+ "//third_party/musl/porting/linux/user/include",
197+ "//third_party/musl/porting/linux/user/ldso",
198+ "//third_party/musl/libc-test/src/common",
199+ ]
200+ use_rtti = true
201+ 
202+ sources = [ "cfi_available_icall_test.cpp" ]
203+ configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
204+}
205+ 
206+ohos_executable("cfi_available_nvcall_test") {
207+ sanitize = {
208+ cfi = true
209+ cfi_cross_dso = true
210+ debug = true
211+ }
212+ subsystem_name = "musl"
213+ part_name = "libc-test"
214+ include_dirs = [
215+ "../common",
216+ "//third_party/musl/porting/linux/user/include",
217+ "//third_party/musl/porting/linux/user/ldso",
218+ "//third_party/musl/libc-test/src/common",
219+ ]
220+ use_rtti = true
221+ 
222+ sources = [ "cfi_available_nvcall_test.cpp" ]
223+ configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
224+}
225+ 
226+ohos_executable("cfi_available_unrelated_cast_test") {
227+ sanitize = {
228+ cfi = true
229+ cfi_cross_dso = true
230+ debug = true
231+ }
232+ subsystem_name = "musl"
233+ part_name = "libc-test"
234+ include_dirs = [
235+ "../common",
236+ "//third_party/musl/porting/linux/user/include",
237+ "//third_party/musl/porting/linux/user/ldso",
238+ "//third_party/musl/libc-test/src/common",
239+ ]
240+ use_rtti = true
241+ 
242+ sources = [ "cfi_available_unrelated_cast_test.cpp" ]
243+ configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
244+}
245+ 
246+ohos_executable("cfi_available_vccall_test") {
247+ sanitize = {
248+ cfi = true
249+ cfi_cross_dso = true
250+ debug = true
251+ }
252+ subsystem_name = "musl"
253+ part_name = "libc-test"
254+ include_dirs = [
255+ "../common",
256+ "//third_party/musl/porting/linux/user/include",
257+ "//third_party/musl/porting/linux/user/ldso",
258+ "//third_party/musl/libc-test/src/common",
259+ ]
260+ use_rtti = true
261+ 
262+ sources = [ "cfi_available_vccall_test.cpp" ]
179 configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]263 configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
180}264}
181 265 
Alibc-test/src/functionalext/ldso_cfi/cfi_available_derived_cast_test.cpp+43-0
@@ -0,0 +1,43 @@
1+/**
2+ * Copyright (c) 2023 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+#include "cfi_class_hierarchy.h"
17+ 
18+void CfiDerivedCast()
19+{
20+ B *b = new B;
21+ A a;
22+ b = static_cast<B *>(&a);
23+}
24+ 
25+int main()
26+{
27+ if (DEBUG) {
28+ ShowCfiLogFile();
29+ }
30+ ClearCfiLog();
31+ if (DEBUG) {
32+ ShowCfiLogFile();
33+ }
34+ 
35+ CfiDerivedCast();
36+ FindAndCheck("runtime error: control flow integrity check for type 'B' failed during base-to-derived cast");
37+ 
38+ if (DEBUG) {
39+ ShowCfiLogFile();
40+ }
41+
42+ return t_status;
43+}
Alibc-test/src/functionalext/ldso_cfi/cfi_available_icall_test.cpp+46-0
@@ -0,0 +1,46 @@
1+/**
2+ * Copyright (c) 2023 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+#include "cfi_class_hierarchy.h"
17+ 
18+void Icall()
19+{
20+ printf("Icall()\n");
21+}
22+ 
23+void CfiIcall()
24+{
25+ ((void (*)(int))Icall)(42);
26+}
27+ 
28+int main()
29+{
30+ if (DEBUG) {
31+ ShowCfiLogFile();
32+ }
33+ ClearCfiLog();
34+ if (DEBUG) {
35+ ShowCfiLogFile();
36+ }
37+ 
38+ CfiIcall();
39+ FindAndCheck("runtime error: control flow integrity check for type 'void (int)' failed during indirect function call");
40+ 
41+ if (DEBUG) {
42+ ShowCfiLogFile();
43+ }
44+
45+ return t_status;
46+}
Alibc-test/src/functionalext/ldso_cfi/cfi_available_nvcall_test.cpp+44-0
@@ -0,0 +1,44 @@
1+/**
2+ * Copyright (c) 2023 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+#include "cfi_class_hierarchy.h"
17+ 
18+void CfiNvcall()
19+{
20+ CallTestA *a;
21+ void *p = new CallTestB;
belanLu
belanLubelanLu2024年9月29日

这个void* 强转是否多余?

void *p = new CallTestB();
likedislike
22+ memcpy(&a, &p, sizeof(a));
23+ a->CallFunc();
24+}
25+ 
26+int main()
27+{
28+ if (DEBUG) {
29+ ShowCfiLogFile();
30+ }
31+ ClearCfiLog();
32+ if (DEBUG) {
33+ ShowCfiLogFile();
34+ }
35+ 
36+ CfiNvcall();
37+ FindAndCheck("runtime error: control flow integrity check for type 'CallTestA' failed during non-virtual call");
38+ 
39+ if (DEBUG) {
40+ ShowCfiLogFile();
41+ }
42+
43+ return t_status;
44+}
Alibc-test/src/functionalext/ldso_cfi/cfi_available_unrelated_cast_test.cpp+43-0
@@ -0,0 +1,43 @@
1+/**
2+ * Copyright (c) 2023 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+#include "cfi_class_hierarchy.h"
17+ 
18+void CfiUnrelatedCast()
19+{
20+ D *d = new D;
21+ A a;
22+ d = ((D *)&a);
23+}
24+ 
25+int main()
26+{
27+ if (DEBUG) {
28+ ShowCfiLogFile();
29+ }
30+ ClearCfiLog();
31+ if (DEBUG) {
32+ ShowCfiLogFile();
33+ }
34+ 
35+ CfiUnrelatedCast();
36+ FindAndCheck("runtime error: control flow integrity check for type 'D' failed during cast to unrelated type");
37+ 
38+ if (DEBUG) {
39+ ShowCfiLogFile();
40+ }
41+
42+ return t_status;
43+}
Alibc-test/src/functionalext/ldso_cfi/cfi_available_vccall_test.cpp+44-0
@@ -0,0 +1,44 @@
1+/**
2+ * Copyright (c) 2023 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+#include "cfi_class_hierarchy.h"
17+ 
18+void CfiVcall()
19+{
20+ CallTestA *a;
21+ void *p = new CallTestB;
22+ memcpy(&a, &p, sizeof(a));
23+ a->VcallFunc();
24+}
25+ 
26+int main()
27+{
28+ if (DEBUG) {
29+ ShowCfiLogFile();
30+ }
31+ ClearCfiLog();
32+ if (DEBUG) {
33+ ShowCfiLogFile();
34+ }
35+ 
36+ CfiVcall();
37+ FindAndCheck("runtime error: control flow integrity check for type 'CallTestA' failed during virtual call");
38+ 
39+ if (DEBUG) {
40+ ShowCfiLogFile();
41+ }
42+
43+ return t_status;
44+}
Rlibc-test/src/functionalext/ldso_cfi/cfi_avaiable_schemes_test.cpplibc-test/src/functionalext/ldso_cfi/cfi_class_hierarchy.h+5-81
@@ -1,5 +1,5 @@
1/**1/**
2- * Copyright (c) 2023 Huawei Device Co., Ltd.2+ * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at5 * You may obtain a copy of the License at
@@ -13,6 +13,9 @@
13 * limitations under the License.13 * limitations under the License.
14 */14 */
15 15 
16+#ifndef CFI_CLASS_HIERARCHY_H
17+#define CFI_CLASS_HIERARCHY_H
18+ 
16#include "cfi_util.h"19#include "cfi_util.h"
17 20 
18struct A {21struct A {
@@ -75,83 +78,4 @@ void CallTestB::CallFunc()
75 printf("CallTestB::CallFunc()\n");78 printf("CallTestB::CallFunc()\n");
76}79}
77 80 
78-void CfiCastStrict()81+#endif
79-{
80- C *c = new C;
81- A a;
82- c = static_cast<C *>(&a);
83-}
84- 
85-void CfiDerivedCast()
86-{
87- B *b = new B;
88- A a;
89- b = static_cast<B *>(&a);
90-}
91- 
92-void CfiUnrelatedCast()
93-{
94- D *d = new D;
95- A a;
96- d = ((D *)&a);
97-}
98- 
99-void Icall()
100-{
101- printf("Icall()\n");
102-}
103- 
104-void CfiIcall()
105-{
106- ((void (*)(int))Icall)(42);
107-}
108- 
109-void CfiVcall()
110-{
111- CallTestA *a;
112- void *p = (void *)(new CallTestB());
113- memcpy(&a, &p, sizeof(a));
114- a->VcallFunc();
115-}
116- 
117-void CfiNvcall()
118-{
119- CallTestA *a;
120- void *p = (void *)(new CallTestB());
121- memcpy(&a, &p, sizeof(a));
122- a->CallFunc();
123-}
124- 
125- 
126-int main()
127-{
128- if (DEBUG) {
129- ShowCfiLogFile();
130- }
131- ClearCfiLog();
132- if (DEBUG) {
133- ShowCfiLogFile();
134- }
135- // clang allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
136- CfiCastStrict();
137- 
138- CfiDerivedCast();
139- FindAndCheck("runtime error: control flow integrity check for type 'B' failed during base-to-derived cast");
140- 
141- CfiUnrelatedCast();
142- FindAndCheck("runtime error: control flow integrity check for type 'D' failed during cast to unrelated type");
143- 
144- CfiNvcall();
145- FindAndCheck("runtime error: control flow integrity check for type 'CallTestA' failed during non-virtual call");
146- 
147- CfiVcall();
148- FindAndCheck("runtime error: control flow integrity check for type 'CallTestA' failed during virtual call");
149- 
150- CfiIcall();
151- FindAndCheck(
152- "runtime error: control flow integrity check for type 'void (int)' failed during indirect function call");
153- 
154- if (DEBUG) {
155- ShowCfiLogFile();
156- }
157-}