tresor: fix <initialize> config constraints

Until now, it was possible to use bad Free-Tree/VBD configurations with the
<initialize/> command. The tresor tester didn't complaining about it but the
tresor lib crashed or, worse, corrupted the tresor container. Now, the tresor
tester checks things, like for instance, that "nr_of_children" must be a power
of 2.

Ref #4971
This commit is contained in:
Martin Stein
2023-07-31 16:10:27 +02:00
committed by Christian Helmuth
parent b44ffc9361
commit f8332ce587

View File

@ -18,9 +18,13 @@
/* base includes */
#include <util/xml_node.h>
/* tresor includes */
#include <tresor/types.h>
namespace Tresor_init {
using namespace Genode;
using namespace Tresor;
class Configuration;
}
@ -36,6 +40,12 @@ class Tresor_init::Configuration
uint64_t _ft_nr_of_children { 0 };
uint64_t _ft_nr_of_leafs { 0 };
static bool _is_power_of_2(uint64_t val)
{
for (; val && (val & 1) == 0; val >>= 1);
return val == 1;
}
public:
struct Invalid : Exception { };
@ -62,15 +72,17 @@ class Tresor_init::Configuration
_ft_nr_of_leafs =
ft.attribute_value("nr_of_leafs", (uint64_t)0);
});
if (_vbd_nr_of_lvls == 0 ||
_vbd_nr_of_children == 0 ||
_vbd_nr_of_leafs == 0 ||
_ft_nr_of_lvls == 0 ||
_ft_nr_of_children == 0 ||
_ft_nr_of_leafs == 0)
{
throw Invalid();
}
ASSERT(_vbd_nr_of_lvls);
ASSERT(_vbd_nr_of_lvls <= TREE_MAX_NR_OF_LEVELS);
ASSERT(_vbd_nr_of_leafs);
ASSERT(_is_power_of_2(_vbd_nr_of_children));
ASSERT(_vbd_nr_of_children <= NR_OF_T1_NODES_PER_BLK);
ASSERT(_ft_nr_of_lvls);
ASSERT(_ft_nr_of_lvls <= TREE_MAX_NR_OF_LEVELS);
ASSERT(_ft_nr_of_leafs);
ASSERT(_is_power_of_2(_ft_nr_of_children));
ASSERT(_ft_nr_of_children <= NR_OF_T1_NODES_PER_BLK);
ASSERT(_ft_nr_of_children <= NR_OF_T2_NODES_PER_BLK);
}
Configuration (Configuration const &other)