From patchwork Fri Jul 8 15:46:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 588578 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:bb16:0:0:0:0 with SMTP id jd22csp822132mab; Fri, 8 Jul 2022 09:39:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vvZDdPSxVVwYiG7rwyIOa8SrXFEUJTtG4G45abyqN1uwcuB1ttaZFy/DGnWFfRmgWMmdPM X-Received: by 2002:ac8:5acc:0:b0:31d:296c:e30e with SMTP id d12-20020ac85acc000000b0031d296ce30emr3791820qtd.624.1657298350100; Fri, 08 Jul 2022 09:39:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657298350; cv=none; d=google.com; s=arc-20160816; b=qahDTFy08+RS7Lc9pQppCVZNV7cBS4WlWLgt2DJwq2WVI38zL9g8kiqxI+QMS9A65B ojNZ/5YRoAkOvFmVmK+T9k19Na9OYM6CfQYVR1QA3bGgM32u2+FwDv6YdYyMz/2dqyvb xPK8h3KtmUrrw0BRxR13gzkZBHnHmrGdrNd5LvCJYxzI01RZep5khMX/CGD8zx7CFJJp 6jigdFpHEGIvRj5uGR/T34bQ5Li/SA30/7rfSDsCvTJWFvhlkKSdyOmR8CUlp/8WW5VZ YSVLE21yuMOc7QaELmSNHRFRaWQyOv2UsT/tEKwkl17IQ6I/VDeknEIroMGy5tZVqCMw ox5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gGqem9bZjG2DPsS841S7DVBWN/tLpAM4ih8tuyrEJjA=; b=mEsDGC20lR351R4J2dSf3m84pG8dSxfX30T5I8usWPSNXbdKv2aGtAAqiB5s6PALhm BqT1i4s+UD8EioGQ4MXNuQjjyhrDqYEuYq3bua+GPOTEZvvRraP1wApOqCxdb4HTLESc kK7drKfrgBqfrhCI9oS/NVKnQFl8t6R0M8P4HQJfLOGcA8SOyB+K2OCISA0x/2jp5ZQI KAwe1WYK+VoQGGkt6J6f3L4ts/XNfn6H3zgQwZmkiWwLBJTxjvkqz6e0xhAHMd2fqeSO uSmzZWfF+Dsh2UK8WMz3KfEBuDT5TnzpcDWgbheCBSViXlDgzAqr09b1190WejIxInD9 F1tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U3jCDad5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id gw5-20020a0562140f0500b004704e74849dsi23976346qvb.110.2022.07.08.09.39.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Jul 2022 09:39:10 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=U3jCDad5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50676 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o9r0X-0003Nd-Mb for patch@linaro.org; Fri, 08 Jul 2022 12:39:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o9qD6-0005xM-Fe for qemu-devel@nongnu.org; Fri, 08 Jul 2022 11:48:04 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]:46947) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o9qD4-0002Ws-Lz for qemu-devel@nongnu.org; Fri, 08 Jul 2022 11:48:04 -0400 Received: by mail-pl1-x634.google.com with SMTP id l12so11314275plk.13 for ; Fri, 08 Jul 2022 08:48:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gGqem9bZjG2DPsS841S7DVBWN/tLpAM4ih8tuyrEJjA=; b=U3jCDad59bvy9857Am6RQ1xs4MTmW8+/AIKxX7dWQ0SQBiV+T4yzaca3DbYxXoeF5g wgsKvBUNxqTF7yKC/DswQna4ILw4zPjViYaRtdxgn0m2RFFs/Lr1DdhC6dXsZ8v+t/L8 CF6mzkDBnXB8nMJ/Bkr9vxNcKP7NYPmxkdBcNaePcPGxG4fFVHJv361kzKQk991tXj5B 5zUdQFum1X+awyoZVE0u7UEUuy6d1ixR4UDN0+FGjVbpD1C1Csau8w3T68fGZHNC/rtB 9k8BFCPOiW8QxDk6OSRP0UFUxdQMJkA4R7DRaYjHUqbYgqWpnCeQm9TXeEf2Rl1bmkaP 2Dsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gGqem9bZjG2DPsS841S7DVBWN/tLpAM4ih8tuyrEJjA=; b=jN8rlTA5ucu0mULKHI8jve5Mo87cszvirenGaI87mpptqpIrSsTVOdIgzgpu5vfKQB aSKvplJEIWK2VQBNY865BhRs+1fUfLGXHg9mp9OU4U1tRV6GlnJMa8nJkchtKAT3WNj0 BFJye9/S4N9A44BwO91/wCP6Ar8At9jINKF0su/J4tzblJZwXisSnGq6YoNSK90kQF7J LLcu1xAnGhJiNRjLELJdF9GdYrT/MiwggNictmGUDpr3PPYyPWMCGTmawjppWgETq2Qt 4LmpZge56QQugLSrIoRbwB5tLjMwO+P9hW79Xd5SnHquRYgmzNKyR0ync9zIxK/vY8bi eENw== X-Gm-Message-State: AJIora8S8Mfn4lbUq2lGpkqfVa0KPs/V+LSfWuOqZEf+jnkZGuHgm5ch Am5FV/WKtcP9Ydg0iA3LsHfyoZXJSSrm4zXX X-Received: by 2002:a17:902:d510:b0:16c:3cd:db78 with SMTP id b16-20020a170902d51000b0016c03cddb78mr4309535plg.17.1657295279790; Fri, 08 Jul 2022 08:47:59 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id y3-20020a17090a390300b001ef81bac701sm1782089pjb.42.2022.07.08.08.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 08:47:59 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org Subject: [RISU PATCH v4 17/29] Add magic and size to the trace header Date: Fri, 8 Jul 2022 21:16:48 +0530 Message-Id: <20220708154700.18682-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220708154700.18682-1-richard.henderson@linaro.org> References: <20220708154700.18682-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Sanity check that we're not getting out of sync with the trace stream. This will be especially bad with the change in size of the sve save data. Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- risu.h | 10 +++- risu.c | 162 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 136 insertions(+), 36 deletions(-) diff --git a/risu.h b/risu.h index dd9fda5..bfcf0af 100644 --- a/risu.h +++ b/risu.h @@ -55,7 +55,11 @@ typedef enum { RES_END, RES_MISMATCH_REG, RES_MISMATCH_MEM, + RES_MISMATCH_OP, RES_BAD_IO, + RES_BAD_MAGIC, + RES_BAD_SIZE, + RES_BAD_OP, } RisuResult; /* The memory block should be this long */ @@ -69,10 +73,14 @@ typedef enum { struct reginfo; typedef struct { - uintptr_t pc; + uint32_t magic; + uint32_t size; uint32_t risu_op; + uintptr_t pc; } trace_header_t; +#define RISU_MAGIC (('R' << 24) | ('I' << 16) | ('S' << 8) | 'U') + /* Socket related routines */ int master_connect(int port); int apprentice_connect(const char *hostname, int port); diff --git a/risu.c b/risu.c index 80bc3b1..a248db1 100644 --- a/risu.c +++ b/risu.c @@ -111,32 +111,54 @@ static RisuResult send_register_info(void *uc) uint64_t paramreg; RisuResult res; RisuOp op; + void *extra; reginfo_init(&ri[MASTER], uc); op = get_risuop(&ri[MASTER]); /* Write a header with PC/op to keep in sync */ + header.magic = RISU_MAGIC; header.pc = get_pc(&ri[MASTER]); header.risu_op = op; + + switch (op) { + case OP_TESTEND: + case OP_COMPARE: + case OP_SIGILL: + header.size = reginfo_size(); + extra = &ri[MASTER]; + break; + case OP_COMPAREMEM: + header.size = MEMBLOCKLEN; + extra = memblock; + break; + case OP_SETMEMBLOCK: + case OP_GETMEMBLOCK: + header.size = 0; + extra = NULL; + break; + default: + abort(); + } + res = write_buffer(&header, sizeof(header)); if (res != RES_OK) { return res; } + if (extra) { + res = write_buffer(extra, header.size); + if (res != RES_OK) { + return res; + } + } switch (op) { case OP_COMPARE: - case OP_TESTEND: case OP_SIGILL: - /* - * Do a simple register compare on (a) explicit request - * (b) end of test (c) a non-risuop UNDEF - */ - res = write_buffer(&ri[MASTER], reginfo_size()); - /* For OP_TEST_END, force exit. */ - if (res == RES_OK && op == OP_TESTEND) { - res = RES_END; - } + case OP_COMPAREMEM: break; + case OP_TESTEND: + return RES_END; case OP_SETMEMBLOCK: paramreg = get_reginfo_paramreg(&ri[MASTER]); memblock = (void *)(uintptr_t)paramreg; @@ -145,12 +167,10 @@ static RisuResult send_register_info(void *uc) paramreg = get_reginfo_paramreg(&ri[MASTER]); set_ucontext_paramreg(uc, paramreg + (uintptr_t)memblock); break; - case OP_COMPAREMEM: - return write_buffer(memblock, MEMBLOCKLEN); default: abort(); } - return res; + return RES_OK; } static void master_sigill(int sig, siginfo_t *si, void *uc) @@ -175,22 +195,35 @@ static RisuResult recv_register_info(struct reginfo *ri) return res; } - /* send OK for the header */ - respond(RES_OK); + if (header.magic != RISU_MAGIC) { + /* If the magic number is wrong, we can't trust the rest. */ + return RES_BAD_MAGIC; + } switch (header.risu_op) { case OP_COMPARE: case OP_TESTEND: case OP_SIGILL: - return read_buffer(ri, reginfo_size()); + /* If we can't store the data, report invalid size. */ + if (header.size > sizeof(*ri)) { + return RES_BAD_SIZE; + } + respond(RES_OK); + return read_buffer(ri, header.size); + case OP_COMPAREMEM: + if (header.size != MEMBLOCKLEN) { + return RES_BAD_SIZE; + } + respond(RES_OK); return read_buffer(other_memblock, MEMBLOCKLEN); + case OP_SETMEMBLOCK: case OP_GETMEMBLOCK: - return RES_OK; + return header.size == 0 ? RES_OK : RES_BAD_SIZE; + default: - /* TODO: Create a better error message. */ - return RES_BAD_IO; + return RES_BAD_OP; } } @@ -204,48 +237,71 @@ static RisuResult recv_and_compare_register_info(void *uc) res = recv_register_info(&ri[MASTER]); if (res != RES_OK) { - /* I/O error. Tell master to exit. */ - respond(RES_END); - return res; + goto done; } op = get_risuop(&ri[APPRENTICE]); - if (header.risu_op != op) { - /* We are out of sync. Tell master to exit. */ - respond(RES_END); - return RES_BAD_IO; - } switch (op) { case OP_COMPARE: case OP_TESTEND: case OP_SIGILL: - if (!reginfo_is_eq(&ri[MASTER], &ri[APPRENTICE])) { + /* + * If we have nothing to compare against, report an op mismatch. + * Otherwise allow the compare to continue, and assume that + * something in the reginfo will be different. + */ + if (header.risu_op != OP_COMPARE && + header.risu_op != OP_TESTEND && + header.risu_op != OP_SIGILL) { + res = RES_MISMATCH_OP; + } else if (!reginfo_is_eq(&ri[MASTER], &ri[APPRENTICE])) { /* register mismatch */ res = RES_MISMATCH_REG; + } else if (op != header.risu_op) { + /* The reginfo matched. We should have matched op. */ + res = RES_MISMATCH_OP; } else if (op == OP_TESTEND) { res = RES_END; } - respond(res == RES_OK ? RES_OK : RES_END); break; + case OP_SETMEMBLOCK: + if (op != header.risu_op) { + res = RES_MISMATCH_OP; + break; + } paramreg = get_reginfo_paramreg(&ri[APPRENTICE]); memblock = (void *)(uintptr_t)paramreg; break; + case OP_GETMEMBLOCK: + if (op != header.risu_op) { + res = RES_MISMATCH_OP; + break; + } paramreg = get_reginfo_paramreg(&ri[APPRENTICE]); set_ucontext_paramreg(uc, paramreg + (uintptr_t)memblock); break; + case OP_COMPAREMEM: + if (op != header.risu_op) { + res = RES_MISMATCH_OP; + break; + } if (memcmp(memblock, other_memblock, MEMBLOCKLEN) != 0) { /* memory mismatch */ res = RES_MISMATCH_MEM; } - respond(res == RES_OK ? RES_OK : RES_END); break; + default: abort(); } + + done: + /* On error, tell master to exit. */ + respond(res == RES_OK ? RES_OK : RES_END); return res; } @@ -346,6 +402,25 @@ static int master(void) } } +static const char *op_name(RisuOp op) +{ + switch (op) { + case OP_SIGILL: + return "SIGILL"; + case OP_COMPARE: + return "COMPARE"; + case OP_TESTEND: + return "TESTEND"; + case OP_SETMEMBLOCK: + return "SETMEMBLOCK"; + case OP_GETMEMBLOCK: + return "GETMEMBLOCK"; + case OP_COMPAREMEM: + return "COMPAREMEM"; + } + abort(); +} + static int apprentice(void) { RisuResult res = sigsetjmp(jmpbuf, 1); @@ -364,7 +439,7 @@ static int apprentice(void) return EXIT_SUCCESS; case RES_MISMATCH_REG: - fprintf(stderr, "mismatch reg after %zd checkpoints\n", signal_count); + fprintf(stderr, "Mismatch reg after %zd checkpoints\n", signal_count); fprintf(stderr, "master reginfo:\n"); reginfo_dump(&ri[MASTER], stderr); fprintf(stderr, "apprentice reginfo:\n"); @@ -373,15 +448,32 @@ static int apprentice(void) return EXIT_FAILURE; case RES_MISMATCH_MEM: - fprintf(stderr, "mismatch mem after %zd checkpoints\n", signal_count); + fprintf(stderr, "Mismatch mem after %zd checkpoints\n", signal_count); + return EXIT_FAILURE; + + case RES_MISMATCH_OP: + /* Out of sync, but both opcodes are known valid. */ + fprintf(stderr, "Mismatch header after %zd checkpoints\n" + "mismatch detail (master : apprentice):\n" + " opcode: %s vs %s\n", + signal_count, op_name(header.risu_op), + op_name(get_risuop(&ri[APPRENTICE]))); return EXIT_FAILURE; case RES_BAD_IO: - fprintf(stderr, "i/o error after %zd checkpoints\n", signal_count); + fprintf(stderr, "I/O error\n"); + return EXIT_FAILURE; + case RES_BAD_MAGIC: + fprintf(stderr, "Unexpected magic number: %#08x\n", header.magic); + return EXIT_FAILURE; + case RES_BAD_SIZE: + fprintf(stderr, "Unexpected payload size: %u\n", header.size); + return EXIT_FAILURE; + case RES_BAD_OP: + fprintf(stderr, "Unexpected opcode: %d\n", header.risu_op); return EXIT_FAILURE; - default: - fprintf(stderr, "unexpected result %d\n", res); + fprintf(stderr, "Unexpected result %d\n", res); return EXIT_FAILURE; } }