1 //: Global variables.
  2 //:
  3 //: Global variables are just labels in the data segment.
  4 //: However, they can only be used in imm32 and not disp32 operands. And they
  5 //: can't be used with jump and call instructions.
  6 //:
  7 //: This layer much the same structure as rewriting labels.
  8 
  9 :(scenario global_variable)
 10 % Mem_offset = CODE_START;
 11 % Mem.resize(0x2000);
 12 == code
 13 b9/copy x/imm32  # copy to ECX
 14 == data
 15 x:
 16 00 00 00 00
 17 +transform: global variable 'x' is at address 0x08049079
 18 
 19 :(before "End Level-2 Transforms")
 20 Transform.push_back(rewrite_global_variables);
 21 :(code)
 22 void rewrite_global_variables(program& p) {
 23   trace(99, "transform") << "-- rewrite global variables" << end();
 24   map<string, uint32_t> address;
 25   compute_addresses_for_global_variables(p, address);
 26   if (trace_contains_errors()) return;
 27   drop_global_variables(p);
 28   replace_global_variables_with_addresses(p, address);
 29 }
 30 
 31 void compute_addresses_for_global_variables(const program& p, map<string, uint32_t>& address) {
 32   for (int i = /*skip code segment*/1;  i < SIZE(p.segments);  ++i)
 33     compute_addresses_for_global_variables(p.segments.at(i), address);
 34 }
 35 
 36 void compute_addresses_for_global_variables(const segment& s, map<string, uint32_t>& address) {
 37   int current_address = s.start;
 38   for (int i = 0;  i < SIZE(s.lines);  ++i) {
 39     const line& inst = s.lines.at(i);
 40     for (int j = 0;  j < SIZE(inst.words);  ++j) {
 41       const word& curr = inst.words.at(j);
 42       if (*curr.data.rbegin() != ':') {
 43         ++current_address;
 44       }
 45       else {
 46         string variable = drop_last(curr.data);
 47         // ensure variables look sufficiently different from raw hex
 48         check_valid_name(variable);
 49         if (trace_contains_errors()) return;
 50         if (j > 0)
 51           raise << "'" << to_string(inst) << "': global variable names can only be the first word in a line.\n" << end();
 52         put(address, variable, current_address);
 53         trace(99, "transform") << "global variable '" << variable << "' is at address 0x" << HEXWORD << current_address << end();
 54         // no modifying current_address; global variable definitions won't be in the final binary
 55       }
 56     }
 57   }
 58 }
 59 
 60 void drop_global_variables(program& p) {
 61   for (int i = /*skip code segment*/1;  i < SIZE(p.segments);  ++i)
 62     drop_labels(p.segments.at(i));
 63 }
 64 
 65 void replace_global_variables_with_addresses(program& p, const map<string, uint32_t>& address) {
 66   if (p.segments.empty()) return;
 67   segment& code = p.segments.at(0);
 68   for (int i = 0;  i < SIZE(code.lines);  ++i) {
 69     line& inst = code.lines.at(i);
 70     line new_inst;
 71     for (int j = 0;  j < SIZE(inst.words);  ++j) {
 72       const word& curr = inst.words.at(j);
 73       if (!contains_key(address, curr.data)) {
 74         new_inst.words.push_back(curr);
 75         continue;
 76       }
 77       if (!valid_use_of_global_variable(curr)) {
 78         raise << "'" << to_string(inst) << "': can't refer to global variable '" << curr.data << "'\n" << end();
 79         return;
 80       }
 81       emit_hex_bytes(new_inst, get(address, curr.data), 4);
 82     }
 83     inst.words.swap(new_inst.words);
 84     trace(99, "transform") << "instruction after transform: '" << data_to_string(inst) << "'" << end();
 85   }
 86 }
 87 
 88 bool valid_use_of_global_variable(const word& curr) {
 89   if (has_operand_metadata(curr, "imm32")) return true;
 90   // End Valid Uses Of Global Variable(curr)
 91   return false;
 92 }
 93 
 94 //:: a more complex sanity check for how we use global variables
 95 //: requires first saving some data early before we pack operands
 96 
 97 :(after "Begin Level-2 Transforms")
 98 Transform.push_back(correlate_disp32_with_mod);
 99 :(code)
100 void correlate_disp32_with_mod(program& p) {
101   if (p.segments.empty()) return;
102   segment& code = p.segments.at(0);
103   for (int i = 0;  i < SIZE(code.lines);  ++i) {
104     line& inst = code.lines.at(i);
105     for (int j = 0;  j < SIZE(inst.words);  ++j) {
106       word& curr = inst.words.at(j);
107       if (has_operand_metadata(curr, "disp32")
108           && has_operand_metadata(inst, "mod"))
109         curr.metadata.push_back("has_mod");
110     }
111   }
112 }
113 
114 :(before "End Valid Uses Of Global Variable(curr)")
115 if (has_operand_metadata(curr, "disp32"))
116   return has_metadata(curr, "has_mod");
117 // todo: more sophisticated check, to ensure we don't use global variable
118 // addresses as a real displacement added to other operands.
119 
120 :(code)
121 bool has_metadata(const word& w, const string& m) {
122   for (int i = 0;  i < SIZE(w.metadata);  ++i)
123     if (w.metadata.at(i) == m) return true;
124   return false;
125 }
126 
127 :(scenario global_variable_disallowed_in_jump)
128 % Hide_errors = true;
129 == code
130 eb/jump x/disp8
131 == data
132 x:
133 00 00 00 00
134 +error: 'eb/jump x/disp8': can't refer to global variable 'x'
135 # sub-optimal error message; should be
136 #? +error: can't jump to data (variable 'x')
137 
138 :(scenario global_variable_disallowed_in_call)
139 % Hide_errors = true;
140 == code
141 e8/call x/disp32
142 == data
143 x:
144 00 00 00 00
145 +error: 'e8/call x/disp32': can't refer to global variable 'x'
146 # sub-optimal error message; should be
147 #? +error: can't call to the data segment ('x')
148 
149 :(scenario disp32_data_with_modrm)
150 % Mem_offset = CODE_START;
151 % Mem.resize(0x2000);
152 == code
153 8b/copy 0/mod/indirect 5/rm32/.disp32 2/r32/EDX x/disp32
154 ==
155 x:
156 00 00 00 00
157 $error: 0
158 
159 :(scenarios transform)
160 :(scenario disp32_data_with_call)
161 == code
162 foo:
163 e8/call bar/disp32
164 bar:
165 $error: 0
166 
167 :(code)
168 string to_full_string(const line& in) {
169   ostringstream out;
170   for (int i = 0;  i < SIZE(in.words);  ++i) {
171     if (i > 0) out << ' ';
172     out << in.words.at(i).data;
173     for (int j = 0;  j < SIZE(in.words.at(i).metadata);  ++j)
174       out << '/' << in.words.at(i).metadata.at(j);
175   }
176   return out.str();
177 }