xref: /aosp_15_r20/bootable/libbootloader/gbl/libgbl/tests/nostd.rs (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1*5225e6b1SAndroid Build Coastguard Worker // Copyright 2023, The Android Open Source Project
2*5225e6b1SAndroid Build Coastguard Worker //
3*5225e6b1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*5225e6b1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*5225e6b1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*5225e6b1SAndroid Build Coastguard Worker //
7*5225e6b1SAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*5225e6b1SAndroid Build Coastguard Worker //
9*5225e6b1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*5225e6b1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*5225e6b1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5225e6b1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*5225e6b1SAndroid Build Coastguard Worker // limitations under the License.
14*5225e6b1SAndroid Build Coastguard Worker 
15*5225e6b1SAndroid Build Coastguard Worker //! This is a compilation-only test to ensure that libgbl can build against
16*5225e6b1SAndroid Build Coastguard Worker //! [no_std] code.
17*5225e6b1SAndroid Build Coastguard Worker //!
18*5225e6b1SAndroid Build Coastguard Worker //! This also provides a reference for the Rust hooks that a [no_std] user must
19*5225e6b1SAndroid Build Coastguard Worker //! provide in order to build against libgbl.
20*5225e6b1SAndroid Build Coastguard Worker 
21*5225e6b1SAndroid Build Coastguard Worker #![no_main]
22*5225e6b1SAndroid Build Coastguard Worker #![no_std]
23*5225e6b1SAndroid Build Coastguard Worker 
24*5225e6b1SAndroid Build Coastguard Worker // use core::panic::PanicInfo;
25*5225e6b1SAndroid Build Coastguard Worker 
26*5225e6b1SAndroid Build Coastguard Worker use gbl as _;
27*5225e6b1SAndroid Build Coastguard Worker 
28*5225e6b1SAndroid Build Coastguard Worker use buddy_system_allocator::LockedHeap;
29*5225e6b1SAndroid Build Coastguard Worker 
30*5225e6b1SAndroid Build Coastguard Worker // Providing allocator to satisfy AVB dependency
31*5225e6b1SAndroid Build Coastguard Worker #[global_allocator]
32*5225e6b1SAndroid Build Coastguard Worker static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();
33*5225e6b1SAndroid Build Coastguard Worker 
34*5225e6b1SAndroid Build Coastguard Worker static mut HEAP: [u8; 65536] = [0; 65536];
35*5225e6b1SAndroid Build Coastguard Worker 
36*5225e6b1SAndroid Build Coastguard Worker // #[panic_handler]
37*5225e6b1SAndroid Build Coastguard Worker // fn panic(_: &PanicInfo) -> ! {
38*5225e6b1SAndroid Build Coastguard Worker //     loop {}
39*5225e6b1SAndroid Build Coastguard Worker // }
40*5225e6b1SAndroid Build Coastguard Worker 
41*5225e6b1SAndroid Build Coastguard Worker /// main() entry point replacement required by [no_std].
42*5225e6b1SAndroid Build Coastguard Worker #[no_mangle]
main() -> !43*5225e6b1SAndroid Build Coastguard Worker pub fn main() -> ! {
44*5225e6b1SAndroid Build Coastguard Worker     // SAFETY: Safe because `HEAP` is only used here and `entry` is only called once.
45*5225e6b1SAndroid Build Coastguard Worker     unsafe {
46*5225e6b1SAndroid Build Coastguard Worker         // Give the allocator some memory to allocate.
47*5225e6b1SAndroid Build Coastguard Worker         HEAP_ALLOCATOR.lock().init(HEAP.as_mut_ptr() as usize, HEAP.len());
48*5225e6b1SAndroid Build Coastguard Worker     }
49*5225e6b1SAndroid Build Coastguard Worker 
50*5225e6b1SAndroid Build Coastguard Worker     panic!()
51*5225e6b1SAndroid Build Coastguard Worker }
52