From owner-freebsd-hackers@freebsd.org Fri Dec 13 07:00:18 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 26F741E5408 for ; Fri, 13 Dec 2019 07:00:18 +0000 (UTC) (envelope-from karnajitw@gmail.com) Received: from mail-vk1-xa2d.google.com (mail-vk1-xa2d.google.com [IPv6:2607:f8b0:4864:20::a2d]) (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 47Z1l45MxRz4gJ1 for ; Fri, 13 Dec 2019 07:00:16 +0000 (UTC) (envelope-from karnajitw@gmail.com) Received: by mail-vk1-xa2d.google.com with SMTP id t129so41654vkg.6 for ; Thu, 12 Dec 2019 23:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=9sa/UTUhgQgwP66skr9p3XLj8SaLRbTFuzVzsUJAv0I=; b=VtOaY3B5sCqPBS2DFSS64D+tsypoZ1RgIbKEAyBqQnfK1B3KSDzhSnXF0qqkpDA4GX 8kf1z6lPbSTrwNxBXLnW+gkYokCFNffDBFUY/bl+Ha9cEFFbTY0uRfzBTqHJM/vToB6q wyjfKAih0fJRqmG57P5KPvF4riE2YQW/WSOWH73E6F9Fd7evoy4wAB8Y9hZ+wj6L7V8R clLjLkqN8Kai4/BnoQV3PfpH5BByDtlWO9jLvdAu1DHJrAmxCqx9Ihyd+3GLHpmmEFWr 6JwjeVLy0Hv2R4qmqZ4VAz1oWXpPJGHrON4r+LCYTs/fBDvEuocm+0raIWnpKKfqlgak SOcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=9sa/UTUhgQgwP66skr9p3XLj8SaLRbTFuzVzsUJAv0I=; b=g4ksxn7fExAszNbnKvyFsUbARWaVmA37QQY8+wXztm9/gT7RrQPbVnh5JcKBnozuGo rpiUVzSCFgX9D5JXBhUazQFBkVY6fueags0An/n4SKxYp3KdTOV3Ly12k3cHc9uI23Hx nPiI850GO4xOa0MlJoSuKUAOySfnBHRWb1ycwTe5G72mBLn86ODN82B3eFcyTXYh6XUv /wljUVmtzTaJmYCExDONNKgJYtJizBLMFhcnME9Fd/iz16YYWg/2K/EcMRma+0ZuD2Cb 69plH/XvhvhaSUMfOT1RRSHyJp4zwfesDZQJeRmrFuSNFXK6qknHoGDJFBOlgF+NPFJS Qd2g== X-Gm-Message-State: APjAAAUeCLtr0awLYo2JqqMJCnfYOfQUrxcts3kr6dbh2XgknkaEErD7 3i9bRjBze0XOXKNXfQlc6CdYy6foH94vF8DF/Xjt+iuG X-Google-Smtp-Source: APXvYqyJnU26ExdpEHDSm53PhNN5ci/dYQ0/2d4O+Z02zTz7v0BLoS6V8TGgYR6ZihfdOMWGSgwqBJ7c6MlOc6IXkoc= X-Received: by 2002:a1f:3a8a:: with SMTP id h132mr12603484vka.95.1576220414377; Thu, 12 Dec 2019 23:00:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: karnajit wangkhem Date: Fri, 13 Dec 2019 12:30:03 +0530 Message-ID: Subject: Re: State of register edx on valgrind-freebsd after syscall To: freebsd-hackers@freebsd.org X-Rspamd-Queue-Id: 47Z1l45MxRz4gJ1 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=VtOaY3B5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of karnajitw@gmail.com designates 2607:f8b0:4864:20::a2d 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]; 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.25), ipnet: 2607:f8b0::/32(-2.20), asn: 15169(-1.91), 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)[d.2.a.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: Fri, 13 Dec 2019 07:00:18 -0000 Hi All, Closing on this thread, I got the issue behind this some time back, but still have to check on a proper fix. syscall-x86-freebsd.S: movl %eax, OFFSET_x86_EAX(%ecx) /* save EAX to vex */ movl %edx, OFFSET_x86_EDX(%ecx) /* save EDX to vex */ <<< We have to probably identify between a syscall that will return a single word(write syscall) or a double word(lseek). Regards, Karan On Tue, Dec 3, 2019 at 1:06 PM karnajit wangkhem wrote: > 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 >