oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.89k stars 2.74k forks source link

Memory allocation failure in `btoa` #12346

Closed mofelee closed 3 months ago

mofelee commented 3 months ago

How can we reproduce the crash?

fetch('https://api.example.com/data', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic ' + btoa('username:password')
  }
})

Relevant log output

00:01:01.892  ============================================================
00:01:01.892  Bun v1.1.18 (5a0b9352) Linux x64 (baseline)
00:01:01.892  Linux Kernel v5.15.0 | glibc v2.31
00:01:01.892  Args: "bun" "run" "create_index.ts"
00:01:01.892  Features: jsc Bun.stdin(2) fetch tsconfig 
00:01:01.892  Builtins: "bun:main" "node:process" "node:string_decoder" "node:util/types" 
00:01:01.892  Elapsed: 69ms | User: 88ms | Sys: 19ms
00:01:01.892  RSS: 1.07GB | Peak: 71.21MB | Commit: 1.07GB | Faults: 0
00:01:01.892  
00:01:01.892  panic(main thread): Segmentation fault at address 0x0
00:01:01.892  oh no: Bun has crashed. This indicates a bug in Bun, not your code.
00:01:01.892  
00:01:01.892  To send a redacted crash report to Bun's team,
00:01:01.892  please file a GitHub issue using the link below:
00:01:01.892  
00:01:01.892   https://bun.report/1.1.18/Br15a0b935AqgiggE2uw3mE+1mOw+9ghDmkkihD_ygmj9D_ygmj9D6l/i9D+7y78DA2AA
00:01:01.892  
00:01:01.892  Illegal instruction (core dumped)

Stack Trace (bun.report)

Bun v1.1.18 (5a0b935) on linux x86_64_baseline [RunCommand]

Segmentation fault at address 0x00000000

mofelee commented 3 months ago

I found that btoa('username:password') outputs empty string

console.log((`Basic ${btoa('elastic:elastic')}`).length) // output: 6
Jarred-Sumner commented 3 months ago

I'm unable to reproduce this. I tried on Linux x64 using the default build, the baseline build, and in canary. I also tried on macOS arm64.

Are you using a CPU emulator a VPS somewhere? What year is your CPU from? Potentially related: #3312

mofelee commented 3 months ago

@Jarred-Sumner

This code runs in a build pipeline triggered by Jenkins.

The pipeline runs on a k3s cluster, which runs on proxmox 8.2.4.

Jenkinsfile:

pipeline {
    agent {
        kubernetes {
            yaml '''
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: shell
    image: oven/bun:1.1.18-debian
    command:
    - sleep
    args:
    - infinity
'''
            defaultContainer 'shell'
        }
    }
    stages {
        stage('Main') {
            steps {
                checkout scm
                sh """
        bun install
                bun run index.ts
                """

            }
        }
    }
}
mofelee commented 3 months ago

@Jarred-Sumner

I reproduced this bug

  1. Create a k3s cluster
  2. sudo kubectl run -i -t bun --image=oven/bun:1.1.18-debian --restart=Never --command sh
  3. Create index.ts with console.log(btoa('hello'))
  4. bun run index.ts
mofelee commented 3 months ago

Sorry, I forgot to submit the CPU information

CPU(s): 48 x AMD EPYC 7402P 24-Core Processor (1 Socket)
Kernel Version: Linux 6.5.11-8-pve (2024-01-30T12:27Z)
Boot Mode: EFI
Manager Version: pve-manager/8.2.4/faa83925c9641325

~$ lscpu
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         40 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  4
  On-line CPU(s) list:   0-3
Vendor ID:               AuthenticAMD
  Model name:            Common KVM processor
    CPU family:          15
    Model:               6
    Thread(s) per core:  1
    Core(s) per socket:  2
    Socket(s):           2
    Stepping:            1
    BogoMIPS:            5589.50
    Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx lm rep_good nopl c
                         puid extd_apicid tsc_known_freq pni cx16 x2apic hypervisor cmp_legacy 3dnowprefetch vmmcall
Virtualization features: 
  Hypervisor vendor:     KVM
  Virtualization type:   full
Caches (sum of all):     
  L1d:                   256 KiB (4 instances)
  L1i:                   256 KiB (4 instances)
  L2:                    2 MiB (4 instances)
  L3:                    64 MiB (4 instances)
NUMA:                    
  NUMA node(s):          1
  NUMA node0 CPU(s):     0-3
Vulnerabilities:         
  Gather data sampling:  Not affected
  Itlb multihit:         Not affected
  L1tf:                  Not affected
  Mds:                   Not affected
  Meltdown:              Not affected
  Mmio stale data:       Not affected
  Retbleed:              Not affected
  Spec rstack overflow:  Not affected
  Spec store bypass:     Not affected
  Spectre v1:            Mitigation; usercopy/swapgs barriers and __user pointer sanitization
  Spectre v2:            Mitigation; Retpolines; STIBP disabled; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
  Srbds:                 Not affected
  Tsx async abort:       Not affected
Jarred-Sumner commented 3 months ago

Ah, Bun needs a CPU > 2008 (AVX, SSE 4.2)

Jarred-Sumner commented 3 months ago

Duplicate of #3312