Adversarial Robustness for Code Pavol Bielik , Martin Vechev - - PowerPoint PPT Presentation

adversarial robustness for code
SMART_READER_LITE
LIVE PREVIEW

Adversarial Robustness for Code Pavol Bielik , Martin Vechev - - PowerPoint PPT Presentation

ICML 2020 Adversarial Robustness for Code Pavol Bielik , Martin Vechev pavol.bielik@inf.ethz.ch, martin.vechev@inf.ethz.ch Department of Computer Science 1 Adversarial Robustness panda gibbon Vision + = Explaining and Harnessing


slide-1
SLIDE 1

Adversarial Robustness for Code

Pavol Bielik, Martin Vechev

pavol.bielik@inf.ethz.ch, martin.vechev@inf.ethz.ch

Department of Computer Science

1

ICML 2020

slide-2
SLIDE 2

Adversarial Robustness

Vision + =

Explaining and Harnessing Adversarial Examples. Goodfellow et. al. ICLR’15

Sound

noise

+ =

Audio Adversarial Examples: Targeted Attacks on Speech-to-Text. Carlini et. al. ICML’18 workshop

panda gibbon

2

slide-3
SLIDE 3

Adversarial Robustness for Code

Vision Sound

noise

Code + = + =

code refactoring

+ =

panda gibbon

Explaining and Harnessing Adversarial Examples. Goodfellow et. al. ICLR’15 Audio Adversarial Examples: Targeted Attacks on Speech-to-Text. Carlini et. al. ICML’18 workshop 3

slide-4
SLIDE 4

2019

Deep Learning + Code

Code Classification 2018 2017 2016 Code Captioning Type Inference Variable Naming Code Completion Program Translation Bug Detection Loop Invariants Code Search Neural Decompilation Accuracy

90%

Prior Works

Bug Repair

4

slide-5
SLIDE 5

2019

Adversarial Robustness for Code

Code Classification 2018 2017 2016 Code Captioning Type Inference Variable Naming Code Completion Program Translation Bug Detection Loop Invariants Code Search Neural Decompilation Robustness

?

Accuracy

90%

Prior Works

Bug Repair

5

slide-6
SLIDE 6

2019 2018 2017 2016

Adversarial Robustness for Code

Code Classification Code Captioning Variable Naming Code Completion Program Translation Bug Detection Loop Invariants Code Search Neural Decompilation Accuracy

90%

Robustness

4%-50%

Prior Works

Bug Repair Type Inference Accuracy

88%

Robustness

84%

This Work

6

slide-7
SLIDE 7

Adversarial Robustness Example

...

v = parseInt( hex.substr(1), radix )

...

Model f(x) → y Input Program x

Goal (Adversarially Robustness): Model is correct for all label preserving program transformations

Program Properties y

...

vnum = parseIntnum( hexstr.substrstr(1), radixnum )

...

(Type Inference)

7 ... v = parseInt( color.substr(1), radix ) ... variable renaming ... v = parseInt( hex.substr(42), radix ) ... constant replacement ... v = parseInt( hex.substr(1), radix + 0 ) ... semantic equivalence ... parseInt( hex.substr(1), radix ) ... remove assignment

slide-8
SLIDE 8

Our Work: Three Key Techniques

... v = parseInt( hexabs.substrabs(1), radixabs ) ...

Abstain

1

8

Allows model not to make a prediction if uncertain

slide-9
SLIDE 9

Our Work: Three Key Techniques

... v = parseInt( hexabs.substrabs(1), radixabs ) ...

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color 9

54%

robustness

54%

robustness

slide-10
SLIDE 10

Our Work: Three Key Techniques

... v = parseInt( hexabs.substrabs(1), radixabs ) ...

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3

10

slide-11
SLIDE 11

Our Work: Three Key Techniques

... v = parseInt( hexabs.substrabs(1), radixabs ) ...

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3

11

84%

robustness

slide-12
SLIDE 12

Our Work: Three Key Techniques

... v = parseIntnum( hexabs.substrabs(1), radixabs ) ...

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3 4

Refinement

12

slide-13
SLIDE 13

Learning to Abstain

Leads to a simpler

  • ptimization problem

Property prediction problem is undecidable

Predict Class Abstains Model should be both Robust and Accurate Model should be

  • nly Robust

= +

abstain y1 y2 input xi

13

slide-14
SLIDE 14

