Skip to content

Commit

Permalink
8299635: Hotspot update for deprecated sprintf in Xcode 14
Browse files Browse the repository at this point in the history
Reviewed-by: sgehwolf, andrew
Backport-of: e80b5ea448c715519d14e238321ceb5ec40b37f4
  • Loading branch information
gdams committed Jul 5, 2024
1 parent 7144027 commit faf55dd
Show file tree
Hide file tree
Showing 35 changed files with 98 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/hotspot/cpu/arm/arm.ad
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ uint MachConstantBaseNode::size(PhaseRegAlloc*) const {
#ifndef PRODUCT
void MachConstantBaseNode::format(PhaseRegAlloc* ra_, outputStream* st) const {
char reg[128];
ra_->dump_register(this, reg);
ra_->dump_register(this, reg, sizeof(reg));
st->print("MOV_SLOW &constanttable,%s\t! constant table base", reg);
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/cpu/ppc/ppc.ad
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ uint MachNopNode::size(PhaseRegAlloc *ra_) const {
void BoxLockNode::format(PhaseRegAlloc *ra_, outputStream *st) const {
int offset = ra_->reg2offset(in_RegMask(0).find_first_elem());
char reg_str[128];
ra_->dump_register(this, reg_str);
ra_->dump_register(this, reg_str, sizeof(reg_str));
st->print("ADDI %s, SP, %d \t// box node", reg_str, offset);
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/os/aix/attachListener_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ int AixAttachListener::init() {
//
AixAttachOperation* AixAttachListener::read_request(int s) {
char ver_str[8];
sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
os::snprintf_checked(ver_str, sizeof(ver_str), "%d", ATTACH_PROTOCOL_VER);

// The request is a sequence of strings so we first figure out the
// expected count and the maximum possible length of the request.
Expand Down Expand Up @@ -311,7 +311,7 @@ AixAttachOperation* AixAttachListener::read_request(int s) {
if ((strlen(buf) != strlen(ver_str)) ||
(atoi(buf) != ATTACH_PROTOCOL_VER)) {
char msg[32];
sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
os::snprintf_checked(msg, sizeof(msg), "%d\n", ATTACH_ERROR_BADVERSION);
write_fully(s, msg, strlen(msg));
return NULL;
}
Expand Down Expand Up @@ -441,7 +441,7 @@ void AixAttachOperation::complete(jint result, bufferedStream* st) {

// write operation result
char msg[32];
sprintf(msg, "%d\n", result);
os::snprintf_checked(msg, sizeof(msg), "%d\n", result);
int rc = AixAttachListener::write_fully(this->socket(), msg, strlen(msg));

// write any result data
Expand Down Expand Up @@ -544,7 +544,7 @@ bool AttachListener::is_init_trigger() {
char fn[PATH_MAX + 1];
int ret;
struct stat64 st;
sprintf(fn, ".attach_pid%d", os::current_process_id());
os::snprintf_checked(fn, sizeof(fn), ".attach_pid%d", os::current_process_id());
RESTARTABLE(::stat64(fn, &st), ret);
if (ret == -1) {
log_trace(attach)("Failed to find attach file: %s, trying alternate", fn);
Expand Down
9 changes: 5 additions & 4 deletions src/hotspot/os/aix/os_aix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ void os::init_system_properties_values() {
#endif
#define EXTENSIONS_DIR "/lib/ext"

// Buffer that fits several sprintfs.
// Buffer that fits several snprintfs.
// Note that the space for the trailing null is provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
Expand Down Expand Up @@ -584,13 +584,14 @@ void os::init_system_properties_values() {

// Concatenate user and invariant part of ld_library_path.
// That's +1 for the colon and +1 for the trailing '\0'.
char *ld_library_path = NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + sizeof(DEFAULT_LIBPATH) + 1, mtInternal);
sprintf(ld_library_path, "%s%s" DEFAULT_LIBPATH, v, v_colon);
size_t pathsize = strlen(v) + 1 + sizeof(DEFAULT_LIBPATH) + 1;
char *ld_library_path = NEW_C_HEAP_ARRAY(char, pathsize, mtInternal);
os::snprintf_checked(ld_library_path, pathsize, "%s%s" DEFAULT_LIBPATH, v, v_colon);
Arguments::set_library_path(ld_library_path);
FREE_C_HEAP_ARRAY(char, ld_library_path);

// Extensions directories.
sprintf(buf, "%s" EXTENSIONS_DIR, Arguments::get_java_home());
os::snprintf_checked(buf, bufsize, "%s" EXTENSIONS_DIR, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);

FREE_C_HEAP_ARRAY(char, buf);
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/os/linux/attachListener_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ int LinuxAttachListener::init() {
//
LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
char ver_str[8];
sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
os::snprintf_checked(ver_str, sizeof(ver_str), "%d", ATTACH_PROTOCOL_VER);

// The request is a sequence of strings so we first figure out the
// expected count and the maximum possible length of the request.
Expand Down Expand Up @@ -290,7 +290,7 @@ LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
if ((strlen(buf) != strlen(ver_str)) ||
(atoi(buf) != ATTACH_PROTOCOL_VER)) {
char msg[32];
sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
os::snprintf_checked(msg, sizeof(msg), "%d\n", ATTACH_ERROR_BADVERSION);
write_fully(s, msg, strlen(msg));
return NULL;
}
Expand Down Expand Up @@ -410,7 +410,7 @@ void LinuxAttachOperation::complete(jint result, bufferedStream* st) {

// write operation result
char msg[32];
sprintf(msg, "%d\n", result);
os::snprintf_checked(msg, sizeof(msg), "%d\n", result);
int rc = LinuxAttachListener::write_fully(this->socket(), msg, strlen(msg));

// write any result data
Expand Down Expand Up @@ -512,7 +512,7 @@ bool AttachListener::is_init_trigger() {
char fn[PATH_MAX + 1];
int ret;
struct stat64 st;
sprintf(fn, ".attach_pid%d", os::current_process_id());
os::snprintf_checked(fn, sizeof(fn), ".attach_pid%d", os::current_process_id());
RESTARTABLE(::stat64(fn, &st), ret);
if (ret == -1) {
log_trace(attach)("Failed to find attach file: %s, trying alternate", fn);
Expand Down
12 changes: 5 additions & 7 deletions src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ void os::init_system_properties_values() {
#define SYS_EXT_DIR "/usr/java/packages"
#define EXTENSIONS_DIR "/lib/ext"

// Buffer that fits several sprintfs.
// Buffer that fits several snprintfs.
// Note that the space for the colon and the trailing null are provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
Expand Down Expand Up @@ -485,17 +485,15 @@ void os::init_system_properties_values() {
const char *v_colon = ":";
if (v == NULL) { v = ""; v_colon = ""; }
// That's +1 for the colon and +1 for the trailing '\0'.
char *ld_library_path = NEW_C_HEAP_ARRAY(char,
strlen(v) + 1 +
sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1,
mtInternal);
sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
size_t pathsize = strlen(v) + 1 + sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1;
char *ld_library_path = NEW_C_HEAP_ARRAY(char, pathsize, mtInternal);
os::snprintf_checked(ld_library_path, pathsize, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
Arguments::set_library_path(ld_library_path);
FREE_C_HEAP_ARRAY(char, ld_library_path);
}

// Extensions directories.
sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
os::snprintf_checked(buf, bufsize, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);

FREE_C_HEAP_ARRAY(char, buf);
Expand Down
5 changes: 3 additions & 2 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,9 @@ void os::init_system_properties_values() {
char path[MAX_PATH];
char buf[2 * MAX_PATH + 2 * sizeof(EXT_DIR) + sizeof(PACKAGE_DIR) + 1];
GetWindowsDirectory(path, MAX_PATH);
sprintf(buf, "%s%s;%s%s%s", Arguments::get_java_home(), EXT_DIR,
path, PACKAGE_DIR, EXT_DIR);
os::snprintf_checked(buf, sizeof(buf), "%s%s;%s%s%s",
Arguments::get_java_home(), EXT_DIR,
path, PACKAGE_DIR, EXT_DIR);
Arguments::set_ext_dirs(buf);
}
#undef EXT_DIR
Expand Down
10 changes: 5 additions & 5 deletions src/hotspot/share/adlc/formssel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ bool InstructForm::check_branch_variant(ArchDesc &AD, InstructForm *short_branch
void InstructForm::rep_var_format(FILE *fp, const char *rep_var) {
// Handle special constant table variables.
if (strcmp(rep_var, "constanttablebase") == 0) {
fprintf(fp, "char reg[128]; ra->dump_register(in(mach_constant_base_node_input()), reg);\n");
fprintf(fp, "char reg[128]; ra->dump_register(in(mach_constant_base_node_input()), reg, sizeof(reg));\n");
fprintf(fp, " st->print(\"%%s\", reg);\n");
return;
}
Expand Down Expand Up @@ -2503,15 +2503,15 @@ void OperandForm::int_format(FILE *fp, FormDict &globals, uint index) {
strcmp(ideal_type(globalAD->globalNames()), "RegFlags") == 0)) {
// !!!!! !!!!!
fprintf(fp," { char reg_str[128];\n");
fprintf(fp," ra->dump_register(node,reg_str);\n");
fprintf(fp," ra->dump_register(node,reg_str, sizeof(reg_str));\n");
fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
fprintf(fp," }\n");
} else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
format_constant( fp, index, dtype );
} else if (ideal_to_sReg_type(_ident) != Form::none) {
// Special format for Stack Slot Register
fprintf(fp," { char reg_str[128];\n");
fprintf(fp," ra->dump_register(node,reg_str);\n");
fprintf(fp," ra->dump_register(node,reg_str, sizeof(reg_str));\n");
fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
fprintf(fp," }\n");
} else {
Expand All @@ -2532,7 +2532,7 @@ void OperandForm::ext_format(FILE *fp, FormDict &globals, uint index) {
fprintf(fp," { char reg_str[128];\n");
fprintf(fp," ra->dump_register(node->in(idx");
if ( index != 0 ) fprintf(fp, "+%d",index);
fprintf(fp, "),reg_str);\n");
fprintf(fp, "),reg_str,sizeof(reg_str));\n");
fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
fprintf(fp," }\n");
} else if (_matrule && (dtype = _matrule->is_base_constant(globals)) != Form::none) {
Expand All @@ -2542,7 +2542,7 @@ void OperandForm::ext_format(FILE *fp, FormDict &globals, uint index) {
fprintf(fp," { char reg_str[128];\n");
fprintf(fp," ra->dump_register(node->in(idx");
if ( index != 0 ) fprintf(fp, "+%d",index);
fprintf(fp, "),reg_str);\n");
fprintf(fp, "),reg_str,sizeof(reg_str));\n");
fprintf(fp," st->print(\"%cs\",reg_str);\n",'%');
fprintf(fp," }\n");
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/callnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static void format_helper( PhaseRegAlloc *regalloc, outputStream* st, Node *n, c
if (regalloc->node_regs_max_index() > 0 &&
OptoReg::is_valid(regalloc->get_reg_first(n))) { // Check for undefined
char buf[50];
regalloc->dump_register(n,buf);
regalloc->dump_register(n,buf,sizeof(buf));
st->print(" %s%d]=%s",msg,i,buf);
} else { // No register, but might be constant
const Type *t = n->bottom_type();
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/cfgnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,7 @@ void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
st->print(", ");
}
char buf[128];
ra->dump_register(n, buf);
ra->dump_register(n, buf, sizeof(buf));
st->print("%s", buf);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/hotspot/share/opto/chaitin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2156,42 +2156,42 @@ void PhaseChaitin::dump_simplified() const {
tty->cr();
}

static char *print_reg(OptoReg::Name reg, const PhaseChaitin* pc, char* buf) {
static char *print_reg(OptoReg::Name reg, const PhaseChaitin* pc, char* buf, size_t buf_size) {
if ((int)reg < 0)
sprintf(buf, "<OptoReg::%d>", (int)reg);
os::snprintf_checked(buf, buf_size, "<OptoReg::%d>", (int)reg);
else if (OptoReg::is_reg(reg))
strcpy(buf, Matcher::regName[reg]);
else
sprintf(buf,"%s + #%d",OptoReg::regname(OptoReg::c_frame_pointer),
os::snprintf_checked(buf, buf_size, "%s + #%d",OptoReg::regname(OptoReg::c_frame_pointer),
pc->reg2offset(reg));
return buf+strlen(buf);
}

// Dump a register name into a buffer. Be intelligent if we get called
// before allocation is complete.
char *PhaseChaitin::dump_register(const Node* n, char* buf) const {
char *PhaseChaitin::dump_register(const Node* n, char* buf, size_t buf_size) const {
if( _node_regs ) {
// Post allocation, use direct mappings, no LRG info available
print_reg( get_reg_first(n), this, buf );
print_reg( get_reg_first(n), this, buf, buf_size);
} else {
uint lidx = _lrg_map.find_const(n); // Grab LRG number
if( !_ifg ) {
sprintf(buf,"L%d",lidx); // No register binding yet
os::snprintf_checked(buf, buf_size, "L%d",lidx); // No register binding yet
} else if( !lidx ) { // Special, not allocated value
strcpy(buf,"Special");
} else {
if (lrgs(lidx)._is_vector) {
if (lrgs(lidx).mask().is_bound_set(lrgs(lidx).num_regs()))
print_reg( lrgs(lidx).reg(), this, buf ); // a bound machine register
print_reg( lrgs(lidx).reg(), this, buf, buf_size); // a bound machine register
else
sprintf(buf,"L%d",lidx); // No register binding yet
os::snprintf_checked(buf, buf_size, "L%d",lidx); // No register binding yet
} else if( (lrgs(lidx).num_regs() == 1)
? lrgs(lidx).mask().is_bound1()
: lrgs(lidx).mask().is_bound_pair() ) {
// Hah! We have a bound machine register
print_reg( lrgs(lidx).reg(), this, buf );
print_reg( lrgs(lidx).reg(), this, buf, buf_size);
} else {
sprintf(buf,"L%d",lidx); // No register binding yet
os::snprintf_checked(buf, buf_size, "L%d",lidx); // No register binding yet
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/chaitin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ class PhaseChaitin : public PhaseRegAlloc {

public:
void dump_frame() const;
char *dump_register(const Node* n, char* buf) const;
char *dump_register(const Node* n, char* buf, size_t buf_size) const;
private:
static void print_chaitin_statistics();
#endif // not PRODUCT
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/share/opto/idealGraphPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) {
if (index >= 10) {
print_prop(short_name, "PA");
} else {
sprintf(buffer, "P%d", index);
os::snprintf_checked(buffer, sizeof(buffer), "P%d", index);
print_prop(short_name, buffer);
}
} else if (strcmp(node->Name(), "IfTrue") == 0) {
Expand All @@ -524,7 +524,7 @@ void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) {

// max. 2 chars allowed
if (value >= -9 && value <= 99) {
sprintf(buffer, "%d", value);
os::snprintf_checked(buffer, sizeof(buffer), "%d", value);
print_prop(short_name, buffer);
} else {
print_prop(short_name, "I");
Expand All @@ -538,7 +538,7 @@ void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) {

// max. 2 chars allowed
if (value >= -9 && value <= 99) {
sprintf(buffer, JLONG_FORMAT, value);
os::snprintf_checked(buffer, sizeof(buffer), JLONG_FORMAT, value);
print_prop(short_name, buffer);
} else {
print_prop(short_name, "L");
Expand Down Expand Up @@ -601,7 +601,7 @@ void IdealGraphPrinter::visit_node(Node *n, bool edges, VectorSet* temp_set) {

if (_chaitin && _chaitin != (PhaseChaitin *)((intptr_t)0xdeadbeef)) {
buffer[0] = 0;
_chaitin->dump_register(node, buffer);
_chaitin->dump_register(node, buffer, sizeof(buffer));
print_prop("reg", buffer);
uint lrg_id = 0;
if (node->_idx < _chaitin->_lrg_map.size()) {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/regalloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class PhaseRegAlloc : public Phase {
static int _max_framesize;

virtual void dump_frame() const = 0;
virtual char *dump_register( const Node *n, char *buf ) const = 0;
virtual char *dump_register( const Node *n, char *buf, size_t buf_size) const = 0;
static void print_statistics();
#endif
};
Expand Down
Loading

1 comment on commit faf55dd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.