AERA
segments.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef segments_h
86 #define segments_h
87 
88 #include <unordered_map>
89 #include "../r_code/object.h"
90 #include "../r_code/list.h"
91 
92 #include "class.h"
93 
94 namespace r_comp {
95 
96 class Reference {
97 public:
98  Reference();
99  Reference(const uint16 i, const Class &c, const Class &cc);
100  uint16 index_;
101  Class class_;
102  Class cast_class_;
103 };
104 
105 // All classes below map components of r_code::Image into r_comp::Image.
106 // Both images are equivalent, the latter being easier to work with (uses vectors instead of a contiguous structure, that is r_code::Image::data).
107 // All read(word32*,uint32)/write(word32*) functions defined in the classes below perfom read/write operations in an r_code::Image::data.
108 
109 class dll_export Metadata {
110 private:
111  uint32 get_class_array_size();
112  uint32 get_classes_size();
113  uint32 get_sys_classes_size();
114  uint32 get_class_names_size();
115  uint32 get_operator_names_size();
116  uint32 get_function_names_size();
117 public:
118  Metadata();
119 
120  std::unordered_map<std::string, Class> classes_; // non-sys classes, operators and device functions.
121  std::unordered_map<std::string, Class> sys_classes_;
122 
123  r_code::resized_vector<std::string> class_names_; // classes and sys-classes; does not include set classes.
124  r_code::resized_vector<std::string> operator_names_;
125  r_code::resized_vector<std::string> function_names_;
126  r_code::resized_vector<Class> classes_by_opcodes_; // classes indexed by opcodes; used to retrieve member names; registers all classes (incl. set classes).
127  r_code::resized_vector<uint16> usr_classes_;
128 
129  Class *get_class(std::string &class_name);
130  Class *get_class(uint16 opcode);
131 
132  void write(word32 *data);
133  void read(word32 *data, uint32 size);
134  uint32 get_size();
135 };
136 
137 class dll_export ObjectMap {
138 public:
140 
141  void shift(uint16 offset);
142 
143  void write(word32 *data);
144  void read(word32 *data, uint32 size);
145  uint32 get_size() const;
146 };
147 
148 class dll_export CodeSegment {
149 public:
151 
152  ~CodeSegment();
153 
154  void write(word32 *data);
155  void read(word32 *data, uint16 object_count);
156  uint32 get_size();
157 };
158 
159 class dll_export ObjectNames {
160 public:
161  std::unordered_map<uint32, std::string> symbols_; // indexed by objects' OIDs.
162 
163  ~ObjectNames();
164 
165  void write(word32 *data);
166  void read(word32 *data);
167  uint32 get_size();
168 
174  uint32 findSymbol(const std::string& name);
175 };
176 
177 class dll_export Image {
178 private:
179  uint32 map_offset_;
180  std::unordered_map<r_code::Code *, uint16> ptrs_to_indices_; // used for injection in memory.
181 
182  void add_object(r_code::Code *object, bool include_invalidated);
183  r_code::SysObject *add_object(r_code::Code *object, std::vector<r_code::SysObject *> &imported_objects);
184  uint32 get_reference_count(const r_code::Code *object) const;
185  void build_references();
186  void build_references(r_code::SysObject *sys_object, r_code::Code *object);
187  void unpack_objects(r_code::resized_vector<r_code::Code *> &ram_objects);
188 public:
189  ObjectMap object_map_;
190  CodeSegment code_segment_;
191  ObjectNames object_names_;
192 
193  Timestamp timestamp_;
194 
195  Image();
196  ~Image();
197 
198  void add_sys_object(r_code::SysObject *object, std::string name); // called by the compiler.
199  void add_sys_object(r_code::SysObject *object); // called by add_object().
200 
201  void get_objects(r_code::Mem *mem, r_code::resized_vector<r_code::Code *> &ram_objects);
202  template<class O> void get_objects(r_code::resized_vector<r_code::Code *> &ram_objects) {
203 
204  for (uint32 i = 0; i < code_segment_.objects_.size(); ++i) {
205 
206  uint16 opcode = code_segment_.objects_[i]->code_[0].asOpcode();
207  ram_objects[i] = new O(code_segment_.objects_[i]);
208  }
209  unpack_objects(ram_objects);
210  }
211 
218  void add_objects(r_code::list<P<r_code::Code> > &objects, bool include_invalidated = false);
219 
220  void add_objects(r_code::list<P<r_code::Code> > &objects, std::vector<r_code::SysObject *> &imported_objects); // called by any r_exec code for decompiling on the fly.
221 
222  template<class I> I *serialize() {
223 
224  I *image = (I *)I::Build(timestamp_, object_map_.get_size(), code_segment_.get_size(), object_names_.get_size());
225 
226  object_map_.shift(image->map_size());
227  object_map_.write(image->data());
228  code_segment_.write(image->data() + image->map_size());
229  object_names_.write(image->data() + image->map_size() + image->code_size());
230 
231  return image;
232  }
233 
234  template<class I> void load(I *image) {
235 
236  timestamp_ = image->timestamp();
237  object_map_.read(image->data(), image->map_size());
238  code_segment_.read(image->data() + image->map_size(), image->map_size());
239  object_names_.read(image->data() + image->map_size() + image->code_size());
240  }
241 };
242 }
243 
244 
245 #endif
r_code::resized_vector< std::string >
r_code::SysObject
Definition: r_code/object.h:136
core::P< r_code::Code >
r_code::Code
Definition: r_code/object.h:224
r_comp::CodeSegment
Definition: segments.h:148
r_comp::ObjectMap
Definition: segments.h:137
r_comp::ObjectNames
Definition: segments.h:159
r_comp::Image
Definition: segments.h:177
r_code::Mem
Definition: r_code/object.h:420
r_code::list
Definition: list.h:99
r_comp::Reference
Definition: segments.h:96
r_comp::Metadata
Definition: segments.h:109
r_comp::Class
Definition: class.h:96