Code Monkey home page Code Monkey logo

Comments (4)

klange avatar klange commented on May 24, 2024

I had a feeling there would be a lingering case of this somewhere...

As with other places where this was all cleaned up some time ago, the solution is to retrieve the expected offset into the stack, and then reference the actual stack pointer for each of the subsequent accesses.

diff --git a/src/vm.c b/src/vm.c
index 22385c8..9141338 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -428,11 +428,11 @@ int krk_processComplexArguments(int argCount, KrkValueArray * positionals, KrkTa
 		krk_writeValueArray(positionals, krk_currentThread.stackTop[-argCount + i - TOP_ARGS]);
 	}
 
-	KrkValue * startOfExtras = &krk_currentThread.stackTop[-kwargsCount * 2 - TOP_ARGS];
+	size_t startOfExtras = &krk_currentThread.stackTop[-kwargsCount * 2 - TOP_ARGS] - krk_currentThread.stack;
 	/* Now unpack everything else. */
 	for (size_t i = 0; i < kwargsCount; ++i) {
-		KrkValue key = startOfExtras[i*2];
-		KrkValue value = startOfExtras[i*2 + 1];
+		KrkValue key = krk_currentThread.stack[startOfExtras + i*2];
+		KrkValue value = krk_currentThread.stack[startOfExtras + i*2 + 1];
 		if (IS_KWARGS(key)) {
 			if (AS_INTEGER(key) == KWARGS_LIST) { /* unpack list */
 				if (krk_unpackIterable(value,positionals,_unpack_args)) return 0;

from kuroko.

klange avatar klange commented on May 24, 2024

I should try running some complex workloads with the stack set up to always reallocate on push and see if that weeds out any other cases...

from kuroko.

klange avatar klange commented on May 24, 2024

You might be interested to see that Valgrind caught this rather well:

==1354975== Memcheck, a memory error detector
==1354975== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1354975== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==1354975== Command: kuroko test.krk
==1354975== 
==1354975== Invalid read of size 8
==1354975==    at 0x158A7B: krk_processComplexArguments (in /usr/bin/kuroko)
==1354975==    by 0x159E27: krk_callValue (in /usr/bin/kuroko)
==1354975==    by 0x15F859: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975==  Address 0x4afa520 is 96 bytes inside a block of size 256 free'd
==1354975==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1354975==    by 0x157C97: krk_growStack (in /usr/bin/kuroko)
==1354975==    by 0x157D59: krk_push (in /usr/bin/kuroko)
==1354975==    by 0x112195: krk_valuesSameOrEqual (in /usr/bin/kuroko)
==1354975==    by 0x1565B6: krk_findEntry (in /usr/bin/kuroko)
==1354975==    by 0x156A3F: krk_tableSet (in /usr/bin/kuroko)
==1354975==    by 0x158B13: krk_processComplexArguments (in /usr/bin/kuroko)
==1354975==    by 0x159E27: krk_callValue (in /usr/bin/kuroko)
==1354975==    by 0x15F859: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975==  Block was alloc'd at
==1354975==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1354975==    by 0x157C97: krk_growStack (in /usr/bin/kuroko)
==1354975==    by 0x1617A1: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975== 
==1354975== Invalid read of size 8
==1354975==    at 0x158A7F: krk_processComplexArguments (in /usr/bin/kuroko)
==1354975==    by 0x159E27: krk_callValue (in /usr/bin/kuroko)
==1354975==    by 0x15F859: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975==  Address 0x4afa528 is 104 bytes inside a block of size 256 free'd
==1354975==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1354975==    by 0x157C97: krk_growStack (in /usr/bin/kuroko)
==1354975==    by 0x157D59: krk_push (in /usr/bin/kuroko)
==1354975==    by 0x112195: krk_valuesSameOrEqual (in /usr/bin/kuroko)
==1354975==    by 0x1565B6: krk_findEntry (in /usr/bin/kuroko)
==1354975==    by 0x156A3F: krk_tableSet (in /usr/bin/kuroko)
==1354975==    by 0x158B13: krk_processComplexArguments (in /usr/bin/kuroko)
==1354975==    by 0x159E27: krk_callValue (in /usr/bin/kuroko)
==1354975==    by 0x15F859: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975==  Block was alloc'd at
==1354975==    at 0x483DFAF: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1354975==    by 0x157C97: krk_growStack (in /usr/bin/kuroko)
==1354975==    by 0x1617A1: ??? (in /usr/bin/kuroko)
==1354975==    by 0x162AD9: krk_callStack (in /usr/bin/kuroko)
==1354975==    by 0x15AD60: krk_runfile (in /usr/bin/kuroko)
==1354975==    by 0x111525: main (in /usr/bin/kuroko)
==1354975== 
{'moment': 7, 'all': 1, 'zero': 0, 'scalar': 2, 'antiscalar': 3, 'point': 4, 'translation': 11, 'flector': 12, 'line': 5, 'motor': 9, 'plane': 6, 'rotation': 10, 'direction': 8}
==1354975== 
==1354975== HEAP SUMMARY:
==1354975==     in use at exit: 0 bytes in 0 blocks
==1354975==   total heap usage: 2,427 allocs, 2,427 frees, 232,168 bytes allocated
==1354975== 
==1354975== All heap blocks were freed -- no leaks are possible
==1354975== 
==1354975== For lists of detected and suppressed errors, rerun with: -s
==1354975== ERROR SUMMARY: 16 errors from 2 contexts (suppressed: 0 from 0)

from kuroko.

notwa avatar notwa commented on May 24, 2024

ah, so you were able to reproduce it! I was definitely led astray by my intuition, and should've checked with valgrind first…

from kuroko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.