SlideShare a Scribd company logo
LLVM UB and Optimization
허규영 Software Engineer
https://twitter.com/bbvch13531
Using freeze instruction
• What is Unde
fi
ned Behavior
• UB and Optimization
• Poison Value
• Freeze Instruction
UB(Unde
fi
ned Behavior)
• Implementation De
fi
ned Behavior: 문서에 의해 정의된 동작
• Unspeci
fi
ed Behavior: 명시되지 않은 동작
• Unde
fi
ned Behavior
UB(Unde
fi
ned Behavior)
• Implementation De
fi
ned
• Unspeci
fi
ed Behavior
• Unde
fi
ned Behavior
컴파일러가 어떻게 동작할지 몰?루
Example of UB program
2021년 정기 2회 정보처리기능사 실기문제
K O R E A
0 1 2 3 4
Access out of bounds
Memory Protection Violation
str
LLVM UB Optimization
Segmentation Fault
Segmentation Fault
LLVM UB Optimization
다른 정보처리기능사 문제
1. 55
2. 77
3. 121
4. 132
sum += *(p + i) ;
11
0
22
1
44 55
0
1
P
Access out of bounds
모 과학고등학교의 정보수행평가 문제
--y와 w+x+y-z 중 어떤 것이 먼저 실행될까요?
Sequence point rules
Order of evaluation
Between the previous and next sequence point a
scalar object must have its stored value modi
fi
ed at
most once by the evaluation of an expression,
Otherwise the behavior is unde
fi
ned.
How UB and Optimization related
output( p + a > p + b )
Peephole Optimization
output( a > b )
Optimize
int* p
int a
int b
output( p + a > p + b )
Peephole Optimization
output( a > b )
Optimize
0xFFFFFF00
int* p
0x100
int a
0x0
int b
output( p + a > p + b )
Peephole Optimization
output( a > b )
Optimize
0xFFFFFF00
int* p
0x100
int a
0x0
int b
0x0
False True
0x100 > 0x0
Over
fl
ow!
output( p + a > p + b )
Peephole Optimization
output( a > b )
Optimize
0xFFFFFF00
int* p
0x100
int a
0x0
int b
False True
0x100 > 0x0
Miscompilation
before optimize after optimize
output( p + a > p + b )
Peephole Optimization
output( a > b )
Optimize
Pointer Arithmetic Over
fl
ow is
Unde
fi
ned Behavior
UB
0x0
Over
fl
ow!
Why poison value is needed
Loop Invariant Code Motion
...
for(i = 0; i < n; i++){
a[i] = p + 0x100
}
q = p + 0x100
for(i = 0; i < n; i++){
a[i] = q
}
Loop Invariant Code Motion
...
for(i = 0; i < n; i++){
a[i] = p + 0x100
}
q = p + 0x100
for(i = 0; i < n; i++){
a[i] = q
}
0xFFFFFF00
p
Loop Invariant Code Motion
...
for(i = 0; i < n; i++){
a[i] = p + 0x100
}
q = p + 0x100
for(i = 0; i < n; i++){
a[i] = q
}
UB
0x0
Over
fl
ow!
Loop Invariant Code Motion
...
for(i = 0; i < n; i++){
a[i] = p + 0x100
}
q = p + 0x100
for(i = 0; i < n; i++){
a[i] = q
}
Miscompilation
Correct Program Wrong Program
when n = 0
UB
...
for(i = 0; i < n; i++){
a[i] = p + 0x100
}
q = p + 0x100
for(i = 0; i < n; i++){
a[i] = q
}
Poison Value
poison
when n = 0
De
fi
nition of Poison Value
poison is a special value that represents a violation of assumption
Each operation on poison value propergate poison or raise UB
Poison Propagation
p a p b
0xFFFFFF00 0x100
poison
0xFFFFFF00 0x0
poison
output
UB
Overflow!
Inconsistency in LLVM
Global Value Numbering (GVN)
if(x == y) {
... use x ...
} else {
...
}
if(x == y) {
... use y ...
} else {
...
}
How to de
fi
ne when branching on poison value
Global Value Numbering (GVN)
if(x == y) {
... use x ...
} else {
...
}
if(x == y) {
... use y ...
} else {
...
}
How to de
fi
ne when branching on poison value
0 poison
Global Value Numbering (GVN)
if(x == y) {
... use x ...
} else {
...
}
if(x == y) {
... use y ...
} else {
...
}
How to de
fi
ne when branching on poison value
0 poison
Miscompilation
Correct Program Wrong Program
Global Value Numbering (GVN)
if(x == y) {
... use x ...
} else {
...
}
if(x == y) {
... use y ...
} else {
...
}
How to de
fi
ne when branching on poison value
poison
Global Value Numbering (GVN)
if(x == y) {
... use x ...
} else {
...
}
if(x == y) {
... use y ...
} else {
...
}
Branching on poison value is
Unde
fi
ned Behavior
UB
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(cond)
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(cond)
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
poison
UB
Branching on poison is
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(cond)
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
poison
UB
Branching on poison is
False
when n = 0
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(cond)
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
poison
UB
Branching on poison is
False
when n = 0
Miscompilation
Correct Program Wrong Program
Global Value Numbering (GVN)
Branching on poison value is
Unde
fi
ned Behavior
GVN + LU
Branching on poison value is
Not Always
Unde
fi
ned Behavior
GVN + LU is inconsistent
Inconsistency in LLVM
• SNU found miscompilation bug in LLVM when GVN+LU optimization
• Google reported a real-world bug due to GVN+LU inconsistance
Handle UB using freeze
Freeze instruction
y = freeze x
• if x is de
fi
ned value
• if x is poison value or undef
freeze x -> x
freeze x ->
which y is nondeterministic value
0
1
2
3
...
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(cond)
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
New Approach
Loop Unswitching (LU)
while(n > 0) {
if(cond)
A
else
B
}
if(freeze(cond))
while(n > 0) {
{ A }
else
while(n > 0) {
{ B }
poison
Nondet.
True / False
New Approach
if( poison )
if(freeze( poison ))
Global Value Numbering
Nondeterministic
UB
Using freeze intruction
Loop Unswitching
Problem solved!
y = x * 2
Using freeze instruction
Without freeze
y = x + x
Not exactly true, Details are missing
y = x * 2
Using freeze instruction
y = x + x
undef
{ 0, 2, 4, 6 ... }
undef
{ 0, 1, 2, 3 ... }
Without freeze
y = x * 2
Using freeze instruction
x' = freeze x
y = x' + x'
undef
{ 0, 2, 4, 6 ... }
Nondet.
{ 0, 2, 4, 6 ... }
Using freeze
{ 0, 1, 2, 3 ... }
Using freeze instruction
a = x / y
b = x % y
a = x / y
b = x - (a * y)
New Approach
undef 1 undef
Using freeze instruction
a = x / y
b = x % y
a = x / y
b = x - (a * y)
New Approach
undef 1 undef
undef undef
a = x / y
b = x - (a * y)
Using freeze instruction
a = x / y
b = x % y
New Approach
undef 1
undef undef
undef
Using freeze instruction
a = x / y
b = x % y
x' = freeze x
a = x' / y
b = x' - (a * y)
Let's assume x is N
N
undef 1
undef
Using freeze instruction
a = x / y
b = x % y
x' = freeze x
a = x' / y
b = x' - (a * y)
Let's assume x is N
N
undef 1
N
1
undef
Using freeze instruction
a = x / y
b = x % y
x' = freeze x
a = x' / y
b = x' - (a * y)
Let's assume x is N
N
undef 1
N
1
N
undef
Using freeze instruction
a = x / y
b = x % y
x' = freeze x
a = x' / y
b = N - N = 0
Let's assume x is N
N
undef 1
N
1
undef
Using freeze instruction
a = x / y
b = x % y
x' = freeze x
a = x' / y
b = 0
Let's assume x is N
N
undef 1
N
1
undef
• 2020 LLVM Developers’ Meeting: J. Lee “Undef and Poison: Present and
Future"
• What Every C Programmer Should Know About Unde
fi
ned Behavior
• Taming Unde
fi
ned Behavior in LLVM
Ad

More Related Content

Similar to LLVM UB Optimization (7)

Deep learning simplified
Deep learning simplifiedDeep learning simplified
Deep learning simplified
Lovelyn Rose
 
Action Recognition (Thesis presentation)
Action Recognition (Thesis presentation)Action Recognition (Thesis presentation)
Action Recognition (Thesis presentation)
nikhilus85
 
Chapter_02_The_Language_of_Bits_Any.pptx
Chapter_02_The_Language_of_Bits_Any.pptxChapter_02_The_Language_of_Bits_Any.pptx
Chapter_02_The_Language_of_Bits_Any.pptx
duttnikhil2403
 
B sc cs i bo-de u-ii logic gates
B sc cs i bo-de u-ii logic gatesB sc cs i bo-de u-ii logic gates
B sc cs i bo-de u-ii logic gates
Rai University
 
CDC18 Jin Gyu Lee
CDC18 Jin Gyu LeeCDC18 Jin Gyu Lee
CDC18 Jin Gyu Lee
CDSL_at_SNU
 
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Silent error detection in numerical time stepping schemes (SIAM PP 2014)Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Austin Benson
 
Arithmetic by aniket bhute
Arithmetic by aniket bhuteArithmetic by aniket bhute
Arithmetic by aniket bhute
Aniket Bhute
 
Deep learning simplified
Deep learning simplifiedDeep learning simplified
Deep learning simplified
Lovelyn Rose
 
Action Recognition (Thesis presentation)
Action Recognition (Thesis presentation)Action Recognition (Thesis presentation)
Action Recognition (Thesis presentation)
nikhilus85
 
Chapter_02_The_Language_of_Bits_Any.pptx
Chapter_02_The_Language_of_Bits_Any.pptxChapter_02_The_Language_of_Bits_Any.pptx
Chapter_02_The_Language_of_Bits_Any.pptx
duttnikhil2403
 
B sc cs i bo-de u-ii logic gates
B sc cs i bo-de u-ii logic gatesB sc cs i bo-de u-ii logic gates
B sc cs i bo-de u-ii logic gates
Rai University
 
CDC18 Jin Gyu Lee
CDC18 Jin Gyu LeeCDC18 Jin Gyu Lee
CDC18 Jin Gyu Lee
CDSL_at_SNU
 
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Silent error detection in numerical time stepping schemes (SIAM PP 2014)Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Austin Benson
 
Arithmetic by aniket bhute
Arithmetic by aniket bhuteArithmetic by aniket bhute
Arithmetic by aniket bhute
Aniket Bhute
 

More from 규영 허 (6)

スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
규영 허
 
프로그래머의 뇌
프로그래머의 뇌프로그래머의 뇌
프로그래머의 뇌
규영 허
 
SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기
규영 허
 
Swift for tensorflow
Swift for tensorflowSwift for tensorflow
Swift for tensorflow
규영 허
 
Influencer
InfluencerInfluencer
Influencer
규영 허
 
Chromium에 contribution하기
Chromium에 contribution하기Chromium에 contribution하기
Chromium에 contribution하기
규영 허
 
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
スケーラブル SwiftUI プロジェクトにおける実用的な TCA モジュラー化
규영 허
 
프로그래머의 뇌
프로그래머의 뇌프로그래머의 뇌
프로그래머의 뇌
규영 허
 
SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기SwiftUI와 TCA로 GitHub Search앱 만들기
SwiftUI와 TCA로 GitHub Search앱 만들기
규영 허
 
Swift for tensorflow
Swift for tensorflowSwift for tensorflow
Swift for tensorflow
규영 허
 
Chromium에 contribution하기
Chromium에 contribution하기Chromium에 contribution하기
Chromium에 contribution하기
규영 허
 
Ad

Recently uploaded (20)

Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Ad

LLVM UB Optimization

  • 1. LLVM UB and Optimization 허규영 Software Engineer https://twitter.com/bbvch13531 Using freeze instruction
  • 2. • What is Unde fi ned Behavior • UB and Optimization • Poison Value • Freeze Instruction
  • 3. UB(Unde fi ned Behavior) • Implementation De fi ned Behavior: 문서에 의해 정의된 동작 • Unspeci fi ed Behavior: 명시되지 않은 동작 • Unde fi ned Behavior
  • 4. UB(Unde fi ned Behavior) • Implementation De fi ned • Unspeci fi ed Behavior • Unde fi ned Behavior 컴파일러가 어떻게 동작할지 몰?루
  • 5. Example of UB program
  • 6. 2021년 정기 2회 정보처리기능사 실기문제 K O R E A 0 1 2 3 4 Access out of bounds Memory Protection Violation str
  • 11. 다른 정보처리기능사 문제 1. 55 2. 77 3. 121 4. 132 sum += *(p + i) ; 11 0 22 1 44 55 0 1 P Access out of bounds
  • 12. 모 과학고등학교의 정보수행평가 문제 --y와 w+x+y-z 중 어떤 것이 먼저 실행될까요? Sequence point rules Order of evaluation Between the previous and next sequence point a scalar object must have its stored value modi fi ed at most once by the evaluation of an expression, Otherwise the behavior is unde fi ned.
  • 13. How UB and Optimization related
  • 14. output( p + a > p + b ) Peephole Optimization output( a > b ) Optimize int* p int a int b
  • 15. output( p + a > p + b ) Peephole Optimization output( a > b ) Optimize 0xFFFFFF00 int* p 0x100 int a 0x0 int b
  • 16. output( p + a > p + b ) Peephole Optimization output( a > b ) Optimize 0xFFFFFF00 int* p 0x100 int a 0x0 int b 0x0 False True 0x100 > 0x0 Over fl ow!
  • 17. output( p + a > p + b ) Peephole Optimization output( a > b ) Optimize 0xFFFFFF00 int* p 0x100 int a 0x0 int b False True 0x100 > 0x0 Miscompilation before optimize after optimize
  • 18. output( p + a > p + b ) Peephole Optimization output( a > b ) Optimize Pointer Arithmetic Over fl ow is Unde fi ned Behavior UB 0x0 Over fl ow!
  • 19. Why poison value is needed
  • 20. Loop Invariant Code Motion ... for(i = 0; i < n; i++){ a[i] = p + 0x100 } q = p + 0x100 for(i = 0; i < n; i++){ a[i] = q }
  • 21. Loop Invariant Code Motion ... for(i = 0; i < n; i++){ a[i] = p + 0x100 } q = p + 0x100 for(i = 0; i < n; i++){ a[i] = q } 0xFFFFFF00 p
  • 22. Loop Invariant Code Motion ... for(i = 0; i < n; i++){ a[i] = p + 0x100 } q = p + 0x100 for(i = 0; i < n; i++){ a[i] = q } UB 0x0 Over fl ow!
  • 23. Loop Invariant Code Motion ... for(i = 0; i < n; i++){ a[i] = p + 0x100 } q = p + 0x100 for(i = 0; i < n; i++){ a[i] = q } Miscompilation Correct Program Wrong Program when n = 0 UB
  • 24. ... for(i = 0; i < n; i++){ a[i] = p + 0x100 } q = p + 0x100 for(i = 0; i < n; i++){ a[i] = q } Poison Value poison when n = 0
  • 25. De fi nition of Poison Value poison is a special value that represents a violation of assumption Each operation on poison value propergate poison or raise UB
  • 26. Poison Propagation p a p b 0xFFFFFF00 0x100 poison 0xFFFFFF00 0x0 poison output UB Overflow!
  • 28. Global Value Numbering (GVN) if(x == y) { ... use x ... } else { ... } if(x == y) { ... use y ... } else { ... } How to de fi ne when branching on poison value
  • 29. Global Value Numbering (GVN) if(x == y) { ... use x ... } else { ... } if(x == y) { ... use y ... } else { ... } How to de fi ne when branching on poison value 0 poison
  • 30. Global Value Numbering (GVN) if(x == y) { ... use x ... } else { ... } if(x == y) { ... use y ... } else { ... } How to de fi ne when branching on poison value 0 poison Miscompilation Correct Program Wrong Program
  • 31. Global Value Numbering (GVN) if(x == y) { ... use x ... } else { ... } if(x == y) { ... use y ... } else { ... } How to de fi ne when branching on poison value poison
  • 32. Global Value Numbering (GVN) if(x == y) { ... use x ... } else { ... } if(x == y) { ... use y ... } else { ... } Branching on poison value is Unde fi ned Behavior UB
  • 33. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(cond) while(n > 0) { { A } else while(n > 0) { { B }
  • 34. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(cond) while(n > 0) { { A } else while(n > 0) { { B } poison UB Branching on poison is
  • 35. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(cond) while(n > 0) { { A } else while(n > 0) { { B } poison UB Branching on poison is False when n = 0
  • 36. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(cond) while(n > 0) { { A } else while(n > 0) { { B } poison UB Branching on poison is False when n = 0 Miscompilation Correct Program Wrong Program
  • 37. Global Value Numbering (GVN) Branching on poison value is Unde fi ned Behavior
  • 38. GVN + LU Branching on poison value is Not Always Unde fi ned Behavior
  • 39. GVN + LU is inconsistent
  • 40. Inconsistency in LLVM • SNU found miscompilation bug in LLVM when GVN+LU optimization • Google reported a real-world bug due to GVN+LU inconsistance
  • 41. Handle UB using freeze
  • 42. Freeze instruction y = freeze x • if x is de fi ned value • if x is poison value or undef freeze x -> x freeze x -> which y is nondeterministic value 0 1 2 3 ...
  • 43. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(cond) while(n > 0) { { A } else while(n > 0) { { B } New Approach
  • 44. Loop Unswitching (LU) while(n > 0) { if(cond) A else B } if(freeze(cond)) while(n > 0) { { A } else while(n > 0) { { B } poison Nondet. True / False New Approach
  • 45. if( poison ) if(freeze( poison )) Global Value Numbering Nondeterministic UB Using freeze intruction Loop Unswitching
  • 47. y = x * 2 Using freeze instruction Without freeze y = x + x Not exactly true, Details are missing
  • 48. y = x * 2 Using freeze instruction y = x + x undef { 0, 2, 4, 6 ... } undef { 0, 1, 2, 3 ... } Without freeze
  • 49. y = x * 2 Using freeze instruction x' = freeze x y = x' + x' undef { 0, 2, 4, 6 ... } Nondet. { 0, 2, 4, 6 ... } Using freeze { 0, 1, 2, 3 ... }
  • 50. Using freeze instruction a = x / y b = x % y a = x / y b = x - (a * y) New Approach undef 1 undef
  • 51. Using freeze instruction a = x / y b = x % y a = x / y b = x - (a * y) New Approach undef 1 undef undef undef
  • 52. a = x / y b = x - (a * y) Using freeze instruction a = x / y b = x % y New Approach undef 1 undef undef undef
  • 53. Using freeze instruction a = x / y b = x % y x' = freeze x a = x' / y b = x' - (a * y) Let's assume x is N N undef 1 undef
  • 54. Using freeze instruction a = x / y b = x % y x' = freeze x a = x' / y b = x' - (a * y) Let's assume x is N N undef 1 N 1 undef
  • 55. Using freeze instruction a = x / y b = x % y x' = freeze x a = x' / y b = x' - (a * y) Let's assume x is N N undef 1 N 1 N undef
  • 56. Using freeze instruction a = x / y b = x % y x' = freeze x a = x' / y b = N - N = 0 Let's assume x is N N undef 1 N 1 undef
  • 57. Using freeze instruction a = x / y b = x % y x' = freeze x a = x' / y b = 0 Let's assume x is N N undef 1 N 1 undef
  • 58. • 2020 LLVM Developers’ Meeting: J. Lee “Undef and Poison: Present and Future" • What Every C Programmer Should Know About Unde fi ned Behavior • Taming Unde fi ned Behavior in LLVM