1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/checkpoint-restore/go-criu/v7/crit"
"github.com/checkpoint-restore/go-criu/v7/crit/cli"
"github.com/checkpoint-restore/go-criu/v7/crit/images/pstree"
)
const loopTestImgDir = "test-imgs/loop"
var pageSize = os.Getpagesize()
func main() {
// Get list of image files
imgs, err := getImgs()
if err != nil {
log.Fatal(err)
}
// Run recode test
if err = recodeImgs(imgs); err != nil {
log.Fatal(err)
}
// Run test for memory pages reading features
if err = readMemoryPages(); err != nil {
log.Fatal(err)
}
// Run test for process shared memory size
if err = testGetShmemSize(); err != nil {
log.Fatal(err)
}
log.Println("=== PASS")
}
func recodeImgs(imgs []string) error {
for _, img := range imgs {
log.Println("===", img)
imgFile, err := os.Open(img)
if err != nil {
return err
}
defer imgFile.Close()
testImg := img + ".test.img"
testImgFile, err := os.Create(testImg)
if err != nil {
return err
}
defer testImgFile.Close()
c := crit.New(imgFile, testImgFile, "", false, false)
entryType, err := cli.GetEntryTypeFromImg(imgFile)
if err != nil {
return err
}
// Decode the binary image file
decodedImg, err := c.Decode(entryType)
if err != nil {
return errors.New(fmt.Sprint("[DECODE]: ", err))
}
// Encode it into test binary image file
if err = c.Encode(decodedImg); err != nil {
return errors.New(fmt.Sprint("[ENCODE]: ", err))
}
// Open and compare original and test files
imgBytes, err := os.ReadFile(img)
if err != nil {
return err
}
testImgBytes, err := os.ReadFile(testImg)
if err != nil {
return err
}
if !bytes.Equal(imgBytes, testImgBytes) {
return errors.New("[RECODE]: Files do not match")
}
}
return nil
}
func getImgs() ([]string, error) {
// Certain image files generated by CRIU do not
// use the protobuf format and contain raw binary
// data. Some image files are also generated using
// external tools (ifaddr, route, tmpfs). As these
// images cannot be processed by CRIT, they are
// excluded from the tests.
skipImgs := []string{
"pages-",
"pages-shmem-",
"iptables-",
"ip6tables-",
"nftables-",
"route-",
"route6-",
"ifaddr-",
"tmpfs-",
"tmpfs-dev-",
"autofs-",
"netns-ct-",
"netns-exp-",
"rule-",
}
// "*.test.img", "*.json.img" or "tmp.*.img" files
// must be skipped as they are generated by tests
criuImg := regexp.MustCompile(`^[^\.]*\.img$`)
dir, err := filepath.Glob(loopTestImgDir + "/*.img")
if err != nil {
return nil, err
}
var imgs []string
nextFile:
for _, file := range dir {
if filepath.Ext(file) == ".img" {
if !criuImg.MatchString(file) {
continue
}
for _, skip := range skipImgs {
if strings.HasPrefix(filepath.Base(file), skip) {
continue nextFile
}
}
imgs = append(imgs, file)
}
}
return imgs, nil
}
// readMemoryPages reads and compares process arguments
// and environment variables from memory pages and corresponding test files.
func readMemoryPages() error {
pid, err := getTestImgPID(loopTestImgDir)
if err != nil {
return err
}
mr, err := crit.NewMemoryReader(loopTestImgDir, pid, pageSize)
if err != nil {
return err
}
// Retrieve process arguments from memory pages
argsBuff, err := mr.GetPsArgs()
if err != nil {
return err
}
// Read process environment variables from the environ test file
testFileArgs, err := os.ReadFile(filepath.Join(loopTestImgDir, "cmdline"))
if err != nil {
return err
}
if !bytes.Equal(testFileArgs, argsBuff.Bytes()) {
return errors.New("process arguments do not match")
}
// Retrieve process environment variables from memory pages
envVarsBuffer, err := mr.GetPsEnvVars()
if err != nil {
return err
}
// Read process environment variables from the environ test file
envVarsTestFile, err := os.ReadFile(filepath.Join(loopTestImgDir, "environ"))
if err != nil {
return err
}
if !bytes.Equal(envVarsTestFile, envVarsBuffer.Bytes()) {
return errors.New("process environment variables do not match")
}
return nil
}
// testGetShmemSize tests the GetShmemSize method of the MemoryReader struct.
func testGetShmemSize() error {
testCases := []struct {
imgsDir string
expectedShmemSize int64
}{
{
imgsDir: loopTestImgDir,
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_p_257b",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_pa_257b",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_s_257b",
// Here and in the next test case, the expected shared memory size is set
// to the value of pageSize because the Linux kernel uses page-aligned
// addresses for memory mapping, so when memory size is less than a page size,
// it aligns the mapping to a single page.
expectedShmemSize: int64(pageSize),
},
{
imgsDir: "test-imgs/mm_sa_257b",
expectedShmemSize: int64(pageSize),
},
{
imgsDir: "test-imgs/mm_p_4kb",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_pa_4kb",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_s_4kb",
expectedShmemSize: 4096,
},
{
imgsDir: "test-imgs/mm_sa_4kb",
expectedShmemSize: 4096,
},
{
imgsDir: "test-imgs/mm_p_32kb",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_pa_32kb",
expectedShmemSize: 0,
},
{
imgsDir: "test-imgs/mm_s_32kb",
expectedShmemSize: 32768,
},
{
imgsDir: "test-imgs/mm_sa_32kb",
expectedShmemSize: 32768,
},
}
for _, test := range testCases {
pid, err := getTestImgPID(test.imgsDir)
if err != nil {
return err
}
mr, err := crit.NewMemoryReader(test.imgsDir, pid, pageSize)
if err != nil {
return err
}
shmemSize, err := mr.GetShmemSize()
if err != nil {
return err
}
if shmemSize != test.expectedShmemSize {
return fmt.Errorf(
"[%s]: expected shared memory size: %d bytes, actual size: %d bytes",
test.imgsDir, test.expectedShmemSize, shmemSize,
)
}
}
return nil
}
func getTestImgPID(dir string) (uint32, error) {
psTreeFile, err := os.Open(filepath.Join(dir, "pstree.img"))
if err != nil {
return 0, err
}
defer psTreeFile.Close()
c := crit.New(psTreeFile, nil, loopTestImgDir, false, true)
psTreeImg, err := c.Decode(&pstree.PstreeEntry{})
if err != nil {
return 0, err
}
pid := psTreeImg.Entries[0].Message.(*pstree.PstreeEntry).GetPid()
return pid, nil
}
|