main.mx raw

   1  // The client half of the network smoke phase.
   2  //
   3  // Three modes, and the difference between them is the point:
   4  //
   5  //	client <url>            the single-domain client: resolve a host name, dial
   6  //	                        the resolved address, write the request, read the
   7  //	                        response. No transport threads, no channels, no
   8  //	                        spawn - the deterministic shape.
   9  //	client --loop N <url>   the same exchange N times on ONE connection, which
  10  //	                        is the keep-alive path and the no-accumulation
  11  //	                        probe: the phase compares the server's RSS before
  12  //	                        and after it.
  13  //	client --get <url>      the same exchange through http.Get, i.e. through
  14  //	                        net/http's Transport. That path is BLOCKED by a
  15  //	                        client defect of its own (the work log):
  16  //	                        dialConn calls readLoop and only then writeLoop, so
  17  //	                        it connects and waits forever for a response to a
  18  //	                        request that is never written. phase8 runs this form
  19  //	                        as an inverted canary: it must FAIL while the
  20  //	                        blocker is real, and it flips to a failure of its
  21  //	                        own the day it succeeds.
  22  //
  23  // The lookup goes through the hosts file (the static-host path, which used to
  24  // SIGSEGV before the root-arena fix in src/net/hosts.mx), and the request goes
  25  // to the address that lookup returned.
  26  package main
  27  
  28  import (
  29  	"bufio"
  30  	"bytes"
  31  	"fmt"
  32  	"io"
  33  	"net"
  34  	"net/http"
  35  	"os"
  36  	"strconv"
  37  )
  38  
  39  // splitURL pulls the authority and the request path out of
  40  // http://host:port/path without a url round trip; the phase only passes that.
  41  func splitURL(u string) (authority, path string) {
  42  	rest := u
  43  	if bytes.HasPrefix(rest, "http://") {
  44  		rest = rest[7:]
  45  	}
  46  	i := bytes.IndexByte(rest, '/')
  47  	if i < 0 {
  48  		return rest, "/"
  49  	}
  50  	return rest[:i], rest[i:]
  51  }
  52  
  53  // splitAuthority separates host:port.
  54  func splitAuthority(a string) (host, port string) {
  55  	i := int32(-1)
  56  	for j := int32(len(a)) - 1; j >= 0; j-- {
  57  		if a[j] == ':' {
  58  			i = j
  59  			break
  60  		}
  61  	}
  62  	if i < 0 {
  63  		return a, "80"
  64  	}
  65  	return a[:i], a[i+1:]
  66  }
  67  
  68  // firstIPv4 picks the first dotted address from a lookup result, so the request
  69  // goes to an address the resolver produced.
  70  func firstIPv4(addrs [][]byte) (pick string) {
  71  	for _, a := range addrs {
  72  		if bytes.IndexByte(a, '.') >= 0 {
  73  			return a
  74  		}
  75  	}
  76  	if len(addrs) > 0 {
  77  		return addrs[0]
  78  	}
  79  	return ""
  80  }
  81  
  82  // resolveTarget resolves a name and returns host:port plus the path, printing
  83  // what the resolver said.
  84  func resolveTarget(url string) (target, path string) {
  85  	authority, p := splitURL(url)
  86  	host, port := splitAuthority(authority)
  87  	name := host
  88  	if bytes.IndexByte(name, '.') >= 0 {
  89  		// The URL names a literal; still resolve a name, so the resolver is
  90  		// exercised and the request goes to what it returned.
  91  		name = "localhost"
  92  	}
  93  	addrs, lerr := net.LookupHost(name)
  94  	if lerr != nil {
  95  		fmt.Printf("lookup failed: %v\n", lerr)
  96  		os.Exit(1)
  97  	}
  98  	if len(addrs) == 0 {
  99  		fmt.Printf("lookup returned no addresses\n")
 100  		os.Exit(1)
 101  	}
 102  	fmt.Printf("lookup %s -> %d address(es)\n", name, len(addrs))
 103  	for _, a := range addrs {
 104  		fmt.Printf("  addr=%s\n", a)
 105  	}
 106  	pick := firstIPv4(addrs)
 107  	if pick == "" {
 108  		fmt.Printf("no usable address\n")
 109  		os.Exit(1)
 110  	}
 111  	return pick | ":" | port, p
 112  }
 113  
 114  // syncGet is the single-domain exchange: resolve, dial, write, read.
 115  func syncGet(url string) {
 116  	target, path := resolveTarget(url)
 117  
 118  	c, derr := net.Dial("tcp", target)
 119  	if derr != nil {
 120  		fmt.Printf("dial %s failed: %v\n", target, derr)
 121  		os.Exit(1)
 122  	}
 123  	defer c.Close()
 124  
 125  	req, rerr := http.NewRequest("GET", "http://" | target | path, nil)
 126  	if rerr != nil {
 127  		fmt.Printf("new request failed: %v\n", rerr)
 128  		os.Exit(1)
 129  	}
 130  	if werr := req.Write(c); werr != nil {
 131  		fmt.Printf("write request failed: %v\n", werr)
 132  		os.Exit(1)
 133  	}
 134  
 135  	resp, sperr := http.ReadResponse(bufio.NewReader(c), req)
 136  	if sperr != nil {
 137  		fmt.Printf("read response failed: %v\n", sperr)
 138  		os.Exit(1)
 139  	}
 140  	body, berr := io.ReadAll(resp.Body)
 141  	if berr != nil {
 142  		fmt.Printf("read body failed: %v\n", berr)
 143  		os.Exit(1)
 144  	}
 145  	fmt.Printf("status=%d\n", resp.StatusCode)
 146  	fmt.Printf("body=%s", body)
 147  
 148  	want := "hello path=" | path | "\n"
 149  	if !bytes.Equal(body, want) {
 150  		fmt.Printf("body mismatch: wanted %s\n", want)
 151  		os.Exit(1)
 152  	}
 153  	if resp.StatusCode != 200 {
 154  		fmt.Printf("status mismatch: wanted 200\n")
 155  		os.Exit(1)
 156  	}
 157  	fmt.Printf("client ok\n")
 158  }
 159  
 160  // loopGet performs n exchanges on ONE connection: the keep-alive path, and the
 161  // probe for per-request accumulation (the phase reads the server's RSS around
 162  // it).
 163  func loopGet(url string, n int32) {
 164  	target, path := resolveTarget(url)
 165  
 166  	c, derr := net.Dial("tcp", target)
 167  	if derr != nil {
 168  		fmt.Printf("dial %s failed: %v\n", target, derr)
 169  		os.Exit(1)
 170  	}
 171  	defer c.Close()
 172  
 173  	req, rerr := http.NewRequest("GET", "http://" | target | path, nil)
 174  	if rerr != nil {
 175  		fmt.Printf("new request failed: %v\n", rerr)
 176  		os.Exit(1)
 177  	}
 178  	br := bufio.NewReader(c)
 179  	want := "hello path=" | path | "\n"
 180  	bad := int32(0)
 181  	var i int32
 182  	for i = int32(0); i < n; i++ {
 183  		if werr := req.Write(c); werr != nil {
 184  			fmt.Printf("write request %d failed: %v\n", i, werr)
 185  			os.Exit(1)
 186  		}
 187  		resp, sperr := http.ReadResponse(br, req)
 188  		if sperr != nil {
 189  			fmt.Printf("read response %d failed: %v\n", i, sperr)
 190  			os.Exit(1)
 191  		}
 192  		body, berr := io.ReadAll(resp.Body)
 193  		if berr != nil {
 194  			fmt.Printf("read body %d failed: %v\n", i, berr)
 195  			os.Exit(1)
 196  		}
 197  		if resp.StatusCode != 200 || !bytes.Equal(body, want) {
 198  			bad = bad + 1
 199  		}
 200  	}
 201  	fmt.Printf("loop=%d bad=%d\n", n, bad)
 202  	if bad != 0 {
 203  		os.Exit(1)
 204  	}
 205  	fmt.Printf("loop ok\n")
 206  }
 207  
 208  // transportGet is the blocked path, kept reachable so the canary can assert it
 209  // is still blocked rather than letting the defect go quiet.
 210  func transportGet(url string) {
 211  	resp, gerr := http.Get(url)
 212  	if gerr != nil {
 213  		fmt.Printf("get failed: %v\n", gerr)
 214  		os.Exit(1)
 215  	}
 216  	body, berr := io.ReadAll(resp.Body)
 217  	if berr != nil {
 218  		fmt.Printf("read body failed: %v\n", berr)
 219  		os.Exit(1)
 220  	}
 221  	fmt.Printf("status=%d\n", resp.StatusCode)
 222  	fmt.Printf("body=%s", body)
 223  	fmt.Printf("transport ok\n")
 224  }
 225  
 226  func main() {
 227  	// Termination: a batch program must still be stoppable, so the entry
 228  	// handles the channel the runtime closes on SIGINT/SIGTERM.
 229  	select {
 230  	case <-shutdown:
 231  		return
 232  	default:
 233  	}
 234  
 235  	url := "http://127.0.0.1:39128/smoke"
 236  	mode := "sync"
 237  	loopN := int32(0)
 238  	var i int32 = 1
 239  	for i < int32(len(os.Args)) {
 240  		a := os.Args[i]
 241  		if a == "--get" {
 242  			mode = "get"
 243  		} else if a == "--loop" {
 244  			i = i + 1
 245  			if i < int32(len(os.Args)) {
 246  				v, aerr := strconv.Atoi(os.Args[i])
 247  				if aerr == nil {
 248  					loopN = v
 249  				}
 250  			}
 251  		} else {
 252  			url = a
 253  		}
 254  		i = i + 1
 255  	}
 256  	if mode == "get" {
 257  		transportGet(url)
 258  		return
 259  	}
 260  	if loopN > 0 {
 261  		loopGet(url, loopN)
 262  		return
 263  	}
 264  	syncGet(url)
 265  }
 266