Skip to content

[TS] Arrays methods #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 7, 2025
Merged

[TS] Arrays methods #304

merged 5 commits into from
Aug 7, 2025

Conversation

CaelmBleidd
Copy link
Member

@CaelmBleidd CaelmBleidd commented Jul 9, 2025

This PR adds approximations for some Array methods: push, pop, fill, shift, unshift, join, slice, concat, indexOf, includes, reverse.

In most cases, only single-argument (and non-array) calls (where it is applicable) are supported for now. For example, Array.unshift(...items) prepends items to the left, but we currently assume unshift receives a single value.

In all methods that receive start/end indices (e.g. slice or fill), negative indices are not yet supported.

In Array.fill and Array.reverse, only relatively small arrays are supported (of size at most 10000 and 5000, respectively), since it is (yet) impossible to create a symbolic array of symbolic length.

@CaelmBleidd CaelmBleidd requested a review from Lipen July 9, 2025 13:29
@CaelmBleidd CaelmBleidd changed the title Caelmbleidd/shift method [TS] Arrays methods Jul 9, 2025
@CaelmBleidd CaelmBleidd requested a review from Lipen July 10, 2025 09:34
@CaelmBleidd CaelmBleidd force-pushed the caelmbleidd/shift_method branch from 9187a68 to b71e504 Compare July 11, 2025 13:25
Base automatically changed from caelmbleidd/lt to main July 11, 2025 17:32
@Lipen Lipen force-pushed the caelmbleidd/shift_method branch from b71e504 to 1209ec8 Compare August 1, 2025 12:38
Copy link
Member

@Lipen Lipen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@Lipen Lipen force-pushed the caelmbleidd/shift_method branch from bdc0374 to 6d4b763 Compare August 1, 2025 13:20
*/
private fun TsExprResolver.handleArrayJoin(
expr: EtsInstanceCallExpr,
arrayType: EtsArrayType,

Check warning

Code scanning / detekt

Function parameter is unused and should be removed. Warning

Function parameter arrayType is unused.
private fun TsExprResolver.handleArrayJoin(
expr: EtsInstanceCallExpr,
arrayType: EtsArrayType,
elementSort: USort,

Check warning

Code scanning / detekt

Function parameter is unused and should be removed. Warning

Function parameter elementSort is unused.
private fun TsExprResolver.handleArrayIndexOf(
expr: EtsInstanceCallExpr,
arrayType: EtsArrayType,
elementSort: USort,

Check warning

Code scanning / detekt

Function parameter is unused and should be removed. Warning

Function parameter elementSort is unused.
*/
private fun TsExprResolver.handleArrayIncludes(
expr: EtsInstanceCallExpr,
arrayType: EtsArrayType,

Check warning

Code scanning / detekt

Function parameter is unused and should be removed. Warning

Function parameter arrayType is unused.
private fun TsExprResolver.handleArrayIncludes(
expr: EtsInstanceCallExpr,
arrayType: EtsArrayType,
elementSort: USort,

Check warning

Code scanning / detekt

Function parameter is unused and should be removed. Warning

Function parameter elementSort is unused.
Comment on lines +66 to +74

/**

Check warning

Code scanning / detekt

Disallow blank lines in lists before, between or after any list element. Warning

Unexpected blank line(s) in value parameter list
@Lipen Lipen requested a review from Copilot August 4, 2025 12:17
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds approximations for several TypeScript array methods (push, pop, fill, shift, unshift) to enable symbolic execution. The implementation focuses on single-argument calls and includes test cases to verify the behavior of these array operations.

  • Implements array method approximations with proper memory management and length tracking
  • Refactors string constant handling to improve state management
  • Adds comprehensive test coverage for the new array methods

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
usvm-ts/src/test/resources/samples/arrays/ArrayMethods.ts TypeScript test samples for array methods including push, pop, fill, shift, unshift, join, slice, concat, indexOf, includes, and reverse
usvm-ts/src/test/kotlin/org/usvm/util/TsTestResolver.kt Minor refactor to extract string constant value retrieval logic
usvm-ts/src/test/kotlin/org/usvm/samples/arrays/ArrayMethods.kt Test implementations verifying array method behavior through symbolic execution
usvm-ts/src/test/kotlin/org/usvm/project/DemoCalc.kt Updates test configuration for demo calculator project
usvm-ts/src/main/kotlin/org/usvm/machine/state/TsState.kt Adds string constant tracking and initialization methods to state management
usvm-ts/src/main/kotlin/org/usvm/machine/interpreter/TsInterpreter.kt Fixes array type resolution for allocated heap references
usvm-ts/src/main/kotlin/org/usvm/machine/expr/TsExprResolver.kt Refactors expression resolution by extracting call approximations
usvm-ts/src/main/kotlin/org/usvm/machine/expr/CallApproximations.kt New file implementing array method approximations and call handling logic
usvm-ts/src/main/kotlin/org/usvm/machine/TsContext.kt Refactors string constant management for better separation of concerns
Comments suppressed due to low confidence (1)

usvm-ts/src/main/kotlin/org/usvm/machine/expr/CallApproximations.kt:625

  • The documentation is incorrect. According to the ECMAScript specification, Array.concat() returns a new array and does not modify the original array in place.
 * https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.concat

@UnitTestBot UnitTestBot deleted a comment from Copilot AI Aug 5, 2025
@CaelmBleidd
Copy link
Member Author

@Lipen please, take a look at the comments

@Lipen Lipen force-pushed the caelmbleidd/shift_method branch from e0488d3 to da024c5 Compare August 5, 2025 15:21
@CaelmBleidd CaelmBleidd force-pushed the caelmbleidd/shift_method branch from 41bb8a0 to 7a5a1ce Compare August 6, 2025 09:05
elementSort: USort,
): UExpr<*>? = with(ctx) {
val array = resolve(expr.instance)?.asExpr(addressSort) ?: return null
check(expr.args.size >= 1 && expr.args.size <= 3) {

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
} else {
mkBv(0)
}
val startBv: UExpr<TsSizeSort> = when (start.sort) {

Check warning

Code scanning / detekt

Braces do not comply with the specified policy Warning

Inconsistent braces, make sure all branches either have or don't have braces.
val lengthLValue = mkArrayLengthLValue(array, arrayType)
scope.calcOnState { memory.read(lengthLValue) }
}
val endBv: UExpr<TsSizeSort> = when (end.sort) {

Check warning

Code scanning / detekt

Braces do not comply with the specified policy Warning

Inconsistent braces, make sure all branches either have or don't have braces.
check(expr.args.size == 1) {
"Array.indexOf() should have exactly one argument, but got ${expr.args.size}"
}
val searchElement = resolve(expr.args.single()) ?: return null

Check warning

Code scanning / detekt

Property is unused and should be removed. Warning

Private property searchElement is unused.
check(expr.args.size == 1) {
"Array.includes() should have exactly one argument, but got ${expr.args.size}"
}
val searchElement = resolve(expr.args.single()) ?: return null

Check warning

Code scanning / detekt

Property is unused and should be removed. Warning

Private property searchElement is unused.
}
)

//! Note: `reversedArray` is a temporary object not used outside this function,

Check warning

Code scanning / detekt

Checks if comments have the right spacing Warning

Missing space after //
@Lipen Lipen merged commit 419eb50 into main Aug 7, 2025
6 checks passed
@Lipen Lipen deleted the caelmbleidd/shift_method branch August 7, 2025 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants