Fundament-Software / scopes

Fundament fork of the Scopes language
Other
3 stars 1 forks source link

Double free with String and itertools #26

Open LunNova opened 1 year ago

LunNova commented 1 year ago

Example code here:

https://github.com/LunNova/scopes-learning/blob/b438933c1e14e9658f1fa3c357d501fadb7c0fd4/aoc/3.sc#L69

Scopes is freeing the String that gets (l as String)d in line 54. Don't think should be an allocation here to free even so very confused.

(gdb) x /s 0x64863b0 0x64863b0: "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL" ^ this is the second line of sample-input

free() gets called on it ... continue free() gets called again crash

Both calls are under find-common-n line 54 according to backtrace

Further discussion happened on discord, didn't get anywhere useful.

Haven't worked out a proper mcve.

LunNova commented 1 year ago
scopes: /build/source/llvm/lib/IR/Globals.cpp:366: llvm::GlobalVariable::GlobalVariable(llvm::Module&, llvm::Type*, bool, llvm::GlobalValue::LinkageTypes, llvm::Constant*, const llvm::Twine&, llvm::GlobalVariable*, llvm::GlobalValue::ThreadLocalMode, llvm::Optional<unsigned int>, bool): Assertion `!Ty->isFunctionTy() && PointerType::isValidElementType(Ty) && "invalid type for global variable"' failed.

Tried to test with debug llvm but falls over with this, looks like #19

LunNova commented 1 year ago

Suggested workaround: don't use cons lists outside of sugars:

fn sum-group-priorities (input)
    returning i32
+   local dst : (Array String)

    ->>
        string-split-generator input h"\n"
        cascade
            take 3
-           'cons-sink '()
-       map find-common-n
+           view dst
+       map (inline (lists) (defer 'clear lists) (find-common-n lists))
        map item-priority
        reduce-sum 0

Having to manually clear the local array on the first sink after the cascade is pretty jank.