Skip to content

Adopt Thread::Backtrace::Location#source_range in ErrorHighlight - #2

Closed
eregon wants to merge 1 commit into
masterfrom
location-source_range-error_highlight
Closed

eregon wants to merge 1 commit into
masterfrom
location-source_range-error_highlight

Conversation

@eregon

@eregon eregon commented Jul 24, 2026 •

Copy link
Copy Markdown
Owner
make test-all TESTS='test/error_highlight/test_error_highlight.rb'
make test-all TESTS='test/error_highlight/test_error_highlight.rb' RUBYOPT='--parser=parse.y'

both pass.

@eregon
eregon force-pushed the location-source_range-error_highlight branch 3 times, most recently from 22709dd to 846c2c8 Compare July 24, 2026 22:54
@eregon
eregon force-pushed the location-source_range branch from ea847dd to bb510c9 Compare July 24, 2026 23:13
@eregon
eregon force-pushed the location-source_range-error_highlight branch from 846c2c8 to e330e9a Compare July 24, 2026 23:14
@eregon
eregon force-pushed the location-source_range branch from bb510c9 to c7afb7a Compare July 29, 2026 21:42
@eregon
eregon force-pushed the location-source_range-error_highlight branch from e330e9a to f0064f1 Compare August 6, 2026 15:01
@eregon
eregon changed the base branch from location-source_range to master August 6, 2026 15:02
XrXr pushed a commit that referenced this pull request Nov 16, 2026
If the source string is modified in the chilled string warning in String#setbyte,
it will cause a buffer overflow because the string length is read before
the warning.

For example, the following code will output an error in ASAN:

    Warning[:deprecated] = true
    module Warning
      def self.warn(msg, **kw) = $s.clear
    end
    # 1000-byte chilled literal (heap allocated)
    eval("$s = '#{"a" * 1000}'")
    $s.setbyte(999, 0x42)
    p $s

Outputs:

    ERROR: AddressSanitizer: use-after-poison on address 0x0001235e44d7 at pc 0x0001027170d4 bp 0x00016dc48f50 sp 0x00016dc48f48
    WRITE of size 1 at 0x0001235e44d7 thread T0
        #0 0x0001027170d0 in rb_str_setbyte string.c:7405
        #1 0x0001028756a4 in vm_call_cfunc_with_frame_ vm_insnhelper.c:3936
        #2 0x000102860918 in vm_call_method_each_type vm_insnhelper.c:5041
        #3 0x00010285ffd4 in vm_call_method vm_insnhelper.c
        ruby#4 0x00010281de04 in vm_sendish vm_insnhelper.c:6429
        ruby#5 0x00010281de04 in vm_exec_core insns.def:912
        ruby#6 0x000102809388 in rb_vm_exec vm.c:2917
        ruby#7 0x0001023a0400 in rb_ec_exec_node eval.c:300
        ruby#8 0x0001023a0228 in ruby_run_node eval.c:338
        ruby#9 0x0001021b4c20 in rb_main main.c:42
        ruby#10 0x0001021b4c20 in main main.c:62
        ruby#11 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
eregon pushed a commit that referenced this pull request Nov 17, 2026
If the source string is modified in the chilled string warning in String#<<,
it's possible for overlapping memcpy to happen.

For example, the following script triggers an ASAN error:

    Warning[:deprecated] = true
    module Warning
      def self.warn(msg, **kw) = $s.replace("foo")
    end
    $s = "abcd"
    $s << $s
    p $s, $s.bytesize

ASAN error:

    ERROR: AddressSanitizer: memcpy-param-overlap: memory ranges [0x000121e33f7b,0x000121e33f7f) and [0x000121e33f78, 0x000121e33f7c) overlap
        #0 0x000102509f08 in __asan_memcpy+0x1e0 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x51f08)
        #1 0x0001010903c0 in ruby_nonempty_memcpy memory.h:714
        #2 0x0001010903c0 in str_buf_cat4 string.c:3695
        #3 0x0001010913cc in rb_str_buf_append string.c:3914
        ruby#4 0x000101198aa4 in vm_opt_ltlt vm_insnhelper.c:7309
        ruby#5 0x000101198aa4 in vm_exec_core insns.def:1578
        ruby#6 0x00010118d148 in rb_vm_exec vm.c:2917
        ruby#7 0x000100d24400 in rb_ec_exec_node eval.c:300
        ruby#8 0x000100d24228 in ruby_run_node eval.c:338
        ruby#9 0x000100b38c20 in rb_main main.c:42
        ruby#10 0x000100b38c20 in main main.c:62
        ruby#11 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
eregon pushed a commit that referenced this pull request Nov 22, 2026
If an ifunc Proc doesn't have an env, then it can have a buffer overflow
read in Proc#==.

For example, the following script crashes under ASAN:

    c = proc { |a, b| }.curry
    p(c == c)

Crashes with:

    ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000013c38 at pc 0x000101bdb4f4 bp 0x00016fdf8f30 sp 0x00016fdf86c8
    READ of size 32 at 0x604000013c38 thread T0
        #0 0x000101bdb4f0 in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long)+0x594 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x1b4f0)
        #1 0x000101bdb8c8 in memcmp+0x54 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x1b8c8)
        #2 0x0001003fa160 in rb_proc_eq proc.c:2109
        #3 0x0001006c7624 in vm_call_cfunc_with_frame_ vm_insnhelper.c:3936
        ruby#4 0x0001006b2898 in vm_call_method_each_type vm_insnhelper.c:5041
        ruby#5 0x0001006b1f54 in vm_call_method vm_insnhelper.c
        ruby#6 0x00010066f85c in vm_sendish vm_insnhelper.c:6443
        ruby#7 0x00010066f85c in vm_exec_core insns.def:912
        ruby#8 0x00010065aeb4 in rb_vm_exec vm.c:2930
        ruby#9 0x0001001f0740 in rb_ec_exec_node eval.c:300
        ruby#10 0x0001001f0568 in ruby_run_node eval.c:338
        ruby#11 0x000100004c20 in rb_main main.c:42
        ruby#12 0x000100004c20 in main main.c:62
        ruby#13 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
