Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues due to OpenAI module breaking backwards-compatibility #27

Merged
merged 2 commits into from
Jan 9, 2024

Conversation

nicovank
Copy link
Collaborator

@nicovank nicovank commented Jan 8, 2024

Recent OpenAI modules on PyPI have broken backwards-compatibility. Fix issues, and pin version to >=1.6.1 (current PyPI version).

Tested on GDB and LLDB.

vscode ➜ /workspaces/ChatDBG/ChatDBG (upgrade-openai) $ clang++ -g ./test/test-overflow.cpp 
vscode ➜ /workspaces/ChatDBG/ChatDBG (upgrade-openai) $ gdb ./a.out 
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
(gdb-ChatDBG) run
Starting program: /workspaces/ChatDBG/ChatDBG/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
TEST 1
TEST 0
TEST 0
TEST 0

Program received signal SIGSEGV, Segmentation fault.
0x0000aaaaaaaa0b10 in foo (n=4, b=1) at ./test/test-overflow.cpp:7
7         cout << "TEST " << x[n * 10000] << endl;
(gdb-ChatDBG) why
The root cause of this error is a segmentation fault (SIGSEGV) which occurs when
trying to access memory that is not allocated or accessible. In this case, the
error is caused by trying to access an element of the array `x` that is out of
bounds at line 7 of the `foo` function.

To fix this error, we need to ensure that the index `n * 10000` is within the
bounds of the array `x`. One way to fix it is by adding a check to ensure that
the index is not exceeding the array size before accessing the element.

Here's an updated code with the fix:

```cpp
#include <iostream>
using namespace std;

int x[] = { 1, 2, 3, 4, 5 };

void foo(int n, float b) {
  if (n * 10000 < sizeof(x)/sizeof(x[0])) {
    cout << "TEST " << x[n * 10000] << endl;
  }
}
        
int main()
{
  for (auto i = 0; i < 10; i++) {
    foo(i, 1.0);
  }
  return 0;
}
```

By adding the `if` condition to check if the index is within bounds, we ensure
that the program does not access elements outside the array, avoiding the
segmentation fault.

(Total cost: approximately $0.00 USD.)
(gdb-ChatDBG) 
quit
A debugging session is active.

        Inferior 1 [process 16435] will be killed.

Quit anyway? (y or n) y
vscode ➜ /workspaces/ChatDBG/ChatDBG (upgrade-openai) $ lldb-17 ./a.out 
(ChatDBG lldb) target create "./a.out"
Current executable set to '/workspaces/ChatDBG/ChatDBG/a.out' (aarch64).
(ChatDBG lldb) run
Process 16565 launched: '/workspaces/ChatDBG/ChatDBG/a.out' (aarch64)
TEST 1
TEST 0
TEST 0
TEST 0
Process 16565 stopped
* thread #1, name = 'a.out', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0xaaaaaaad8158)
    frame #0: 0x0000aaaaaaaa0b10 a.out`foo(n=4, b=1) at test-overflow.cpp:7:22
   4    int x[] = { 1, 2, 3, 4, 5 };
   5   
   6    void foo(int n, float b) {
-> 7      cout << "TEST " << x[n * 10000] << endl;
   8    }
   9   
   10   int main()
(ChatDBG lldb) why
The root cause of this error is a segmentation fault (SIGSEGV) caused by
accessing an out-of-bounds index of the array `x`.

In frame 1, the `foo()` function is called from `main()` in a for loop. The loop
iterates from 0 to 9, calling `foo(i, 1.0)`. On the last iteration, `i` is 4,
and this value is passed as the parameter `n` to `foo()`.

In frame 0, within the `foo()` function, an attempt is made to access the
element at index `n * 10000` of the array `x`. Since `n` is 4, this computes an
index of 40000, which exceeds the size of the `x` array (size 5). This results
in accessing memory outside the bounds of the array, causing a segmentation
fault.

To fix this error, we should ensure that the index used to access the `x` array
is within its bounds. In this case, we should add a check to ensure that `n *
10000` is a valid index for the `x` array. We can modify the code in frame 0 as
follows:

```
void foo(int n, float b) {
  if (n * 10000 >= 0 && n * 10000 < sizeof(x)/sizeof(x[0])) {
    cout << "TEST " << x[n * 10000] << endl;
  } else {
    cout << "Invalid index" << endl;
  }
}
```

By checking if the index is within the bounds of the array, we can prevent the
segmentation fault and handle the situation where the index is invalid.

(Total cost: approximately $0.00 USD.)

PS: still some code duplication that should be fixed.

@nicovank nicovank requested a review from khlevin January 8, 2024 17:42
Copy link
Contributor

@khlevin khlevin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works.

@khlevin khlevin merged commit f630e12 into main Jan 9, 2024
2 checks passed
@nicovank nicovank deleted the upgrade-openai branch January 11, 2024 21:50
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.

2 participants