Unreachable code after an inherited function call in Solidity - ethereum

Compiling the code
contract Bar {
function blockingFunction() public pure returns (bool) {
assembly {
return(0,0x20)
}
}
}
contract Foo is Bar {
function foo() public pure returns(bool) {
bool result = blockingFunction();
require(result == true, "msg");
return result;
}
}
gives me a warning
Warning: Unreachable code.
--> contracts/implementation/Foo.sol:18:9:
|
18 | require(result == true, "msg");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning: Unreachable code.
--> contracts/implementation/Foo.sol:19:9:
|
19 | return result;
| ^^^^^^^^^^^^^
which makes no sense to me. The blockingFunction call seems to block the following code execution, even though it should return a boolean. Can someone tell me how to fix this? This is my hardhat.config.ts
import "#nomicfoundation/hardhat-toolbox";
import { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
solidity: "0.8.9",
mocha: {
timeout: 100000000
}
}
export default config;

from docs
return(p, s) end execution, return data mem[p…(p+s))
execution will end in assembly{return(0,0x20)} code. so when you called this
bool result = blockingFunction();
the code after this will not be executed

Related

Hook instagram apk using Frida

I wanted to hook some functions from instagram apk using frida, decompiled the apk with
jadx/JEB, one of the functions I wanted to hook was in this:
public static void A00(HGf arg8, I1Y arg9) {
arg8.A0P();
Boolean v0 = arg9.A0v;
if(v0 != null) {
arg8.A0l("about_your_account_bloks_entrypoint_enabled", v0.booleanValue());
}
//some other code here
}
Tried to hook the function with this frida script:
try {
//
let I1X = Java.use("X.I1X")
console.log("this is instance: ", I1X)
console.log(
"these are the methods:: ",
Java.use("X.I1X").class.getDeclaredMethods()
)
I1X.A00.overload().implemention = function (HGf, I1Y) {
console.log("A0l is called")
let ret = this.A0l(str, z)
console.log("A0l ret value is " + ret)
}
} catch (e) {
console.log("failed!" + e)
}
})
this script outputs:
this is instance: <class: X.I1X>
these are the methods::
failed!TypeError: not a function
apparently the A00 here is not a function, so back to jadx in the compiled code there is another class with the same name within same package but consisting of some other code, here it is:
/* renamed from: X.I1x reason: case insensitive filesystem */
/* loaded from: classes7.dex */
public final class C39227I1x {
public C39229I1z A00 = null;
}
apparently Frida is hooking this variable instance A00 In my opinion that is why
it is returning not a function here.
So my question, how can I hook like this situation?
Edit:
the two classes are somewhat different in jadx.

Implement function overloading in typescript