Learning to Abstain

Predict Class Model should be both Robust and Accurate = +

abstain input xi

Combine Robustness + Learning to Abstain Main Insight

Deep Gamblers: Learning to Abstain with Portfolio Theory. Liu et. al. NeurIPS’19

How to Abstain?

14

Leads to a simpler

  • ptimization problem

Property prediction problem is undecidable

slide-15
SLIDE 15

Our Work: Three Key Techniques

... v = parseIntnum( hexabs.substrabs(1), radixabs ) ...

Learned Jointly

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3 4

Refinement

abstain y1 y2

15

slide-16
SLIDE 16

Adversarial Training

min loss(𝜄, x, y) Standard training

measures the model performance ground-truth label

min [max loss(𝜄, x + 𝜀, y)] 𝜀 ∊ S(x) Adversarial training

Solve the inner

max loss efficiently

Define the space S of program transformations

1 2 Label preserving program transformations

16

slide-17
SLIDE 17

Label Preserving Program Transformations

Word Substitution

Constants, Binary Operators, ...

7 radix + offset 42 radix - offset x + 𝜀

Word Renaming

Rename Variables, Parameters, Fields, Method Names, ...

def getID() {...} client.Name def get_id() {...} client.name x + 𝜀

Sequence Substitution

Adding Dead Code, Reordering Statements, ...

a = get_id() b = 42 b = 42 a = get_id() x + 𝜀 tensors + 𝜀

very fast

tensors + 𝜀 + analysis

fast

tensors→code + 𝜀 + analysis→tensors

slow

17

slide-18
SLIDE 18

Adversarial Training

min loss(𝜄, x, y) Standard training

measures the model performance ground-truth label

min [max loss(𝜄, x + 𝜀, y)] 𝜀 ∊ S(x) Adversarial training

Label preserving program transformations

Solve the inner

max loss efficiently

Define the space S of program transformations

1 2

18

slide-19
SLIDE 19

Solving the Inner max loss Efficiently

Adversarial Examples for Models of Code. Yefet et. al. ArXiv’20

Gradient Based Optimization

x + 𝜀

𝜄 ← 𝜄 - ∇ loss(𝜄, x + 𝜀, y)

𝜀 ∊ S(x)

S(x)

decision boundary no structural transformations Discrete and disruptive changes Highly structured and large programs hard optimization problem

Limitations

same or worse robustness standard

54%

adversarial

54%

19

slide-20
SLIDE 20

Solving the Inner max loss Efficiently

Gradient Based Optimization

x + 𝜀

𝜄 ← 𝜄 - ∇ loss(𝜄, x + 𝜀, y)

𝜀 ∊ S(x)

S(x)

min [max loss(𝜄, x + 𝜀, y)]

𝜀 ∊ S(𝛽(x))

S(𝛽(x))

Refine S

... v = parseInt( color.substr(1), radix ) ... parseInt( _, _ )

learned representation

20

slide-21
SLIDE 21

Solving the Inner max loss Efficiently

Gradient Based Optimization

x + 𝜀

𝜄 ← 𝜄 - ∇ loss(𝜄, x + 𝜀, y)

𝜀 ∊ S(x)

S(x)

min [max loss(𝜄, x + 𝜀, y)]

𝜀 ∊ S(𝛽(x))

S(𝛽(x))

Refine S

... v = parseInt( color.substr(1), radix ) ... parseInt( _, _ )

reduces the search space leads to an easier optimization

21

slide-22
SLIDE 22

Solving the Inner max loss Efficiently

Gradient Based Optimization

x + 𝜀

𝜄 ← 𝜄 - ∇ loss(𝜄, x + 𝜀, y)

𝜀 ∊ S(x)

S(x)

min [max loss(𝜄, x + 𝜀, y)]

𝜀 ∊ S(𝛽(x))

S(𝛽(x))

Refine S

... v = parseInt( color.substr(1), radix ) ... parseInt( _, _ )

reduces the search space leads to an easier optimization

  • rthogonal to gradient optimization

supports all transformations

22

slide-23
SLIDE 23

Our Work: Three Key Techniques

... v = parseIntnum( hexabs.substrabs(1), radixabs ) ...

Learned Jointly

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3 4

Refinement

abstain y1 y2

23

slide-24
SLIDE 24

Representation Learning

Programs as Graphs

1

