From patchwork Sat Jun 11 03:52:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 55031 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 11B76395BC6A for ; Sat, 11 Jun 2022 03:53:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 11B76395BC6A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654919599; bh=3Gjg/275BSVvAvV1QoVnhZr2eFzDY53dYnaEQYFKbGo=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=L+xX6Lk/52ZKjwkUafqIT4Swc2bJWjYpmfZipg/mrnNuUQSoXEfJWus85I57LHlfG aeZs8jMXqvrqbpxZZcJdUAcBAGRR3bKVXJrO0NWGyRs6NTEUtgbj5IsmK3BAXod97T g+gn6I/6MMxQ7jKQ+5AfxYs9xfRQobdShpPgDc3E= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 5267838983B7 for ; Sat, 11 Jun 2022 03:52:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5267838983B7 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-226-cvCndRDJP5yWDLYWw2uUhw-1; Fri, 10 Jun 2022 23:52:55 -0400 X-MC-Unique: cvCndRDJP5yWDLYWw2uUhw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 890B8185A79C for ; Sat, 11 Jun 2022 03:52:55 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 743221121314 for ; Sat, 11 Jun 2022 03:52:55 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 25B3qsIa4021169 for ; Fri, 10 Jun 2022 23:52:54 -0400 Date: Fri, 10 Jun 2022 23:52:54 -0400 Message-Id: To: libc-alpha@sourceware.org Subject: [patch/idea] Add register scrambling to testsuite X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: DJ Delorie via Libc-alpha From: DJ Delorie Reply-To: DJ Delorie Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" [Note: I tried to add a special case for the bug noted below, but ran out of time while trying to learn enough ppc64/vsx opcodery] Allow for target-specific register "scrambling" - loading arbitrary values into all registers that need not be call-saved. These values should be non-zero and invalid addresses, to help catch inadvertent uses of otherwise uninitialized registers. Intended to help prevent bugs such as those fixed by 0218463dd8265ed937622f88ac68c7d984fe0cfc diff --git a/support/Makefile b/support/Makefile index 9b50eac117..91b940c379 100644 --- a/support/Makefile +++ b/support/Makefile @@ -76,6 +76,7 @@ libsupport-routines = \ support_quote_string \ support_record_failure \ support_run_diff \ + support_scramble_registers \ support_select_modifies_timeout \ support_select_normalizes_timeout \ support_set_small_thread_stack_size \ diff --git a/support/support.h b/support/support.h index d20051da4d..3d049575d0 100644 --- a/support/support.h +++ b/support/support.h @@ -233,6 +233,11 @@ void support_stack_free (struct support_stack *stack); The returned value is the lowest file descriptor number. */ int support_open_dev_null_range (int num, int flags, mode_t mode); +/* Write arbitrary values to all registers that can be written do, to + avoid assumptions about initial register contents in test + cases. */ +void support_scramble_registers (void); + __END_DECLS #endif /* SUPPORT_H */ diff --git a/support/support_scramble_registers.c b/support/support_scramble_registers.c new file mode 100644 index 0000000000..d5e2d3fd6d --- /dev/null +++ b/support/support_scramble_registers.c @@ -0,0 +1,29 @@ +/* scramble any call-not-preserved registers + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include "scramble-regs.h" + +void +support_scramble_registers(void) +{ +#ifdef SCRAMBLE_REGS + SCRAMBLE_REGS; +#endif +} diff --git a/support/support_test_main.c b/support/support_test_main.c index 60307fd68e..0ccb182791 100644 --- a/support/support_test_main.c +++ b/support/support_test_main.c @@ -269,6 +269,8 @@ adjust_exit_status (int status) int support_test_main (int argc, char **argv, const struct test_config *config) { + support_scramble_registers(); + if (test_main_called) { printf ("error: test_main called for a second time\n"); diff --git a/sysdeps/generic/scramble-regs.h b/sysdeps/generic/scramble-regs.h new file mode 100644 index 0000000000..7ac55d1bfc --- /dev/null +++ b/sysdeps/generic/scramble-regs.h @@ -0,0 +1,36 @@ +/* scramble any call-not-preserved registers, target portion. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Example target-specific usage: + + #define SCRAMBLE_REGS \ + asm volatile ("movl %0, %%eax" :: "i" (1234)); \ + asm volatile ("movl %0, %%edx" :: "i" (5678)); + + Targets are encouraged to create their own target-specific sub-definitions, like + + #ifndef SCRAMBLE_REGS_FPU + #define SCRAMBLE_REGS_FPU + #endif + #define SCRAMBLE_REGS \ + SCRAMBLE_REGS_FPU \ + asm volatile ("..."); \ + +*/ + +/* #define SCRAMBLE_REGS */ diff --git a/sysdeps/powerpc/scramble-regs.h b/sysdeps/powerpc/scramble-regs.h new file mode 100644 index 0000000000..9400b2ed6b --- /dev/null +++ b/sysdeps/powerpc/scramble-regs.h @@ -0,0 +1,20 @@ +/* scramble any call-not-preserved registers, powerpc version + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define SCRAMBLE_REGS \ + asm volatile ("li 0, %0" :: "i" (0x1235)); diff --git a/sysdeps/x86_64/scramble-regs.h b/sysdeps/x86_64/scramble-regs.h new file mode 100644 index 0000000000..66ffab3c8b --- /dev/null +++ b/sysdeps/x86_64/scramble-regs.h @@ -0,0 +1,31 @@ +/* scramble any call-not-preserved registers, x86_64 version + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* SysV ABI: preserve EBX, ESP, EBP and R12-R15. */ + +#define SCRAMBLE_REGS \ + asm volatile ("movl %0, %%eax" :: "i" (0x12345679)); \ + asm volatile ("movl %0, %%ecx" :: "i" (0x12345679)); \ + asm volatile ("movl %0, %%edx" :: "i" (0x12345679)); \ + asm volatile ("movl %0, %%esi" :: "i" (0x12345679)); \ + asm volatile ("movl %0, %%edi" :: "i" (0x12345679)); \ + asm volatile ("mov %0, %%r8" :: "i" (0x12345679)); \ + asm volatile ("mov %0, %%r9" :: "i" (0x12345679)); \ + asm volatile ("mov %0, %%r10" :: "i" (0x12345679)); \ + asm volatile ("mov %0, %%r11" :: "i" (0x12345679)); \ +