Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Musl
提交
4a672a91
T
Third Party Musl
项目概览
OpenHarmony
/
Third Party Musl
1 年多 前同步成功
通知
37
Star
125
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Musl
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4a672a91
编写于
8月 21, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 21, 2023
浏览文件
操作
浏览文件
下载
差异文件
!1022 Add cfi test
Merge pull request !1022 from yinchuang/add_cfi-test
上级
e7501497
3961dc5b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
418 addition
and
0 deletion
+418
-0
libc-test/src/common/cfi_util.h
libc-test/src/common/cfi_util.h
+105
-0
libc-test/src/functionalext/ldso_cfi/BUILD.gn
libc-test/src/functionalext/ldso_cfi/BUILD.gn
+62
-0
libc-test/src/functionalext/ldso_cfi/cfi_avaiable_schemes_test.cpp
.../src/functionalext/ldso_cfi/cfi_avaiable_schemes_test.cpp
+156
-0
libc-test/src/functionalext/ldso_cfi/crossdso/cfi_test_exe.cpp
...test/src/functionalext/ldso_cfi/crossdso/cfi_test_exe.cpp
+59
-0
libc-test/src/functionalext/ldso_cfi/crossdso/cfi_test_lib.cpp
...test/src/functionalext/ldso_cfi/crossdso/cfi_test_lib.cpp
+36
-0
未找到文件。
libc-test/src/common/cfi_util.h
0 → 100644
浏览文件 @
4a672a91
/**
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <dirent.h>
extern
"C"
{
#include "test.h"
}
#define UBSAN_LOG_DIR "/data/log/sanitizer/ubsan/"
#define UBSAN_LOG_TAG "ubsan"
#define DEBUG 0
#define BUFFER_SIZE 4096
static
void
ShowCfiLogFile
()
{
DIR
*
dir
;
struct
dirent
*
ptr
;
dir
=
opendir
(
UBSAN_LOG_DIR
);
while
((
ptr
=
readdir
(
dir
))
!=
NULL
)
{
if
(
strstr
(
ptr
->
d_name
,
UBSAN_LOG_TAG
)
!=
NULL
)
{
printf
(
"%s: %s
\n
"
,
UBSAN_LOG_DIR
,
ptr
->
d_name
);
}
}
closedir
(
dir
);
}
static
void
ClearCfiLog
()
{
DIR
*
dir
;
struct
dirent
*
ptr
;
dir
=
opendir
(
UBSAN_LOG_DIR
);
while
((
ptr
=
readdir
(
dir
))
!=
NULL
)
{
if
(
strstr
(
ptr
->
d_name
,
UBSAN_LOG_TAG
)
!=
NULL
)
{
char
tmp
[
BUFFER_SIZE
]
=
UBSAN_LOG_DIR
;
strcat
(
tmp
,
ptr
->
d_name
);
remove
(
tmp
);
}
}
closedir
(
dir
);
}
static
void
CheckCfiLog
(
char
*
file
,
const
char
*
needle
)
{
if
(
DEBUG
)
{
printf
(
"[cfi checking]:%s
\n
"
,
file
);
}
char
buffer
[
BUFFER_SIZE
];
FILE
*
fp
=
fopen
(
file
,
"r"
);
if
(
!
fp
)
{
return
;
}
if
(
fseek
(
fp
,
0
,
SEEK_END
)
==
-
1
)
{
return
;
}
int
size
=
ftell
(
fp
);
if
(
size
<=
0
)
{
fclose
(
fp
);
t_error
(
"FAIL %s size is <=0!
\n
"
,
file
);
}
if
(
fseek
(
fp
,
0
,
SEEK_SET
)
==
-
1
)
{
fclose
(
fp
);
return
;
}
int
rsize
=
fread
(
buffer
,
1
,
size
,
fp
);
if
(
rsize
==
0
)
{
fclose
(
fp
);
return
;
}
if
(
strstr
(
buffer
,
needle
)
!=
NULL
)
{
printf
(
"[cfi checking] %s is ok.
\n
"
,
needle
);
}
else
{
t_error
(
"FAIL %s is failed!
\n
"
,
needle
);
}
fclose
(
fp
);
}
static
void
FindAndCheck
(
const
char
*
pattern
)
{
DIR
*
dir
;
struct
dirent
*
ptr
;
dir
=
opendir
(
UBSAN_LOG_DIR
);
while
((
ptr
=
readdir
(
dir
))
!=
NULL
)
{
if
(
strstr
(
ptr
->
d_name
,
UBSAN_LOG_TAG
)
!=
NULL
)
{
char
tmp
[
BUFFER_SIZE
]
=
UBSAN_LOG_DIR
;
strcat
(
tmp
,
ptr
->
d_name
);
CheckCfiLog
(
tmp
,
pattern
);
}
}
closedir
(
dir
);
}
\ No newline at end of file
libc-test/src/functionalext/ldso_cfi/BUILD.gn
浏览文件 @
4a672a91
...
...
@@ -16,6 +16,9 @@ import("../../../test_template.gni")
group("ldso_cfi_test") {
testonly = true
deps = [
":cfi_avaiable_schemes_test",
":cfi_cross_dso_test_exe",
":cfi_cross_dso_test_lib",
":ldso_cfi_check",
":ldso_cfi_test_lib",
]
...
...
@@ -44,3 +47,62 @@ ohos_shared_library("ldso_cfi_test_lib") {
subsystem_name = "musl"
part_name = "libc-test-lib"
}
ohos_shared_library("cfi_cross_dso_test_lib") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = true
}
subsystem_name = "musl"
part_name = "libc-test"
include_dirs = [
"../common",
"//third_party/musl/porting/linux/user/include",
"//third_party/musl/porting/linux/user/ldso",
"//third_party/musl/libc-test/src/common",
]
use_rtti = true
sources = [ "./crossdso/cfi_test_lib.cpp" ]
}
ohos_executable("cfi_cross_dso_test_exe") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = true
}
subsystem_name = "musl"
part_name = "libc-test"
include_dirs = [
"../common",
"//third_party/musl/porting/linux/user/include",
"//third_party/musl/porting/linux/user/ldso",
"//third_party/musl/libc-test/src/common",
]
use_rtti = true
sources = [ "./crossdso/cfi_test_exe.cpp" ]
configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
}
ohos_executable("cfi_avaiable_schemes_test") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = true
}
subsystem_name = "musl"
part_name = "libc-test"
include_dirs = [
"../common",
"//third_party/musl/porting/linux/user/include",
"//third_party/musl/porting/linux/user/ldso",
"//third_party/musl/libc-test/src/common",
]
use_rtti = true
sources = [ "cfi_avaiable_schemes_test.cpp" ]
configs = [ "//third_party/musl/libc-test/src/common:config_runtest" ]
}
libc-test/src/functionalext/ldso_cfi/cfi_avaiable_schemes_test.cpp
0 → 100644
浏览文件 @
4a672a91
/**
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cfi_util.h"
struct
A
{
virtual
void
Test
();
};
void
A
::
Test
()
{
printf
(
"A::Test()
\n
"
);
}
struct
B
:
A
{
virtual
void
Test
();
};
void
B
::
Test
()
{
printf
(
"B::Test()
\n
"
);
}
struct
C
:
A
{
};
struct
D
{
virtual
void
Test
();
};
void
D
::
Test
()
{
printf
(
"D::Test()
\n
"
);
}
struct
CallTestA
{
virtual
void
VcallFunc
();
void
CallFunc
();
};
void
CallTestA
::
VcallFunc
()
{
printf
(
"CallTestA::VcallFunc()
\n
"
);
}
void
CallTestA
::
CallFunc
()
{
printf
(
"CallTestA::CallFunc()
\n
"
);
}
struct
CallTestB
{
virtual
void
VcallFunc
();
void
CallFunc
();
};
void
CallTestB
::
VcallFunc
()
{
printf
(
"CallTestB::VcallFunc()
\n
"
);
}
void
CallTestB
::
CallFunc
()
{
printf
(
"CallTestB::CallFunc()
\n
"
);
}
void
CfiCastStrict
()
{
C
*
c
=
new
C
;
A
a
;
c
=
static_cast
<
C
*>
(
&
a
);
}
void
CfiDerivedCast
()
{
B
*
b
=
new
B
;
A
a
;
b
=
static_cast
<
B
*>
(
&
a
);
}
void
CfiUnrelatedCast
()
{
D
*
d
=
new
D
;
A
a
;
d
=
((
D
*
)
&
a
);
}
void
Icall
()
{
printf
(
"Icall()
\n
"
);
}
void
CfiIcall
()
{
((
void
(
*
)(
int
))
Icall
)(
42
);
}
void
CfiVcall
()
{
CallTestA
*
a
;
void
*
p
=
(
void
*
)(
new
CallTestB
());
memcpy
(
&
a
,
&
p
,
sizeof
(
a
));
a
->
VcallFunc
();
}
void
CfiNvcall
()
{
CallTestA
*
a
;
void
*
p
=
(
void
*
)(
new
CallTestB
());
memcpy
(
&
a
,
&
p
,
sizeof
(
a
));
a
->
CallFunc
();
}
int
main
()
{
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
ClearCfiLog
();
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
// clang allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
CfiCastStrict
();
CfiDerivedCast
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'B' failed during base-to-derived cast"
);
CfiUnrelatedCast
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'D' failed during cast to unrelated type"
);
CfiNvcall
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'CallTestA' failed during non-virtual call"
);
CfiVcall
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'CallTestA' failed during virtual call"
);
CfiIcall
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'void (int)' failed during indirect function call"
);
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
}
libc-test/src/functionalext/ldso_cfi/crossdso/cfi_test_exe.cpp
0 → 100644
浏览文件 @
4a672a91
/**
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
#include <dlfcn.h>
#include <cstring>
#include "cfi_util.h"
struct
AA
{
virtual
void
Test
();
};
void
AA
::
Test
()
{
printf
(
"AA::Test()
\n
"
);
}
void
ChangeToAnotherObj
()
{
AA
*
a
=
new
AA
;
void
*
handle
=
dlopen
(
"libcfi_cross_dso_test_lib.z.so"
,
RTLD_NOW
);
if
(
handle
==
nullptr
)
{
return
;
}
void
*
(
*
create_B
)()
=
(
void
*
(
*
)())
dlsym
(
handle
,
"CreateObj"
);
void
*
p
=
create_B
();
a
->
Test
();
memcpy
(
&
a
,
&
p
,
sizeof
(
void
*
));
a
->
Test
();
}
int
main
()
{
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
ClearCfiLog
();
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
ChangeToAnotherObj
();
FindAndCheck
(
"runtime error: control flow integrity check for type 'AA' failed during virtual call"
);
if
(
DEBUG
)
{
ShowCfiLogFile
();
}
return
0
;
}
\ No newline at end of file
libc-test/src/functionalext/ldso_cfi/crossdso/cfi_test_lib.cpp
0 → 100644
浏览文件 @
4a672a91
/**
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
struct
BB
{
void
Func
();
virtual
void
Test
();
};
void
BB
::
Test
()
{
printf
(
"BB::Test()
\n
"
);
}
void
BB
::
Func
()
{
printf
(
"BB::Func()
\n
"
);
}
extern
"C"
void
*
CreateObj
()
{
return
new
BB
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录