Skip to content
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
3 changes: 3 additions & 0 deletions .github/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Only scan ADI's own sources. engine/ vendors the full ONNXRuntime tree —
# upstream code we neither maintain nor want CodeQL findings for (and it is
# far too large to scan on every run).
# clitools/stb vendors the stb single-header libraries (nothings/stb) —
# likewise third-party code we do not patch; report those findings upstream.
name: "ADI CodeQL config"

paths:
Expand All @@ -11,3 +13,4 @@ paths:
paths-ignore:
- engine
- sd
- clitools/stb
49 changes: 45 additions & 4 deletions clitools/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
#include <windows.h>
#endif

// shell-free subprocess spawn (fp16 converter): avoids system()/shell injection
#ifdef _WIN32
#include <process.h>
#else
#include <cerrno>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif

#include "adi.h"

#define STB_IMAGE_IMPLEMENTATION
Expand Down Expand Up @@ -234,6 +244,36 @@ static uint64_t component_bytes(const std::string &onnx_path_) {
return file_bytes(onnx_path_) + file_bytes(parent_dir(onnx_path_) + "/model.onnx_data");
}

// Run a subprocess with an explicit argv (no shell involved), so paths and
// env-provided values can never be reinterpreted as shell syntax.
// Returns the child's exit code, or -1 on spawn/wait failure.
static int run_subprocess_no_shell(const std::string &program_, const std::vector<std::string> &args_) {
std::vector<std::string> argv_storage_ = {program_};
argv_storage_.insert(argv_storage_.end(), args_.begin(), args_.end());
#ifdef _WIN32
std::vector<const char *> argv_;
for (auto &s_ : argv_storage_) argv_.push_back(s_.c_str());
argv_.push_back(nullptr);
intptr_t rc_ = _spawnvp(_P_WAIT, program_.c_str(), argv_.data());
return (rc_ == -1) ? -1 : int(rc_);
#else
pid_t pid_ = fork();
if (pid_ < 0) return -1;
if (pid_ == 0) {
std::vector<char *> argv_;
for (auto &s_ : argv_storage_) argv_.push_back(const_cast<char *>(s_.c_str()));
argv_.push_back(nullptr);
execvp(program_.c_str(), argv_.data());
_exit(127); // exec failed
}
int status_ = 0;
while (waitpid(pid_, &status_, 0) < 0) {
if (errno != EINTR) return -1;
}
return WIFEXITED(status_) ? WEXITSTATUS(status_) : -1;
#endif
}

static void resolve_precision(CommandLineInput &params) {
if (params.sd_precision != "auto" && params.sd_precision != "fp32" && params.sd_precision != "fp16") {
fprintf(stderr, "error: --precision must be one of: auto | fp32 | fp16\n");
Expand Down Expand Up @@ -280,9 +320,10 @@ static void resolve_precision(CommandLineInput &params) {
std::string fp16_onnx_ = set_dir_ + "-fp16/" + comp_name_ + "/model.onnx";
if (file_bytes(fp16_onnx_) == 0) {
printf("[precision] converting %s -> fp16 (one-time, cached)\n", comp_dir_.c_str());
std::string cmd_ = "\"" + python_cmd_ + "\" \"" + tool_path_ + "\" \"" +
comp_dir_ + "\" \"" + (set_dir_ + "-fp16/" + comp_name_) + "\"";
int rc_ = system(cmd_.c_str());
int rc_ = run_subprocess_no_shell(
python_cmd_,
{tool_path_, comp_dir_, set_dir_ + "-fp16/" + comp_name_}
);
if (rc_ != 0 || file_bytes(fp16_onnx_) == 0) {
fprintf(stderr, "[precision] WARN: fp16 conversion failed for %s, keeping fp32\n",
comp_dir_.c_str());
Expand Down Expand Up @@ -968,7 +1009,7 @@ static void read_image(const CommandLineInput &params, uint8_t** image_data){
int resized_height = int(params.sd_input_height);
int resized_width = int(params.sd_input_width);

auto *resized_image_buffer = (uint8_t *) malloc(resized_height * resized_width * 3);
auto *resized_image_buffer = (uint8_t *) malloc(size_t(resized_height) * size_t(resized_width) * 3u);
if (resized_image_buffer == nullptr) {
fprintf(stderr, "error: allocate memory for resize input image\n");
free((*image_data));
Expand Down
4 changes: 2 additions & 2 deletions source/units/model_clip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Clip::~Clip(){
void Clip::generate_output(std::vector<Tensor> &output_tensors_) {
{
std::vector<float> output_hidden_(
sd_clip_config.sd_tokenizer_config.avail_token_size *
sd_clip_config.sd_tokenizer_config.major_hidden_dim
size_t(sd_clip_config.sd_tokenizer_config.avail_token_size) *
size_t(sd_clip_config.sd_tokenizer_config.major_hidden_dim)
);
TensorShape hidden_shape_ = {
1,
Expand Down
Loading