Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [ '1.19', '1.20', '1.21.x', '1.22.x', '1.23.x', '1.24.x', '1.25.x', '1.26.x' ]
go-version: [ '1.25.x', '1.26.x' ]
os: [ ubuntu-latest, macos-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
38 changes: 19 additions & 19 deletions cli/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,25 @@ impl<'a> ParWalker<'a> {
);
let t_active = start_time.elapsed();

if let Some(limit) = cpu_limit {
if limit < 100 {
// Calculate the required sleep duration to limit
// CPU usage to the target percentage.
//
// Let T_active be the elapsed time scanning the
// file. Let T_sleep be the sleep time. The target
// utilization percentage is P.
//
// P = 100 * T_active / (T_active + T_sleep)
// P * (T_active + T_sleep) = 100 * T_active
// P * T_sleep = (100 - P) * T_active
// T_sleep = T_active * (100 - P) / P
let t_sleep = t_active.mul_f64(
(100.0 - limit as f64) / limit as f64,
);
if !t_sleep.is_zero() {
thread::sleep(t_sleep);
}
if let Some(limit) = cpu_limit
&& limit < 100
{
// Calculate the required sleep duration to limit
// CPU usage to the target percentage.
//
// Let T_active be the elapsed time scanning the
// file. Let T_sleep be the sleep time. The target
// utilization percentage is P.
//
// P = 100 * T_active / (T_active + T_sleep)
// P * (T_active + T_sleep) = 100 * T_active
// P * T_sleep = (100 - P) * T_active
// T_sleep = T_active * (100 - P) / P
let t_sleep = t_active.mul_f64(
(100.0 - limit as f64) / limit as f64,
);
if !t_sleep.is_zero() {
thread::sleep(t_sleep);
}
}

Expand Down
9 changes: 5 additions & 4 deletions go/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yara_x

// #include <yara_x.h>
import "C"

import (
"encoding/json"
"errors"
Expand All @@ -28,7 +29,7 @@ type CompileOption func(c *Compiler) error
//
// Valid value types include: int, int32, int64, bool, string, float32 and
// float64.
func Globals(vars map[string]interface{}) CompileOption {
func Globals(vars map[string]any) CompileOption {
return func(c *Compiler) error {
for ident, value := range vars {
c.vars[ident] = value
Expand Down Expand Up @@ -289,7 +290,7 @@ type Compiler struct {
includesEnabled bool
ignoredModules map[string]bool
bannedModules map[string]bannedModule
vars map[string]interface{}
vars map[string]any
features []string
includeDirs []string
maxWarnings *int
Expand All @@ -301,7 +302,7 @@ func NewCompiler(opts ...CompileOption) (*Compiler, error) {
includesEnabled: true,
ignoredModules: make(map[string]bool),
bannedModules: make(map[string]bannedModule),
vars: make(map[string]interface{}),
vars: make(map[string]any),
features: make([]string, 0),
includeDirs: make([]string, 0),
}
Expand Down Expand Up @@ -558,7 +559,7 @@ func (c *Compiler) DefineGlobal(ident string, value interface{}) error {
ret = C.int(C.yrx_compiler_define_global_float(c.cCompiler, cIdent, C.double(v)))
case float64:
ret = C.int(C.yrx_compiler_define_global_float(c.cCompiler, cIdent, C.double(v)))
case map[string]interface{}, []interface{}:
case map[string]any, []any:
jsonStr, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("failed to marshal '%s' to json: '%v'", ident, err)
Expand Down
48 changes: 24 additions & 24 deletions go/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package yara_x
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNamespaces(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.NewNamespace("foo")
c.AddSource("rule test { condition: true }")
Expand All @@ -26,7 +26,7 @@ func TestNamespaces(t *testing.T) {

func TestGlobals(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)
x := map[string]any{"a": map[string]any{"a": "b"}, "b": "d"}
y := []any{"z"}

Expand Down Expand Up @@ -54,7 +54,7 @@ func TestUnsupportedModules(t *testing.T) {
rule test { condition: true }`,
IgnoreModule("unsupported_module"))

assert.NoError(t, err)
require.NoError(t, err)
scanResults, _ := r.Scan([]byte{})
assert.Len(t, scanResults.MatchingRules(), 1)
}
Expand Down Expand Up @@ -85,23 +85,23 @@ func TestDisabledIncludes(t *testing.T) {
}

func TestIncludes(t *testing.T) {
file, err := ioutil.TempFile("", "prefix")
assert.NoError(t, err)
file, err := os.CreateTemp(t.TempDir(), "prefix")
require.NoError(t, err)

defer os.Remove(file.Name())

_, err = Compile(
fmt.Sprintf(`include "%s"`, file.Name()),
IncludeDir(os.TempDir()))

assert.NoError(t, err)
require.NoError(t, err)
}

func TestRelaxedReSyntax(t *testing.T) {
r, err := Compile(`
rule test { strings: $a = /\Release/ condition: $a }`,
RelaxedReSyntax(true))
assert.NoError(t, err)
require.NoError(t, err)
scanResults, _ := r.Scan([]byte("Release"))
assert.Len(t, scanResults.MatchingRules(), 1)
}
Expand All @@ -110,7 +110,7 @@ func TestConditionOptimization(t *testing.T) {
_, err := Compile(`
rule test { condition: true }`,
ConditionOptimization(true))
assert.NoError(t, err)
require.NoError(t, err)
}

func TestErrorOnSlowPattern(t *testing.T) {
Expand All @@ -129,14 +129,14 @@ func TestErrorOnSlowLoop(t *testing.T) {

func TestSerialization(t *testing.T) {
r, err := Compile("rule test { condition: true }")
assert.NoError(t, err)
require.NoError(t, err)

var buf bytes.Buffer

// Write rules into buffer
n, err := r.WriteTo(&buf)

assert.NoError(t, err)
require.NoError(t, err)
assert.Len(t, buf.Bytes(), int(n))

// Read rules from buffer
Expand All @@ -157,7 +157,7 @@ func TestVariables(t *testing.T) {
assert.Len(t, scanResults.MatchingRules(), 1)

c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.DefineGlobal("var", 1234)
c.AddSource("rule test { condition: var == 1234 }")
Expand All @@ -177,7 +177,7 @@ func TestVariables(t *testing.T) {
c.DefineGlobal("var", false)
c.AddSource("rule test { condition: var }")
scanResults, _ = NewScanner(c.Build()).Scan([]byte{})
assert.Len(t, scanResults.MatchingRules(), 0)
assert.Empty(t, scanResults.MatchingRules())

c.DefineGlobal("var", "foo")
c.AddSource("rule test { condition: var == \"foo\" }")
Expand Down Expand Up @@ -207,26 +207,26 @@ func TestCompilerFeatures(t *testing.T) {
rules := `import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }`

_, err := Compile(rules)
assert.EqualError(t, err, `error[E100]: foo is required
require.EqualError(t, err, `error[E100]: foo is required
--> line:1:57
|
1 | import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }
| ^^^^^^^^^^^^^^^^^^^^ this field was used without foo`)

_, err = Compile(rules, WithFeature("foo"))
assert.EqualError(t, err, `error[E100]: bar is required
require.EqualError(t, err, `error[E100]: bar is required
--> line:1:57
|
1 | import "test_proto2" rule test { condition: test_proto2.requires_foo_and_bar }
| ^^^^^^^^^^^^^^^^^^^^ this field was used without bar`)

_, err = Compile(rules, WithFeature("foo"), WithFeature("bar"))
assert.NoError(t, err)
require.NoError(t, err)
}

func TestErrors(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.AddSource("rule test_1 { condition: true }")
assert.Equal(t, []CompileError{}, c.Errors())
Expand Down Expand Up @@ -291,13 +291,13 @@ func TestErrors(t *testing.T) {

func TestRules(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.AddSource(`rule test_1 : tag1 tag2 {
condition:
true
}`)
assert.NoError(t, err)
require.NoError(t, err)

c.AddSource(`rule test_2 {
meta:
Expand All @@ -308,7 +308,7 @@ func TestRules(t *testing.T) {
condition:
true
}`)
assert.NoError(t, err)
require.NoError(t, err)

rules := c.Build()
assert.Equal(t, 2, rules.Count())
Expand All @@ -324,7 +324,7 @@ func TestRules(t *testing.T) {
assert.Equal(t, []string{"tag1", "tag2"}, slice[0].Tags())
assert.Equal(t, []string{}, slice[1].Tags())

assert.Len(t, slice[0].Metadata(), 0)
assert.Empty(t, slice[0].Metadata())
assert.Len(t, slice[1].Metadata(), 4)

assert.Equal(t, "foo", slice[1].Metadata()[0].Identifier())
Expand All @@ -342,7 +342,7 @@ func TestRules(t *testing.T) {

func TestImportsIter(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.AddSource(`
import "pe"
Expand All @@ -351,7 +351,7 @@ func TestImportsIter(t *testing.T) {
condition:
true
}`)
assert.NoError(t, err)
require.NoError(t, err)

rules := c.Build()
imports := rules.Imports()
Expand All @@ -363,7 +363,7 @@ func TestImportsIter(t *testing.T) {

func TestWarnings(t *testing.T) {
c, err := NewCompiler()
assert.NoError(t, err)
require.NoError(t, err)

c.AddSource("rule test { strings: $a = {01 [0-1][0-1] 02 } condition: $a }")

Expand Down
1 change: 0 additions & 1 deletion go/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func Example_compilerAndScanner() {
condition:
$bar
}`)

if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/VirusTotal/yara-x/go

go 1.18
go 1.25

require (
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
6 changes: 3 additions & 3 deletions go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ type Pattern struct {
// Metadata represents a metadata in a Rule.
type Metadata struct {
identifier string
value interface{}
value any
}

// Match contains information about the offset where a match occurred and
Expand Down Expand Up @@ -358,7 +358,7 @@ func (m *Metadata) Identifier() string {
}

// Value associated to the metadata.
func (m *Metadata) Value() interface{} {
func (m *Metadata) Value() any {
return m.value
}

Expand Down Expand Up @@ -474,7 +474,7 @@ func metadataCallback(metadata *C.YRX_METADATA, handle C.uintptr_t) {
panic("matchCallback didn't receive a *[]Metadata")
}

var value interface{}
var value any

switch metadata.value_type {
case C.YRX_I64:
Expand Down
Loading
Loading