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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
fn try_lower_member_builtin(
func: &Expression,
args: &[Expression],
ctx: &mut LoweringContext,
instructions: &mut Vec<Instruction>,
) -> Option<bool> {
let Expression::MemberAccess(_, inner, member) = func else {
return None;
};
let Expression::Variable(base) = inner.as_ref() else {
return None;
};
// Task #65 — `abi.encodeCall(Target.foo, (args))` resolves the function
// pointer to its 4-byte selector via the type-method selector registry
// (same mechanism Task #54 wired up for `this.method.selector`). The
// resulting calldata mirrors `abi.encodeWithSelector(selector, args)`:
// `selector ++ abi.encode(args)`.
//
// Task #44 (this re-apply) complements the selector by routing the
// trailing args through the EVM-canonical `abiEncode` handler; together
// they produce the 36-byte payload the fuzz harness
// `abi_encode_call_same_bug_as_encode` pins.
//
// Task #106 — canonicalise struct params into their EVM tuple form
// (`(field1,field2,...)`) both in the selector signature AND in the
// encoded payload (by expanding each struct arg into per-field 32-byte
// slots). Without this, `abi.encodeCall(this.f, (p))` where
// `p = P{uint256 a; bool b}` under-sizes to 36 bytes (selector+BE(p.a))
// and computes the wrong selector.
if base.name == "abi" && member.name == "encodeCall" {
if args.len() != 2 {
ctx.record_error(
"abi.encodeCall requires a function selector and a tuple argument list",
);
return Some(false);
}
let selector_bytes = match &args[0] {
Expression::MemberAccess(_, target_inner, target_method) => {
// Task #106 — also recognise `this.method` by resolving
// against the current contract's method table (mirrors the
// `.selector` path in member_access/selectors.rs). Without
// this, `abi.encodeCall(this.f, ...)` falls through every
// contract-type check and lands in the `keccak256(name())`
// fallback — producing the parameterless-method selector
// regardless of the real signature.
let type_name = match target_inner.as_ref() {
Expression::Variable(type_id) if ctx.is_contract_type_name(&type_id.name) => {
Some(type_id.name.clone())
}
Expression::Variable(type_id) if type_id.name == "this" => {
Some(ctx.current_contract_name().to_string())
}
Expression::MemberAccess(_, namespace_expr, type_id)
if matches!(
namespace_expr.as_ref(),
Expression::Variable(namespace_id)
if !ctx.param_index_map.contains_key(&namespace_id.name)
&& ctx.resolve_local(&namespace_id.name).is_none()
&& !ctx.state_index_map.contains_key(&namespace_id.name)
&& !ctx.is_contract_type_name(&namespace_id.name)
) && ctx.is_contract_type_name(&type_id.name) =>
{
Some(type_id.name.clone())
}
_ => None,
};
let resolved = type_name
.and_then(|name| {
ctx.type_method_selectors(&name, &target_method.name)
.and_then(|selectors| {
if selectors.len() == 1 {
Some(selectors[0].to_vec())
} else {
None
}
})
})
.unwrap_or_else(|| {
// Fallback: keccak256(methodName()) matches the
// Ethereum-spec convention when the method has no
// params and is the default for unresolved refs.
let mut hasher = Keccak256::new();
hasher.update(format!("{}()", target_method.name).as_bytes());
let digest = hasher.finalize();
digest[..4].to_vec()
});
resolved
}
_ => {
ctx.record_error(
"abi.encodeCall function reference must be `Type.method` or an interface member",
);
return Some(false);
}
};
// Push the 4-byte selector.
instructions.push(Instruction::PushLiteral(LiteralValue::ByteArray(
selector_bytes,
)));
// Extract the tuple argument list: `(arg0, arg1, ...)` or a single
// non-tuple expression. The AST shape is `Expression::List` for
// multi-arg tuples and a plain expression for single-arg tuples.
let payload_args: Vec<&Expression> = match &args[1] {
Expression::List(_, params) => params
.iter()
.filter_map(|(_, p)| p.as_ref().map(|param| ¶m.ty))
.collect(),
Expression::Parenthesis(_, inner) => vec![inner.as_ref()],
other => vec![other],
};
if let Some(result) =
lower_abi_encode_args_direct_for_encode_call(&payload_args, ctx, instructions)
{
if !result {
return Some(false);
}
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
return Some(true);
}
// Task #89 — `bytesN(..)` / `address(..)` casts route through
// `coerce_to_fixed_bytes` which leaves the MEMCPY-returned dst
// buffer on the stack BENEATH the canonical ByteString result.
// The subsequent `AbiEncode` only pops `arg_count` canonical items
// from the top, so each leaked buffer would persist under the
// selector pushed above. The final `BytesConcat(2)` would then
// CAT a stray leak with the encoded payload instead of the real
// selector — producing a 0x00000000 selector slot that duplicates
// the arg bytes (see fuzz harness
// batch35_k5c_encode_call_selector_bytes32_mismatch). Mirror the
// Task #66 fix in resolved.rs: follow each leaky cast with
// `Swap; Drop` to discard the leak before AbiEncode.
//
// Task #106 — when an arg's inferred type is a struct, expand it
// into its fields for the payload: emit `LoadParameter(N+i)` for
// each flattened field so `abi.encode(p.a, p.b, ...)` is produced.
// `p.a` / `p.b` land in separate param slots once the struct arg
// is flattened at INITSLOT (see bytecode_emit_ir::emit_ir_function).
// If the arg isn't a struct, lower it normally.
let mut success = true;
let mut flat_arg_count: usize = 0;
for arg in &payload_args {
// Detect struct-typed arg whose expression is a bare Variable —
// then use `LoadParameter(base+i)` for each flattened field.
if let Some((base_slot, field_count)) = resolve_struct_param_flat_slots(arg, ctx) {
for i in 0..field_count {
instructions.push(Instruction::LoadParameter(base_slot + i));
}
flat_arg_count += field_count;
continue;
}
if !lower_expression(arg, ctx, instructions) {
success = false;
continue;
}
// No Swap; Drop needed: real NeoVM MEMCPY pushes nothing.
flat_arg_count += 1;
}
if !success {
return Some(false);
}
if flat_arg_count > 0 {
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::AbiEncode,
arg_count: flat_arg_count,
});
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
}
return Some(true);
}
if base.name == "abi"
&& matches!(
member.name.as_str(),
"encodeWithSignature" | "encodeWithSelector"
)
{
ctx.record_warning_with_suggestion(
format!(
"abi.{}(...) is approximated on Neo N3 as selector bytes concatenated with abi.encode(args). This differs from raw EVM calldata semantics.",
member.name
),
"Prefer typed contract calls when possible, or use the returned bytes as a Neo-side calldata approximation only.",
);
if member.name == "encodeWithSignature" {
let Some((signature_expr, payload_args)) = args.split_first() else {
ctx.record_error("abi.encodeWithSignature requires a signature argument");
return Some(false);
};
let Some(signature) = resolve_signature_string(signature_expr, ctx) else {
ctx.record_error(
"abi.encodeWithSignature signature must be a string literal or a constant string",
);
return Some(false);
};
let mut hasher = Keccak256::new();
hasher.update(signature.as_bytes());
let digest = hasher.finalize();
let selector = digest[..4].to_vec();
instructions.push(Instruction::PushLiteral(LiteralValue::ByteArray(selector)));
if payload_args.is_empty() {
return Some(true);
}
if let Some(result) =
lower_abi_encode_args_direct_from_slice(payload_args, ctx, instructions)
{
if !result {
return Some(false);
}
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
return Some(true);
}
let mut success = true;
for arg in payload_args {
if !lower_expression(arg, ctx, instructions) {
success = false;
}
}
if !success {
return Some(false);
}
if !payload_args.is_empty() {
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::AbiEncode,
arg_count: payload_args.len(),
});
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
}
return Some(true);
}
let Some((selector_expr, payload_args)) = args.split_first() else {
ctx.record_error("abi.encodeWithSelector requires a selector argument");
return Some(false);
};
if !lower_expression(selector_expr, ctx, instructions) {
return Some(false);
}
if payload_args.is_empty() {
return Some(true);
}
if let Some(result) =
lower_abi_encode_args_direct_from_slice(payload_args, ctx, instructions)
{
if !result {
return Some(false);
}
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
return Some(true);
}
let mut success = true;
for arg in payload_args {
if !lower_expression(arg, ctx, instructions) {
success = false;
}
}
if !success {
return Some(false);
}
if !payload_args.is_empty() {
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::AbiEncode,
arg_count: payload_args.len(),
});
instructions.push(Instruction::CallBuiltin {
builtin: BuiltinCall::BytesConcat,
arg_count: 2,
});
}
return Some(true);
}
if let Some(result) = try_lower_runtime_member_builtin(base, member, args, ctx, instructions) {
return Some(result);
}
if let Some(result) = try_lower_syscalls_member_builtin(base, member, args, ctx, instructions) {
return Some(result);
}
if let Some(result) = try_lower_storage_member_builtin(base, member, args, ctx, instructions) {
return Some(result);
}
if let Some(result) = try_lower_neo_member_builtin(base, member, args, ctx, instructions) {
return Some(result);
}
if let Some(result) =
try_lower_nativecalls_member_builtin(base, member, args, ctx, instructions)
{
return Some(result);
}
None
}