From 6f5881d2d1444a8bb64b2c9c3210537c791e0cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kenan=20Faruk=20=C3=87ak=C4=B1r?= Date: Mon, 30 Jan 2023 15:51:44 +0000 Subject: [PATCH 1/3] prevent race on calls to faker func --- core/scenario/scripting/injection/environment.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/scenario/scripting/injection/environment.go b/core/scenario/scripting/injection/environment.go index 90b5b45e..6f9fc600 100644 --- a/core/scenario/scripting/injection/environment.go +++ b/core/scenario/scripting/injection/environment.go @@ -7,6 +7,7 @@ import ( "reflect" "regexp" "strings" + "sync" "time" "go.ddosify.com/ddosify/core/types/regex" @@ -17,6 +18,7 @@ type EnvironmentInjector struct { jr *regexp.Regexp dr *regexp.Regexp jdr *regexp.Regexp + mu sync.Mutex } func (ei *EnvironmentInjector) Init() { @@ -34,8 +36,13 @@ func (ei *EnvironmentInjector) getFakeData(key string) (interface{}, error) { return nil, fmt.Errorf("%s is not a valid dynamic variable", key) } - res := reflect.ValueOf(fakeFunc).Call(nil)[0].Interface() - return res, nil + preventRaceOnRandomFunc := func(fakeFunc interface{}) interface{} { + ei.mu.Lock() + defer ei.mu.Unlock() + return reflect.ValueOf(fakeFunc).Call(nil)[0].Interface() + } + + return preventRaceOnRandomFunc(fakeFunc), nil } func truncateTag(tag string, rx string) string { From 40011106cd54c074dc15d3108129bb7e0cd18aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kenan=20Faruk=20=C3=87ak=C4=B1r?= Date: Tue, 31 Jan 2023 11:35:04 +0300 Subject: [PATCH 2/3] dynamic variable race test --- core/scenario/scripting/injection/dynamic_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 core/scenario/scripting/injection/dynamic_test.go diff --git a/core/scenario/scripting/injection/dynamic_test.go b/core/scenario/scripting/injection/dynamic_test.go new file mode 100644 index 00000000..36f3e6d0 --- /dev/null +++ b/core/scenario/scripting/injection/dynamic_test.go @@ -0,0 +1,15 @@ +package injection + +import ( + "testing" +) + +func TestDynamicVariableRace(t *testing.T) { + num := 10 + ei := EnvironmentInjector{} + for key := range dynamicFakeDataMap { + for i := 0; i < num; i++ { + go ei.getFakeData(key) + } + } +} From f805aaa9c91c7702863454af6417f354c3a3d92a Mon Sep 17 00:00:00 2001 From: fatihbaltaci Date: Tue, 31 Jan 2023 20:28:37 +0300 Subject: [PATCH 3/3] Update jenkinsfile --- Jenkinsfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfile b/Jenkinsfile index 47c31d13..693fa01e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -37,6 +37,7 @@ fi''' steps { lock('multi_branch_server') { sh 'go run --race main.go -t https://servdown.com/ -d 1 -n 1500' + sh 'go test -race -run ^TestDynamicVariableRace$ go.ddosify.com/ddosify/core/scenario/scripting/injection' } } }