summaryrefslogtreecommitdiff
path: root/plugins/efi_resolver/src/TypePropagation.cpp
blob: 68bc06f204d9c17ae1dc2fc2fd311d096ab7d3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "TypePropagation.h"
#include "highlevelilinstruction.h"

TypePropagation::TypePropagation(BinaryView* view)
{
	m_view = view;
	m_queue.clear();
	m_platform = view->GetDefaultPlatform();
}

const std::map<std::string, std::string> defaultName = {{"EFI_SYSTEM_TABLE", "gST"}, {"EFI_BOOT_SERVICES", "gBS"},
	{"EFI_RUNTIME_SERVICES", "gRT"}, {"EFI_MM_SYSTEM_TABLE", "gMmst"}, {"EFI_SMM_SYSTEM_TABLE2", "gSmmst"},
	{"EFI_HANDLE", "gHandle"}};

bool TypePropagation::propagateFuncParamTypes(Function* func)
{
	m_queue.push_back(func->GetStart());

	LogDebugF("Start Type propagation from {:#x}", func->GetStart());

	while (!m_queue.empty())
	{
		uint64_t addr = m_queue.front();
		m_queue.pop_front();

		Ref<Function> target_func = m_view->GetAnalysisFunction(m_platform, addr);
		auto params = target_func->GetType()->GetParameters();
		bool update = false;

		auto param_vars = target_func->GetParameterVariables().GetValue();
		for (auto var : param_vars)
		{
			bool propagate = false;
			auto var_type = target_func->GetVariableType(var).GetValue();

			if (var_type->IsPointer())
			{
				Ref<Type> target_type = var_type->GetChildType().GetValue();
				if (target_type->IsPointer() || target_type->IsNamedTypeRefer())
					propagate = true;
			}
			else if (var_type->IsNamedTypeRefer())
			{
				Ref<Type> target_type = m_view->GetTypeById(var_type->GetNamedTypeReference()->GetTypeId());
				if (target_type->IsPointer())
					propagate = true;
			}
			if (!propagate)
				continue;

			// Check whether the param is an aliased var. If it's an aliased var, it may not be directly used in the
			// function
			Ref<HighLevelILFunction> hlil_func_ssa = target_func->GetHighLevelIL()->GetSSAForm();
			std::set<Variable> aliased_vars = target_func->GetHighLevelILAliasedVariables();

			auto it = aliased_vars.find(var);
			if (it == aliased_vars.end())
			{
				// not an aliaed var, use version 0
				update |= propagateFuncParamTypes(target_func, SSAVariable(var, 0));
			}
			else
			{
				// this param is an aliased var, get the ssa_var
				auto uses = target_func->GetHighLevelIL()->GetVariableUses(var);
				for (auto use : uses)
				{
					auto hlil_instr = target_func->GetHighLevelIL()->GetExpr(use);
					hlil_instr = hlil_instr.GetParent();
					if (hlil_instr.operation != HLIL_VAR_INIT)
						continue;
					SSAVariable ssa_var = hlil_instr.GetSSAForm().GetDestSSAVariable();
					update |= propagateFuncParamTypes(target_func, ssa_var);
				}
			}
		}

		if (update)
			m_view->UpdateAnalysisAndWait();
	}
	return true;
}

bool TypePropagation::propagateFuncParamTypes(Function* func, SSAVariable ssa_var)
{
	bool update = false;
	auto mlil_func_ssa = func->GetMediumLevelIL()->GetSSAForm();
	auto uses = mlil_func_ssa->GetSSAVarUses(ssa_var);
	for (auto use : uses)
	{
		auto instr = mlil_func_ssa->GetInstruction(use);
		switch (instr.operation)
		{
		case MLIL_CALL_SSA:
		case MLIL_TAILCALL_SSA:
		{
			// propagate variable type to sub function
			auto dest = instr.GetDestExpr();
			if (!dest.GetValue().IsConstant())
				continue;
			Ref<Function> subfunc = m_view->GetAnalysisFunction(m_platform, dest.GetValue().value);

			if (!subfunc)
				continue;

			auto subfunc_type = subfunc->GetType();
			auto subfunc_params = subfunc->GetType()->GetParameters();

			auto instr_params = instr.GetParameterExprs();
			for (int i = 0; i < instr_params.size(); i++)
			{
				if (instr_params[i].operation != MLIL_VAR_SSA)
					continue;
				if (instr_params[i].GetSourceSSAVariable() != ssa_var)
					continue;
				if (i >= subfunc_params.size())
					break;
				auto ssa_var_type = func->GetVariableType(ssa_var.var).GetValue();
				auto typeName = GetOriginalTypeName(ssa_var_type);

				auto changeFuncType =
					[](BinaryView* bv, Ref<Type> funcType, std::string paramName, Ref<Type> paramType, int paramIdx) {
						auto newFuncType = TypeBuilder(funcType);
						auto adjustedParams = newFuncType.GetParameters();
						adjustedParams.at(paramIdx) = FunctionParameter(paramName, paramType);
						newFuncType.SetParameters(adjustedParams);
						return newFuncType.Finalize();
					};

				subfunc->SetUserType(
					changeFuncType(m_view, subfunc_type, GetVarNameForTypeStr(typeName), ssa_var_type, i));
				m_view->UpdateAnalysisAndWait();

				if (std::find(m_queue.begin(), m_queue.end(), subfunc->GetStart()) == m_queue.end())
					m_queue.push_back(subfunc->GetStart());
				update = true;
				break;
			}
			break;
		}

		case MLIL_STORE_SSA:
		{
			auto target = instr.GetDestExpr<MLIL_STORE_SSA>();
			if (!target.GetValue().IsConstant())
				continue;
			auto constant = target.GetValue().value;
			auto ssa_var_type = func->GetVariableType(ssa_var.var).GetValue();
			auto typeName = GetOriginalTypeName(ssa_var_type);

			auto it = defaultName.find(typeName);
			if (it != defaultName.end())
				typeName = it->second;

			m_view->DefineDataVariable(constant, ssa_var_type);
			m_view->DefineUserSymbol(new Symbol(DataSymbol, typeName, constant));

			update = true;
			break;
		}

		case MLIL_SET_VAR_SSA:
		{
			auto src = instr.GetSourceExpr<MLIL_SET_VAR_SSA>();
			auto dest = instr.GetDestSSAVariable<MLIL_SET_VAR_SSA>();

			auto dest_type = func->GetVariableType(dest.var);
			Confidence<Ref<Type>> src_type;
			switch (src.operation)
			{
			case MLIL_VAR_SSA:
				src_type = func->GetVariableType(src.GetSourceSSAVariable().var);
				break;

			case MLIL_LOAD_SSA:
			case MLIL_LOAD_STRUCT_SSA:
				src_type = src.GetType();
				break;

			default:
				continue;
			}

			if (src_type.GetValue() && src_type.GetValue() != dest_type.GetValue())
			{
				func->CreateUserVariable(dest.var, src_type, func->GetVariableName(dest.var));
				update |= propagateFuncParamTypes(func, SSAVariable(dest.var, dest.version));
			}
			break;
		}

		default:
			LogInfoF("Not handled case during type propagation. At {:#x}: {}", instr.address, instr.operation);
			break;
		}
	}
	return update;
}