51
51
#include " machine-memory-range-descr.h"
52
52
#include " machine-reg.h"
53
53
#include " machine-runtime-config.h"
54
- #include " os.h"
55
54
#include " plic-factory.h"
56
55
#include " pma-constants.h"
57
56
#include " pma-defines.h"
@@ -384,7 +383,7 @@ machine::machine(machine_config c, machine_runtime_config r) : m_c{std::move(c)}
384
383
}
385
384
// Auto detect flash drive image length
386
385
if (f.length == UINT64_C (-1 )) {
387
- auto fp = unique_fopen (f.image_filename .c_str (), " rb" );
386
+ auto fp = make_unique_fopen (f.image_filename .c_str (), " rb" );
388
387
if (fseek (fp.get (), 0 , SEEK_END) != 0 ) {
389
388
throw std::system_error{errno, std::generic_category (),
390
389
" unable to obtain length of image file '" s + f.image_filename + " ' when initializing " s +
@@ -524,11 +523,8 @@ machine::machine(machine_config c, machine_runtime_config r) : m_c{std::move(c)}
524
523
// Initialize TLB device.
525
524
// This must be done after all PMA entries are already registered, so we can lookup page addresses
526
525
if (!m_c.tlb .image_filename .empty ()) {
527
- unsigned char *buf = os_map_file (m_c.tlb .image_filename .c_str (), PMA_SHADOW_TLB_LENGTH, false );
528
- // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
529
- const auto &shadow_tlb = *reinterpret_cast <const shadow_tlb_state *>(buf);
530
- init_tlb (shadow_tlb);
531
- os_unmap_file (buf, PMA_SHADOW_TLB_LENGTH);
526
+ auto shadow_tlb = make_unique_mmap<shadow_tlb_state>(m_c.tlb .image_filename .c_str (), 1 , false /* not shared */ );
527
+ init_tlb (*shadow_tlb);
532
528
} else {
533
529
init_tlb ();
534
530
}
@@ -573,7 +569,7 @@ machine::machine(machine_config c, machine_runtime_config r) : m_c{std::move(c)}
573
569
574
570
static void load_hash (const std::string &dir, machine::hash_type &h) {
575
571
auto name = dir + " /hash" ;
576
- auto fp = unique_fopen (name.c_str (), " rb" );
572
+ auto fp = make_unique_fopen (name.c_str (), " rb" );
577
573
if (fread (h.data (), 1 , h.size (), fp.get ()) != h.size ()) {
578
574
throw std::runtime_error{" error reading from '" + name + " '" };
579
575
}
@@ -737,9 +733,9 @@ static void store_device_pma(const machine &m, const pma_entry &pma, const std::
737
733
if (!pma.get_istart_IO ()) {
738
734
throw std::runtime_error{" attempt to save non-device PMA" };
739
735
}
740
- auto scratch = unique_calloc <unsigned char >(PMA_PAGE_SIZE); // will throw if it fails
736
+ auto scratch = make_unique_calloc <unsigned char >(PMA_PAGE_SIZE); // will throw if it fails
741
737
auto name = machine_config::get_image_filename (dir, pma.get_start (), pma.get_length ());
742
- auto fp = unique_fopen (name.c_str (), " wb" );
738
+ auto fp = make_unique_fopen (name.c_str (), " wb" );
743
739
for (uint64_t page_start_in_range = 0 ; page_start_in_range < pma.get_length ();
744
740
page_start_in_range += PMA_PAGE_SIZE) {
745
741
const unsigned char *page_data = nullptr ;
@@ -762,7 +758,7 @@ static void store_memory_pma(const pma_entry &pma, const std::string &dir) {
762
758
throw std::runtime_error{" attempt to save non-memory PMA" };
763
759
}
764
760
auto name = machine_config::get_image_filename (dir, pma.get_start (), pma.get_length ());
765
- auto fp = unique_fopen (name.c_str (), " wb" );
761
+ auto fp = make_unique_fopen (name.c_str (), " wb" );
766
762
const pma_memory &mem = pma.get_memory ();
767
763
if (fwrite (mem.get_host_memory (), 1 , pma.get_length (), fp.get ()) != pma.get_length ()) {
768
764
throw std::runtime_error{" error writing to '" + name + " '" };
@@ -938,7 +934,7 @@ void machine::store_pmas(const machine_config &c, const std::string &dir) const
938
934
939
935
static void store_hash (const machine::hash_type &h, const std::string &dir) {
940
936
auto name = dir + " /hash" ;
941
- auto fp = unique_fopen (name.c_str (), " wb" );
937
+ auto fp = make_unique_fopen (name.c_str (), " wb" );
942
938
if (fwrite (h.data (), 1 , h.size (), fp.get ()) != h.size ()) {
943
939
throw std::runtime_error{" error writing to '" + name + " '" };
944
940
}
@@ -1820,7 +1816,7 @@ bool machine::verify_dirty_page_maps() const {
1820
1816
static_assert (PMA_PAGE_SIZE == machine_merkle_tree::get_page_size (),
1821
1817
" PMA and machine_merkle_tree page sizes must match" );
1822
1818
machine_merkle_tree::hasher_type h;
1823
- auto scratch = unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1819
+ auto scratch = make_unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1824
1820
if (!scratch) {
1825
1821
return false ;
1826
1822
}
@@ -1885,7 +1881,7 @@ bool machine::update_merkle_tree() const {
1885
1881
// runtime config or as the hardware supports.
1886
1882
const uint64_t n = get_task_concurrency (m_r.concurrency .update_merkle_tree );
1887
1883
const bool succeeded = os_parallel_for (n, [&](int j, const parallel_for_mutex &mutex) -> bool {
1888
- auto scratch = unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1884
+ auto scratch = make_unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1889
1885
if (!scratch) {
1890
1886
return false ;
1891
1887
}
@@ -1949,7 +1945,7 @@ bool machine::update_merkle_tree_page(uint64_t address) {
1949
1945
pma_entry &pma = find_pma_entry (m_merkle_pmas, address, sizeof (uint64_t ));
1950
1946
const uint64_t page_start_in_range = address - pma.get_start ();
1951
1947
machine_merkle_tree::hasher_type h;
1952
- auto scratch = unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1948
+ auto scratch = make_unique_calloc <unsigned char >(PMA_PAGE_SIZE, std::nothrow_t {});
1953
1949
if (!scratch) {
1954
1950
return false ;
1955
1951
}
@@ -1999,7 +1995,7 @@ machine::hash_type machine::get_merkle_tree_node_hash(uint64_t address, int log2
1999
1995
throw std::domain_error{" address not aligned to log2_size" };
2000
1996
}
2001
1997
const auto size = UINT64_C (1 ) << log2_size;
2002
- auto scratch = unique_calloc <unsigned char >(size);
1998
+ auto scratch = make_unique_calloc <unsigned char >(size);
2003
1999
read_memory (address, scratch.get (), size);
2004
2000
machine_merkle_tree::hasher_type h;
2005
2001
machine_merkle_tree::hash_type hash;
@@ -2064,7 +2060,7 @@ machine_merkle_tree::proof_type machine::get_proof(uint64_t address, int log2_si
2064
2060
if (log2_size < machine_merkle_tree::get_log2_page_size ()) {
2065
2061
const uint64_t length = UINT64_C (1 ) << log2_size;
2066
2062
const pma_entry &pma = find_pma_entry (m_merkle_pmas, address, length);
2067
- auto scratch = unique_calloc <unsigned char >(PMA_PAGE_SIZE);
2063
+ auto scratch = make_unique_calloc <unsigned char >(PMA_PAGE_SIZE);
2068
2064
const unsigned char *page_data = nullptr ;
2069
2065
// If the PMA range is empty, we know the desired range is
2070
2066
// entirely outside of any non-pristine PMA.
@@ -2491,16 +2487,15 @@ interpreter_break_reason machine::log_step(uint64_t mcycle_count, const std::str
2491
2487
interpreter_break_reason machine::verify_step (const hash_type &root_hash_before, const std::string &filename,
2492
2488
uint64_t mcycle_count, const hash_type &root_hash_after) {
2493
2489
auto data_length = os_get_file_length (filename.c_str (), " step log file" );
2494
- auto * data = os_map_file (filename.c_str (), data_length, false /* not shared */ );
2490
+ auto data = make_unique_mmap< unsigned char > (filename.c_str (), data_length, false /* not shared */ );
2495
2491
replay_step_state_access::context context;
2496
- replay_step_state_access a (context, data, data_length, root_hash_before);
2492
+ replay_step_state_access a (context, data. get () , data_length, root_hash_before);
2497
2493
uint64_t mcycle_end{};
2498
2494
if (__builtin_add_overflow (a.read_mcycle (), mcycle_count, &mcycle_end)) {
2499
2495
mcycle_end = UINT64_MAX;
2500
2496
}
2501
2497
auto break_reason = interpret (a, mcycle_end);
2502
2498
a.finish (root_hash_after);
2503
- os_unmap_file (data, data_length);
2504
2499
return break_reason;
2505
2500
}
2506
2501
0 commit comments