eregon pushed a commit that referenced this pull request Nov 22, 2026
Passing send symbol proc into a Thread will cause a buffer overflow where
it will read past the VM stack.

For example, the following script crashes in ASAN:

    p Thread.new(1, :to_s, &:send).value

Crashes with:

    ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000125ca4820 at pc 0x000100802a0c bp 0x00017056a070 sp 0x00017056a068
    READ of size 8 at 0x000125ca4820 thread T2
        #0 0x000100802a08 in send_internal vm_eval.c:1268
        #1 0x00010080058c in vm_call0_body vm_eval.c:289
        #2 0x000100802ee8 in vm_call0_cc vm_eval.c:103
        #3 0x000100802ee8 in rb_call0 vm_eval.c:603
        ruby#4 0x00010080837c in vm_yield_with_symbol vm_insnhelper.c
        ruby#5 0x0001007c39bc in invoke_block_from_c_proc vm.c:2039
        ruby#6 0x0001007c39bc in vm_invoke_proc vm.c:2054
        ruby#7 0x00010070eb54 in thread_do_start_proc thread.c:663
        ruby#8 0x00010070c020 in thread_do_start thread.c:681
        ruby#9 0x00010070c020 in thread_start_func_2 thread.c:739
        ruby#10 0x000100715320 in call_thread_start_func_2 thread_sched.c:1985
        ruby#11 0x000100715320 in nt_start thread_sched.c:2020
        ruby#12 0x000101c79cfc in asan_thread_start(void*)+0x48 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x51cfc)
        ruby#13 0x000191790ce0 in _pthread_start+0x84 (libsystem_pthread.dylib:arm64e+0x6ce0)
        ruby#14 0x00019178bccc in thread_start+0x4 (libsystem_pthread.dylib:arm64e+0x1ccc)
eregon pushed a commit that referenced this pull request Nov 22, 2026
If the receiving string is modified during the to_str call on the hash keys
and values, then it could cache stale encoding and coderange, which could
give negative character lengths.

For example, the following code reproduces the issue:

    s = "abc" * 1000
    k = Object.new
    k.define_singleton_method(:to_str) do
      s.replace("\x8F\xA1\xA1".force_encoding("EUC-JP") * 1000)
      "\xA4\xA2".force_encoding("EUC-JP")
    end
    s.tr!({ k => "X" })
    p s.bytesize

Output on ASAN:

    ERROR: AddressSanitizer: negative-size-param: (size=-401)
        #0 0x0001064ab22c in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long)+0x2d0 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x1b22c)
        #1 0x0001064ab8c8 in memcmp+0x54 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x1b8c8)
        #2 0x000105146578 in tr_trans_pairs string.c:11193
        #3 0x00010526d8a4 in vm_call_cfunc_with_frame_ vm_insnhelper.c:3944
        ruby#4 0x000105258ad8 in vm_call_method_each_type vm_insnhelper.c:5049
        ruby#5 0x000105258194 in vm_call_method vm_insnhelper.c
        ruby#6 0x0001052162c4 in vm_sendish vm_insnhelper.c:6437
        ruby#7 0x0001052162c4 in vm_exec_core insns.def:912
        ruby#8 0x000105201848 in rb_vm_exec vm.c:2930
        ruby#9 0x000104d96d40 in rb_ec_exec_node eval.c:300
        ruby#10 0x000104d96b68 in ruby_run_node eval.c:338
        ruby#11 0x000104bacc20 in rb_main main.c:42
        ruby#12 0x000104bacc20 in main main.c:62
        ruby#13 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
eregon pushed a commit that referenced this pull request Nov 22, 2026
Array#sort! makes ary shared and tmp the shared root. The assumption is
that tmp is never referred to by any object other than ary. However, if
ary is sliced, than another array would also refer to tmp. This can cause
an use-after-free when ary is also freed.

For example, the following code demonstrates an use-after-free:

    a = Array.new(1000) { |i| "s#{i}" }
    x = nil
    a.sort! do |p, q|
      x ||= a[0, 500]
      p <=> q
    end
    a.clear
    p x[0], x[499]

On regular builds, this outputs corrupted values. On ASAN, it outputs the
following error:

    ERROR: AddressSanitizer: heap-use-after-free on address 0x625000124908 at pc 0x000104d25924 bp 0x00016b74d450 sp 0x00016b74d448
    READ of size 8 at 0x625000124908 thread T0
        #0 0x000104d25920 in rb_ary_entry_internal array.h:71
        #1 0x000104d25920 in vm_opt_aref vm_insnhelper.c:7381
        #2 0x000104d25920 in vm_exec_core insns.def:1628
        #3 0x000104d07188 in rb_vm_exec vm.c:2930
        ruby#4 0x00010489c200 in rb_ec_exec_node eval.c:300
        ruby#5 0x00010489c028 in ruby_run_node eval.c:338
        ruby#6 0x0001046b0c20 in rb_main main.c:42
        ruby#7 0x0001046b0c20 in main main.c:62
        ruby#8 0x0001916bcd78 in start+0x19f8 (dyld:arm64e+0x31d78)
@eregon eregon closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant