-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added program factory skeleton implementation (#3)
Implemented Program Factories, including a concrete implementation for Random Program generation, docs, tests, and coverage.
- Loading branch information
1 parent
1196091
commit d4c5a6d
Showing
15 changed files
with
673 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Program Factories | ||
================= | ||
|
||
To generate Program instances based on certain criteria rather than programming them manually every | ||
time, program factories are introduced. They allow to generate ready-made Program instances with a | ||
straight forward interface, requiring only the expected size of the program, and the runtime | ||
environment parameters the programs are expected to be run under (variable memory size, string table | ||
size, string table item length limit). The generic interface for these factories is governed by a | ||
:ref:`Program Factory Base` class, while concrete implementations may offer more (optional) | ||
customization options beyond it, steering the generation process towards specific requirements. | ||
|
||
In this document, the :ref:`Program Factory Base` as well as the :ref:`Random Program Factory` | ||
classes are introduced. The latter generates programs randomly, but in a valid way. More factories | ||
may be added in the future on a per-need-basis. | ||
|
||
|
||
Program Factory Base | ||
-------------------- | ||
|
||
This is the base class for all program factories. It dictates a simple interface requiring the size | ||
of the expected programs to be generated, and the runtime environment they are expected to be | ||
executed under. The central call for all factories is the `generate()` function, which returns the | ||
generated Program instances. | ||
|
||
.. doxygenclass:: beast::ProgramFactoryBase | ||
:members: | ||
|
||
|
||
Random Program Factory | ||
---------------------- | ||
|
||
This concrete implementation of the :ref:`Program Factory Base` interface class generated random | ||
programs. These programs make use of the full scope of operators available in the BEAST library. All | ||
Program instances generated by this factory adhere to proper OpCodes (no invalid OpCodes are used), | ||
and correctly align their parameters, bit-wise and semantically. This means that per operator, all | ||
parameters are in the correct position and have reasonable values based on the environment passed to | ||
the `generate()` function: | ||
|
||
* Variables are only declared/used in the range of the passed in variable memory space | ||
* String table entries are only read/written in the range of the passed in string table size | ||
* String table entry content is only generated with valid lengths (although still random) | ||
|
||
Also, expected parameter ranges are adhered to (operands are distinguished between `int32_t`, | ||
`int8_t`, and `bool` accordingly). Program instances generated by this class likely don't make sense | ||
logically, but semantically they are correct. These programs can be used as seed material for random | ||
recombination, ensuring that at least the semantic layer is close to what is executable within the | ||
BEAST environment. | ||
|
||
Future plans for this class include adding an option to exclude specific operators from the | ||
generation process. | ||
|
||
.. doxygenclass:: beast::RandomProgramFactory | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef BEAST_PROGRAM_FACTORY_BASE_HPP_ | ||
#define BEAST_PROGRAM_FACTORY_BASE_HPP_ | ||
|
||
// Internal | ||
#include <beast/program.hpp> | ||
|
||
namespace beast { | ||
|
||
/** | ||
* @class ProgramFactoryBase | ||
* @brief Base class for program factories | ||
* | ||
* Program factories generate programs based on high level parameters and criteria. A concrete | ||
* implementation of a factory can have a multitude of parameterization options that define the | ||
* characteristics of the generated programs. | ||
*/ | ||
class ProgramFactoryBase { | ||
public: | ||
/** | ||
* @fn ProgramFactoryBase::~ProgramFactoryBase | ||
* @brief Default virtual destructor | ||
* | ||
* Ensures vtable consistency. | ||
*/ | ||
virtual ~ProgramFactoryBase() = default; | ||
|
||
/** | ||
* @fn ProgramFactoryBase::generate | ||
* @brief Generates a program based on the factory's semantics | ||
* | ||
* Generates a Program instance that follows the factory's concrete implementation's | ||
* characteristics. The given parameters that define a runtime-parameterization are used to make | ||
* an educated guess on how to make the generated program executable. For example, only variables | ||
* may be declared or used whose indices lie within the expected memory size to avoid illogical | ||
* program traits. The concrete use of these parameters, if any, depends on the respective | ||
* implementation of the factory though. | ||
* | ||
* @param size The maximum size of the program to generate, in bytes | ||
* @param memory_size The memory size the generated program would be executed with | ||
* @param string_table_size The string table size the generated program would be executed with | ||
* @param string_table_item_length The string table item length the generated program would be | ||
* executed with | ||
* @return The generated Program instance | ||
*/ | ||
virtual Program generate( | ||
uint32_t size, uint32_t memory_size, uint32_t string_table_size, | ||
uint32_t string_table_item_length) = 0; | ||
}; | ||
|
||
} // namespace beast | ||
|
||
#endif // BEAST_PROGRAM_FACTORY_BASE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef BEAST_RANDOM_PROGRAM_FACTORY_HPP_ | ||
#define BEAST_RANDOM_PROGRAM_FACTORY_HPP_ | ||
|
||
// Internal | ||
#include <beast/program_factory_base.hpp> | ||
|
||
namespace beast { | ||
|
||
/** | ||
* @class RandomProgramFactory | ||
* @brief Generates random programs with a valid structure | ||
* | ||
* This implementation of the abstract ProgramFactoryBase class can be used to generate Program | ||
* instances that have a random, albeit valid structure. Only valid operators are used in the | ||
* respective programs, the operators are aligned correctly, and variable indices, string table | ||
* indices, and string lengths are all within bounds of the passed in runtime environment | ||
* parameters. | ||
*/ | ||
class RandomProgramFactory : public ProgramFactoryBase { | ||
public: | ||
/** | ||
* @fn RandomProgramFactory::generate | ||
* @brief Generates a program consisting of random but valid operators and operands | ||
* | ||
* The programs generated by this function make use of the full operator set supported by the | ||
* BEAST library. | ||
* | ||
* @param size The maximum size of the program to generate, in bytes | ||
* @param memory_size The memory size the generated program would be executed with | ||
* @param string_table_size The string table size the generated program would be executed with | ||
* @param string_table_item_length The string table item length the generated program would be | ||
* executed with | ||
* @return A randomly generated, but valid program | ||
*/ | ||
Program generate( | ||
uint32_t size, uint32_t memory_size, uint32_t string_table_size, | ||
uint32_t string_table_item_length) override; | ||
}; | ||
|
||
} // namespace beast | ||
|
||
#endif // BEAST_RANDOM_PROGRAM_FACTORY_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.