RFR: 8325095: C2: bailout message broken: ResourceArea allocated string used after free

Johan Sjölen jsjolen at openjdk.org
Thu Feb 15 06:54:21 UTC 2024


On Mon, 5 Feb 2024 16:02:57 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

> **Problem**
> Bailout `failure_reason` strings used to be either `nullptr` or a static string. But with [JDK-8303951](https://bugs.openjdk.org/browse/JDK-8303951) I had introduced dynamic strings (e.g. formatted using `stringStream::as_string()`). These dynamic strings were ResourceArea allocated, and have a very limited life-span, i.e. withing the most recent ResourceMark scope. The pointer is passed to `Compile::_failure_reason`, which already might leave that ResourceMark scope, and the pointer is invalid. And then the pointer is further passed to `ciEnv::_failure_reason`. And finally, it even gets passed up to `CompileBroker::invoke_compiler_on_method`. When the string is finally printed for the bailout message, we already left original `ResourceMark` scope, and also the `Compile` and `ciEnv` scopes. The pointer now points to basically random data.
> 
> **Solution**
> Whenever a string is passed to an outer scope, I make a CHeap copy, and the outer scope is the owner of that copy.
> This way every scope is the owner of its own copy, and can allocate and deallocate according to its own strategy safely.
> 
> I introduced a utility class `CHeapStringHolder`:
>  - `set`: make local copy on CHeap.
>  - `clear`: free local copy. We before `set`, and in the destructor. Thus, when the holder goes out of scope, the memory is automatically freed.
> 
> We have these 4 scopes:
> - `ResourceMark`: It allocates from `ResourcArea`, and deallocation is taken care at the end of the ResourceMark scope.
> - `Compile`: We turn the `_failure_reason` from a `char*` into a `CHeapStringHolder`. We set `_failure_reason` while the `ResourceMark` is live.
> - `ciEnv`: We turn the `_failure_reason` from a `char*` into a `CHeapStringHolder`. We set `_failure_reason` while `Compile` is live.
> - `CompileTask`: We used to just set `failure_reason`, which assumes that the string is static or elsewhere managed. Now I use pattern available in other places of `CompileBroker::invoke_compiler_on_method`: duplicate the string with `os::strdup` from some other scope, and set `reason_on_C_heap = true`, which means that we are the owner of that copy, and are responsible for freeing it later. Not sure if that is a nice pattern, but I don't want to refactor that code and it does what I need it to do.

Hi,

I don't agree with the way this patch works and I think we're bound for trouble. Why not store an instance of this class inside of the task instead? It takes a bit more space (136 bytes) but it actually handles its own lifetime.

```c++
class CompilationFailure {
  stringStream failure_reason;
  CompilationFailure() : failure_reason() {}
  void replace_failure(const char* reason) { 
    failure_reason.reset();
    failure_reason.print_raw(reason);
  }
  const stringStream& reason() const {
    return failure_reason;
  }
}

I don't like it because you're leaking memory over the lifetime of the `CompileTask` and because there are simpler solutions, as we only ever store one failure reason.

>Your suggestion would require that there is only a single failure_reason around at any given time. But I'm not sure if there is really only a single reason around, at least they are set at different places, and can be replaced... That is why I was looking for something that can keep the lifetime of multiple strings.

How would multiple strings be accessed through one const pointer :-)?

>Essencially, your solution would just be a better version of what we have with failure_reason_on_C_heap.

Yes, the lifetime is managed correctly and automatically by the RAII object. Plus, the bool can be removed. One issue is that you will probably have to add a bool `has_reason` to the class  I suggested, we can't depend on something being `nullptr` to indicate absence of reason anymore.

You don't need a new string class, you can use `stringStream` as that will hold the string memory and its local copy. I've looked at the code and we have 3 separate pointers to a failure reason (as you said), one in ciEnv, one in Compile, and one in CompileTask. Instead of using pointers, use the class I suggested and have these 3 have ownership of their own copies of these strings. The allocations' cost is irrelevant :-).

Depending on the usage you'll just have to use `.freeze()` or `.base()` which will give you the underlying string array, no copying needed!

-------------

PR Comment: https://git.openjdk.org/jdk/pull/17710#issuecomment-1929195534
PR Comment: https://git.openjdk.org/jdk/pull/17710#issuecomment-1929295815
PR Comment: https://git.openjdk.org/jdk/pull/17710#issuecomment-1929361362
PR Comment: https://git.openjdk.org/jdk/pull/17710#issuecomment-1929388369


More information about the hotspot-dev mailing list