1
1
use rustc_span:: Symbol ;
2
- use rustc_target:: spec:: abi:: Abi ;
3
2
use rustc_target:: abi:: { Align , Size } ;
3
+ use rustc_target:: spec:: abi:: Abi ;
4
4
5
5
use crate :: * ;
6
6
@@ -21,17 +21,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
21
21
match link_name. as_str ( ) {
22
22
// Allocation
23
23
"memalign" => {
24
- let [ align, size] = this. check_shim ( abi, Abi :: C { unwind : false } , link_name, args) ?;
24
+ let [ align, size] =
25
+ this. check_shim ( abi, Abi :: C { unwind : false } , link_name, args) ?;
25
26
let align = this. read_target_usize ( align) ?;
26
27
let size = this. read_target_usize ( size) ?;
27
28
28
- // memalign wants an alignment to be a power of 2
29
- // and to be, at least, of word size.
30
- // http://docs.oracle.com/cd/E88353_01/html/E37743/memalign-3c.html
31
- if !align. is_power_of_two ( ) || align < this. pointer_size ( ) . bytes ( ) {
29
+ // memalign requires the size to be greater than 0, the alignment to be a power of 2
30
+ // to be, at least, of word size in which EINVAL is emitted
31
+ // and a null pointer is returned.
32
+ // https://docs.oracle.com/cd/E88353_01/html/E37843/memalign-3c.html
33
+ if !align. is_power_of_two ( ) || align < this. pointer_size ( ) . bytes ( ) || size == 0 {
34
+ this. set_last_error ( this. eval_libc ( "EINVAL" ) ) ?;
32
35
this. write_null ( dest) ?;
33
36
} else {
34
- let ptr = this. allocate_ptr ( Size :: from_bytes ( size) , Align :: from_bytes ( align) . unwrap ( ) , MiriMemoryKind :: C . into ( ) , ) ?;
37
+ let ptr = this. allocate_ptr (
38
+ Size :: from_bytes ( size) ,
39
+ Align :: from_bytes ( align) . unwrap ( ) ,
40
+ MiriMemoryKind :: C . into ( ) ,
41
+ ) ?;
35
42
this. write_pointer ( ptr, dest) ?;
36
43
}
37
44
}
0 commit comments