I know that function overloading is not supported in typescript and javascript.
And I'm studying a detour method to work like this function overloading.
My case is as follows.
first:
The last ans first argument is fixed.
public A(arg1:number, arg2:number, argLast:any){
}
public A(arg1:number, arg2:number, arg3:Array<any>, argLast:any){
}
Second :
There is an indicator for whether the function is overloaded.
Of course, as in the above example, it is possible, but I have to create it through a new interface, so it does not fit in my case.
I have tried various function overloading methods.
I also implemented it through john resig blog.
(https://johnresig.com/blog/javascript-method-overloading/)
The code below is an example I made through the above blog.
function addMethod (object, name, fn) {
var old = object[ name ]
object[ name ] = function () {
if (fn.length === arguments.length) {
return fn.apply(this, arguments)
} else if (typeof old === 'function') {
return old.apply(this, arguments)
}
}
}
export class Users{
find(...arg: any[]){
Users.prototype['findOVF'](arg)
}
}
addMethod(Users.prototype, 'findOVF', function () {
console.log('ARG 0')
})
addMethod(Users.prototype, 'findOVF', function (name:string, age:number) {
console.log('ARG 2')
})
addMethod(Users.prototype, 'findOVF', function (first_name:string, last_name:string,age:number) {
console.log('ARG 3')
})
var users = new Users()
users.find()
users.find('John',19)
users.find('John', 'Resig', 19)
When I use this method, I call the function with the parameter (... arg).
I've tried to implement all the methods of the blog below.
(https://blog.mariusschulz.com/2016/08/18/function-overloads-in-typescript)
public A(arg1:number, arg2:number, argLast:any){
}
public A(arg1:number, arg2:number, arg3:Array<any>|null, argLast:any){
}
If I implement function overloading this way, I get an error every time I call the A function. It's not really function overloading.
But I have not found a way to implement these two elements at the same time.
I want to know if this is impossible with the typescript grammar or if there is any other possibility.
Thanks for seeing my awkward English.
You can create overloads in typescript but all the overloads have a single implementation and it's up to you to differentiate between them. The simplest solution, if your methods are all differentiated just by number of parameters is this:
class User { }
export class Users {
find(): User;
find(name: string, age: number): User;
find(first_name: string, last_name: string, age: number): User
find(...args: [any?, any?, any?]): User {
if (args.length == 0) {
return null as any;
} else if (args.length == 1) {
let [name, age, _]: [string?, number?, any?] = args;
console.log(`${name}, ${age}`);
return null as any;
} else if (args.length == 2){
let [first_name, last_name, age]: [string?, string?, number?] = args;
console.log(`${first_name}, ${last_name}, ${age}`);
return null as any;
} else {
throw "Not implemented";
}
}
}
Playground link
The above solution preserves call site type safety, your solution does not since the argument to find is any[] in your example.
You could use a more automated approach by defining a function that will create an overloaded function form an array of functions but wather this extra complexity is worth it you be the judge.
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
function overloads<TThis>(){
return function<T extends Array<(this: TThis, ...args: any[]) => any>>(...fns: T): UnionToIntersection<T[number]> {
return function (this: TThis, ...args: any[]) {
for (var fn of fns) {
if (fn.length === args.length) {
return fn.apply(this, args);
}
}
throw "Not implemented";
} as any
}
}
class User { }
export class Users {
member: string = "M"
find = overloads<Users>()(
function () {
console.log('ARG 0');
this.member // this is Users
},
function (name: string, age: number) {
console.log('ARG 2')
},
function (first_name: string, last_name: string, age: number) {
console.log('ARG 3')
}
);
}
var users = new Users()
users.find('John', 19)
users.find('John', 'Resig', 19)
Playground link
Or a version that assigns the function to the prototype not the instance:
export class Users {
member: string = "M"
}
let find = overloads<Users>()(
function () {
console.log('ARG 0');
this.member // this is Users
},
function (name: string, age: number) {
console.log('ARG 2')
},
function (first_name: string, last_name: string, age: number) {
console.log('ARG 3')
}
);
export interface Users {
find: typeof find;
}
Users.prototype.find = find;
Playground link

Typescript Interfaces: method that return a generic function

Someone know how can I define an interface for a class method like:
wrap(fn) {
return function(){
... // do something
fn()
}
}
I'm crashing my head around this, basically how can I define the type of a parameter (and a return value) to be function?
I'm supposing you wanted to return another function that have identical type of fn.
class Wrapper {
// generic for any fn, without handling parameter type
// a return type is not required: tsc infers from `return` statement.
wrap<T extends Function>(fn: T) {
return function () {
// NOTE this version does not handle types of parameters.
// you will have to use `arguments`
return fn();
} as any as T;
}
// generic for all unary fn
// we can have correct type of arg1 in this way
wrapF1<P1, R>(fn: (arg1: P1) => R) {
const wrapped = function (arg1: P1) {
return fn(arg1);
}
return wrapped;
}
// wrapF2, wrapF3, if you need more
}

Returning same function signature as the received one

I'm trying to create an interceptor function. In my specific case, a throttle function.
Consider the following example:
function throttle(func: Function, wait: number = 0): Function {
let previous: {
args: any[];
timestamp: number;
result: any;
} = {
args: [],
timestamp: 0,
result: null
};
return (...currentArgs: any[]): any => {
const now = Date.now();
const remaining = wait && (wait - (now - previous.timestamp));
const argumentsChanged = JSON.stringify(currentArgs) !== JSON.stringify(previous.args);
if (argumentsChanged || (wait && (remaining <= 0 ||remaining > wait))) {
previous = {
args: currentArgs,
timestamp: now,
result: func.apply(this, currentArgs)
};
}
return previous.result;
};
}
This function will initially call the function passed by argument and will not call it again until the specified wait time has been reached or the target function's arguments changed.
The problem with this is that it should return the same function type as the function passed in by argument, so it can be transparent to the caller.
For instance, this should be allowed but it's not:
function updateStatus(id: number, newStatus: string): void {
// ...
}
// ...
// Type 'Function' is not assignable to type '(id: number, newStatus: string) => void'
this.updateStatus = throttle(this.updateStatus.bind(this), 500);
How can I achieve this?
Instead of using the Function type use a generic constraint.
The function signature should look like this:
function throttle<T extends Function>(func: T, wait: number = 0): T
In a simple example:
function throttle<T extends Function>(func: T, wait: number = 0): T {
return null;
}
function fn(a: string, b: number): boolean {
return false;
}
let throttled = throttle(fn, 3); // type of throttled: (a: string, b: number) => boolean
throttled("stirng", 0); // ok
throttled(3, 4); // error: Argument of type '3' is not assignable to parameter of type 'string'
(code in playground)

Typescript infer return type from passed functions return type

I might be trying to achieve the impossible but here goes.
I want to define a function ( function A ) which will return the same type as a new function passed into the parameter of function A.
e.g.
export function test<T> ( arg:Function ):T {
return arg;
}
function a():string {
return 'a';
}
function b():number {
return 0;
}
let aVal:string = test(a);
let bVal:number = test(b);
Obviously this will allow me to strongly type my responses for some compile time errors.
Does anyone have any ideas or know if I'm just dreaming.
** Note: Code slapped together for demo **
Cheers
How about this?
function test<T>(arg: () => T): T {
return arg();
}
function a(): string {
return 'a';
}
function b(): number {
return 0;
}
let aVal: string = test(a);
let bVal: number = test(b);
Instead of using the Function interface we defined arg as a function that takes no arguments and returns something of type T. The actual type T then can be defined by the function that's passed in.