eecs280staff / tutorials

Tools and tutorials
https://eecs280staff.github.io/tutorials/
Other
5 stars 4 forks source link

CodeLLDB and ASAN #113

Closed awdeorio closed 1 year ago

awdeorio commented 1 year ago

An Address Sanitizer error causes the CodeLLDB extension to show binary code. The issue applies to macOS.

I expected it to show the line where the error was identified. EDIT: If you use the stack trace, you can find the line that caused the problem. It would just be nice for the debugger to stop and highlight the line that caused the problem.

Replicate

Run macOS with the CodeLLDB extension.

Example program with use-after-free bug.

#include <iostream>
using namespace std;

int main() {
  int * p = new int;
  delete p;
  cout << *p << endl;
}

Compile and run, see use-after-free problem identified by ASAN.

$ g++ -Wall -Werror -pedantic -g --std=c++11 -Wno-sign-compare -Wno-comment -fsanitize=address -fsanitize=undefined main.cpp -o main.exe
$ ./main.exe
=================================================================
==84387==ERROR: AddressSanitizer: heap-use-after-free on address 0x000106f006f0 at pc 0x000104af2fa0 bp 0x00016b30f180 sp 0x00016b30f178
...

VSCode launch.json with CodeLLDB extension.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/main.exe",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

Run in VSCode, here's what you see.

screenshot-codelldb

Comparison: Microsoft extension

Here's what we get with the Microsoft C++ extension with the optional ASAN_OPTIONS set. This is on macOS with LLDB.

The VS Code debugger hangs due to the undefined behavior. This is a known bug in the VS Code MS C++ extension. We chose to recommend CodeLLDB on macOS due to known bugs when code under test contains undefined behavior.

MS extension launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                  "name": "ASAN_OPTIONS",
                  "value": "abort_on_error=1:detect_leaks=0"
                }
              ],
            "externalConsole": false,
            "MIMode": "lldb"
        }

    ]
}
awdeorio commented 1 year ago

Here's the CodeLLDB Manual. The option lldb.showDisassembly at least prevents the binary display. Related SO post about how to set that.