|
| 1 | +#ifndef COTTON_BOX_HPP |
| 2 | +#define COTTON_BOX_HPP |
| 3 | +#include <string> |
| 4 | +#include <map> |
| 5 | +#include <functional> |
| 6 | +#include <cstdint> |
| 7 | +#include <stdexcept> |
| 8 | +#include <boost/serialization/export.hpp> |
| 9 | +#include <boost/archive/text_oarchive.hpp> |
| 10 | +#define REGISTER_SANDBOX(sbx) __attribute((constructor)) static void register_sandbox_ ## sbx() { \ |
| 11 | + box_creators[#sbx] = &create_sandbox<sbx>; \ |
| 12 | +} \ |
| 13 | +BOOST_CLASS_EXPORT(sbx) |
| 14 | + |
| 15 | + |
| 16 | +typedef uint64_t feature_mask_t; |
| 17 | +static const feature_mask_t memory_limit = 0x00000001; |
| 18 | +static const feature_mask_t time_limit = 0x00000002; |
| 19 | +static const feature_mask_t wall_time_limit = 0x00000004; |
| 20 | +static const feature_mask_t process_limit = 0x00000008; // Limits process creation |
| 21 | +static const feature_mask_t process_limit_full = 0x00000010; // Limits the number of processes |
| 22 | +static const feature_mask_t disk_limit = 0x00000020; // Works for non-malicious errors |
| 23 | +static const feature_mask_t disk_limit_full = 0x00000040; // Works for malicious attempts |
| 24 | +static const feature_mask_t folder_mount = 0x00000080; |
| 25 | +static const feature_mask_t memory_usage = 0x00000100; |
| 26 | +static const feature_mask_t running_time = 0x00000200; |
| 27 | +static const feature_mask_t wall_time = 0x00000400; |
| 28 | +static const feature_mask_t clear = 0x00000800; |
| 29 | + |
| 30 | +// Times should be milliseconds. |
| 31 | +// Space usages should be kilobytes |
| 32 | +class SandBox { |
| 33 | +public: |
| 34 | + SandBox(const std::string& base_path) {} |
| 35 | + virtual bool is_available() { |
| 36 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 37 | + } |
| 38 | + virtual int get_penality() { |
| 39 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 40 | + } |
| 41 | + virtual feature_mask_t get_features() { |
| 42 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 43 | + } |
| 44 | + virtual size_t create_box() { |
| 45 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 46 | + } |
| 47 | + virtual bool check() { |
| 48 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 49 | + } |
| 50 | + virtual bool set_memory_limit(size_t limit) { |
| 51 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 52 | + } |
| 53 | + virtual bool set_time_limit(size_t limit) { |
| 54 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 55 | + } |
| 56 | + virtual bool set_wall_time_limit(size_t limit) { |
| 57 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 58 | + } |
| 59 | + virtual bool set_process_limit(size_t limit) { |
| 60 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 61 | + } |
| 62 | + virtual bool set_disk_limit(size_t limit) { |
| 63 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 64 | + } |
| 65 | + virtual size_t get_memory_limit() { |
| 66 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 67 | + } |
| 68 | + virtual size_t get_time_limit() { |
| 69 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 70 | + } |
| 71 | + virtual size_t get_wall_time_limit() { |
| 72 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 73 | + } |
| 74 | + virtual size_t get_process_limit() { |
| 75 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 76 | + } |
| 77 | + virtual size_t get_disk_limit() { |
| 78 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 79 | + } |
| 80 | + virtual std::vector<std::pair<std::string, std::string>> mount() { |
| 81 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 82 | + } |
| 83 | + virtual std::string mount(const std::string& box_path) { |
| 84 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 85 | + } |
| 86 | + virtual bool mount(const std::string& box_path, const std::string& orig_path, bool rw = false) { |
| 87 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 88 | + } |
| 89 | + virtual bool umount(const std::string& box_path) { |
| 90 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 91 | + } |
| 92 | + virtual bool run(const std::string& command, const std::vector<std::string>& args) { |
| 93 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 94 | + } |
| 95 | + virtual size_t get_memory_usage() { |
| 96 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 97 | + } |
| 98 | + virtual size_t get_running_time() { |
| 99 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 100 | + } |
| 101 | + virtual size_t get_wall_time() { |
| 102 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 103 | + } |
| 104 | + virtual bool clear() { |
| 105 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 106 | + } |
| 107 | + virtual bool remove() { |
| 108 | + throw std::invalid_argument("This method is not implemented by this sandbox!"); |
| 109 | + } |
| 110 | + virtual ~SandBox() = 0; |
| 111 | +}; |
| 112 | + |
| 113 | +extern std::map<std::string, std::function<SandBox*(const std::string& base_path)>> box_creators; |
| 114 | +template<typename T> SandBox* create_sandbox(const std::string& base_path) {return new T(base_path);} |
| 115 | + |
| 116 | +#endif |
0 commit comments