Learning to Represent Programs with Graphs. Allamanis et. al. ICLR’18 Generative Code Modeling with Graphs. Brockschmidt et. al. ICLR’19

+ x 7 = v v = x + 7

G =〈V, E, 𝜊〉

nodes edges attributes

Define Refinement

2

𝛽: 〈V, E, 𝜊〉→〈V, E’ ⊆ E, 𝜊〉

+ x 7 = v

Remove Graph Edges

24

slide-25
SLIDE 25

Representation Learning

Programs as Graphs

1

Learning to Represent Programs with Graphs. Allamanis et. al. ICLR’18 Generative Code Modeling with Graphs. Brockschmidt et. al. ICLR’19

Define Refinement

2

+ x 7 = v v = x + 7

G =〈V, E, 𝜊〉

nodes edges attributes

𝛽: 〈V, E, 𝜊〉→〈V, E’ ⊆ E, 𝜊〉

+ x 7 = v

Remove Graph Edges

All decisions are made locally

25

slide-26
SLIDE 26

Representation Learning

Programs as Graphs

1

Learning to Represent Programs with Graphs. Allamanis et. al. ICLR’18 Generative Code Modeling with Graphs. Brockschmidt et. al. ICLR’19

Define Refinement

2

Optimize 𝛽

3

+ x 7 = v v = x + 7

G =〈V, E, 𝜊〉

nodes edges attributes

𝛽: 〈V, E, 𝜊〉→〈V, E’ ⊆ E, 𝜊〉

+ x 7 = v

Remove Graph Edges Minimize Graph Size

arg min ∑ |𝛽(x)| 𝛽

(x, y) ∈

subject to loss(𝜄, x, y) ≈ loss(𝜄, 𝛽(x), y)

26

slide-27
SLIDE 27

Our Work: Three Key Techniques

... v = parseIntnum( hexabs.substrabs(1), radixabs ) ...

Learned Jointly

Abstain

1

Adversarial Training

2

... vnum = parseIntnum( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseIntnum( _, _ )

Representation Learning

3 4

Refinement

abstain y1 y2

27

slide-28
SLIDE 28

Evaluation

vnum = parseIntnum( hexstr.substrstr(1), radixnum )

Type Inference

string, number, boolean, void ()⇒string, ()⇒number, ()⇒boolean, ()⇒void any target classes (y)

JavaScript

28

Task

Typilus: Neural Type Hints. Allamanis et. al. PLDI’20 LambdaNet: Probabilistic Type Inference using Graph Neural Networks. Wei et. al. ICLR’20

more complex type inference

Models

LSTM DeepTyper Graph Neural Networks GNNTransformer GNNGCN GNNGGNN LSTM + 1 layer GNN + LSTM

DeepTyper: Deep Learning Type Inference. Hellendoorn et. al., FSE’18

slide-29
SLIDE 29

Our Work: Three Key Techniques

4

Refinement Abstain Representation Learning

1st Model 2nd Model 3rd Model

29

slide-30
SLIDE 30

Evaluation

Accuracy Robustness Standard Training 89.3% 54.9% 90.3% 54.3% 83.8% 88.4% Adversarial Training All Components

GNNTransformer

30

Robustness

+29%

Accuracy

  • 1%
slide-31
SLIDE 31

Evaluation

Accuracy Robustness Standard Training 89.3% 54.9% 90.3% 54.3% 83.8% 88.4% Adversarial Training All Components

GNNTransformer

0% 99% 100%

Target Accuracy

All Components All Components 99.6% 99.9% 99.0% 99.9% 61.3% 75.9% Abstain

  • 31

Allows training highly accurate & robust models

slide-32
SLIDE 32

Adversarial Robustness for Code

... v = parseInt( hex.substr(1), radix ) ...

Abstain

1

Adversarial Training

2

... v = parseInt( color.substr(1), radix ) ... 𝜀 = hex → color

𝛽(x + 𝜀)

parseInt( _, _ )

Representation Learning

3

32

4

Refinement

For more experiments and results, please refer to the extended version of our paper We only scratched the surface, more work in domain of code is needed and is being done, e.g.:

Adversarial Examples for Models of Code. Yefet et. al. ArXiv Optimization-guided binary diversification to mislead neural networks for malware detection. Sharif et. al. ArXiv Semantic Robustness of Models of Source Code. Ramakrishnan et. al., ArXiv