-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding warning for usage of unsound features when a function is being…
… summarized.
- Loading branch information
1 parent
80a7a45
commit 564c28f
Showing
12 changed files
with
346 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
taint-tracking-problems: | ||
- sources: | ||
- package: main | ||
method: Source | ||
- package: command-line-arguments | ||
method: Source | ||
- package: (main|command-line-arguments) | ||
field: Source | ||
sinks: | ||
- package: command-line-arguments | ||
method: Sink | ||
- package: main | ||
method: Sink | ||
options: | ||
summarize-on-demand: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"strconv" | ||
"unsafe" | ||
) | ||
|
||
func mayPanic() { | ||
panic("a problem") | ||
} | ||
|
||
func Source() string { | ||
return "x" + strconv.Itoa(rand.Int()) | ||
} | ||
|
||
func Sink(x string) { | ||
fmt.Println(x) | ||
} | ||
|
||
type Example struct { | ||
x int | ||
f string | ||
} | ||
|
||
func (e Example) String() string { | ||
return e.f + strconv.Itoa(e.x) | ||
} | ||
|
||
func usingUnsafe() { | ||
s := Example{ | ||
x: 1243, | ||
f: Source(), | ||
} | ||
x := []string{"a", "b", "c"} | ||
var i uintptr | ||
i = 3 | ||
// equivalent to f := unsafe.Pointer(&s.f) | ||
f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f)) | ||
// equivalent to e := unsafe.Pointer(&x[i]) | ||
e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0])) | ||
|
||
fmt.Println(e, f) | ||
} | ||
|
||
func usingReflect() { | ||
x := 10 | ||
name := "Go Lang" | ||
example := Example{1, Source()} // @Source(reflect) | ||
fmt.Println(reflect.TypeOf(x)) | ||
fmt.Println(reflect.TypeOf(name)) | ||
fmt.Println(reflect.TypeOf(example)) | ||
|
||
method := reflect.ValueOf(example).MethodByName("String") | ||
if !method.IsValid() { | ||
fmt.Println("ERROR: String is not implemented") | ||
return | ||
} | ||
e := method.Call(nil) // this call is elided by the pointer analysis and results in false negatives! | ||
Sink(e[0].String()) // @Sink(reflect) | ||
} | ||
|
||
func usingRecover() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
|
||
fmt.Println("Recovered. Error:\n", r) | ||
} | ||
}() | ||
mayPanic() | ||
fmt.Println("After mayPanic()") | ||
|
||
} | ||
|
||
func main() { | ||
usingUnsafe() | ||
usingReflect() | ||
usingRecover() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dataflow_test | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/awslabs/ar-go-tools/analysis/config" | ||
"github.com/awslabs/ar-go-tools/analysis/dataflow" | ||
"github.com/awslabs/ar-go-tools/internal/analysistest" | ||
) | ||
|
||
func TestUnsoundFeatures(t *testing.T) { | ||
dir := filepath.Join("testdata", "unsound-features") | ||
lp, err := analysistest.LoadTest(testfsys, dir, []string{}, | ||
analysistest.LoadTestOptions{ApplyRewrite: true}) | ||
if err != nil { | ||
t.Fatalf("failed to load test: %v", err) | ||
} | ||
c, err := dataflow.NewInitializedAnalyzerState(lp.Prog, lp.Pkgs, config.NewLogGroup(lp.Config), lp.Config) | ||
if err != nil { | ||
t.Errorf("error building state: %q", err) | ||
} | ||
var unsafeChecked, reflectChecked, recoverChecked bool | ||
for f := range c.ReachableFunctions() { | ||
if strings.Contains(f.Name(), "usingUnsafe") { | ||
|
||
uf := dataflow.FindUnsoundFeatures(f) | ||
if len(uf.UnsafeUsages) <= 2 { | ||
t.Errorf("Did not detect enough usages of unsage in usingUnsafe") | ||
} | ||
unsafeChecked = true | ||
} | ||
if strings.Contains(f.Name(), "usingReflect") { | ||
uf := dataflow.FindUnsoundFeatures(f) | ||
if len(uf.ReflectUsages) <= 2 { | ||
t.Errorf("Did not detect enough usages of unsage in usingReflect") | ||
} | ||
reflectChecked = true | ||
} | ||
if strings.Contains(f.Name(), "usingRecover$1") { | ||
uf := dataflow.FindUnsoundFeatures(f) | ||
if len(uf.Recovers) <= 0 { | ||
t.Errorf("Did not detect enough usages of recover in usingRecover") | ||
} | ||
recoverChecked = true | ||
} | ||
} | ||
if !(recoverChecked && reflectChecked && unsafeChecked) { | ||
t.Errorf("Failed to check for recover, reflect and unsafe functions") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.