Skip to content

Add runtime cost tracking for string extension functions#1288

Open
qq96932100 wants to merge 1 commit intogoogle:masterfrom
qq96932100:string-cost-tracking
Open

Add runtime cost tracking for string extension functions#1288
qq96932100 wants to merge 1 commit intogoogle:masterfrom
qq96932100:string-cost-tracking

Conversation

@qq96932100
Copy link

stringLib.ProgramOptions() returns an empty slice, so string extension calls (replace, split, join, indexOf, etc.) all get charged a flat runtime cost of 1 in costCall() regardless of input size. This makes CostLimit ineffective for these operations — chained replace calls can produce exponentially growing output while staying under the cost budget.

The lists and regex extensions already register proper cost trackers. This adds the same for string extension overloads, version-gated to match function availability.

stringLib.ProgramOptions() returns empty, so string extension
calls get a flat runtime cost of 1 regardless of input size.
The lists and regex extensions already register cost trackers;
this adds the same for string overloads.
@jnthntatum
Copy link
Collaborator

/gcbrun

func (*stringLib) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{}
func (lib *stringLib) ProgramOptions() []cel.ProgramOption {
trackers := []interpreter.CostTrackerOption{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Even though the functions are introduced at a specific version, changes in cost tracking also need to be versioned, so you'll have to make the version check >= 4 for all cost changes.

// ProgramOptions implements the Library interface method.
func (*stringLib) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{}
func (lib *stringLib) ProgramOptions() []cel.ProgramOption {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note, it's important to introduce cost calculations for the checked expression as well, otherwise the runtime cost value may far exceed the expected cost

@TristonianJones
Copy link
Collaborator

@qq96932100 thanks for the contributions!

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.

3 participants