History

DisVM will now only use 0 initialized memory. - The alloc_memory function will only return 0 initialize memory - calloc_memory was removed

Floating point division is now relying on underlying FPU and IEEE for behavior with respect to divide by 0

arr committed 9 years ago

Commit b92328e1 parent bd1f49311 files · +48 −112Browse files

Showing the changes to src/vm/vm_memory.cpp (1 of 11 files). Show all

Unified · Split

src/vm/vm_memory.cpp+7−55
@@ -106,7 +106,7 @@ void *disvm::runtime::alloc_memory(std::size_t amount_in_bytes) · 105 unmodified lines
106106 {
107107 safe_alloc s;
108108
109− auto memory = std::malloc(amount_in_bytes);
109+ auto memory = std::calloc(amount_in_bytes, sizeof(byte_t));
110110 if (memory == nullptr)
111111 throw vm_system_exception{ "Out of memory" };
112112
@@ -116,20 +116,6 @@ void *disvm::runtime::alloc_memory(std::size_t amount_in_bytes) · 3 unmodified lines
116116 return memory;
117117 }
118118
119−void *disvm::runtime::calloc_memory(std::size_t amount_in_bytes)
120−{
121− safe_alloc s;
122−
123− auto memory = std::calloc(amount_in_bytes, sizeof(uint8_t));
124− if (memory == nullptr)
125− throw vm_system_exception{ "Out of memory" };
126−
127− if (debug::is_component_tracing_enabled<debug::component_trace_t::memory>())
128− debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "alloc: zeroed: %#" PRIxPTR " %d", memory, amount_in_bytes);
129−
130− return memory;
131−}
132−
133119 void disvm::runtime::free_memory(void *memory)
134120 {
135121 if (memory == nullptr)
@@ -147,27 +133,12 @@ void disvm::runtime::init_memory(const type_descriptor_t &type_desc, void *data) · 11 unmodified lines
147133 if (type_desc.size_in_bytes == 0)
148134 return;
149135
150− debug::assign_debug_memory(data, type_desc.size_in_bytes);
151−
152− auto memory = reinterpret_cast<word_t *>(data);
153− for (auto i = word_t{ 0 }; i < type_desc.map_in_bytes; ++i, memory += 8)
154− {
155− const auto words8 = type_desc.pointer_map[i];
156− if (words8 != 0)
157− {
158− const auto flags = std::bitset<sizeof(words8) * 8>{ words8 };
159−
160− // Enumerating the flags in reverse order so memory access is linear.
161− if (flags[7]) memory[0] = runtime_constants::nil;
162− if (flags[6]) memory[1] = runtime_constants::nil;
163− if (flags[5]) memory[2] = runtime_constants::nil;
164− if (flags[4]) memory[3] = runtime_constants::nil;
165− if (flags[3]) memory[4] = runtime_constants::nil;
166− if (flags[2]) memory[5] = runtime_constants::nil;
167− if (flags[1]) memory[6] = runtime_constants::nil;
168− if (flags[0]) memory[7] = runtime_constants::nil;
169− }
170− }
136+ // [SPEC] The Dis VM spec does not guarantee non-pointer offsets
137+ // to be initialized to zero, but it was found to be an assumption
138+ // when getting the limbo compiler run on this VM. Therefore this
139+ // implemetation will make the strong guarantee that memory will
140+ // always be 0 initialized.
141+ std::memset(data, 0, type_desc.size_in_bytes);
171142 }
172143
173144 namespace
@@ -210,8 +181,6 @@ void disvm::runtime::dec_ref_count_and_free(vm_alloc_t *alloc) · 36 unmodified lines
210181 delete alloc;
211182 }
212183
213−const zero_memory_t vm_alloc_t::zero_memory = {};
214−
215184 void *vm_alloc_t::operator new(std::size_t sz)
216185 {
217186 return alloc_memory(sz);
@@ -231,27 +200,10 @@ vm_alloc_t *vm_alloc_t::allocate(std::shared_ptr<const type_descriptor_t> td) · 13 unmodified lines
231200 auto mem = alloc_memory(sizeof(vm_alloc_t) + td->size_in_bytes);
232201 auto alloc = ::new(mem)vm_alloc_t{ td };
233202
234− // Initialize pointer types
235− init_memory(*td, alloc->get_allocation());
236−
237203 debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "init: vm alloc: %d", td->size_in_bytes);
238204 return alloc;
239205 }
240206
241−vm_alloc_t *vm_alloc_t::allocate(std::shared_ptr<const type_descriptor_t> td, const zero_memory_t &)
242−{
243− assert(td != nullptr);
244− if (td->size_in_bytes <= 0)
245− throw vm_system_exception{ "Invalid dynamic memory allocation size" };
246−
247− // No need to initialize memory since it is already zeroed out.
248− auto mem = calloc_memory(sizeof(vm_alloc_t) + td->size_in_bytes);
249− auto alloc = ::new(mem)vm_alloc_t{ td };
250−
251− debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "initz: vm alloc: %d", td->size_in_bytes);
252− return alloc;
253−}
254−
255207 vm_alloc_t *vm_alloc_t::copy(const vm_alloc_t &other)
256208 {
257209 auto alloc_copy = vm_alloc_t::allocate(other.alloc_type);