How to use init method of compiler Package

Best Syzkaller code snippet using compiler.init

compiler.go

Source:compiler.go Github

copy

Full Screen

...69 SanitizerAttribute llvm.Attribute70 // Importer is the importer. If nil, the compiler will set this field71 // automatically using MakeImporter().72 Importer types.Importer73 // InitMap is the init map used by Importer. If Importer is nil, the74 // compiler will set this field automatically using MakeImporter().75 // If Importer is non-nil, InitMap must be non-nil also.76 InitMap map[*types.Package]gccgoimporter.InitData77 // PackageCreated is a hook passed to the go/loader package via78 // loader.Config, see the documentation for that package for more79 // information.80 PackageCreated func(*types.Package)81 // DisableUnusedImportCheck disables the unused import check performed82 // by go/types if set to true.83 DisableUnusedImportCheck bool84 // Packages is used by go/types as the imported package map if non-nil.85 Packages map[string]*types.Package86}87type Compiler struct {88 opts CompilerOptions89 dataLayout string90}91func NewCompiler(opts CompilerOptions) (*Compiler, error) {92 compiler := &Compiler{opts: opts}93 dataLayout, err := llvmDataLayout(compiler.opts.TargetTriple)94 if err != nil {95 return nil, err96 }97 compiler.dataLayout = dataLayout98 return compiler, nil99}100func (c *Compiler) Compile(fset *token.FileSet, astFiles []*ast.File, importpath string) (m *Module, err error) {101 target := llvm.NewTargetData(c.dataLayout)102 compiler := &compiler{103 CompilerOptions: c.opts,104 dataLayout: c.dataLayout,105 target: target,106 llvmtypes: NewLLVMTypeMap(llvm.GlobalContext(), target),107 }108 return compiler.compile(fset, astFiles, importpath)109}110type compiler struct {111 CompilerOptions112 module *Module113 dataLayout string114 target llvm.TargetData115 fileset *token.FileSet116 runtime *runtimeInterface117 llvmtypes *llvmTypeMap118 types *TypeMap119 debug *debug.DIBuilder120}121func (c *compiler) logf(format string, v ...interface{}) {122 if c.Logger != nil {123 c.Logger.Printf(format, v...)124 }125}126func (c *compiler) addCommonFunctionAttrs(fn llvm.Value) {127 fn.AddTargetDependentFunctionAttr("disable-tail-calls", "true")128 fn.AddTargetDependentFunctionAttr("split-stack", "")129 if c.SanitizerAttribute.GetEnumKind() != 0 {130 fn.AddFunctionAttr(c.SanitizerAttribute)131 }132}133// MakeImporter sets CompilerOptions.Importer to an appropriate importer134// for the search paths given in CompilerOptions.ImportPaths, and sets135// CompilerOptions.InitMap to an init map belonging to that importer.136// If CompilerOptions.GccgoPath is non-empty, the importer will also use137// the search paths for that gccgo installation.138func (opts *CompilerOptions) MakeImporter() error {139 opts.InitMap = make(map[*types.Package]gccgoimporter.InitData)140 if opts.GccgoPath == "" {141 paths := append(append([]string{}, opts.ImportPaths...), ".")142 opts.Importer = gccgoimporter.GetImporter(paths, opts.InitMap)143 } else {144 var inst gccgoimporter.GccgoInstallation145 err := inst.InitFromDriver(opts.GccgoPath)146 if err != nil {147 return err148 }149 opts.Importer = inst.GetImporter(opts.ImportPaths, opts.InitMap)150 }151 return nil152}153func (compiler *compiler) compile(fset *token.FileSet, astFiles []*ast.File, importpath string) (m *Module, err error) {154 buildctx, err := llgobuild.ContextFromTriple(compiler.TargetTriple)155 if err != nil {156 return nil, err157 }158 if compiler.Importer == nil {159 err = compiler.MakeImporter()160 if err != nil {161 return nil, err162 }163 }164 impcfg := &loader.Config{165 Fset: fset,166 TypeChecker: types.Config{167 Packages: compiler.Packages,168 Import: compiler.Importer,169 Sizes: compiler.llvmtypes,170 DisableUnusedImportCheck: compiler.DisableUnusedImportCheck,171 },172 ImportFromBinary: true,173 Build: &buildctx.Context,174 PackageCreated: compiler.PackageCreated,175 }176 // If no import path is specified, then set the import177 // path to be the same as the package's name.178 if importpath == "" {179 importpath = astFiles[0].Name.String()180 }181 impcfg.CreateFromFiles(importpath, astFiles...)182 iprog, err := impcfg.Load()183 if err != nil {184 return nil, err185 }186 program := ssa.Create(iprog, ssa.BareInits)187 mainPkginfo := iprog.InitialPackages()[0]188 mainPkg := program.CreatePackage(mainPkginfo)189 // Create a Module, which contains the LLVM module.190 modulename := importpath191 compiler.module = &Module{Module: llvm.NewModule(modulename), Path: modulename, Package: mainPkg.Object}192 compiler.module.SetTarget(compiler.TargetTriple)193 compiler.module.SetDataLayout(compiler.dataLayout)194 // Create a new translation unit.195 unit := newUnit(compiler, mainPkg)196 // Create the runtime interface.197 compiler.runtime, err = newRuntimeInterface(compiler.module.Module, compiler.llvmtypes)198 if err != nil {199 return nil, err200 }201 mainPkg.Build()202 // Create a struct responsible for mapping static types to LLVM types,203 // and to runtime/dynamic type values.204 compiler.types = NewTypeMap(205 mainPkg,206 compiler.llvmtypes,207 compiler.module.Module,208 compiler.runtime,209 MethodResolver(unit),210 )211 if compiler.GenerateDebug {212 compiler.debug = debug.NewDIBuilder(213 types.Sizes(compiler.llvmtypes),214 compiler.module.Module,215 impcfg.Fset,216 compiler.DebugPrefixMaps,217 )218 defer compiler.debug.Destroy()219 defer compiler.debug.Finalize()220 }221 unit.translatePackage(mainPkg)222 compiler.processAnnotations(unit, mainPkginfo)223 if importpath == "main" {224 compiler.createInitMainFunction(mainPkg)225 } else {226 compiler.module.ExportData = compiler.buildExportData(mainPkg)227 }228 return compiler.module, nil229}230type byPriorityThenFunc []gccgoimporter.PackageInit231func (a byPriorityThenFunc) Len() int { return len(a) }232func (a byPriorityThenFunc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }233func (a byPriorityThenFunc) Less(i, j int) bool {234 switch {235 case a[i].Priority < a[j].Priority:236 return true237 case a[i].Priority > a[j].Priority:238 return false239 case a[i].InitFunc < a[j].InitFunc:240 return true241 default:242 return false243 }244}245func (c *compiler) buildPackageInitData(mainPkg *ssa.Package) gccgoimporter.InitData {246 var inits []gccgoimporter.PackageInit247 for _, imp := range mainPkg.Object.Imports() {248 inits = append(inits, c.InitMap[imp].Inits...)249 }250 sort.Sort(byPriorityThenFunc(inits))251 // Deduplicate init entries. We want to preserve the entry with the highest priority.252 // Normally a package's priorities will be consistent among its dependencies, but it is253 // possible for them to be different. For example, if a standard library test augments a254 // package which is a dependency of 'regexp' (which is imported by every test main package)255 // with additional dependencies, those dependencies may cause the package under test to256 // receive a higher priority than indicated by its init clause in 'regexp'.257 uniqinits := make([]gccgoimporter.PackageInit, len(inits))258 uniqinitpos := len(inits)259 uniqinitnames := make(map[string]struct{})260 for i, _ := range inits {261 init := inits[len(inits)-1-i]262 if _, ok := uniqinitnames[init.InitFunc]; !ok {263 uniqinitnames[init.InitFunc] = struct{}{}264 uniqinitpos--265 uniqinits[uniqinitpos] = init266 }267 }268 uniqinits = uniqinits[uniqinitpos:]269 ourprio := 1270 if len(uniqinits) != 0 {271 ourprio = uniqinits[len(uniqinits)-1].Priority + 1272 }273 if imp := mainPkg.Func("init"); imp != nil {274 impname := c.types.mc.mangleFunctionName(imp)275 uniqinits = append(uniqinits, gccgoimporter.PackageInit{mainPkg.Object.Name(), impname, ourprio})276 }277 return gccgoimporter.InitData{ourprio, uniqinits}278}279func (c *compiler) createInitMainFunction(mainPkg *ssa.Package) {280 int8ptr := llvm.PointerType(c.types.ctx.Int8Type(), 0)281 ftyp := llvm.FunctionType(llvm.VoidType(), []llvm.Type{int8ptr}, false)282 initMain := llvm.AddFunction(c.module.Module, "__go_init_main", ftyp)283 c.addCommonFunctionAttrs(initMain)284 entry := llvm.AddBasicBlock(initMain, "entry")285 builder := llvm.GlobalContext().NewBuilder()286 defer builder.Dispose()287 builder.SetInsertPointAtEnd(entry)288 args := []llvm.Value{llvm.Undef(int8ptr)}289 if !c.GccgoABI {290 initfn := c.module.Module.NamedFunction("main..import")291 if !initfn.IsNil() {292 builder.CreateCall(initfn, args, "")293 }294 builder.CreateRetVoid()295 return296 }297 initdata := c.buildPackageInitData(mainPkg)298 for _, init := range initdata.Inits {299 initfn := c.module.Module.NamedFunction(init.InitFunc)300 if initfn.IsNil() {301 initfn = llvm.AddFunction(c.module.Module, init.InitFunc, ftyp)302 }303 builder.CreateCall(initfn, args, "")304 }305 builder.CreateRetVoid()306}307func (c *compiler) buildExportData(mainPkg *ssa.Package) []byte {308 exportData := importer.ExportData(mainPkg.Object)309 b := bytes.NewBuffer(exportData)310 b.WriteString("v1;\n")311 if !c.GccgoABI {312 return b.Bytes()313 }314 initdata := c.buildPackageInitData(mainPkg)315 b.WriteString("priority ")316 b.WriteString(strconv.Itoa(initdata.Priority))317 b.WriteString(";\n")318 if len(initdata.Inits) != 0 {319 b.WriteString("init")320 for _, init := range initdata.Inits {321 b.WriteRune(' ')322 b.WriteString(init.Name)323 b.WriteRune(' ')324 b.WriteString(init.InitFunc)325 b.WriteRune(' ')326 b.WriteString(strconv.Itoa(init.Priority))327 }328 b.WriteString(";\n")329 }330 return b.Bytes()331}332// vim: set ft=go :...

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello World")4}5import "fmt"6func main() {7 fmt.Println("Hello World")8}9import "fmt"10func main() {11 fmt.Println("Hello World")12}13import "fmt"14func main() {15 fmt.Println("Hello World")16}17import "fmt"18func main() {19 fmt.Println("Hello World")20}21import "fmt"22func main() {23 fmt.Println("Hello World")24}25import "fmt"26func main() {27 fmt.Println("Hello World")28}29import "fmt"30func main() {31 fmt.Println("Hello World")32}33import "fmt"34func main() {35 fmt.Println("Hello World")36}37import "fmt"38func main() {39 fmt.Println("Hello World")40}41import "fmt"42func main() {43 fmt.Println("Hello World")44}45import "fmt"46func main() {47 fmt.Println("Hello World")48}49import "fmt"50func main() {51 fmt.Println("Hello World")52}53import "fmt"54func main() {55 fmt.Println("Hello World")56}57import "fmt"58func main() {59 fmt.Println("Hello World")60}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(runtime.Compiler)4}5import (6func main() {7 fmt.Println(runtime.GOOS)8 fmt.Println(runtime.GOARCH)9}10import (11func main() {12 fmt.Println(runtime.NumCPU())13}14import (15func main() {16 fmt.Println(runtime.NumCgoCall())17}18import (19func main() {20 fmt.Println(runtime.NumGoroutine())21}22import (23func main() {24 fmt.Println(runtime.GOMAXPROCS(4))25}26import (27func main() {28 fmt.Println(runtime.GOEXPERIMENT)29}30import (31func main() {32 fmt.Println(runtime.GO111MODULE)33}34import (35func main() {36 fmt.Println(runtime.GOFLAGS)37}38import (39func main() {40 fmt.Println(runtime.GOGC)41}42import (43func main() {44 fmt.Println(runtime.GODEBUG)45}46import (47func main() {48 fmt.Println(runtime.GOMODCACHE)49}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Go is awesome!")4}5import "fmt"6func main() {7 fmt.Println("Go is awesome!")8}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2type Compiler struct {3}4func (c *Compiler) init() {5}6func main() {7 c = new(Compiler)8 fmt.Println(reflect.TypeOf(c))9 fmt.Println(c.Language)10}11When we uncomment the line c.init() , the output will be:122. init() method with multiple files133. init() method with multiple packages144. init() method with multiple init() methods155. init() method with multiple init() methods in different packages166. init() method with multiple init() methods in different files of same package177. init() method with multiple init() methods in different packages and different files of same package188. init() method with multiple init() methods in different packages and different files of same package and different struct types19If we have multiple init() methods in different packages and different files of same package and different struct types, the init() method will be called in the order the packages are imported in the main.go

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Number of CPU's: ", runtime.NumCPU())4}5import (6func main() {7 fmt.Println("Number of CPU's: ", runtime.NumCPU())8 fmt.Println("Number of Goroutines: ", runtime.NumGoroutine())9}10import (11func main() {12 fmt.Println("Number of CPU's: ", runtime.NumCPU())13 fmt.Println("Number of Goroutines: ", runtime.NumGoroutine())14 go func() {15 fmt.Println("Hello from Go Routine")16 }()17 fmt.Println("Number of Goroutines: ", runtime.NumGoroutine())18}19import (20func main() {21 fmt.Println("Number of CPU's: ", runtime.NumCPU())22 fmt.Println("Number of Goroutines: ", runtime.NumGoroutine())23 go func() {24 fmt.Println("Hello from Go Routine")25 }()26 fmt.Println("Number

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import "fmt"2type compiler struct {3}4func (c compiler) compile() {5 fmt.Println("Compiling the code using",c.name,"version",c.version)6}7func (c compiler) execute() {8 fmt.Println("Executing the code using",c.name,"version",c.version)9}10func main() {11 c := compiler{name: "Go", version: "1.14.1"}12 c.compile()13 c.execute()14}15func (r receiver_type) method_name(parameters) return_type {16}17func (r *receiver_type) method_name(parameters) return_type {18}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, World!")4}5import "fmt"6func main() {7 fmt.Println("Hello, World!")8}9func init() {10 fmt.Println("init called")11}

Full Screen

Full Screen

init

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, World!")4}5Go: init() Method6func init() {7}8import "fmt"9func init() {10 fmt.Println("Hello, World!")11}12func main() {13 fmt.Println("Hello, World!")14}15Go: init() Method with Multiple Files16func init() {17}18import "fmt"19func init() {20 fmt.Println("Hello, World!")21}22func main() {23 fmt.Println("Hello, World!")24}25Go: init() Method with Multiple Files26func init() {27}28import "fmt"29func init() {30 fmt.Println("Hello, World!")31}32func main() {33 fmt.Println("Hello, World!")34}35Go: init() Method with Multiple Files36func init() {37}38import "fmt"39func init() {40 fmt.Println("Hello, World!")41}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful