conditional compilation is dead, long live conditional ... · map preprocessor usage to c • macro...

17
Paul Gazzillo and Shiyi Wei ICSE-NIER 2019 Conditional Compilation Is Dead, Long Live Conditional Compilation! @paul_gazzillo pgazz.com

Upload: others

Post on 18-Aug-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

PaulGazzillo andShiyiWeiICSE-NIER2019

ConditionalCompilationIsDead,LongLiveConditionalCompilation!

@paul_gazzillo pgazz.com🔗

Page 2: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

TheCPreprocessorCreatesaDilemma

• ConditionalcompilationimplementedwiththeCpreprocessor• Reallygreatforperformance• Reallybadforsoftwaretools

2

Page 3: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

Goal:ReplacePreprocessorUsagewithCItself

• Easierforsoftwaretools• PreserveexistingCsoftware• How?

• NewClanguageconstructs• Automateconditionalcompilationwithcompileroptimizations

3

Page 4: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

CUseHasGrown!

4

IEEESpectrumPopularityRankings,May2019

IoTDeveloperSurvey,EclipseFoundation2019

TIOBEPopularityRankings,May2019

Page 5: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

ConditionalCompilationMakesReusePossible

• Linuxconfigurabletomanydevices• Noextraprogrammingneeded

5

Linux IoT devices in our home

7

Linux

Page 6: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

CPreprocessorUsedExtensively

• Macrosusedabout1in4SLoC,ingeneral[Ernstetal1999]• Linuxv4.19(late2018)

• Developersuseittohand-optimizeobjectfilesize• CompilingallLinuxfeatureswouldmakeanenormousbinary

6

Sourcelinesofcode about12million

Preprocessormacrosdefined about1million

Preprocessordirectivesused about2million

Preprocessorconditionalblocks about60,000

Page 7: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

ConditionalCompilationIsImplementedwiththePreprocessor

7

void irq_add(int *ops) {int irq = *ops;

}

int *ops = NULL;

ops = &irq_ops;

irq_add(ops);

#ifdef CONFIG_OF_IRQ_DOMAIN

#endif

#ifdef CONFIG_OF_IRQ

#endif

Configurationoptionstestedatbuild-time

Page 8: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

VariabilityBugs:ExistenceDependsonConfigurationSettings

8

void irq_add(int *ops) {int irq = *ops;

}

int *ops = NULL;

ops = &irq_ops;

irq_add(ops);

#ifdef CONFIG_OF_IRQ_DOMAIN

#endif

#ifdef CONFIG_OF_IRQ

#endif

3.Nullpointererror insomeconfigurations

1.Initialize“ops”pointer

2.Onlysetinsomeconfigurations

Onlycertainconfigurationshavebugs

Page 9: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

WhyDon’tWeJustUsea“Better”Language?

9

• Millions(billions?)ofSLoCinactive,widely-usedprojects

• RustandGowill(hopefully)supplantC,but…• Rusthasconfigurationmacros

• #[cfg]attributes• Gohasbuildconstraints

Page 10: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

GreatResearchEffortsTacklingConditionalCompilation

• Newlanguage• Captureconditionalcompilationasvariability

• Similarchallengesforanalysistools

10

• “Lift”analysestoallconfigurations• State-of-the-artisintraproceduraldataflow

• MuchlefttomatchpureCtools,e.g.,• Points-toanalysis• Abstractinterpretation• Modelchecking• Separationlogic• Symbolicexecution

Page 11: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

BestofBothWorlds:KeepCandAutomateConditionalCompilation

• Replacepreprocessorwithanewcompilerphase• Configurationmacros->programvalues• Conditionalcompilationbecomescompileroptimization

• Constantprop+deadcodeelimination=#ifdef

11

Page 12: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

WhataretheConstructsoftheCombinedLanguage?

• Formalsemanticstypicallyreliesonwell-definedabstractsyntax• ThecombinedC/preprocessorlanguagehaswonkysyntax

• Someusageshouldprobablyberestricted

• Whatarethesemanticsofthecombinedlanguage?• CMod formallydefined#includeusage[Srivastavaetal.,TSE2008]

12

#define LBRACE {int main() LBRACE}

Page 13: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

MapPreprocessorUsagetoC

• Macro->programvariable• #ifdef->Cconditional• Conditionalcompilation->deadcodeelimination• Transformationhasbeendonebefore[Iosif-Lazaretal.,Sci.Prog. 2017]

13

int *ops = NULL;#ifdef CONFIG_OF_IRQops = &irq_ops;#endifirq_add(ops);

bool CONFIG_OF_IRQ;int *ops = NULL;if (CONFIG_OF_IRQ) {ops = &irq_ops;}irq_add(ops);

Page 14: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

SomeConstructsAreQuestionable

• Codeduplicationisawkward• Couldalterconditionstohaveonlytwobranches

• Shouldsuchcasesbeprohibited?

14

#ifdef CONFIG_PSAUXif (imajor == 10)

i = 31;else

#endifi = iminor – 32;

bool CONFIG_PSAUX;if (CONFIG_PSAUX) {

if (imajor == 10)i = 31;

elsei = iminor – 32;

} else {i = iminor – 32;

}

Page 15: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

#ifdefsCanAppearAroundDeclarations

• #ifdefsfrequentlysurrounddeclarationsanddefinitions• Akintoadependenttype

• Typeandexistenceof"quota"dependsonprogramvariable

• Similarityobservedbeforein[Chenetal.,TOPLAS2014]

15

struct {u16 size;

#ifdef CONFIG_QUOTAint quota;

#endif}

bool CONFIG_QUOTA;struct {u16 size;

int __attribute__((config (CONFIG_SMP))) quota;

}

Page 16: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->

Conclusion

• Preprocessordilemma• Greatforperformance• Badfortools

• Goal:ReplacepreprocessorusagewithCitself• Automateconditionalcompilation• Extensionsforsomepreprocessorusecases

• Futurework• Languagedefinition:Whataretherightconstructs?Whatshouldbeillegal?• Empiricalevaluationofhowoftentranslationispossible• Newcompilerphaseandoptimizations

16

@paul_gazzillo pgazz.com🔗

Page 17: Conditional Compilation Is Dead, Long Live Conditional ... · Map Preprocessor Usage to C • Macro -> program variable • #ifdef -> C conditional • Conditional compilation ->