runtime_strip.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2020, The Garble Authors.
  2. // See LICENSE for licensing information.
  3. package main
  4. import (
  5. "go/ast"
  6. "strings"
  7. ah "mvdan.cc/garble/internal/asthelper"
  8. )
  9. // stripRuntime removes unnecessary code from the runtime,
  10. // such as panic and fatal error printing, and code that
  11. // prints trace/debug info of the runtime.
  12. func stripRuntime(filename string, file *ast.File) {
  13. stripPrints := func(node ast.Node) bool {
  14. call, ok := node.(*ast.CallExpr)
  15. if !ok {
  16. return true
  17. }
  18. id, ok := call.Fun.(*ast.Ident)
  19. if !ok {
  20. return true
  21. }
  22. switch id.Name {
  23. case "print", "println":
  24. id.Name = "hidePrint"
  25. return false
  26. default:
  27. return true
  28. }
  29. }
  30. for _, decl := range file.Decls {
  31. funcDecl, ok := decl.(*ast.FuncDecl)
  32. if !ok {
  33. continue
  34. }
  35. switch filename {
  36. case "error.go":
  37. // only used in panics
  38. switch funcDecl.Name.Name {
  39. case "printany", "printanycustomtype":
  40. funcDecl.Body.List = nil
  41. }
  42. case "mgcscavenge.go":
  43. // used in tracing the scavenger
  44. if funcDecl.Name.Name == "printScavTrace" {
  45. funcDecl.Body.List = nil
  46. }
  47. case "mprof.go":
  48. // remove all functions that print debug/tracing info
  49. // of the runtime
  50. if strings.HasPrefix(funcDecl.Name.Name, "trace") {
  51. funcDecl.Body.List = nil
  52. }
  53. case "panic.go":
  54. // used for printing panics
  55. switch funcDecl.Name.Name {
  56. case "preprintpanics", "printpanics":
  57. funcDecl.Body.List = nil
  58. }
  59. case "print.go":
  60. // only used in tracebacks
  61. if funcDecl.Name.Name == "hexdumpWords" {
  62. funcDecl.Body.List = nil
  63. }
  64. case "proc.go":
  65. // used in tracing the scheduler
  66. if funcDecl.Name.Name == "schedtrace" {
  67. funcDecl.Body.List = nil
  68. }
  69. case "runtime1.go":
  70. usesEnv := func(node ast.Node) bool {
  71. seen := false
  72. ast.Inspect(node, func(node ast.Node) bool {
  73. ident, ok := node.(*ast.Ident)
  74. if ok && ident.Name == "gogetenv" {
  75. seen = true
  76. return false
  77. }
  78. return true
  79. })
  80. return seen
  81. }
  82. filenames:
  83. switch funcDecl.Name.Name {
  84. case "parsedebugvars":
  85. // keep defaults for GODEBUG cgocheck and invalidptr,
  86. // remove code that reads GODEBUG via gogetenv
  87. for i, stmt := range funcDecl.Body.List {
  88. if usesEnv(stmt) {
  89. funcDecl.Body.List = funcDecl.Body.List[:i]
  90. break filenames
  91. }
  92. }
  93. panic("did not see any gogetenv call in parsedebugvars")
  94. case "setTraceback":
  95. // tracebacks are completely hidden, no
  96. // sense keeping this function
  97. funcDecl.Body.List = nil
  98. }
  99. case "traceback.go":
  100. // only used for printing tracebacks
  101. switch funcDecl.Name.Name {
  102. case "tracebackdefers", "printcreatedby", "printcreatedby1", "traceback", "tracebacktrap", "traceback1", "printAncestorTraceback",
  103. "printAncestorTracebackFuncInfo", "goroutineheader", "tracebackothers", "tracebackHexdump", "printCgoTraceback":
  104. funcDecl.Body.List = nil
  105. case "printOneCgoTraceback":
  106. funcDecl.Body = ah.BlockStmt(ah.ReturnStmt(ah.IntLit(0)))
  107. default:
  108. if strings.HasPrefix(funcDecl.Name.Name, "print") {
  109. funcDecl.Body.List = nil
  110. }
  111. }
  112. }
  113. }
  114. if filename == "print.go" {
  115. file.Decls = append(file.Decls, hidePrintDecl)
  116. return
  117. }
  118. // replace all 'print' and 'println' statements in
  119. // the runtime with an empty func, which will be
  120. // optimized out by the compiler
  121. ast.Inspect(file, stripPrints)
  122. }
  123. var hidePrintDecl = &ast.FuncDecl{
  124. Name: ast.NewIdent("hidePrint"),
  125. Type: &ast.FuncType{Params: &ast.FieldList{
  126. List: []*ast.Field{{
  127. Names: []*ast.Ident{{Name: "args"}},
  128. Type: &ast.Ellipsis{Elt: &ast.InterfaceType{
  129. Methods: &ast.FieldList{},
  130. }},
  131. }},
  132. }},
  133. Body: &ast.BlockStmt{},
  134. }