Skip to content

[flang] Fix const cast issue in FreeMemory function call in execute_command_line #77906

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

Merged
merged 4 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions flang/docs/Intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ used in constant expressions have currently no folding support at all.

#### Usage and Info

- **Standard:** Fortran 2008 and later, specified in 16.9.73
- **Standard:** Fortran 2008 and later, specified in subclause 16.9.73
- **Class:** Subroutine
- **Syntax:** `CALL EXECUTE_COMMAND_LINE(COMMAND [, WAIT, EXITSTAT, CMDSTAT, CMDMSG ])`
- **Arguments:**
Expand All @@ -862,28 +862,50 @@ used in constant expressions have currently no folding support at all.

#### Implementation Specifics

- **`COMMAND`:**
- Must be preset.
##### `COMMAND`:

- **`WAIT`:**
- If set to `false`, the command is executed asynchronously. If not preset or set to `false`, it is executed synchronously.
- Sync: achieved by passing command into `std::system` on all systems.
- Async: achieved by calling a `fork()` on POSIX-compatible systems, or `CreateProcess()` on Windows.
- Must be preset.

- **`CMDSTAT`:**
##### `WAIT`:

- If set to `false`, the command is executed asynchronously.
- If not preset or set to `true`, it is executed synchronously.
- Synchronous execution is achieved by passing the command into `std::system` on all systems.
- Asynchronous execution is achieved by calling `fork()` on POSIX-compatible systems or `CreateProcess()` on Windows.

##### `EXITSTAT`:

- Synchronous execution:
- Inferred by the return value of `std::system(cmd)`.
- On POSIX-compatible systems: return value is first passed into `WEXITSTATUS(status)`, then assigned to `EXITSTAT`.
- On Windows, the value is directly assigned as the return value of `std::system()`.
- Asynchronous execution:
- Value is not modified.

##### `CMDSTAT`:

- Synchronous execution:
- -2: No error condition occurs, but `WAIT` is present with the value `false`, and the processor does not support asynchronous execution.
- -1: The processor does not support command line execution.
- \+ (positive value): An error condition occurs.
- 1: Fork Error, where `pid_t < 0`, would only occur on POSIX-compatible systems.
- 2: Execution Error, a command exits with status -1.
- 3: Invalid Command Error, determined by the exit code depending on the system.
- On Windows, if the exit code is 1.
- On POSIX-compatible systems, if the exit code is 127 or 126.
- 4: Signal error, either it is stopped or killed by signal, would only occur on POSIX-compatible systems.
- 1: Fork Error (occurs only on POSIX-compatible systems).
- 2: Execution Error (command exits with status -1).
- 3: Invalid Command Error (determined by the exit code depending on the system).
- On Windows: exit code is 1.
- On POSIX-compatible systems: exit code is 127 or 126.
- 4: Signal error (either stopped or killed by signal, occurs only on POSIX-compatible systems).
- 0: Otherwise.
- Asynchronous execution:
- 0 will always be assigned.

##### `CMDMSG`:

- **`CMDMSG`:**
- If an error condition occurs, it is assigned an explanatory message. Otherwise, it remains unchanged.
- Synchronous execution:
- If an error condition occurs, it is assigned an explanatory message; otherwise, it remains unchanged.
- If a condition occurs that would assign a nonzero value to `CMDSTAT` but the `CMDSTAT` variable is not present, error termination is initiated (applies to both POSIX-compatible systems and Windows).
- Asynchronous execution:
- The value is unchanged.
- If a condition occurs that would assign a nonzero value to `CMDSTAT` but the `CMDSTAT` variable is not present, error termination is initiated.
- On POSIX-compatible systems, this applies to both synchronous and asynchronous error termination. When the execution mode is set to async with error termination, the child process (async process) will be terminated with no effect on the parent process (continues).
- On Windows, this only applies to synchronous error termination.
- On POSIX-compatible systems, the child process (async process) will be terminated with no effect on the parent process (continues).
- On Windows, error termination is not initiated.

2 changes: 1 addition & 1 deletion flang/runtime/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
const Descriptor *exitstat, const Descriptor *cmdstat,
const Descriptor *cmdmsg, const char *sourceFile, int line) {
Terminator terminator{sourceFile, line};
const char *newCmd{EnsureNullTerminated(
char *newCmd{EnsureNullTerminated(
command.OffsetElement(), command.ElementBytes(), terminator)};

if (exitstat) {
Expand Down
4 changes: 2 additions & 2 deletions flang/runtime/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from) {
ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous());
}

RT_API_ATTRS const char *EnsureNullTerminated(
const char *str, std::size_t length, Terminator &terminator) {
RT_API_ATTRS char *EnsureNullTerminated(
char *str, std::size_t length, Terminator &terminator) {
if (std::memchr(str, '\0', length) == nullptr) {
char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
std::memcpy(newCmd, str, length);
Expand Down
4 changes: 2 additions & 2 deletions flang/runtime/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from);
// size memory for null-terminator if necessary. Returns the original or a newly
// allocated null-terminated string (responsibility for deallocation is on the
// caller).
RT_API_ATTRS const char *EnsureNullTerminated(
const char *str, std::size_t length, Terminator &terminator);
RT_API_ATTRS char *EnsureNullTerminated(
char *str, std::size_t length, Terminator &terminator);

RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value);

Expand Down