Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
bbc84241
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bbc84241
编写于
7月 01, 2016
作者:
S
Scott Olson
提交者:
GitHub
7月 01, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #41 from oli-obk/master
optimize all ZST allocations into one single allocation
上级
1720b1f4
3d958833
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
55 addition
and
8 deletion
+55
-8
src/interpreter/terminator.rs
src/interpreter/terminator.rs
+2
-2
src/memory.rs
src/memory.rs
+39
-5
tests/compile-fail/out_of_bounds_read.rs
tests/compile-fail/out_of_bounds_read.rs
+1
-1
tests/compile-fail/out_of_bounds_read2.rs
tests/compile-fail/out_of_bounds_read2.rs
+5
-0
tests/run-pass/zst.rs
tests/run-pass/zst.rs
+8
-0
未找到文件。
src/interpreter/terminator.rs
浏览文件 @
bbc84241
...
...
@@ -423,8 +423,8 @@ fn call_c_abi(
"__rust_reallocate"
=>
{
let
ptr
=
self
.memory
.read_ptr
(
args
[
0
])
?
;
let
size
=
self
.memory
.read_usize
(
args
[
2
])
?
;
self
.memory
.reallocate
(
ptr
,
size
as
usize
)
?
;
self
.memory
.write_ptr
(
dest
,
ptr
)
?
;
let
new_ptr
=
self
.memory
.reallocate
(
ptr
,
size
as
usize
)
?
;
self
.memory
.write_ptr
(
dest
,
new_
ptr
)
?
;
}
"memcmp"
=>
{
...
...
src/memory.rs
浏览文件 @
bbc84241
...
...
@@ -41,6 +41,15 @@ impl Pointer {
pub
fn
offset
(
self
,
i
:
isize
)
->
Self
{
Pointer
{
offset
:
(
self
.offset
as
isize
+
i
)
as
usize
,
..
self
}
}
pub
fn
points_to_zst
(
&
self
)
->
bool
{
self
.alloc_id
.0
==
0
}
fn
zst_ptr
()
->
Self
{
Pointer
{
alloc_id
:
ZST_ALLOC_ID
,
offset
:
0
,
}
}
}
#[derive(Debug,
Copy,
Clone,
Hash,
Eq,
PartialEq)]
...
...
@@ -66,15 +75,29 @@ pub struct Memory<'a, 'tcx> {
pub
layout
:
&
'a
TargetDataLayout
,
}
const
ZST_ALLOC_ID
:
AllocId
=
AllocId
(
0
);
impl
<
'a
,
'tcx
>
Memory
<
'a
,
'tcx
>
{
pub
fn
new
(
layout
:
&
'a
TargetDataLayout
)
->
Self
{
Memory
{
let
mut
mem
=
Memory
{
alloc_map
:
HashMap
::
new
(),
functions
:
HashMap
::
new
(),
function_alloc_cache
:
HashMap
::
new
(),
next_id
:
AllocId
(
0
),
next_id
:
AllocId
(
1
),
layout
:
layout
,
}
};
// alloc id 0 is reserved for ZSTs, this is an optimization to prevent ZST
// (e.g. function items, (), [], ...) from requiring memory
let
alloc
=
Allocation
{
bytes
:
Vec
::
new
(),
relocations
:
BTreeMap
::
new
(),
undef_mask
:
UndefMask
::
new
(
0
),
};
mem
.alloc_map
.insert
(
ZST_ALLOC_ID
,
alloc
);
// check that additional zst allocs work
debug_assert!
(
mem
.allocate
(
0
)
.points_to_zst
());
debug_assert!
(
mem
.get
(
ZST_ALLOC_ID
)
.is_ok
());
mem
}
pub
fn
allocations
<
'b
>
(
&
'b
self
)
->
::
std
::
collections
::
hash_map
::
Iter
<
'b
,
AllocId
,
Allocation
>
{
...
...
@@ -105,6 +128,9 @@ pub fn create_fn_ptr(&mut self, def_id: DefId, substs: &'tcx Substs<'tcx>, fn_ty
}
pub
fn
allocate
(
&
mut
self
,
size
:
usize
)
->
Pointer
{
if
size
==
0
{
return
Pointer
::
zst_ptr
();
}
let
alloc
=
Allocation
{
bytes
:
vec!
[
0
;
size
],
relocations
:
BTreeMap
::
new
(),
...
...
@@ -121,11 +147,14 @@ pub fn allocate(&mut self, size: usize) -> Pointer {
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
// when reallocating/deallocating any others.
pub
fn
reallocate
(
&
mut
self
,
ptr
:
Pointer
,
new_size
:
usize
)
->
EvalResult
<
'tcx
,
()
>
{
pub
fn
reallocate
(
&
mut
self
,
ptr
:
Pointer
,
new_size
:
usize
)
->
EvalResult
<
'tcx
,
Pointer
>
{
if
ptr
.offset
!=
0
{
// TODO(solson): Report error about non-__rust_allocate'd pointer.
return
Err
(
EvalError
::
Unimplemented
(
format!
(
"bad pointer offset: {}"
,
ptr
.offset
)));
}
if
ptr
.points_to_zst
()
{
return
Ok
(
self
.allocate
(
new_size
));
}
let
size
=
self
.get_mut
(
ptr
.alloc_id
)
?
.bytes
.len
();
...
...
@@ -141,21 +170,26 @@ pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<'tcx,
alloc
.undef_mask
.truncate
(
new_size
);
}
Ok
(
()
)
Ok
(
ptr
)
}
// TODO(solson): See comment on `reallocate`.
pub
fn
deallocate
(
&
mut
self
,
ptr
:
Pointer
)
->
EvalResult
<
'tcx
,
()
>
{
if
ptr
.points_to_zst
()
{
return
Ok
(());
}
if
ptr
.offset
!=
0
{
// TODO(solson): Report error about non-__rust_allocate'd pointer.
return
Err
(
EvalError
::
Unimplemented
(
format!
(
"bad pointer offset: {}"
,
ptr
.offset
)));
}
if
self
.alloc_map
.remove
(
&
ptr
.alloc_id
)
.is_none
()
{
debug!
(
"deallocated a pointer twice: {}"
,
ptr
.alloc_id
);
// TODO(solson): Report error about erroneous free. This is blocked on properly tracking
// already-dropped state since this if-statement is entered even in safe code without
// it.
}
debug!
(
"deallocated : {}"
,
ptr
.alloc_id
);
Ok
(())
}
...
...
tests/compile-fail/out_of_bounds_read.rs
浏览文件 @
bbc84241
fn
main
()
{
let
v
:
Vec
<
u8
>
=
vec!
[
1
,
2
];
let
x
=
unsafe
{
*
v
.get_unchecked
(
5
)
};
//~ ERROR:
memory access of 5..6 outside bounds of allocation 31
which has size 2
let
x
=
unsafe
{
*
v
.get_unchecked
(
5
)
};
//~ ERROR: which has size 2
panic!
(
"this should never print: {}"
,
x
);
}
tests/compile-fail/out_of_bounds_read2.rs
0 → 100644
浏览文件 @
bbc84241
fn
main
()
{
let
v
:
Vec
<
u8
>
=
vec!
[
1
,
2
];
let
x
=
unsafe
{
*
v
.get_unchecked
(
5
)
};
//~ ERROR: memory access of 5..6 outside bounds of allocation
panic!
(
"this should never print: {}"
,
x
);
}
tests/run-pass/zst.rs
浏览文件 @
bbc84241
// the following flag prevents this test from running on the host machine
// this should only be run on miri, because rust doesn't (yet?) optimize ZSTs of different types
// into the same memory location
// ignore-test
#[derive(PartialEq,
Debug)]
struct
A
;
...
...
@@ -13,4 +19,6 @@ fn use_zst() -> A {
fn
main
()
{
assert_eq!
(
zst_ret
(),
A
);
assert_eq!
(
use_zst
(),
A
);
assert_eq!
(
&
A
as
*
const
A
as
*
const
(),
&
()
as
*
const
_
);
assert_eq!
(
&
A
as
*
const
A
,
&
A
as
*
const
A
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录