From owner-freebsd-hackers@freebsd.org Tue Dec 3 07:36:17 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B517B1CB683 for ; Tue, 3 Dec 2019 07:36:17 +0000 (UTC) (envelope-from karnajitw@gmail.com) Received: from mail-vs1-xe30.google.com (mail-vs1-xe30.google.com [IPv6:2607:f8b0:4864:20::e30]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47Rv1F00P6z41gC for ; Tue, 3 Dec 2019 07:36:16 +0000 (UTC) (envelope-from karnajitw@gmail.com) Received: by mail-vs1-xe30.google.com with SMTP id p6so1701724vsj.11 for ; Mon, 02 Dec 2019 23:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=FzyfIcOoVb5Q02ZdCO3LdcY6liqRTzzNGujCwTp8cK4=; b=spOI7EfRkxiHBF53+ODiJzeqdk6Hp6Ii15oJ4gMm1T9jLOxnropxh9LUGxLD9i7Q01 hKw8pOsyHpoTwk0SxfFFNMu07i+4NIUfcseOGZhgeRJp0QmBLn7xK3beDkr7EAYve17j OYnG0Uepoc1LvLh9aZXRlIVXWH3F/BlLDWwm/IFDyOoUzahxt6+XfS5X/AjZhIxhrmEj yklA0E9wafeMUE4sYmVvmMeje6Rp0NgZl3eIFqP891mQ56hOAmvt5B5yrRIptXG4b1GQ znMWeAcJJaOQ9CPCS/CERZgZTsTQnIOLEW58ErdztrkDmtm3Z4SD5xjg01c6+efN/09o d+/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=FzyfIcOoVb5Q02ZdCO3LdcY6liqRTzzNGujCwTp8cK4=; b=k7OkNaPz38sVhlYZbScV4y7awzVioYITutGfwc0XxESXABm8P5ChCBtL0OflnYtCQr vRVZqGjy+hjbqcG+iuXQ+kwUPOAjJ1pkYRGg+gIAedloIEQI1q7p/b1M3OuTwu/01B2D 9VvrODePa16ig9snfRnxi6y1E/F9oyzPwp/8JkMsDut8BUG6tLMEc5mO+4JTZkUZHasf eyYhLGMT3CdUw9ZcTYQACUL20FrkD8S5ZNDNRX89nfra9YjCQLiLpG2yI7cUUU+JN8MC EJwdqE6mUU4EtgKOZaSKS8z2FsEi/idcXm1TqgE4a455YhGX+6iZCWEacbJnsi212jYo 7cVg== X-Gm-Message-State: APjAAAWVnZq4v+0cHeJ8A82fDJfBuIcWzAxZgymVfVzxsTalijDoQQjb PSZ+okd+YuMU+Nm5p6Jn59QwKHFE4bLfI3Gr+amquEQs X-Google-Smtp-Source: APXvYqweWBtJNoyOxW2pab4NkqNijsxwMr9rYqmj4PiqsUw2jYcNQMcXKOyvZ1G1kse+WkqNDSHFR9vA7hAbeHhMW6Y= X-Received: by 2002:a05:6102:a17:: with SMTP id t23mr1827739vsa.121.1575358575490; Mon, 02 Dec 2019 23:36:15 -0800 (PST) MIME-Version: 1.0 From: karnajit wangkhem Date: Tue, 3 Dec 2019 13:06:04 +0530 Message-ID: Subject: State of register edx on valgrind-freebsd after syscall To: freebsd-hackers@freebsd.org X-Rspamd-Queue-Id: 47Rv1F00P6z41gC X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=spOI7EfR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of karnajitw@gmail.com designates 2607:f8b0:4864:20::e30 as permitted sender) smtp.mailfrom=karnajitw@gmail.com X-Spamd-Result: default: False [-3.00 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; TO_DN_NONE(0.00)[]; IP_SCORE_FREEMAIL(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; IP_SCORE(0.00)[ip: (-9.63), ipnet: 2607:f8b0::/32(-2.24), asn: 15169(-1.94), country: US(-0.05)]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_IN_DNSWL_NONE(0.00)[0.3.e.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.b.8.f.7.0.6.2.list.dnswl.org : 127.0.5.0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Dec 2019 07:36:17 -0000 Hi All, Below is the valgrind-freebsd I used https://www.freshports.org/devel/valgrind https://bitbucket.org/stass/valgrind-freebsd/get/ce1acb28953f.tar.gz?dummy=/ $ ./valgrind --version valgrind-3.10.1 Please consider the following assembly ============================================================ .section .data .long 0 /* Valgrind segfaults without this dummy */ .section .text .global _start STR0: .ascii "Hello World\n" kernel: int $0x80 ret _start: mov $2, %ebx /* Store value 2, 3, 4 in reg ebx, ecx, edx */ mov $3, %ecx mov $4, %edx push $12 /* Push arguments on stack as per x86 freebsd convention */ push $STR0 push $1 mov $4, %eax /* write syscall no == 4 */ call kernel add %ebx, %eax /* Now lets add all the GP registers */ add %ecx, %eax add %edx, %eax push %eax /* We expect 4(edx) + 3(ecx) + 2(ebx) + 12(eax) == 21 */ mov $1, %eax /* Perform exit syscall */ call kernel ============================================================ compile: $ clang --target=i386-unknown-freebsd10.3 -nostdlib -nostdinc reg_minimal.s $ ./a.out Hello World $ echo $? 21 $ ./valgrind ./a.out ==69326== Memcheck, a memory error detector ==69326== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==69326== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==69326== Command: ./a.out ==69326== Hello World ==69326== ==69326== HEAP SUMMARY: ==69326== in use at exit: 0 bytes in 0 blocks ==69326== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==69326== ==69326== All heap blocks were freed -- no leaks are possible ==69326== ==69326== For counts of detected and suppressed errors, rerun with: -v ==69326== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) $ echo $? 177 <<<<<<<<<< This is wrong Now, if we modify the code slightly to return %edx instead, the return value will looks like this $ echo $? 160 So, the reason why eax + ebx + ecx + edx returned 177 instead of 21 was because register edx has been clobbered by valgrind instrumentation of syscall. In case of linux, the syscall calling convention is bit different and we need to pass the arguments in registers ebx, ecx, edx, but even after the syscall, these register value holds the same argument values (valgrind-linux valgrind-3.7.0). Could someone give a pointer to which part of the freebsd-valgrind code could be responsible for this bug? Or if possible where I can fix this? Regards, Karan