forked from tianocore/edk2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
340 lines (282 loc) · 7.77 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2019 Intel Corporation
// Copyright (c) Microsoft Corporation
//
// 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.
#![feature(alloc_layout_extra)]
#![feature(allocator_api)]
#![feature(core_panic_info)]
#![cfg_attr(not(test), no_std)]
#![allow(unused)]
mod mem;
use r_efi::efi;
use r_efi::efi::{Status};
extern {
fn AllocatePool (Size: usize) -> *mut c_void;
fn FreePool (Buffer: *mut c_void);
}
use core::panic::PanicInfo;
use core::ffi::c_void;
use core::mem::size_of;
use core::mem::transmute;
use core::slice;
use core::slice::from_raw_parts;
use core::slice::from_raw_parts_mut;
extern crate uefi_rust_panic_lib;
extern crate uefi_rust_allocation_lib;
extern crate alloc;
use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::{
alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout},
};
#[no_mangle]
#[export_name = "TestIntegerOverflow"]
pub extern fn test_integer_overflow (
buffer_size: usize,
width : u32,
height : u32,
) -> Status
{
let data_size = width * height * 4;
if data_size as usize > buffer_size {
return Status::UNSUPPORTED;
}
Status::SUCCESS
}
#[no_mangle]
#[export_name = "TestIntegerCheckedOverflow"]
pub extern fn test_integer_checked_overflow (
buffer_size: usize,
width : u32,
height : u32,
) -> Status
{
let mut data_size: u32 = 0;
match width.checked_mul(height) {
Some(size) => {data_size = size},
None => {return Status::INVALID_PARAMETER},
}
match data_size.checked_mul(4) {
Some(size) => {data_size = size},
None => {return Status::INVALID_PARAMETER},
}
if data_size as usize > buffer_size {
return Status::UNSUPPORTED;
}
Status::SUCCESS
}
#[no_mangle]
#[export_name = "TestIntegerCast"]
pub extern fn test_integer_cast (
buffer_size: u64,
) -> u32
{
let data_size : u32 = buffer_size as u32;
data_size
}
extern {
fn ExternInit(Data: *mut usize);
}
#[no_mangle]
#[export_name = "TestUninitializedVariable"]
pub extern fn test_uninitializd_variable (
index: usize,
) -> usize
{
let mut data : usize = 1;
if index > 10 {
data = 0;
}
unsafe { ExternInit (&mut data ); }
data = data + 1;
data
}
#[no_mangle]
#[export_name = "TestArrayOutOfRange"]
pub extern fn test_array_out_of_range (
index: usize,
) -> usize
{
let mut data : [u8; 8] = [0; 8];
data[index] = 1;
data[index + 1] as usize
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct TestTable {
pub r#type: u32,
pub length: u32,
pub value: [u8; 0],
}
#[no_mangle]
#[export_name = "TestBufferOverflow"]
pub extern fn test_buffer_overflow (
buffer: &mut [u8; 0],
buffer_size: usize,
table: &TestTable,
table_size: usize,
)
{
let mut dest = crate::mem::MemoryRegion::new(buffer as *mut [u8; 0] as usize as u64, buffer_size as u64);
let mut source = crate::mem::MemoryRegion::new(&table.value as *const [u8; 0] as usize as u64, table_size as u64);
for index in 0_u64 .. table.length as u64 {
dest.write_u8(index, source.read_u8(index));
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct TestTableFixed {
pub r#type: u32,
pub length: u32,
pub value: [u8; 64],
}
#[no_mangle]
#[export_name = "TestBufferOverflowFixed"]
pub extern fn test_buffer_overflow_fixed (
buffer: &mut [u8; 32],
table: &TestTableFixed,
)
{
(*buffer)[0_usize..(table.length as usize)].copy_from_slice(
&table.value[0_usize..(table.length as usize)]
);
}
fn get_buffer<'a> () -> Option<&'a mut TestTableFixed>
{
let ptr : *mut c_void = unsafe { AllocatePool (size_of::<TestTableFixed>()) };
if ptr.is_null() {
return None;
}
let buffer : &mut TestTableFixed = unsafe { core::mem::transmute::<*mut c_void, &mut TestTableFixed>(ptr) };
Some(buffer)
}
fn release_buffer (test_table : &mut TestTableFixed)
{
test_table.r#type = 0;
unsafe { FreePool (test_table as *mut TestTableFixed as *mut c_void) ; }
}
#[no_mangle]
#[export_name = "TestBufferDrop"]
pub extern fn test_buffer_drop (
)
{
match get_buffer () {
Some(buffer) => {
buffer.r#type = 1;
release_buffer(buffer);
drop (buffer); // This is required.
//buffer.r#type = 1; // error
},
None => {},
}
}
#[no_mangle]
#[export_name = "TestBufferBorrow"]
pub extern fn test_buffer_borrow (
test_table : &mut TestTableFixed
)
{
let test_table2 : &mut TestTableFixed = test_table;
test_table2.r#type = 1;
let test_table3 : &mut [u8; 64] = &mut test_table.value;
test_table3[63] = 0;
//test_table2.r#type = 2; // error
}
#[no_mangle]
#[export_name = "TestBufferAlloc"]
pub extern fn test_buffer_alloc (
)
{
let layout = unsafe { core::alloc::Layout::from_size_align_unchecked(32, 4) };
unsafe {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
let mut box_buffer = Box::from_raw(from_raw_parts_mut(buffer.ptr.as_ptr(), layout.size()));
box_buffer[0] = 1;
Global.dealloc (buffer.ptr, layout);
drop (buffer); // It is useless
box_buffer[0] = 1; // cannot catch
},
Err(_) => handle_alloc_error (layout),
}
}
let layout = core::alloc::Layout::new::<u32>();
unsafe {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
Global.dealloc (buffer.ptr, layout);
},
Err(_) => handle_alloc_error (layout),
}
}
}
fn get_box (
r#type: u32
) -> Box<TestTableFixed>
{
let mut a = Box::new(TestTableFixed{
r#type: 0,
length: size_of::<TestTableFixed>() as u32,
value: [0; 64]
}); // it will call __rust_alloc().
a.r#type = r#type;
a
}
#[no_mangle]
#[export_name = "TestBoxAlloc"]
pub extern fn test_box_alloc (
r#type: u32
) -> Box<TestTableFixed>
{
let mut a = get_box(1);
a.r#type = r#type;
//test_box_free(a); // build fail.
let b = a;
b
}
#[no_mangle]
#[export_name = "TestBoxFree"]
pub extern fn test_box_free (
buffer: Box<TestTableFixed>
)
{
// it will call __rust_dealloc()
}
#[no_mangle]
#[export_name = "TestBoxAllocFail"]
pub extern fn test_box_alloc_fail (
size: u32
) -> Box<[u8; 0x800]>
{
let mut a = Box::new([0_u8; 0x800]); // it will call __rust_alloc().
a
}
#[no_mangle]
#[export_name = "TestBoxConvert"]
pub extern fn test_box_convert (
size: usize
) -> *mut u8
{
let layout = unsafe { core::alloc::Layout::from_size_align_unchecked(size, 4) };
unsafe {
match Global.alloc (layout, AllocInit::Zeroed) {
Ok(buffer) => {
let mut box_buffer = Box::<u8>::from_raw(from_raw_parts_mut(buffer.ptr.as_ptr(), layout.size()) as *mut [u8] as *mut u8 );
Global.dealloc (buffer.ptr, layout);
*box_buffer = 1;
Box::<u8>::into_raw(box_buffer)
},
Err(_) => handle_alloc_error (layout),
}
}
}