| // Copyright 2018 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/developer/debug/zxdb/expr/resolve_array.h" | |
| #include <algorithm> | |
| #include <safemath/checked_math.h> | |
| #include "src/developer/debug/shared/logging/logging.h" | |
| #include "src/developer/debug/zxdb/common/err.h" | |
| #include "src/developer/debug/zxdb/expr/eval_context.h" | |
| #include "src/developer/debug/zxdb/expr/expr_value.h" | |
| #include "src/developer/debug/zxdb/expr/pretty_type.h" | |
| #include "src/developer/debug/zxdb/expr/pretty_type_manager.h" | |
| #include "src/developer/debug/zxdb/expr/resolve_ptr_ref.h" | |
| #include "src/developer/debug/zxdb/symbols/arch.h" | |
| #include "src/developer/debug/zxdb/symbols/array_type.h" | |
| #include "src/developer/debug/zxdb/symbols/collection.h" | |
| #include "src/developer/debug/zxdb/symbols/data_member.h" | |
| #include "src/developer/debug/zxdb/symbols/modified_type.h" | |
| #include "src/developer/debug/zxdb/symbols/symbol_data_provider.h" | |
| #include "src/developer/debug/zxdb/symbols/type.h" | |
| namespace zxdb { | |
| namespace { | |
| enum class ArrayKind { | |
| kError, | |
| kStatic, // int[4] | |
| kPointer, // int* | |
| }; | |
| struct ArrayInfo { | |
| ArrayKind kind = ArrayKind::kError; | |
| // Guaranteed for kind != kError. | |
| fxl::RefPtr<Type> original_value_type; // Use for error messages. | |
| fxl::RefPtr<Type> concrete_value_type; // Use to get the element count, etc. | |
| fxl::RefPtr<Type> erased_type; // Use to get the size of one element, if the type does | |
| // not match |concrete_value_type|. | |
| // Valid when kind == kStatic. | |
| fxl::RefPtr<ArrayType> static_type; | |
| }; | |
| // On success, the ArrayInfo will have kind != kError. | |
| ErrOr<ArrayInfo> ClassifyArray(const fxl::RefPtr<EvalContext>& eval_context, const ExprValue& array, | |
| const Type* erased_type) { | |
| if (!array.type()) | |
| return Err("No type information."); | |
| ArrayInfo info; | |
| info.erased_type = RefPtrTo(erased_type); | |
| fxl::RefPtr<Type> concrete = eval_context->GetConcreteType(array.type()); | |
| if (const ArrayType* array_type = concrete->As<ArrayType>()) { | |
| info.kind = ArrayKind::kStatic; | |
| info.static_type = RefPtrTo(array_type); | |
| info.original_value_type = RefPtrTo(array_type->value_type()); | |
| info.concrete_value_type = eval_context->GetConcreteType(info.original_value_type); | |
| if (!info.concrete_value_type) | |
| return Err("Bad type information for '%s'.", array.type()->GetFullName().c_str()); | |
| return info; | |
| } else if (const ModifiedType* modified_type = concrete->As<ModifiedType>()) { | |
| if (modified_type->tag() == DwarfTag::kPointerType) { | |
| info.kind = ArrayKind::kPointer; | |
| info.original_value_type = RefPtrTo(modified_type->modified().Get()->As<Type>()); | |
| info.concrete_value_type = eval_context->GetConcreteType(info.original_value_type); | |
| if (!info.concrete_value_type) | |
| return Err("Bad type information for '%s'.", array.type()->GetFullName().c_str()); | |
| return info; | |
| } | |
| } | |
| return Err("Not an array type."); | |
| } | |
| void ArrayFromPointer(const fxl::RefPtr<EvalContext>& eval_context, uint64_t begin_address, | |
| fxl::RefPtr<Type> element_type, size_t element_count, EvalCallback cb) { | |
| fxl::RefPtr<Type> concrete_element_type = eval_context->GetConcreteType(element_type); | |
| if (!concrete_element_type) | |
| return cb(Err("Bad type information.")); | |
| auto array_type = fxl::MakeRefCounted<ArrayType>(element_type, element_count); | |
| eval_context->GetDataProvider()->GetMemoryAsync( | |
| begin_address, array_type->byte_size(), | |
| [array_type, begin_address, cb = std::move(cb)](const Err& err, | |
| std::vector<uint8_t> data) mutable { | |
| if (err.has_error()) | |
| return cb(err); | |
| if (data.size() < array_type->byte_size()) | |
| return cb(Err("Array memory not valid.")); // A short read indicates invalid memory. | |
| FX_DCHECK(data.size() == array_type->byte_size()); | |
| cb(ExprValue(array_type, std::move(data), ExprValueSource(begin_address))); | |
| }); | |
| } | |
| // Handles the "int[4]" case. | |
| ErrOrValueVector ResolveStaticArray(const ExprValue& array, const ArrayInfo& info, | |
| size_t begin_index, size_t end_index) { | |
| if (array.data().size() < info.static_type->byte_size()) { | |
| return Err( | |
| "Array data (%zu bytes) is too small for the expected size " | |
| "(%u bytes).", | |
| array.data().size(), info.static_type->byte_size()); | |
| } | |
| if (end_index <= begin_index) | |
| return std::vector<ExprValue>(); | |
| uint32_t type_size = info.concrete_value_type->byte_size(); | |
| if (type_size == 0) | |
| return Err("Cannot resolve array of zero-sized elements."); | |
| std::vector<ExprValue> result; | |
| size_t max_count = std::min(end_index - begin_index, array.data().size() / type_size); | |
| result.reserve(max_count); | |
| for (size_t i = begin_index; i < end_index; i++) { | |
| size_t begin_offset = 0; | |
| size_t end_offset = 0; | |
| if (!safemath::CheckMul<size_t>(i, type_size).AssignIfValid(&begin_offset) || | |
| !safemath::CheckAdd<size_t>(begin_offset, type_size).AssignIfValid(&end_offset) || | |
| end_offset > array.data().size()) | |
| break; | |
| // Describe the source of this data. | |
| ExprValueSource source = array.source(); | |
| if (source.type() == ExprValueSource::Type::kMemory) { | |
| source = source.GetOffsetInto(static_cast<uint32_t>(begin_offset)); | |
| } else if (source.type() == ExprValueSource::Type::kRegister) { | |
| // Vector register, compute the bit shifts for this subset. This assumes little-endian | |
| // so we can compute the bit shifts to write to the register from the left. | |
| source = ExprValueSource(source.register_id(), type_size * 8, | |
| source.bit_shift() + static_cast<uint32_t>(begin_offset * 8)); | |
| } | |
| // else keep as original temporary/constant source. | |
| // Extract the array element data. | |
| std::optional<TaggedData> data = array.data().Extract(begin_offset, type_size); | |
| if (!data) | |
| return Err("Array data out of range."); | |
| result.emplace_back(info.original_value_type, std::move(*data), source); | |
| } | |
| return result; | |
| } | |
| // Fetches `count` elements of `type` starting at `begin_address`. | |
| void FetchPointerArrayElements(const fxl::RefPtr<EvalContext>& eval_context, | |
| TargetPointer begin_address, fxl::RefPtr<Type> type, size_t count, | |
| fit::callback<void(ErrOrValueVector)> cb) { | |
| if (count == 0) { | |
| cb(std::vector<ExprValue>()); | |
| return; | |
| } | |
| uint32_t type_size = type->byte_size(); | |
| if (type_size == 0) { | |
| cb(Err("Cannot resolve array of zero-sized elements.")); | |
| return; | |
| } | |
| uint32_t byte_span = 0; | |
| if (!safemath::CheckMul<uint64_t>(count, type_size).AssignIfValid(&byte_span)) { | |
| cb(Err("Array size is too large.")); | |
| return; | |
| } | |
| if (!safemath::CheckAdd<uint64_t>(begin_address, byte_span).IsValid()) { | |
| cb(Err("Array range overflows address space.")); | |
| return; | |
| } | |
| eval_context->GetDataProvider()->GetMemoryAsync( | |
| begin_address, byte_span, | |
| [begin_address, type = std::move(type), count, cb = std::move(cb)]( | |
| const Err& err, std::vector<uint8_t> data) mutable { | |
| if (err.has_error()) { | |
| cb(err); | |
| return; | |
| } | |
| // Convert returned raw memory to ExprValues. | |
| uint32_t type_size = type->byte_size(); | |
| if (type_size == 0) { | |
| cb(Err("Cannot resolve array of zero-sized elements.")); | |
| return; | |
| } | |
| size_t max_count = std::min(count, data.size() / type_size); | |
| std::vector<ExprValue> result; | |
| result.reserve(max_count); | |
| for (size_t i = 0; i < max_count; i++) { | |
| size_t begin_offset = i * static_cast<size_t>(type_size); | |
| std::vector<uint8_t> item_data(data.data() + begin_offset, | |
| data.data() + begin_offset + type_size); | |
| result.emplace_back(type, std::move(item_data), | |
| ExprValueSource(begin_address + begin_offset)); | |
| } | |
| cb(std::move(result)); | |
| }); | |
| } | |
| // Handles the "Foo*" case. | |
| void ResolvePointerArray(const fxl::RefPtr<EvalContext>& eval_context, const ExprValue& array, | |
| const ArrayInfo& info, size_t begin_index, size_t end_index, | |
| fit::callback<void(ErrOrValueVector)> cb) { | |
| // The address is stored in the contents of the array value. | |
| auto pointer_value_or = ExtractPointerValue(array); | |
| if (pointer_value_or.has_error()) { | |
| cb(pointer_value_or.err()); | |
| return; | |
| } | |
| TargetPointer base_address = pointer_value_or.value(); | |
| if (end_index <= begin_index) { | |
| cb(std::vector<ExprValue>()); | |
| return; | |
| } | |
| fxl::RefPtr<Type> type = info.erased_type ? info.erased_type : info.concrete_value_type; | |
| uint32_t type_size = type->byte_size(); | |
| if (type_size == 0) { | |
| cb(Err("Cannot resolve array of zero-sized elements.")); | |
| return; | |
| } | |
| TargetPointer begin_address = 0; | |
| if (!safemath::CheckAdd<uint64_t>(base_address, | |
| safemath::CheckMul<uint64_t>(begin_index, type_size)) | |
| .AssignIfValid(&begin_address)) { | |
| cb(std::vector<ExprValue>()); | |
| return; | |
| } | |
| FetchPointerArrayElements(eval_context, begin_address, std::move(type), end_index - begin_index, | |
| std::move(cb)); | |
| } | |
| // Handles single-item access on "Foo*". Signed offsets (passed as size_t from | |
| // ArrayAccessExprNode) are supported explicitly for negative indexing like ptr[-1]. | |
| void ResolvePointerArrayItem(const fxl::RefPtr<EvalContext>& eval_context, const ExprValue& array, | |
| const ArrayInfo& info, size_t index, | |
| fit::callback<void(ErrOrValueVector)> cb) { | |
| auto pointer_value_or = ExtractPointerValue(array); | |
| if (pointer_value_or.has_error()) { | |
| cb(pointer_value_or.err()); | |
| return; | |
| } | |
| TargetPointer base_address = pointer_value_or.value(); | |
| fxl::RefPtr<Type> type = info.erased_type ? info.erased_type : info.concrete_value_type; | |
| uint32_t type_size = type->byte_size(); | |
| if (type_size == 0) { | |
| cb(Err("Cannot resolve array of zero-sized elements.")); | |
| return; | |
| } | |
| int64_t signed_index = static_cast<int64_t>(index); | |
| TargetPointer element_address = 0; | |
| if (signed_index >= 0) { | |
| if (!safemath::CheckAdd<uint64_t>( | |
| base_address, | |
| safemath::CheckMul<uint64_t>(static_cast<uint64_t>(signed_index), type_size)) | |
| .AssignIfValid(&element_address)) { | |
| cb(std::vector<ExprValue>()); | |
| return; | |
| } | |
| } else { | |
| uint64_t abs_index = 0u - static_cast<uint64_t>(signed_index); | |
| if (!safemath::CheckSub<uint64_t>(base_address, | |
| safemath::CheckMul<uint64_t>(abs_index, type_size)) | |
| .AssignIfValid(&element_address)) { | |
| cb(std::vector<ExprValue>()); | |
| return; | |
| } | |
| } | |
| FetchPointerArrayElements(eval_context, element_address, std::move(type), /*count=*/1, | |
| std::move(cb)); | |
| } | |
| } // namespace | |
| void ResolveArray(const fxl::RefPtr<EvalContext>& eval_context, const Type* erased_type, | |
| const ExprValue& array, size_t begin_index, size_t end_index, | |
| fit::callback<void(ErrOrValueVector)> cb) { | |
| auto info_or = ClassifyArray(eval_context, array, erased_type); | |
| if (info_or.has_error()) { | |
| cb(info_or.err()); | |
| return; | |
| } | |
| const ArrayInfo& info = info_or.value(); | |
| switch (info.kind) { | |
| case ArrayKind::kStatic: | |
| cb(ResolveStaticArray(array, info, begin_index, end_index)); | |
| break; | |
| case ArrayKind::kPointer: | |
| ResolvePointerArray(eval_context, array, info, begin_index, end_index, std::move(cb)); | |
| break; | |
| default: | |
| FX_NOTREACHED(); | |
| break; | |
| } | |
| } | |
| void ResolveArrayItem(const fxl::RefPtr<EvalContext>& eval_context, const ExprValue& array, | |
| size_t index, EvalCallback cb) { | |
| auto info_or = ClassifyArray(eval_context, array, nullptr); | |
| if (info_or.ok()) { | |
| auto unwrap_single = [cb = std::move(cb)](ErrOrValueVector result) mutable { | |
| if (result.has_error()) { | |
| cb(result.err()); | |
| } else if (result.value().empty()) { | |
| // Short read. | |
| cb(Err("Invalid array index.")); | |
| } else { | |
| cb(std::move(result.value()[0])); // Should have only one value. | |
| } | |
| }; | |
| const ArrayInfo& info = info_or.value(); | |
| switch (info.kind) { | |
| case ArrayKind::kStatic: { | |
| size_t end_index = 0; | |
| if (!safemath::CheckAdd<size_t>(index, 1).AssignIfValid(&end_index)) { | |
| unwrap_single(std::vector<ExprValue>()); | |
| } else { | |
| unwrap_single(ResolveStaticArray(array, info, index, end_index)); | |
| } | |
| break; | |
| } | |
| case ArrayKind::kPointer: | |
| ResolvePointerArrayItem(eval_context, array, info, index, std::move(unwrap_single)); | |
| break; | |
| default: | |
| FX_NOTREACHED(); | |
| break; | |
| } | |
| } else { | |
| // Not an array, check for pretty types that support array access. | |
| if (const PrettyType* pretty = eval_context->GetPrettyTypeManager().GetForType(array.type())) { | |
| if (auto array_access = pretty->GetArrayAccess()) | |
| return array_access(eval_context, array, index, std::move(cb)); | |
| } | |
| cb(Err("Can't resolve an array access on type '%s'.", | |
| array.type() ? array.type()->GetFullName().c_str() : "<Unknown>")); | |
| } | |
| } | |
| void CoerceArraySize(const fxl::RefPtr<EvalContext>& eval_context, const ExprValue& array, | |
| size_t new_size, EvalCallback cb) { | |
| auto info_or = ClassifyArray(eval_context, array, nullptr); | |
| if (info_or.has_error()) | |
| return cb(info_or.err()); | |
| ArrayInfo& info = info_or.value(); | |
| switch (info.kind) { | |
| case ArrayKind::kError: | |
| FX_NOTREACHED(); // Errors should be caught above. | |
| break; | |
| case ArrayKind::kStatic: { | |
| if (info.static_type->num_elts() && new_size <= *info.static_type->num_elts()) { | |
| // Shrinking a static array, can just extract the subrange. | |
| auto new_array_type = fxl::MakeRefCounted<ArrayType>(info.original_value_type, new_size); | |
| auto extracted = array.data().Extract(0, new_array_type->byte_size()); | |
| if (!extracted) | |
| return cb(Err("Array contains less data than expected.")); | |
| cb(ExprValue(std::move(new_array_type), std::move(*extracted), array.source())); | |
| } else { | |
| // Expanding a static array. This requires the memory be re-fetched. | |
| if (array.source().type() != ExprValueSource::Type::kMemory) | |
| return cb(Err("Can not expand array that is not in memory.")); | |
| ArrayFromPointer(eval_context, array.source().address(), info.original_value_type, new_size, | |
| std::move(cb)); | |
| } | |
| break; | |
| } | |
| case ArrayKind::kPointer: { | |
| // Fetch the memory to convert to an array. | |
| auto pointer_value_or = ExtractPointerValue(array); | |
| if (pointer_value_or.has_error()) | |
| return cb(pointer_value_or.err()); | |
| fxl::RefPtr<Type> type = nullptr; | |
| if (info.erased_type) { | |
| type = info.erased_type; | |
| } else { | |
| type = info.original_value_type; | |
| } | |
| ArrayFromPointer(eval_context, pointer_value_or.value(), type, new_size, std::move(cb)); | |
| break; | |
| } | |
| } | |
| } | |
| } // namespace zxdb |