Skip to content

Commit

Permalink
Add new constant called SYSTEM_PROCESS_NAME_BUFFER_LENGTH
Browse files Browse the repository at this point in the history
Previously we were using the value of C.PR_SET_NAME to limit the size of
the buffer used to write the process name into when calling
'nvmlSystemGetProcessName()'. While this technically wouldn't cause a
buffer overrun, it limited the length of the process name by a
(somewhat) arbitrary amount since C.PR_SET_NAME is not meant to indicate
a maximum length for a process name, but rather an arbitrary code for
telling 'prctl()' to set the name for a process (which has nothing to do
with its length).

It was a clear oversight to use this random value as the buffer length.
In the new implementation we simply hard code a new variable called
SYSTEM_PROCESS_NAME_BUFFER_LENGTH to 256 (which is well more than the
actual maximum process name length on linux). Having a larger value
won't hurt anything, and will make it more portable if / when we decide
to start supporting windows.

Signed-off-by: Kevin Klues <[email protected]>
  • Loading branch information
root authored and klueska committed Feb 16, 2021
1 parent 314e96d commit 72a47dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 19 additions & 0 deletions gen/nvml/const_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nvml

const (
SYSTEM_PROCESS_NAME_BUFFER_SIZE = 256
)
7 changes: 2 additions & 5 deletions gen/nvml/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

package nvml

// #include <sys/prctl.h>
import "C"

// nvml.SystemGetDriverVersion()
func SystemGetDriverVersion() (string, Return) {
Version := make([]byte, SYSTEM_DRIVER_VERSION_BUFFER_SIZE)
Expand Down Expand Up @@ -47,8 +44,8 @@ func SystemGetCudaDriverVersion_v2() (int, Return) {

// nvml.SystemGetProcessName()
func SystemGetProcessName(Pid int) (string, Return) {
Name := make([]byte, C.PR_SET_NAME)
ret := nvmlSystemGetProcessName(uint32(Pid), &Name[0], C.PR_SET_NAME)
Name := make([]byte, SYSTEM_PROCESS_NAME_BUFFER_SIZE)
ret := nvmlSystemGetProcessName(uint32(Pid), &Name[0], SYSTEM_PROCESS_NAME_BUFFER_SIZE)
return string(Name[:clen(Name)]), ret
}

Expand Down

0 comments on commit 72a47dc

Please sign in to comment.