Repository navigation
A signed integer cast to a pointer sign-extends, in a local and in a constant global - #1231
Merged
Merged
Conversation
…constant global sqlite3.h's SQLITE_TRANSIENT is ((void (*)(void *))-1). c_import binds it correctly; codegen did not. Two exact lines: - CodegenDispatch.w, the Int -> Ptr cast, emitted a bare inttoptr from an i32. LLVM zero-extends a narrower integer, so -1 became 0x00000000FFFFFFFF. - CodegenTraits.w, a module binding whose initializer folds to an integer, handled a pointer-typed binding only for the value 0. Any other value fell to the generic path and was emitted as an i32 global (`@G = internal constant i32 -1` in the IR) and read back as eight bytes. sqlite3_bind_text(..., SQLITE_TRANSIENT) therefore passed SQLite a pointer that was not SQLITE_TRANSIENT; lldb shows bindText -> vdbeUnbind -> vdbeMemClear calling it as a destructor. Binds LLVMConstIntToPtr for the constant case. behav_int_to_pointer_cast_sign_extends.w fails at its first assertion on the previous compiler and passes on this one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sqlite3.h's
SQLITE_TRANSIENTis((void (*)(void *))-1). c_import binds it correctly; codegen miscompiled it, silently, in two places.src/CodegenDispatch.w, Int → Ptr): a bareinttoptrfrom ani32. LLVM zero-extends a narrower integer, so-1became0x00000000FFFFFFFF. A signed source now widens to 64 bits first; an unsigned source still zero-extends.src/CodegenTraits.w, module binding whose initializer folds to an integer): a pointer-typed binding was handled only for the value 0. Any other value fell to the generic path and was emitted as an integer global (@G = internal constant i32 -1in the IR) and read back as eight bytes, so its upper half was adjacent memory. It is now a pointer constant. BindsLLVMConstIntToPtr.Effect:
sqlite3_bind_text(..., SQLITE_TRANSIENT)passed SQLite a pointer that was notSQLITE_TRANSIENT. lldb:bindText → vdbeUnbind → vdbeMemClearcalled it as a destructor (EXC_BAD_ACCESS). Found while rewritingexamples/c-interop.Test:
behav_int_to_pointer_cast_sign_extends.wcovers the local, the global, a zero global, a negative*const u8global, a runtime signed value and an unsigned one. It fails at its first assertion on the previous compiler.Battery, seed-driven at 444dbb4, alone in its batch: build, :fixpoint, :test, :move-audit, :drop-audit, :test-green, :last-green all green.