Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f02d9e63
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,发现更多精彩内容 >>
提交
f02d9e63
编写于
7月 20, 2017
作者:
O
Oliver Schneider
提交者:
GitHub
7月 20, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #259 from RalfJung/ref-validate
No longer check aligment and non-NULLness on `&`
上级
27c64479
7648ccaa
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
28 addition
and
31 deletion
+28
-31
src/cast.rs
src/cast.rs
+1
-3
src/eval_context.rs
src/eval_context.rs
+12
-12
src/memory.rs
src/memory.rs
+5
-3
tests/compile-fail/int_ptr_cast.rs
tests/compile-fail/int_ptr_cast.rs
+0
-5
tests/compile-fail/int_ptr_cast2.rs
tests/compile-fail/int_ptr_cast2.rs
+0
-5
tests/compile-fail/reference_to_packed.rs
tests/compile-fail/reference_to_packed.rs
+2
-2
tests/compile-fail/unaligned_ptr_cast.rs
tests/compile-fail/unaligned_ptr_cast.rs
+1
-1
tests/compile-fail/unaligned_ptr_cast2.rs
tests/compile-fail/unaligned_ptr_cast2.rs
+7
-0
未找到文件。
src/cast.rs
浏览文件 @
f02d9e63
...
...
@@ -46,9 +46,7 @@ fn cast_signed_int(&self, val: i128, ty: ty::Ty<'tcx>) -> EvalResult<'tcx, PrimV
fn
cast_int
(
&
self
,
v
:
u128
,
ty
:
ty
::
Ty
<
'tcx
>
,
negative
:
bool
)
->
EvalResult
<
'tcx
,
PrimVal
>
{
use
rustc
::
ty
::
TypeVariants
::
*
;
match
ty
.sty
{
TyBool
if
v
==
0
=>
Ok
(
PrimVal
::
from_bool
(
false
)),
TyBool
if
v
==
1
=>
Ok
(
PrimVal
::
from_bool
(
true
)),
TyBool
=>
Err
(
EvalError
::
InvalidBool
),
// Casts to bool are not permitted by rustc, no need to handle them here.
TyInt
(
IntTy
::
I8
)
=>
Ok
(
PrimVal
::
Bytes
(
v
as
i128
as
i8
as
u128
)),
TyInt
(
IntTy
::
I16
)
=>
Ok
(
PrimVal
::
Bytes
(
v
as
i128
as
i16
as
u128
)),
...
...
src/eval_context.rs
浏览文件 @
f02d9e63
...
...
@@ -670,9 +670,9 @@ pub(super) fn eval_rvalue_into_lvalue(
Ref
(
_
,
_
,
ref
lvalue
)
=>
{
let
src
=
self
.eval_lvalue
(
lvalue
)
?
;
// We ignore the alignment of the lvalue here -- this rvalue produces sth. of type &, which must always be aligned.
// We ignore the alignment of the lvalue here -- special handling for packed structs ends
// at the `&` operator.
let
(
ptr
,
extra
,
_
aligned
)
=
self
.force_allocation
(
src
)
?
.to_ptr_extra_aligned
();
let
ty
=
self
.lvalue_ty
(
lvalue
);
let
val
=
match
extra
{
LvalueExtra
::
None
=>
ptr
.to_value
(),
...
...
@@ -682,10 +682,6 @@ pub(super) fn eval_rvalue_into_lvalue(
bug!
(
"attempted to take a reference to an enum downcast lvalue"
),
};
// Check alignment and non-NULLness.
let
(
_
,
align
)
=
self
.size_and_align_of_dst
(
ty
,
val
)
?
;
self
.memory
.check_align
(
ptr
,
align
)
?
;
self
.write_value
(
val
,
dest
,
dest_ty
)
?
;
}
...
...
@@ -1105,6 +1101,7 @@ pub(super) fn write_value(
dest
:
Lvalue
<
'tcx
>
,
dest_ty
:
Ty
<
'tcx
>
,
)
->
EvalResult
<
'tcx
>
{
//trace!("Writing {:?} to {:?} at type {:?}", src_val, dest, dest_ty);
// Note that it is really important that the type here is the right one, and matches the type things are read at.
// In case `src_val` is a `ByValPair`, we don't do any magic here to handle padding properly, which is only
// correct if we never look at this data with the wrong type.
...
...
@@ -1382,7 +1379,10 @@ fn try_read_value(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, Opt
// if we transmute a ptr to an isize, reading it back into a primval shouldn't panic
// Due to read_ptr ignoring the sign, we need to jump around some hoops
match
self
.memory
.read_int
(
ptr
.to_ptr
()
?
,
size
)
{
Err
(
EvalError
::
ReadPointerAsBytes
)
if
size
==
self
.memory
.pointer_size
()
=>
self
.memory
.read_ptr
(
ptr
.to_ptr
()
?
)
?
.into_inner_primval
(),
Err
(
EvalError
::
ReadPointerAsBytes
)
if
size
==
self
.memory
.pointer_size
()
=>
// Reading as an int failed because we are seeing ptr bytes *and* we are actually reading at ptr size.
// Let's try again, reading a ptr this time.
self
.memory
.read_ptr
(
ptr
.to_ptr
()
?
)
?
.into_inner_primval
(),
other
=>
PrimVal
::
from_i128
(
other
?
),
}
}
...
...
@@ -1397,11 +1397,11 @@ fn try_read_value(&mut self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, Opt
U128
=>
16
,
Us
=>
self
.memory
.pointer_size
(),
};
if
size
==
self
.memory
.pointer_size
()
{
// if we transmute a ptr to an usize, reading it back into a primval shouldn't panic
self
.memory
.read_ptr
(
ptr
.to_ptr
()
?
)
?
.into_inner_primval
()
}
else
{
PrimVal
::
from_u128
(
self
.memory
.read_uint
(
ptr
.to_ptr
()
?
,
size
)
?
)
// if we transmute a ptr to an usize, reading it back into a primval shouldn't panic
// for consistency's sake, we use the same code as above
match
self
.memory
.read_uint
(
ptr
.to_ptr
()
?
,
size
)
{
Err
(
EvalError
::
ReadPointerAsBytes
)
if
size
==
self
.memory
.pointer_size
()
=>
self
.memory
.read_ptr
(
ptr
.to_ptr
()
?
)
?
.into_inner_primval
(),
other
=>
PrimVal
::
from_u128
(
other
?
),
}
}
...
...
src/memory.rs
浏览文件 @
f02d9e63
...
...
@@ -720,12 +720,14 @@ pub fn write_repeat(&mut self, ptr: Pointer, val: u8, count: u64) -> EvalResult<
pub
fn
read_ptr
(
&
self
,
ptr
:
MemoryPointer
)
->
EvalResult
<
'tcx
,
Pointer
>
{
let
size
=
self
.pointer_size
();
if
self
.check_defined
(
ptr
,
size
)
.is_err
()
{
return
Ok
(
PrimVal
::
Undef
.into
());
}
self
.check_relocation_edges
(
ptr
,
size
)
?
;
// Make sure we don't read part of a pointer as a pointer
let
endianess
=
self
.endianess
();
let
bytes
=
self
.get_bytes_unchecked
(
ptr
,
size
,
size
)
?
;
// Undef check happens *after* we established that the alignment is correct.
// We must not return Ok() for unaligned pointers!
if
self
.check_defined
(
ptr
,
size
)
.is_err
()
{
return
Ok
(
PrimVal
::
Undef
.into
());
}
let
offset
=
read_target_uint
(
endianess
,
bytes
)
.unwrap
();
assert_eq!
(
offset
as
u64
as
u128
,
offset
);
let
offset
=
offset
as
u64
;
...
...
tests/compile-fail/int_ptr_cast.rs
已删除
100644 → 0
浏览文件 @
27c64479
fn
main
()
{
let
x
=
2u
size
as
*
const
u32
;
// This must fail because alignment is violated
let
_
=
unsafe
{
&*
x
};
//~ ERROR: tried to access memory with alignment 2, but alignment 4 is required
}
tests/compile-fail/int_ptr_cast2.rs
已删除
100644 → 0
浏览文件 @
27c64479
fn
main
()
{
let
x
=
0u
size
as
*
const
u32
;
// This must fail because the pointer is NULL
let
_
=
unsafe
{
&*
x
};
//~ ERROR: invalid use of NULL pointer
}
tests/compile-fail/reference_to_packed.rs
浏览文件 @
f02d9e63
...
...
@@ -11,6 +11,6 @@ fn main() {
x
:
42
,
y
:
99
,
};
let
p
=
&
foo
.x
;
//~ ERROR tried to access memory with alignment 1, but alignment 4 is required
let
i
=
*
p
;
let
p
=
&
foo
.x
;
let
i
=
*
p
;
//~ ERROR tried to access memory with alignment 1, but alignment 4 is required
}
tests/compile-fail/unaligned_ptr_cast.rs
浏览文件 @
f02d9e63
...
...
@@ -2,5 +2,5 @@ fn main() {
let
x
=
&
2u16
;
let
x
=
x
as
*
const
_
as
*
const
u32
;
// This must fail because alignment is violated
let
_
=
unsafe
{
&
*
x
};
//~ ERROR: tried to access memory with alignment 2, but alignment 4 is required
let
_
x
=
unsafe
{
*
x
};
//~ ERROR: tried to access memory with alignment 2, but alignment 4 is required
}
tests/compile-fail/unaligned_ptr_cast2.rs
0 → 100644
浏览文件 @
f02d9e63
fn
main
()
{
let
x
=
&
2u16
;
let
x
=
x
as
*
const
_
as
*
const
*
const
u8
;
// This must fail because alignment is violated. Test specifically for loading pointers, which have special code
// in miri's memory.
let
_
x
=
unsafe
{
*
x
};
//~ ERROR: tried to access memory with alignment 2, but alignment
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录