From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Taylan Kammer Newsgroups: gmane.lisp.guile.bugs Subject: bug#42345: "Wrong number of arguments to 1" Date: Mon, 24 May 2021 00:11:10 +0200 Message-ID: <62c508e7-74b1-adc2-affb-7f3f82ebcbf9@gmail.com> References: <104d8455-0da3-8dcf-13da-1a4ecec442a2@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11918"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 Cc: Christopher Lam , Matt Wette To: 42345@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon May 24 00:12:07 2021 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lkwKN-0002uo-6w for guile-bugs@m.gmane-mx.org; Mon, 24 May 2021 00:12:07 +0200 Original-Received: from localhost ([::1]:37084 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lkwKM-0000xa-7o for guile-bugs@m.gmane-mx.org; Sun, 23 May 2021 18:12:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38536) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lkwKI-0000xR-Fi for bug-guile@gnu.org; Sun, 23 May 2021 18:12:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:58596) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lkwKI-0002HN-89 for bug-guile@gnu.org; Sun, 23 May 2021 18:12:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lkwKI-0000pW-2K for bug-guile@gnu.org; Sun, 23 May 2021 18:12:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <104d8455-0da3-8dcf-13da-1a4ecec442a2@gmail.com> Resent-From: Taylan Kammer Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sun, 23 May 2021 22:12:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 42345 X-GNU-PR-Package: guile Original-Received: via spool by 42345-submit@debbugs.gnu.org id=B42345.16218078823135 (code B ref 42345); Sun, 23 May 2021 22:12:02 +0000 Original-Received: (at 42345) by debbugs.gnu.org; 23 May 2021 22:11:22 +0000 Original-Received: from localhost ([127.0.0.1]:41909 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lkwJe-0000oV-3P for submit@debbugs.gnu.org; Sun, 23 May 2021 18:11:22 -0400 Original-Received: from mail-ed1-f45.google.com ([209.85.208.45]:37395) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lkwJZ-0000oF-Gr for 42345@debbugs.gnu.org; Sun, 23 May 2021 18:11:21 -0400 Original-Received: by mail-ed1-f45.google.com with SMTP id g7so17508297edm.4 for <42345@debbugs.gnu.org>; Sun, 23 May 2021 15:11:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:cc:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=FiTRIocwpJ73ibk59sKr2UrFE3xjBt5UzH7jxqpDenk=; b=fThJ66uNhQknxRnTEkVBilHl2jE4dchG97faJeBrJGSdrwWpB9h5K6HDm4ktkix5aZ nsPdTJ1nfrhcY7TZhfNNlvetqJHPJZ0gwkspxDq633UBJCkmBWg3ELwtWA/zRkVSwRTf s/czO32CMJPjOm82aMH1v+9kXiLVOyWLTFD+eFn+IVMMwgRVEZsK3ohKD4Ft5qWOa9Wa QDyljjszLt3WwlukHMhz9RXvXbRLcHOwK02xDdrdV8hlWHDy+eS7Y3dgjCnI863h3Udv 7tgwQP1Xnlk6ABzcg2SmqrUJDgyVeUPRVPHB7Sr/78M46tVAvLYu8vilyjoBjMQBNB9C e7Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:cc:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=FiTRIocwpJ73ibk59sKr2UrFE3xjBt5UzH7jxqpDenk=; b=aJ2GwTa641Gs/PWJlyZx3wsU18TPfx9IgfMBo59v8w/dfUq9p/R+DqrQLuL8ahN35v Bwo4WfEOgIZJp90ZoFPs5Y+OfjJKY8SmqUMKP1XoYZVj2B2lMIMO+6ijDPN9OMElbf2L lG4R/bJt6pzM/c9gwg+JCDWh1WDlCtUc7gc4dSWWkBHBA1gV6FN5GXsBwvkBw+fOW8Mv rK/PGDxYuckwrjwMCY/cp52+LPtJiUulqoIKuIjISIBqJr4sGdad81bnw0eVrfJxPLPI WD1aRZ2OyMSIDz14XOnzJxMsGz6yH4fyvJX7hh9h/jJL8YP3wV3LB1u60Mmb7KN9TB6Q dfOw== X-Gm-Message-State: AOAM532rpXu3HhwMeavfxHJg5r973yOizqNixeJk4wphA8XSQGAY2jqO M9WyBeyhxNRdbR7CWRPJ/Zc= X-Google-Smtp-Source: ABdhPJy7uoOwspBjbeFzLWlsT2wV4BtGAJ2elQWkD0a5JzyCWqM/dj17gJbAMm+U3z6J36ttdbF9uA== X-Received: by 2002:a05:6402:17d9:: with SMTP id s25mr22206852edy.337.1621807871779; Sun, 23 May 2021 15:11:11 -0700 (PDT) Original-Received: from [192.168.178.20] (b2b-109-90-125-150.unitymedia.biz. [109.90.125.150]) by smtp.gmail.com with ESMTPSA id jt11sm7090728ejb.83.2021.05.23.15.11.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 23 May 2021 15:11:11 -0700 (PDT) Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:10108 Archived-At: Note: merged with https://bugs.gnu.org/42757 as it's the same bug. Andy is in CC since we almost certainly need his input. :-) I've been working on this since a few days (had to learn much about the VM) and here's an explanation of what's going on. Firstly here's a minimal procedure that exhibits the bug: (define (test x) (define (inner-proc a) #f) (inner-proc 0 x)) Calling this procedure will always result in the error message: Wrong number of arguments to 0 Because the first argument to inner-proc is 0. Were the first argument x, then the argument we pass to 'test' would appear in the error message. That's our bug at a high level: the first argument to inner-proc being reported as the procedure that raised the error. The disassembly is quite slim: Disassembly of # at #x559c98764348: 0 (instrument-entry 82) 2 (assert-nargs-ee/locals 2 0) ;; 2 slots (1 arg) 3 (make-immediate 1 2) ;; 0 4 (handle-interrupts) 5 (tail-call-label 2) ;; inner-proc at #x559c98764364 ---------------------------------------- Disassembly of inner-proc at #x559c98764364: 0 (instrument-entry 81) 2 (assert-nargs-ee/locals 1 0) ;; 1 slot (0 args) 3 (make-immediate 0 4) ;; #f 4 (handle-interrupts) 5 (return-values) Here's some explanations for those who aren't savvy with the VM, starting from the top. (The instructions, like assert-nargs-ee/locals, are defined in libguile/vm-engine.c.) Ignore the instrument-entry and handle-interrupts instructions. - (assert-nargs-ee/locals 2 0) checks the number of arguments, ensuring that there's a total of 2 slots on the stack. [1] - (make-immediate 1 2) puts the Scheme number 0 in slot 0. [2] - (tail-call-label 2) jumps to the beginning of inner-proc. [3] - (assert-nargs-ee/locals 1 0) checks the number of arguments, ensuring that there's a total of 1 slot on the stack. [4] That's where the error is raised, by calling error_wrong_num_args from libguile/intrinsics.c, because there's 2 slots on the stack, not 1. Error_wrong_num_args tries to get the currently executed procedure from slot 0, and finds the number 0 there, wrongly using that instead. [1] Slot 0 is usually filled with the procedure being executed itself, before it's called, so the number of slots is usually the number of arguments plus one. [2] Slot references in the VM are referenced backwards from N-1 to 0, where N is the number of slots. In our case, as we have 2 slots, the number 1 refers to slot 0. The reason has to do with the Guile stack growing downwards. The constant 0 is represented by 2 because of its type tag as an "immediate" int, see scm.h for details. [3] The 2 represents the relative position of the first instruction of inner-proc, in 4-byte units. It's pretty close, as it's compiled right aside our top-level procedure 'test'. Since we're currently on instruction 5 of 'test', and instructions are 4 bytes long, and 'test' begins at 0x559c98764348, this means we're jumping to: 0x559c98764348 + 5*4 + 2*4 = 0x559c98764364 Which happens to be the address of inner-proc, see? :-) [4] I don't know why only one slot even though it has one argument; it should be two slots. Maybe an optimization, as the compiler decides that the procedure doesn't ever need a reference to itself? To summarize: for some reason the compiler decides to *not* use an extra slot for the currently-executed procedure when calling inner-proc, which leads to the first argument to inner-proc (in this case 0) being used as the "procedure being executed" during error reporting. Perhaps that optimization (assuming it is one and not simply a bug in the compiler) should be disabled, or maybe the code shouldn't even compile since it can be statically proven that a procedure is being called with the wrong number of arguments. This is where I defer to the Guile compiler experts. -- Taylan